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"
33 #include "diagnostic-core.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_CNEW (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_NEWVEC (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_CNEW (cp_lexer);
450 /* We do not own the buffer. */
451 lexer->buffer = NULL;
452 lexer->buffer_length = 0;
453 lexer->next_token = first == last ? &eof_token : first;
454 lexer->last_token = last;
456 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
457 CP_SAVED_TOKEN_STACK);
459 #ifdef ENABLE_CHECKING
460 /* Initially we are not debugging. */
461 lexer->debugging_p = false;
464 gcc_assert (lexer->next_token->type != CPP_PURGED);
468 /* Frees all resources associated with LEXER. */
471 cp_lexer_destroy (cp_lexer *lexer)
474 ggc_free (lexer->buffer);
475 VEC_free (cp_token_position, heap, lexer->saved_tokens);
479 /* Returns nonzero if debugging information should be output. */
481 #ifdef ENABLE_CHECKING
484 cp_lexer_debugging_p (cp_lexer *lexer)
486 return lexer->debugging_p;
489 #endif /* ENABLE_CHECKING */
491 static inline cp_token_position
492 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
494 gcc_assert (!previous_p || lexer->next_token != &eof_token);
496 return lexer->next_token - previous_p;
499 static inline cp_token *
500 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
505 /* nonzero if we are presently saving tokens. */
508 cp_lexer_saving_tokens (const cp_lexer* lexer)
510 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
513 /* Store the next token from the preprocessor in *TOKEN. Return true
514 if we reach EOF. If LEXER is NULL, assume we are handling an
515 initial #pragma pch_preprocess, and thus want the lexer to return
516 processed strings. */
519 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
521 static int is_extern_c = 0;
523 /* Get a new token from the preprocessor. */
525 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
526 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
527 token->keyword = RID_MAX;
528 token->pragma_kind = PRAGMA_NONE;
530 /* On some systems, some header files are surrounded by an
531 implicit extern "C" block. Set a flag in the token if it
532 comes from such a header. */
533 is_extern_c += pending_lang_change;
534 pending_lang_change = 0;
535 token->implicit_extern_c = is_extern_c > 0;
537 /* Check to see if this token is a keyword. */
538 if (token->type == CPP_NAME)
540 if (C_IS_RESERVED_WORD (token->u.value))
542 /* Mark this token as a keyword. */
543 token->type = CPP_KEYWORD;
544 /* Record which keyword. */
545 token->keyword = C_RID_CODE (token->u.value);
549 if (warn_cxx0x_compat
550 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
551 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
553 /* Warn about the C++0x keyword (but still treat it as
555 warning (OPT_Wc__0x_compat,
556 "identifier %qE will become a keyword in C++0x",
559 /* Clear out the C_RID_CODE so we don't warn about this
560 particular identifier-turned-keyword again. */
561 C_SET_RID_CODE (token->u.value, RID_MAX);
564 token->ambiguous_p = false;
565 token->keyword = RID_MAX;
568 /* Handle Objective-C++ keywords. */
569 else if (token->type == CPP_AT_NAME)
571 token->type = CPP_KEYWORD;
572 switch (C_RID_CODE (token->u.value))
574 /* Map 'class' to '@class', 'private' to '@private', etc. */
575 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
576 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
577 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
578 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
579 case RID_THROW: token->keyword = RID_AT_THROW; break;
580 case RID_TRY: token->keyword = RID_AT_TRY; break;
581 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
582 default: token->keyword = C_RID_CODE (token->u.value);
585 else if (token->type == CPP_PRAGMA)
587 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
588 token->pragma_kind = ((enum pragma_kind)
589 TREE_INT_CST_LOW (token->u.value));
590 token->u.value = NULL_TREE;
594 /* Update the globals input_location and the input file stack from TOKEN. */
596 cp_lexer_set_source_position_from_token (cp_token *token)
598 if (token->type != CPP_EOF)
600 input_location = token->location;
604 /* Return a pointer to the next token in the token stream, but do not
607 static inline cp_token *
608 cp_lexer_peek_token (cp_lexer *lexer)
610 if (cp_lexer_debugging_p (lexer))
612 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
613 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
614 putc ('\n', cp_lexer_debug_stream);
616 return lexer->next_token;
619 /* Return true if the next token has the indicated TYPE. */
622 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
624 return cp_lexer_peek_token (lexer)->type == type;
627 /* Return true if the next token does not have the indicated TYPE. */
630 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
632 return !cp_lexer_next_token_is (lexer, type);
635 /* Return true if the next token is the indicated KEYWORD. */
638 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
640 return cp_lexer_peek_token (lexer)->keyword == keyword;
643 /* Return true if the next token is not the indicated KEYWORD. */
646 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
648 return cp_lexer_peek_token (lexer)->keyword != keyword;
651 /* Return true if the next token is a keyword for a decl-specifier. */
654 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
658 token = cp_lexer_peek_token (lexer);
659 switch (token->keyword)
661 /* auto specifier: storage-class-specifier in C++,
662 simple-type-specifier in C++0x. */
664 /* Storage classes. */
670 /* Elaborated type specifiers. */
676 /* Simple type specifiers. */
691 /* GNU extensions. */
694 /* C++0x extensions. */
703 /* Return a pointer to the Nth token in the token stream. If N is 1,
704 then this is precisely equivalent to cp_lexer_peek_token (except
705 that it is not inline). One would like to disallow that case, but
706 there is one case (cp_parser_nth_token_starts_template_id) where
707 the caller passes a variable for N and it might be 1. */
710 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
714 /* N is 1-based, not zero-based. */
717 if (cp_lexer_debugging_p (lexer))
718 fprintf (cp_lexer_debug_stream,
719 "cp_lexer: peeking ahead %ld at token: ", (long)n);
722 token = lexer->next_token;
723 gcc_assert (!n || token != &eof_token);
727 if (token == lexer->last_token)
733 if (token->type != CPP_PURGED)
737 if (cp_lexer_debugging_p (lexer))
739 cp_lexer_print_token (cp_lexer_debug_stream, token);
740 putc ('\n', cp_lexer_debug_stream);
746 /* Return the next token, and advance the lexer's next_token pointer
747 to point to the next non-purged token. */
750 cp_lexer_consume_token (cp_lexer* lexer)
752 cp_token *token = lexer->next_token;
754 gcc_assert (token != &eof_token);
755 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
760 if (lexer->next_token == lexer->last_token)
762 lexer->next_token = &eof_token;
767 while (lexer->next_token->type == CPP_PURGED);
769 cp_lexer_set_source_position_from_token (token);
771 /* Provide debugging output. */
772 if (cp_lexer_debugging_p (lexer))
774 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
775 cp_lexer_print_token (cp_lexer_debug_stream, token);
776 putc ('\n', cp_lexer_debug_stream);
782 /* Permanently remove the next token from the token stream, and
783 advance the next_token pointer to refer to the next non-purged
787 cp_lexer_purge_token (cp_lexer *lexer)
789 cp_token *tok = lexer->next_token;
791 gcc_assert (tok != &eof_token);
792 tok->type = CPP_PURGED;
793 tok->location = UNKNOWN_LOCATION;
794 tok->u.value = NULL_TREE;
795 tok->keyword = RID_MAX;
800 if (tok == lexer->last_token)
806 while (tok->type == CPP_PURGED);
807 lexer->next_token = tok;
810 /* Permanently remove all tokens after TOK, up to, but not
811 including, the token that will be returned next by
812 cp_lexer_peek_token. */
815 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
817 cp_token *peek = lexer->next_token;
819 if (peek == &eof_token)
820 peek = lexer->last_token;
822 gcc_assert (tok < peek);
824 for ( tok += 1; tok != peek; tok += 1)
826 tok->type = CPP_PURGED;
827 tok->location = UNKNOWN_LOCATION;
828 tok->u.value = NULL_TREE;
829 tok->keyword = RID_MAX;
833 /* Begin saving tokens. All tokens consumed after this point will be
837 cp_lexer_save_tokens (cp_lexer* lexer)
839 /* Provide debugging output. */
840 if (cp_lexer_debugging_p (lexer))
841 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
843 VEC_safe_push (cp_token_position, heap,
844 lexer->saved_tokens, lexer->next_token);
847 /* Commit to the portion of the token stream most recently saved. */
850 cp_lexer_commit_tokens (cp_lexer* lexer)
852 /* Provide debugging output. */
853 if (cp_lexer_debugging_p (lexer))
854 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
856 VEC_pop (cp_token_position, lexer->saved_tokens);
859 /* Return all tokens saved since the last call to cp_lexer_save_tokens
860 to the token stream. Stop saving tokens. */
863 cp_lexer_rollback_tokens (cp_lexer* lexer)
865 /* Provide debugging output. */
866 if (cp_lexer_debugging_p (lexer))
867 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
869 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
872 /* Print a representation of the TOKEN on the STREAM. */
874 #ifdef ENABLE_CHECKING
877 cp_lexer_print_token (FILE * stream, cp_token *token)
879 /* We don't use cpp_type2name here because the parser defines
880 a few tokens of its own. */
881 static const char *const token_names[] = {
882 /* cpplib-defined token types */
888 /* C++ parser token types - see "Manifest constants", above. */
891 "NESTED_NAME_SPECIFIER",
895 /* If we have a name for the token, print it out. Otherwise, we
896 simply give the numeric code. */
897 gcc_assert (token->type < ARRAY_SIZE(token_names));
898 fputs (token_names[token->type], stream);
900 /* For some tokens, print the associated data. */
904 /* Some keywords have a value that is not an IDENTIFIER_NODE.
905 For example, `struct' is mapped to an INTEGER_CST. */
906 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
908 /* else fall through */
910 fputs (IDENTIFIER_POINTER (token->u.value), stream);
918 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
926 /* Start emitting debugging information. */
929 cp_lexer_start_debugging (cp_lexer* lexer)
931 lexer->debugging_p = true;
934 /* Stop emitting debugging information. */
937 cp_lexer_stop_debugging (cp_lexer* lexer)
939 lexer->debugging_p = false;
942 #endif /* ENABLE_CHECKING */
944 /* Create a new cp_token_cache, representing a range of tokens. */
946 static cp_token_cache *
947 cp_token_cache_new (cp_token *first, cp_token *last)
949 cp_token_cache *cache = GGC_NEW (cp_token_cache);
950 cache->first = first;
956 /* Decl-specifiers. */
958 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
961 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
963 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
968 /* Nothing other than the parser should be creating declarators;
969 declarators are a semi-syntactic representation of C++ entities.
970 Other parts of the front end that need to create entities (like
971 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
973 static cp_declarator *make_call_declarator
974 (cp_declarator *, tree, cp_cv_quals, tree, tree);
975 static cp_declarator *make_array_declarator
976 (cp_declarator *, tree);
977 static cp_declarator *make_pointer_declarator
978 (cp_cv_quals, cp_declarator *);
979 static cp_declarator *make_reference_declarator
980 (cp_cv_quals, cp_declarator *, bool);
981 static cp_parameter_declarator *make_parameter_declarator
982 (cp_decl_specifier_seq *, cp_declarator *, tree);
983 static cp_declarator *make_ptrmem_declarator
984 (cp_cv_quals, tree, cp_declarator *);
986 /* An erroneous declarator. */
987 static cp_declarator *cp_error_declarator;
989 /* The obstack on which declarators and related data structures are
991 static struct obstack declarator_obstack;
993 /* Alloc BYTES from the declarator memory pool. */
996 alloc_declarator (size_t bytes)
998 return obstack_alloc (&declarator_obstack, bytes);
1001 /* Allocate a declarator of the indicated KIND. Clear fields that are
1002 common to all declarators. */
1004 static cp_declarator *
1005 make_declarator (cp_declarator_kind kind)
1007 cp_declarator *declarator;
1009 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1010 declarator->kind = kind;
1011 declarator->attributes = NULL_TREE;
1012 declarator->declarator = NULL;
1013 declarator->parameter_pack_p = false;
1014 declarator->id_loc = UNKNOWN_LOCATION;
1019 /* Make a declarator for a generalized identifier. If
1020 QUALIFYING_SCOPE is non-NULL, the identifier is
1021 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1022 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1025 static cp_declarator *
1026 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1027 special_function_kind sfk)
1029 cp_declarator *declarator;
1031 /* It is valid to write:
1033 class C { void f(); };
1037 The standard is not clear about whether `typedef const C D' is
1038 legal; as of 2002-09-15 the committee is considering that
1039 question. EDG 3.0 allows that syntax. Therefore, we do as
1041 if (qualifying_scope && TYPE_P (qualifying_scope))
1042 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1044 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1045 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1046 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1048 declarator = make_declarator (cdk_id);
1049 declarator->u.id.qualifying_scope = qualifying_scope;
1050 declarator->u.id.unqualified_name = unqualified_name;
1051 declarator->u.id.sfk = sfk;
1056 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1057 of modifiers such as const or volatile to apply to the pointer
1058 type, represented as identifiers. */
1061 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1063 cp_declarator *declarator;
1065 declarator = make_declarator (cdk_pointer);
1066 declarator->declarator = target;
1067 declarator->u.pointer.qualifiers = cv_qualifiers;
1068 declarator->u.pointer.class_type = NULL_TREE;
1071 declarator->parameter_pack_p = target->parameter_pack_p;
1072 target->parameter_pack_p = false;
1075 declarator->parameter_pack_p = false;
1080 /* Like make_pointer_declarator -- but for references. */
1083 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1086 cp_declarator *declarator;
1088 declarator = make_declarator (cdk_reference);
1089 declarator->declarator = target;
1090 declarator->u.reference.qualifiers = cv_qualifiers;
1091 declarator->u.reference.rvalue_ref = rvalue_ref;
1094 declarator->parameter_pack_p = target->parameter_pack_p;
1095 target->parameter_pack_p = false;
1098 declarator->parameter_pack_p = false;
1103 /* Like make_pointer_declarator -- but for a pointer to a non-static
1104 member of CLASS_TYPE. */
1107 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1108 cp_declarator *pointee)
1110 cp_declarator *declarator;
1112 declarator = make_declarator (cdk_ptrmem);
1113 declarator->declarator = pointee;
1114 declarator->u.pointer.qualifiers = cv_qualifiers;
1115 declarator->u.pointer.class_type = class_type;
1119 declarator->parameter_pack_p = pointee->parameter_pack_p;
1120 pointee->parameter_pack_p = false;
1123 declarator->parameter_pack_p = false;
1128 /* Make a declarator for the function given by TARGET, with the
1129 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1130 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1131 indicates what exceptions can be thrown. */
1134 make_call_declarator (cp_declarator *target,
1136 cp_cv_quals cv_qualifiers,
1137 tree exception_specification,
1138 tree late_return_type)
1140 cp_declarator *declarator;
1142 declarator = make_declarator (cdk_function);
1143 declarator->declarator = target;
1144 declarator->u.function.parameters = parms;
1145 declarator->u.function.qualifiers = cv_qualifiers;
1146 declarator->u.function.exception_specification = exception_specification;
1147 declarator->u.function.late_return_type = late_return_type;
1150 declarator->parameter_pack_p = target->parameter_pack_p;
1151 target->parameter_pack_p = false;
1154 declarator->parameter_pack_p = false;
1159 /* Make a declarator for an array of BOUNDS elements, each of which is
1160 defined by ELEMENT. */
1163 make_array_declarator (cp_declarator *element, tree bounds)
1165 cp_declarator *declarator;
1167 declarator = make_declarator (cdk_array);
1168 declarator->declarator = element;
1169 declarator->u.array.bounds = bounds;
1172 declarator->parameter_pack_p = element->parameter_pack_p;
1173 element->parameter_pack_p = false;
1176 declarator->parameter_pack_p = false;
1181 /* Determine whether the declarator we've seen so far can be a
1182 parameter pack, when followed by an ellipsis. */
1184 declarator_can_be_parameter_pack (cp_declarator *declarator)
1186 /* Search for a declarator name, or any other declarator that goes
1187 after the point where the ellipsis could appear in a parameter
1188 pack. If we find any of these, then this declarator can not be
1189 made into a parameter pack. */
1191 while (declarator && !found)
1193 switch ((int)declarator->kind)
1204 declarator = declarator->declarator;
1212 cp_parameter_declarator *no_parameters;
1214 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1215 DECLARATOR and DEFAULT_ARGUMENT. */
1217 cp_parameter_declarator *
1218 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1219 cp_declarator *declarator,
1220 tree default_argument)
1222 cp_parameter_declarator *parameter;
1224 parameter = ((cp_parameter_declarator *)
1225 alloc_declarator (sizeof (cp_parameter_declarator)));
1226 parameter->next = NULL;
1227 if (decl_specifiers)
1228 parameter->decl_specifiers = *decl_specifiers;
1230 clear_decl_specs (¶meter->decl_specifiers);
1231 parameter->declarator = declarator;
1232 parameter->default_argument = default_argument;
1233 parameter->ellipsis_p = false;
1238 /* Returns true iff DECLARATOR is a declaration for a function. */
1241 function_declarator_p (const cp_declarator *declarator)
1245 if (declarator->kind == cdk_function
1246 && declarator->declarator->kind == cdk_id)
1248 if (declarator->kind == cdk_id
1249 || declarator->kind == cdk_error)
1251 declarator = declarator->declarator;
1261 A cp_parser parses the token stream as specified by the C++
1262 grammar. Its job is purely parsing, not semantic analysis. For
1263 example, the parser breaks the token stream into declarators,
1264 expressions, statements, and other similar syntactic constructs.
1265 It does not check that the types of the expressions on either side
1266 of an assignment-statement are compatible, or that a function is
1267 not declared with a parameter of type `void'.
1269 The parser invokes routines elsewhere in the compiler to perform
1270 semantic analysis and to build up the abstract syntax tree for the
1273 The parser (and the template instantiation code, which is, in a
1274 way, a close relative of parsing) are the only parts of the
1275 compiler that should be calling push_scope and pop_scope, or
1276 related functions. The parser (and template instantiation code)
1277 keeps track of what scope is presently active; everything else
1278 should simply honor that. (The code that generates static
1279 initializers may also need to set the scope, in order to check
1280 access control correctly when emitting the initializers.)
1285 The parser is of the standard recursive-descent variety. Upcoming
1286 tokens in the token stream are examined in order to determine which
1287 production to use when parsing a non-terminal. Some C++ constructs
1288 require arbitrary look ahead to disambiguate. For example, it is
1289 impossible, in the general case, to tell whether a statement is an
1290 expression or declaration without scanning the entire statement.
1291 Therefore, the parser is capable of "parsing tentatively." When the
1292 parser is not sure what construct comes next, it enters this mode.
1293 Then, while we attempt to parse the construct, the parser queues up
1294 error messages, rather than issuing them immediately, and saves the
1295 tokens it consumes. If the construct is parsed successfully, the
1296 parser "commits", i.e., it issues any queued error messages and
1297 the tokens that were being preserved are permanently discarded.
1298 If, however, the construct is not parsed successfully, the parser
1299 rolls back its state completely so that it can resume parsing using
1300 a different alternative.
1305 The performance of the parser could probably be improved substantially.
1306 We could often eliminate the need to parse tentatively by looking ahead
1307 a little bit. In some places, this approach might not entirely eliminate
1308 the need to parse tentatively, but it might still speed up the average
1311 /* Flags that are passed to some parsing functions. These values can
1312 be bitwise-ored together. */
1317 CP_PARSER_FLAGS_NONE = 0x0,
1318 /* The construct is optional. If it is not present, then no error
1319 should be issued. */
1320 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1321 /* When parsing a type-specifier, treat user-defined type-names
1322 as non-type identifiers. */
1323 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1324 /* When parsing a type-specifier, do not try to parse a class-specifier
1325 or enum-specifier. */
1326 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1329 /* This type is used for parameters and variables which hold
1330 combinations of the above flags. */
1331 typedef int cp_parser_flags;
1333 /* The different kinds of declarators we want to parse. */
1335 typedef enum cp_parser_declarator_kind
1337 /* We want an abstract declarator. */
1338 CP_PARSER_DECLARATOR_ABSTRACT,
1339 /* We want a named declarator. */
1340 CP_PARSER_DECLARATOR_NAMED,
1341 /* We don't mind, but the name must be an unqualified-id. */
1342 CP_PARSER_DECLARATOR_EITHER
1343 } cp_parser_declarator_kind;
1345 /* The precedence values used to parse binary expressions. The minimum value
1346 of PREC must be 1, because zero is reserved to quickly discriminate
1347 binary operators from other tokens. */
1352 PREC_LOGICAL_OR_EXPRESSION,
1353 PREC_LOGICAL_AND_EXPRESSION,
1354 PREC_INCLUSIVE_OR_EXPRESSION,
1355 PREC_EXCLUSIVE_OR_EXPRESSION,
1356 PREC_AND_EXPRESSION,
1357 PREC_EQUALITY_EXPRESSION,
1358 PREC_RELATIONAL_EXPRESSION,
1359 PREC_SHIFT_EXPRESSION,
1360 PREC_ADDITIVE_EXPRESSION,
1361 PREC_MULTIPLICATIVE_EXPRESSION,
1363 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1366 /* A mapping from a token type to a corresponding tree node type, with a
1367 precedence value. */
1369 typedef struct cp_parser_binary_operations_map_node
1371 /* The token type. */
1372 enum cpp_ttype token_type;
1373 /* The corresponding tree code. */
1374 enum tree_code tree_type;
1375 /* The precedence of this operator. */
1376 enum cp_parser_prec prec;
1377 } cp_parser_binary_operations_map_node;
1379 /* The status of a tentative parse. */
1381 typedef enum cp_parser_status_kind
1383 /* No errors have occurred. */
1384 CP_PARSER_STATUS_KIND_NO_ERROR,
1385 /* An error has occurred. */
1386 CP_PARSER_STATUS_KIND_ERROR,
1387 /* We are committed to this tentative parse, whether or not an error
1389 CP_PARSER_STATUS_KIND_COMMITTED
1390 } cp_parser_status_kind;
1392 typedef struct cp_parser_expression_stack_entry
1394 /* Left hand side of the binary operation we are currently
1397 /* Original tree code for left hand side, if it was a binary
1398 expression itself (used for -Wparentheses). */
1399 enum tree_code lhs_type;
1400 /* Tree code for the binary operation we are parsing. */
1401 enum tree_code tree_type;
1402 /* Precedence of the binary operation we are parsing. */
1403 enum cp_parser_prec prec;
1404 } cp_parser_expression_stack_entry;
1406 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1407 entries because precedence levels on the stack are monotonically
1409 typedef struct cp_parser_expression_stack_entry
1410 cp_parser_expression_stack[NUM_PREC_VALUES];
1412 /* Context that is saved and restored when parsing tentatively. */
1413 typedef struct GTY (()) cp_parser_context {
1414 /* If this is a tentative parsing context, the status of the
1416 enum cp_parser_status_kind status;
1417 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1418 that are looked up in this context must be looked up both in the
1419 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1420 the context of the containing expression. */
1423 /* The next parsing context in the stack. */
1424 struct cp_parser_context *next;
1425 } cp_parser_context;
1429 /* Constructors and destructors. */
1431 static cp_parser_context *cp_parser_context_new
1432 (cp_parser_context *);
1434 /* Class variables. */
1436 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1438 /* The operator-precedence table used by cp_parser_binary_expression.
1439 Transformed into an associative array (binops_by_token) by
1442 static const cp_parser_binary_operations_map_node binops[] = {
1443 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1444 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1446 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1447 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1448 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1450 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1451 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1453 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1454 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1456 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1457 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1458 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1459 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1461 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1462 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1464 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1466 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1468 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1470 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1472 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1475 /* The same as binops, but initialized by cp_parser_new so that
1476 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1478 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1480 /* Constructors and destructors. */
1482 /* Construct a new context. The context below this one on the stack
1483 is given by NEXT. */
1485 static cp_parser_context *
1486 cp_parser_context_new (cp_parser_context* next)
1488 cp_parser_context *context;
1490 /* Allocate the storage. */
1491 if (cp_parser_context_free_list != NULL)
1493 /* Pull the first entry from the free list. */
1494 context = cp_parser_context_free_list;
1495 cp_parser_context_free_list = context->next;
1496 memset (context, 0, sizeof (*context));
1499 context = GGC_CNEW (cp_parser_context);
1501 /* No errors have occurred yet in this context. */
1502 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1503 /* If this is not the bottommost context, copy information that we
1504 need from the previous context. */
1507 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1508 expression, then we are parsing one in this context, too. */
1509 context->object_type = next->object_type;
1510 /* Thread the stack. */
1511 context->next = next;
1517 /* The cp_parser structure represents the C++ parser. */
1519 typedef struct GTY(()) cp_parser {
1520 /* The lexer from which we are obtaining tokens. */
1523 /* The scope in which names should be looked up. If NULL_TREE, then
1524 we look up names in the scope that is currently open in the
1525 source program. If non-NULL, this is either a TYPE or
1526 NAMESPACE_DECL for the scope in which we should look. It can
1527 also be ERROR_MARK, when we've parsed a bogus scope.
1529 This value is not cleared automatically after a name is looked
1530 up, so we must be careful to clear it before starting a new look
1531 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1532 will look up `Z' in the scope of `X', rather than the current
1533 scope.) Unfortunately, it is difficult to tell when name lookup
1534 is complete, because we sometimes peek at a token, look it up,
1535 and then decide not to consume it. */
1538 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1539 last lookup took place. OBJECT_SCOPE is used if an expression
1540 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1541 respectively. QUALIFYING_SCOPE is used for an expression of the
1542 form "X::Y"; it refers to X. */
1544 tree qualifying_scope;
1546 /* A stack of parsing contexts. All but the bottom entry on the
1547 stack will be tentative contexts.
1549 We parse tentatively in order to determine which construct is in
1550 use in some situations. For example, in order to determine
1551 whether a statement is an expression-statement or a
1552 declaration-statement we parse it tentatively as a
1553 declaration-statement. If that fails, we then reparse the same
1554 token stream as an expression-statement. */
1555 cp_parser_context *context;
1557 /* True if we are parsing GNU C++. If this flag is not set, then
1558 GNU extensions are not recognized. */
1559 bool allow_gnu_extensions_p;
1561 /* TRUE if the `>' token should be interpreted as the greater-than
1562 operator. FALSE if it is the end of a template-id or
1563 template-parameter-list. In C++0x mode, this flag also applies to
1564 `>>' tokens, which are viewed as two consecutive `>' tokens when
1565 this flag is FALSE. */
1566 bool greater_than_is_operator_p;
1568 /* TRUE if default arguments are allowed within a parameter list
1569 that starts at this point. FALSE if only a gnu extension makes
1570 them permissible. */
1571 bool default_arg_ok_p;
1573 /* TRUE if we are parsing an integral constant-expression. See
1574 [expr.const] for a precise definition. */
1575 bool integral_constant_expression_p;
1577 /* TRUE if we are parsing an integral constant-expression -- but a
1578 non-constant expression should be permitted as well. This flag
1579 is used when parsing an array bound so that GNU variable-length
1580 arrays are tolerated. */
1581 bool allow_non_integral_constant_expression_p;
1583 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1584 been seen that makes the expression non-constant. */
1585 bool non_integral_constant_expression_p;
1587 /* TRUE if local variable names and `this' are forbidden in the
1589 bool local_variables_forbidden_p;
1591 /* TRUE if the declaration we are parsing is part of a
1592 linkage-specification of the form `extern string-literal
1594 bool in_unbraced_linkage_specification_p;
1596 /* TRUE if we are presently parsing a declarator, after the
1597 direct-declarator. */
1598 bool in_declarator_p;
1600 /* TRUE if we are presently parsing a template-argument-list. */
1601 bool in_template_argument_list_p;
1603 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1604 to IN_OMP_BLOCK if parsing OpenMP structured block and
1605 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1606 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1607 iteration-statement, OpenMP block or loop within that switch. */
1608 #define IN_SWITCH_STMT 1
1609 #define IN_ITERATION_STMT 2
1610 #define IN_OMP_BLOCK 4
1611 #define IN_OMP_FOR 8
1612 #define IN_IF_STMT 16
1613 unsigned char in_statement;
1615 /* TRUE if we are presently parsing the body of a switch statement.
1616 Note that this doesn't quite overlap with in_statement above.
1617 The difference relates to giving the right sets of error messages:
1618 "case not in switch" vs "break statement used with OpenMP...". */
1619 bool in_switch_statement_p;
1621 /* TRUE if we are parsing a type-id in an expression context. In
1622 such a situation, both "type (expr)" and "type (type)" are valid
1624 bool in_type_id_in_expr_p;
1626 /* TRUE if we are currently in a header file where declarations are
1627 implicitly extern "C". */
1628 bool implicit_extern_c;
1630 /* TRUE if strings in expressions should be translated to the execution
1632 bool translate_strings_p;
1634 /* TRUE if we are presently parsing the body of a function, but not
1636 bool in_function_body;
1638 /* If non-NULL, then we are parsing a construct where new type
1639 definitions are not permitted. The string stored here will be
1640 issued as an error message if a type is defined. */
1641 const char *type_definition_forbidden_message;
1643 /* A list of lists. The outer list is a stack, used for member
1644 functions of local classes. At each level there are two sub-list,
1645 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1646 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1647 TREE_VALUE's. The functions are chained in reverse declaration
1650 The TREE_PURPOSE sublist contains those functions with default
1651 arguments that need post processing, and the TREE_VALUE sublist
1652 contains those functions with definitions that need post
1655 These lists can only be processed once the outermost class being
1656 defined is complete. */
1657 tree unparsed_functions_queues;
1659 /* The number of classes whose definitions are currently in
1661 unsigned num_classes_being_defined;
1663 /* The number of template parameter lists that apply directly to the
1664 current declaration. */
1665 unsigned num_template_parameter_lists;
1670 /* Constructors and destructors. */
1672 static cp_parser *cp_parser_new
1675 /* Routines to parse various constructs.
1677 Those that return `tree' will return the error_mark_node (rather
1678 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1679 Sometimes, they will return an ordinary node if error-recovery was
1680 attempted, even though a parse error occurred. So, to check
1681 whether or not a parse error occurred, you should always use
1682 cp_parser_error_occurred. If the construct is optional (indicated
1683 either by an `_opt' in the name of the function that does the
1684 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1685 the construct is not present. */
1687 /* Lexical conventions [gram.lex] */
1689 static tree cp_parser_identifier
1691 static tree cp_parser_string_literal
1692 (cp_parser *, bool, bool);
1694 /* Basic concepts [gram.basic] */
1696 static bool cp_parser_translation_unit
1699 /* Expressions [gram.expr] */
1701 static tree cp_parser_primary_expression
1702 (cp_parser *, bool, bool, bool, cp_id_kind *);
1703 static tree cp_parser_id_expression
1704 (cp_parser *, bool, bool, bool *, bool, bool);
1705 static tree cp_parser_unqualified_id
1706 (cp_parser *, bool, bool, bool, bool);
1707 static tree cp_parser_nested_name_specifier_opt
1708 (cp_parser *, bool, bool, bool, bool);
1709 static tree cp_parser_nested_name_specifier
1710 (cp_parser *, bool, bool, bool, bool);
1711 static tree cp_parser_qualifying_entity
1712 (cp_parser *, bool, bool, bool, bool, bool);
1713 static tree cp_parser_postfix_expression
1714 (cp_parser *, bool, bool, bool, cp_id_kind *);
1715 static tree cp_parser_postfix_open_square_expression
1716 (cp_parser *, tree, bool);
1717 static tree cp_parser_postfix_dot_deref_expression
1718 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1719 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1720 (cp_parser *, int, bool, bool, bool *);
1721 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1722 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1723 static void cp_parser_pseudo_destructor_name
1724 (cp_parser *, tree *, tree *);
1725 static tree cp_parser_unary_expression
1726 (cp_parser *, bool, bool, cp_id_kind *);
1727 static enum tree_code cp_parser_unary_operator
1729 static tree cp_parser_new_expression
1731 static VEC(tree,gc) *cp_parser_new_placement
1733 static tree cp_parser_new_type_id
1734 (cp_parser *, tree *);
1735 static cp_declarator *cp_parser_new_declarator_opt
1737 static cp_declarator *cp_parser_direct_new_declarator
1739 static VEC(tree,gc) *cp_parser_new_initializer
1741 static tree cp_parser_delete_expression
1743 static tree cp_parser_cast_expression
1744 (cp_parser *, bool, bool, cp_id_kind *);
1745 static tree cp_parser_binary_expression
1746 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1747 static tree cp_parser_question_colon_clause
1748 (cp_parser *, tree);
1749 static tree cp_parser_assignment_expression
1750 (cp_parser *, bool, cp_id_kind *);
1751 static enum tree_code cp_parser_assignment_operator_opt
1753 static tree cp_parser_expression
1754 (cp_parser *, bool, cp_id_kind *);
1755 static tree cp_parser_constant_expression
1756 (cp_parser *, bool, bool *);
1757 static tree cp_parser_builtin_offsetof
1759 static tree cp_parser_lambda_expression
1761 static void cp_parser_lambda_introducer
1762 (cp_parser *, tree);
1763 static void cp_parser_lambda_declarator_opt
1764 (cp_parser *, tree);
1765 static void cp_parser_lambda_body
1766 (cp_parser *, tree);
1768 /* Statements [gram.stmt.stmt] */
1770 static void cp_parser_statement
1771 (cp_parser *, tree, bool, bool *);
1772 static void cp_parser_label_for_labeled_statement
1774 static tree cp_parser_expression_statement
1775 (cp_parser *, tree);
1776 static tree cp_parser_compound_statement
1777 (cp_parser *, tree, bool);
1778 static void cp_parser_statement_seq_opt
1779 (cp_parser *, tree);
1780 static tree cp_parser_selection_statement
1781 (cp_parser *, bool *);
1782 static tree cp_parser_condition
1784 static tree cp_parser_iteration_statement
1786 static void cp_parser_for_init_statement
1788 static tree cp_parser_jump_statement
1790 static void cp_parser_declaration_statement
1793 static tree cp_parser_implicitly_scoped_statement
1794 (cp_parser *, bool *);
1795 static void cp_parser_already_scoped_statement
1798 /* Declarations [gram.dcl.dcl] */
1800 static void cp_parser_declaration_seq_opt
1802 static void cp_parser_declaration
1804 static void cp_parser_block_declaration
1805 (cp_parser *, bool);
1806 static void cp_parser_simple_declaration
1807 (cp_parser *, bool);
1808 static void cp_parser_decl_specifier_seq
1809 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1810 static tree cp_parser_storage_class_specifier_opt
1812 static tree cp_parser_function_specifier_opt
1813 (cp_parser *, cp_decl_specifier_seq *);
1814 static tree cp_parser_type_specifier
1815 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1817 static tree cp_parser_simple_type_specifier
1818 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1819 static tree cp_parser_type_name
1821 static tree cp_parser_nonclass_name
1822 (cp_parser* parser);
1823 static tree cp_parser_elaborated_type_specifier
1824 (cp_parser *, bool, bool);
1825 static tree cp_parser_enum_specifier
1827 static void cp_parser_enumerator_list
1828 (cp_parser *, tree);
1829 static void cp_parser_enumerator_definition
1830 (cp_parser *, tree);
1831 static tree cp_parser_namespace_name
1833 static void cp_parser_namespace_definition
1835 static void cp_parser_namespace_body
1837 static tree cp_parser_qualified_namespace_specifier
1839 static void cp_parser_namespace_alias_definition
1841 static bool cp_parser_using_declaration
1842 (cp_parser *, bool);
1843 static void cp_parser_using_directive
1845 static void cp_parser_asm_definition
1847 static void cp_parser_linkage_specification
1849 static void cp_parser_static_assert
1850 (cp_parser *, bool);
1851 static tree cp_parser_decltype
1854 /* Declarators [gram.dcl.decl] */
1856 static tree cp_parser_init_declarator
1857 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1858 static cp_declarator *cp_parser_declarator
1859 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1860 static cp_declarator *cp_parser_direct_declarator
1861 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1862 static enum tree_code cp_parser_ptr_operator
1863 (cp_parser *, tree *, cp_cv_quals *);
1864 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1866 static tree cp_parser_late_return_type_opt
1868 static tree cp_parser_declarator_id
1869 (cp_parser *, bool);
1870 static tree cp_parser_type_id
1872 static tree cp_parser_template_type_arg
1874 static tree cp_parser_trailing_type_id (cp_parser *);
1875 static tree cp_parser_type_id_1
1876 (cp_parser *, bool, bool);
1877 static void cp_parser_type_specifier_seq
1878 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1879 static tree cp_parser_parameter_declaration_clause
1881 static tree cp_parser_parameter_declaration_list
1882 (cp_parser *, bool *);
1883 static cp_parameter_declarator *cp_parser_parameter_declaration
1884 (cp_parser *, bool, bool *);
1885 static tree cp_parser_default_argument
1886 (cp_parser *, bool);
1887 static void cp_parser_function_body
1889 static tree cp_parser_initializer
1890 (cp_parser *, bool *, bool *);
1891 static tree cp_parser_initializer_clause
1892 (cp_parser *, bool *);
1893 static tree cp_parser_braced_list
1894 (cp_parser*, bool*);
1895 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1896 (cp_parser *, bool *);
1898 static bool cp_parser_ctor_initializer_opt_and_function_body
1901 /* Classes [gram.class] */
1903 static tree cp_parser_class_name
1904 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1905 static tree cp_parser_class_specifier
1907 static tree cp_parser_class_head
1908 (cp_parser *, bool *, tree *, tree *);
1909 static enum tag_types cp_parser_class_key
1911 static void cp_parser_member_specification_opt
1913 static void cp_parser_member_declaration
1915 static tree cp_parser_pure_specifier
1917 static tree cp_parser_constant_initializer
1920 /* Derived classes [gram.class.derived] */
1922 static tree cp_parser_base_clause
1924 static tree cp_parser_base_specifier
1927 /* Special member functions [gram.special] */
1929 static tree cp_parser_conversion_function_id
1931 static tree cp_parser_conversion_type_id
1933 static cp_declarator *cp_parser_conversion_declarator_opt
1935 static bool cp_parser_ctor_initializer_opt
1937 static void cp_parser_mem_initializer_list
1939 static tree cp_parser_mem_initializer
1941 static tree cp_parser_mem_initializer_id
1944 /* Overloading [gram.over] */
1946 static tree cp_parser_operator_function_id
1948 static tree cp_parser_operator
1951 /* Templates [gram.temp] */
1953 static void cp_parser_template_declaration
1954 (cp_parser *, bool);
1955 static tree cp_parser_template_parameter_list
1957 static tree cp_parser_template_parameter
1958 (cp_parser *, bool *, bool *);
1959 static tree cp_parser_type_parameter
1960 (cp_parser *, bool *);
1961 static tree cp_parser_template_id
1962 (cp_parser *, bool, bool, bool);
1963 static tree cp_parser_template_name
1964 (cp_parser *, bool, bool, bool, bool *);
1965 static tree cp_parser_template_argument_list
1967 static tree cp_parser_template_argument
1969 static void cp_parser_explicit_instantiation
1971 static void cp_parser_explicit_specialization
1974 /* Exception handling [gram.exception] */
1976 static tree cp_parser_try_block
1978 static bool cp_parser_function_try_block
1980 static void cp_parser_handler_seq
1982 static void cp_parser_handler
1984 static tree cp_parser_exception_declaration
1986 static tree cp_parser_throw_expression
1988 static tree cp_parser_exception_specification_opt
1990 static tree cp_parser_type_id_list
1993 /* GNU Extensions */
1995 static tree cp_parser_asm_specification_opt
1997 static tree cp_parser_asm_operand_list
1999 static tree cp_parser_asm_clobber_list
2001 static tree cp_parser_asm_label_list
2003 static tree cp_parser_attributes_opt
2005 static tree cp_parser_attribute_list
2007 static bool cp_parser_extension_opt
2008 (cp_parser *, int *);
2009 static void cp_parser_label_declaration
2012 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2013 static bool cp_parser_pragma
2014 (cp_parser *, enum pragma_context);
2016 /* Objective-C++ Productions */
2018 static tree cp_parser_objc_message_receiver
2020 static tree cp_parser_objc_message_args
2022 static tree cp_parser_objc_message_expression
2024 static tree cp_parser_objc_encode_expression
2026 static tree cp_parser_objc_defs_expression
2028 static tree cp_parser_objc_protocol_expression
2030 static tree cp_parser_objc_selector_expression
2032 static tree cp_parser_objc_expression
2034 static bool cp_parser_objc_selector_p
2036 static tree cp_parser_objc_selector
2038 static tree cp_parser_objc_protocol_refs_opt
2040 static void cp_parser_objc_declaration
2042 static tree cp_parser_objc_statement
2045 /* Utility Routines */
2047 static tree cp_parser_lookup_name
2048 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2049 static tree cp_parser_lookup_name_simple
2050 (cp_parser *, tree, location_t);
2051 static tree cp_parser_maybe_treat_template_as_class
2053 static bool cp_parser_check_declarator_template_parameters
2054 (cp_parser *, cp_declarator *, location_t);
2055 static bool cp_parser_check_template_parameters
2056 (cp_parser *, unsigned, location_t, cp_declarator *);
2057 static tree cp_parser_simple_cast_expression
2059 static tree cp_parser_global_scope_opt
2060 (cp_parser *, bool);
2061 static bool cp_parser_constructor_declarator_p
2062 (cp_parser *, bool);
2063 static tree cp_parser_function_definition_from_specifiers_and_declarator
2064 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2065 static tree cp_parser_function_definition_after_declarator
2066 (cp_parser *, bool);
2067 static void cp_parser_template_declaration_after_export
2068 (cp_parser *, bool);
2069 static void cp_parser_perform_template_parameter_access_checks
2070 (VEC (deferred_access_check,gc)*);
2071 static tree cp_parser_single_declaration
2072 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2073 static tree cp_parser_functional_cast
2074 (cp_parser *, tree);
2075 static tree cp_parser_save_member_function_body
2076 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2077 static tree cp_parser_enclosed_template_argument_list
2079 static void cp_parser_save_default_args
2080 (cp_parser *, tree);
2081 static void cp_parser_late_parsing_for_member
2082 (cp_parser *, tree);
2083 static void cp_parser_late_parsing_default_args
2084 (cp_parser *, tree);
2085 static tree cp_parser_sizeof_operand
2086 (cp_parser *, enum rid);
2087 static tree cp_parser_trait_expr
2088 (cp_parser *, enum rid);
2089 static bool cp_parser_declares_only_class_p
2091 static void cp_parser_set_storage_class
2092 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2093 static void cp_parser_set_decl_spec_type
2094 (cp_decl_specifier_seq *, tree, location_t, bool);
2095 static bool cp_parser_friend_p
2096 (const cp_decl_specifier_seq *);
2097 static void cp_parser_required_error
2098 (cp_parser *, required_token, bool);
2099 static cp_token *cp_parser_require
2100 (cp_parser *, enum cpp_ttype, required_token);
2101 static cp_token *cp_parser_require_keyword
2102 (cp_parser *, enum rid, required_token);
2103 static bool cp_parser_token_starts_function_definition_p
2105 static bool cp_parser_next_token_starts_class_definition_p
2107 static bool cp_parser_next_token_ends_template_argument_p
2109 static bool cp_parser_nth_token_starts_template_argument_list_p
2110 (cp_parser *, size_t);
2111 static enum tag_types cp_parser_token_is_class_key
2113 static void cp_parser_check_class_key
2114 (enum tag_types, tree type);
2115 static void cp_parser_check_access_in_redeclaration
2116 (tree type, location_t location);
2117 static bool cp_parser_optional_template_keyword
2119 static void cp_parser_pre_parsed_nested_name_specifier
2121 static bool cp_parser_cache_group
2122 (cp_parser *, enum cpp_ttype, unsigned);
2123 static void cp_parser_parse_tentatively
2125 static void cp_parser_commit_to_tentative_parse
2127 static void cp_parser_abort_tentative_parse
2129 static bool cp_parser_parse_definitely
2131 static inline bool cp_parser_parsing_tentatively
2133 static bool cp_parser_uncommitted_to_tentative_parse_p
2135 static void cp_parser_error
2136 (cp_parser *, const char *);
2137 static void cp_parser_name_lookup_error
2138 (cp_parser *, tree, tree, name_lookup_error, location_t);
2139 static bool cp_parser_simulate_error
2141 static bool cp_parser_check_type_definition
2143 static void cp_parser_check_for_definition_in_return_type
2144 (cp_declarator *, tree, location_t type_location);
2145 static void cp_parser_check_for_invalid_template_id
2146 (cp_parser *, tree, location_t location);
2147 static bool cp_parser_non_integral_constant_expression
2148 (cp_parser *, non_integral_constant);
2149 static void cp_parser_diagnose_invalid_type_name
2150 (cp_parser *, tree, tree, location_t);
2151 static bool cp_parser_parse_and_diagnose_invalid_type_name
2153 static int cp_parser_skip_to_closing_parenthesis
2154 (cp_parser *, bool, bool, bool);
2155 static void cp_parser_skip_to_end_of_statement
2157 static void cp_parser_consume_semicolon_at_end_of_statement
2159 static void cp_parser_skip_to_end_of_block_or_statement
2161 static bool cp_parser_skip_to_closing_brace
2163 static void cp_parser_skip_to_end_of_template_parameter_list
2165 static void cp_parser_skip_to_pragma_eol
2166 (cp_parser*, cp_token *);
2167 static bool cp_parser_error_occurred
2169 static bool cp_parser_allow_gnu_extensions_p
2171 static bool cp_parser_is_string_literal
2173 static bool cp_parser_is_keyword
2174 (cp_token *, enum rid);
2175 static tree cp_parser_make_typename_type
2176 (cp_parser *, tree, tree, location_t location);
2177 static cp_declarator * cp_parser_make_indirect_declarator
2178 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2180 /* Returns nonzero if we are parsing tentatively. */
2183 cp_parser_parsing_tentatively (cp_parser* parser)
2185 return parser->context->next != NULL;
2188 /* Returns nonzero if TOKEN is a string literal. */
2191 cp_parser_is_string_literal (cp_token* token)
2193 return (token->type == CPP_STRING ||
2194 token->type == CPP_STRING16 ||
2195 token->type == CPP_STRING32 ||
2196 token->type == CPP_WSTRING ||
2197 token->type == CPP_UTF8STRING);
2200 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2203 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2205 return token->keyword == keyword;
2208 /* If not parsing tentatively, issue a diagnostic of the form
2209 FILE:LINE: MESSAGE before TOKEN
2210 where TOKEN is the next token in the input stream. MESSAGE
2211 (specified by the caller) is usually of the form "expected
2215 cp_parser_error (cp_parser* parser, const char* gmsgid)
2217 if (!cp_parser_simulate_error (parser))
2219 cp_token *token = cp_lexer_peek_token (parser->lexer);
2220 /* This diagnostic makes more sense if it is tagged to the line
2221 of the token we just peeked at. */
2222 cp_lexer_set_source_position_from_token (token);
2224 if (token->type == CPP_PRAGMA)
2226 error_at (token->location,
2227 "%<#pragma%> is not allowed here");
2228 cp_parser_skip_to_pragma_eol (parser, token);
2232 c_parse_error (gmsgid,
2233 /* Because c_parser_error does not understand
2234 CPP_KEYWORD, keywords are treated like
2236 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2237 token->u.value, token->flags);
2241 /* Issue an error about name-lookup failing. NAME is the
2242 IDENTIFIER_NODE DECL is the result of
2243 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2244 the thing that we hoped to find. */
2247 cp_parser_name_lookup_error (cp_parser* parser,
2250 name_lookup_error desired,
2251 location_t location)
2253 /* If name lookup completely failed, tell the user that NAME was not
2255 if (decl == error_mark_node)
2257 if (parser->scope && parser->scope != global_namespace)
2258 error_at (location, "%<%E::%E%> has not been declared",
2259 parser->scope, name);
2260 else if (parser->scope == global_namespace)
2261 error_at (location, "%<::%E%> has not been declared", name);
2262 else if (parser->object_scope
2263 && !CLASS_TYPE_P (parser->object_scope))
2264 error_at (location, "request for member %qE in non-class type %qT",
2265 name, parser->object_scope);
2266 else if (parser->object_scope)
2267 error_at (location, "%<%T::%E%> has not been declared",
2268 parser->object_scope, name);
2270 error_at (location, "%qE has not been declared", name);
2272 else if (parser->scope && parser->scope != global_namespace)
2277 error_at (location, "%<%E::%E%> is not a type",
2278 parser->scope, name);
2281 error_at (location, "%<%E::%E%> is not a class or namespace",
2282 parser->scope, name);
2286 "%<%E::%E%> is not a class, namespace, or enumeration",
2287 parser->scope, name);
2294 else if (parser->scope == global_namespace)
2299 error_at (location, "%<::%E%> is not a type", name);
2302 error_at (location, "%<::%E%> is not a class or namespace", name);
2306 "%<::%E%> is not a class, namespace, or enumeration",
2318 error_at (location, "%qE is not a type", name);
2321 error_at (location, "%qE is not a class or namespace", name);
2325 "%qE is not a class, namespace, or enumeration", name);
2333 /* If we are parsing tentatively, remember that an error has occurred
2334 during this tentative parse. Returns true if the error was
2335 simulated; false if a message should be issued by the caller. */
2338 cp_parser_simulate_error (cp_parser* parser)
2340 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2342 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2348 /* Check for repeated decl-specifiers. */
2351 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2352 location_t location)
2356 for (ds = ds_first; ds != ds_last; ++ds)
2358 unsigned count = decl_specs->specs[ds];
2361 /* The "long" specifier is a special case because of "long long". */
2365 error_at (location, "%<long long long%> is too long for GCC");
2367 pedwarn_cxx98 (location, OPT_Wlong_long,
2368 "ISO C++ 1998 does not support %<long long%>");
2372 static const char *const decl_spec_names[] = {
2389 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2394 /* This function is called when a type is defined. If type
2395 definitions are forbidden at this point, an error message is
2399 cp_parser_check_type_definition (cp_parser* parser)
2401 /* If types are forbidden here, issue a message. */
2402 if (parser->type_definition_forbidden_message)
2404 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2405 in the message need to be interpreted. */
2406 error (parser->type_definition_forbidden_message);
2412 /* This function is called when the DECLARATOR is processed. The TYPE
2413 was a type defined in the decl-specifiers. If it is invalid to
2414 define a type in the decl-specifiers for DECLARATOR, an error is
2415 issued. TYPE_LOCATION is the location of TYPE and is used
2416 for error reporting. */
2419 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2420 tree type, location_t type_location)
2422 /* [dcl.fct] forbids type definitions in return types.
2423 Unfortunately, it's not easy to know whether or not we are
2424 processing a return type until after the fact. */
2426 && (declarator->kind == cdk_pointer
2427 || declarator->kind == cdk_reference
2428 || declarator->kind == cdk_ptrmem))
2429 declarator = declarator->declarator;
2431 && declarator->kind == cdk_function)
2433 error_at (type_location,
2434 "new types may not be defined in a return type");
2435 inform (type_location,
2436 "(perhaps a semicolon is missing after the definition of %qT)",
2441 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2442 "<" in any valid C++ program. If the next token is indeed "<",
2443 issue a message warning the user about what appears to be an
2444 invalid attempt to form a template-id. LOCATION is the location
2445 of the type-specifier (TYPE) */
2448 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2449 tree type, location_t location)
2451 cp_token_position start = 0;
2453 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2456 error_at (location, "%qT is not a template", type);
2457 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2458 error_at (location, "%qE is not a template", type);
2460 error_at (location, "invalid template-id");
2461 /* Remember the location of the invalid "<". */
2462 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2463 start = cp_lexer_token_position (parser->lexer, true);
2464 /* Consume the "<". */
2465 cp_lexer_consume_token (parser->lexer);
2466 /* Parse the template arguments. */
2467 cp_parser_enclosed_template_argument_list (parser);
2468 /* Permanently remove the invalid template arguments so that
2469 this error message is not issued again. */
2471 cp_lexer_purge_tokens_after (parser->lexer, start);
2475 /* If parsing an integral constant-expression, issue an error message
2476 about the fact that THING appeared and return true. Otherwise,
2477 return false. In either case, set
2478 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2481 cp_parser_non_integral_constant_expression (cp_parser *parser,
2482 non_integral_constant thing)
2484 parser->non_integral_constant_expression_p = true;
2485 if (parser->integral_constant_expression_p)
2487 if (!parser->allow_non_integral_constant_expression_p)
2489 const char *msg = NULL;
2493 error ("floating-point literal "
2494 "cannot appear in a constant-expression");
2497 error ("a cast to a type other than an integral or "
2498 "enumeration type cannot appear in a "
2499 "constant-expression");
2502 error ("%<typeid%> operator "
2503 "cannot appear in a constant-expression");
2506 error ("non-constant compound literals "
2507 "cannot appear in a constant-expression");
2510 error ("a function call "
2511 "cannot appear in a constant-expression");
2514 error ("an increment "
2515 "cannot appear in a constant-expression");
2518 error ("an decrement "
2519 "cannot appear in a constant-expression");
2522 error ("an array reference "
2523 "cannot appear in a constant-expression");
2525 case NIC_ADDR_LABEL:
2526 error ("the address of a label "
2527 "cannot appear in a constant-expression");
2529 case NIC_OVERLOADED:
2530 error ("calls to overloaded operators "
2531 "cannot appear in a constant-expression");
2533 case NIC_ASSIGNMENT:
2534 error ("an assignment cannot appear in a constant-expression");
2537 error ("a comma operator "
2538 "cannot appear in a constant-expression");
2540 case NIC_CONSTRUCTOR:
2541 error ("a call to a constructor "
2542 "cannot appear in a constant-expression");
2548 msg = "__FUNCTION__";
2550 case NIC_PRETTY_FUNC:
2551 msg = "__PRETTY_FUNCTION__";
2571 case NIC_PREINCREMENT:
2574 case NIC_PREDECREMENT:
2587 error ("%qs cannot appear in a constant-expression", msg);
2594 /* Emit a diagnostic for an invalid type name. SCOPE is the
2595 qualifying scope (or NULL, if none) for ID. This function commits
2596 to the current active tentative parse, if any. (Otherwise, the
2597 problematic construct might be encountered again later, resulting
2598 in duplicate error messages.) LOCATION is the location of ID. */
2601 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2602 tree scope, tree id,
2603 location_t location)
2605 tree decl, old_scope;
2606 /* Try to lookup the identifier. */
2607 old_scope = parser->scope;
2608 parser->scope = scope;
2609 decl = cp_parser_lookup_name_simple (parser, id, location);
2610 parser->scope = old_scope;
2611 /* If the lookup found a template-name, it means that the user forgot
2612 to specify an argument list. Emit a useful error message. */
2613 if (TREE_CODE (decl) == TEMPLATE_DECL)
2615 "invalid use of template-name %qE without an argument list",
2617 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2618 error_at (location, "invalid use of destructor %qD as a type", id);
2619 else if (TREE_CODE (decl) == TYPE_DECL)
2620 /* Something like 'unsigned A a;' */
2621 error_at (location, "invalid combination of multiple type-specifiers");
2622 else if (!parser->scope)
2624 /* Issue an error message. */
2625 error_at (location, "%qE does not name a type", id);
2626 /* If we're in a template class, it's possible that the user was
2627 referring to a type from a base class. For example:
2629 template <typename T> struct A { typedef T X; };
2630 template <typename T> struct B : public A<T> { X x; };
2632 The user should have said "typename A<T>::X". */
2633 if (processing_template_decl && current_class_type
2634 && TYPE_BINFO (current_class_type))
2638 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2642 tree base_type = BINFO_TYPE (b);
2643 if (CLASS_TYPE_P (base_type)
2644 && dependent_type_p (base_type))
2647 /* Go from a particular instantiation of the
2648 template (which will have an empty TYPE_FIELDs),
2649 to the main version. */
2650 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2651 for (field = TYPE_FIELDS (base_type);
2653 field = TREE_CHAIN (field))
2654 if (TREE_CODE (field) == TYPE_DECL
2655 && DECL_NAME (field) == id)
2658 "(perhaps %<typename %T::%E%> was intended)",
2659 BINFO_TYPE (b), id);
2668 /* Here we diagnose qualified-ids where the scope is actually correct,
2669 but the identifier does not resolve to a valid type name. */
2670 else if (parser->scope != error_mark_node)
2672 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2673 error_at (location, "%qE in namespace %qE does not name a type",
2675 else if (CLASS_TYPE_P (parser->scope)
2676 && constructor_name_p (id, parser->scope))
2679 error_at (location, "%<%T::%E%> names the constructor, not"
2680 " the type", parser->scope, id);
2681 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2682 error_at (location, "and %qT has no template constructors",
2685 else if (TYPE_P (parser->scope)
2686 && dependent_scope_p (parser->scope))
2687 error_at (location, "need %<typename%> before %<%T::%E%> because "
2688 "%qT is a dependent scope",
2689 parser->scope, id, parser->scope);
2690 else if (TYPE_P (parser->scope))
2691 error_at (location, "%qE in class %qT does not name a type",
2696 cp_parser_commit_to_tentative_parse (parser);
2699 /* Check for a common situation where a type-name should be present,
2700 but is not, and issue a sensible error message. Returns true if an
2701 invalid type-name was detected.
2703 The situation handled by this function are variable declarations of the
2704 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2705 Usually, `ID' should name a type, but if we got here it means that it
2706 does not. We try to emit the best possible error message depending on
2707 how exactly the id-expression looks like. */
2710 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2713 cp_token *token = cp_lexer_peek_token (parser->lexer);
2715 /* Avoid duplicate error about ambiguous lookup. */
2716 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2718 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2719 if (next->type == CPP_NAME && next->ambiguous_p)
2723 cp_parser_parse_tentatively (parser);
2724 id = cp_parser_id_expression (parser,
2725 /*template_keyword_p=*/false,
2726 /*check_dependency_p=*/true,
2727 /*template_p=*/NULL,
2728 /*declarator_p=*/true,
2729 /*optional_p=*/false);
2730 /* If the next token is a (, this is a function with no explicit return
2731 type, i.e. constructor, destructor or conversion op. */
2732 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2733 || TREE_CODE (id) == TYPE_DECL)
2735 cp_parser_abort_tentative_parse (parser);
2738 if (!cp_parser_parse_definitely (parser))
2741 /* Emit a diagnostic for the invalid type. */
2742 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2743 id, token->location);
2745 /* If we aren't in the middle of a declarator (i.e. in a
2746 parameter-declaration-clause), skip to the end of the declaration;
2747 there's no point in trying to process it. */
2748 if (!parser->in_declarator_p)
2749 cp_parser_skip_to_end_of_block_or_statement (parser);
2753 /* Consume tokens up to, and including, the next non-nested closing `)'.
2754 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2755 are doing error recovery. Returns -1 if OR_COMMA is true and we
2756 found an unnested comma. */
2759 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2764 unsigned paren_depth = 0;
2765 unsigned brace_depth = 0;
2766 unsigned square_depth = 0;
2768 if (recovering && !or_comma
2769 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2774 cp_token * token = cp_lexer_peek_token (parser->lexer);
2776 switch (token->type)
2779 case CPP_PRAGMA_EOL:
2780 /* If we've run out of tokens, then there is no closing `)'. */
2783 /* This is good for lambda expression capture-lists. */
2784 case CPP_OPEN_SQUARE:
2787 case CPP_CLOSE_SQUARE:
2788 if (!square_depth--)
2793 /* This matches the processing in skip_to_end_of_statement. */
2798 case CPP_OPEN_BRACE:
2801 case CPP_CLOSE_BRACE:
2807 if (recovering && or_comma && !brace_depth && !paren_depth
2812 case CPP_OPEN_PAREN:
2817 case CPP_CLOSE_PAREN:
2818 if (!brace_depth && !paren_depth--)
2821 cp_lexer_consume_token (parser->lexer);
2830 /* Consume the token. */
2831 cp_lexer_consume_token (parser->lexer);
2835 /* Consume tokens until we reach the end of the current statement.
2836 Normally, that will be just before consuming a `;'. However, if a
2837 non-nested `}' comes first, then we stop before consuming that. */
2840 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2842 unsigned nesting_depth = 0;
2846 cp_token *token = cp_lexer_peek_token (parser->lexer);
2848 switch (token->type)
2851 case CPP_PRAGMA_EOL:
2852 /* If we've run out of tokens, stop. */
2856 /* If the next token is a `;', we have reached the end of the
2862 case CPP_CLOSE_BRACE:
2863 /* If this is a non-nested '}', stop before consuming it.
2864 That way, when confronted with something like:
2868 we stop before consuming the closing '}', even though we
2869 have not yet reached a `;'. */
2870 if (nesting_depth == 0)
2873 /* If it is the closing '}' for a block that we have
2874 scanned, stop -- but only after consuming the token.
2880 we will stop after the body of the erroneously declared
2881 function, but before consuming the following `typedef'
2883 if (--nesting_depth == 0)
2885 cp_lexer_consume_token (parser->lexer);
2889 case CPP_OPEN_BRACE:
2897 /* Consume the token. */
2898 cp_lexer_consume_token (parser->lexer);
2902 /* This function is called at the end of a statement or declaration.
2903 If the next token is a semicolon, it is consumed; otherwise, error
2904 recovery is attempted. */
2907 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2909 /* Look for the trailing `;'. */
2910 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2912 /* If there is additional (erroneous) input, skip to the end of
2914 cp_parser_skip_to_end_of_statement (parser);
2915 /* If the next token is now a `;', consume it. */
2916 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2917 cp_lexer_consume_token (parser->lexer);
2921 /* Skip tokens until we have consumed an entire block, or until we
2922 have consumed a non-nested `;'. */
2925 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2927 int nesting_depth = 0;
2929 while (nesting_depth >= 0)
2931 cp_token *token = cp_lexer_peek_token (parser->lexer);
2933 switch (token->type)
2936 case CPP_PRAGMA_EOL:
2937 /* If we've run out of tokens, stop. */
2941 /* Stop if this is an unnested ';'. */
2946 case CPP_CLOSE_BRACE:
2947 /* Stop if this is an unnested '}', or closes the outermost
2950 if (nesting_depth < 0)
2956 case CPP_OPEN_BRACE:
2965 /* Consume the token. */
2966 cp_lexer_consume_token (parser->lexer);
2970 /* Skip tokens until a non-nested closing curly brace is the next
2971 token, or there are no more tokens. Return true in the first case,
2975 cp_parser_skip_to_closing_brace (cp_parser *parser)
2977 unsigned nesting_depth = 0;
2981 cp_token *token = cp_lexer_peek_token (parser->lexer);
2983 switch (token->type)
2986 case CPP_PRAGMA_EOL:
2987 /* If we've run out of tokens, stop. */
2990 case CPP_CLOSE_BRACE:
2991 /* If the next token is a non-nested `}', then we have reached
2992 the end of the current block. */
2993 if (nesting_depth-- == 0)
2997 case CPP_OPEN_BRACE:
2998 /* If it the next token is a `{', then we are entering a new
2999 block. Consume the entire block. */
3007 /* Consume the token. */
3008 cp_lexer_consume_token (parser->lexer);
3012 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3013 parameter is the PRAGMA token, allowing us to purge the entire pragma
3017 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3021 parser->lexer->in_pragma = false;
3024 token = cp_lexer_consume_token (parser->lexer);
3025 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3027 /* Ensure that the pragma is not parsed again. */
3028 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3031 /* Require pragma end of line, resyncing with it as necessary. The
3032 arguments are as for cp_parser_skip_to_pragma_eol. */
3035 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3037 parser->lexer->in_pragma = false;
3038 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3039 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3042 /* This is a simple wrapper around make_typename_type. When the id is
3043 an unresolved identifier node, we can provide a superior diagnostic
3044 using cp_parser_diagnose_invalid_type_name. */
3047 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3048 tree id, location_t id_location)
3051 if (TREE_CODE (id) == IDENTIFIER_NODE)
3053 result = make_typename_type (scope, id, typename_type,
3054 /*complain=*/tf_none);
3055 if (result == error_mark_node)
3056 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3059 return make_typename_type (scope, id, typename_type, tf_error);
3062 /* This is a wrapper around the
3063 make_{pointer,ptrmem,reference}_declarator functions that decides
3064 which one to call based on the CODE and CLASS_TYPE arguments. The
3065 CODE argument should be one of the values returned by
3066 cp_parser_ptr_operator. */
3067 static cp_declarator *
3068 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3069 cp_cv_quals cv_qualifiers,
3070 cp_declarator *target)
3072 if (code == ERROR_MARK)
3073 return cp_error_declarator;
3075 if (code == INDIRECT_REF)
3076 if (class_type == NULL_TREE)
3077 return make_pointer_declarator (cv_qualifiers, target);
3079 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3080 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3081 return make_reference_declarator (cv_qualifiers, target, false);
3082 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3083 return make_reference_declarator (cv_qualifiers, target, true);
3087 /* Create a new C++ parser. */
3090 cp_parser_new (void)
3096 /* cp_lexer_new_main is called before calling ggc_alloc because
3097 cp_lexer_new_main might load a PCH file. */
3098 lexer = cp_lexer_new_main ();
3100 /* Initialize the binops_by_token so that we can get the tree
3101 directly from the token. */
3102 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3103 binops_by_token[binops[i].token_type] = binops[i];
3105 parser = GGC_CNEW (cp_parser);
3106 parser->lexer = lexer;
3107 parser->context = cp_parser_context_new (NULL);
3109 /* For now, we always accept GNU extensions. */
3110 parser->allow_gnu_extensions_p = 1;
3112 /* The `>' token is a greater-than operator, not the end of a
3114 parser->greater_than_is_operator_p = true;
3116 parser->default_arg_ok_p = true;
3118 /* We are not parsing a constant-expression. */
3119 parser->integral_constant_expression_p = false;
3120 parser->allow_non_integral_constant_expression_p = false;
3121 parser->non_integral_constant_expression_p = false;
3123 /* Local variable names are not forbidden. */
3124 parser->local_variables_forbidden_p = false;
3126 /* We are not processing an `extern "C"' declaration. */
3127 parser->in_unbraced_linkage_specification_p = false;
3129 /* We are not processing a declarator. */
3130 parser->in_declarator_p = false;
3132 /* We are not processing a template-argument-list. */
3133 parser->in_template_argument_list_p = false;
3135 /* We are not in an iteration statement. */
3136 parser->in_statement = 0;
3138 /* We are not in a switch statement. */
3139 parser->in_switch_statement_p = false;
3141 /* We are not parsing a type-id inside an expression. */
3142 parser->in_type_id_in_expr_p = false;
3144 /* Declarations aren't implicitly extern "C". */
3145 parser->implicit_extern_c = false;
3147 /* String literals should be translated to the execution character set. */
3148 parser->translate_strings_p = true;
3150 /* We are not parsing a function body. */
3151 parser->in_function_body = false;
3153 /* The unparsed function queue is empty. */
3154 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
3156 /* There are no classes being defined. */
3157 parser->num_classes_being_defined = 0;
3159 /* No template parameters apply. */
3160 parser->num_template_parameter_lists = 0;
3165 /* Create a cp_lexer structure which will emit the tokens in CACHE
3166 and push it onto the parser's lexer stack. This is used for delayed
3167 parsing of in-class method bodies and default arguments, and should
3168 not be confused with tentative parsing. */
3170 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3172 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3173 lexer->next = parser->lexer;
3174 parser->lexer = lexer;
3176 /* Move the current source position to that of the first token in the
3178 cp_lexer_set_source_position_from_token (lexer->next_token);
3181 /* Pop the top lexer off the parser stack. This is never used for the
3182 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3184 cp_parser_pop_lexer (cp_parser *parser)
3186 cp_lexer *lexer = parser->lexer;
3187 parser->lexer = lexer->next;
3188 cp_lexer_destroy (lexer);
3190 /* Put the current source position back where it was before this
3191 lexer was pushed. */
3192 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3195 /* Lexical conventions [gram.lex] */
3197 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3201 cp_parser_identifier (cp_parser* parser)
3205 /* Look for the identifier. */
3206 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3207 /* Return the value. */
3208 return token ? token->u.value : error_mark_node;
3211 /* Parse a sequence of adjacent string constants. Returns a
3212 TREE_STRING representing the combined, nul-terminated string
3213 constant. If TRANSLATE is true, translate the string to the
3214 execution character set. If WIDE_OK is true, a wide string is
3217 C++98 [lex.string] says that if a narrow string literal token is
3218 adjacent to a wide string literal token, the behavior is undefined.
3219 However, C99 6.4.5p4 says that this results in a wide string literal.
3220 We follow C99 here, for consistency with the C front end.
3222 This code is largely lifted from lex_string() in c-lex.c.
3224 FUTURE: ObjC++ will need to handle @-strings here. */
3226 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3230 struct obstack str_ob;
3231 cpp_string str, istr, *strs;
3233 enum cpp_ttype type;
3235 tok = cp_lexer_peek_token (parser->lexer);
3236 if (!cp_parser_is_string_literal (tok))
3238 cp_parser_error (parser, "expected string-literal");
3239 return error_mark_node;
3244 /* Try to avoid the overhead of creating and destroying an obstack
3245 for the common case of just one string. */
3246 if (!cp_parser_is_string_literal
3247 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3249 cp_lexer_consume_token (parser->lexer);
3251 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3252 str.len = TREE_STRING_LENGTH (tok->u.value);
3259 gcc_obstack_init (&str_ob);
3264 cp_lexer_consume_token (parser->lexer);
3266 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3267 str.len = TREE_STRING_LENGTH (tok->u.value);
3269 if (type != tok->type)
3271 if (type == CPP_STRING)
3273 else if (tok->type != CPP_STRING)
3274 error_at (tok->location,
3275 "unsupported non-standard concatenation "
3276 "of string literals");
3279 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3281 tok = cp_lexer_peek_token (parser->lexer);
3283 while (cp_parser_is_string_literal (tok));
3285 strs = (cpp_string *) obstack_finish (&str_ob);
3288 if (type != CPP_STRING && !wide_ok)
3290 cp_parser_error (parser, "a wide string is invalid in this context");
3294 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3295 (parse_in, strs, count, &istr, type))
3297 value = build_string (istr.len, (const char *)istr.text);
3298 free (CONST_CAST (unsigned char *, istr.text));
3304 case CPP_UTF8STRING:
3305 TREE_TYPE (value) = char_array_type_node;
3308 TREE_TYPE (value) = char16_array_type_node;
3311 TREE_TYPE (value) = char32_array_type_node;
3314 TREE_TYPE (value) = wchar_array_type_node;
3318 value = fix_string_type (value);
3321 /* cpp_interpret_string has issued an error. */
3322 value = error_mark_node;
3325 obstack_free (&str_ob, 0);
3331 /* Basic concepts [gram.basic] */
3333 /* Parse a translation-unit.
3336 declaration-seq [opt]
3338 Returns TRUE if all went well. */
3341 cp_parser_translation_unit (cp_parser* parser)
3343 /* The address of the first non-permanent object on the declarator
3345 static void *declarator_obstack_base;
3349 /* Create the declarator obstack, if necessary. */
3350 if (!cp_error_declarator)
3352 gcc_obstack_init (&declarator_obstack);
3353 /* Create the error declarator. */
3354 cp_error_declarator = make_declarator (cdk_error);
3355 /* Create the empty parameter list. */
3356 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3357 /* Remember where the base of the declarator obstack lies. */
3358 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3361 cp_parser_declaration_seq_opt (parser);
3363 /* If there are no tokens left then all went well. */
3364 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3366 /* Get rid of the token array; we don't need it any more. */
3367 cp_lexer_destroy (parser->lexer);
3368 parser->lexer = NULL;
3370 /* This file might have been a context that's implicitly extern
3371 "C". If so, pop the lang context. (Only relevant for PCH.) */
3372 if (parser->implicit_extern_c)
3374 pop_lang_context ();
3375 parser->implicit_extern_c = false;
3379 finish_translation_unit ();
3385 cp_parser_error (parser, "expected declaration");
3389 /* Make sure the declarator obstack was fully cleaned up. */
3390 gcc_assert (obstack_next_free (&declarator_obstack)
3391 == declarator_obstack_base);
3393 /* All went well. */
3397 /* Expressions [gram.expr] */
3399 /* Parse a primary-expression.
3410 ( compound-statement )
3411 __builtin_va_arg ( assignment-expression , type-id )
3412 __builtin_offsetof ( type-id , offsetof-expression )
3415 __has_nothrow_assign ( type-id )
3416 __has_nothrow_constructor ( type-id )
3417 __has_nothrow_copy ( type-id )
3418 __has_trivial_assign ( type-id )
3419 __has_trivial_constructor ( type-id )
3420 __has_trivial_copy ( type-id )
3421 __has_trivial_destructor ( type-id )
3422 __has_virtual_destructor ( type-id )
3423 __is_abstract ( type-id )
3424 __is_base_of ( type-id , type-id )
3425 __is_class ( type-id )
3426 __is_convertible_to ( type-id , type-id )
3427 __is_empty ( type-id )
3428 __is_enum ( type-id )
3429 __is_pod ( type-id )
3430 __is_polymorphic ( type-id )
3431 __is_union ( type-id )
3433 Objective-C++ Extension:
3441 ADDRESS_P is true iff this expression was immediately preceded by
3442 "&" and therefore might denote a pointer-to-member. CAST_P is true
3443 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3444 true iff this expression is a template argument.
3446 Returns a representation of the expression. Upon return, *IDK
3447 indicates what kind of id-expression (if any) was present. */
3450 cp_parser_primary_expression (cp_parser *parser,
3453 bool template_arg_p,
3456 cp_token *token = NULL;
3458 /* Assume the primary expression is not an id-expression. */
3459 *idk = CP_ID_KIND_NONE;
3461 /* Peek at the next token. */
3462 token = cp_lexer_peek_token (parser->lexer);
3463 switch (token->type)
3476 token = cp_lexer_consume_token (parser->lexer);
3477 if (TREE_CODE (token->u.value) == FIXED_CST)
3479 error_at (token->location,
3480 "fixed-point types not supported in C++");
3481 return error_mark_node;
3483 /* Floating-point literals are only allowed in an integral
3484 constant expression if they are cast to an integral or
3485 enumeration type. */
3486 if (TREE_CODE (token->u.value) == REAL_CST
3487 && parser->integral_constant_expression_p
3490 /* CAST_P will be set even in invalid code like "int(2.7 +
3491 ...)". Therefore, we have to check that the next token
3492 is sure to end the cast. */
3495 cp_token *next_token;
3497 next_token = cp_lexer_peek_token (parser->lexer);
3498 if (/* The comma at the end of an
3499 enumerator-definition. */
3500 next_token->type != CPP_COMMA
3501 /* The curly brace at the end of an enum-specifier. */
3502 && next_token->type != CPP_CLOSE_BRACE
3503 /* The end of a statement. */
3504 && next_token->type != CPP_SEMICOLON
3505 /* The end of the cast-expression. */
3506 && next_token->type != CPP_CLOSE_PAREN
3507 /* The end of an array bound. */
3508 && next_token->type != CPP_CLOSE_SQUARE
3509 /* The closing ">" in a template-argument-list. */
3510 && (next_token->type != CPP_GREATER
3511 || parser->greater_than_is_operator_p)
3512 /* C++0x only: A ">>" treated like two ">" tokens,
3513 in a template-argument-list. */
3514 && (next_token->type != CPP_RSHIFT
3515 || (cxx_dialect == cxx98)
3516 || parser->greater_than_is_operator_p))
3520 /* If we are within a cast, then the constraint that the
3521 cast is to an integral or enumeration type will be
3522 checked at that point. If we are not within a cast, then
3523 this code is invalid. */
3525 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3527 return token->u.value;
3533 case CPP_UTF8STRING:
3534 /* ??? Should wide strings be allowed when parser->translate_strings_p
3535 is false (i.e. in attributes)? If not, we can kill the third
3536 argument to cp_parser_string_literal. */
3537 return cp_parser_string_literal (parser,
3538 parser->translate_strings_p,
3541 case CPP_OPEN_PAREN:
3544 bool saved_greater_than_is_operator_p;
3546 /* Consume the `('. */
3547 cp_lexer_consume_token (parser->lexer);
3548 /* Within a parenthesized expression, a `>' token is always
3549 the greater-than operator. */
3550 saved_greater_than_is_operator_p
3551 = parser->greater_than_is_operator_p;
3552 parser->greater_than_is_operator_p = true;
3553 /* If we see `( { ' then we are looking at the beginning of
3554 a GNU statement-expression. */
3555 if (cp_parser_allow_gnu_extensions_p (parser)
3556 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3558 /* Statement-expressions are not allowed by the standard. */
3559 pedwarn (token->location, OPT_pedantic,
3560 "ISO C++ forbids braced-groups within expressions");
3562 /* And they're not allowed outside of a function-body; you
3563 cannot, for example, write:
3565 int i = ({ int j = 3; j + 1; });
3567 at class or namespace scope. */
3568 if (!parser->in_function_body
3569 || parser->in_template_argument_list_p)
3571 error_at (token->location,
3572 "statement-expressions are not allowed outside "
3573 "functions nor in template-argument lists");
3574 cp_parser_skip_to_end_of_block_or_statement (parser);
3575 expr = error_mark_node;
3579 /* Start the statement-expression. */
3580 expr = begin_stmt_expr ();
3581 /* Parse the compound-statement. */
3582 cp_parser_compound_statement (parser, expr, false);
3584 expr = finish_stmt_expr (expr, false);
3589 /* Parse the parenthesized expression. */
3590 expr = cp_parser_expression (parser, cast_p, idk);
3591 /* Let the front end know that this expression was
3592 enclosed in parentheses. This matters in case, for
3593 example, the expression is of the form `A::B', since
3594 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3596 finish_parenthesized_expr (expr);
3598 /* The `>' token might be the end of a template-id or
3599 template-parameter-list now. */
3600 parser->greater_than_is_operator_p
3601 = saved_greater_than_is_operator_p;
3602 /* Consume the `)'. */
3603 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3604 cp_parser_skip_to_end_of_statement (parser);
3609 case CPP_OPEN_SQUARE:
3610 if (c_dialect_objc ())
3611 /* We have an Objective-C++ message. */
3612 return cp_parser_objc_expression (parser);
3613 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3614 return cp_parser_lambda_expression (parser);
3616 case CPP_OBJC_STRING:
3617 if (c_dialect_objc ())
3618 /* We have an Objective-C++ string literal. */
3619 return cp_parser_objc_expression (parser);
3620 cp_parser_error (parser, "expected primary-expression");
3621 return error_mark_node;
3624 switch (token->keyword)
3626 /* These two are the boolean literals. */
3628 cp_lexer_consume_token (parser->lexer);
3629 return boolean_true_node;
3631 cp_lexer_consume_token (parser->lexer);
3632 return boolean_false_node;
3634 /* The `__null' literal. */
3636 cp_lexer_consume_token (parser->lexer);
3639 /* The `nullptr' literal. */
3641 cp_lexer_consume_token (parser->lexer);
3642 return nullptr_node;
3644 /* Recognize the `this' keyword. */
3646 cp_lexer_consume_token (parser->lexer);
3647 if (parser->local_variables_forbidden_p)
3649 error_at (token->location,
3650 "%<this%> may not be used in this context");
3651 return error_mark_node;
3653 /* Pointers cannot appear in constant-expressions. */
3654 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3655 return error_mark_node;
3656 return finish_this_expr ();
3658 /* The `operator' keyword can be the beginning of an
3663 case RID_FUNCTION_NAME:
3664 case RID_PRETTY_FUNCTION_NAME:
3665 case RID_C99_FUNCTION_NAME:
3667 non_integral_constant name;
3669 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3670 __func__ are the names of variables -- but they are
3671 treated specially. Therefore, they are handled here,
3672 rather than relying on the generic id-expression logic
3673 below. Grammatically, these names are id-expressions.
3675 Consume the token. */
3676 token = cp_lexer_consume_token (parser->lexer);
3678 switch (token->keyword)
3680 case RID_FUNCTION_NAME:
3681 name = NIC_FUNC_NAME;
3683 case RID_PRETTY_FUNCTION_NAME:
3684 name = NIC_PRETTY_FUNC;
3686 case RID_C99_FUNCTION_NAME:
3687 name = NIC_C99_FUNC;
3693 if (cp_parser_non_integral_constant_expression (parser, name))
3694 return error_mark_node;
3696 /* Look up the name. */
3697 return finish_fname (token->u.value);
3705 /* The `__builtin_va_arg' construct is used to handle
3706 `va_arg'. Consume the `__builtin_va_arg' token. */
3707 cp_lexer_consume_token (parser->lexer);
3708 /* Look for the opening `('. */
3709 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3710 /* Now, parse the assignment-expression. */
3711 expression = cp_parser_assignment_expression (parser,
3712 /*cast_p=*/false, NULL);
3713 /* Look for the `,'. */
3714 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3715 /* Parse the type-id. */
3716 type = cp_parser_type_id (parser);
3717 /* Look for the closing `)'. */
3718 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3719 /* Using `va_arg' in a constant-expression is not
3721 if (cp_parser_non_integral_constant_expression (parser,
3723 return error_mark_node;
3724 return build_x_va_arg (expression, type);
3728 return cp_parser_builtin_offsetof (parser);
3730 case RID_HAS_NOTHROW_ASSIGN:
3731 case RID_HAS_NOTHROW_CONSTRUCTOR:
3732 case RID_HAS_NOTHROW_COPY:
3733 case RID_HAS_TRIVIAL_ASSIGN:
3734 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3735 case RID_HAS_TRIVIAL_COPY:
3736 case RID_HAS_TRIVIAL_DESTRUCTOR:
3737 case RID_HAS_VIRTUAL_DESTRUCTOR:
3738 case RID_IS_ABSTRACT:
3739 case RID_IS_BASE_OF:
3741 case RID_IS_CONVERTIBLE_TO:
3745 case RID_IS_POLYMORPHIC:
3746 case RID_IS_STD_LAYOUT:
3747 case RID_IS_TRIVIAL:
3749 return cp_parser_trait_expr (parser, token->keyword);
3751 /* Objective-C++ expressions. */
3753 case RID_AT_PROTOCOL:
3754 case RID_AT_SELECTOR:
3755 return cp_parser_objc_expression (parser);
3758 cp_parser_error (parser, "expected primary-expression");
3759 return error_mark_node;
3762 /* An id-expression can start with either an identifier, a
3763 `::' as the beginning of a qualified-id, or the "operator"
3767 case CPP_TEMPLATE_ID:
3768 case CPP_NESTED_NAME_SPECIFIER:
3772 const char *error_msg;
3775 cp_token *id_expr_token;
3778 /* Parse the id-expression. */
3780 = cp_parser_id_expression (parser,
3781 /*template_keyword_p=*/false,
3782 /*check_dependency_p=*/true,
3784 /*declarator_p=*/false,
3785 /*optional_p=*/false);
3786 if (id_expression == error_mark_node)
3787 return error_mark_node;
3788 id_expr_token = token;
3789 token = cp_lexer_peek_token (parser->lexer);
3790 done = (token->type != CPP_OPEN_SQUARE
3791 && token->type != CPP_OPEN_PAREN
3792 && token->type != CPP_DOT
3793 && token->type != CPP_DEREF
3794 && token->type != CPP_PLUS_PLUS
3795 && token->type != CPP_MINUS_MINUS);
3796 /* If we have a template-id, then no further lookup is
3797 required. If the template-id was for a template-class, we
3798 will sometimes have a TYPE_DECL at this point. */
3799 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3800 || TREE_CODE (id_expression) == TYPE_DECL)
3801 decl = id_expression;
3802 /* Look up the name. */
3805 tree ambiguous_decls;
3807 /* If we already know that this lookup is ambiguous, then
3808 we've already issued an error message; there's no reason
3810 if (id_expr_token->type == CPP_NAME
3811 && id_expr_token->ambiguous_p)
3813 cp_parser_simulate_error (parser);
3814 return error_mark_node;
3817 decl = cp_parser_lookup_name (parser, id_expression,
3820 /*is_namespace=*/false,
3821 /*check_dependency=*/true,
3823 id_expr_token->location);
3824 /* If the lookup was ambiguous, an error will already have
3826 if (ambiguous_decls)
3827 return error_mark_node;
3829 /* In Objective-C++, an instance variable (ivar) may be preferred
3830 to whatever cp_parser_lookup_name() found. */
3831 decl = objc_lookup_ivar (decl, id_expression);
3833 /* If name lookup gives us a SCOPE_REF, then the
3834 qualifying scope was dependent. */
3835 if (TREE_CODE (decl) == SCOPE_REF)
3837 /* At this point, we do not know if DECL is a valid
3838 integral constant expression. We assume that it is
3839 in fact such an expression, so that code like:
3841 template <int N> struct A {
3845 is accepted. At template-instantiation time, we
3846 will check that B<N>::i is actually a constant. */
3849 /* Check to see if DECL is a local variable in a context
3850 where that is forbidden. */
3851 if (parser->local_variables_forbidden_p
3852 && local_variable_p (decl))
3854 /* It might be that we only found DECL because we are
3855 trying to be generous with pre-ISO scoping rules.
3856 For example, consider:
3860 for (int i = 0; i < 10; ++i) {}
3861 extern void f(int j = i);
3864 Here, name look up will originally find the out
3865 of scope `i'. We need to issue a warning message,
3866 but then use the global `i'. */
3867 decl = check_for_out_of_scope_variable (decl);
3868 if (local_variable_p (decl))
3870 error_at (id_expr_token->location,
3871 "local variable %qD may not appear in this context",
3873 return error_mark_node;
3878 decl = (finish_id_expression
3879 (id_expression, decl, parser->scope,
3881 parser->integral_constant_expression_p,
3882 parser->allow_non_integral_constant_expression_p,
3883 &parser->non_integral_constant_expression_p,
3884 template_p, done, address_p,
3887 id_expr_token->location));
3889 cp_parser_error (parser, error_msg);
3893 /* Anything else is an error. */
3895 cp_parser_error (parser, "expected primary-expression");
3896 return error_mark_node;
3900 /* Parse an id-expression.
3907 :: [opt] nested-name-specifier template [opt] unqualified-id
3909 :: operator-function-id
3912 Return a representation of the unqualified portion of the
3913 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3914 a `::' or nested-name-specifier.
3916 Often, if the id-expression was a qualified-id, the caller will
3917 want to make a SCOPE_REF to represent the qualified-id. This
3918 function does not do this in order to avoid wastefully creating
3919 SCOPE_REFs when they are not required.
3921 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3924 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3925 uninstantiated templates.
3927 If *TEMPLATE_P is non-NULL, it is set to true iff the
3928 `template' keyword is used to explicitly indicate that the entity
3929 named is a template.
3931 If DECLARATOR_P is true, the id-expression is appearing as part of
3932 a declarator, rather than as part of an expression. */
3935 cp_parser_id_expression (cp_parser *parser,
3936 bool template_keyword_p,
3937 bool check_dependency_p,
3942 bool global_scope_p;
3943 bool nested_name_specifier_p;
3945 /* Assume the `template' keyword was not used. */
3947 *template_p = template_keyword_p;
3949 /* Look for the optional `::' operator. */
3951 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3953 /* Look for the optional nested-name-specifier. */
3954 nested_name_specifier_p
3955 = (cp_parser_nested_name_specifier_opt (parser,
3956 /*typename_keyword_p=*/false,
3961 /* If there is a nested-name-specifier, then we are looking at
3962 the first qualified-id production. */
3963 if (nested_name_specifier_p)
3966 tree saved_object_scope;
3967 tree saved_qualifying_scope;
3968 tree unqualified_id;
3971 /* See if the next token is the `template' keyword. */
3973 template_p = &is_template;
3974 *template_p = cp_parser_optional_template_keyword (parser);
3975 /* Name lookup we do during the processing of the
3976 unqualified-id might obliterate SCOPE. */
3977 saved_scope = parser->scope;
3978 saved_object_scope = parser->object_scope;
3979 saved_qualifying_scope = parser->qualifying_scope;
3980 /* Process the final unqualified-id. */
3981 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3984 /*optional_p=*/false);
3985 /* Restore the SAVED_SCOPE for our caller. */
3986 parser->scope = saved_scope;
3987 parser->object_scope = saved_object_scope;
3988 parser->qualifying_scope = saved_qualifying_scope;
3990 return unqualified_id;
3992 /* Otherwise, if we are in global scope, then we are looking at one
3993 of the other qualified-id productions. */
3994 else if (global_scope_p)
3999 /* Peek at the next token. */
4000 token = cp_lexer_peek_token (parser->lexer);
4002 /* If it's an identifier, and the next token is not a "<", then
4003 we can avoid the template-id case. This is an optimization
4004 for this common case. */
4005 if (token->type == CPP_NAME
4006 && !cp_parser_nth_token_starts_template_argument_list_p
4008 return cp_parser_identifier (parser);
4010 cp_parser_parse_tentatively (parser);
4011 /* Try a template-id. */
4012 id = cp_parser_template_id (parser,
4013 /*template_keyword_p=*/false,
4014 /*check_dependency_p=*/true,
4016 /* If that worked, we're done. */
4017 if (cp_parser_parse_definitely (parser))
4020 /* Peek at the next token. (Changes in the token buffer may
4021 have invalidated the pointer obtained above.) */
4022 token = cp_lexer_peek_token (parser->lexer);
4024 switch (token->type)
4027 return cp_parser_identifier (parser);
4030 if (token->keyword == RID_OPERATOR)
4031 return cp_parser_operator_function_id (parser);
4035 cp_parser_error (parser, "expected id-expression");
4036 return error_mark_node;
4040 return cp_parser_unqualified_id (parser, template_keyword_p,
4041 /*check_dependency_p=*/true,
4046 /* Parse an unqualified-id.
4050 operator-function-id
4051 conversion-function-id
4055 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4056 keyword, in a construct like `A::template ...'.
4058 Returns a representation of unqualified-id. For the `identifier'
4059 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4060 production a BIT_NOT_EXPR is returned; the operand of the
4061 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4062 other productions, see the documentation accompanying the
4063 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4064 names are looked up in uninstantiated templates. If DECLARATOR_P
4065 is true, the unqualified-id is appearing as part of a declarator,
4066 rather than as part of an expression. */
4069 cp_parser_unqualified_id (cp_parser* parser,
4070 bool template_keyword_p,
4071 bool check_dependency_p,
4077 /* Peek at the next token. */
4078 token = cp_lexer_peek_token (parser->lexer);
4080 switch (token->type)
4086 /* We don't know yet whether or not this will be a
4088 cp_parser_parse_tentatively (parser);
4089 /* Try a template-id. */
4090 id = cp_parser_template_id (parser, template_keyword_p,
4093 /* If it worked, we're done. */
4094 if (cp_parser_parse_definitely (parser))
4096 /* Otherwise, it's an ordinary identifier. */
4097 return cp_parser_identifier (parser);
4100 case CPP_TEMPLATE_ID:
4101 return cp_parser_template_id (parser, template_keyword_p,
4108 tree qualifying_scope;
4113 /* Consume the `~' token. */
4114 cp_lexer_consume_token (parser->lexer);
4115 /* Parse the class-name. The standard, as written, seems to
4118 template <typename T> struct S { ~S (); };
4119 template <typename T> S<T>::~S() {}
4121 is invalid, since `~' must be followed by a class-name, but
4122 `S<T>' is dependent, and so not known to be a class.
4123 That's not right; we need to look in uninstantiated
4124 templates. A further complication arises from:
4126 template <typename T> void f(T t) {
4130 Here, it is not possible to look up `T' in the scope of `T'
4131 itself. We must look in both the current scope, and the
4132 scope of the containing complete expression.
4134 Yet another issue is:
4143 The standard does not seem to say that the `S' in `~S'
4144 should refer to the type `S' and not the data member
4147 /* DR 244 says that we look up the name after the "~" in the
4148 same scope as we looked up the qualifying name. That idea
4149 isn't fully worked out; it's more complicated than that. */
4150 scope = parser->scope;
4151 object_scope = parser->object_scope;
4152 qualifying_scope = parser->qualifying_scope;
4154 /* Check for invalid scopes. */
4155 if (scope == error_mark_node)
4157 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4158 cp_lexer_consume_token (parser->lexer);
4159 return error_mark_node;
4161 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4163 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4164 error_at (token->location,
4165 "scope %qT before %<~%> is not a class-name",
4167 cp_parser_simulate_error (parser);
4168 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4169 cp_lexer_consume_token (parser->lexer);
4170 return error_mark_node;
4172 gcc_assert (!scope || TYPE_P (scope));
4174 /* If the name is of the form "X::~X" it's OK even if X is a
4176 token = cp_lexer_peek_token (parser->lexer);
4178 && token->type == CPP_NAME
4179 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4181 && (token->u.value == TYPE_IDENTIFIER (scope)
4182 || constructor_name_p (token->u.value, scope)))
4184 cp_lexer_consume_token (parser->lexer);
4185 return build_nt (BIT_NOT_EXPR, scope);
4188 /* If there was an explicit qualification (S::~T), first look
4189 in the scope given by the qualification (i.e., S).
4191 Note: in the calls to cp_parser_class_name below we pass
4192 typename_type so that lookup finds the injected-class-name
4193 rather than the constructor. */
4195 type_decl = NULL_TREE;
4198 cp_parser_parse_tentatively (parser);
4199 type_decl = cp_parser_class_name (parser,
4200 /*typename_keyword_p=*/false,
4201 /*template_keyword_p=*/false,
4203 /*check_dependency=*/false,
4204 /*class_head_p=*/false,
4206 if (cp_parser_parse_definitely (parser))
4209 /* In "N::S::~S", look in "N" as well. */
4210 if (!done && scope && qualifying_scope)
4212 cp_parser_parse_tentatively (parser);
4213 parser->scope = qualifying_scope;
4214 parser->object_scope = NULL_TREE;
4215 parser->qualifying_scope = NULL_TREE;
4217 = cp_parser_class_name (parser,
4218 /*typename_keyword_p=*/false,
4219 /*template_keyword_p=*/false,
4221 /*check_dependency=*/false,
4222 /*class_head_p=*/false,
4224 if (cp_parser_parse_definitely (parser))
4227 /* In "p->S::~T", look in the scope given by "*p" as well. */
4228 else if (!done && object_scope)
4230 cp_parser_parse_tentatively (parser);
4231 parser->scope = object_scope;
4232 parser->object_scope = NULL_TREE;
4233 parser->qualifying_scope = NULL_TREE;
4235 = cp_parser_class_name (parser,
4236 /*typename_keyword_p=*/false,
4237 /*template_keyword_p=*/false,
4239 /*check_dependency=*/false,
4240 /*class_head_p=*/false,
4242 if (cp_parser_parse_definitely (parser))
4245 /* Look in the surrounding context. */
4248 parser->scope = NULL_TREE;
4249 parser->object_scope = NULL_TREE;
4250 parser->qualifying_scope = NULL_TREE;
4251 if (processing_template_decl)
4252 cp_parser_parse_tentatively (parser);
4254 = cp_parser_class_name (parser,
4255 /*typename_keyword_p=*/false,
4256 /*template_keyword_p=*/false,
4258 /*check_dependency=*/false,
4259 /*class_head_p=*/false,
4261 if (processing_template_decl
4262 && ! cp_parser_parse_definitely (parser))
4264 /* We couldn't find a type with this name, so just accept
4265 it and check for a match at instantiation time. */
4266 type_decl = cp_parser_identifier (parser);
4267 if (type_decl != error_mark_node)
4268 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4272 /* If an error occurred, assume that the name of the
4273 destructor is the same as the name of the qualifying
4274 class. That allows us to keep parsing after running
4275 into ill-formed destructor names. */
4276 if (type_decl == error_mark_node && scope)
4277 return build_nt (BIT_NOT_EXPR, scope);
4278 else if (type_decl == error_mark_node)
4279 return error_mark_node;
4281 /* Check that destructor name and scope match. */
4282 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4284 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4285 error_at (token->location,
4286 "declaration of %<~%T%> as member of %qT",
4288 cp_parser_simulate_error (parser);
4289 return error_mark_node;
4294 A typedef-name that names a class shall not be used as the
4295 identifier in the declarator for a destructor declaration. */
4297 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4298 && !DECL_SELF_REFERENCE_P (type_decl)
4299 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4300 error_at (token->location,
4301 "typedef-name %qD used as destructor declarator",
4304 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4308 if (token->keyword == RID_OPERATOR)
4312 /* This could be a template-id, so we try that first. */
4313 cp_parser_parse_tentatively (parser);
4314 /* Try a template-id. */
4315 id = cp_parser_template_id (parser, template_keyword_p,
4316 /*check_dependency_p=*/true,
4318 /* If that worked, we're done. */
4319 if (cp_parser_parse_definitely (parser))
4321 /* We still don't know whether we're looking at an
4322 operator-function-id or a conversion-function-id. */
4323 cp_parser_parse_tentatively (parser);
4324 /* Try an operator-function-id. */
4325 id = cp_parser_operator_function_id (parser);
4326 /* If that didn't work, try a conversion-function-id. */
4327 if (!cp_parser_parse_definitely (parser))
4328 id = cp_parser_conversion_function_id (parser);
4337 cp_parser_error (parser, "expected unqualified-id");
4338 return error_mark_node;
4342 /* Parse an (optional) nested-name-specifier.
4344 nested-name-specifier: [C++98]
4345 class-or-namespace-name :: nested-name-specifier [opt]
4346 class-or-namespace-name :: template nested-name-specifier [opt]
4348 nested-name-specifier: [C++0x]
4351 nested-name-specifier identifier ::
4352 nested-name-specifier template [opt] simple-template-id ::
4354 PARSER->SCOPE should be set appropriately before this function is
4355 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4356 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4359 Sets PARSER->SCOPE to the class (TYPE) or namespace
4360 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4361 it unchanged if there is no nested-name-specifier. Returns the new
4362 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4364 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4365 part of a declaration and/or decl-specifier. */
4368 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4369 bool typename_keyword_p,
4370 bool check_dependency_p,
4372 bool is_declaration)
4374 bool success = false;
4375 cp_token_position start = 0;
4378 /* Remember where the nested-name-specifier starts. */
4379 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4381 start = cp_lexer_token_position (parser->lexer, false);
4382 push_deferring_access_checks (dk_deferred);
4389 tree saved_qualifying_scope;
4390 bool template_keyword_p;
4392 /* Spot cases that cannot be the beginning of a
4393 nested-name-specifier. */
4394 token = cp_lexer_peek_token (parser->lexer);
4396 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4397 the already parsed nested-name-specifier. */
4398 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4400 /* Grab the nested-name-specifier and continue the loop. */
4401 cp_parser_pre_parsed_nested_name_specifier (parser);
4402 /* If we originally encountered this nested-name-specifier
4403 with IS_DECLARATION set to false, we will not have
4404 resolved TYPENAME_TYPEs, so we must do so here. */
4406 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4408 new_scope = resolve_typename_type (parser->scope,
4409 /*only_current_p=*/false);
4410 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4411 parser->scope = new_scope;
4417 /* Spot cases that cannot be the beginning of a
4418 nested-name-specifier. On the second and subsequent times
4419 through the loop, we look for the `template' keyword. */
4420 if (success && token->keyword == RID_TEMPLATE)
4422 /* A template-id can start a nested-name-specifier. */
4423 else if (token->type == CPP_TEMPLATE_ID)
4427 /* If the next token is not an identifier, then it is
4428 definitely not a type-name or namespace-name. */
4429 if (token->type != CPP_NAME)
4431 /* If the following token is neither a `<' (to begin a
4432 template-id), nor a `::', then we are not looking at a
4433 nested-name-specifier. */
4434 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4435 if (token->type != CPP_SCOPE
4436 && !cp_parser_nth_token_starts_template_argument_list_p
4441 /* The nested-name-specifier is optional, so we parse
4443 cp_parser_parse_tentatively (parser);
4445 /* Look for the optional `template' keyword, if this isn't the
4446 first time through the loop. */
4448 template_keyword_p = cp_parser_optional_template_keyword (parser);
4450 template_keyword_p = false;
4452 /* Save the old scope since the name lookup we are about to do
4453 might destroy it. */
4454 old_scope = parser->scope;
4455 saved_qualifying_scope = parser->qualifying_scope;
4456 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4457 look up names in "X<T>::I" in order to determine that "Y" is
4458 a template. So, if we have a typename at this point, we make
4459 an effort to look through it. */
4461 && !typename_keyword_p
4463 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4464 parser->scope = resolve_typename_type (parser->scope,
4465 /*only_current_p=*/false);
4466 /* Parse the qualifying entity. */
4468 = cp_parser_qualifying_entity (parser,
4474 /* Look for the `::' token. */
4475 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4477 /* If we found what we wanted, we keep going; otherwise, we're
4479 if (!cp_parser_parse_definitely (parser))
4481 bool error_p = false;
4483 /* Restore the OLD_SCOPE since it was valid before the
4484 failed attempt at finding the last
4485 class-or-namespace-name. */
4486 parser->scope = old_scope;
4487 parser->qualifying_scope = saved_qualifying_scope;
4488 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4490 /* If the next token is an identifier, and the one after
4491 that is a `::', then any valid interpretation would have
4492 found a class-or-namespace-name. */
4493 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4494 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4496 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4499 token = cp_lexer_consume_token (parser->lexer);
4502 if (!token->ambiguous_p)
4505 tree ambiguous_decls;
4507 decl = cp_parser_lookup_name (parser, token->u.value,
4509 /*is_template=*/false,
4510 /*is_namespace=*/false,
4511 /*check_dependency=*/true,
4514 if (TREE_CODE (decl) == TEMPLATE_DECL)
4515 error_at (token->location,
4516 "%qD used without template parameters",
4518 else if (ambiguous_decls)
4520 error_at (token->location,
4521 "reference to %qD is ambiguous",
4523 print_candidates (ambiguous_decls);
4524 decl = error_mark_node;
4528 if (cxx_dialect != cxx98)
4529 cp_parser_name_lookup_error
4530 (parser, token->u.value, decl, NLE_NOT_CXX98,
4533 cp_parser_name_lookup_error
4534 (parser, token->u.value, decl, NLE_CXX98,
4538 parser->scope = error_mark_node;
4540 /* Treat this as a successful nested-name-specifier
4545 If the name found is not a class-name (clause
4546 _class_) or namespace-name (_namespace.def_), the
4547 program is ill-formed. */
4550 cp_lexer_consume_token (parser->lexer);
4554 /* We've found one valid nested-name-specifier. */
4556 /* Name lookup always gives us a DECL. */
4557 if (TREE_CODE (new_scope) == TYPE_DECL)
4558 new_scope = TREE_TYPE (new_scope);
4559 /* Uses of "template" must be followed by actual templates. */
4560 if (template_keyword_p
4561 && !(CLASS_TYPE_P (new_scope)
4562 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4563 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4564 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4565 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4566 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4567 == TEMPLATE_ID_EXPR)))
4568 permerror (input_location, TYPE_P (new_scope)
4569 ? "%qT is not a template"
4570 : "%qD is not a template",
4572 /* If it is a class scope, try to complete it; we are about to
4573 be looking up names inside the class. */
4574 if (TYPE_P (new_scope)
4575 /* Since checking types for dependency can be expensive,
4576 avoid doing it if the type is already complete. */
4577 && !COMPLETE_TYPE_P (new_scope)
4578 /* Do not try to complete dependent types. */
4579 && !dependent_type_p (new_scope))
4581 new_scope = complete_type (new_scope);
4582 /* If it is a typedef to current class, use the current
4583 class instead, as the typedef won't have any names inside
4585 if (!COMPLETE_TYPE_P (new_scope)
4586 && currently_open_class (new_scope))
4587 new_scope = TYPE_MAIN_VARIANT (new_scope);
4589 /* Make sure we look in the right scope the next time through
4591 parser->scope = new_scope;
4594 /* If parsing tentatively, replace the sequence of tokens that makes
4595 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4596 token. That way, should we re-parse the token stream, we will
4597 not have to repeat the effort required to do the parse, nor will
4598 we issue duplicate error messages. */
4599 if (success && start)
4603 token = cp_lexer_token_at (parser->lexer, start);
4604 /* Reset the contents of the START token. */
4605 token->type = CPP_NESTED_NAME_SPECIFIER;
4606 /* Retrieve any deferred checks. Do not pop this access checks yet
4607 so the memory will not be reclaimed during token replacing below. */
4608 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4609 token->u.tree_check_value->value = parser->scope;
4610 token->u.tree_check_value->checks = get_deferred_access_checks ();
4611 token->u.tree_check_value->qualifying_scope =
4612 parser->qualifying_scope;
4613 token->keyword = RID_MAX;
4615 /* Purge all subsequent tokens. */
4616 cp_lexer_purge_tokens_after (parser->lexer, start);
4620 pop_to_parent_deferring_access_checks ();
4622 return success ? parser->scope : NULL_TREE;
4625 /* Parse a nested-name-specifier. See
4626 cp_parser_nested_name_specifier_opt for details. This function
4627 behaves identically, except that it will an issue an error if no
4628 nested-name-specifier is present. */
4631 cp_parser_nested_name_specifier (cp_parser *parser,
4632 bool typename_keyword_p,
4633 bool check_dependency_p,
4635 bool is_declaration)
4639 /* Look for the nested-name-specifier. */
4640 scope = cp_parser_nested_name_specifier_opt (parser,
4645 /* If it was not present, issue an error message. */
4648 cp_parser_error (parser, "expected nested-name-specifier");
4649 parser->scope = NULL_TREE;
4655 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4656 this is either a class-name or a namespace-name (which corresponds
4657 to the class-or-namespace-name production in the grammar). For
4658 C++0x, it can also be a type-name that refers to an enumeration
4661 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4662 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4663 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4664 TYPE_P is TRUE iff the next name should be taken as a class-name,
4665 even the same name is declared to be another entity in the same
4668 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4669 specified by the class-or-namespace-name. If neither is found the
4670 ERROR_MARK_NODE is returned. */
4673 cp_parser_qualifying_entity (cp_parser *parser,
4674 bool typename_keyword_p,
4675 bool template_keyword_p,
4676 bool check_dependency_p,
4678 bool is_declaration)
4681 tree saved_qualifying_scope;
4682 tree saved_object_scope;
4685 bool successful_parse_p;
4687 /* Before we try to parse the class-name, we must save away the
4688 current PARSER->SCOPE since cp_parser_class_name will destroy
4690 saved_scope = parser->scope;
4691 saved_qualifying_scope = parser->qualifying_scope;
4692 saved_object_scope = parser->object_scope;
4693 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4694 there is no need to look for a namespace-name. */
4695 only_class_p = template_keyword_p
4696 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4698 cp_parser_parse_tentatively (parser);
4699 scope = cp_parser_class_name (parser,
4702 type_p ? class_type : none_type,
4704 /*class_head_p=*/false,
4706 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4707 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4709 && cxx_dialect != cxx98
4710 && !successful_parse_p)
4712 /* Restore the saved scope. */
4713 parser->scope = saved_scope;
4714 parser->qualifying_scope = saved_qualifying_scope;
4715 parser->object_scope = saved_object_scope;
4717 /* Parse tentatively. */
4718 cp_parser_parse_tentatively (parser);
4720 /* Parse a typedef-name or enum-name. */
4721 scope = cp_parser_nonclass_name (parser);
4723 /* "If the name found does not designate a namespace or a class,
4724 enumeration, or dependent type, the program is ill-formed."
4726 We cover classes and dependent types above and namespaces below,
4727 so this code is only looking for enums. */
4728 if (!scope || TREE_CODE (scope) != TYPE_DECL
4729 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4730 cp_parser_simulate_error (parser);
4732 successful_parse_p = cp_parser_parse_definitely (parser);
4734 /* If that didn't work, try for a namespace-name. */
4735 if (!only_class_p && !successful_parse_p)
4737 /* Restore the saved scope. */
4738 parser->scope = saved_scope;
4739 parser->qualifying_scope = saved_qualifying_scope;
4740 parser->object_scope = saved_object_scope;
4741 /* If we are not looking at an identifier followed by the scope
4742 resolution operator, then this is not part of a
4743 nested-name-specifier. (Note that this function is only used
4744 to parse the components of a nested-name-specifier.) */
4745 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4746 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4747 return error_mark_node;
4748 scope = cp_parser_namespace_name (parser);
4754 /* Parse a postfix-expression.
4758 postfix-expression [ expression ]
4759 postfix-expression ( expression-list [opt] )
4760 simple-type-specifier ( expression-list [opt] )
4761 typename :: [opt] nested-name-specifier identifier
4762 ( expression-list [opt] )
4763 typename :: [opt] nested-name-specifier template [opt] template-id
4764 ( expression-list [opt] )
4765 postfix-expression . template [opt] id-expression
4766 postfix-expression -> template [opt] id-expression
4767 postfix-expression . pseudo-destructor-name
4768 postfix-expression -> pseudo-destructor-name
4769 postfix-expression ++
4770 postfix-expression --
4771 dynamic_cast < type-id > ( expression )
4772 static_cast < type-id > ( expression )
4773 reinterpret_cast < type-id > ( expression )
4774 const_cast < type-id > ( expression )
4775 typeid ( expression )
4781 ( type-id ) { initializer-list , [opt] }
4783 This extension is a GNU version of the C99 compound-literal
4784 construct. (The C99 grammar uses `type-name' instead of `type-id',
4785 but they are essentially the same concept.)
4787 If ADDRESS_P is true, the postfix expression is the operand of the
4788 `&' operator. CAST_P is true if this expression is the target of a
4791 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4792 class member access expressions [expr.ref].
4794 Returns a representation of the expression. */
4797 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4798 bool member_access_only_p,
4799 cp_id_kind * pidk_return)
4803 cp_id_kind idk = CP_ID_KIND_NONE;
4804 tree postfix_expression = NULL_TREE;
4805 bool is_member_access = false;
4807 /* Peek at the next token. */
4808 token = cp_lexer_peek_token (parser->lexer);
4809 /* Some of the productions are determined by keywords. */
4810 keyword = token->keyword;
4820 const char *saved_message;
4822 /* All of these can be handled in the same way from the point
4823 of view of parsing. Begin by consuming the token
4824 identifying the cast. */
4825 cp_lexer_consume_token (parser->lexer);
4827 /* New types cannot be defined in the cast. */
4828 saved_message = parser->type_definition_forbidden_message;
4829 parser->type_definition_forbidden_message
4830 = G_("types may not be defined in casts");
4832 /* Look for the opening `<'. */
4833 cp_parser_require (parser, CPP_LESS, RT_LESS);
4834 /* Parse the type to which we are casting. */
4835 type = cp_parser_type_id (parser);
4836 /* Look for the closing `>'. */
4837 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4838 /* Restore the old message. */
4839 parser->type_definition_forbidden_message = saved_message;
4841 /* And the expression which is being cast. */
4842 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4843 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4844 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4846 /* Only type conversions to integral or enumeration types
4847 can be used in constant-expressions. */
4848 if (!cast_valid_in_integral_constant_expression_p (type)
4849 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4850 return error_mark_node;
4856 = build_dynamic_cast (type, expression, tf_warning_or_error);
4860 = build_static_cast (type, expression, tf_warning_or_error);
4864 = build_reinterpret_cast (type, expression,
4865 tf_warning_or_error);
4869 = build_const_cast (type, expression, tf_warning_or_error);
4880 const char *saved_message;
4881 bool saved_in_type_id_in_expr_p;
4883 /* Consume the `typeid' token. */
4884 cp_lexer_consume_token (parser->lexer);
4885 /* Look for the `(' token. */
4886 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4887 /* Types cannot be defined in a `typeid' expression. */
4888 saved_message = parser->type_definition_forbidden_message;
4889 parser->type_definition_forbidden_message
4890 = G_("types may not be defined in a %<typeid%> expression");
4891 /* We can't be sure yet whether we're looking at a type-id or an
4893 cp_parser_parse_tentatively (parser);
4894 /* Try a type-id first. */
4895 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4896 parser->in_type_id_in_expr_p = true;
4897 type = cp_parser_type_id (parser);
4898 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4899 /* Look for the `)' token. Otherwise, we can't be sure that
4900 we're not looking at an expression: consider `typeid (int
4901 (3))', for example. */
4902 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4903 /* If all went well, simply lookup the type-id. */
4904 if (cp_parser_parse_definitely (parser))
4905 postfix_expression = get_typeid (type);
4906 /* Otherwise, fall back to the expression variant. */
4911 /* Look for an expression. */
4912 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4913 /* Compute its typeid. */
4914 postfix_expression = build_typeid (expression);
4915 /* Look for the `)' token. */
4916 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4918 /* Restore the saved message. */
4919 parser->type_definition_forbidden_message = saved_message;
4920 /* `typeid' may not appear in an integral constant expression. */
4921 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4922 return error_mark_node;
4929 /* The syntax permitted here is the same permitted for an
4930 elaborated-type-specifier. */
4931 type = cp_parser_elaborated_type_specifier (parser,
4932 /*is_friend=*/false,
4933 /*is_declaration=*/false);
4934 postfix_expression = cp_parser_functional_cast (parser, type);
4942 /* If the next thing is a simple-type-specifier, we may be
4943 looking at a functional cast. We could also be looking at
4944 an id-expression. So, we try the functional cast, and if
4945 that doesn't work we fall back to the primary-expression. */
4946 cp_parser_parse_tentatively (parser);
4947 /* Look for the simple-type-specifier. */
4948 type = cp_parser_simple_type_specifier (parser,
4949 /*decl_specs=*/NULL,
4950 CP_PARSER_FLAGS_NONE);
4951 /* Parse the cast itself. */
4952 if (!cp_parser_error_occurred (parser))
4954 = cp_parser_functional_cast (parser, type);
4955 /* If that worked, we're done. */
4956 if (cp_parser_parse_definitely (parser))
4959 /* If the functional-cast didn't work out, try a
4960 compound-literal. */
4961 if (cp_parser_allow_gnu_extensions_p (parser)
4962 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4964 VEC(constructor_elt,gc) *initializer_list = NULL;
4965 bool saved_in_type_id_in_expr_p;
4967 cp_parser_parse_tentatively (parser);
4968 /* Consume the `('. */
4969 cp_lexer_consume_token (parser->lexer);
4970 /* Parse the type. */
4971 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4972 parser->in_type_id_in_expr_p = true;
4973 type = cp_parser_type_id (parser);
4974 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4975 /* Look for the `)'. */
4976 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4977 /* Look for the `{'. */
4978 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4979 /* If things aren't going well, there's no need to
4981 if (!cp_parser_error_occurred (parser))
4983 bool non_constant_p;
4984 /* Parse the initializer-list. */
4986 = cp_parser_initializer_list (parser, &non_constant_p);
4987 /* Allow a trailing `,'. */
4988 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4989 cp_lexer_consume_token (parser->lexer);
4990 /* Look for the final `}'. */
4991 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4993 /* If that worked, we're definitely looking at a
4994 compound-literal expression. */
4995 if (cp_parser_parse_definitely (parser))
4997 /* Warn the user that a compound literal is not
4998 allowed in standard C++. */
4999 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5000 /* For simplicity, we disallow compound literals in
5001 constant-expressions. We could
5002 allow compound literals of integer type, whose
5003 initializer was a constant, in constant
5004 expressions. Permitting that usage, as a further
5005 extension, would not change the meaning of any
5006 currently accepted programs. (Of course, as
5007 compound literals are not part of ISO C++, the
5008 standard has nothing to say.) */
5009 if (cp_parser_non_integral_constant_expression (parser,
5012 postfix_expression = error_mark_node;
5015 /* Form the representation of the compound-literal. */
5017 = (finish_compound_literal
5018 (type, build_constructor (init_list_type_node,
5019 initializer_list)));
5024 /* It must be a primary-expression. */
5026 = cp_parser_primary_expression (parser, address_p, cast_p,
5027 /*template_arg_p=*/false,
5033 /* Keep looping until the postfix-expression is complete. */
5036 if (idk == CP_ID_KIND_UNQUALIFIED
5037 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5038 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5039 /* It is not a Koenig lookup function call. */
5041 = unqualified_name_lookup_error (postfix_expression);
5043 /* Peek at the next token. */
5044 token = cp_lexer_peek_token (parser->lexer);
5046 switch (token->type)
5048 case CPP_OPEN_SQUARE:
5050 = cp_parser_postfix_open_square_expression (parser,
5053 idk = CP_ID_KIND_NONE;
5054 is_member_access = false;
5057 case CPP_OPEN_PAREN:
5058 /* postfix-expression ( expression-list [opt] ) */
5061 bool is_builtin_constant_p;
5062 bool saved_integral_constant_expression_p = false;
5063 bool saved_non_integral_constant_expression_p = false;
5066 is_member_access = false;
5068 is_builtin_constant_p
5069 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5070 if (is_builtin_constant_p)
5072 /* The whole point of __builtin_constant_p is to allow
5073 non-constant expressions to appear as arguments. */
5074 saved_integral_constant_expression_p
5075 = parser->integral_constant_expression_p;
5076 saved_non_integral_constant_expression_p
5077 = parser->non_integral_constant_expression_p;
5078 parser->integral_constant_expression_p = false;
5080 args = (cp_parser_parenthesized_expression_list
5082 /*cast_p=*/false, /*allow_expansion_p=*/true,
5083 /*non_constant_p=*/NULL));
5084 if (is_builtin_constant_p)
5086 parser->integral_constant_expression_p
5087 = saved_integral_constant_expression_p;
5088 parser->non_integral_constant_expression_p
5089 = saved_non_integral_constant_expression_p;
5094 postfix_expression = error_mark_node;
5098 /* Function calls are not permitted in
5099 constant-expressions. */
5100 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5101 && cp_parser_non_integral_constant_expression (parser,
5104 postfix_expression = error_mark_node;
5105 release_tree_vector (args);
5110 if (idk == CP_ID_KIND_UNQUALIFIED
5111 || idk == CP_ID_KIND_TEMPLATE_ID)
5113 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5115 if (!VEC_empty (tree, args))
5118 if (!any_type_dependent_arguments_p (args))
5120 = perform_koenig_lookup (postfix_expression, args);
5124 = unqualified_fn_lookup_error (postfix_expression);
5126 /* We do not perform argument-dependent lookup if
5127 normal lookup finds a non-function, in accordance
5128 with the expected resolution of DR 218. */
5129 else if (!VEC_empty (tree, args)
5130 && is_overloaded_fn (postfix_expression))
5132 tree fn = get_first_fn (postfix_expression);
5133 fn = STRIP_TEMPLATE (fn);
5135 /* Do not do argument dependent lookup if regular
5136 lookup finds a member function or a block-scope
5137 function declaration. [basic.lookup.argdep]/3 */
5138 if (!DECL_FUNCTION_MEMBER_P (fn)
5139 && !DECL_LOCAL_FUNCTION_P (fn))
5142 if (!any_type_dependent_arguments_p (args))
5144 = perform_koenig_lookup (postfix_expression, args);
5149 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5151 tree instance = TREE_OPERAND (postfix_expression, 0);
5152 tree fn = TREE_OPERAND (postfix_expression, 1);
5154 if (processing_template_decl
5155 && (type_dependent_expression_p (instance)
5156 || (!BASELINK_P (fn)
5157 && TREE_CODE (fn) != FIELD_DECL)
5158 || type_dependent_expression_p (fn)
5159 || any_type_dependent_arguments_p (args)))
5162 = build_nt_call_vec (postfix_expression, args);
5163 release_tree_vector (args);
5167 if (BASELINK_P (fn))
5170 = (build_new_method_call
5171 (instance, fn, &args, NULL_TREE,
5172 (idk == CP_ID_KIND_QUALIFIED
5173 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5175 tf_warning_or_error));
5179 = finish_call_expr (postfix_expression, &args,
5180 /*disallow_virtual=*/false,
5182 tf_warning_or_error);
5184 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5185 || TREE_CODE (postfix_expression) == MEMBER_REF
5186 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5187 postfix_expression = (build_offset_ref_call_from_tree
5188 (postfix_expression, &args));
5189 else if (idk == CP_ID_KIND_QUALIFIED)
5190 /* A call to a static class member, or a namespace-scope
5193 = finish_call_expr (postfix_expression, &args,
5194 /*disallow_virtual=*/true,
5196 tf_warning_or_error);
5198 /* All other function calls. */
5200 = finish_call_expr (postfix_expression, &args,
5201 /*disallow_virtual=*/false,
5203 tf_warning_or_error);
5205 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5206 idk = CP_ID_KIND_NONE;
5208 release_tree_vector (args);
5214 /* postfix-expression . template [opt] id-expression
5215 postfix-expression . pseudo-destructor-name
5216 postfix-expression -> template [opt] id-expression
5217 postfix-expression -> pseudo-destructor-name */
5219 /* Consume the `.' or `->' operator. */
5220 cp_lexer_consume_token (parser->lexer);
5223 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5228 is_member_access = true;
5232 /* postfix-expression ++ */
5233 /* Consume the `++' token. */
5234 cp_lexer_consume_token (parser->lexer);
5235 /* Generate a representation for the complete expression. */
5237 = finish_increment_expr (postfix_expression,
5238 POSTINCREMENT_EXPR);
5239 /* Increments may not appear in constant-expressions. */
5240 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5241 postfix_expression = error_mark_node;
5242 idk = CP_ID_KIND_NONE;
5243 is_member_access = false;
5246 case CPP_MINUS_MINUS:
5247 /* postfix-expression -- */
5248 /* Consume the `--' token. */
5249 cp_lexer_consume_token (parser->lexer);
5250 /* Generate a representation for the complete expression. */
5252 = finish_increment_expr (postfix_expression,
5253 POSTDECREMENT_EXPR);
5254 /* Decrements may not appear in constant-expressions. */
5255 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5256 postfix_expression = error_mark_node;
5257 idk = CP_ID_KIND_NONE;
5258 is_member_access = false;
5262 if (pidk_return != NULL)
5263 * pidk_return = idk;
5264 if (member_access_only_p)
5265 return is_member_access? postfix_expression : error_mark_node;
5267 return postfix_expression;
5271 /* We should never get here. */
5273 return error_mark_node;
5276 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5277 by cp_parser_builtin_offsetof. We're looking for
5279 postfix-expression [ expression ]
5281 FOR_OFFSETOF is set if we're being called in that context, which
5282 changes how we deal with integer constant expressions. */
5285 cp_parser_postfix_open_square_expression (cp_parser *parser,
5286 tree postfix_expression,
5291 /* Consume the `[' token. */
5292 cp_lexer_consume_token (parser->lexer);
5294 /* Parse the index expression. */
5295 /* ??? For offsetof, there is a question of what to allow here. If
5296 offsetof is not being used in an integral constant expression context,
5297 then we *could* get the right answer by computing the value at runtime.
5298 If we are in an integral constant expression context, then we might
5299 could accept any constant expression; hard to say without analysis.
5300 Rather than open the barn door too wide right away, allow only integer
5301 constant expressions here. */
5303 index = cp_parser_constant_expression (parser, false, NULL);
5305 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5307 /* Look for the closing `]'. */
5308 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5310 /* Build the ARRAY_REF. */
5311 postfix_expression = grok_array_decl (postfix_expression, index);
5313 /* When not doing offsetof, array references are not permitted in
5314 constant-expressions. */
5316 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5317 postfix_expression = error_mark_node;
5319 return postfix_expression;
5322 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5323 by cp_parser_builtin_offsetof. We're looking for
5325 postfix-expression . template [opt] id-expression
5326 postfix-expression . pseudo-destructor-name
5327 postfix-expression -> template [opt] id-expression
5328 postfix-expression -> pseudo-destructor-name
5330 FOR_OFFSETOF is set if we're being called in that context. That sorta
5331 limits what of the above we'll actually accept, but nevermind.
5332 TOKEN_TYPE is the "." or "->" token, which will already have been
5333 removed from the stream. */
5336 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5337 enum cpp_ttype token_type,
5338 tree postfix_expression,
5339 bool for_offsetof, cp_id_kind *idk,
5340 location_t location)
5344 bool pseudo_destructor_p;
5345 tree scope = NULL_TREE;
5347 /* If this is a `->' operator, dereference the pointer. */
5348 if (token_type == CPP_DEREF)
5349 postfix_expression = build_x_arrow (postfix_expression);
5350 /* Check to see whether or not the expression is type-dependent. */
5351 dependent_p = type_dependent_expression_p (postfix_expression);
5352 /* The identifier following the `->' or `.' is not qualified. */
5353 parser->scope = NULL_TREE;
5354 parser->qualifying_scope = NULL_TREE;
5355 parser->object_scope = NULL_TREE;
5356 *idk = CP_ID_KIND_NONE;
5358 /* Enter the scope corresponding to the type of the object
5359 given by the POSTFIX_EXPRESSION. */
5360 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5362 scope = TREE_TYPE (postfix_expression);
5363 /* According to the standard, no expression should ever have
5364 reference type. Unfortunately, we do not currently match
5365 the standard in this respect in that our internal representation
5366 of an expression may have reference type even when the standard
5367 says it does not. Therefore, we have to manually obtain the
5368 underlying type here. */
5369 scope = non_reference (scope);
5370 /* The type of the POSTFIX_EXPRESSION must be complete. */
5371 if (scope == unknown_type_node)
5373 error_at (location, "%qE does not have class type",
5374 postfix_expression);
5378 scope = complete_type_or_else (scope, NULL_TREE);
5379 /* Let the name lookup machinery know that we are processing a
5380 class member access expression. */
5381 parser->context->object_type = scope;
5382 /* If something went wrong, we want to be able to discern that case,
5383 as opposed to the case where there was no SCOPE due to the type
5384 of expression being dependent. */
5386 scope = error_mark_node;
5387 /* If the SCOPE was erroneous, make the various semantic analysis
5388 functions exit quickly -- and without issuing additional error
5390 if (scope == error_mark_node)
5391 postfix_expression = error_mark_node;
5394 /* Assume this expression is not a pseudo-destructor access. */
5395 pseudo_destructor_p = false;
5397 /* If the SCOPE is a scalar type, then, if this is a valid program,
5398 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5399 is type dependent, it can be pseudo-destructor-name or something else.
5400 Try to parse it as pseudo-destructor-name first. */
5401 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5406 cp_parser_parse_tentatively (parser);
5407 /* Parse the pseudo-destructor-name. */
5409 cp_parser_pseudo_destructor_name (parser, &s, &type);
5411 && (cp_parser_error_occurred (parser)
5412 || TREE_CODE (type) != TYPE_DECL
5413 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5414 cp_parser_abort_tentative_parse (parser);
5415 else if (cp_parser_parse_definitely (parser))
5417 pseudo_destructor_p = true;
5419 = finish_pseudo_destructor_expr (postfix_expression,
5420 s, TREE_TYPE (type));
5424 if (!pseudo_destructor_p)
5426 /* If the SCOPE is not a scalar type, we are looking at an
5427 ordinary class member access expression, rather than a
5428 pseudo-destructor-name. */
5430 cp_token *token = cp_lexer_peek_token (parser->lexer);
5431 /* Parse the id-expression. */
5432 name = (cp_parser_id_expression
5434 cp_parser_optional_template_keyword (parser),
5435 /*check_dependency_p=*/true,
5437 /*declarator_p=*/false,
5438 /*optional_p=*/false));
5439 /* In general, build a SCOPE_REF if the member name is qualified.
5440 However, if the name was not dependent and has already been
5441 resolved; there is no need to build the SCOPE_REF. For example;
5443 struct X { void f(); };
5444 template <typename T> void f(T* t) { t->X::f(); }
5446 Even though "t" is dependent, "X::f" is not and has been resolved
5447 to a BASELINK; there is no need to include scope information. */
5449 /* But we do need to remember that there was an explicit scope for
5450 virtual function calls. */
5452 *idk = CP_ID_KIND_QUALIFIED;
5454 /* If the name is a template-id that names a type, we will get a
5455 TYPE_DECL here. That is invalid code. */
5456 if (TREE_CODE (name) == TYPE_DECL)
5458 error_at (token->location, "invalid use of %qD", name);
5459 postfix_expression = error_mark_node;
5463 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5465 name = build_qualified_name (/*type=*/NULL_TREE,
5469 parser->scope = NULL_TREE;
5470 parser->qualifying_scope = NULL_TREE;
5471 parser->object_scope = NULL_TREE;
5473 if (scope && name && BASELINK_P (name))
5474 adjust_result_of_qualified_name_lookup
5475 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5477 = finish_class_member_access_expr (postfix_expression, name,
5479 tf_warning_or_error);
5483 /* We no longer need to look up names in the scope of the object on
5484 the left-hand side of the `.' or `->' operator. */
5485 parser->context->object_type = NULL_TREE;
5487 /* Outside of offsetof, these operators may not appear in
5488 constant-expressions. */
5490 && (cp_parser_non_integral_constant_expression
5491 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5492 postfix_expression = error_mark_node;
5494 return postfix_expression;
5497 /* Parse a parenthesized expression-list.
5500 assignment-expression
5501 expression-list, assignment-expression
5506 identifier, expression-list
5508 CAST_P is true if this expression is the target of a cast.
5510 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5513 Returns a vector of trees. Each element is a representation of an
5514 assignment-expression. NULL is returned if the ( and or ) are
5515 missing. An empty, but allocated, vector is returned on no
5516 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5517 if we are parsing an attribute list for an attribute that wants a
5518 plain identifier argument, normal_attr for an attribute that wants
5519 an expression, or non_attr if we aren't parsing an attribute list. If
5520 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5521 not all of the expressions in the list were constant. */
5523 static VEC(tree,gc) *
5524 cp_parser_parenthesized_expression_list (cp_parser* parser,
5525 int is_attribute_list,
5527 bool allow_expansion_p,
5528 bool *non_constant_p)
5530 VEC(tree,gc) *expression_list;
5531 bool fold_expr_p = is_attribute_list != non_attr;
5532 tree identifier = NULL_TREE;
5533 bool saved_greater_than_is_operator_p;
5535 /* Assume all the expressions will be constant. */
5537 *non_constant_p = false;
5539 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5542 expression_list = make_tree_vector ();
5544 /* Within a parenthesized expression, a `>' token is always
5545 the greater-than operator. */
5546 saved_greater_than_is_operator_p
5547 = parser->greater_than_is_operator_p;
5548 parser->greater_than_is_operator_p = true;
5550 /* Consume expressions until there are no more. */
5551 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5556 /* At the beginning of attribute lists, check to see if the
5557 next token is an identifier. */
5558 if (is_attribute_list == id_attr
5559 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5563 /* Consume the identifier. */
5564 token = cp_lexer_consume_token (parser->lexer);
5565 /* Save the identifier. */
5566 identifier = token->u.value;
5570 bool expr_non_constant_p;
5572 /* Parse the next assignment-expression. */
5573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5575 /* A braced-init-list. */
5576 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5577 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5578 if (non_constant_p && expr_non_constant_p)
5579 *non_constant_p = true;
5581 else if (non_constant_p)
5583 expr = (cp_parser_constant_expression
5584 (parser, /*allow_non_constant_p=*/true,
5585 &expr_non_constant_p));
5586 if (expr_non_constant_p)
5587 *non_constant_p = true;
5590 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5593 expr = fold_non_dependent_expr (expr);
5595 /* If we have an ellipsis, then this is an expression
5597 if (allow_expansion_p
5598 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5600 /* Consume the `...'. */
5601 cp_lexer_consume_token (parser->lexer);
5603 /* Build the argument pack. */
5604 expr = make_pack_expansion (expr);
5607 /* Add it to the list. We add error_mark_node
5608 expressions to the list, so that we can still tell if
5609 the correct form for a parenthesized expression-list
5610 is found. That gives better errors. */
5611 VEC_safe_push (tree, gc, expression_list, expr);
5613 if (expr == error_mark_node)
5617 /* After the first item, attribute lists look the same as
5618 expression lists. */
5619 is_attribute_list = non_attr;
5622 /* If the next token isn't a `,', then we are done. */
5623 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5626 /* Otherwise, consume the `,' and keep going. */
5627 cp_lexer_consume_token (parser->lexer);
5630 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5635 /* We try and resync to an unnested comma, as that will give the
5636 user better diagnostics. */
5637 ending = cp_parser_skip_to_closing_parenthesis (parser,
5638 /*recovering=*/true,
5640 /*consume_paren=*/true);
5645 parser->greater_than_is_operator_p
5646 = saved_greater_than_is_operator_p;
5651 parser->greater_than_is_operator_p
5652 = saved_greater_than_is_operator_p;
5655 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5657 return expression_list;
5660 /* Parse a pseudo-destructor-name.
5662 pseudo-destructor-name:
5663 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5664 :: [opt] nested-name-specifier template template-id :: ~ type-name
5665 :: [opt] nested-name-specifier [opt] ~ type-name
5667 If either of the first two productions is used, sets *SCOPE to the
5668 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5669 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5670 or ERROR_MARK_NODE if the parse fails. */
5673 cp_parser_pseudo_destructor_name (cp_parser* parser,
5677 bool nested_name_specifier_p;
5679 /* Assume that things will not work out. */
5680 *type = error_mark_node;
5682 /* Look for the optional `::' operator. */
5683 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5684 /* Look for the optional nested-name-specifier. */
5685 nested_name_specifier_p
5686 = (cp_parser_nested_name_specifier_opt (parser,
5687 /*typename_keyword_p=*/false,
5688 /*check_dependency_p=*/true,
5690 /*is_declaration=*/false)
5692 /* Now, if we saw a nested-name-specifier, we might be doing the
5693 second production. */
5694 if (nested_name_specifier_p
5695 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5697 /* Consume the `template' keyword. */
5698 cp_lexer_consume_token (parser->lexer);
5699 /* Parse the template-id. */
5700 cp_parser_template_id (parser,
5701 /*template_keyword_p=*/true,
5702 /*check_dependency_p=*/false,
5703 /*is_declaration=*/true);
5704 /* Look for the `::' token. */
5705 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5707 /* If the next token is not a `~', then there might be some
5708 additional qualification. */
5709 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5711 /* At this point, we're looking for "type-name :: ~". The type-name
5712 must not be a class-name, since this is a pseudo-destructor. So,
5713 it must be either an enum-name, or a typedef-name -- both of which
5714 are just identifiers. So, we peek ahead to check that the "::"
5715 and "~" tokens are present; if they are not, then we can avoid
5716 calling type_name. */
5717 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5718 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5719 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5721 cp_parser_error (parser, "non-scalar type");
5725 /* Look for the type-name. */
5726 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5727 if (*scope == error_mark_node)
5730 /* Look for the `::' token. */
5731 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5736 /* Look for the `~'. */
5737 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5738 /* Look for the type-name again. We are not responsible for
5739 checking that it matches the first type-name. */
5740 *type = cp_parser_nonclass_name (parser);
5743 /* Parse a unary-expression.
5749 unary-operator cast-expression
5750 sizeof unary-expression
5758 __extension__ cast-expression
5759 __alignof__ unary-expression
5760 __alignof__ ( type-id )
5761 __real__ cast-expression
5762 __imag__ cast-expression
5765 ADDRESS_P is true iff the unary-expression is appearing as the
5766 operand of the `&' operator. CAST_P is true if this expression is
5767 the target of a cast.
5769 Returns a representation of the expression. */
5772 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5776 enum tree_code unary_operator;
5778 /* Peek at the next token. */
5779 token = cp_lexer_peek_token (parser->lexer);
5780 /* Some keywords give away the kind of expression. */
5781 if (token->type == CPP_KEYWORD)
5783 enum rid keyword = token->keyword;
5793 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5794 /* Consume the token. */
5795 cp_lexer_consume_token (parser->lexer);
5796 /* Parse the operand. */
5797 operand = cp_parser_sizeof_operand (parser, keyword);
5799 if (TYPE_P (operand))
5800 return cxx_sizeof_or_alignof_type (operand, op, true);
5802 return cxx_sizeof_or_alignof_expr (operand, op, true);
5806 return cp_parser_new_expression (parser);
5809 return cp_parser_delete_expression (parser);
5813 /* The saved value of the PEDANTIC flag. */
5817 /* Save away the PEDANTIC flag. */
5818 cp_parser_extension_opt (parser, &saved_pedantic);
5819 /* Parse the cast-expression. */
5820 expr = cp_parser_simple_cast_expression (parser);
5821 /* Restore the PEDANTIC flag. */
5822 pedantic = saved_pedantic;
5832 /* Consume the `__real__' or `__imag__' token. */
5833 cp_lexer_consume_token (parser->lexer);
5834 /* Parse the cast-expression. */
5835 expression = cp_parser_simple_cast_expression (parser);
5836 /* Create the complete representation. */
5837 return build_x_unary_op ((keyword == RID_REALPART
5838 ? REALPART_EXPR : IMAGPART_EXPR),
5840 tf_warning_or_error);
5849 /* Look for the `:: new' and `:: delete', which also signal the
5850 beginning of a new-expression, or delete-expression,
5851 respectively. If the next token is `::', then it might be one of
5853 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5857 /* See if the token after the `::' is one of the keywords in
5858 which we're interested. */
5859 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5860 /* If it's `new', we have a new-expression. */
5861 if (keyword == RID_NEW)
5862 return cp_parser_new_expression (parser);
5863 /* Similarly, for `delete'. */
5864 else if (keyword == RID_DELETE)
5865 return cp_parser_delete_expression (parser);
5868 /* Look for a unary operator. */
5869 unary_operator = cp_parser_unary_operator (token);
5870 /* The `++' and `--' operators can be handled similarly, even though
5871 they are not technically unary-operators in the grammar. */
5872 if (unary_operator == ERROR_MARK)
5874 if (token->type == CPP_PLUS_PLUS)
5875 unary_operator = PREINCREMENT_EXPR;
5876 else if (token->type == CPP_MINUS_MINUS)
5877 unary_operator = PREDECREMENT_EXPR;
5878 /* Handle the GNU address-of-label extension. */
5879 else if (cp_parser_allow_gnu_extensions_p (parser)
5880 && token->type == CPP_AND_AND)
5884 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5886 /* Consume the '&&' token. */
5887 cp_lexer_consume_token (parser->lexer);
5888 /* Look for the identifier. */
5889 identifier = cp_parser_identifier (parser);
5890 /* Create an expression representing the address. */
5891 expression = finish_label_address_expr (identifier, loc);
5892 if (cp_parser_non_integral_constant_expression (parser,
5894 expression = error_mark_node;
5898 if (unary_operator != ERROR_MARK)
5900 tree cast_expression;
5901 tree expression = error_mark_node;
5902 non_integral_constant non_constant_p = NIC_NONE;
5904 /* Consume the operator token. */
5905 token = cp_lexer_consume_token (parser->lexer);
5906 /* Parse the cast-expression. */
5908 = cp_parser_cast_expression (parser,
5909 unary_operator == ADDR_EXPR,
5910 /*cast_p=*/false, pidk);
5911 /* Now, build an appropriate representation. */
5912 switch (unary_operator)
5915 non_constant_p = NIC_STAR;
5916 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5917 tf_warning_or_error);
5921 non_constant_p = NIC_ADDR;
5924 expression = build_x_unary_op (unary_operator, cast_expression,
5925 tf_warning_or_error);
5928 case PREINCREMENT_EXPR:
5929 case PREDECREMENT_EXPR:
5930 non_constant_p = unary_operator == PREINCREMENT_EXPR
5931 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5933 case UNARY_PLUS_EXPR:
5935 case TRUTH_NOT_EXPR:
5936 expression = finish_unary_op_expr (unary_operator, cast_expression);
5943 if (non_constant_p != NIC_NONE
5944 && cp_parser_non_integral_constant_expression (parser,
5946 expression = error_mark_node;
5951 return cp_parser_postfix_expression (parser, address_p, cast_p,
5952 /*member_access_only_p=*/false,
5956 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5957 unary-operator, the corresponding tree code is returned. */
5959 static enum tree_code
5960 cp_parser_unary_operator (cp_token* token)
5962 switch (token->type)
5965 return INDIRECT_REF;
5971 return UNARY_PLUS_EXPR;
5977 return TRUTH_NOT_EXPR;
5980 return BIT_NOT_EXPR;
5987 /* Parse a new-expression.
5990 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5991 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5993 Returns a representation of the expression. */
5996 cp_parser_new_expression (cp_parser* parser)
5998 bool global_scope_p;
5999 VEC(tree,gc) *placement;
6001 VEC(tree,gc) *initializer;
6005 /* Look for the optional `::' operator. */
6007 = (cp_parser_global_scope_opt (parser,
6008 /*current_scope_valid_p=*/false)
6010 /* Look for the `new' operator. */
6011 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6012 /* There's no easy way to tell a new-placement from the
6013 `( type-id )' construct. */
6014 cp_parser_parse_tentatively (parser);
6015 /* Look for a new-placement. */
6016 placement = cp_parser_new_placement (parser);
6017 /* If that didn't work out, there's no new-placement. */
6018 if (!cp_parser_parse_definitely (parser))
6020 if (placement != NULL)
6021 release_tree_vector (placement);
6025 /* If the next token is a `(', then we have a parenthesized
6027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6030 /* Consume the `('. */
6031 cp_lexer_consume_token (parser->lexer);
6032 /* Parse the type-id. */
6033 type = cp_parser_type_id (parser);
6034 /* Look for the closing `)'. */
6035 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6036 token = cp_lexer_peek_token (parser->lexer);
6037 /* There should not be a direct-new-declarator in this production,
6038 but GCC used to allowed this, so we check and emit a sensible error
6039 message for this case. */
6040 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6042 error_at (token->location,
6043 "array bound forbidden after parenthesized type-id");
6044 inform (token->location,
6045 "try removing the parentheses around the type-id");
6046 cp_parser_direct_new_declarator (parser);
6050 /* Otherwise, there must be a new-type-id. */
6052 type = cp_parser_new_type_id (parser, &nelts);
6054 /* If the next token is a `(' or '{', then we have a new-initializer. */
6055 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6056 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6057 initializer = cp_parser_new_initializer (parser);
6061 /* A new-expression may not appear in an integral constant
6063 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6064 ret = error_mark_node;
6067 /* Create a representation of the new-expression. */
6068 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6069 tf_warning_or_error);
6072 if (placement != NULL)
6073 release_tree_vector (placement);
6074 if (initializer != NULL)
6075 release_tree_vector (initializer);
6080 /* Parse a new-placement.
6085 Returns the same representation as for an expression-list. */
6087 static VEC(tree,gc) *
6088 cp_parser_new_placement (cp_parser* parser)
6090 VEC(tree,gc) *expression_list;
6092 /* Parse the expression-list. */
6093 expression_list = (cp_parser_parenthesized_expression_list
6094 (parser, non_attr, /*cast_p=*/false,
6095 /*allow_expansion_p=*/true,
6096 /*non_constant_p=*/NULL));
6098 return expression_list;
6101 /* Parse a new-type-id.
6104 type-specifier-seq new-declarator [opt]
6106 Returns the TYPE allocated. If the new-type-id indicates an array
6107 type, *NELTS is set to the number of elements in the last array
6108 bound; the TYPE will not include the last array bound. */
6111 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6113 cp_decl_specifier_seq type_specifier_seq;
6114 cp_declarator *new_declarator;
6115 cp_declarator *declarator;
6116 cp_declarator *outer_declarator;
6117 const char *saved_message;
6120 /* The type-specifier sequence must not contain type definitions.
6121 (It cannot contain declarations of new types either, but if they
6122 are not definitions we will catch that because they are not
6124 saved_message = parser->type_definition_forbidden_message;
6125 parser->type_definition_forbidden_message
6126 = G_("types may not be defined in a new-type-id");
6127 /* Parse the type-specifier-seq. */
6128 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6129 /*is_trailing_return=*/false,
6130 &type_specifier_seq);
6131 /* Restore the old message. */
6132 parser->type_definition_forbidden_message = saved_message;
6133 /* Parse the new-declarator. */
6134 new_declarator = cp_parser_new_declarator_opt (parser);
6136 /* Determine the number of elements in the last array dimension, if
6139 /* Skip down to the last array dimension. */
6140 declarator = new_declarator;
6141 outer_declarator = NULL;
6142 while (declarator && (declarator->kind == cdk_pointer
6143 || declarator->kind == cdk_ptrmem))
6145 outer_declarator = declarator;
6146 declarator = declarator->declarator;
6149 && declarator->kind == cdk_array
6150 && declarator->declarator
6151 && declarator->declarator->kind == cdk_array)
6153 outer_declarator = declarator;
6154 declarator = declarator->declarator;
6157 if (declarator && declarator->kind == cdk_array)
6159 *nelts = declarator->u.array.bounds;
6160 if (*nelts == error_mark_node)
6161 *nelts = integer_one_node;
6163 if (outer_declarator)
6164 outer_declarator->declarator = declarator->declarator;
6166 new_declarator = NULL;
6169 type = groktypename (&type_specifier_seq, new_declarator, false);
6173 /* Parse an (optional) new-declarator.
6176 ptr-operator new-declarator [opt]
6177 direct-new-declarator
6179 Returns the declarator. */
6181 static cp_declarator *
6182 cp_parser_new_declarator_opt (cp_parser* parser)
6184 enum tree_code code;
6186 cp_cv_quals cv_quals;
6188 /* We don't know if there's a ptr-operator next, or not. */
6189 cp_parser_parse_tentatively (parser);
6190 /* Look for a ptr-operator. */
6191 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6192 /* If that worked, look for more new-declarators. */
6193 if (cp_parser_parse_definitely (parser))
6195 cp_declarator *declarator;
6197 /* Parse another optional declarator. */
6198 declarator = cp_parser_new_declarator_opt (parser);
6200 return cp_parser_make_indirect_declarator
6201 (code, type, cv_quals, declarator);
6204 /* If the next token is a `[', there is a direct-new-declarator. */
6205 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6206 return cp_parser_direct_new_declarator (parser);
6211 /* Parse a direct-new-declarator.
6213 direct-new-declarator:
6215 direct-new-declarator [constant-expression]
6219 static cp_declarator *
6220 cp_parser_direct_new_declarator (cp_parser* parser)
6222 cp_declarator *declarator = NULL;
6228 /* Look for the opening `['. */
6229 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6230 /* The first expression is not required to be constant. */
6233 cp_token *token = cp_lexer_peek_token (parser->lexer);
6234 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6235 /* The standard requires that the expression have integral
6236 type. DR 74 adds enumeration types. We believe that the
6237 real intent is that these expressions be handled like the
6238 expression in a `switch' condition, which also allows
6239 classes with a single conversion to integral or
6240 enumeration type. */
6241 if (!processing_template_decl)
6244 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6249 error_at (token->location,
6250 "expression in new-declarator must have integral "
6251 "or enumeration type");
6252 expression = error_mark_node;
6256 /* But all the other expressions must be. */
6259 = cp_parser_constant_expression (parser,
6260 /*allow_non_constant=*/false,
6262 /* Look for the closing `]'. */
6263 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6265 /* Add this bound to the declarator. */
6266 declarator = make_array_declarator (declarator, expression);
6268 /* If the next token is not a `[', then there are no more
6270 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6277 /* Parse a new-initializer.
6280 ( expression-list [opt] )
6283 Returns a representation of the expression-list. */
6285 static VEC(tree,gc) *
6286 cp_parser_new_initializer (cp_parser* parser)
6288 VEC(tree,gc) *expression_list;
6290 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6293 bool expr_non_constant_p;
6294 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6295 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6296 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6297 expression_list = make_tree_vector_single (t);
6300 expression_list = (cp_parser_parenthesized_expression_list
6301 (parser, non_attr, /*cast_p=*/false,
6302 /*allow_expansion_p=*/true,
6303 /*non_constant_p=*/NULL));
6305 return expression_list;
6308 /* Parse a delete-expression.
6311 :: [opt] delete cast-expression
6312 :: [opt] delete [ ] cast-expression
6314 Returns a representation of the expression. */
6317 cp_parser_delete_expression (cp_parser* parser)
6319 bool global_scope_p;
6323 /* Look for the optional `::' operator. */
6325 = (cp_parser_global_scope_opt (parser,
6326 /*current_scope_valid_p=*/false)
6328 /* Look for the `delete' keyword. */
6329 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6330 /* See if the array syntax is in use. */
6331 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6333 /* Consume the `[' token. */
6334 cp_lexer_consume_token (parser->lexer);
6335 /* Look for the `]' token. */
6336 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6337 /* Remember that this is the `[]' construct. */
6343 /* Parse the cast-expression. */
6344 expression = cp_parser_simple_cast_expression (parser);
6346 /* A delete-expression may not appear in an integral constant
6348 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6349 return error_mark_node;
6351 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6354 /* Returns true if TOKEN may start a cast-expression and false
6358 cp_parser_token_starts_cast_expression (cp_token *token)
6360 switch (token->type)
6366 case CPP_CLOSE_SQUARE:
6367 case CPP_CLOSE_PAREN:
6368 case CPP_CLOSE_BRACE:
6372 case CPP_DEREF_STAR:
6380 case CPP_GREATER_EQ:
6400 /* '[' may start a primary-expression in obj-c++. */
6401 case CPP_OPEN_SQUARE:
6402 return c_dialect_objc ();
6409 /* Parse a cast-expression.
6413 ( type-id ) cast-expression
6415 ADDRESS_P is true iff the unary-expression is appearing as the
6416 operand of the `&' operator. CAST_P is true if this expression is
6417 the target of a cast.
6419 Returns a representation of the expression. */
6422 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6425 /* If it's a `(', then we might be looking at a cast. */
6426 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6428 tree type = NULL_TREE;
6429 tree expr = NULL_TREE;
6430 bool compound_literal_p;
6431 const char *saved_message;
6433 /* There's no way to know yet whether or not this is a cast.
6434 For example, `(int (3))' is a unary-expression, while `(int)
6435 3' is a cast. So, we resort to parsing tentatively. */
6436 cp_parser_parse_tentatively (parser);
6437 /* Types may not be defined in a cast. */
6438 saved_message = parser->type_definition_forbidden_message;
6439 parser->type_definition_forbidden_message
6440 = G_("types may not be defined in casts");
6441 /* Consume the `('. */
6442 cp_lexer_consume_token (parser->lexer);
6443 /* A very tricky bit is that `(struct S) { 3 }' is a
6444 compound-literal (which we permit in C++ as an extension).
6445 But, that construct is not a cast-expression -- it is a
6446 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6447 is legal; if the compound-literal were a cast-expression,
6448 you'd need an extra set of parentheses.) But, if we parse
6449 the type-id, and it happens to be a class-specifier, then we
6450 will commit to the parse at that point, because we cannot
6451 undo the action that is done when creating a new class. So,
6452 then we cannot back up and do a postfix-expression.
6454 Therefore, we scan ahead to the closing `)', and check to see
6455 if the token after the `)' is a `{'. If so, we are not
6456 looking at a cast-expression.
6458 Save tokens so that we can put them back. */
6459 cp_lexer_save_tokens (parser->lexer);
6460 /* Skip tokens until the next token is a closing parenthesis.
6461 If we find the closing `)', and the next token is a `{', then
6462 we are looking at a compound-literal. */
6464 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6465 /*consume_paren=*/true)
6466 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6467 /* Roll back the tokens we skipped. */
6468 cp_lexer_rollback_tokens (parser->lexer);
6469 /* If we were looking at a compound-literal, simulate an error
6470 so that the call to cp_parser_parse_definitely below will
6472 if (compound_literal_p)
6473 cp_parser_simulate_error (parser);
6476 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6477 parser->in_type_id_in_expr_p = true;
6478 /* Look for the type-id. */
6479 type = cp_parser_type_id (parser);
6480 /* Look for the closing `)'. */
6481 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6482 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6485 /* Restore the saved message. */
6486 parser->type_definition_forbidden_message = saved_message;
6488 /* At this point this can only be either a cast or a
6489 parenthesized ctor such as `(T ())' that looks like a cast to
6490 function returning T. */
6491 if (!cp_parser_error_occurred (parser)
6492 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6495 cp_parser_parse_definitely (parser);
6496 expr = cp_parser_cast_expression (parser,
6497 /*address_p=*/false,
6498 /*cast_p=*/true, pidk);
6500 /* Warn about old-style casts, if so requested. */
6501 if (warn_old_style_cast
6502 && !in_system_header
6503 && !VOID_TYPE_P (type)
6504 && current_lang_name != lang_name_c)
6505 warning (OPT_Wold_style_cast, "use of old-style cast");
6507 /* Only type conversions to integral or enumeration types
6508 can be used in constant-expressions. */
6509 if (!cast_valid_in_integral_constant_expression_p (type)
6510 && cp_parser_non_integral_constant_expression (parser,
6512 return error_mark_node;
6514 /* Perform the cast. */
6515 expr = build_c_cast (input_location, type, expr);
6519 cp_parser_abort_tentative_parse (parser);
6522 /* If we get here, then it's not a cast, so it must be a
6523 unary-expression. */
6524 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6527 /* Parse a binary expression of the general form:
6531 pm-expression .* cast-expression
6532 pm-expression ->* cast-expression
6534 multiplicative-expression:
6536 multiplicative-expression * pm-expression
6537 multiplicative-expression / pm-expression
6538 multiplicative-expression % pm-expression
6540 additive-expression:
6541 multiplicative-expression
6542 additive-expression + multiplicative-expression
6543 additive-expression - multiplicative-expression
6547 shift-expression << additive-expression
6548 shift-expression >> additive-expression
6550 relational-expression:
6552 relational-expression < shift-expression
6553 relational-expression > shift-expression
6554 relational-expression <= shift-expression
6555 relational-expression >= shift-expression
6559 relational-expression:
6560 relational-expression <? shift-expression
6561 relational-expression >? shift-expression
6563 equality-expression:
6564 relational-expression
6565 equality-expression == relational-expression
6566 equality-expression != relational-expression
6570 and-expression & equality-expression
6572 exclusive-or-expression:
6574 exclusive-or-expression ^ and-expression
6576 inclusive-or-expression:
6577 exclusive-or-expression
6578 inclusive-or-expression | exclusive-or-expression
6580 logical-and-expression:
6581 inclusive-or-expression
6582 logical-and-expression && inclusive-or-expression
6584 logical-or-expression:
6585 logical-and-expression
6586 logical-or-expression || logical-and-expression
6588 All these are implemented with a single function like:
6591 simple-cast-expression
6592 binary-expression <token> binary-expression
6594 CAST_P is true if this expression is the target of a cast.
6596 The binops_by_token map is used to get the tree codes for each <token> type.
6597 binary-expressions are associated according to a precedence table. */
6599 #define TOKEN_PRECEDENCE(token) \
6600 (((token->type == CPP_GREATER \
6601 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6602 && !parser->greater_than_is_operator_p) \
6603 ? PREC_NOT_OPERATOR \
6604 : binops_by_token[token->type].prec)
6607 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6608 bool no_toplevel_fold_p,
6609 enum cp_parser_prec prec,
6612 cp_parser_expression_stack stack;
6613 cp_parser_expression_stack_entry *sp = &stack[0];
6616 enum tree_code tree_type, lhs_type, rhs_type;
6617 enum cp_parser_prec new_prec, lookahead_prec;
6620 /* Parse the first expression. */
6621 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6622 lhs_type = ERROR_MARK;
6626 /* Get an operator token. */
6627 token = cp_lexer_peek_token (parser->lexer);
6629 if (warn_cxx0x_compat
6630 && token->type == CPP_RSHIFT
6631 && !parser->greater_than_is_operator_p)
6633 if (warning_at (token->location, OPT_Wc__0x_compat,
6634 "%<>>%> operator will be treated as"
6635 " two right angle brackets in C++0x"))
6636 inform (token->location,
6637 "suggest parentheses around %<>>%> expression");
6640 new_prec = TOKEN_PRECEDENCE (token);
6642 /* Popping an entry off the stack means we completed a subexpression:
6643 - either we found a token which is not an operator (`>' where it is not
6644 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6645 will happen repeatedly;
6646 - or, we found an operator which has lower priority. This is the case
6647 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6649 if (new_prec <= prec)
6658 tree_type = binops_by_token[token->type].tree_type;
6660 /* We used the operator token. */
6661 cp_lexer_consume_token (parser->lexer);
6663 /* For "false && x" or "true || x", x will never be executed;
6664 disable warnings while evaluating it. */
6665 if (tree_type == TRUTH_ANDIF_EXPR)
6666 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6667 else if (tree_type == TRUTH_ORIF_EXPR)
6668 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6670 /* Extract another operand. It may be the RHS of this expression
6671 or the LHS of a new, higher priority expression. */
6672 rhs = cp_parser_simple_cast_expression (parser);
6673 rhs_type = ERROR_MARK;
6675 /* Get another operator token. Look up its precedence to avoid
6676 building a useless (immediately popped) stack entry for common
6677 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6678 token = cp_lexer_peek_token (parser->lexer);
6679 lookahead_prec = TOKEN_PRECEDENCE (token);
6680 if (lookahead_prec > new_prec)
6682 /* ... and prepare to parse the RHS of the new, higher priority
6683 expression. Since precedence levels on the stack are
6684 monotonically increasing, we do not have to care about
6687 sp->tree_type = tree_type;
6689 sp->lhs_type = lhs_type;
6692 lhs_type = rhs_type;
6694 new_prec = lookahead_prec;
6698 lookahead_prec = new_prec;
6699 /* If the stack is not empty, we have parsed into LHS the right side
6700 (`4' in the example above) of an expression we had suspended.
6701 We can use the information on the stack to recover the LHS (`3')
6702 from the stack together with the tree code (`MULT_EXPR'), and
6703 the precedence of the higher level subexpression
6704 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6705 which will be used to actually build the additive expression. */
6708 tree_type = sp->tree_type;
6710 rhs_type = lhs_type;
6712 lhs_type = sp->lhs_type;
6715 /* Undo the disabling of warnings done above. */
6716 if (tree_type == TRUTH_ANDIF_EXPR)
6717 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6718 else if (tree_type == TRUTH_ORIF_EXPR)
6719 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6721 overloaded_p = false;
6722 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6723 ERROR_MARK for everything that is not a binary expression.
6724 This makes warn_about_parentheses miss some warnings that
6725 involve unary operators. For unary expressions we should
6726 pass the correct tree_code unless the unary expression was
6727 surrounded by parentheses.
6729 if (no_toplevel_fold_p
6730 && lookahead_prec <= prec
6732 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6733 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6735 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6736 &overloaded_p, tf_warning_or_error);
6737 lhs_type = tree_type;
6739 /* If the binary operator required the use of an overloaded operator,
6740 then this expression cannot be an integral constant-expression.
6741 An overloaded operator can be used even if both operands are
6742 otherwise permissible in an integral constant-expression if at
6743 least one of the operands is of enumeration type. */
6746 && cp_parser_non_integral_constant_expression (parser,
6748 return error_mark_node;
6755 /* Parse the `? expression : assignment-expression' part of a
6756 conditional-expression. The LOGICAL_OR_EXPR is the
6757 logical-or-expression that started the conditional-expression.
6758 Returns a representation of the entire conditional-expression.
6760 This routine is used by cp_parser_assignment_expression.
6762 ? expression : assignment-expression
6766 ? : assignment-expression */
6769 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6772 tree assignment_expr;
6774 /* Consume the `?' token. */
6775 cp_lexer_consume_token (parser->lexer);
6776 if (cp_parser_allow_gnu_extensions_p (parser)
6777 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6779 /* Implicit true clause. */
6781 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6785 /* Parse the expression. */
6786 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6787 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6788 c_inhibit_evaluation_warnings +=
6789 ((logical_or_expr == truthvalue_true_node)
6790 - (logical_or_expr == truthvalue_false_node));
6793 /* The next token should be a `:'. */
6794 cp_parser_require (parser, CPP_COLON, RT_COLON);
6795 /* Parse the assignment-expression. */
6796 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6797 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6799 /* Build the conditional-expression. */
6800 return build_x_conditional_expr (logical_or_expr,
6803 tf_warning_or_error);
6806 /* Parse an assignment-expression.
6808 assignment-expression:
6809 conditional-expression
6810 logical-or-expression assignment-operator assignment_expression
6813 CAST_P is true if this expression is the target of a cast.
6815 Returns a representation for the expression. */
6818 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6823 /* If the next token is the `throw' keyword, then we're looking at
6824 a throw-expression. */
6825 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6826 expr = cp_parser_throw_expression (parser);
6827 /* Otherwise, it must be that we are looking at a
6828 logical-or-expression. */
6831 /* Parse the binary expressions (logical-or-expression). */
6832 expr = cp_parser_binary_expression (parser, cast_p, false,
6833 PREC_NOT_OPERATOR, pidk);
6834 /* If the next token is a `?' then we're actually looking at a
6835 conditional-expression. */
6836 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6837 return cp_parser_question_colon_clause (parser, expr);
6840 enum tree_code assignment_operator;
6842 /* If it's an assignment-operator, we're using the second
6845 = cp_parser_assignment_operator_opt (parser);
6846 if (assignment_operator != ERROR_MARK)
6848 bool non_constant_p;
6850 /* Parse the right-hand side of the assignment. */
6851 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6853 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6854 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6856 /* An assignment may not appear in a
6857 constant-expression. */
6858 if (cp_parser_non_integral_constant_expression (parser,
6860 return error_mark_node;
6861 /* Build the assignment expression. */
6862 expr = build_x_modify_expr (expr,
6863 assignment_operator,
6865 tf_warning_or_error);
6873 /* Parse an (optional) assignment-operator.
6875 assignment-operator: one of
6876 = *= /= %= += -= >>= <<= &= ^= |=
6880 assignment-operator: one of
6883 If the next token is an assignment operator, the corresponding tree
6884 code is returned, and the token is consumed. For example, for
6885 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6886 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6887 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6888 operator, ERROR_MARK is returned. */
6890 static enum tree_code
6891 cp_parser_assignment_operator_opt (cp_parser* parser)
6896 /* Peek at the next token. */
6897 token = cp_lexer_peek_token (parser->lexer);
6899 switch (token->type)
6910 op = TRUNC_DIV_EXPR;
6914 op = TRUNC_MOD_EXPR;
6946 /* Nothing else is an assignment operator. */
6950 /* If it was an assignment operator, consume it. */
6951 if (op != ERROR_MARK)
6952 cp_lexer_consume_token (parser->lexer);
6957 /* Parse an expression.
6960 assignment-expression
6961 expression , assignment-expression
6963 CAST_P is true if this expression is the target of a cast.
6965 Returns a representation of the expression. */
6968 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6970 tree expression = NULL_TREE;
6974 tree assignment_expression;
6976 /* Parse the next assignment-expression. */
6977 assignment_expression
6978 = cp_parser_assignment_expression (parser, cast_p, pidk);
6979 /* If this is the first assignment-expression, we can just
6982 expression = assignment_expression;
6984 expression = build_x_compound_expr (expression,
6985 assignment_expression,
6986 tf_warning_or_error);
6987 /* If the next token is not a comma, then we are done with the
6989 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6991 /* Consume the `,'. */
6992 cp_lexer_consume_token (parser->lexer);
6993 /* A comma operator cannot appear in a constant-expression. */
6994 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
6995 expression = error_mark_node;
7001 /* Parse a constant-expression.
7003 constant-expression:
7004 conditional-expression
7006 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7007 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7008 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7009 is false, NON_CONSTANT_P should be NULL. */
7012 cp_parser_constant_expression (cp_parser* parser,
7013 bool allow_non_constant_p,
7014 bool *non_constant_p)
7016 bool saved_integral_constant_expression_p;
7017 bool saved_allow_non_integral_constant_expression_p;
7018 bool saved_non_integral_constant_expression_p;
7021 /* It might seem that we could simply parse the
7022 conditional-expression, and then check to see if it were
7023 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7024 one that the compiler can figure out is constant, possibly after
7025 doing some simplifications or optimizations. The standard has a
7026 precise definition of constant-expression, and we must honor
7027 that, even though it is somewhat more restrictive.
7033 is not a legal declaration, because `(2, 3)' is not a
7034 constant-expression. The `,' operator is forbidden in a
7035 constant-expression. However, GCC's constant-folding machinery
7036 will fold this operation to an INTEGER_CST for `3'. */
7038 /* Save the old settings. */
7039 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7040 saved_allow_non_integral_constant_expression_p
7041 = parser->allow_non_integral_constant_expression_p;
7042 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7043 /* We are now parsing a constant-expression. */
7044 parser->integral_constant_expression_p = true;
7045 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
7046 parser->non_integral_constant_expression_p = false;
7047 /* Although the grammar says "conditional-expression", we parse an
7048 "assignment-expression", which also permits "throw-expression"
7049 and the use of assignment operators. In the case that
7050 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7051 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7052 actually essential that we look for an assignment-expression.
7053 For example, cp_parser_initializer_clauses uses this function to
7054 determine whether a particular assignment-expression is in fact
7056 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7057 /* Restore the old settings. */
7058 parser->integral_constant_expression_p
7059 = saved_integral_constant_expression_p;
7060 parser->allow_non_integral_constant_expression_p
7061 = saved_allow_non_integral_constant_expression_p;
7062 if (allow_non_constant_p)
7063 *non_constant_p = parser->non_integral_constant_expression_p;
7064 else if (parser->non_integral_constant_expression_p)
7065 expression = error_mark_node;
7066 parser->non_integral_constant_expression_p
7067 = saved_non_integral_constant_expression_p;
7072 /* Parse __builtin_offsetof.
7074 offsetof-expression:
7075 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7077 offsetof-member-designator:
7079 | offsetof-member-designator "." id-expression
7080 | offsetof-member-designator "[" expression "]"
7081 | offsetof-member-designator "->" id-expression */
7084 cp_parser_builtin_offsetof (cp_parser *parser)
7086 int save_ice_p, save_non_ice_p;
7091 /* We're about to accept non-integral-constant things, but will
7092 definitely yield an integral constant expression. Save and
7093 restore these values around our local parsing. */
7094 save_ice_p = parser->integral_constant_expression_p;
7095 save_non_ice_p = parser->non_integral_constant_expression_p;
7097 /* Consume the "__builtin_offsetof" token. */
7098 cp_lexer_consume_token (parser->lexer);
7099 /* Consume the opening `('. */
7100 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7101 /* Parse the type-id. */
7102 type = cp_parser_type_id (parser);
7103 /* Look for the `,'. */
7104 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7105 token = cp_lexer_peek_token (parser->lexer);
7107 /* Build the (type *)null that begins the traditional offsetof macro. */
7108 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7109 tf_warning_or_error);
7111 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7112 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7113 true, &dummy, token->location);
7116 token = cp_lexer_peek_token (parser->lexer);
7117 switch (token->type)
7119 case CPP_OPEN_SQUARE:
7120 /* offsetof-member-designator "[" expression "]" */
7121 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7125 /* offsetof-member-designator "->" identifier */
7126 expr = grok_array_decl (expr, integer_zero_node);
7130 /* offsetof-member-designator "." identifier */
7131 cp_lexer_consume_token (parser->lexer);
7132 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7137 case CPP_CLOSE_PAREN:
7138 /* Consume the ")" token. */
7139 cp_lexer_consume_token (parser->lexer);
7143 /* Error. We know the following require will fail, but
7144 that gives the proper error message. */
7145 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7146 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7147 expr = error_mark_node;
7153 /* If we're processing a template, we can't finish the semantics yet.
7154 Otherwise we can fold the entire expression now. */
7155 if (processing_template_decl)
7156 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7158 expr = finish_offsetof (expr);
7161 parser->integral_constant_expression_p = save_ice_p;
7162 parser->non_integral_constant_expression_p = save_non_ice_p;
7167 /* Parse a trait expression. */
7170 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7173 tree type1, type2 = NULL_TREE;
7174 bool binary = false;
7175 cp_decl_specifier_seq decl_specs;
7179 case RID_HAS_NOTHROW_ASSIGN:
7180 kind = CPTK_HAS_NOTHROW_ASSIGN;
7182 case RID_HAS_NOTHROW_CONSTRUCTOR:
7183 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7185 case RID_HAS_NOTHROW_COPY:
7186 kind = CPTK_HAS_NOTHROW_COPY;
7188 case RID_HAS_TRIVIAL_ASSIGN:
7189 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7191 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7192 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7194 case RID_HAS_TRIVIAL_COPY:
7195 kind = CPTK_HAS_TRIVIAL_COPY;
7197 case RID_HAS_TRIVIAL_DESTRUCTOR:
7198 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7200 case RID_HAS_VIRTUAL_DESTRUCTOR:
7201 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7203 case RID_IS_ABSTRACT:
7204 kind = CPTK_IS_ABSTRACT;
7206 case RID_IS_BASE_OF:
7207 kind = CPTK_IS_BASE_OF;
7211 kind = CPTK_IS_CLASS;
7213 case RID_IS_CONVERTIBLE_TO:
7214 kind = CPTK_IS_CONVERTIBLE_TO;
7218 kind = CPTK_IS_EMPTY;
7221 kind = CPTK_IS_ENUM;
7226 case RID_IS_POLYMORPHIC:
7227 kind = CPTK_IS_POLYMORPHIC;
7229 case RID_IS_STD_LAYOUT:
7230 kind = CPTK_IS_STD_LAYOUT;
7232 case RID_IS_TRIVIAL:
7233 kind = CPTK_IS_TRIVIAL;
7236 kind = CPTK_IS_UNION;
7242 /* Consume the token. */
7243 cp_lexer_consume_token (parser->lexer);
7245 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7247 type1 = cp_parser_type_id (parser);
7249 if (type1 == error_mark_node)
7250 return error_mark_node;
7252 /* Build a trivial decl-specifier-seq. */
7253 clear_decl_specs (&decl_specs);
7254 decl_specs.type = type1;
7256 /* Call grokdeclarator to figure out what type this is. */
7257 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7258 /*initialized=*/0, /*attrlist=*/NULL);
7262 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7264 type2 = cp_parser_type_id (parser);
7266 if (type2 == error_mark_node)
7267 return error_mark_node;
7269 /* Build a trivial decl-specifier-seq. */
7270 clear_decl_specs (&decl_specs);
7271 decl_specs.type = type2;
7273 /* Call grokdeclarator to figure out what type this is. */
7274 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7275 /*initialized=*/0, /*attrlist=*/NULL);
7278 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7280 /* Complete the trait expression, which may mean either processing
7281 the trait expr now or saving it for template instantiation. */
7282 return finish_trait_expr (kind, type1, type2);
7285 /* Lambdas that appear in variable initializer or default argument scope
7286 get that in their mangling, so we need to record it. We might as well
7287 use the count for function and namespace scopes as well. */
7288 static GTY(()) tree lambda_scope;
7289 static GTY(()) int lambda_count;
7290 typedef struct GTY(()) tree_int
7295 DEF_VEC_O(tree_int);
7296 DEF_VEC_ALLOC_O(tree_int,gc);
7297 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7300 start_lambda_scope (tree decl)
7304 /* Once we're inside a function, we ignore other scopes and just push
7305 the function again so that popping works properly. */
7306 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7307 decl = current_function_decl;
7308 ti.t = lambda_scope;
7309 ti.i = lambda_count;
7310 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7311 if (lambda_scope != decl)
7313 /* Don't reset the count if we're still in the same function. */
7314 lambda_scope = decl;
7320 record_lambda_scope (tree lambda)
7322 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7323 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7327 finish_lambda_scope (void)
7329 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7330 if (lambda_scope != p->t)
7332 lambda_scope = p->t;
7333 lambda_count = p->i;
7335 VEC_pop (tree_int, lambda_scope_stack);
7338 /* Parse a lambda expression.
7341 lambda-introducer lambda-declarator [opt] compound-statement
7343 Returns a representation of the expression. */
7346 cp_parser_lambda_expression (cp_parser* parser)
7348 tree lambda_expr = build_lambda_expr ();
7351 LAMBDA_EXPR_LOCATION (lambda_expr)
7352 = cp_lexer_peek_token (parser->lexer)->location;
7354 if (cp_unevaluated_operand)
7355 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7356 "lambda-expression in unevaluated context");
7358 /* We may be in the middle of deferred access check. Disable
7360 push_deferring_access_checks (dk_no_deferred);
7362 cp_parser_lambda_introducer (parser, lambda_expr);
7364 type = begin_lambda_type (lambda_expr);
7366 record_lambda_scope (lambda_expr);
7368 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7369 determine_visibility (TYPE_NAME (type));
7371 /* Now that we've started the type, add the capture fields for any
7372 explicit captures. */
7373 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7376 /* Inside the class, surrounding template-parameter-lists do not apply. */
7377 unsigned int saved_num_template_parameter_lists
7378 = parser->num_template_parameter_lists;
7380 parser->num_template_parameter_lists = 0;
7382 /* By virtue of defining a local class, a lambda expression has access to
7383 the private variables of enclosing classes. */
7385 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7387 cp_parser_lambda_body (parser, lambda_expr);
7389 /* The capture list was built up in reverse order; fix that now. */
7391 tree newlist = NULL_TREE;
7394 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7397 tree field = TREE_PURPOSE (elt);
7400 next = TREE_CHAIN (elt);
7401 TREE_CHAIN (elt) = newlist;
7404 /* Also add __ to the beginning of the field name so that code
7405 outside the lambda body can't see the captured name. We could
7406 just remove the name entirely, but this is more useful for
7408 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7409 /* The 'this' capture already starts with __. */
7412 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7413 buf[1] = buf[0] = '_';
7414 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7415 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7416 DECL_NAME (field) = get_identifier (buf);
7418 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7421 maybe_add_lambda_conv_op (type);
7423 type = finish_struct (type, /*attributes=*/NULL_TREE);
7425 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7428 pop_deferring_access_checks ();
7430 return build_lambda_object (lambda_expr);
7433 /* Parse the beginning of a lambda expression.
7436 [ lambda-capture [opt] ]
7438 LAMBDA_EXPR is the current representation of the lambda expression. */
7441 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7443 /* Need commas after the first capture. */
7446 /* Eat the leading `['. */
7447 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7449 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7450 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7451 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7452 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7453 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7454 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7456 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7458 cp_lexer_consume_token (parser->lexer);
7462 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7464 cp_token* capture_token;
7466 tree capture_init_expr;
7467 cp_id_kind idk = CP_ID_KIND_NONE;
7468 bool explicit_init_p = false;
7470 enum capture_kind_type
7475 enum capture_kind_type capture_kind = BY_COPY;
7477 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7479 error ("expected end of capture-list");
7486 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7488 /* Possibly capture `this'. */
7489 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7491 cp_lexer_consume_token (parser->lexer);
7492 add_capture (lambda_expr,
7493 /*id=*/get_identifier ("__this"),
7494 /*initializer=*/finish_this_expr(),
7495 /*by_reference_p=*/false,
7500 /* Remember whether we want to capture as a reference or not. */
7501 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7503 capture_kind = BY_REFERENCE;
7504 cp_lexer_consume_token (parser->lexer);
7507 /* Get the identifier. */
7508 capture_token = cp_lexer_peek_token (parser->lexer);
7509 capture_id = cp_parser_identifier (parser);
7511 if (capture_id == error_mark_node)
7512 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7513 delimiters, but I modified this to stop on unnested ']' as well. It
7514 was already changed to stop on unnested '}', so the
7515 "closing_parenthesis" name is no more misleading with my change. */
7517 cp_parser_skip_to_closing_parenthesis (parser,
7518 /*recovering=*/true,
7520 /*consume_paren=*/true);
7524 /* Find the initializer for this capture. */
7525 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7527 /* An explicit expression exists. */
7528 cp_lexer_consume_token (parser->lexer);
7529 pedwarn (input_location, OPT_pedantic,
7530 "ISO C++ does not allow initializers "
7531 "in lambda expression capture lists");
7532 capture_init_expr = cp_parser_assignment_expression (parser,
7535 explicit_init_p = true;
7539 const char* error_msg;
7541 /* Turn the identifier into an id-expression. */
7543 = cp_parser_lookup_name
7547 /*is_template=*/false,
7548 /*is_namespace=*/false,
7549 /*check_dependency=*/true,
7550 /*ambiguous_decls=*/NULL,
7551 capture_token->location);
7554 = finish_id_expression
7559 /*integral_constant_expression_p=*/false,
7560 /*allow_non_integral_constant_expression_p=*/false,
7561 /*non_integral_constant_expression_p=*/NULL,
7562 /*template_p=*/false,
7564 /*address_p=*/false,
7565 /*template_arg_p=*/false,
7567 capture_token->location);
7570 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7572 = unqualified_name_lookup_error (capture_init_expr);
7574 add_capture (lambda_expr,
7577 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7581 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7584 /* Parse the (optional) middle of a lambda expression.
7587 ( parameter-declaration-clause [opt] )
7588 attribute-specifier [opt]
7590 exception-specification [opt]
7591 lambda-return-type-clause [opt]
7593 LAMBDA_EXPR is the current representation of the lambda expression. */
7596 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7598 /* 5.1.1.4 of the standard says:
7599 If a lambda-expression does not include a lambda-declarator, it is as if
7600 the lambda-declarator were ().
7601 This means an empty parameter list, no attributes, and no exception
7603 tree param_list = void_list_node;
7604 tree attributes = NULL_TREE;
7605 tree exception_spec = NULL_TREE;
7608 /* The lambda-declarator is optional, but must begin with an opening
7609 parenthesis if present. */
7610 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7612 cp_lexer_consume_token (parser->lexer);
7614 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7616 /* Parse parameters. */
7617 param_list = cp_parser_parameter_declaration_clause (parser);
7619 /* Default arguments shall not be specified in the
7620 parameter-declaration-clause of a lambda-declarator. */
7621 for (t = param_list; t; t = TREE_CHAIN (t))
7622 if (TREE_PURPOSE (t))
7623 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7624 "default argument specified for lambda parameter");
7626 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7628 attributes = cp_parser_attributes_opt (parser);
7630 /* Parse optional `mutable' keyword. */
7631 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7633 cp_lexer_consume_token (parser->lexer);
7634 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7637 /* Parse optional exception specification. */
7638 exception_spec = cp_parser_exception_specification_opt (parser);
7640 /* Parse optional trailing return type. */
7641 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7643 cp_lexer_consume_token (parser->lexer);
7644 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7647 /* The function parameters must be in scope all the way until after the
7648 trailing-return-type in case of decltype. */
7649 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7650 pop_binding (DECL_NAME (t), t);
7655 /* Create the function call operator.
7657 Messing with declarators like this is no uglier than building up the
7658 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7661 cp_decl_specifier_seq return_type_specs;
7662 cp_declarator* declarator;
7667 clear_decl_specs (&return_type_specs);
7668 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7669 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7671 /* Maybe we will deduce the return type later, but we can use void
7672 as a placeholder return type anyways. */
7673 return_type_specs.type = void_type_node;
7675 p = obstack_alloc (&declarator_obstack, 0);
7677 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7680 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7681 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7682 declarator = make_call_declarator (declarator, param_list, quals,
7684 /*late_return_type=*/NULL_TREE);
7685 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7687 fco = grokmethod (&return_type_specs,
7690 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7691 DECL_ARTIFICIAL (fco) = 1;
7693 finish_member_declaration (fco);
7695 obstack_free (&declarator_obstack, p);
7699 /* Parse the body of a lambda expression, which is simply
7703 but which requires special handling.
7704 LAMBDA_EXPR is the current representation of the lambda expression. */
7707 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7709 bool nested = (current_function_decl != NULL_TREE);
7711 push_function_context ();
7713 /* Finish the function call operator
7715 + late_parsing_for_member
7716 + function_definition_after_declarator
7717 + ctor_initializer_opt_and_function_body */
7719 tree fco = lambda_function (lambda_expr);
7723 /* Let the front end know that we are going to be defining this
7725 start_preparsed_function (fco,
7727 SF_PRE_PARSED | SF_INCLASS_INLINE);
7729 start_lambda_scope (fco);
7730 body = begin_function_body ();
7732 /* 5.1.1.4 of the standard says:
7733 If a lambda-expression does not include a trailing-return-type, it
7734 is as if the trailing-return-type denotes the following type:
7735 * if the compound-statement is of the form
7736 { return attribute-specifier [opt] expression ; }
7737 the type of the returned expression after lvalue-to-rvalue
7738 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7739 (_conv.array_ 4.2), and function-to-pointer conversion
7741 * otherwise, void. */
7743 /* In a lambda that has neither a lambda-return-type-clause
7744 nor a deducible form, errors should be reported for return statements
7745 in the body. Since we used void as the placeholder return type, parsing
7746 the body as usual will give such desired behavior. */
7747 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7748 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7749 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7750 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7753 tree expr = NULL_TREE;
7754 cp_id_kind idk = CP_ID_KIND_NONE;
7756 /* Parse tentatively in case there's more after the initial return
7758 cp_parser_parse_tentatively (parser);
7760 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7761 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7763 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7765 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7766 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7768 if (cp_parser_parse_definitely (parser))
7770 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7772 compound_stmt = begin_compound_stmt (0);
7773 /* Will get error here if type not deduced yet. */
7774 finish_return_stmt (expr);
7775 finish_compound_stmt (compound_stmt);
7783 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7784 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7785 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7786 cp_parser_compound_stmt does not pass it. */
7787 cp_parser_function_body (parser);
7788 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7791 finish_function_body (body);
7792 finish_lambda_scope ();
7794 /* Finish the function and generate code for it if necessary. */
7795 expand_or_defer_fn (finish_function (/*inline*/2));
7799 pop_function_context();
7802 /* Statements [gram.stmt.stmt] */
7804 /* Parse a statement.
7808 expression-statement
7813 declaration-statement
7816 IN_COMPOUND is true when the statement is nested inside a
7817 cp_parser_compound_statement; this matters for certain pragmas.
7819 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7820 is a (possibly labeled) if statement which is not enclosed in braces
7821 and has an else clause. This is used to implement -Wparentheses. */
7824 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7825 bool in_compound, bool *if_p)
7829 location_t statement_location;
7834 /* There is no statement yet. */
7835 statement = NULL_TREE;
7836 /* Peek at the next token. */
7837 token = cp_lexer_peek_token (parser->lexer);
7838 /* Remember the location of the first token in the statement. */
7839 statement_location = token->location;
7840 /* If this is a keyword, then that will often determine what kind of
7841 statement we have. */
7842 if (token->type == CPP_KEYWORD)
7844 enum rid keyword = token->keyword;
7850 /* Looks like a labeled-statement with a case label.
7851 Parse the label, and then use tail recursion to parse
7853 cp_parser_label_for_labeled_statement (parser);
7858 statement = cp_parser_selection_statement (parser, if_p);
7864 statement = cp_parser_iteration_statement (parser);
7871 statement = cp_parser_jump_statement (parser);
7874 /* Objective-C++ exception-handling constructs. */
7877 case RID_AT_FINALLY:
7878 case RID_AT_SYNCHRONIZED:
7880 statement = cp_parser_objc_statement (parser);
7884 statement = cp_parser_try_block (parser);
7888 /* This must be a namespace alias definition. */
7889 cp_parser_declaration_statement (parser);
7893 /* It might be a keyword like `int' that can start a
7894 declaration-statement. */
7898 else if (token->type == CPP_NAME)
7900 /* If the next token is a `:', then we are looking at a
7901 labeled-statement. */
7902 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7903 if (token->type == CPP_COLON)
7905 /* Looks like a labeled-statement with an ordinary label.
7906 Parse the label, and then use tail recursion to parse
7908 cp_parser_label_for_labeled_statement (parser);
7912 /* Anything that starts with a `{' must be a compound-statement. */
7913 else if (token->type == CPP_OPEN_BRACE)
7914 statement = cp_parser_compound_statement (parser, NULL, false);
7915 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7916 a statement all its own. */
7917 else if (token->type == CPP_PRAGMA)
7919 /* Only certain OpenMP pragmas are attached to statements, and thus
7920 are considered statements themselves. All others are not. In
7921 the context of a compound, accept the pragma as a "statement" and
7922 return so that we can check for a close brace. Otherwise we
7923 require a real statement and must go back and read one. */
7925 cp_parser_pragma (parser, pragma_compound);
7926 else if (!cp_parser_pragma (parser, pragma_stmt))
7930 else if (token->type == CPP_EOF)
7932 cp_parser_error (parser, "expected statement");
7936 /* Everything else must be a declaration-statement or an
7937 expression-statement. Try for the declaration-statement
7938 first, unless we are looking at a `;', in which case we know that
7939 we have an expression-statement. */
7942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7944 cp_parser_parse_tentatively (parser);
7945 /* Try to parse the declaration-statement. */
7946 cp_parser_declaration_statement (parser);
7947 /* If that worked, we're done. */
7948 if (cp_parser_parse_definitely (parser))
7951 /* Look for an expression-statement instead. */
7952 statement = cp_parser_expression_statement (parser, in_statement_expr);
7955 /* Set the line number for the statement. */
7956 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7957 SET_EXPR_LOCATION (statement, statement_location);
7960 /* Parse the label for a labeled-statement, i.e.
7963 case constant-expression :
7967 case constant-expression ... constant-expression : statement
7969 When a label is parsed without errors, the label is added to the
7970 parse tree by the finish_* functions, so this function doesn't
7971 have to return the label. */
7974 cp_parser_label_for_labeled_statement (cp_parser* parser)
7977 tree label = NULL_TREE;
7979 /* The next token should be an identifier. */
7980 token = cp_lexer_peek_token (parser->lexer);
7981 if (token->type != CPP_NAME
7982 && token->type != CPP_KEYWORD)
7984 cp_parser_error (parser, "expected labeled-statement");
7988 switch (token->keyword)
7995 /* Consume the `case' token. */
7996 cp_lexer_consume_token (parser->lexer);
7997 /* Parse the constant-expression. */
7998 expr = cp_parser_constant_expression (parser,
7999 /*allow_non_constant_p=*/false,
8002 ellipsis = cp_lexer_peek_token (parser->lexer);
8003 if (ellipsis->type == CPP_ELLIPSIS)
8005 /* Consume the `...' token. */
8006 cp_lexer_consume_token (parser->lexer);
8008 cp_parser_constant_expression (parser,
8009 /*allow_non_constant_p=*/false,
8011 /* We don't need to emit warnings here, as the common code
8012 will do this for us. */
8015 expr_hi = NULL_TREE;
8017 if (parser->in_switch_statement_p)
8018 finish_case_label (token->location, expr, expr_hi);
8020 error_at (token->location,
8021 "case label %qE not within a switch statement",
8027 /* Consume the `default' token. */
8028 cp_lexer_consume_token (parser->lexer);
8030 if (parser->in_switch_statement_p)
8031 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8033 error_at (token->location, "case label not within a switch statement");
8037 /* Anything else must be an ordinary label. */
8038 label = finish_label_stmt (cp_parser_identifier (parser));
8042 /* Require the `:' token. */
8043 cp_parser_require (parser, CPP_COLON, RT_COLON);
8045 /* An ordinary label may optionally be followed by attributes.
8046 However, this is only permitted if the attributes are then
8047 followed by a semicolon. This is because, for backward
8048 compatibility, when parsing
8049 lab: __attribute__ ((unused)) int i;
8050 we want the attribute to attach to "i", not "lab". */
8051 if (label != NULL_TREE
8052 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8056 cp_parser_parse_tentatively (parser);
8057 attrs = cp_parser_attributes_opt (parser);
8058 if (attrs == NULL_TREE
8059 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8060 cp_parser_abort_tentative_parse (parser);
8061 else if (!cp_parser_parse_definitely (parser))
8064 cplus_decl_attributes (&label, attrs, 0);
8068 /* Parse an expression-statement.
8070 expression-statement:
8073 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8074 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8075 indicates whether this expression-statement is part of an
8076 expression statement. */
8079 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8081 tree statement = NULL_TREE;
8082 cp_token *token = cp_lexer_peek_token (parser->lexer);
8084 /* If the next token is a ';', then there is no expression
8086 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8087 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8089 /* Give a helpful message for "A<T>::type t;" and the like. */
8090 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8091 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8093 if (TREE_CODE (statement) == SCOPE_REF)
8094 error_at (token->location, "need %<typename%> before %qE because "
8095 "%qT is a dependent scope",
8096 statement, TREE_OPERAND (statement, 0));
8097 else if (is_overloaded_fn (statement)
8098 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8101 tree fn = get_first_fn (statement);
8102 error_at (token->location,
8103 "%<%T::%D%> names the constructor, not the type",
8104 DECL_CONTEXT (fn), DECL_NAME (fn));
8108 /* Consume the final `;'. */
8109 cp_parser_consume_semicolon_at_end_of_statement (parser);
8111 if (in_statement_expr
8112 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8113 /* This is the final expression statement of a statement
8115 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8117 statement = finish_expr_stmt (statement);
8124 /* Parse a compound-statement.
8127 { statement-seq [opt] }
8132 { label-declaration-seq [opt] statement-seq [opt] }
8134 label-declaration-seq:
8136 label-declaration-seq label-declaration
8138 Returns a tree representing the statement. */
8141 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8146 /* Consume the `{'. */
8147 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8148 return error_mark_node;
8149 /* Begin the compound-statement. */
8150 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8151 /* If the next keyword is `__label__' we have a label declaration. */
8152 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8153 cp_parser_label_declaration (parser);
8154 /* Parse an (optional) statement-seq. */
8155 cp_parser_statement_seq_opt (parser, in_statement_expr);
8156 /* Finish the compound-statement. */
8157 finish_compound_stmt (compound_stmt);
8158 /* Consume the `}'. */
8159 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8161 return compound_stmt;
8164 /* Parse an (optional) statement-seq.
8168 statement-seq [opt] statement */
8171 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8173 /* Scan statements until there aren't any more. */
8176 cp_token *token = cp_lexer_peek_token (parser->lexer);
8178 /* If we're looking at a `}', then we've run out of statements. */
8179 if (token->type == CPP_CLOSE_BRACE
8180 || token->type == CPP_EOF
8181 || token->type == CPP_PRAGMA_EOL)
8184 /* If we are in a compound statement and find 'else' then
8185 something went wrong. */
8186 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8188 if (parser->in_statement & IN_IF_STMT)
8192 token = cp_lexer_consume_token (parser->lexer);
8193 error_at (token->location, "%<else%> without a previous %<if%>");
8197 /* Parse the statement. */
8198 cp_parser_statement (parser, in_statement_expr, true, NULL);
8202 /* Parse a selection-statement.
8204 selection-statement:
8205 if ( condition ) statement
8206 if ( condition ) statement else statement
8207 switch ( condition ) statement
8209 Returns the new IF_STMT or SWITCH_STMT.
8211 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8212 is a (possibly labeled) if statement which is not enclosed in
8213 braces and has an else clause. This is used to implement
8217 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8225 /* Peek at the next token. */
8226 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8228 /* See what kind of keyword it is. */
8229 keyword = token->keyword;
8238 /* Look for the `('. */
8239 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8241 cp_parser_skip_to_end_of_statement (parser);
8242 return error_mark_node;
8245 /* Begin the selection-statement. */
8246 if (keyword == RID_IF)
8247 statement = begin_if_stmt ();
8249 statement = begin_switch_stmt ();
8251 /* Parse the condition. */
8252 condition = cp_parser_condition (parser);
8253 /* Look for the `)'. */
8254 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8255 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8256 /*consume_paren=*/true);
8258 if (keyword == RID_IF)
8261 unsigned char in_statement;
8263 /* Add the condition. */
8264 finish_if_stmt_cond (condition, statement);
8266 /* Parse the then-clause. */
8267 in_statement = parser->in_statement;
8268 parser->in_statement |= IN_IF_STMT;
8269 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8271 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8272 add_stmt (build_empty_stmt (loc));
8273 cp_lexer_consume_token (parser->lexer);
8274 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8275 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8276 "empty body in an %<if%> statement");
8280 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8281 parser->in_statement = in_statement;
8283 finish_then_clause (statement);
8285 /* If the next token is `else', parse the else-clause. */
8286 if (cp_lexer_next_token_is_keyword (parser->lexer,
8289 /* Consume the `else' keyword. */
8290 cp_lexer_consume_token (parser->lexer);
8291 begin_else_clause (statement);
8292 /* Parse the else-clause. */
8293 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8296 loc = cp_lexer_peek_token (parser->lexer)->location;
8298 OPT_Wempty_body, "suggest braces around "
8299 "empty body in an %<else%> statement");
8300 add_stmt (build_empty_stmt (loc));
8301 cp_lexer_consume_token (parser->lexer);
8304 cp_parser_implicitly_scoped_statement (parser, NULL);
8306 finish_else_clause (statement);
8308 /* If we are currently parsing a then-clause, then
8309 IF_P will not be NULL. We set it to true to
8310 indicate that this if statement has an else clause.
8311 This may trigger the Wparentheses warning below
8312 when we get back up to the parent if statement. */
8318 /* This if statement does not have an else clause. If
8319 NESTED_IF is true, then the then-clause is an if
8320 statement which does have an else clause. We warn
8321 about the potential ambiguity. */
8323 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8324 "suggest explicit braces to avoid ambiguous"
8328 /* Now we're all done with the if-statement. */
8329 finish_if_stmt (statement);
8333 bool in_switch_statement_p;
8334 unsigned char in_statement;
8336 /* Add the condition. */
8337 finish_switch_cond (condition, statement);
8339 /* Parse the body of the switch-statement. */
8340 in_switch_statement_p = parser->in_switch_statement_p;
8341 in_statement = parser->in_statement;
8342 parser->in_switch_statement_p = true;
8343 parser->in_statement |= IN_SWITCH_STMT;
8344 cp_parser_implicitly_scoped_statement (parser, NULL);
8345 parser->in_switch_statement_p = in_switch_statement_p;
8346 parser->in_statement = in_statement;
8348 /* Now we're all done with the switch-statement. */
8349 finish_switch_stmt (statement);
8357 cp_parser_error (parser, "expected selection-statement");
8358 return error_mark_node;
8362 /* Parse a condition.
8366 type-specifier-seq declarator = initializer-clause
8367 type-specifier-seq declarator braced-init-list
8372 type-specifier-seq declarator asm-specification [opt]
8373 attributes [opt] = assignment-expression
8375 Returns the expression that should be tested. */
8378 cp_parser_condition (cp_parser* parser)
8380 cp_decl_specifier_seq type_specifiers;
8381 const char *saved_message;
8383 /* Try the declaration first. */
8384 cp_parser_parse_tentatively (parser);
8385 /* New types are not allowed in the type-specifier-seq for a
8387 saved_message = parser->type_definition_forbidden_message;
8388 parser->type_definition_forbidden_message
8389 = G_("types may not be defined in conditions");
8390 /* Parse the type-specifier-seq. */
8391 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8392 /*is_trailing_return=*/false,
8394 /* Restore the saved message. */
8395 parser->type_definition_forbidden_message = saved_message;
8396 /* If all is well, we might be looking at a declaration. */
8397 if (!cp_parser_error_occurred (parser))
8400 tree asm_specification;
8402 cp_declarator *declarator;
8403 tree initializer = NULL_TREE;
8405 /* Parse the declarator. */
8406 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8407 /*ctor_dtor_or_conv_p=*/NULL,
8408 /*parenthesized_p=*/NULL,
8409 /*member_p=*/false);
8410 /* Parse the attributes. */
8411 attributes = cp_parser_attributes_opt (parser);
8412 /* Parse the asm-specification. */
8413 asm_specification = cp_parser_asm_specification_opt (parser);
8414 /* If the next token is not an `=' or '{', then we might still be
8415 looking at an expression. For example:
8419 looks like a decl-specifier-seq and a declarator -- but then
8420 there is no `=', so this is an expression. */
8421 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8422 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8423 cp_parser_simulate_error (parser);
8425 /* If we did see an `=' or '{', then we are looking at a declaration
8427 if (cp_parser_parse_definitely (parser))
8430 bool non_constant_p;
8431 bool flags = LOOKUP_ONLYCONVERTING;
8433 /* Create the declaration. */
8434 decl = start_decl (declarator, &type_specifiers,
8435 /*initialized_p=*/true,
8436 attributes, /*prefix_attributes=*/NULL_TREE,
8439 /* Parse the initializer. */
8440 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8442 initializer = cp_parser_braced_list (parser, &non_constant_p);
8443 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8448 /* Consume the `='. */
8449 cp_parser_require (parser, CPP_EQ, RT_EQ);
8450 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8452 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8453 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8455 if (!non_constant_p)
8456 initializer = fold_non_dependent_expr (initializer);
8458 /* Process the initializer. */
8459 cp_finish_decl (decl,
8460 initializer, !non_constant_p,
8465 pop_scope (pushed_scope);
8467 return convert_from_reference (decl);
8470 /* If we didn't even get past the declarator successfully, we are
8471 definitely not looking at a declaration. */
8473 cp_parser_abort_tentative_parse (parser);
8475 /* Otherwise, we are looking at an expression. */
8476 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8479 /* Parse an iteration-statement.
8481 iteration-statement:
8482 while ( condition ) statement
8483 do statement while ( expression ) ;
8484 for ( for-init-statement condition [opt] ; expression [opt] )
8487 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8490 cp_parser_iteration_statement (cp_parser* parser)
8495 unsigned char in_statement;
8497 /* Peek at the next token. */
8498 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8500 return error_mark_node;
8502 /* Remember whether or not we are already within an iteration
8504 in_statement = parser->in_statement;
8506 /* See what kind of keyword it is. */
8507 keyword = token->keyword;
8514 /* Begin the while-statement. */
8515 statement = begin_while_stmt ();
8516 /* Look for the `('. */
8517 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8518 /* Parse the condition. */
8519 condition = cp_parser_condition (parser);
8520 finish_while_stmt_cond (condition, statement);
8521 /* Look for the `)'. */
8522 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8523 /* Parse the dependent statement. */
8524 parser->in_statement = IN_ITERATION_STMT;
8525 cp_parser_already_scoped_statement (parser);
8526 parser->in_statement = in_statement;
8527 /* We're done with the while-statement. */
8528 finish_while_stmt (statement);
8536 /* Begin the do-statement. */
8537 statement = begin_do_stmt ();
8538 /* Parse the body of the do-statement. */
8539 parser->in_statement = IN_ITERATION_STMT;
8540 cp_parser_implicitly_scoped_statement (parser, NULL);
8541 parser->in_statement = in_statement;
8542 finish_do_body (statement);
8543 /* Look for the `while' keyword. */
8544 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8545 /* Look for the `('. */
8546 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8547 /* Parse the expression. */
8548 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8549 /* We're done with the do-statement. */
8550 finish_do_stmt (expression, statement);
8551 /* Look for the `)'. */
8552 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8553 /* Look for the `;'. */
8554 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8560 tree condition = NULL_TREE;
8561 tree expression = NULL_TREE;
8563 /* Begin the for-statement. */
8564 statement = begin_for_stmt ();
8565 /* Look for the `('. */
8566 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8567 /* Parse the initialization. */
8568 cp_parser_for_init_statement (parser);
8569 finish_for_init_stmt (statement);
8571 /* If there's a condition, process it. */
8572 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8573 condition = cp_parser_condition (parser);
8574 finish_for_cond (condition, statement);
8575 /* Look for the `;'. */
8576 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8578 /* If there's an expression, process it. */
8579 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8580 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8581 finish_for_expr (expression, statement);
8582 /* Look for the `)'. */
8583 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8585 /* Parse the body of the for-statement. */
8586 parser->in_statement = IN_ITERATION_STMT;
8587 cp_parser_already_scoped_statement (parser);
8588 parser->in_statement = in_statement;
8590 /* We're done with the for-statement. */
8591 finish_for_stmt (statement);
8596 cp_parser_error (parser, "expected iteration-statement");
8597 statement = error_mark_node;
8604 /* Parse a for-init-statement.
8607 expression-statement
8608 simple-declaration */
8611 cp_parser_for_init_statement (cp_parser* parser)
8613 /* If the next token is a `;', then we have an empty
8614 expression-statement. Grammatically, this is also a
8615 simple-declaration, but an invalid one, because it does not
8616 declare anything. Therefore, if we did not handle this case
8617 specially, we would issue an error message about an invalid
8619 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8621 /* We're going to speculatively look for a declaration, falling back
8622 to an expression, if necessary. */
8623 cp_parser_parse_tentatively (parser);
8624 /* Parse the declaration. */
8625 cp_parser_simple_declaration (parser,
8626 /*function_definition_allowed_p=*/false);
8627 /* If the tentative parse failed, then we shall need to look for an
8628 expression-statement. */
8629 if (cp_parser_parse_definitely (parser))
8633 cp_parser_expression_statement (parser, NULL_TREE);
8636 /* Parse a jump-statement.
8641 return expression [opt] ;
8642 return braced-init-list ;
8650 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8653 cp_parser_jump_statement (cp_parser* parser)
8655 tree statement = error_mark_node;
8658 unsigned char in_statement;
8660 /* Peek at the next token. */
8661 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
8663 return error_mark_node;
8665 /* See what kind of keyword it is. */
8666 keyword = token->keyword;
8670 in_statement = parser->in_statement & ~IN_IF_STMT;
8671 switch (in_statement)
8674 error_at (token->location, "break statement not within loop or switch");
8677 gcc_assert ((in_statement & IN_SWITCH_STMT)
8678 || in_statement == IN_ITERATION_STMT);
8679 statement = finish_break_stmt ();
8682 error_at (token->location, "invalid exit from OpenMP structured block");
8685 error_at (token->location, "break statement used with OpenMP for loop");
8688 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8692 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8695 error_at (token->location, "continue statement not within a loop");
8697 case IN_ITERATION_STMT:
8699 statement = finish_continue_stmt ();
8702 error_at (token->location, "invalid exit from OpenMP structured block");
8707 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8713 bool expr_non_constant_p;
8715 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8717 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8718 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8720 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8721 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8723 /* If the next token is a `;', then there is no
8726 /* Build the return-statement. */
8727 statement = finish_return_stmt (expr);
8728 /* Look for the final `;'. */
8729 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8734 /* Create the goto-statement. */
8735 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8737 /* Issue a warning about this use of a GNU extension. */
8738 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8739 /* Consume the '*' token. */
8740 cp_lexer_consume_token (parser->lexer);
8741 /* Parse the dependent expression. */
8742 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8745 finish_goto_stmt (cp_parser_identifier (parser));
8746 /* Look for the final `;'. */
8747 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8751 cp_parser_error (parser, "expected jump-statement");
8758 /* Parse a declaration-statement.
8760 declaration-statement:
8761 block-declaration */
8764 cp_parser_declaration_statement (cp_parser* parser)
8768 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8769 p = obstack_alloc (&declarator_obstack, 0);
8771 /* Parse the block-declaration. */
8772 cp_parser_block_declaration (parser, /*statement_p=*/true);
8774 /* Free any declarators allocated. */
8775 obstack_free (&declarator_obstack, p);
8777 /* Finish off the statement. */
8781 /* Some dependent statements (like `if (cond) statement'), are
8782 implicitly in their own scope. In other words, if the statement is
8783 a single statement (as opposed to a compound-statement), it is
8784 none-the-less treated as if it were enclosed in braces. Any
8785 declarations appearing in the dependent statement are out of scope
8786 after control passes that point. This function parses a statement,
8787 but ensures that is in its own scope, even if it is not a
8790 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8791 is a (possibly labeled) if statement which is not enclosed in
8792 braces and has an else clause. This is used to implement
8795 Returns the new statement. */
8798 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8805 /* Mark if () ; with a special NOP_EXPR. */
8806 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8808 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8809 cp_lexer_consume_token (parser->lexer);
8810 statement = add_stmt (build_empty_stmt (loc));
8812 /* if a compound is opened, we simply parse the statement directly. */
8813 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8814 statement = cp_parser_compound_statement (parser, NULL, false);
8815 /* If the token is not a `{', then we must take special action. */
8818 /* Create a compound-statement. */
8819 statement = begin_compound_stmt (0);
8820 /* Parse the dependent-statement. */
8821 cp_parser_statement (parser, NULL_TREE, false, if_p);
8822 /* Finish the dummy compound-statement. */
8823 finish_compound_stmt (statement);
8826 /* Return the statement. */
8830 /* For some dependent statements (like `while (cond) statement'), we
8831 have already created a scope. Therefore, even if the dependent
8832 statement is a compound-statement, we do not want to create another
8836 cp_parser_already_scoped_statement (cp_parser* parser)
8838 /* If the token is a `{', then we must take special action. */
8839 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8840 cp_parser_statement (parser, NULL_TREE, false, NULL);
8843 /* Avoid calling cp_parser_compound_statement, so that we
8844 don't create a new scope. Do everything else by hand. */
8845 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
8846 /* If the next keyword is `__label__' we have a label declaration. */
8847 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8848 cp_parser_label_declaration (parser);
8849 /* Parse an (optional) statement-seq. */
8850 cp_parser_statement_seq_opt (parser, NULL_TREE);
8851 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8855 /* Declarations [gram.dcl.dcl] */
8857 /* Parse an optional declaration-sequence.
8861 declaration-seq declaration */
8864 cp_parser_declaration_seq_opt (cp_parser* parser)
8870 token = cp_lexer_peek_token (parser->lexer);
8872 if (token->type == CPP_CLOSE_BRACE
8873 || token->type == CPP_EOF
8874 || token->type == CPP_PRAGMA_EOL)
8877 if (token->type == CPP_SEMICOLON)
8879 /* A declaration consisting of a single semicolon is
8880 invalid. Allow it unless we're being pedantic. */
8881 cp_lexer_consume_token (parser->lexer);
8882 if (!in_system_header)
8883 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8887 /* If we're entering or exiting a region that's implicitly
8888 extern "C", modify the lang context appropriately. */
8889 if (!parser->implicit_extern_c && token->implicit_extern_c)
8891 push_lang_context (lang_name_c);
8892 parser->implicit_extern_c = true;
8894 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8896 pop_lang_context ();
8897 parser->implicit_extern_c = false;
8900 if (token->type == CPP_PRAGMA)
8902 /* A top-level declaration can consist solely of a #pragma.
8903 A nested declaration cannot, so this is done here and not
8904 in cp_parser_declaration. (A #pragma at block scope is
8905 handled in cp_parser_statement.) */
8906 cp_parser_pragma (parser, pragma_external);
8910 /* Parse the declaration itself. */
8911 cp_parser_declaration (parser);
8915 /* Parse a declaration.
8920 template-declaration
8921 explicit-instantiation
8922 explicit-specialization
8923 linkage-specification
8924 namespace-definition
8929 __extension__ declaration */
8932 cp_parser_declaration (cp_parser* parser)
8939 /* Check for the `__extension__' keyword. */
8940 if (cp_parser_extension_opt (parser, &saved_pedantic))
8942 /* Parse the qualified declaration. */
8943 cp_parser_declaration (parser);
8944 /* Restore the PEDANTIC flag. */
8945 pedantic = saved_pedantic;
8950 /* Try to figure out what kind of declaration is present. */
8951 token1 = *cp_lexer_peek_token (parser->lexer);
8953 if (token1.type != CPP_EOF)
8954 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8957 token2.type = CPP_EOF;
8958 token2.keyword = RID_MAX;
8961 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8962 p = obstack_alloc (&declarator_obstack, 0);
8964 /* If the next token is `extern' and the following token is a string
8965 literal, then we have a linkage specification. */
8966 if (token1.keyword == RID_EXTERN
8967 && cp_parser_is_string_literal (&token2))
8968 cp_parser_linkage_specification (parser);
8969 /* If the next token is `template', then we have either a template
8970 declaration, an explicit instantiation, or an explicit
8972 else if (token1.keyword == RID_TEMPLATE)
8974 /* `template <>' indicates a template specialization. */
8975 if (token2.type == CPP_LESS
8976 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8977 cp_parser_explicit_specialization (parser);
8978 /* `template <' indicates a template declaration. */
8979 else if (token2.type == CPP_LESS)
8980 cp_parser_template_declaration (parser, /*member_p=*/false);
8981 /* Anything else must be an explicit instantiation. */
8983 cp_parser_explicit_instantiation (parser);
8985 /* If the next token is `export', then we have a template
8987 else if (token1.keyword == RID_EXPORT)
8988 cp_parser_template_declaration (parser, /*member_p=*/false);
8989 /* If the next token is `extern', 'static' or 'inline' and the one
8990 after that is `template', we have a GNU extended explicit
8991 instantiation directive. */
8992 else if (cp_parser_allow_gnu_extensions_p (parser)
8993 && (token1.keyword == RID_EXTERN
8994 || token1.keyword == RID_STATIC
8995 || token1.keyword == RID_INLINE)
8996 && token2.keyword == RID_TEMPLATE)
8997 cp_parser_explicit_instantiation (parser);
8998 /* If the next token is `namespace', check for a named or unnamed
8999 namespace definition. */
9000 else if (token1.keyword == RID_NAMESPACE
9001 && (/* A named namespace definition. */
9002 (token2.type == CPP_NAME
9003 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9005 /* An unnamed namespace definition. */
9006 || token2.type == CPP_OPEN_BRACE
9007 || token2.keyword == RID_ATTRIBUTE))
9008 cp_parser_namespace_definition (parser);
9009 /* An inline (associated) namespace definition. */
9010 else if (token1.keyword == RID_INLINE
9011 && token2.keyword == RID_NAMESPACE)
9012 cp_parser_namespace_definition (parser);
9013 /* Objective-C++ declaration/definition. */
9014 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9015 cp_parser_objc_declaration (parser);
9016 /* We must have either a block declaration or a function
9019 /* Try to parse a block-declaration, or a function-definition. */
9020 cp_parser_block_declaration (parser, /*statement_p=*/false);
9022 /* Free any declarators allocated. */
9023 obstack_free (&declarator_obstack, p);
9026 /* Parse a block-declaration.
9031 namespace-alias-definition
9038 __extension__ block-declaration
9043 static_assert-declaration
9045 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9046 part of a declaration-statement. */
9049 cp_parser_block_declaration (cp_parser *parser,
9055 /* Check for the `__extension__' keyword. */
9056 if (cp_parser_extension_opt (parser, &saved_pedantic))
9058 /* Parse the qualified declaration. */
9059 cp_parser_block_declaration (parser, statement_p);
9060 /* Restore the PEDANTIC flag. */
9061 pedantic = saved_pedantic;
9066 /* Peek at the next token to figure out which kind of declaration is
9068 token1 = cp_lexer_peek_token (parser->lexer);
9070 /* If the next keyword is `asm', we have an asm-definition. */
9071 if (token1->keyword == RID_ASM)
9074 cp_parser_commit_to_tentative_parse (parser);
9075 cp_parser_asm_definition (parser);
9077 /* If the next keyword is `namespace', we have a
9078 namespace-alias-definition. */
9079 else if (token1->keyword == RID_NAMESPACE)
9080 cp_parser_namespace_alias_definition (parser);
9081 /* If the next keyword is `using', we have either a
9082 using-declaration or a using-directive. */
9083 else if (token1->keyword == RID_USING)
9088 cp_parser_commit_to_tentative_parse (parser);
9089 /* If the token after `using' is `namespace', then we have a
9091 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9092 if (token2->keyword == RID_NAMESPACE)
9093 cp_parser_using_directive (parser);
9094 /* Otherwise, it's a using-declaration. */
9096 cp_parser_using_declaration (parser,
9097 /*access_declaration_p=*/false);
9099 /* If the next keyword is `__label__' we have a misplaced label
9101 else if (token1->keyword == RID_LABEL)
9103 cp_lexer_consume_token (parser->lexer);
9104 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9105 cp_parser_skip_to_end_of_statement (parser);
9106 /* If the next token is now a `;', consume it. */
9107 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9108 cp_lexer_consume_token (parser->lexer);
9110 /* If the next token is `static_assert' we have a static assertion. */
9111 else if (token1->keyword == RID_STATIC_ASSERT)
9112 cp_parser_static_assert (parser, /*member_p=*/false);
9113 /* Anything else must be a simple-declaration. */
9115 cp_parser_simple_declaration (parser, !statement_p);
9118 /* Parse a simple-declaration.
9121 decl-specifier-seq [opt] init-declarator-list [opt] ;
9123 init-declarator-list:
9125 init-declarator-list , init-declarator
9127 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9128 function-definition as a simple-declaration. */
9131 cp_parser_simple_declaration (cp_parser* parser,
9132 bool function_definition_allowed_p)
9134 cp_decl_specifier_seq decl_specifiers;
9135 int declares_class_or_enum;
9136 bool saw_declarator;
9138 /* Defer access checks until we know what is being declared; the
9139 checks for names appearing in the decl-specifier-seq should be
9140 done as if we were in the scope of the thing being declared. */
9141 push_deferring_access_checks (dk_deferred);
9143 /* Parse the decl-specifier-seq. We have to keep track of whether
9144 or not the decl-specifier-seq declares a named class or
9145 enumeration type, since that is the only case in which the
9146 init-declarator-list is allowed to be empty.
9150 In a simple-declaration, the optional init-declarator-list can be
9151 omitted only when declaring a class or enumeration, that is when
9152 the decl-specifier-seq contains either a class-specifier, an
9153 elaborated-type-specifier, or an enum-specifier. */
9154 cp_parser_decl_specifier_seq (parser,
9155 CP_PARSER_FLAGS_OPTIONAL,
9157 &declares_class_or_enum);
9158 /* We no longer need to defer access checks. */
9159 stop_deferring_access_checks ();
9161 /* In a block scope, a valid declaration must always have a
9162 decl-specifier-seq. By not trying to parse declarators, we can
9163 resolve the declaration/expression ambiguity more quickly. */
9164 if (!function_definition_allowed_p
9165 && !decl_specifiers.any_specifiers_p)
9167 cp_parser_error (parser, "expected declaration");
9171 /* If the next two tokens are both identifiers, the code is
9172 erroneous. The usual cause of this situation is code like:
9176 where "T" should name a type -- but does not. */
9177 if (!decl_specifiers.any_type_specifiers_p
9178 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9180 /* If parsing tentatively, we should commit; we really are
9181 looking at a declaration. */
9182 cp_parser_commit_to_tentative_parse (parser);
9187 /* If we have seen at least one decl-specifier, and the next token
9188 is not a parenthesis, then we must be looking at a declaration.
9189 (After "int (" we might be looking at a functional cast.) */
9190 if (decl_specifiers.any_specifiers_p
9191 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9192 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9193 && !cp_parser_error_occurred (parser))
9194 cp_parser_commit_to_tentative_parse (parser);
9196 /* Keep going until we hit the `;' at the end of the simple
9198 saw_declarator = false;
9199 while (cp_lexer_next_token_is_not (parser->lexer,
9203 bool function_definition_p;
9208 /* If we are processing next declarator, coma is expected */
9209 token = cp_lexer_peek_token (parser->lexer);
9210 gcc_assert (token->type == CPP_COMMA);
9211 cp_lexer_consume_token (parser->lexer);
9214 saw_declarator = true;
9216 /* Parse the init-declarator. */
9217 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9219 function_definition_allowed_p,
9221 declares_class_or_enum,
9222 &function_definition_p);
9223 /* If an error occurred while parsing tentatively, exit quickly.
9224 (That usually happens when in the body of a function; each
9225 statement is treated as a declaration-statement until proven
9227 if (cp_parser_error_occurred (parser))
9229 /* Handle function definitions specially. */
9230 if (function_definition_p)
9232 /* If the next token is a `,', then we are probably
9233 processing something like:
9237 which is erroneous. */
9238 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9240 cp_token *token = cp_lexer_peek_token (parser->lexer);
9241 error_at (token->location,
9243 " declarations and function-definitions is forbidden");
9245 /* Otherwise, we're done with the list of declarators. */
9248 pop_deferring_access_checks ();
9252 /* The next token should be either a `,' or a `;'. */
9253 token = cp_lexer_peek_token (parser->lexer);
9254 /* If it's a `,', there are more declarators to come. */
9255 if (token->type == CPP_COMMA)
9256 /* will be consumed next time around */;
9257 /* If it's a `;', we are done. */
9258 else if (token->type == CPP_SEMICOLON)
9260 /* Anything else is an error. */
9263 /* If we have already issued an error message we don't need
9264 to issue another one. */
9265 if (decl != error_mark_node
9266 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9267 cp_parser_error (parser, "expected %<,%> or %<;%>");
9268 /* Skip tokens until we reach the end of the statement. */
9269 cp_parser_skip_to_end_of_statement (parser);
9270 /* If the next token is now a `;', consume it. */
9271 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9272 cp_lexer_consume_token (parser->lexer);
9275 /* After the first time around, a function-definition is not
9276 allowed -- even if it was OK at first. For example:
9281 function_definition_allowed_p = false;
9284 /* Issue an error message if no declarators are present, and the
9285 decl-specifier-seq does not itself declare a class or
9287 if (!saw_declarator)
9289 if (cp_parser_declares_only_class_p (parser))
9290 shadow_tag (&decl_specifiers);
9291 /* Perform any deferred access checks. */
9292 perform_deferred_access_checks ();
9295 /* Consume the `;'. */
9296 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9299 pop_deferring_access_checks ();
9302 /* Parse a decl-specifier-seq.
9305 decl-specifier-seq [opt] decl-specifier
9308 storage-class-specifier
9319 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9321 The parser flags FLAGS is used to control type-specifier parsing.
9323 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9326 1: one of the decl-specifiers is an elaborated-type-specifier
9327 (i.e., a type declaration)
9328 2: one of the decl-specifiers is an enum-specifier or a
9329 class-specifier (i.e., a type definition)
9334 cp_parser_decl_specifier_seq (cp_parser* parser,
9335 cp_parser_flags flags,
9336 cp_decl_specifier_seq *decl_specs,
9337 int* declares_class_or_enum)
9339 bool constructor_possible_p = !parser->in_declarator_p;
9340 cp_token *start_token = NULL;
9342 /* Clear DECL_SPECS. */
9343 clear_decl_specs (decl_specs);
9345 /* Assume no class or enumeration type is declared. */
9346 *declares_class_or_enum = 0;
9348 /* Keep reading specifiers until there are no more to read. */
9352 bool found_decl_spec;
9355 /* Peek at the next token. */
9356 token = cp_lexer_peek_token (parser->lexer);
9358 /* Save the first token of the decl spec list for error
9361 start_token = token;
9362 /* Handle attributes. */
9363 if (token->keyword == RID_ATTRIBUTE)
9365 /* Parse the attributes. */
9366 decl_specs->attributes
9367 = chainon (decl_specs->attributes,
9368 cp_parser_attributes_opt (parser));
9371 /* Assume we will find a decl-specifier keyword. */
9372 found_decl_spec = true;
9373 /* If the next token is an appropriate keyword, we can simply
9374 add it to the list. */
9375 switch (token->keyword)
9381 if (!at_class_scope_p ())
9383 error_at (token->location, "%<friend%> used outside of class");
9384 cp_lexer_purge_token (parser->lexer);
9388 ++decl_specs->specs[(int) ds_friend];
9389 /* Consume the token. */
9390 cp_lexer_consume_token (parser->lexer);
9395 ++decl_specs->specs[(int) ds_constexpr];
9396 cp_lexer_consume_token (parser->lexer);
9399 /* function-specifier:
9406 cp_parser_function_specifier_opt (parser, decl_specs);
9412 ++decl_specs->specs[(int) ds_typedef];
9413 /* Consume the token. */
9414 cp_lexer_consume_token (parser->lexer);
9415 /* A constructor declarator cannot appear in a typedef. */
9416 constructor_possible_p = false;
9417 /* The "typedef" keyword can only occur in a declaration; we
9418 may as well commit at this point. */
9419 cp_parser_commit_to_tentative_parse (parser);
9421 if (decl_specs->storage_class != sc_none)
9422 decl_specs->conflicting_specifiers_p = true;
9425 /* storage-class-specifier:
9435 if (cxx_dialect == cxx98)
9437 /* Consume the token. */
9438 cp_lexer_consume_token (parser->lexer);
9440 /* Complain about `auto' as a storage specifier, if
9441 we're complaining about C++0x compatibility. */
9442 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9443 " will change meaning in C++0x; please remove it");
9445 /* Set the storage class anyway. */
9446 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9450 /* C++0x auto type-specifier. */
9451 found_decl_spec = false;
9458 /* Consume the token. */
9459 cp_lexer_consume_token (parser->lexer);
9460 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9464 /* Consume the token. */
9465 cp_lexer_consume_token (parser->lexer);
9466 ++decl_specs->specs[(int) ds_thread];
9470 /* We did not yet find a decl-specifier yet. */
9471 found_decl_spec = false;
9475 /* Constructors are a special case. The `S' in `S()' is not a
9476 decl-specifier; it is the beginning of the declarator. */
9479 && constructor_possible_p
9480 && (cp_parser_constructor_declarator_p
9481 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9483 /* If we don't have a DECL_SPEC yet, then we must be looking at
9484 a type-specifier. */
9485 if (!found_decl_spec && !constructor_p)
9487 int decl_spec_declares_class_or_enum;
9488 bool is_cv_qualifier;
9492 = cp_parser_type_specifier (parser, flags,
9494 /*is_declaration=*/true,
9495 &decl_spec_declares_class_or_enum,
9497 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9499 /* If this type-specifier referenced a user-defined type
9500 (a typedef, class-name, etc.), then we can't allow any
9501 more such type-specifiers henceforth.
9505 The longest sequence of decl-specifiers that could
9506 possibly be a type name is taken as the
9507 decl-specifier-seq of a declaration. The sequence shall
9508 be self-consistent as described below.
9512 As a general rule, at most one type-specifier is allowed
9513 in the complete decl-specifier-seq of a declaration. The
9514 only exceptions are the following:
9516 -- const or volatile can be combined with any other
9519 -- signed or unsigned can be combined with char, long,
9527 void g (const int Pc);
9529 Here, Pc is *not* part of the decl-specifier seq; it's
9530 the declarator. Therefore, once we see a type-specifier
9531 (other than a cv-qualifier), we forbid any additional
9532 user-defined types. We *do* still allow things like `int
9533 int' to be considered a decl-specifier-seq, and issue the
9534 error message later. */
9535 if (type_spec && !is_cv_qualifier)
9536 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9537 /* A constructor declarator cannot follow a type-specifier. */
9540 constructor_possible_p = false;
9541 found_decl_spec = true;
9542 if (!is_cv_qualifier)
9543 decl_specs->any_type_specifiers_p = true;
9547 /* If we still do not have a DECL_SPEC, then there are no more
9549 if (!found_decl_spec)
9552 decl_specs->any_specifiers_p = true;
9553 /* After we see one decl-specifier, further decl-specifiers are
9555 flags |= CP_PARSER_FLAGS_OPTIONAL;
9558 cp_parser_check_decl_spec (decl_specs, start_token->location);
9560 /* Don't allow a friend specifier with a class definition. */
9561 if (decl_specs->specs[(int) ds_friend] != 0
9562 && (*declares_class_or_enum & 2))
9563 error_at (start_token->location,
9564 "class definition may not be declared a friend");
9567 /* Parse an (optional) storage-class-specifier.
9569 storage-class-specifier:
9578 storage-class-specifier:
9581 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9584 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9586 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9589 if (cxx_dialect != cxx98)
9591 /* Fall through for C++98. */
9598 /* Consume the token. */
9599 return cp_lexer_consume_token (parser->lexer)->u.value;
9606 /* Parse an (optional) function-specifier.
9613 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9614 Updates DECL_SPECS, if it is non-NULL. */
9617 cp_parser_function_specifier_opt (cp_parser* parser,
9618 cp_decl_specifier_seq *decl_specs)
9620 cp_token *token = cp_lexer_peek_token (parser->lexer);
9621 switch (token->keyword)
9625 ++decl_specs->specs[(int) ds_inline];
9629 /* 14.5.2.3 [temp.mem]
9631 A member function template shall not be virtual. */
9632 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9633 error_at (token->location, "templates may not be %<virtual%>");
9634 else if (decl_specs)
9635 ++decl_specs->specs[(int) ds_virtual];
9640 ++decl_specs->specs[(int) ds_explicit];
9647 /* Consume the token. */
9648 return cp_lexer_consume_token (parser->lexer)->u.value;
9651 /* Parse a linkage-specification.
9653 linkage-specification:
9654 extern string-literal { declaration-seq [opt] }
9655 extern string-literal declaration */
9658 cp_parser_linkage_specification (cp_parser* parser)
9662 /* Look for the `extern' keyword. */
9663 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
9665 /* Look for the string-literal. */
9666 linkage = cp_parser_string_literal (parser, false, false);
9668 /* Transform the literal into an identifier. If the literal is a
9669 wide-character string, or contains embedded NULs, then we can't
9670 handle it as the user wants. */
9671 if (strlen (TREE_STRING_POINTER (linkage))
9672 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9674 cp_parser_error (parser, "invalid linkage-specification");
9675 /* Assume C++ linkage. */
9676 linkage = lang_name_cplusplus;
9679 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9681 /* We're now using the new linkage. */
9682 push_lang_context (linkage);
9684 /* If the next token is a `{', then we're using the first
9686 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9688 /* Consume the `{' token. */
9689 cp_lexer_consume_token (parser->lexer);
9690 /* Parse the declarations. */
9691 cp_parser_declaration_seq_opt (parser);
9692 /* Look for the closing `}'. */
9693 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9695 /* Otherwise, there's just one declaration. */
9698 bool saved_in_unbraced_linkage_specification_p;
9700 saved_in_unbraced_linkage_specification_p
9701 = parser->in_unbraced_linkage_specification_p;
9702 parser->in_unbraced_linkage_specification_p = true;
9703 cp_parser_declaration (parser);
9704 parser->in_unbraced_linkage_specification_p
9705 = saved_in_unbraced_linkage_specification_p;
9708 /* We're done with the linkage-specification. */
9709 pop_lang_context ();
9712 /* Parse a static_assert-declaration.
9714 static_assert-declaration:
9715 static_assert ( constant-expression , string-literal ) ;
9717 If MEMBER_P, this static_assert is a class member. */
9720 cp_parser_static_assert(cp_parser *parser, bool member_p)
9725 location_t saved_loc;
9727 /* Peek at the `static_assert' token so we can keep track of exactly
9728 where the static assertion started. */
9729 token = cp_lexer_peek_token (parser->lexer);
9730 saved_loc = token->location;
9732 /* Look for the `static_assert' keyword. */
9733 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9737 /* We know we are in a static assertion; commit to any tentative
9739 if (cp_parser_parsing_tentatively (parser))
9740 cp_parser_commit_to_tentative_parse (parser);
9742 /* Parse the `(' starting the static assertion condition. */
9743 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9745 /* Parse the constant-expression. */
9747 cp_parser_constant_expression (parser,
9748 /*allow_non_constant_p=*/false,
9749 /*non_constant_p=*/NULL);
9751 /* Parse the separating `,'. */
9752 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9754 /* Parse the string-literal message. */
9755 message = cp_parser_string_literal (parser,
9756 /*translate=*/false,
9759 /* A `)' completes the static assertion. */
9760 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9761 cp_parser_skip_to_closing_parenthesis (parser,
9762 /*recovering=*/true,
9764 /*consume_paren=*/true);
9766 /* A semicolon terminates the declaration. */
9767 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9769 /* Complete the static assertion, which may mean either processing
9770 the static assert now or saving it for template instantiation. */
9771 finish_static_assert (condition, message, saved_loc, member_p);
9774 /* Parse a `decltype' type. Returns the type.
9776 simple-type-specifier:
9777 decltype ( expression ) */
9780 cp_parser_decltype (cp_parser *parser)
9783 bool id_expression_or_member_access_p = false;
9784 const char *saved_message;
9785 bool saved_integral_constant_expression_p;
9786 bool saved_non_integral_constant_expression_p;
9787 cp_token *id_expr_start_token;
9789 /* Look for the `decltype' token. */
9790 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
9791 return error_mark_node;
9793 /* Types cannot be defined in a `decltype' expression. Save away the
9795 saved_message = parser->type_definition_forbidden_message;
9797 /* And create the new one. */
9798 parser->type_definition_forbidden_message
9799 = G_("types may not be defined in %<decltype%> expressions");
9801 /* The restrictions on constant-expressions do not apply inside
9802 decltype expressions. */
9803 saved_integral_constant_expression_p
9804 = parser->integral_constant_expression_p;
9805 saved_non_integral_constant_expression_p
9806 = parser->non_integral_constant_expression_p;
9807 parser->integral_constant_expression_p = false;
9809 /* Do not actually evaluate the expression. */
9810 ++cp_unevaluated_operand;
9812 /* Do not warn about problems with the expression. */
9813 ++c_inhibit_evaluation_warnings;
9815 /* Parse the opening `('. */
9816 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9817 return error_mark_node;
9819 /* First, try parsing an id-expression. */
9820 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9821 cp_parser_parse_tentatively (parser);
9822 expr = cp_parser_id_expression (parser,
9823 /*template_keyword_p=*/false,
9824 /*check_dependency_p=*/true,
9825 /*template_p=*/NULL,
9826 /*declarator_p=*/false,
9827 /*optional_p=*/false);
9829 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9831 bool non_integral_constant_expression_p = false;
9832 tree id_expression = expr;
9834 const char *error_msg;
9836 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9837 /* Lookup the name we got back from the id-expression. */
9838 expr = cp_parser_lookup_name (parser, expr,
9840 /*is_template=*/false,
9841 /*is_namespace=*/false,
9842 /*check_dependency=*/true,
9843 /*ambiguous_decls=*/NULL,
9844 id_expr_start_token->location);
9847 && expr != error_mark_node
9848 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9849 && TREE_CODE (expr) != TYPE_DECL
9850 && (TREE_CODE (expr) != BIT_NOT_EXPR
9851 || !TYPE_P (TREE_OPERAND (expr, 0)))
9852 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9854 /* Complete lookup of the id-expression. */
9855 expr = (finish_id_expression
9856 (id_expression, expr, parser->scope, &idk,
9857 /*integral_constant_expression_p=*/false,
9858 /*allow_non_integral_constant_expression_p=*/true,
9859 &non_integral_constant_expression_p,
9860 /*template_p=*/false,
9862 /*address_p=*/false,
9863 /*template_arg_p=*/false,
9865 id_expr_start_token->location));
9867 if (expr == error_mark_node)
9868 /* We found an id-expression, but it was something that we
9869 should not have found. This is an error, not something
9870 we can recover from, so note that we found an
9871 id-expression and we'll recover as gracefully as
9873 id_expression_or_member_access_p = true;
9877 && expr != error_mark_node
9878 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9879 /* We have an id-expression. */
9880 id_expression_or_member_access_p = true;
9883 if (!id_expression_or_member_access_p)
9885 /* Abort the id-expression parse. */
9886 cp_parser_abort_tentative_parse (parser);
9888 /* Parsing tentatively, again. */
9889 cp_parser_parse_tentatively (parser);
9891 /* Parse a class member access. */
9892 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9894 /*member_access_only_p=*/true, NULL);
9897 && expr != error_mark_node
9898 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9899 /* We have an id-expression. */
9900 id_expression_or_member_access_p = true;
9903 if (id_expression_or_member_access_p)
9904 /* We have parsed the complete id-expression or member access. */
9905 cp_parser_parse_definitely (parser);
9908 bool saved_greater_than_is_operator_p;
9910 /* Abort our attempt to parse an id-expression or member access
9912 cp_parser_abort_tentative_parse (parser);
9914 /* Within a parenthesized expression, a `>' token is always
9915 the greater-than operator. */
9916 saved_greater_than_is_operator_p
9917 = parser->greater_than_is_operator_p;
9918 parser->greater_than_is_operator_p = true;
9920 /* Parse a full expression. */
9921 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9923 /* The `>' token might be the end of a template-id or
9924 template-parameter-list now. */
9925 parser->greater_than_is_operator_p
9926 = saved_greater_than_is_operator_p;
9929 /* Go back to evaluating expressions. */
9930 --cp_unevaluated_operand;
9931 --c_inhibit_evaluation_warnings;
9933 /* Restore the old message and the integral constant expression
9935 parser->type_definition_forbidden_message = saved_message;
9936 parser->integral_constant_expression_p
9937 = saved_integral_constant_expression_p;
9938 parser->non_integral_constant_expression_p
9939 = saved_non_integral_constant_expression_p;
9941 if (expr == error_mark_node)
9943 /* Skip everything up to the closing `)'. */
9944 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9945 /*consume_paren=*/true);
9946 return error_mark_node;
9949 /* Parse to the closing `)'. */
9950 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9952 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9953 /*consume_paren=*/true);
9954 return error_mark_node;
9957 return finish_decltype_type (expr, id_expression_or_member_access_p);
9960 /* Special member functions [gram.special] */
9962 /* Parse a conversion-function-id.
9964 conversion-function-id:
9965 operator conversion-type-id
9967 Returns an IDENTIFIER_NODE representing the operator. */
9970 cp_parser_conversion_function_id (cp_parser* parser)
9974 tree saved_qualifying_scope;
9975 tree saved_object_scope;
9976 tree pushed_scope = NULL_TREE;
9978 /* Look for the `operator' token. */
9979 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
9980 return error_mark_node;
9981 /* When we parse the conversion-type-id, the current scope will be
9982 reset. However, we need that information in able to look up the
9983 conversion function later, so we save it here. */
9984 saved_scope = parser->scope;
9985 saved_qualifying_scope = parser->qualifying_scope;
9986 saved_object_scope = parser->object_scope;
9987 /* We must enter the scope of the class so that the names of
9988 entities declared within the class are available in the
9989 conversion-type-id. For example, consider:
9996 S::operator I() { ... }
9998 In order to see that `I' is a type-name in the definition, we
9999 must be in the scope of `S'. */
10001 pushed_scope = push_scope (saved_scope);
10002 /* Parse the conversion-type-id. */
10003 type = cp_parser_conversion_type_id (parser);
10004 /* Leave the scope of the class, if any. */
10006 pop_scope (pushed_scope);
10007 /* Restore the saved scope. */
10008 parser->scope = saved_scope;
10009 parser->qualifying_scope = saved_qualifying_scope;
10010 parser->object_scope = saved_object_scope;
10011 /* If the TYPE is invalid, indicate failure. */
10012 if (type == error_mark_node)
10013 return error_mark_node;
10014 return mangle_conv_op_name_for_type (type);
10017 /* Parse a conversion-type-id:
10019 conversion-type-id:
10020 type-specifier-seq conversion-declarator [opt]
10022 Returns the TYPE specified. */
10025 cp_parser_conversion_type_id (cp_parser* parser)
10028 cp_decl_specifier_seq type_specifiers;
10029 cp_declarator *declarator;
10030 tree type_specified;
10032 /* Parse the attributes. */
10033 attributes = cp_parser_attributes_opt (parser);
10034 /* Parse the type-specifiers. */
10035 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10036 /*is_trailing_return=*/false,
10038 /* If that didn't work, stop. */
10039 if (type_specifiers.type == error_mark_node)
10040 return error_mark_node;
10041 /* Parse the conversion-declarator. */
10042 declarator = cp_parser_conversion_declarator_opt (parser);
10044 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10045 /*initialized=*/0, &attributes);
10047 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10049 /* Don't give this error when parsing tentatively. This happens to
10050 work because we always parse this definitively once. */
10051 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10052 && type_uses_auto (type_specified))
10054 error ("invalid use of %<auto%> in conversion operator");
10055 return error_mark_node;
10058 return type_specified;
10061 /* Parse an (optional) conversion-declarator.
10063 conversion-declarator:
10064 ptr-operator conversion-declarator [opt]
10068 static cp_declarator *
10069 cp_parser_conversion_declarator_opt (cp_parser* parser)
10071 enum tree_code code;
10073 cp_cv_quals cv_quals;
10075 /* We don't know if there's a ptr-operator next, or not. */
10076 cp_parser_parse_tentatively (parser);
10077 /* Try the ptr-operator. */
10078 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10079 /* If it worked, look for more conversion-declarators. */
10080 if (cp_parser_parse_definitely (parser))
10082 cp_declarator *declarator;
10084 /* Parse another optional declarator. */
10085 declarator = cp_parser_conversion_declarator_opt (parser);
10087 return cp_parser_make_indirect_declarator
10088 (code, class_type, cv_quals, declarator);
10094 /* Parse an (optional) ctor-initializer.
10097 : mem-initializer-list
10099 Returns TRUE iff the ctor-initializer was actually present. */
10102 cp_parser_ctor_initializer_opt (cp_parser* parser)
10104 /* If the next token is not a `:', then there is no
10105 ctor-initializer. */
10106 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10108 /* Do default initialization of any bases and members. */
10109 if (DECL_CONSTRUCTOR_P (current_function_decl))
10110 finish_mem_initializers (NULL_TREE);
10115 /* Consume the `:' token. */
10116 cp_lexer_consume_token (parser->lexer);
10117 /* And the mem-initializer-list. */
10118 cp_parser_mem_initializer_list (parser);
10123 /* Parse a mem-initializer-list.
10125 mem-initializer-list:
10126 mem-initializer ... [opt]
10127 mem-initializer ... [opt] , mem-initializer-list */
10130 cp_parser_mem_initializer_list (cp_parser* parser)
10132 tree mem_initializer_list = NULL_TREE;
10133 cp_token *token = cp_lexer_peek_token (parser->lexer);
10135 /* Let the semantic analysis code know that we are starting the
10136 mem-initializer-list. */
10137 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10138 error_at (token->location,
10139 "only constructors take member initializers");
10141 /* Loop through the list. */
10144 tree mem_initializer;
10146 token = cp_lexer_peek_token (parser->lexer);
10147 /* Parse the mem-initializer. */
10148 mem_initializer = cp_parser_mem_initializer (parser);
10149 /* If the next token is a `...', we're expanding member initializers. */
10150 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10152 /* Consume the `...'. */
10153 cp_lexer_consume_token (parser->lexer);
10155 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10156 can be expanded but members cannot. */
10157 if (mem_initializer != error_mark_node
10158 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10160 error_at (token->location,
10161 "cannot expand initializer for member %<%D%>",
10162 TREE_PURPOSE (mem_initializer));
10163 mem_initializer = error_mark_node;
10166 /* Construct the pack expansion type. */
10167 if (mem_initializer != error_mark_node)
10168 mem_initializer = make_pack_expansion (mem_initializer);
10170 /* Add it to the list, unless it was erroneous. */
10171 if (mem_initializer != error_mark_node)
10173 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10174 mem_initializer_list = mem_initializer;
10176 /* If the next token is not a `,', we're done. */
10177 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10179 /* Consume the `,' token. */
10180 cp_lexer_consume_token (parser->lexer);
10183 /* Perform semantic analysis. */
10184 if (DECL_CONSTRUCTOR_P (current_function_decl))
10185 finish_mem_initializers (mem_initializer_list);
10188 /* Parse a mem-initializer.
10191 mem-initializer-id ( expression-list [opt] )
10192 mem-initializer-id braced-init-list
10197 ( expression-list [opt] )
10199 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10200 class) or FIELD_DECL (for a non-static data member) to initialize;
10201 the TREE_VALUE is the expression-list. An empty initialization
10202 list is represented by void_list_node. */
10205 cp_parser_mem_initializer (cp_parser* parser)
10207 tree mem_initializer_id;
10208 tree expression_list;
10210 cp_token *token = cp_lexer_peek_token (parser->lexer);
10212 /* Find out what is being initialized. */
10213 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10215 permerror (token->location,
10216 "anachronistic old-style base class initializer");
10217 mem_initializer_id = NULL_TREE;
10221 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10222 if (mem_initializer_id == error_mark_node)
10223 return mem_initializer_id;
10225 member = expand_member_init (mem_initializer_id);
10226 if (member && !DECL_P (member))
10227 in_base_initializer = 1;
10229 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10231 bool expr_non_constant_p;
10232 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10233 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10234 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10235 expression_list = build_tree_list (NULL_TREE, expression_list);
10240 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10242 /*allow_expansion_p=*/true,
10243 /*non_constant_p=*/NULL);
10245 return error_mark_node;
10246 expression_list = build_tree_list_vec (vec);
10247 release_tree_vector (vec);
10250 if (expression_list == error_mark_node)
10251 return error_mark_node;
10252 if (!expression_list)
10253 expression_list = void_type_node;
10255 in_base_initializer = 0;
10257 return member ? build_tree_list (member, expression_list) : error_mark_node;
10260 /* Parse a mem-initializer-id.
10262 mem-initializer-id:
10263 :: [opt] nested-name-specifier [opt] class-name
10266 Returns a TYPE indicating the class to be initializer for the first
10267 production. Returns an IDENTIFIER_NODE indicating the data member
10268 to be initialized for the second production. */
10271 cp_parser_mem_initializer_id (cp_parser* parser)
10273 bool global_scope_p;
10274 bool nested_name_specifier_p;
10275 bool template_p = false;
10278 cp_token *token = cp_lexer_peek_token (parser->lexer);
10280 /* `typename' is not allowed in this context ([temp.res]). */
10281 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10283 error_at (token->location,
10284 "keyword %<typename%> not allowed in this context (a qualified "
10285 "member initializer is implicitly a type)");
10286 cp_lexer_consume_token (parser->lexer);
10288 /* Look for the optional `::' operator. */
10290 = (cp_parser_global_scope_opt (parser,
10291 /*current_scope_valid_p=*/false)
10293 /* Look for the optional nested-name-specifier. The simplest way to
10298 The keyword `typename' is not permitted in a base-specifier or
10299 mem-initializer; in these contexts a qualified name that
10300 depends on a template-parameter is implicitly assumed to be a
10303 is to assume that we have seen the `typename' keyword at this
10305 nested_name_specifier_p
10306 = (cp_parser_nested_name_specifier_opt (parser,
10307 /*typename_keyword_p=*/true,
10308 /*check_dependency_p=*/true,
10310 /*is_declaration=*/true)
10312 if (nested_name_specifier_p)
10313 template_p = cp_parser_optional_template_keyword (parser);
10314 /* If there is a `::' operator or a nested-name-specifier, then we
10315 are definitely looking for a class-name. */
10316 if (global_scope_p || nested_name_specifier_p)
10317 return cp_parser_class_name (parser,
10318 /*typename_keyword_p=*/true,
10319 /*template_keyword_p=*/template_p,
10321 /*check_dependency_p=*/true,
10322 /*class_head_p=*/false,
10323 /*is_declaration=*/true);
10324 /* Otherwise, we could also be looking for an ordinary identifier. */
10325 cp_parser_parse_tentatively (parser);
10326 /* Try a class-name. */
10327 id = cp_parser_class_name (parser,
10328 /*typename_keyword_p=*/true,
10329 /*template_keyword_p=*/false,
10331 /*check_dependency_p=*/true,
10332 /*class_head_p=*/false,
10333 /*is_declaration=*/true);
10334 /* If we found one, we're done. */
10335 if (cp_parser_parse_definitely (parser))
10337 /* Otherwise, look for an ordinary identifier. */
10338 return cp_parser_identifier (parser);
10341 /* Overloading [gram.over] */
10343 /* Parse an operator-function-id.
10345 operator-function-id:
10348 Returns an IDENTIFIER_NODE for the operator which is a
10349 human-readable spelling of the identifier, e.g., `operator +'. */
10352 cp_parser_operator_function_id (cp_parser* parser)
10354 /* Look for the `operator' keyword. */
10355 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10356 return error_mark_node;
10357 /* And then the name of the operator itself. */
10358 return cp_parser_operator (parser);
10361 /* Parse an operator.
10364 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10365 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10366 || ++ -- , ->* -> () []
10373 Returns an IDENTIFIER_NODE for the operator which is a
10374 human-readable spelling of the identifier, e.g., `operator +'. */
10377 cp_parser_operator (cp_parser* parser)
10379 tree id = NULL_TREE;
10382 /* Peek at the next token. */
10383 token = cp_lexer_peek_token (parser->lexer);
10384 /* Figure out which operator we have. */
10385 switch (token->type)
10391 /* The keyword should be either `new' or `delete'. */
10392 if (token->keyword == RID_NEW)
10394 else if (token->keyword == RID_DELETE)
10399 /* Consume the `new' or `delete' token. */
10400 cp_lexer_consume_token (parser->lexer);
10402 /* Peek at the next token. */
10403 token = cp_lexer_peek_token (parser->lexer);
10404 /* If it's a `[' token then this is the array variant of the
10406 if (token->type == CPP_OPEN_SQUARE)
10408 /* Consume the `[' token. */
10409 cp_lexer_consume_token (parser->lexer);
10410 /* Look for the `]' token. */
10411 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10412 id = ansi_opname (op == NEW_EXPR
10413 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10415 /* Otherwise, we have the non-array variant. */
10417 id = ansi_opname (op);
10423 id = ansi_opname (PLUS_EXPR);
10427 id = ansi_opname (MINUS_EXPR);
10431 id = ansi_opname (MULT_EXPR);
10435 id = ansi_opname (TRUNC_DIV_EXPR);
10439 id = ansi_opname (TRUNC_MOD_EXPR);
10443 id = ansi_opname (BIT_XOR_EXPR);
10447 id = ansi_opname (BIT_AND_EXPR);
10451 id = ansi_opname (BIT_IOR_EXPR);
10455 id = ansi_opname (BIT_NOT_EXPR);
10459 id = ansi_opname (TRUTH_NOT_EXPR);
10463 id = ansi_assopname (NOP_EXPR);
10467 id = ansi_opname (LT_EXPR);
10471 id = ansi_opname (GT_EXPR);
10475 id = ansi_assopname (PLUS_EXPR);
10479 id = ansi_assopname (MINUS_EXPR);
10483 id = ansi_assopname (MULT_EXPR);
10487 id = ansi_assopname (TRUNC_DIV_EXPR);
10491 id = ansi_assopname (TRUNC_MOD_EXPR);
10495 id = ansi_assopname (BIT_XOR_EXPR);
10499 id = ansi_assopname (BIT_AND_EXPR);
10503 id = ansi_assopname (BIT_IOR_EXPR);
10507 id = ansi_opname (LSHIFT_EXPR);
10511 id = ansi_opname (RSHIFT_EXPR);
10514 case CPP_LSHIFT_EQ:
10515 id = ansi_assopname (LSHIFT_EXPR);
10518 case CPP_RSHIFT_EQ:
10519 id = ansi_assopname (RSHIFT_EXPR);
10523 id = ansi_opname (EQ_EXPR);
10527 id = ansi_opname (NE_EXPR);
10531 id = ansi_opname (LE_EXPR);
10534 case CPP_GREATER_EQ:
10535 id = ansi_opname (GE_EXPR);
10539 id = ansi_opname (TRUTH_ANDIF_EXPR);
10543 id = ansi_opname (TRUTH_ORIF_EXPR);
10546 case CPP_PLUS_PLUS:
10547 id = ansi_opname (POSTINCREMENT_EXPR);
10550 case CPP_MINUS_MINUS:
10551 id = ansi_opname (PREDECREMENT_EXPR);
10555 id = ansi_opname (COMPOUND_EXPR);
10558 case CPP_DEREF_STAR:
10559 id = ansi_opname (MEMBER_REF);
10563 id = ansi_opname (COMPONENT_REF);
10566 case CPP_OPEN_PAREN:
10567 /* Consume the `('. */
10568 cp_lexer_consume_token (parser->lexer);
10569 /* Look for the matching `)'. */
10570 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10571 return ansi_opname (CALL_EXPR);
10573 case CPP_OPEN_SQUARE:
10574 /* Consume the `['. */
10575 cp_lexer_consume_token (parser->lexer);
10576 /* Look for the matching `]'. */
10577 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10578 return ansi_opname (ARRAY_REF);
10581 /* Anything else is an error. */
10585 /* If we have selected an identifier, we need to consume the
10588 cp_lexer_consume_token (parser->lexer);
10589 /* Otherwise, no valid operator name was present. */
10592 cp_parser_error (parser, "expected operator");
10593 id = error_mark_node;
10599 /* Parse a template-declaration.
10601 template-declaration:
10602 export [opt] template < template-parameter-list > declaration
10604 If MEMBER_P is TRUE, this template-declaration occurs within a
10607 The grammar rule given by the standard isn't correct. What
10608 is really meant is:
10610 template-declaration:
10611 export [opt] template-parameter-list-seq
10612 decl-specifier-seq [opt] init-declarator [opt] ;
10613 export [opt] template-parameter-list-seq
10614 function-definition
10616 template-parameter-list-seq:
10617 template-parameter-list-seq [opt]
10618 template < template-parameter-list > */
10621 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10623 /* Check for `export'. */
10624 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10626 /* Consume the `export' token. */
10627 cp_lexer_consume_token (parser->lexer);
10628 /* Warn that we do not support `export'. */
10629 warning (0, "keyword %<export%> not implemented, and will be ignored");
10632 cp_parser_template_declaration_after_export (parser, member_p);
10635 /* Parse a template-parameter-list.
10637 template-parameter-list:
10639 template-parameter-list , template-parameter
10641 Returns a TREE_LIST. Each node represents a template parameter.
10642 The nodes are connected via their TREE_CHAINs. */
10645 cp_parser_template_parameter_list (cp_parser* parser)
10647 tree parameter_list = NULL_TREE;
10649 begin_template_parm_list ();
10654 bool is_parameter_pack;
10655 location_t parm_loc;
10657 /* Parse the template-parameter. */
10658 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10659 parameter = cp_parser_template_parameter (parser,
10661 &is_parameter_pack);
10662 /* Add it to the list. */
10663 if (parameter != error_mark_node)
10664 parameter_list = process_template_parm (parameter_list,
10668 is_parameter_pack);
10671 tree err_parm = build_tree_list (parameter, parameter);
10672 TREE_VALUE (err_parm) = error_mark_node;
10673 parameter_list = chainon (parameter_list, err_parm);
10676 /* If the next token is not a `,', we're done. */
10677 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10679 /* Otherwise, consume the `,' token. */
10680 cp_lexer_consume_token (parser->lexer);
10683 return end_template_parm_list (parameter_list);
10686 /* Parse a template-parameter.
10688 template-parameter:
10690 parameter-declaration
10692 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10693 the parameter. The TREE_PURPOSE is the default value, if any.
10694 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10695 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10696 set to true iff this parameter is a parameter pack. */
10699 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10700 bool *is_parameter_pack)
10703 cp_parameter_declarator *parameter_declarator;
10704 cp_declarator *id_declarator;
10707 /* Assume it is a type parameter or a template parameter. */
10708 *is_non_type = false;
10709 /* Assume it not a parameter pack. */
10710 *is_parameter_pack = false;
10711 /* Peek at the next token. */
10712 token = cp_lexer_peek_token (parser->lexer);
10713 /* If it is `class' or `template', we have a type-parameter. */
10714 if (token->keyword == RID_TEMPLATE)
10715 return cp_parser_type_parameter (parser, is_parameter_pack);
10716 /* If it is `class' or `typename' we do not know yet whether it is a
10717 type parameter or a non-type parameter. Consider:
10719 template <typename T, typename T::X X> ...
10723 template <class C, class D*> ...
10725 Here, the first parameter is a type parameter, and the second is
10726 a non-type parameter. We can tell by looking at the token after
10727 the identifier -- if it is a `,', `=', or `>' then we have a type
10729 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10731 /* Peek at the token after `class' or `typename'. */
10732 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10733 /* If it's an ellipsis, we have a template type parameter
10735 if (token->type == CPP_ELLIPSIS)
10736 return cp_parser_type_parameter (parser, is_parameter_pack);
10737 /* If it's an identifier, skip it. */
10738 if (token->type == CPP_NAME)
10739 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10740 /* Now, see if the token looks like the end of a template
10742 if (token->type == CPP_COMMA
10743 || token->type == CPP_EQ
10744 || token->type == CPP_GREATER)
10745 return cp_parser_type_parameter (parser, is_parameter_pack);
10748 /* Otherwise, it is a non-type parameter.
10752 When parsing a default template-argument for a non-type
10753 template-parameter, the first non-nested `>' is taken as the end
10754 of the template parameter-list rather than a greater-than
10756 *is_non_type = true;
10757 parameter_declarator
10758 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10759 /*parenthesized_p=*/NULL);
10761 /* If the parameter declaration is marked as a parameter pack, set
10762 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10763 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10765 if (parameter_declarator
10766 && parameter_declarator->declarator
10767 && parameter_declarator->declarator->parameter_pack_p)
10769 *is_parameter_pack = true;
10770 parameter_declarator->declarator->parameter_pack_p = false;
10773 /* If the next token is an ellipsis, and we don't already have it
10774 marked as a parameter pack, then we have a parameter pack (that
10775 has no declarator). */
10776 if (!*is_parameter_pack
10777 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10778 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10780 /* Consume the `...'. */
10781 cp_lexer_consume_token (parser->lexer);
10782 maybe_warn_variadic_templates ();
10784 *is_parameter_pack = true;
10786 /* We might end up with a pack expansion as the type of the non-type
10787 template parameter, in which case this is a non-type template
10789 else if (parameter_declarator
10790 && parameter_declarator->decl_specifiers.type
10791 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10793 *is_parameter_pack = true;
10794 parameter_declarator->decl_specifiers.type =
10795 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10798 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10800 /* Parameter packs cannot have default arguments. However, a
10801 user may try to do so, so we'll parse them and give an
10802 appropriate diagnostic here. */
10804 /* Consume the `='. */
10805 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10806 cp_lexer_consume_token (parser->lexer);
10808 /* Find the name of the parameter pack. */
10809 id_declarator = parameter_declarator->declarator;
10810 while (id_declarator && id_declarator->kind != cdk_id)
10811 id_declarator = id_declarator->declarator;
10813 if (id_declarator && id_declarator->kind == cdk_id)
10814 error_at (start_token->location,
10815 "template parameter pack %qD cannot have a default argument",
10816 id_declarator->u.id.unqualified_name);
10818 error_at (start_token->location,
10819 "template parameter pack cannot have a default argument");
10821 /* Parse the default argument, but throw away the result. */
10822 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10825 parm = grokdeclarator (parameter_declarator->declarator,
10826 ¶meter_declarator->decl_specifiers,
10827 TPARM, /*initialized=*/0,
10828 /*attrlist=*/NULL);
10829 if (parm == error_mark_node)
10830 return error_mark_node;
10832 return build_tree_list (parameter_declarator->default_argument, parm);
10835 /* Parse a type-parameter.
10838 class identifier [opt]
10839 class identifier [opt] = type-id
10840 typename identifier [opt]
10841 typename identifier [opt] = type-id
10842 template < template-parameter-list > class identifier [opt]
10843 template < template-parameter-list > class identifier [opt]
10846 GNU Extension (variadic templates):
10849 class ... identifier [opt]
10850 typename ... identifier [opt]
10852 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10853 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10854 the declaration of the parameter.
10856 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10859 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10864 /* Look for a keyword to tell us what kind of parameter this is. */
10865 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
10867 return error_mark_node;
10869 switch (token->keyword)
10875 tree default_argument;
10877 /* If the next token is an ellipsis, we have a template
10879 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10881 /* Consume the `...' token. */
10882 cp_lexer_consume_token (parser->lexer);
10883 maybe_warn_variadic_templates ();
10885 *is_parameter_pack = true;
10888 /* If the next token is an identifier, then it names the
10890 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10891 identifier = cp_parser_identifier (parser);
10893 identifier = NULL_TREE;
10895 /* Create the parameter. */
10896 parameter = finish_template_type_parm (class_type_node, identifier);
10898 /* If the next token is an `=', we have a default argument. */
10899 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10901 /* Consume the `=' token. */
10902 cp_lexer_consume_token (parser->lexer);
10903 /* Parse the default-argument. */
10904 push_deferring_access_checks (dk_no_deferred);
10905 default_argument = cp_parser_type_id (parser);
10907 /* Template parameter packs cannot have default
10909 if (*is_parameter_pack)
10912 error_at (token->location,
10913 "template parameter pack %qD cannot have a "
10914 "default argument", identifier);
10916 error_at (token->location,
10917 "template parameter packs cannot have "
10918 "default arguments");
10919 default_argument = NULL_TREE;
10921 pop_deferring_access_checks ();
10924 default_argument = NULL_TREE;
10926 /* Create the combined representation of the parameter and the
10927 default argument. */
10928 parameter = build_tree_list (default_argument, parameter);
10935 tree default_argument;
10937 /* Look for the `<'. */
10938 cp_parser_require (parser, CPP_LESS, RT_LESS);
10939 /* Parse the template-parameter-list. */
10940 cp_parser_template_parameter_list (parser);
10941 /* Look for the `>'. */
10942 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
10943 /* Look for the `class' keyword. */
10944 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
10945 /* If the next token is an ellipsis, we have a template
10947 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10949 /* Consume the `...' token. */
10950 cp_lexer_consume_token (parser->lexer);
10951 maybe_warn_variadic_templates ();
10953 *is_parameter_pack = true;
10955 /* If the next token is an `=', then there is a
10956 default-argument. If the next token is a `>', we are at
10957 the end of the parameter-list. If the next token is a `,',
10958 then we are at the end of this parameter. */
10959 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10960 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10961 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10963 identifier = cp_parser_identifier (parser);
10964 /* Treat invalid names as if the parameter were nameless. */
10965 if (identifier == error_mark_node)
10966 identifier = NULL_TREE;
10969 identifier = NULL_TREE;
10971 /* Create the template parameter. */
10972 parameter = finish_template_template_parm (class_type_node,
10975 /* If the next token is an `=', then there is a
10976 default-argument. */
10977 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10981 /* Consume the `='. */
10982 cp_lexer_consume_token (parser->lexer);
10983 /* Parse the id-expression. */
10984 push_deferring_access_checks (dk_no_deferred);
10985 /* save token before parsing the id-expression, for error
10987 token = cp_lexer_peek_token (parser->lexer);
10989 = cp_parser_id_expression (parser,
10990 /*template_keyword_p=*/false,
10991 /*check_dependency_p=*/true,
10992 /*template_p=*/&is_template,
10993 /*declarator_p=*/false,
10994 /*optional_p=*/false);
10995 if (TREE_CODE (default_argument) == TYPE_DECL)
10996 /* If the id-expression was a template-id that refers to
10997 a template-class, we already have the declaration here,
10998 so no further lookup is needed. */
11001 /* Look up the name. */
11003 = cp_parser_lookup_name (parser, default_argument,
11005 /*is_template=*/is_template,
11006 /*is_namespace=*/false,
11007 /*check_dependency=*/true,
11008 /*ambiguous_decls=*/NULL,
11010 /* See if the default argument is valid. */
11012 = check_template_template_default_arg (default_argument);
11014 /* Template parameter packs cannot have default
11016 if (*is_parameter_pack)
11019 error_at (token->location,
11020 "template parameter pack %qD cannot "
11021 "have a default argument",
11024 error_at (token->location, "template parameter packs cannot "
11025 "have default arguments");
11026 default_argument = NULL_TREE;
11028 pop_deferring_access_checks ();
11031 default_argument = NULL_TREE;
11033 /* Create the combined representation of the parameter and the
11034 default argument. */
11035 parameter = build_tree_list (default_argument, parameter);
11040 gcc_unreachable ();
11047 /* Parse a template-id.
11050 template-name < template-argument-list [opt] >
11052 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11053 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11054 returned. Otherwise, if the template-name names a function, or set
11055 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11056 names a class, returns a TYPE_DECL for the specialization.
11058 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11059 uninstantiated templates. */
11062 cp_parser_template_id (cp_parser *parser,
11063 bool template_keyword_p,
11064 bool check_dependency_p,
11065 bool is_declaration)
11071 cp_token_position start_of_id = 0;
11072 deferred_access_check *chk;
11073 VEC (deferred_access_check,gc) *access_check;
11074 cp_token *next_token = NULL, *next_token_2 = NULL;
11075 bool is_identifier;
11077 /* If the next token corresponds to a template-id, there is no need
11079 next_token = cp_lexer_peek_token (parser->lexer);
11080 if (next_token->type == CPP_TEMPLATE_ID)
11082 struct tree_check *check_value;
11084 /* Get the stored value. */
11085 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11086 /* Perform any access checks that were deferred. */
11087 access_check = check_value->checks;
11091 VEC_iterate (deferred_access_check, access_check, i, chk) ;
11094 perform_or_defer_access_check (chk->binfo,
11099 /* Return the stored value. */
11100 return check_value->value;
11103 /* Avoid performing name lookup if there is no possibility of
11104 finding a template-id. */
11105 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11106 || (next_token->type == CPP_NAME
11107 && !cp_parser_nth_token_starts_template_argument_list_p
11110 cp_parser_error (parser, "expected template-id");
11111 return error_mark_node;
11114 /* Remember where the template-id starts. */
11115 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11116 start_of_id = cp_lexer_token_position (parser->lexer, false);
11118 push_deferring_access_checks (dk_deferred);
11120 /* Parse the template-name. */
11121 is_identifier = false;
11122 templ = cp_parser_template_name (parser, template_keyword_p,
11123 check_dependency_p,
11126 if (templ == error_mark_node || is_identifier)
11128 pop_deferring_access_checks ();
11132 /* If we find the sequence `[:' after a template-name, it's probably
11133 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11134 parse correctly the argument list. */
11135 next_token = cp_lexer_peek_token (parser->lexer);
11136 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11137 if (next_token->type == CPP_OPEN_SQUARE
11138 && next_token->flags & DIGRAPH
11139 && next_token_2->type == CPP_COLON
11140 && !(next_token_2->flags & PREV_WHITE))
11142 cp_parser_parse_tentatively (parser);
11143 /* Change `:' into `::'. */
11144 next_token_2->type = CPP_SCOPE;
11145 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11147 cp_lexer_consume_token (parser->lexer);
11149 /* Parse the arguments. */
11150 arguments = cp_parser_enclosed_template_argument_list (parser);
11151 if (!cp_parser_parse_definitely (parser))
11153 /* If we couldn't parse an argument list, then we revert our changes
11154 and return simply an error. Maybe this is not a template-id
11156 next_token_2->type = CPP_COLON;
11157 cp_parser_error (parser, "expected %<<%>");
11158 pop_deferring_access_checks ();
11159 return error_mark_node;
11161 /* Otherwise, emit an error about the invalid digraph, but continue
11162 parsing because we got our argument list. */
11163 if (permerror (next_token->location,
11164 "%<<::%> cannot begin a template-argument list"))
11166 static bool hint = false;
11167 inform (next_token->location,
11168 "%<<:%> is an alternate spelling for %<[%>."
11169 " Insert whitespace between %<<%> and %<::%>");
11170 if (!hint && !flag_permissive)
11172 inform (next_token->location, "(if you use %<-fpermissive%>"
11173 " G++ will accept your code)");
11180 /* Look for the `<' that starts the template-argument-list. */
11181 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11183 pop_deferring_access_checks ();
11184 return error_mark_node;
11186 /* Parse the arguments. */
11187 arguments = cp_parser_enclosed_template_argument_list (parser);
11190 /* Build a representation of the specialization. */
11191 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11192 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11193 else if (DECL_CLASS_TEMPLATE_P (templ)
11194 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11196 bool entering_scope;
11197 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11198 template (rather than some instantiation thereof) only if
11199 is not nested within some other construct. For example, in
11200 "template <typename T> void f(T) { A<T>::", A<T> is just an
11201 instantiation of A. */
11202 entering_scope = (template_parm_scope_p ()
11203 && cp_lexer_next_token_is (parser->lexer,
11206 = finish_template_type (templ, arguments, entering_scope);
11210 /* If it's not a class-template or a template-template, it should be
11211 a function-template. */
11212 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11213 || TREE_CODE (templ) == OVERLOAD
11214 || BASELINK_P (templ)));
11216 template_id = lookup_template_function (templ, arguments);
11219 /* If parsing tentatively, replace the sequence of tokens that makes
11220 up the template-id with a CPP_TEMPLATE_ID token. That way,
11221 should we re-parse the token stream, we will not have to repeat
11222 the effort required to do the parse, nor will we issue duplicate
11223 error messages about problems during instantiation of the
11227 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11229 /* Reset the contents of the START_OF_ID token. */
11230 token->type = CPP_TEMPLATE_ID;
11231 /* Retrieve any deferred checks. Do not pop this access checks yet
11232 so the memory will not be reclaimed during token replacing below. */
11233 token->u.tree_check_value = GGC_CNEW (struct tree_check);
11234 token->u.tree_check_value->value = template_id;
11235 token->u.tree_check_value->checks = get_deferred_access_checks ();
11236 token->keyword = RID_MAX;
11238 /* Purge all subsequent tokens. */
11239 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11241 /* ??? Can we actually assume that, if template_id ==
11242 error_mark_node, we will have issued a diagnostic to the
11243 user, as opposed to simply marking the tentative parse as
11245 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11246 error_at (token->location, "parse error in template argument list");
11249 pop_deferring_access_checks ();
11250 return template_id;
11253 /* Parse a template-name.
11258 The standard should actually say:
11262 operator-function-id
11264 A defect report has been filed about this issue.
11266 A conversion-function-id cannot be a template name because they cannot
11267 be part of a template-id. In fact, looking at this code:
11269 a.operator K<int>()
11271 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11272 It is impossible to call a templated conversion-function-id with an
11273 explicit argument list, since the only allowed template parameter is
11274 the type to which it is converting.
11276 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11277 `template' keyword, in a construction like:
11281 In that case `f' is taken to be a template-name, even though there
11282 is no way of knowing for sure.
11284 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11285 name refers to a set of overloaded functions, at least one of which
11286 is a template, or an IDENTIFIER_NODE with the name of the template,
11287 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11288 names are looked up inside uninstantiated templates. */
11291 cp_parser_template_name (cp_parser* parser,
11292 bool template_keyword_p,
11293 bool check_dependency_p,
11294 bool is_declaration,
11295 bool *is_identifier)
11300 cp_token *token = cp_lexer_peek_token (parser->lexer);
11302 /* If the next token is `operator', then we have either an
11303 operator-function-id or a conversion-function-id. */
11304 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11306 /* We don't know whether we're looking at an
11307 operator-function-id or a conversion-function-id. */
11308 cp_parser_parse_tentatively (parser);
11309 /* Try an operator-function-id. */
11310 identifier = cp_parser_operator_function_id (parser);
11311 /* If that didn't work, try a conversion-function-id. */
11312 if (!cp_parser_parse_definitely (parser))
11314 cp_parser_error (parser, "expected template-name");
11315 return error_mark_node;
11318 /* Look for the identifier. */
11320 identifier = cp_parser_identifier (parser);
11322 /* If we didn't find an identifier, we don't have a template-id. */
11323 if (identifier == error_mark_node)
11324 return error_mark_node;
11326 /* If the name immediately followed the `template' keyword, then it
11327 is a template-name. However, if the next token is not `<', then
11328 we do not treat it as a template-name, since it is not being used
11329 as part of a template-id. This enables us to handle constructs
11332 template <typename T> struct S { S(); };
11333 template <typename T> S<T>::S();
11335 correctly. We would treat `S' as a template -- if it were `S<T>'
11336 -- but we do not if there is no `<'. */
11338 if (processing_template_decl
11339 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11341 /* In a declaration, in a dependent context, we pretend that the
11342 "template" keyword was present in order to improve error
11343 recovery. For example, given:
11345 template <typename T> void f(T::X<int>);
11347 we want to treat "X<int>" as a template-id. */
11349 && !template_keyword_p
11350 && parser->scope && TYPE_P (parser->scope)
11351 && check_dependency_p
11352 && dependent_scope_p (parser->scope)
11353 /* Do not do this for dtors (or ctors), since they never
11354 need the template keyword before their name. */
11355 && !constructor_name_p (identifier, parser->scope))
11357 cp_token_position start = 0;
11359 /* Explain what went wrong. */
11360 error_at (token->location, "non-template %qD used as template",
11362 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11363 parser->scope, identifier);
11364 /* If parsing tentatively, find the location of the "<" token. */
11365 if (cp_parser_simulate_error (parser))
11366 start = cp_lexer_token_position (parser->lexer, true);
11367 /* Parse the template arguments so that we can issue error
11368 messages about them. */
11369 cp_lexer_consume_token (parser->lexer);
11370 cp_parser_enclosed_template_argument_list (parser);
11371 /* Skip tokens until we find a good place from which to
11372 continue parsing. */
11373 cp_parser_skip_to_closing_parenthesis (parser,
11374 /*recovering=*/true,
11376 /*consume_paren=*/false);
11377 /* If parsing tentatively, permanently remove the
11378 template argument list. That will prevent duplicate
11379 error messages from being issued about the missing
11380 "template" keyword. */
11382 cp_lexer_purge_tokens_after (parser->lexer, start);
11384 *is_identifier = true;
11388 /* If the "template" keyword is present, then there is generally
11389 no point in doing name-lookup, so we just return IDENTIFIER.
11390 But, if the qualifying scope is non-dependent then we can
11391 (and must) do name-lookup normally. */
11392 if (template_keyword_p
11394 || (TYPE_P (parser->scope)
11395 && dependent_type_p (parser->scope))))
11399 /* Look up the name. */
11400 decl = cp_parser_lookup_name (parser, identifier,
11402 /*is_template=*/true,
11403 /*is_namespace=*/false,
11404 check_dependency_p,
11405 /*ambiguous_decls=*/NULL,
11408 /* If DECL is a template, then the name was a template-name. */
11409 if (TREE_CODE (decl) == TEMPLATE_DECL)
11413 tree fn = NULL_TREE;
11415 /* The standard does not explicitly indicate whether a name that
11416 names a set of overloaded declarations, some of which are
11417 templates, is a template-name. However, such a name should
11418 be a template-name; otherwise, there is no way to form a
11419 template-id for the overloaded templates. */
11420 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11421 if (TREE_CODE (fns) == OVERLOAD)
11422 for (fn = fns; fn; fn = OVL_NEXT (fn))
11423 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11428 /* The name does not name a template. */
11429 cp_parser_error (parser, "expected template-name");
11430 return error_mark_node;
11434 /* If DECL is dependent, and refers to a function, then just return
11435 its name; we will look it up again during template instantiation. */
11436 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11438 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11439 if (TYPE_P (scope) && dependent_type_p (scope))
11446 /* Parse a template-argument-list.
11448 template-argument-list:
11449 template-argument ... [opt]
11450 template-argument-list , template-argument ... [opt]
11452 Returns a TREE_VEC containing the arguments. */
11455 cp_parser_template_argument_list (cp_parser* parser)
11457 tree fixed_args[10];
11458 unsigned n_args = 0;
11459 unsigned alloced = 10;
11460 tree *arg_ary = fixed_args;
11462 bool saved_in_template_argument_list_p;
11464 bool saved_non_ice_p;
11466 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11467 parser->in_template_argument_list_p = true;
11468 /* Even if the template-id appears in an integral
11469 constant-expression, the contents of the argument list do
11471 saved_ice_p = parser->integral_constant_expression_p;
11472 parser->integral_constant_expression_p = false;
11473 saved_non_ice_p = parser->non_integral_constant_expression_p;
11474 parser->non_integral_constant_expression_p = false;
11475 /* Parse the arguments. */
11481 /* Consume the comma. */
11482 cp_lexer_consume_token (parser->lexer);
11484 /* Parse the template-argument. */
11485 argument = cp_parser_template_argument (parser);
11487 /* If the next token is an ellipsis, we're expanding a template
11489 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11491 if (argument == error_mark_node)
11493 cp_token *token = cp_lexer_peek_token (parser->lexer);
11494 error_at (token->location,
11495 "expected parameter pack before %<...%>");
11497 /* Consume the `...' token. */
11498 cp_lexer_consume_token (parser->lexer);
11500 /* Make the argument into a TYPE_PACK_EXPANSION or
11501 EXPR_PACK_EXPANSION. */
11502 argument = make_pack_expansion (argument);
11505 if (n_args == alloced)
11509 if (arg_ary == fixed_args)
11511 arg_ary = XNEWVEC (tree, alloced);
11512 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11515 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11517 arg_ary[n_args++] = argument;
11519 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11521 vec = make_tree_vec (n_args);
11524 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11526 if (arg_ary != fixed_args)
11528 parser->non_integral_constant_expression_p = saved_non_ice_p;
11529 parser->integral_constant_expression_p = saved_ice_p;
11530 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11531 #ifdef ENABLE_CHECKING
11532 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11537 /* Parse a template-argument.
11540 assignment-expression
11544 The representation is that of an assignment-expression, type-id, or
11545 id-expression -- except that the qualified id-expression is
11546 evaluated, so that the value returned is either a DECL or an
11549 Although the standard says "assignment-expression", it forbids
11550 throw-expressions or assignments in the template argument.
11551 Therefore, we use "conditional-expression" instead. */
11554 cp_parser_template_argument (cp_parser* parser)
11559 bool maybe_type_id = false;
11560 cp_token *token = NULL, *argument_start_token = NULL;
11563 /* There's really no way to know what we're looking at, so we just
11564 try each alternative in order.
11568 In a template-argument, an ambiguity between a type-id and an
11569 expression is resolved to a type-id, regardless of the form of
11570 the corresponding template-parameter.
11572 Therefore, we try a type-id first. */
11573 cp_parser_parse_tentatively (parser);
11574 argument = cp_parser_template_type_arg (parser);
11575 /* If there was no error parsing the type-id but the next token is a
11576 '>>', our behavior depends on which dialect of C++ we're
11577 parsing. In C++98, we probably found a typo for '> >'. But there
11578 are type-id which are also valid expressions. For instance:
11580 struct X { int operator >> (int); };
11581 template <int V> struct Foo {};
11584 Here 'X()' is a valid type-id of a function type, but the user just
11585 wanted to write the expression "X() >> 5". Thus, we remember that we
11586 found a valid type-id, but we still try to parse the argument as an
11587 expression to see what happens.
11589 In C++0x, the '>>' will be considered two separate '>'
11591 if (!cp_parser_error_occurred (parser)
11592 && cxx_dialect == cxx98
11593 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11595 maybe_type_id = true;
11596 cp_parser_abort_tentative_parse (parser);
11600 /* If the next token isn't a `,' or a `>', then this argument wasn't
11601 really finished. This means that the argument is not a valid
11603 if (!cp_parser_next_token_ends_template_argument_p (parser))
11604 cp_parser_error (parser, "expected template-argument");
11605 /* If that worked, we're done. */
11606 if (cp_parser_parse_definitely (parser))
11609 /* We're still not sure what the argument will be. */
11610 cp_parser_parse_tentatively (parser);
11611 /* Try a template. */
11612 argument_start_token = cp_lexer_peek_token (parser->lexer);
11613 argument = cp_parser_id_expression (parser,
11614 /*template_keyword_p=*/false,
11615 /*check_dependency_p=*/true,
11617 /*declarator_p=*/false,
11618 /*optional_p=*/false);
11619 /* If the next token isn't a `,' or a `>', then this argument wasn't
11620 really finished. */
11621 if (!cp_parser_next_token_ends_template_argument_p (parser))
11622 cp_parser_error (parser, "expected template-argument");
11623 if (!cp_parser_error_occurred (parser))
11625 /* Figure out what is being referred to. If the id-expression
11626 was for a class template specialization, then we will have a
11627 TYPE_DECL at this point. There is no need to do name lookup
11628 at this point in that case. */
11629 if (TREE_CODE (argument) != TYPE_DECL)
11630 argument = cp_parser_lookup_name (parser, argument,
11632 /*is_template=*/template_p,
11633 /*is_namespace=*/false,
11634 /*check_dependency=*/true,
11635 /*ambiguous_decls=*/NULL,
11636 argument_start_token->location);
11637 if (TREE_CODE (argument) != TEMPLATE_DECL
11638 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11639 cp_parser_error (parser, "expected template-name");
11641 if (cp_parser_parse_definitely (parser))
11643 /* It must be a non-type argument. There permitted cases are given
11644 in [temp.arg.nontype]:
11646 -- an integral constant-expression of integral or enumeration
11649 -- the name of a non-type template-parameter; or
11651 -- the name of an object or function with external linkage...
11653 -- the address of an object or function with external linkage...
11655 -- a pointer to member... */
11656 /* Look for a non-type template parameter. */
11657 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11659 cp_parser_parse_tentatively (parser);
11660 argument = cp_parser_primary_expression (parser,
11661 /*address_p=*/false,
11663 /*template_arg_p=*/true,
11665 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11666 || !cp_parser_next_token_ends_template_argument_p (parser))
11667 cp_parser_simulate_error (parser);
11668 if (cp_parser_parse_definitely (parser))
11672 /* If the next token is "&", the argument must be the address of an
11673 object or function with external linkage. */
11674 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11676 cp_lexer_consume_token (parser->lexer);
11677 /* See if we might have an id-expression. */
11678 token = cp_lexer_peek_token (parser->lexer);
11679 if (token->type == CPP_NAME
11680 || token->keyword == RID_OPERATOR
11681 || token->type == CPP_SCOPE
11682 || token->type == CPP_TEMPLATE_ID
11683 || token->type == CPP_NESTED_NAME_SPECIFIER)
11685 cp_parser_parse_tentatively (parser);
11686 argument = cp_parser_primary_expression (parser,
11689 /*template_arg_p=*/true,
11691 if (cp_parser_error_occurred (parser)
11692 || !cp_parser_next_token_ends_template_argument_p (parser))
11693 cp_parser_abort_tentative_parse (parser);
11698 if (TREE_CODE (argument) == INDIRECT_REF)
11700 gcc_assert (REFERENCE_REF_P (argument));
11701 argument = TREE_OPERAND (argument, 0);
11704 /* If we're in a template, we represent a qualified-id referring
11705 to a static data member as a SCOPE_REF even if the scope isn't
11706 dependent so that we can check access control later. */
11708 if (TREE_CODE (probe) == SCOPE_REF)
11709 probe = TREE_OPERAND (probe, 1);
11710 if (TREE_CODE (probe) == VAR_DECL)
11712 /* A variable without external linkage might still be a
11713 valid constant-expression, so no error is issued here
11714 if the external-linkage check fails. */
11715 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11716 cp_parser_simulate_error (parser);
11718 else if (is_overloaded_fn (argument))
11719 /* All overloaded functions are allowed; if the external
11720 linkage test does not pass, an error will be issued
11724 && (TREE_CODE (argument) == OFFSET_REF
11725 || TREE_CODE (argument) == SCOPE_REF))
11726 /* A pointer-to-member. */
11728 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11731 cp_parser_simulate_error (parser);
11733 if (cp_parser_parse_definitely (parser))
11736 argument = build_x_unary_op (ADDR_EXPR, argument,
11737 tf_warning_or_error);
11742 /* If the argument started with "&", there are no other valid
11743 alternatives at this point. */
11746 cp_parser_error (parser, "invalid non-type template argument");
11747 return error_mark_node;
11750 /* If the argument wasn't successfully parsed as a type-id followed
11751 by '>>', the argument can only be a constant expression now.
11752 Otherwise, we try parsing the constant-expression tentatively,
11753 because the argument could really be a type-id. */
11755 cp_parser_parse_tentatively (parser);
11756 argument = cp_parser_constant_expression (parser,
11757 /*allow_non_constant_p=*/false,
11758 /*non_constant_p=*/NULL);
11759 argument = fold_non_dependent_expr (argument);
11760 if (!maybe_type_id)
11762 if (!cp_parser_next_token_ends_template_argument_p (parser))
11763 cp_parser_error (parser, "expected template-argument");
11764 if (cp_parser_parse_definitely (parser))
11766 /* We did our best to parse the argument as a non type-id, but that
11767 was the only alternative that matched (albeit with a '>' after
11768 it). We can assume it's just a typo from the user, and a
11769 diagnostic will then be issued. */
11770 return cp_parser_template_type_arg (parser);
11773 /* Parse an explicit-instantiation.
11775 explicit-instantiation:
11776 template declaration
11778 Although the standard says `declaration', what it really means is:
11780 explicit-instantiation:
11781 template decl-specifier-seq [opt] declarator [opt] ;
11783 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11784 supposed to be allowed. A defect report has been filed about this
11789 explicit-instantiation:
11790 storage-class-specifier template
11791 decl-specifier-seq [opt] declarator [opt] ;
11792 function-specifier template
11793 decl-specifier-seq [opt] declarator [opt] ; */
11796 cp_parser_explicit_instantiation (cp_parser* parser)
11798 int declares_class_or_enum;
11799 cp_decl_specifier_seq decl_specifiers;
11800 tree extension_specifier = NULL_TREE;
11802 /* Look for an (optional) storage-class-specifier or
11803 function-specifier. */
11804 if (cp_parser_allow_gnu_extensions_p (parser))
11806 extension_specifier
11807 = cp_parser_storage_class_specifier_opt (parser);
11808 if (!extension_specifier)
11809 extension_specifier
11810 = cp_parser_function_specifier_opt (parser,
11811 /*decl_specs=*/NULL);
11814 /* Look for the `template' keyword. */
11815 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
11816 /* Let the front end know that we are processing an explicit
11818 begin_explicit_instantiation ();
11819 /* [temp.explicit] says that we are supposed to ignore access
11820 control while processing explicit instantiation directives. */
11821 push_deferring_access_checks (dk_no_check);
11822 /* Parse a decl-specifier-seq. */
11823 cp_parser_decl_specifier_seq (parser,
11824 CP_PARSER_FLAGS_OPTIONAL,
11826 &declares_class_or_enum);
11827 /* If there was exactly one decl-specifier, and it declared a class,
11828 and there's no declarator, then we have an explicit type
11830 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11834 type = check_tag_decl (&decl_specifiers);
11835 /* Turn access control back on for names used during
11836 template instantiation. */
11837 pop_deferring_access_checks ();
11839 do_type_instantiation (type, extension_specifier,
11840 /*complain=*/tf_error);
11844 cp_declarator *declarator;
11847 /* Parse the declarator. */
11849 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11850 /*ctor_dtor_or_conv_p=*/NULL,
11851 /*parenthesized_p=*/NULL,
11852 /*member_p=*/false);
11853 if (declares_class_or_enum & 2)
11854 cp_parser_check_for_definition_in_return_type (declarator,
11855 decl_specifiers.type,
11856 decl_specifiers.type_location);
11857 if (declarator != cp_error_declarator)
11859 decl = grokdeclarator (declarator, &decl_specifiers,
11860 NORMAL, 0, &decl_specifiers.attributes);
11861 /* Turn access control back on for names used during
11862 template instantiation. */
11863 pop_deferring_access_checks ();
11864 /* Do the explicit instantiation. */
11865 do_decl_instantiation (decl, extension_specifier);
11869 pop_deferring_access_checks ();
11870 /* Skip the body of the explicit instantiation. */
11871 cp_parser_skip_to_end_of_statement (parser);
11874 /* We're done with the instantiation. */
11875 end_explicit_instantiation ();
11877 cp_parser_consume_semicolon_at_end_of_statement (parser);
11880 /* Parse an explicit-specialization.
11882 explicit-specialization:
11883 template < > declaration
11885 Although the standard says `declaration', what it really means is:
11887 explicit-specialization:
11888 template <> decl-specifier [opt] init-declarator [opt] ;
11889 template <> function-definition
11890 template <> explicit-specialization
11891 template <> template-declaration */
11894 cp_parser_explicit_specialization (cp_parser* parser)
11896 bool need_lang_pop;
11897 cp_token *token = cp_lexer_peek_token (parser->lexer);
11899 /* Look for the `template' keyword. */
11900 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
11901 /* Look for the `<'. */
11902 cp_parser_require (parser, CPP_LESS, RT_LESS);
11903 /* Look for the `>'. */
11904 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11905 /* We have processed another parameter list. */
11906 ++parser->num_template_parameter_lists;
11909 A template ... explicit specialization ... shall not have C
11911 if (current_lang_name == lang_name_c)
11913 error_at (token->location, "template specialization with C linkage");
11914 /* Give it C++ linkage to avoid confusing other parts of the
11916 push_lang_context (lang_name_cplusplus);
11917 need_lang_pop = true;
11920 need_lang_pop = false;
11921 /* Let the front end know that we are beginning a specialization. */
11922 if (!begin_specialization ())
11924 end_specialization ();
11928 /* If the next keyword is `template', we need to figure out whether
11929 or not we're looking a template-declaration. */
11930 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11932 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11933 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11934 cp_parser_template_declaration_after_export (parser,
11935 /*member_p=*/false);
11937 cp_parser_explicit_specialization (parser);
11940 /* Parse the dependent declaration. */
11941 cp_parser_single_declaration (parser,
11943 /*member_p=*/false,
11944 /*explicit_specialization_p=*/true,
11945 /*friend_p=*/NULL);
11946 /* We're done with the specialization. */
11947 end_specialization ();
11948 /* For the erroneous case of a template with C linkage, we pushed an
11949 implicit C++ linkage scope; exit that scope now. */
11951 pop_lang_context ();
11952 /* We're done with this parameter list. */
11953 --parser->num_template_parameter_lists;
11956 /* Parse a type-specifier.
11959 simple-type-specifier
11962 elaborated-type-specifier
11970 Returns a representation of the type-specifier. For a
11971 class-specifier, enum-specifier, or elaborated-type-specifier, a
11972 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11974 The parser flags FLAGS is used to control type-specifier parsing.
11976 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11977 in a decl-specifier-seq.
11979 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11980 class-specifier, enum-specifier, or elaborated-type-specifier, then
11981 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11982 if a type is declared; 2 if it is defined. Otherwise, it is set to
11985 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11986 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11987 is set to FALSE. */
11990 cp_parser_type_specifier (cp_parser* parser,
11991 cp_parser_flags flags,
11992 cp_decl_specifier_seq *decl_specs,
11993 bool is_declaration,
11994 int* declares_class_or_enum,
11995 bool* is_cv_qualifier)
11997 tree type_spec = NULL_TREE;
12000 cp_decl_spec ds = ds_last;
12002 /* Assume this type-specifier does not declare a new type. */
12003 if (declares_class_or_enum)
12004 *declares_class_or_enum = 0;
12005 /* And that it does not specify a cv-qualifier. */
12006 if (is_cv_qualifier)
12007 *is_cv_qualifier = false;
12008 /* Peek at the next token. */
12009 token = cp_lexer_peek_token (parser->lexer);
12011 /* If we're looking at a keyword, we can use that to guide the
12012 production we choose. */
12013 keyword = token->keyword;
12017 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12018 goto elaborated_type_specifier;
12020 /* Look for the enum-specifier. */
12021 type_spec = cp_parser_enum_specifier (parser);
12022 /* If that worked, we're done. */
12025 if (declares_class_or_enum)
12026 *declares_class_or_enum = 2;
12028 cp_parser_set_decl_spec_type (decl_specs,
12031 /*user_defined_p=*/true);
12035 goto elaborated_type_specifier;
12037 /* Any of these indicate either a class-specifier, or an
12038 elaborated-type-specifier. */
12042 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12043 goto elaborated_type_specifier;
12045 /* Parse tentatively so that we can back up if we don't find a
12046 class-specifier. */
12047 cp_parser_parse_tentatively (parser);
12048 /* Look for the class-specifier. */
12049 type_spec = cp_parser_class_specifier (parser);
12050 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12051 /* If that worked, we're done. */
12052 if (cp_parser_parse_definitely (parser))
12054 if (declares_class_or_enum)
12055 *declares_class_or_enum = 2;
12057 cp_parser_set_decl_spec_type (decl_specs,
12060 /*user_defined_p=*/true);
12064 /* Fall through. */
12065 elaborated_type_specifier:
12066 /* We're declaring (not defining) a class or enum. */
12067 if (declares_class_or_enum)
12068 *declares_class_or_enum = 1;
12070 /* Fall through. */
12072 /* Look for an elaborated-type-specifier. */
12074 = (cp_parser_elaborated_type_specifier
12076 decl_specs && decl_specs->specs[(int) ds_friend],
12079 cp_parser_set_decl_spec_type (decl_specs,
12082 /*user_defined_p=*/true);
12087 if (is_cv_qualifier)
12088 *is_cv_qualifier = true;
12093 if (is_cv_qualifier)
12094 *is_cv_qualifier = true;
12099 if (is_cv_qualifier)
12100 *is_cv_qualifier = true;
12104 /* The `__complex__' keyword is a GNU extension. */
12112 /* Handle simple keywords. */
12117 ++decl_specs->specs[(int)ds];
12118 decl_specs->any_specifiers_p = true;
12120 return cp_lexer_consume_token (parser->lexer)->u.value;
12123 /* If we do not already have a type-specifier, assume we are looking
12124 at a simple-type-specifier. */
12125 type_spec = cp_parser_simple_type_specifier (parser,
12129 /* If we didn't find a type-specifier, and a type-specifier was not
12130 optional in this context, issue an error message. */
12131 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12133 cp_parser_error (parser, "expected type specifier");
12134 return error_mark_node;
12140 /* Parse a simple-type-specifier.
12142 simple-type-specifier:
12143 :: [opt] nested-name-specifier [opt] type-name
12144 :: [opt] nested-name-specifier template template-id
12159 simple-type-specifier:
12161 decltype ( expression )
12167 simple-type-specifier:
12169 __typeof__ unary-expression
12170 __typeof__ ( type-id )
12172 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12173 appropriately updated. */
12176 cp_parser_simple_type_specifier (cp_parser* parser,
12177 cp_decl_specifier_seq *decl_specs,
12178 cp_parser_flags flags)
12180 tree type = NULL_TREE;
12183 /* Peek at the next token. */
12184 token = cp_lexer_peek_token (parser->lexer);
12186 /* If we're looking at a keyword, things are easy. */
12187 switch (token->keyword)
12191 decl_specs->explicit_char_p = true;
12192 type = char_type_node;
12195 type = char16_type_node;
12198 type = char32_type_node;
12201 type = wchar_type_node;
12204 type = boolean_type_node;
12208 ++decl_specs->specs[(int) ds_short];
12209 type = short_integer_type_node;
12213 decl_specs->explicit_int_p = true;
12214 type = integer_type_node;
12217 if (!int128_integer_type_node)
12220 decl_specs->explicit_int128_p = true;
12221 type = int128_integer_type_node;
12225 ++decl_specs->specs[(int) ds_long];
12226 type = long_integer_type_node;
12230 ++decl_specs->specs[(int) ds_signed];
12231 type = integer_type_node;
12235 ++decl_specs->specs[(int) ds_unsigned];
12236 type = unsigned_type_node;
12239 type = float_type_node;
12242 type = double_type_node;
12245 type = void_type_node;
12249 maybe_warn_cpp0x (CPP0X_AUTO);
12250 type = make_auto ();
12254 /* Parse the `decltype' type. */
12255 type = cp_parser_decltype (parser);
12258 cp_parser_set_decl_spec_type (decl_specs, type,
12260 /*user_defined_p=*/true);
12265 /* Consume the `typeof' token. */
12266 cp_lexer_consume_token (parser->lexer);
12267 /* Parse the operand to `typeof'. */
12268 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12269 /* If it is not already a TYPE, take its type. */
12270 if (!TYPE_P (type))
12271 type = finish_typeof (type);
12274 cp_parser_set_decl_spec_type (decl_specs, type,
12276 /*user_defined_p=*/true);
12284 /* If the type-specifier was for a built-in type, we're done. */
12287 /* Record the type. */
12289 && (token->keyword != RID_SIGNED
12290 && token->keyword != RID_UNSIGNED
12291 && token->keyword != RID_SHORT
12292 && token->keyword != RID_LONG))
12293 cp_parser_set_decl_spec_type (decl_specs,
12296 /*user_defined=*/false);
12298 decl_specs->any_specifiers_p = true;
12300 /* Consume the token. */
12301 cp_lexer_consume_token (parser->lexer);
12303 /* There is no valid C++ program where a non-template type is
12304 followed by a "<". That usually indicates that the user thought
12305 that the type was a template. */
12306 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12308 return TYPE_NAME (type);
12311 /* The type-specifier must be a user-defined type. */
12312 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12317 /* Don't gobble tokens or issue error messages if this is an
12318 optional type-specifier. */
12319 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12320 cp_parser_parse_tentatively (parser);
12322 /* Look for the optional `::' operator. */
12324 = (cp_parser_global_scope_opt (parser,
12325 /*current_scope_valid_p=*/false)
12327 /* Look for the nested-name specifier. */
12329 = (cp_parser_nested_name_specifier_opt (parser,
12330 /*typename_keyword_p=*/false,
12331 /*check_dependency_p=*/true,
12333 /*is_declaration=*/false)
12335 token = cp_lexer_peek_token (parser->lexer);
12336 /* If we have seen a nested-name-specifier, and the next token
12337 is `template', then we are using the template-id production. */
12339 && cp_parser_optional_template_keyword (parser))
12341 /* Look for the template-id. */
12342 type = cp_parser_template_id (parser,
12343 /*template_keyword_p=*/true,
12344 /*check_dependency_p=*/true,
12345 /*is_declaration=*/false);
12346 /* If the template-id did not name a type, we are out of
12348 if (TREE_CODE (type) != TYPE_DECL)
12350 cp_parser_error (parser, "expected template-id for type");
12354 /* Otherwise, look for a type-name. */
12356 type = cp_parser_type_name (parser);
12357 /* Keep track of all name-lookups performed in class scopes. */
12361 && TREE_CODE (type) == TYPE_DECL
12362 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12363 maybe_note_name_used_in_class (DECL_NAME (type), type);
12364 /* If it didn't work out, we don't have a TYPE. */
12365 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12366 && !cp_parser_parse_definitely (parser))
12368 if (type && decl_specs)
12369 cp_parser_set_decl_spec_type (decl_specs, type,
12371 /*user_defined=*/true);
12374 /* If we didn't get a type-name, issue an error message. */
12375 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12377 cp_parser_error (parser, "expected type-name");
12378 return error_mark_node;
12381 /* There is no valid C++ program where a non-template type is
12382 followed by a "<". That usually indicates that the user thought
12383 that the type was a template. */
12384 if (type && type != error_mark_node)
12386 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12387 If it is, then the '<'...'>' enclose protocol names rather than
12388 template arguments, and so everything is fine. */
12389 if (c_dialect_objc ()
12390 && (objc_is_id (type) || objc_is_class_name (type)))
12392 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12393 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12395 /* Clobber the "unqualified" type previously entered into
12396 DECL_SPECS with the new, improved protocol-qualified version. */
12398 decl_specs->type = qual_type;
12403 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12410 /* Parse a type-name.
12423 Returns a TYPE_DECL for the type. */
12426 cp_parser_type_name (cp_parser* parser)
12430 /* We can't know yet whether it is a class-name or not. */
12431 cp_parser_parse_tentatively (parser);
12432 /* Try a class-name. */
12433 type_decl = cp_parser_class_name (parser,
12434 /*typename_keyword_p=*/false,
12435 /*template_keyword_p=*/false,
12437 /*check_dependency_p=*/true,
12438 /*class_head_p=*/false,
12439 /*is_declaration=*/false);
12440 /* If it's not a class-name, keep looking. */
12441 if (!cp_parser_parse_definitely (parser))
12443 /* It must be a typedef-name or an enum-name. */
12444 return cp_parser_nonclass_name (parser);
12450 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12458 Returns a TYPE_DECL for the type. */
12461 cp_parser_nonclass_name (cp_parser* parser)
12466 cp_token *token = cp_lexer_peek_token (parser->lexer);
12467 identifier = cp_parser_identifier (parser);
12468 if (identifier == error_mark_node)
12469 return error_mark_node;
12471 /* Look up the type-name. */
12472 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12474 if (TREE_CODE (type_decl) != TYPE_DECL
12475 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12477 /* See if this is an Objective-C type. */
12478 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12479 tree type = objc_get_protocol_qualified_type (identifier, protos);
12481 type_decl = TYPE_NAME (type);
12484 /* Issue an error if we did not find a type-name. */
12485 if (TREE_CODE (type_decl) != TYPE_DECL)
12487 if (!cp_parser_simulate_error (parser))
12488 cp_parser_name_lookup_error (parser, identifier, type_decl,
12489 NLE_TYPE, token->location);
12490 return error_mark_node;
12492 /* Remember that the name was used in the definition of the
12493 current class so that we can check later to see if the
12494 meaning would have been different after the class was
12495 entirely defined. */
12496 else if (type_decl != error_mark_node
12498 maybe_note_name_used_in_class (identifier, type_decl);
12503 /* Parse an elaborated-type-specifier. Note that the grammar given
12504 here incorporates the resolution to DR68.
12506 elaborated-type-specifier:
12507 class-key :: [opt] nested-name-specifier [opt] identifier
12508 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12509 enum-key :: [opt] nested-name-specifier [opt] identifier
12510 typename :: [opt] nested-name-specifier identifier
12511 typename :: [opt] nested-name-specifier template [opt]
12516 elaborated-type-specifier:
12517 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12518 class-key attributes :: [opt] nested-name-specifier [opt]
12519 template [opt] template-id
12520 enum attributes :: [opt] nested-name-specifier [opt] identifier
12522 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12523 declared `friend'. If IS_DECLARATION is TRUE, then this
12524 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12525 something is being declared.
12527 Returns the TYPE specified. */
12530 cp_parser_elaborated_type_specifier (cp_parser* parser,
12532 bool is_declaration)
12534 enum tag_types tag_type;
12536 tree type = NULL_TREE;
12537 tree attributes = NULL_TREE;
12539 cp_token *token = NULL;
12541 /* See if we're looking at the `enum' keyword. */
12542 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12544 /* Consume the `enum' token. */
12545 cp_lexer_consume_token (parser->lexer);
12546 /* Remember that it's an enumeration type. */
12547 tag_type = enum_type;
12548 /* Parse the optional `struct' or `class' key (for C++0x scoped
12550 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12551 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12553 if (cxx_dialect == cxx98)
12554 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12556 /* Consume the `struct' or `class'. */
12557 cp_lexer_consume_token (parser->lexer);
12559 /* Parse the attributes. */
12560 attributes = cp_parser_attributes_opt (parser);
12562 /* Or, it might be `typename'. */
12563 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12566 /* Consume the `typename' token. */
12567 cp_lexer_consume_token (parser->lexer);
12568 /* Remember that it's a `typename' type. */
12569 tag_type = typename_type;
12571 /* Otherwise it must be a class-key. */
12574 tag_type = cp_parser_class_key (parser);
12575 if (tag_type == none_type)
12576 return error_mark_node;
12577 /* Parse the attributes. */
12578 attributes = cp_parser_attributes_opt (parser);
12581 /* Look for the `::' operator. */
12582 globalscope = cp_parser_global_scope_opt (parser,
12583 /*current_scope_valid_p=*/false);
12584 /* Look for the nested-name-specifier. */
12585 if (tag_type == typename_type && !globalscope)
12587 if (!cp_parser_nested_name_specifier (parser,
12588 /*typename_keyword_p=*/true,
12589 /*check_dependency_p=*/true,
12592 return error_mark_node;
12595 /* Even though `typename' is not present, the proposed resolution
12596 to Core Issue 180 says that in `class A<T>::B', `B' should be
12597 considered a type-name, even if `A<T>' is dependent. */
12598 cp_parser_nested_name_specifier_opt (parser,
12599 /*typename_keyword_p=*/true,
12600 /*check_dependency_p=*/true,
12603 /* For everything but enumeration types, consider a template-id.
12604 For an enumeration type, consider only a plain identifier. */
12605 if (tag_type != enum_type)
12607 bool template_p = false;
12610 /* Allow the `template' keyword. */
12611 template_p = cp_parser_optional_template_keyword (parser);
12612 /* If we didn't see `template', we don't know if there's a
12613 template-id or not. */
12615 cp_parser_parse_tentatively (parser);
12616 /* Parse the template-id. */
12617 token = cp_lexer_peek_token (parser->lexer);
12618 decl = cp_parser_template_id (parser, template_p,
12619 /*check_dependency_p=*/true,
12621 /* If we didn't find a template-id, look for an ordinary
12623 if (!template_p && !cp_parser_parse_definitely (parser))
12625 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12626 in effect, then we must assume that, upon instantiation, the
12627 template will correspond to a class. */
12628 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12629 && tag_type == typename_type)
12630 type = make_typename_type (parser->scope, decl,
12632 /*complain=*/tf_error);
12633 /* If the `typename' keyword is in effect and DECL is not a type
12634 decl. Then type is non existant. */
12635 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12638 type = TREE_TYPE (decl);
12643 token = cp_lexer_peek_token (parser->lexer);
12644 identifier = cp_parser_identifier (parser);
12646 if (identifier == error_mark_node)
12648 parser->scope = NULL_TREE;
12649 return error_mark_node;
12652 /* For a `typename', we needn't call xref_tag. */
12653 if (tag_type == typename_type
12654 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12655 return cp_parser_make_typename_type (parser, parser->scope,
12658 /* Look up a qualified name in the usual way. */
12662 tree ambiguous_decls;
12664 decl = cp_parser_lookup_name (parser, identifier,
12666 /*is_template=*/false,
12667 /*is_namespace=*/false,
12668 /*check_dependency=*/true,
12672 /* If the lookup was ambiguous, an error will already have been
12674 if (ambiguous_decls)
12675 return error_mark_node;
12677 /* If we are parsing friend declaration, DECL may be a
12678 TEMPLATE_DECL tree node here. However, we need to check
12679 whether this TEMPLATE_DECL results in valid code. Consider
12680 the following example:
12683 template <class T> class C {};
12686 template <class T> friend class N::C; // #1, valid code
12688 template <class T> class Y {
12689 friend class N::C; // #2, invalid code
12692 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12693 name lookup of `N::C'. We see that friend declaration must
12694 be template for the code to be valid. Note that
12695 processing_template_decl does not work here since it is
12696 always 1 for the above two cases. */
12698 decl = (cp_parser_maybe_treat_template_as_class
12699 (decl, /*tag_name_p=*/is_friend
12700 && parser->num_template_parameter_lists));
12702 if (TREE_CODE (decl) != TYPE_DECL)
12704 cp_parser_diagnose_invalid_type_name (parser,
12708 return error_mark_node;
12711 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12713 bool allow_template = (parser->num_template_parameter_lists
12714 || DECL_SELF_REFERENCE_P (decl));
12715 type = check_elaborated_type_specifier (tag_type, decl,
12718 if (type == error_mark_node)
12719 return error_mark_node;
12722 /* Forward declarations of nested types, such as
12727 are invalid unless all components preceding the final '::'
12728 are complete. If all enclosing types are complete, these
12729 declarations become merely pointless.
12731 Invalid forward declarations of nested types are errors
12732 caught elsewhere in parsing. Those that are pointless arrive
12735 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12736 && !is_friend && !processing_explicit_instantiation)
12737 warning (0, "declaration %qD does not declare anything", decl);
12739 type = TREE_TYPE (decl);
12743 /* An elaborated-type-specifier sometimes introduces a new type and
12744 sometimes names an existing type. Normally, the rule is that it
12745 introduces a new type only if there is not an existing type of
12746 the same name already in scope. For example, given:
12749 void f() { struct S s; }
12751 the `struct S' in the body of `f' is the same `struct S' as in
12752 the global scope; the existing definition is used. However, if
12753 there were no global declaration, this would introduce a new
12754 local class named `S'.
12756 An exception to this rule applies to the following code:
12758 namespace N { struct S; }
12760 Here, the elaborated-type-specifier names a new type
12761 unconditionally; even if there is already an `S' in the
12762 containing scope this declaration names a new type.
12763 This exception only applies if the elaborated-type-specifier
12764 forms the complete declaration:
12768 A declaration consisting solely of `class-key identifier ;' is
12769 either a redeclaration of the name in the current scope or a
12770 forward declaration of the identifier as a class name. It
12771 introduces the name into the current scope.
12773 We are in this situation precisely when the next token is a `;'.
12775 An exception to the exception is that a `friend' declaration does
12776 *not* name a new type; i.e., given:
12778 struct S { friend struct T; };
12780 `T' is not a new type in the scope of `S'.
12782 Also, `new struct S' or `sizeof (struct S)' never results in the
12783 definition of a new type; a new type can only be declared in a
12784 declaration context. */
12790 /* Friends have special name lookup rules. */
12791 ts = ts_within_enclosing_non_class;
12792 else if (is_declaration
12793 && cp_lexer_next_token_is (parser->lexer,
12795 /* This is a `class-key identifier ;' */
12801 (parser->num_template_parameter_lists
12802 && (cp_parser_next_token_starts_class_definition_p (parser)
12803 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12804 /* An unqualified name was used to reference this type, so
12805 there were no qualifying templates. */
12806 if (!cp_parser_check_template_parameters (parser,
12807 /*num_templates=*/0,
12809 /*declarator=*/NULL))
12810 return error_mark_node;
12811 type = xref_tag (tag_type, identifier, ts, template_p);
12815 if (type == error_mark_node)
12816 return error_mark_node;
12818 /* Allow attributes on forward declarations of classes. */
12821 if (TREE_CODE (type) == TYPENAME_TYPE)
12822 warning (OPT_Wattributes,
12823 "attributes ignored on uninstantiated type");
12824 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12825 && ! processing_explicit_instantiation)
12826 warning (OPT_Wattributes,
12827 "attributes ignored on template instantiation");
12828 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12829 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12831 warning (OPT_Wattributes,
12832 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12835 if (tag_type != enum_type)
12836 cp_parser_check_class_key (tag_type, type);
12838 /* A "<" cannot follow an elaborated type specifier. If that
12839 happens, the user was probably trying to form a template-id. */
12840 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12845 /* Parse an enum-specifier.
12848 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12853 enum struct [C++0x]
12856 : type-specifier-seq
12859 enum-key attributes[opt] identifier [opt] enum-base [opt]
12860 { enumerator-list [opt] }attributes[opt]
12862 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12863 if the token stream isn't an enum-specifier after all. */
12866 cp_parser_enum_specifier (cp_parser* parser)
12871 bool scoped_enum_p = false;
12872 bool has_underlying_type = false;
12873 tree underlying_type = NULL_TREE;
12875 /* Parse tentatively so that we can back up if we don't find a
12877 cp_parser_parse_tentatively (parser);
12879 /* Caller guarantees that the current token is 'enum', an identifier
12880 possibly follows, and the token after that is an opening brace.
12881 If we don't have an identifier, fabricate an anonymous name for
12882 the enumeration being defined. */
12883 cp_lexer_consume_token (parser->lexer);
12885 /* Parse the "class" or "struct", which indicates a scoped
12886 enumeration type in C++0x. */
12887 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12888 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12890 if (cxx_dialect == cxx98)
12891 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12893 /* Consume the `struct' or `class' token. */
12894 cp_lexer_consume_token (parser->lexer);
12896 scoped_enum_p = true;
12899 attributes = cp_parser_attributes_opt (parser);
12901 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12902 identifier = cp_parser_identifier (parser);
12904 identifier = make_anon_name ();
12906 /* Check for the `:' that denotes a specified underlying type in C++0x.
12907 Note that a ':' could also indicate a bitfield width, however. */
12908 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12910 cp_decl_specifier_seq type_specifiers;
12912 /* Consume the `:'. */
12913 cp_lexer_consume_token (parser->lexer);
12915 /* Parse the type-specifier-seq. */
12916 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12917 /*is_trailing_return=*/false,
12920 /* At this point this is surely not elaborated type specifier. */
12921 if (!cp_parser_parse_definitely (parser))
12924 if (cxx_dialect == cxx98)
12925 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12927 has_underlying_type = true;
12929 /* If that didn't work, stop. */
12930 if (type_specifiers.type != error_mark_node)
12932 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12933 /*initialized=*/0, NULL);
12934 if (underlying_type == error_mark_node)
12935 underlying_type = NULL_TREE;
12939 /* Look for the `{' but don't consume it yet. */
12940 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12942 cp_parser_error (parser, "expected %<{%>");
12943 if (has_underlying_type)
12947 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12950 /* Issue an error message if type-definitions are forbidden here. */
12951 if (!cp_parser_check_type_definition (parser))
12952 type = error_mark_node;
12954 /* Create the new type. We do this before consuming the opening
12955 brace so the enum will be recorded as being on the line of its
12956 tag (or the 'enum' keyword, if there is no tag). */
12957 type = start_enum (identifier, underlying_type, scoped_enum_p);
12959 /* Consume the opening brace. */
12960 cp_lexer_consume_token (parser->lexer);
12962 if (type == error_mark_node)
12964 cp_parser_skip_to_end_of_block_or_statement (parser);
12965 return error_mark_node;
12968 /* If the next token is not '}', then there are some enumerators. */
12969 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12970 cp_parser_enumerator_list (parser, type);
12972 /* Consume the final '}'. */
12973 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12975 /* Look for trailing attributes to apply to this enumeration, and
12976 apply them if appropriate. */
12977 if (cp_parser_allow_gnu_extensions_p (parser))
12979 tree trailing_attr = cp_parser_attributes_opt (parser);
12980 trailing_attr = chainon (trailing_attr, attributes);
12981 cplus_decl_attributes (&type,
12983 (int) ATTR_FLAG_TYPE_IN_PLACE);
12986 /* Finish up the enumeration. */
12987 finish_enum (type);
12992 /* Parse an enumerator-list. The enumerators all have the indicated
12996 enumerator-definition
12997 enumerator-list , enumerator-definition */
13000 cp_parser_enumerator_list (cp_parser* parser, tree type)
13004 /* Parse an enumerator-definition. */
13005 cp_parser_enumerator_definition (parser, type);
13007 /* If the next token is not a ',', we've reached the end of
13009 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13011 /* Otherwise, consume the `,' and keep going. */
13012 cp_lexer_consume_token (parser->lexer);
13013 /* If the next token is a `}', there is a trailing comma. */
13014 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13016 if (!in_system_header)
13017 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13023 /* Parse an enumerator-definition. The enumerator has the indicated
13026 enumerator-definition:
13028 enumerator = constant-expression
13034 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13039 /* Look for the identifier. */
13040 identifier = cp_parser_identifier (parser);
13041 if (identifier == error_mark_node)
13044 /* If the next token is an '=', then there is an explicit value. */
13045 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13047 /* Consume the `=' token. */
13048 cp_lexer_consume_token (parser->lexer);
13049 /* Parse the value. */
13050 value = cp_parser_constant_expression (parser,
13051 /*allow_non_constant_p=*/false,
13057 /* If we are processing a template, make sure the initializer of the
13058 enumerator doesn't contain any bare template parameter pack. */
13059 if (check_for_bare_parameter_packs (value))
13060 value = error_mark_node;
13062 /* Create the enumerator. */
13063 build_enumerator (identifier, value, type);
13066 /* Parse a namespace-name.
13069 original-namespace-name
13072 Returns the NAMESPACE_DECL for the namespace. */
13075 cp_parser_namespace_name (cp_parser* parser)
13078 tree namespace_decl;
13080 cp_token *token = cp_lexer_peek_token (parser->lexer);
13082 /* Get the name of the namespace. */
13083 identifier = cp_parser_identifier (parser);
13084 if (identifier == error_mark_node)
13085 return error_mark_node;
13087 /* Look up the identifier in the currently active scope. Look only
13088 for namespaces, due to:
13090 [basic.lookup.udir]
13092 When looking up a namespace-name in a using-directive or alias
13093 definition, only namespace names are considered.
13097 [basic.lookup.qual]
13099 During the lookup of a name preceding the :: scope resolution
13100 operator, object, function, and enumerator names are ignored.
13102 (Note that cp_parser_qualifying_entity only calls this
13103 function if the token after the name is the scope resolution
13105 namespace_decl = cp_parser_lookup_name (parser, identifier,
13107 /*is_template=*/false,
13108 /*is_namespace=*/true,
13109 /*check_dependency=*/true,
13110 /*ambiguous_decls=*/NULL,
13112 /* If it's not a namespace, issue an error. */
13113 if (namespace_decl == error_mark_node
13114 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13116 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13117 error_at (token->location, "%qD is not a namespace-name", identifier);
13118 cp_parser_error (parser, "expected namespace-name");
13119 namespace_decl = error_mark_node;
13122 return namespace_decl;
13125 /* Parse a namespace-definition.
13127 namespace-definition:
13128 named-namespace-definition
13129 unnamed-namespace-definition
13131 named-namespace-definition:
13132 original-namespace-definition
13133 extension-namespace-definition
13135 original-namespace-definition:
13136 namespace identifier { namespace-body }
13138 extension-namespace-definition:
13139 namespace original-namespace-name { namespace-body }
13141 unnamed-namespace-definition:
13142 namespace { namespace-body } */
13145 cp_parser_namespace_definition (cp_parser* parser)
13147 tree identifier, attribs;
13148 bool has_visibility;
13151 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13154 cp_lexer_consume_token (parser->lexer);
13159 /* Look for the `namespace' keyword. */
13160 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13162 /* Get the name of the namespace. We do not attempt to distinguish
13163 between an original-namespace-definition and an
13164 extension-namespace-definition at this point. The semantic
13165 analysis routines are responsible for that. */
13166 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13167 identifier = cp_parser_identifier (parser);
13169 identifier = NULL_TREE;
13171 /* Parse any specified attributes. */
13172 attribs = cp_parser_attributes_opt (parser);
13174 /* Look for the `{' to start the namespace. */
13175 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13176 /* Start the namespace. */
13177 push_namespace (identifier);
13179 /* "inline namespace" is equivalent to a stub namespace definition
13180 followed by a strong using directive. */
13183 tree name_space = current_namespace;
13184 /* Set up namespace association. */
13185 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13186 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13187 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13188 /* Import the contents of the inline namespace. */
13190 do_using_directive (name_space);
13191 push_namespace (identifier);
13194 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13196 /* Parse the body of the namespace. */
13197 cp_parser_namespace_body (parser);
13199 #ifdef HANDLE_PRAGMA_VISIBILITY
13200 if (has_visibility)
13201 pop_visibility (1);
13204 /* Finish the namespace. */
13206 /* Look for the final `}'. */
13207 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13210 /* Parse a namespace-body.
13213 declaration-seq [opt] */
13216 cp_parser_namespace_body (cp_parser* parser)
13218 cp_parser_declaration_seq_opt (parser);
13221 /* Parse a namespace-alias-definition.
13223 namespace-alias-definition:
13224 namespace identifier = qualified-namespace-specifier ; */
13227 cp_parser_namespace_alias_definition (cp_parser* parser)
13230 tree namespace_specifier;
13232 cp_token *token = cp_lexer_peek_token (parser->lexer);
13234 /* Look for the `namespace' keyword. */
13235 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13236 /* Look for the identifier. */
13237 identifier = cp_parser_identifier (parser);
13238 if (identifier == error_mark_node)
13240 /* Look for the `=' token. */
13241 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13242 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13244 error_at (token->location, "%<namespace%> definition is not allowed here");
13245 /* Skip the definition. */
13246 cp_lexer_consume_token (parser->lexer);
13247 if (cp_parser_skip_to_closing_brace (parser))
13248 cp_lexer_consume_token (parser->lexer);
13251 cp_parser_require (parser, CPP_EQ, RT_EQ);
13252 /* Look for the qualified-namespace-specifier. */
13253 namespace_specifier
13254 = cp_parser_qualified_namespace_specifier (parser);
13255 /* Look for the `;' token. */
13256 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13258 /* Register the alias in the symbol table. */
13259 do_namespace_alias (identifier, namespace_specifier);
13262 /* Parse a qualified-namespace-specifier.
13264 qualified-namespace-specifier:
13265 :: [opt] nested-name-specifier [opt] namespace-name
13267 Returns a NAMESPACE_DECL corresponding to the specified
13271 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13273 /* Look for the optional `::'. */
13274 cp_parser_global_scope_opt (parser,
13275 /*current_scope_valid_p=*/false);
13277 /* Look for the optional nested-name-specifier. */
13278 cp_parser_nested_name_specifier_opt (parser,
13279 /*typename_keyword_p=*/false,
13280 /*check_dependency_p=*/true,
13282 /*is_declaration=*/true);
13284 return cp_parser_namespace_name (parser);
13287 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13288 access declaration.
13291 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13292 using :: unqualified-id ;
13294 access-declaration:
13300 cp_parser_using_declaration (cp_parser* parser,
13301 bool access_declaration_p)
13304 bool typename_p = false;
13305 bool global_scope_p;
13310 if (access_declaration_p)
13311 cp_parser_parse_tentatively (parser);
13314 /* Look for the `using' keyword. */
13315 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13317 /* Peek at the next token. */
13318 token = cp_lexer_peek_token (parser->lexer);
13319 /* See if it's `typename'. */
13320 if (token->keyword == RID_TYPENAME)
13322 /* Remember that we've seen it. */
13324 /* Consume the `typename' token. */
13325 cp_lexer_consume_token (parser->lexer);
13329 /* Look for the optional global scope qualification. */
13331 = (cp_parser_global_scope_opt (parser,
13332 /*current_scope_valid_p=*/false)
13335 /* If we saw `typename', or didn't see `::', then there must be a
13336 nested-name-specifier present. */
13337 if (typename_p || !global_scope_p)
13338 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13339 /*check_dependency_p=*/true,
13341 /*is_declaration=*/true);
13342 /* Otherwise, we could be in either of the two productions. In that
13343 case, treat the nested-name-specifier as optional. */
13345 qscope = cp_parser_nested_name_specifier_opt (parser,
13346 /*typename_keyword_p=*/false,
13347 /*check_dependency_p=*/true,
13349 /*is_declaration=*/true);
13351 qscope = global_namespace;
13353 if (access_declaration_p && cp_parser_error_occurred (parser))
13354 /* Something has already gone wrong; there's no need to parse
13355 further. Since an error has occurred, the return value of
13356 cp_parser_parse_definitely will be false, as required. */
13357 return cp_parser_parse_definitely (parser);
13359 token = cp_lexer_peek_token (parser->lexer);
13360 /* Parse the unqualified-id. */
13361 identifier = cp_parser_unqualified_id (parser,
13362 /*template_keyword_p=*/false,
13363 /*check_dependency_p=*/true,
13364 /*declarator_p=*/true,
13365 /*optional_p=*/false);
13367 if (access_declaration_p)
13369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13370 cp_parser_simulate_error (parser);
13371 if (!cp_parser_parse_definitely (parser))
13375 /* The function we call to handle a using-declaration is different
13376 depending on what scope we are in. */
13377 if (qscope == error_mark_node || identifier == error_mark_node)
13379 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13380 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13381 /* [namespace.udecl]
13383 A using declaration shall not name a template-id. */
13384 error_at (token->location,
13385 "a template-id may not appear in a using-declaration");
13388 if (at_class_scope_p ())
13390 /* Create the USING_DECL. */
13391 decl = do_class_using_decl (parser->scope, identifier);
13393 if (check_for_bare_parameter_packs (decl))
13396 /* Add it to the list of members in this class. */
13397 finish_member_declaration (decl);
13401 decl = cp_parser_lookup_name_simple (parser,
13404 if (decl == error_mark_node)
13405 cp_parser_name_lookup_error (parser, identifier,
13408 else if (check_for_bare_parameter_packs (decl))
13410 else if (!at_namespace_scope_p ())
13411 do_local_using_decl (decl, qscope, identifier);
13413 do_toplevel_using_decl (decl, qscope, identifier);
13417 /* Look for the final `;'. */
13418 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13423 /* Parse a using-directive.
13426 using namespace :: [opt] nested-name-specifier [opt]
13427 namespace-name ; */
13430 cp_parser_using_directive (cp_parser* parser)
13432 tree namespace_decl;
13435 /* Look for the `using' keyword. */
13436 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13437 /* And the `namespace' keyword. */
13438 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13439 /* Look for the optional `::' operator. */
13440 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13441 /* And the optional nested-name-specifier. */
13442 cp_parser_nested_name_specifier_opt (parser,
13443 /*typename_keyword_p=*/false,
13444 /*check_dependency_p=*/true,
13446 /*is_declaration=*/true);
13447 /* Get the namespace being used. */
13448 namespace_decl = cp_parser_namespace_name (parser);
13449 /* And any specified attributes. */
13450 attribs = cp_parser_attributes_opt (parser);
13451 /* Update the symbol table. */
13452 parse_using_directive (namespace_decl, attribs);
13453 /* Look for the final `;'. */
13454 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13457 /* Parse an asm-definition.
13460 asm ( string-literal ) ;
13465 asm volatile [opt] ( string-literal ) ;
13466 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13467 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13468 : asm-operand-list [opt] ) ;
13469 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13470 : asm-operand-list [opt]
13471 : asm-clobber-list [opt] ) ;
13472 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13473 : asm-clobber-list [opt]
13474 : asm-goto-list ) ; */
13477 cp_parser_asm_definition (cp_parser* parser)
13480 tree outputs = NULL_TREE;
13481 tree inputs = NULL_TREE;
13482 tree clobbers = NULL_TREE;
13483 tree labels = NULL_TREE;
13485 bool volatile_p = false;
13486 bool extended_p = false;
13487 bool invalid_inputs_p = false;
13488 bool invalid_outputs_p = false;
13489 bool goto_p = false;
13490 required_token missing = RT_NONE;
13492 /* Look for the `asm' keyword. */
13493 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
13494 /* See if the next token is `volatile'. */
13495 if (cp_parser_allow_gnu_extensions_p (parser)
13496 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13498 /* Remember that we saw the `volatile' keyword. */
13500 /* Consume the token. */
13501 cp_lexer_consume_token (parser->lexer);
13503 if (cp_parser_allow_gnu_extensions_p (parser)
13504 && parser->in_function_body
13505 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13507 /* Remember that we saw the `goto' keyword. */
13509 /* Consume the token. */
13510 cp_lexer_consume_token (parser->lexer);
13512 /* Look for the opening `('. */
13513 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13515 /* Look for the string. */
13516 string = cp_parser_string_literal (parser, false, false);
13517 if (string == error_mark_node)
13519 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13520 /*consume_paren=*/true);
13524 /* If we're allowing GNU extensions, check for the extended assembly
13525 syntax. Unfortunately, the `:' tokens need not be separated by
13526 a space in C, and so, for compatibility, we tolerate that here
13527 too. Doing that means that we have to treat the `::' operator as
13529 if (cp_parser_allow_gnu_extensions_p (parser)
13530 && parser->in_function_body
13531 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13532 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13534 bool inputs_p = false;
13535 bool clobbers_p = false;
13536 bool labels_p = false;
13538 /* The extended syntax was used. */
13541 /* Look for outputs. */
13542 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13544 /* Consume the `:'. */
13545 cp_lexer_consume_token (parser->lexer);
13546 /* Parse the output-operands. */
13547 if (cp_lexer_next_token_is_not (parser->lexer,
13549 && cp_lexer_next_token_is_not (parser->lexer,
13551 && cp_lexer_next_token_is_not (parser->lexer,
13554 outputs = cp_parser_asm_operand_list (parser);
13556 if (outputs == error_mark_node)
13557 invalid_outputs_p = true;
13559 /* If the next token is `::', there are no outputs, and the
13560 next token is the beginning of the inputs. */
13561 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13562 /* The inputs are coming next. */
13565 /* Look for inputs. */
13567 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13569 /* Consume the `:' or `::'. */
13570 cp_lexer_consume_token (parser->lexer);
13571 /* Parse the output-operands. */
13572 if (cp_lexer_next_token_is_not (parser->lexer,
13574 && cp_lexer_next_token_is_not (parser->lexer,
13576 && cp_lexer_next_token_is_not (parser->lexer,
13578 inputs = cp_parser_asm_operand_list (parser);
13580 if (inputs == error_mark_node)
13581 invalid_inputs_p = true;
13583 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13584 /* The clobbers are coming next. */
13587 /* Look for clobbers. */
13589 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13592 /* Consume the `:' or `::'. */
13593 cp_lexer_consume_token (parser->lexer);
13594 /* Parse the clobbers. */
13595 if (cp_lexer_next_token_is_not (parser->lexer,
13597 && cp_lexer_next_token_is_not (parser->lexer,
13599 clobbers = cp_parser_asm_clobber_list (parser);
13602 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13603 /* The labels are coming next. */
13606 /* Look for labels. */
13608 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13611 /* Consume the `:' or `::'. */
13612 cp_lexer_consume_token (parser->lexer);
13613 /* Parse the labels. */
13614 labels = cp_parser_asm_label_list (parser);
13617 if (goto_p && !labels_p)
13618 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
13621 missing = RT_COLON_SCOPE;
13623 /* Look for the closing `)'. */
13624 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13625 missing ? missing : RT_CLOSE_PAREN))
13626 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13627 /*consume_paren=*/true);
13628 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13630 if (!invalid_inputs_p && !invalid_outputs_p)
13632 /* Create the ASM_EXPR. */
13633 if (parser->in_function_body)
13635 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13636 inputs, clobbers, labels);
13637 /* If the extended syntax was not used, mark the ASM_EXPR. */
13640 tree temp = asm_stmt;
13641 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13642 temp = TREE_OPERAND (temp, 0);
13644 ASM_INPUT_P (temp) = 1;
13648 cgraph_add_asm_node (string);
13652 /* Declarators [gram.dcl.decl] */
13654 /* Parse an init-declarator.
13657 declarator initializer [opt]
13662 declarator asm-specification [opt] attributes [opt] initializer [opt]
13664 function-definition:
13665 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13667 decl-specifier-seq [opt] declarator function-try-block
13671 function-definition:
13672 __extension__ function-definition
13674 The DECL_SPECIFIERS apply to this declarator. Returns a
13675 representation of the entity declared. If MEMBER_P is TRUE, then
13676 this declarator appears in a class scope. The new DECL created by
13677 this declarator is returned.
13679 The CHECKS are access checks that should be performed once we know
13680 what entity is being declared (and, therefore, what classes have
13683 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13684 for a function-definition here as well. If the declarator is a
13685 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13686 be TRUE upon return. By that point, the function-definition will
13687 have been completely parsed.
13689 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13693 cp_parser_init_declarator (cp_parser* parser,
13694 cp_decl_specifier_seq *decl_specifiers,
13695 VEC (deferred_access_check,gc)* checks,
13696 bool function_definition_allowed_p,
13698 int declares_class_or_enum,
13699 bool* function_definition_p)
13701 cp_token *token = NULL, *asm_spec_start_token = NULL,
13702 *attributes_start_token = NULL;
13703 cp_declarator *declarator;
13704 tree prefix_attributes;
13706 tree asm_specification;
13708 tree decl = NULL_TREE;
13710 int is_initialized;
13711 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13712 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13714 enum cpp_ttype initialization_kind;
13715 bool is_direct_init = false;
13716 bool is_non_constant_init;
13717 int ctor_dtor_or_conv_p;
13719 tree pushed_scope = NULL;
13721 /* Gather the attributes that were provided with the
13722 decl-specifiers. */
13723 prefix_attributes = decl_specifiers->attributes;
13725 /* Assume that this is not the declarator for a function
13727 if (function_definition_p)
13728 *function_definition_p = false;
13730 /* Defer access checks while parsing the declarator; we cannot know
13731 what names are accessible until we know what is being
13733 resume_deferring_access_checks ();
13735 /* Parse the declarator. */
13736 token = cp_lexer_peek_token (parser->lexer);
13738 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13739 &ctor_dtor_or_conv_p,
13740 /*parenthesized_p=*/NULL,
13741 /*member_p=*/false);
13742 /* Gather up the deferred checks. */
13743 stop_deferring_access_checks ();
13745 /* If the DECLARATOR was erroneous, there's no need to go
13747 if (declarator == cp_error_declarator)
13748 return error_mark_node;
13750 /* Check that the number of template-parameter-lists is OK. */
13751 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13753 return error_mark_node;
13755 if (declares_class_or_enum & 2)
13756 cp_parser_check_for_definition_in_return_type (declarator,
13757 decl_specifiers->type,
13758 decl_specifiers->type_location);
13760 /* Figure out what scope the entity declared by the DECLARATOR is
13761 located in. `grokdeclarator' sometimes changes the scope, so
13762 we compute it now. */
13763 scope = get_scope_of_declarator (declarator);
13765 /* Perform any lookups in the declared type which were thought to be
13766 dependent, but are not in the scope of the declarator. */
13767 decl_specifiers->type
13768 = maybe_update_decl_type (decl_specifiers->type, scope);
13770 /* If we're allowing GNU extensions, look for an asm-specification
13772 if (cp_parser_allow_gnu_extensions_p (parser))
13774 /* Look for an asm-specification. */
13775 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13776 asm_specification = cp_parser_asm_specification_opt (parser);
13777 /* And attributes. */
13778 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13779 attributes = cp_parser_attributes_opt (parser);
13783 asm_specification = NULL_TREE;
13784 attributes = NULL_TREE;
13787 /* Peek at the next token. */
13788 token = cp_lexer_peek_token (parser->lexer);
13789 /* Check to see if the token indicates the start of a
13790 function-definition. */
13791 if (function_declarator_p (declarator)
13792 && cp_parser_token_starts_function_definition_p (token))
13794 if (!function_definition_allowed_p)
13796 /* If a function-definition should not appear here, issue an
13798 cp_parser_error (parser,
13799 "a function-definition is not allowed here");
13800 return error_mark_node;
13804 location_t func_brace_location
13805 = cp_lexer_peek_token (parser->lexer)->location;
13807 /* Neither attributes nor an asm-specification are allowed
13808 on a function-definition. */
13809 if (asm_specification)
13810 error_at (asm_spec_start_token->location,
13811 "an asm-specification is not allowed "
13812 "on a function-definition");
13814 error_at (attributes_start_token->location,
13815 "attributes are not allowed on a function-definition");
13816 /* This is a function-definition. */
13817 *function_definition_p = true;
13819 /* Parse the function definition. */
13821 decl = cp_parser_save_member_function_body (parser,
13824 prefix_attributes);
13827 = (cp_parser_function_definition_from_specifiers_and_declarator
13828 (parser, decl_specifiers, prefix_attributes, declarator));
13830 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13832 /* This is where the prologue starts... */
13833 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13834 = func_brace_location;
13843 Only in function declarations for constructors, destructors, and
13844 type conversions can the decl-specifier-seq be omitted.
13846 We explicitly postpone this check past the point where we handle
13847 function-definitions because we tolerate function-definitions
13848 that are missing their return types in some modes. */
13849 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13851 cp_parser_error (parser,
13852 "expected constructor, destructor, or type conversion");
13853 return error_mark_node;
13856 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13857 if (token->type == CPP_EQ
13858 || token->type == CPP_OPEN_PAREN
13859 || token->type == CPP_OPEN_BRACE)
13861 is_initialized = SD_INITIALIZED;
13862 initialization_kind = token->type;
13864 if (token->type == CPP_EQ
13865 && function_declarator_p (declarator))
13867 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13868 if (t2->keyword == RID_DEFAULT)
13869 is_initialized = SD_DEFAULTED;
13870 else if (t2->keyword == RID_DELETE)
13871 is_initialized = SD_DELETED;
13876 /* If the init-declarator isn't initialized and isn't followed by a
13877 `,' or `;', it's not a valid init-declarator. */
13878 if (token->type != CPP_COMMA
13879 && token->type != CPP_SEMICOLON)
13881 cp_parser_error (parser, "expected initializer");
13882 return error_mark_node;
13884 is_initialized = SD_UNINITIALIZED;
13885 initialization_kind = CPP_EOF;
13888 /* Because start_decl has side-effects, we should only call it if we
13889 know we're going ahead. By this point, we know that we cannot
13890 possibly be looking at any other construct. */
13891 cp_parser_commit_to_tentative_parse (parser);
13893 /* If the decl specifiers were bad, issue an error now that we're
13894 sure this was intended to be a declarator. Then continue
13895 declaring the variable(s), as int, to try to cut down on further
13897 if (decl_specifiers->any_specifiers_p
13898 && decl_specifiers->type == error_mark_node)
13900 cp_parser_error (parser, "invalid type in declaration");
13901 decl_specifiers->type = integer_type_node;
13904 /* Check to see whether or not this declaration is a friend. */
13905 friend_p = cp_parser_friend_p (decl_specifiers);
13907 /* Enter the newly declared entry in the symbol table. If we're
13908 processing a declaration in a class-specifier, we wait until
13909 after processing the initializer. */
13912 if (parser->in_unbraced_linkage_specification_p)
13913 decl_specifiers->storage_class = sc_extern;
13914 decl = start_decl (declarator, decl_specifiers,
13915 is_initialized, attributes, prefix_attributes,
13919 /* Enter the SCOPE. That way unqualified names appearing in the
13920 initializer will be looked up in SCOPE. */
13921 pushed_scope = push_scope (scope);
13923 /* Perform deferred access control checks, now that we know in which
13924 SCOPE the declared entity resides. */
13925 if (!member_p && decl)
13927 tree saved_current_function_decl = NULL_TREE;
13929 /* If the entity being declared is a function, pretend that we
13930 are in its scope. If it is a `friend', it may have access to
13931 things that would not otherwise be accessible. */
13932 if (TREE_CODE (decl) == FUNCTION_DECL)
13934 saved_current_function_decl = current_function_decl;
13935 current_function_decl = decl;
13938 /* Perform access checks for template parameters. */
13939 cp_parser_perform_template_parameter_access_checks (checks);
13941 /* Perform the access control checks for the declarator and the
13942 decl-specifiers. */
13943 perform_deferred_access_checks ();
13945 /* Restore the saved value. */
13946 if (TREE_CODE (decl) == FUNCTION_DECL)
13947 current_function_decl = saved_current_function_decl;
13950 /* Parse the initializer. */
13951 initializer = NULL_TREE;
13952 is_direct_init = false;
13953 is_non_constant_init = true;
13954 if (is_initialized)
13956 if (function_declarator_p (declarator))
13958 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13959 if (initialization_kind == CPP_EQ)
13960 initializer = cp_parser_pure_specifier (parser);
13963 /* If the declaration was erroneous, we don't really
13964 know what the user intended, so just silently
13965 consume the initializer. */
13966 if (decl != error_mark_node)
13967 error_at (initializer_start_token->location,
13968 "initializer provided for function");
13969 cp_parser_skip_to_closing_parenthesis (parser,
13970 /*recovering=*/true,
13971 /*or_comma=*/false,
13972 /*consume_paren=*/true);
13977 /* We want to record the extra mangling scope for in-class
13978 initializers of class members and initializers of static data
13979 member templates. The former is a C++0x feature which isn't
13980 implemented yet, and I expect it will involve deferring
13981 parsing of the initializer until end of class as with default
13982 arguments. So right here we only handle the latter. */
13983 if (!member_p && processing_template_decl)
13984 start_lambda_scope (decl);
13985 initializer = cp_parser_initializer (parser,
13987 &is_non_constant_init);
13988 if (!member_p && processing_template_decl)
13989 finish_lambda_scope ();
13993 /* The old parser allows attributes to appear after a parenthesized
13994 initializer. Mark Mitchell proposed removing this functionality
13995 on the GCC mailing lists on 2002-08-13. This parser accepts the
13996 attributes -- but ignores them. */
13997 if (cp_parser_allow_gnu_extensions_p (parser)
13998 && initialization_kind == CPP_OPEN_PAREN)
13999 if (cp_parser_attributes_opt (parser))
14000 warning (OPT_Wattributes,
14001 "attributes after parenthesized initializer ignored");
14003 /* For an in-class declaration, use `grokfield' to create the
14009 pop_scope (pushed_scope);
14010 pushed_scope = false;
14012 decl = grokfield (declarator, decl_specifiers,
14013 initializer, !is_non_constant_init,
14014 /*asmspec=*/NULL_TREE,
14015 prefix_attributes);
14016 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14017 cp_parser_save_default_args (parser, decl);
14020 /* Finish processing the declaration. But, skip friend
14022 if (!friend_p && decl && decl != error_mark_node)
14024 cp_finish_decl (decl,
14025 initializer, !is_non_constant_init,
14027 /* If the initializer is in parentheses, then this is
14028 a direct-initialization, which means that an
14029 `explicit' constructor is OK. Otherwise, an
14030 `explicit' constructor cannot be used. */
14031 ((is_direct_init || !is_initialized)
14032 ? 0 : LOOKUP_ONLYCONVERTING));
14034 else if ((cxx_dialect != cxx98) && friend_p
14035 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14036 /* Core issue #226 (C++0x only): A default template-argument
14037 shall not be specified in a friend class template
14039 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14040 /*is_partial=*/0, /*is_friend_decl=*/1);
14042 if (!friend_p && pushed_scope)
14043 pop_scope (pushed_scope);
14048 /* Parse a declarator.
14052 ptr-operator declarator
14054 abstract-declarator:
14055 ptr-operator abstract-declarator [opt]
14056 direct-abstract-declarator
14061 attributes [opt] direct-declarator
14062 attributes [opt] ptr-operator declarator
14064 abstract-declarator:
14065 attributes [opt] ptr-operator abstract-declarator [opt]
14066 attributes [opt] direct-abstract-declarator
14068 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14069 detect constructor, destructor or conversion operators. It is set
14070 to -1 if the declarator is a name, and +1 if it is a
14071 function. Otherwise it is set to zero. Usually you just want to
14072 test for >0, but internally the negative value is used.
14074 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14075 a decl-specifier-seq unless it declares a constructor, destructor,
14076 or conversion. It might seem that we could check this condition in
14077 semantic analysis, rather than parsing, but that makes it difficult
14078 to handle something like `f()'. We want to notice that there are
14079 no decl-specifiers, and therefore realize that this is an
14080 expression, not a declaration.)
14082 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14083 the declarator is a direct-declarator of the form "(...)".
14085 MEMBER_P is true iff this declarator is a member-declarator. */
14087 static cp_declarator *
14088 cp_parser_declarator (cp_parser* parser,
14089 cp_parser_declarator_kind dcl_kind,
14090 int* ctor_dtor_or_conv_p,
14091 bool* parenthesized_p,
14094 cp_declarator *declarator;
14095 enum tree_code code;
14096 cp_cv_quals cv_quals;
14098 tree attributes = NULL_TREE;
14100 /* Assume this is not a constructor, destructor, or type-conversion
14102 if (ctor_dtor_or_conv_p)
14103 *ctor_dtor_or_conv_p = 0;
14105 if (cp_parser_allow_gnu_extensions_p (parser))
14106 attributes = cp_parser_attributes_opt (parser);
14108 /* Check for the ptr-operator production. */
14109 cp_parser_parse_tentatively (parser);
14110 /* Parse the ptr-operator. */
14111 code = cp_parser_ptr_operator (parser,
14114 /* If that worked, then we have a ptr-operator. */
14115 if (cp_parser_parse_definitely (parser))
14117 /* If a ptr-operator was found, then this declarator was not
14119 if (parenthesized_p)
14120 *parenthesized_p = true;
14121 /* The dependent declarator is optional if we are parsing an
14122 abstract-declarator. */
14123 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14124 cp_parser_parse_tentatively (parser);
14126 /* Parse the dependent declarator. */
14127 declarator = cp_parser_declarator (parser, dcl_kind,
14128 /*ctor_dtor_or_conv_p=*/NULL,
14129 /*parenthesized_p=*/NULL,
14130 /*member_p=*/false);
14132 /* If we are parsing an abstract-declarator, we must handle the
14133 case where the dependent declarator is absent. */
14134 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14135 && !cp_parser_parse_definitely (parser))
14138 declarator = cp_parser_make_indirect_declarator
14139 (code, class_type, cv_quals, declarator);
14141 /* Everything else is a direct-declarator. */
14144 if (parenthesized_p)
14145 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14147 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14148 ctor_dtor_or_conv_p,
14152 if (attributes && declarator && declarator != cp_error_declarator)
14153 declarator->attributes = attributes;
14158 /* Parse a direct-declarator or direct-abstract-declarator.
14162 direct-declarator ( parameter-declaration-clause )
14163 cv-qualifier-seq [opt]
14164 exception-specification [opt]
14165 direct-declarator [ constant-expression [opt] ]
14168 direct-abstract-declarator:
14169 direct-abstract-declarator [opt]
14170 ( parameter-declaration-clause )
14171 cv-qualifier-seq [opt]
14172 exception-specification [opt]
14173 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14174 ( abstract-declarator )
14176 Returns a representation of the declarator. DCL_KIND is
14177 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14178 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14179 we are parsing a direct-declarator. It is
14180 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14181 of ambiguity we prefer an abstract declarator, as per
14182 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14183 cp_parser_declarator. */
14185 static cp_declarator *
14186 cp_parser_direct_declarator (cp_parser* parser,
14187 cp_parser_declarator_kind dcl_kind,
14188 int* ctor_dtor_or_conv_p,
14192 cp_declarator *declarator = NULL;
14193 tree scope = NULL_TREE;
14194 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14195 bool saved_in_declarator_p = parser->in_declarator_p;
14197 tree pushed_scope = NULL_TREE;
14201 /* Peek at the next token. */
14202 token = cp_lexer_peek_token (parser->lexer);
14203 if (token->type == CPP_OPEN_PAREN)
14205 /* This is either a parameter-declaration-clause, or a
14206 parenthesized declarator. When we know we are parsing a
14207 named declarator, it must be a parenthesized declarator
14208 if FIRST is true. For instance, `(int)' is a
14209 parameter-declaration-clause, with an omitted
14210 direct-abstract-declarator. But `((*))', is a
14211 parenthesized abstract declarator. Finally, when T is a
14212 template parameter `(T)' is a
14213 parameter-declaration-clause, and not a parenthesized
14216 We first try and parse a parameter-declaration-clause,
14217 and then try a nested declarator (if FIRST is true).
14219 It is not an error for it not to be a
14220 parameter-declaration-clause, even when FIRST is
14226 The first is the declaration of a function while the
14227 second is the definition of a variable, including its
14230 Having seen only the parenthesis, we cannot know which of
14231 these two alternatives should be selected. Even more
14232 complex are examples like:
14237 The former is a function-declaration; the latter is a
14238 variable initialization.
14240 Thus again, we try a parameter-declaration-clause, and if
14241 that fails, we back out and return. */
14243 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14246 unsigned saved_num_template_parameter_lists;
14247 bool is_declarator = false;
14250 /* In a member-declarator, the only valid interpretation
14251 of a parenthesis is the start of a
14252 parameter-declaration-clause. (It is invalid to
14253 initialize a static data member with a parenthesized
14254 initializer; only the "=" form of initialization is
14257 cp_parser_parse_tentatively (parser);
14259 /* Consume the `('. */
14260 cp_lexer_consume_token (parser->lexer);
14263 /* If this is going to be an abstract declarator, we're
14264 in a declarator and we can't have default args. */
14265 parser->default_arg_ok_p = false;
14266 parser->in_declarator_p = true;
14269 /* Inside the function parameter list, surrounding
14270 template-parameter-lists do not apply. */
14271 saved_num_template_parameter_lists
14272 = parser->num_template_parameter_lists;
14273 parser->num_template_parameter_lists = 0;
14275 begin_scope (sk_function_parms, NULL_TREE);
14277 /* Parse the parameter-declaration-clause. */
14278 params = cp_parser_parameter_declaration_clause (parser);
14280 parser->num_template_parameter_lists
14281 = saved_num_template_parameter_lists;
14283 /* If all went well, parse the cv-qualifier-seq and the
14284 exception-specification. */
14285 if (member_p || cp_parser_parse_definitely (parser))
14287 cp_cv_quals cv_quals;
14288 tree exception_specification;
14291 is_declarator = true;
14293 if (ctor_dtor_or_conv_p)
14294 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14296 /* Consume the `)'. */
14297 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14299 /* Parse the cv-qualifier-seq. */
14300 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14301 /* And the exception-specification. */
14302 exception_specification
14303 = cp_parser_exception_specification_opt (parser);
14306 = cp_parser_late_return_type_opt (parser);
14308 /* Create the function-declarator. */
14309 declarator = make_call_declarator (declarator,
14312 exception_specification,
14314 /* Any subsequent parameter lists are to do with
14315 return type, so are not those of the declared
14317 parser->default_arg_ok_p = false;
14320 /* Remove the function parms from scope. */
14321 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14322 pop_binding (DECL_NAME (t), t);
14326 /* Repeat the main loop. */
14330 /* If this is the first, we can try a parenthesized
14334 bool saved_in_type_id_in_expr_p;
14336 parser->default_arg_ok_p = saved_default_arg_ok_p;
14337 parser->in_declarator_p = saved_in_declarator_p;
14339 /* Consume the `('. */
14340 cp_lexer_consume_token (parser->lexer);
14341 /* Parse the nested declarator. */
14342 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14343 parser->in_type_id_in_expr_p = true;
14345 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14346 /*parenthesized_p=*/NULL,
14348 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14350 /* Expect a `)'. */
14351 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14352 declarator = cp_error_declarator;
14353 if (declarator == cp_error_declarator)
14356 goto handle_declarator;
14358 /* Otherwise, we must be done. */
14362 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14363 && token->type == CPP_OPEN_SQUARE)
14365 /* Parse an array-declarator. */
14368 if (ctor_dtor_or_conv_p)
14369 *ctor_dtor_or_conv_p = 0;
14372 parser->default_arg_ok_p = false;
14373 parser->in_declarator_p = true;
14374 /* Consume the `['. */
14375 cp_lexer_consume_token (parser->lexer);
14376 /* Peek at the next token. */
14377 token = cp_lexer_peek_token (parser->lexer);
14378 /* If the next token is `]', then there is no
14379 constant-expression. */
14380 if (token->type != CPP_CLOSE_SQUARE)
14382 bool non_constant_p;
14385 = cp_parser_constant_expression (parser,
14386 /*allow_non_constant=*/true,
14388 if (!non_constant_p)
14389 bounds = fold_non_dependent_expr (bounds);
14390 /* Normally, the array bound must be an integral constant
14391 expression. However, as an extension, we allow VLAs
14392 in function scopes as long as they aren't part of a
14393 parameter declaration. */
14394 else if (!parser->in_function_body
14395 || current_binding_level->kind == sk_function_parms)
14397 cp_parser_error (parser,
14398 "array bound is not an integer constant");
14399 bounds = error_mark_node;
14401 else if (processing_template_decl && !error_operand_p (bounds))
14403 /* Remember this wasn't a constant-expression. */
14404 bounds = build_nop (TREE_TYPE (bounds), bounds);
14405 TREE_SIDE_EFFECTS (bounds) = 1;
14409 bounds = NULL_TREE;
14410 /* Look for the closing `]'. */
14411 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14413 declarator = cp_error_declarator;
14417 declarator = make_array_declarator (declarator, bounds);
14419 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14422 tree qualifying_scope;
14423 tree unqualified_name;
14424 special_function_kind sfk;
14426 bool pack_expansion_p = false;
14427 cp_token *declarator_id_start_token;
14429 /* Parse a declarator-id */
14430 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14433 cp_parser_parse_tentatively (parser);
14435 /* If we see an ellipsis, we should be looking at a
14437 if (token->type == CPP_ELLIPSIS)
14439 /* Consume the `...' */
14440 cp_lexer_consume_token (parser->lexer);
14442 pack_expansion_p = true;
14446 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14448 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14449 qualifying_scope = parser->scope;
14454 if (!unqualified_name && pack_expansion_p)
14456 /* Check whether an error occurred. */
14457 okay = !cp_parser_error_occurred (parser);
14459 /* We already consumed the ellipsis to mark a
14460 parameter pack, but we have no way to report it,
14461 so abort the tentative parse. We will be exiting
14462 immediately anyway. */
14463 cp_parser_abort_tentative_parse (parser);
14466 okay = cp_parser_parse_definitely (parser);
14469 unqualified_name = error_mark_node;
14470 else if (unqualified_name
14471 && (qualifying_scope
14472 || (TREE_CODE (unqualified_name)
14473 != IDENTIFIER_NODE)))
14475 cp_parser_error (parser, "expected unqualified-id");
14476 unqualified_name = error_mark_node;
14480 if (!unqualified_name)
14482 if (unqualified_name == error_mark_node)
14484 declarator = cp_error_declarator;
14485 pack_expansion_p = false;
14486 declarator->parameter_pack_p = false;
14490 if (qualifying_scope && at_namespace_scope_p ()
14491 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14493 /* In the declaration of a member of a template class
14494 outside of the class itself, the SCOPE will sometimes
14495 be a TYPENAME_TYPE. For example, given:
14497 template <typename T>
14498 int S<T>::R::i = 3;
14500 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14501 this context, we must resolve S<T>::R to an ordinary
14502 type, rather than a typename type.
14504 The reason we normally avoid resolving TYPENAME_TYPEs
14505 is that a specialization of `S' might render
14506 `S<T>::R' not a type. However, if `S' is
14507 specialized, then this `i' will not be used, so there
14508 is no harm in resolving the types here. */
14511 /* Resolve the TYPENAME_TYPE. */
14512 type = resolve_typename_type (qualifying_scope,
14513 /*only_current_p=*/false);
14514 /* If that failed, the declarator is invalid. */
14515 if (TREE_CODE (type) == TYPENAME_TYPE)
14517 if (typedef_variant_p (type))
14518 error_at (declarator_id_start_token->location,
14519 "cannot define member of dependent typedef "
14522 error_at (declarator_id_start_token->location,
14523 "%<%T::%E%> is not a type",
14524 TYPE_CONTEXT (qualifying_scope),
14525 TYPE_IDENTIFIER (qualifying_scope));
14527 qualifying_scope = type;
14532 if (unqualified_name)
14536 if (qualifying_scope
14537 && CLASS_TYPE_P (qualifying_scope))
14538 class_type = qualifying_scope;
14540 class_type = current_class_type;
14542 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14544 tree name_type = TREE_TYPE (unqualified_name);
14545 if (class_type && same_type_p (name_type, class_type))
14547 if (qualifying_scope
14548 && CLASSTYPE_USE_TEMPLATE (name_type))
14550 error_at (declarator_id_start_token->location,
14551 "invalid use of constructor as a template");
14552 inform (declarator_id_start_token->location,
14553 "use %<%T::%D%> instead of %<%T::%D%> to "
14554 "name the constructor in a qualified name",
14556 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14557 class_type, name_type);
14558 declarator = cp_error_declarator;
14562 unqualified_name = constructor_name (class_type);
14566 /* We do not attempt to print the declarator
14567 here because we do not have enough
14568 information about its original syntactic
14570 cp_parser_error (parser, "invalid declarator");
14571 declarator = cp_error_declarator;
14578 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14579 sfk = sfk_destructor;
14580 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14581 sfk = sfk_conversion;
14582 else if (/* There's no way to declare a constructor
14583 for an anonymous type, even if the type
14584 got a name for linkage purposes. */
14585 !TYPE_WAS_ANONYMOUS (class_type)
14586 && constructor_name_p (unqualified_name,
14589 unqualified_name = constructor_name (class_type);
14590 sfk = sfk_constructor;
14592 else if (is_overloaded_fn (unqualified_name)
14593 && DECL_CONSTRUCTOR_P (get_first_fn
14594 (unqualified_name)))
14595 sfk = sfk_constructor;
14597 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14598 *ctor_dtor_or_conv_p = -1;
14601 declarator = make_id_declarator (qualifying_scope,
14604 declarator->id_loc = token->location;
14605 declarator->parameter_pack_p = pack_expansion_p;
14607 if (pack_expansion_p)
14608 maybe_warn_variadic_templates ();
14611 handle_declarator:;
14612 scope = get_scope_of_declarator (declarator);
14614 /* Any names that appear after the declarator-id for a
14615 member are looked up in the containing scope. */
14616 pushed_scope = push_scope (scope);
14617 parser->in_declarator_p = true;
14618 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14619 || (declarator && declarator->kind == cdk_id))
14620 /* Default args are only allowed on function
14622 parser->default_arg_ok_p = saved_default_arg_ok_p;
14624 parser->default_arg_ok_p = false;
14633 /* For an abstract declarator, we might wind up with nothing at this
14634 point. That's an error; the declarator is not optional. */
14636 cp_parser_error (parser, "expected declarator");
14638 /* If we entered a scope, we must exit it now. */
14640 pop_scope (pushed_scope);
14642 parser->default_arg_ok_p = saved_default_arg_ok_p;
14643 parser->in_declarator_p = saved_in_declarator_p;
14648 /* Parse a ptr-operator.
14651 * cv-qualifier-seq [opt]
14653 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14658 & cv-qualifier-seq [opt]
14660 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14661 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14662 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14663 filled in with the TYPE containing the member. *CV_QUALS is
14664 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14665 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14666 Note that the tree codes returned by this function have nothing
14667 to do with the types of trees that will be eventually be created
14668 to represent the pointer or reference type being parsed. They are
14669 just constants with suggestive names. */
14670 static enum tree_code
14671 cp_parser_ptr_operator (cp_parser* parser,
14673 cp_cv_quals *cv_quals)
14675 enum tree_code code = ERROR_MARK;
14678 /* Assume that it's not a pointer-to-member. */
14680 /* And that there are no cv-qualifiers. */
14681 *cv_quals = TYPE_UNQUALIFIED;
14683 /* Peek at the next token. */
14684 token = cp_lexer_peek_token (parser->lexer);
14686 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14687 if (token->type == CPP_MULT)
14688 code = INDIRECT_REF;
14689 else if (token->type == CPP_AND)
14691 else if ((cxx_dialect != cxx98) &&
14692 token->type == CPP_AND_AND) /* C++0x only */
14693 code = NON_LVALUE_EXPR;
14695 if (code != ERROR_MARK)
14697 /* Consume the `*', `&' or `&&'. */
14698 cp_lexer_consume_token (parser->lexer);
14700 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14701 `&', if we are allowing GNU extensions. (The only qualifier
14702 that can legally appear after `&' is `restrict', but that is
14703 enforced during semantic analysis. */
14704 if (code == INDIRECT_REF
14705 || cp_parser_allow_gnu_extensions_p (parser))
14706 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14710 /* Try the pointer-to-member case. */
14711 cp_parser_parse_tentatively (parser);
14712 /* Look for the optional `::' operator. */
14713 cp_parser_global_scope_opt (parser,
14714 /*current_scope_valid_p=*/false);
14715 /* Look for the nested-name specifier. */
14716 token = cp_lexer_peek_token (parser->lexer);
14717 cp_parser_nested_name_specifier (parser,
14718 /*typename_keyword_p=*/false,
14719 /*check_dependency_p=*/true,
14721 /*is_declaration=*/false);
14722 /* If we found it, and the next token is a `*', then we are
14723 indeed looking at a pointer-to-member operator. */
14724 if (!cp_parser_error_occurred (parser)
14725 && cp_parser_require (parser, CPP_MULT, RT_MULT))
14727 /* Indicate that the `*' operator was used. */
14728 code = INDIRECT_REF;
14730 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14731 error_at (token->location, "%qD is a namespace", parser->scope);
14734 /* The type of which the member is a member is given by the
14736 *type = parser->scope;
14737 /* The next name will not be qualified. */
14738 parser->scope = NULL_TREE;
14739 parser->qualifying_scope = NULL_TREE;
14740 parser->object_scope = NULL_TREE;
14741 /* Look for the optional cv-qualifier-seq. */
14742 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14745 /* If that didn't work we don't have a ptr-operator. */
14746 if (!cp_parser_parse_definitely (parser))
14747 cp_parser_error (parser, "expected ptr-operator");
14753 /* Parse an (optional) cv-qualifier-seq.
14756 cv-qualifier cv-qualifier-seq [opt]
14767 Returns a bitmask representing the cv-qualifiers. */
14770 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14772 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14777 cp_cv_quals cv_qualifier;
14779 /* Peek at the next token. */
14780 token = cp_lexer_peek_token (parser->lexer);
14781 /* See if it's a cv-qualifier. */
14782 switch (token->keyword)
14785 cv_qualifier = TYPE_QUAL_CONST;
14789 cv_qualifier = TYPE_QUAL_VOLATILE;
14793 cv_qualifier = TYPE_QUAL_RESTRICT;
14797 cv_qualifier = TYPE_UNQUALIFIED;
14804 if (cv_quals & cv_qualifier)
14806 error_at (token->location, "duplicate cv-qualifier");
14807 cp_lexer_purge_token (parser->lexer);
14811 cp_lexer_consume_token (parser->lexer);
14812 cv_quals |= cv_qualifier;
14819 /* Parse a late-specified return type, if any. This is not a separate
14820 non-terminal, but part of a function declarator, which looks like
14822 -> trailing-type-specifier-seq abstract-declarator(opt)
14824 Returns the type indicated by the type-id. */
14827 cp_parser_late_return_type_opt (cp_parser* parser)
14831 /* Peek at the next token. */
14832 token = cp_lexer_peek_token (parser->lexer);
14833 /* A late-specified return type is indicated by an initial '->'. */
14834 if (token->type != CPP_DEREF)
14837 /* Consume the ->. */
14838 cp_lexer_consume_token (parser->lexer);
14840 return cp_parser_trailing_type_id (parser);
14843 /* Parse a declarator-id.
14847 :: [opt] nested-name-specifier [opt] type-name
14849 In the `id-expression' case, the value returned is as for
14850 cp_parser_id_expression if the id-expression was an unqualified-id.
14851 If the id-expression was a qualified-id, then a SCOPE_REF is
14852 returned. The first operand is the scope (either a NAMESPACE_DECL
14853 or TREE_TYPE), but the second is still just a representation of an
14857 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14860 /* The expression must be an id-expression. Assume that qualified
14861 names are the names of types so that:
14864 int S<T>::R::i = 3;
14866 will work; we must treat `S<T>::R' as the name of a type.
14867 Similarly, assume that qualified names are templates, where
14871 int S<T>::R<T>::i = 3;
14874 id = cp_parser_id_expression (parser,
14875 /*template_keyword_p=*/false,
14876 /*check_dependency_p=*/false,
14877 /*template_p=*/NULL,
14878 /*declarator_p=*/true,
14880 if (id && BASELINK_P (id))
14881 id = BASELINK_FUNCTIONS (id);
14885 /* Parse a type-id.
14888 type-specifier-seq abstract-declarator [opt]
14890 Returns the TYPE specified. */
14893 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14894 bool is_trailing_return)
14896 cp_decl_specifier_seq type_specifier_seq;
14897 cp_declarator *abstract_declarator;
14899 /* Parse the type-specifier-seq. */
14900 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14901 is_trailing_return,
14902 &type_specifier_seq);
14903 if (type_specifier_seq.type == error_mark_node)
14904 return error_mark_node;
14906 /* There might or might not be an abstract declarator. */
14907 cp_parser_parse_tentatively (parser);
14908 /* Look for the declarator. */
14909 abstract_declarator
14910 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14911 /*parenthesized_p=*/NULL,
14912 /*member_p=*/false);
14913 /* Check to see if there really was a declarator. */
14914 if (!cp_parser_parse_definitely (parser))
14915 abstract_declarator = NULL;
14917 if (type_specifier_seq.type
14918 && type_uses_auto (type_specifier_seq.type))
14920 /* A type-id with type 'auto' is only ok if the abstract declarator
14921 is a function declarator with a late-specified return type. */
14922 if (abstract_declarator
14923 && abstract_declarator->kind == cdk_function
14924 && abstract_declarator->u.function.late_return_type)
14928 error ("invalid use of %<auto%>");
14929 return error_mark_node;
14933 return groktypename (&type_specifier_seq, abstract_declarator,
14937 static tree cp_parser_type_id (cp_parser *parser)
14939 return cp_parser_type_id_1 (parser, false, false);
14942 static tree cp_parser_template_type_arg (cp_parser *parser)
14944 return cp_parser_type_id_1 (parser, true, false);
14947 static tree cp_parser_trailing_type_id (cp_parser *parser)
14949 return cp_parser_type_id_1 (parser, false, true);
14952 /* Parse a type-specifier-seq.
14954 type-specifier-seq:
14955 type-specifier type-specifier-seq [opt]
14959 type-specifier-seq:
14960 attributes type-specifier-seq [opt]
14962 If IS_DECLARATION is true, we are at the start of a "condition" or
14963 exception-declaration, so we might be followed by a declarator-id.
14965 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14966 i.e. we've just seen "->".
14968 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14971 cp_parser_type_specifier_seq (cp_parser* parser,
14972 bool is_declaration,
14973 bool is_trailing_return,
14974 cp_decl_specifier_seq *type_specifier_seq)
14976 bool seen_type_specifier = false;
14977 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14978 cp_token *start_token = NULL;
14980 /* Clear the TYPE_SPECIFIER_SEQ. */
14981 clear_decl_specs (type_specifier_seq);
14983 /* In the context of a trailing return type, enum E { } is an
14984 elaborated-type-specifier followed by a function-body, not an
14986 if (is_trailing_return)
14987 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14989 /* Parse the type-specifiers and attributes. */
14992 tree type_specifier;
14993 bool is_cv_qualifier;
14995 /* Check for attributes first. */
14996 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14998 type_specifier_seq->attributes =
14999 chainon (type_specifier_seq->attributes,
15000 cp_parser_attributes_opt (parser));
15004 /* record the token of the beginning of the type specifier seq,
15005 for error reporting purposes*/
15007 start_token = cp_lexer_peek_token (parser->lexer);
15009 /* Look for the type-specifier. */
15010 type_specifier = cp_parser_type_specifier (parser,
15012 type_specifier_seq,
15013 /*is_declaration=*/false,
15016 if (!type_specifier)
15018 /* If the first type-specifier could not be found, this is not a
15019 type-specifier-seq at all. */
15020 if (!seen_type_specifier)
15022 cp_parser_error (parser, "expected type-specifier");
15023 type_specifier_seq->type = error_mark_node;
15026 /* If subsequent type-specifiers could not be found, the
15027 type-specifier-seq is complete. */
15031 seen_type_specifier = true;
15032 /* The standard says that a condition can be:
15034 type-specifier-seq declarator = assignment-expression
15041 we should treat the "S" as a declarator, not as a
15042 type-specifier. The standard doesn't say that explicitly for
15043 type-specifier-seq, but it does say that for
15044 decl-specifier-seq in an ordinary declaration. Perhaps it
15045 would be clearer just to allow a decl-specifier-seq here, and
15046 then add a semantic restriction that if any decl-specifiers
15047 that are not type-specifiers appear, the program is invalid. */
15048 if (is_declaration && !is_cv_qualifier)
15049 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15052 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15055 /* Parse a parameter-declaration-clause.
15057 parameter-declaration-clause:
15058 parameter-declaration-list [opt] ... [opt]
15059 parameter-declaration-list , ...
15061 Returns a representation for the parameter declarations. A return
15062 value of NULL indicates a parameter-declaration-clause consisting
15063 only of an ellipsis. */
15066 cp_parser_parameter_declaration_clause (cp_parser* parser)
15073 /* Peek at the next token. */
15074 token = cp_lexer_peek_token (parser->lexer);
15075 /* Check for trivial parameter-declaration-clauses. */
15076 if (token->type == CPP_ELLIPSIS)
15078 /* Consume the `...' token. */
15079 cp_lexer_consume_token (parser->lexer);
15082 else if (token->type == CPP_CLOSE_PAREN)
15083 /* There are no parameters. */
15085 #ifndef NO_IMPLICIT_EXTERN_C
15086 if (in_system_header && current_class_type == NULL
15087 && current_lang_name == lang_name_c)
15091 return void_list_node;
15093 /* Check for `(void)', too, which is a special case. */
15094 else if (token->keyword == RID_VOID
15095 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15096 == CPP_CLOSE_PAREN))
15098 /* Consume the `void' token. */
15099 cp_lexer_consume_token (parser->lexer);
15100 /* There are no parameters. */
15101 return void_list_node;
15104 /* Parse the parameter-declaration-list. */
15105 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15106 /* If a parse error occurred while parsing the
15107 parameter-declaration-list, then the entire
15108 parameter-declaration-clause is erroneous. */
15112 /* Peek at the next token. */
15113 token = cp_lexer_peek_token (parser->lexer);
15114 /* If it's a `,', the clause should terminate with an ellipsis. */
15115 if (token->type == CPP_COMMA)
15117 /* Consume the `,'. */
15118 cp_lexer_consume_token (parser->lexer);
15119 /* Expect an ellipsis. */
15121 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15123 /* It might also be `...' if the optional trailing `,' was
15125 else if (token->type == CPP_ELLIPSIS)
15127 /* Consume the `...' token. */
15128 cp_lexer_consume_token (parser->lexer);
15129 /* And remember that we saw it. */
15133 ellipsis_p = false;
15135 /* Finish the parameter list. */
15137 parameters = chainon (parameters, void_list_node);
15142 /* Parse a parameter-declaration-list.
15144 parameter-declaration-list:
15145 parameter-declaration
15146 parameter-declaration-list , parameter-declaration
15148 Returns a representation of the parameter-declaration-list, as for
15149 cp_parser_parameter_declaration_clause. However, the
15150 `void_list_node' is never appended to the list. Upon return,
15151 *IS_ERROR will be true iff an error occurred. */
15154 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15156 tree parameters = NULL_TREE;
15157 tree *tail = ¶meters;
15158 bool saved_in_unbraced_linkage_specification_p;
15161 /* Assume all will go well. */
15163 /* The special considerations that apply to a function within an
15164 unbraced linkage specifications do not apply to the parameters
15165 to the function. */
15166 saved_in_unbraced_linkage_specification_p
15167 = parser->in_unbraced_linkage_specification_p;
15168 parser->in_unbraced_linkage_specification_p = false;
15170 /* Look for more parameters. */
15173 cp_parameter_declarator *parameter;
15174 tree decl = error_mark_node;
15175 bool parenthesized_p;
15176 /* Parse the parameter. */
15178 = cp_parser_parameter_declaration (parser,
15179 /*template_parm_p=*/false,
15182 /* We don't know yet if the enclosing context is deprecated, so wait
15183 and warn in grokparms if appropriate. */
15184 deprecated_state = DEPRECATED_SUPPRESS;
15187 decl = grokdeclarator (parameter->declarator,
15188 ¶meter->decl_specifiers,
15190 parameter->default_argument != NULL_TREE,
15191 ¶meter->decl_specifiers.attributes);
15193 deprecated_state = DEPRECATED_NORMAL;
15195 /* If a parse error occurred parsing the parameter declaration,
15196 then the entire parameter-declaration-list is erroneous. */
15197 if (decl == error_mark_node)
15200 parameters = error_mark_node;
15204 if (parameter->decl_specifiers.attributes)
15205 cplus_decl_attributes (&decl,
15206 parameter->decl_specifiers.attributes,
15208 if (DECL_NAME (decl))
15209 decl = pushdecl (decl);
15211 if (decl != error_mark_node)
15213 retrofit_lang_decl (decl);
15214 DECL_PARM_INDEX (decl) = ++index;
15217 /* Add the new parameter to the list. */
15218 *tail = build_tree_list (parameter->default_argument, decl);
15219 tail = &TREE_CHAIN (*tail);
15221 /* Peek at the next token. */
15222 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15223 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15224 /* These are for Objective-C++ */
15225 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15226 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15227 /* The parameter-declaration-list is complete. */
15229 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15233 /* Peek at the next token. */
15234 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15235 /* If it's an ellipsis, then the list is complete. */
15236 if (token->type == CPP_ELLIPSIS)
15238 /* Otherwise, there must be more parameters. Consume the
15240 cp_lexer_consume_token (parser->lexer);
15241 /* When parsing something like:
15243 int i(float f, double d)
15245 we can tell after seeing the declaration for "f" that we
15246 are not looking at an initialization of a variable "i",
15247 but rather at the declaration of a function "i".
15249 Due to the fact that the parsing of template arguments
15250 (as specified to a template-id) requires backtracking we
15251 cannot use this technique when inside a template argument
15253 if (!parser->in_template_argument_list_p
15254 && !parser->in_type_id_in_expr_p
15255 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15256 /* However, a parameter-declaration of the form
15257 "foat(f)" (which is a valid declaration of a
15258 parameter "f") can also be interpreted as an
15259 expression (the conversion of "f" to "float"). */
15260 && !parenthesized_p)
15261 cp_parser_commit_to_tentative_parse (parser);
15265 cp_parser_error (parser, "expected %<,%> or %<...%>");
15266 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15267 cp_parser_skip_to_closing_parenthesis (parser,
15268 /*recovering=*/true,
15269 /*or_comma=*/false,
15270 /*consume_paren=*/false);
15275 parser->in_unbraced_linkage_specification_p
15276 = saved_in_unbraced_linkage_specification_p;
15281 /* Parse a parameter declaration.
15283 parameter-declaration:
15284 decl-specifier-seq ... [opt] declarator
15285 decl-specifier-seq declarator = assignment-expression
15286 decl-specifier-seq ... [opt] abstract-declarator [opt]
15287 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15289 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15290 declares a template parameter. (In that case, a non-nested `>'
15291 token encountered during the parsing of the assignment-expression
15292 is not interpreted as a greater-than operator.)
15294 Returns a representation of the parameter, or NULL if an error
15295 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15296 true iff the declarator is of the form "(p)". */
15298 static cp_parameter_declarator *
15299 cp_parser_parameter_declaration (cp_parser *parser,
15300 bool template_parm_p,
15301 bool *parenthesized_p)
15303 int declares_class_or_enum;
15304 cp_decl_specifier_seq decl_specifiers;
15305 cp_declarator *declarator;
15306 tree default_argument;
15307 cp_token *token = NULL, *declarator_token_start = NULL;
15308 const char *saved_message;
15310 /* In a template parameter, `>' is not an operator.
15314 When parsing a default template-argument for a non-type
15315 template-parameter, the first non-nested `>' is taken as the end
15316 of the template parameter-list rather than a greater-than
15319 /* Type definitions may not appear in parameter types. */
15320 saved_message = parser->type_definition_forbidden_message;
15321 parser->type_definition_forbidden_message
15322 = G_("types may not be defined in parameter types");
15324 /* Parse the declaration-specifiers. */
15325 cp_parser_decl_specifier_seq (parser,
15326 CP_PARSER_FLAGS_NONE,
15328 &declares_class_or_enum);
15330 /* Complain about missing 'typename' or other invalid type names. */
15331 if (!decl_specifiers.any_type_specifiers_p)
15332 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15334 /* If an error occurred, there's no reason to attempt to parse the
15335 rest of the declaration. */
15336 if (cp_parser_error_occurred (parser))
15338 parser->type_definition_forbidden_message = saved_message;
15342 /* Peek at the next token. */
15343 token = cp_lexer_peek_token (parser->lexer);
15345 /* If the next token is a `)', `,', `=', `>', or `...', then there
15346 is no declarator. However, when variadic templates are enabled,
15347 there may be a declarator following `...'. */
15348 if (token->type == CPP_CLOSE_PAREN
15349 || token->type == CPP_COMMA
15350 || token->type == CPP_EQ
15351 || token->type == CPP_GREATER)
15354 if (parenthesized_p)
15355 *parenthesized_p = false;
15357 /* Otherwise, there should be a declarator. */
15360 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15361 parser->default_arg_ok_p = false;
15363 /* After seeing a decl-specifier-seq, if the next token is not a
15364 "(", there is no possibility that the code is a valid
15365 expression. Therefore, if parsing tentatively, we commit at
15367 if (!parser->in_template_argument_list_p
15368 /* In an expression context, having seen:
15372 we cannot be sure whether we are looking at a
15373 function-type (taking a "char" as a parameter) or a cast
15374 of some object of type "char" to "int". */
15375 && !parser->in_type_id_in_expr_p
15376 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15377 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15378 cp_parser_commit_to_tentative_parse (parser);
15379 /* Parse the declarator. */
15380 declarator_token_start = token;
15381 declarator = cp_parser_declarator (parser,
15382 CP_PARSER_DECLARATOR_EITHER,
15383 /*ctor_dtor_or_conv_p=*/NULL,
15385 /*member_p=*/false);
15386 parser->default_arg_ok_p = saved_default_arg_ok_p;
15387 /* After the declarator, allow more attributes. */
15388 decl_specifiers.attributes
15389 = chainon (decl_specifiers.attributes,
15390 cp_parser_attributes_opt (parser));
15393 /* If the next token is an ellipsis, and we have not seen a
15394 declarator name, and the type of the declarator contains parameter
15395 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15396 a parameter pack expansion expression. Otherwise, leave the
15397 ellipsis for a C-style variadic function. */
15398 token = cp_lexer_peek_token (parser->lexer);
15399 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15401 tree type = decl_specifiers.type;
15403 if (type && DECL_P (type))
15404 type = TREE_TYPE (type);
15407 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15408 && declarator_can_be_parameter_pack (declarator)
15409 && (!declarator || !declarator->parameter_pack_p)
15410 && uses_parameter_packs (type))
15412 /* Consume the `...'. */
15413 cp_lexer_consume_token (parser->lexer);
15414 maybe_warn_variadic_templates ();
15416 /* Build a pack expansion type */
15418 declarator->parameter_pack_p = true;
15420 decl_specifiers.type = make_pack_expansion (type);
15424 /* The restriction on defining new types applies only to the type
15425 of the parameter, not to the default argument. */
15426 parser->type_definition_forbidden_message = saved_message;
15428 /* If the next token is `=', then process a default argument. */
15429 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15431 /* Consume the `='. */
15432 cp_lexer_consume_token (parser->lexer);
15434 /* If we are defining a class, then the tokens that make up the
15435 default argument must be saved and processed later. */
15436 if (!template_parm_p && at_class_scope_p ()
15437 && TYPE_BEING_DEFINED (current_class_type)
15438 && !LAMBDA_TYPE_P (current_class_type))
15440 unsigned depth = 0;
15441 int maybe_template_id = 0;
15442 cp_token *first_token;
15445 /* Add tokens until we have processed the entire default
15446 argument. We add the range [first_token, token). */
15447 first_token = cp_lexer_peek_token (parser->lexer);
15452 /* Peek at the next token. */
15453 token = cp_lexer_peek_token (parser->lexer);
15454 /* What we do depends on what token we have. */
15455 switch (token->type)
15457 /* In valid code, a default argument must be
15458 immediately followed by a `,' `)', or `...'. */
15460 if (depth == 0 && maybe_template_id)
15462 /* If we've seen a '<', we might be in a
15463 template-argument-list. Until Core issue 325 is
15464 resolved, we don't know how this situation ought
15465 to be handled, so try to DTRT. We check whether
15466 what comes after the comma is a valid parameter
15467 declaration list. If it is, then the comma ends
15468 the default argument; otherwise the default
15469 argument continues. */
15470 bool error = false;
15472 /* Set ITALP so cp_parser_parameter_declaration_list
15473 doesn't decide to commit to this parse. */
15474 bool saved_italp = parser->in_template_argument_list_p;
15475 parser->in_template_argument_list_p = true;
15477 cp_parser_parse_tentatively (parser);
15478 cp_lexer_consume_token (parser->lexer);
15479 cp_parser_parameter_declaration_list (parser, &error);
15480 if (!cp_parser_error_occurred (parser) && !error)
15482 cp_parser_abort_tentative_parse (parser);
15484 parser->in_template_argument_list_p = saved_italp;
15487 case CPP_CLOSE_PAREN:
15489 /* If we run into a non-nested `;', `}', or `]',
15490 then the code is invalid -- but the default
15491 argument is certainly over. */
15492 case CPP_SEMICOLON:
15493 case CPP_CLOSE_BRACE:
15494 case CPP_CLOSE_SQUARE:
15497 /* Update DEPTH, if necessary. */
15498 else if (token->type == CPP_CLOSE_PAREN
15499 || token->type == CPP_CLOSE_BRACE
15500 || token->type == CPP_CLOSE_SQUARE)
15504 case CPP_OPEN_PAREN:
15505 case CPP_OPEN_SQUARE:
15506 case CPP_OPEN_BRACE:
15512 /* This might be the comparison operator, or it might
15513 start a template argument list. */
15514 ++maybe_template_id;
15518 if (cxx_dialect == cxx98)
15520 /* Fall through for C++0x, which treats the `>>'
15521 operator like two `>' tokens in certain
15527 /* This might be an operator, or it might close a
15528 template argument list. But if a previous '<'
15529 started a template argument list, this will have
15530 closed it, so we can't be in one anymore. */
15531 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15532 if (maybe_template_id < 0)
15533 maybe_template_id = 0;
15537 /* If we run out of tokens, issue an error message. */
15539 case CPP_PRAGMA_EOL:
15540 error_at (token->location, "file ends in default argument");
15546 /* In these cases, we should look for template-ids.
15547 For example, if the default argument is
15548 `X<int, double>()', we need to do name lookup to
15549 figure out whether or not `X' is a template; if
15550 so, the `,' does not end the default argument.
15552 That is not yet done. */
15559 /* If we've reached the end, stop. */
15563 /* Add the token to the token block. */
15564 token = cp_lexer_consume_token (parser->lexer);
15567 /* Create a DEFAULT_ARG to represent the unparsed default
15569 default_argument = make_node (DEFAULT_ARG);
15570 DEFARG_TOKENS (default_argument)
15571 = cp_token_cache_new (first_token, token);
15572 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15574 /* Outside of a class definition, we can just parse the
15575 assignment-expression. */
15578 token = cp_lexer_peek_token (parser->lexer);
15580 = cp_parser_default_argument (parser, template_parm_p);
15583 if (!parser->default_arg_ok_p)
15585 if (flag_permissive)
15586 warning (0, "deprecated use of default argument for parameter of non-function");
15589 error_at (token->location,
15590 "default arguments are only "
15591 "permitted for function parameters");
15592 default_argument = NULL_TREE;
15595 else if ((declarator && declarator->parameter_pack_p)
15596 || (decl_specifiers.type
15597 && PACK_EXPANSION_P (decl_specifiers.type)))
15599 /* Find the name of the parameter pack. */
15600 cp_declarator *id_declarator = declarator;
15601 while (id_declarator && id_declarator->kind != cdk_id)
15602 id_declarator = id_declarator->declarator;
15604 if (id_declarator && id_declarator->kind == cdk_id)
15605 error_at (declarator_token_start->location,
15607 ? "template parameter pack %qD"
15608 " cannot have a default argument"
15609 : "parameter pack %qD cannot have a default argument",
15610 id_declarator->u.id.unqualified_name);
15612 error_at (declarator_token_start->location,
15614 ? "template parameter pack cannot have a default argument"
15615 : "parameter pack cannot have a default argument");
15617 default_argument = NULL_TREE;
15621 default_argument = NULL_TREE;
15623 return make_parameter_declarator (&decl_specifiers,
15628 /* Parse a default argument and return it.
15630 TEMPLATE_PARM_P is true if this is a default argument for a
15631 non-type template parameter. */
15633 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15635 tree default_argument = NULL_TREE;
15636 bool saved_greater_than_is_operator_p;
15637 bool saved_local_variables_forbidden_p;
15639 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15641 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15642 parser->greater_than_is_operator_p = !template_parm_p;
15643 /* Local variable names (and the `this' keyword) may not
15644 appear in a default argument. */
15645 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15646 parser->local_variables_forbidden_p = true;
15647 /* Parse the assignment-expression. */
15648 if (template_parm_p)
15649 push_deferring_access_checks (dk_no_deferred);
15651 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15652 if (template_parm_p)
15653 pop_deferring_access_checks ();
15654 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15655 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15657 return default_argument;
15660 /* Parse a function-body.
15663 compound_statement */
15666 cp_parser_function_body (cp_parser *parser)
15668 cp_parser_compound_statement (parser, NULL, false);
15671 /* Parse a ctor-initializer-opt followed by a function-body. Return
15672 true if a ctor-initializer was present. */
15675 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15678 bool ctor_initializer_p;
15680 /* Begin the function body. */
15681 body = begin_function_body ();
15682 /* Parse the optional ctor-initializer. */
15683 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15684 /* Parse the function-body. */
15685 cp_parser_function_body (parser);
15686 /* Finish the function body. */
15687 finish_function_body (body);
15689 return ctor_initializer_p;
15692 /* Parse an initializer.
15695 = initializer-clause
15696 ( expression-list )
15698 Returns an expression representing the initializer. If no
15699 initializer is present, NULL_TREE is returned.
15701 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15702 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15703 set to TRUE if there is no initializer present. If there is an
15704 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15705 is set to true; otherwise it is set to false. */
15708 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15709 bool* non_constant_p)
15714 /* Peek at the next token. */
15715 token = cp_lexer_peek_token (parser->lexer);
15717 /* Let our caller know whether or not this initializer was
15719 *is_direct_init = (token->type != CPP_EQ);
15720 /* Assume that the initializer is constant. */
15721 *non_constant_p = false;
15723 if (token->type == CPP_EQ)
15725 /* Consume the `='. */
15726 cp_lexer_consume_token (parser->lexer);
15727 /* Parse the initializer-clause. */
15728 init = cp_parser_initializer_clause (parser, non_constant_p);
15730 else if (token->type == CPP_OPEN_PAREN)
15733 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15735 /*allow_expansion_p=*/true,
15738 return error_mark_node;
15739 init = build_tree_list_vec (vec);
15740 release_tree_vector (vec);
15742 else if (token->type == CPP_OPEN_BRACE)
15744 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15745 init = cp_parser_braced_list (parser, non_constant_p);
15746 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15750 /* Anything else is an error. */
15751 cp_parser_error (parser, "expected initializer");
15752 init = error_mark_node;
15758 /* Parse an initializer-clause.
15760 initializer-clause:
15761 assignment-expression
15764 Returns an expression representing the initializer.
15766 If the `assignment-expression' production is used the value
15767 returned is simply a representation for the expression.
15769 Otherwise, calls cp_parser_braced_list. */
15772 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15776 /* Assume the expression is constant. */
15777 *non_constant_p = false;
15779 /* If it is not a `{', then we are looking at an
15780 assignment-expression. */
15781 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15784 = cp_parser_constant_expression (parser,
15785 /*allow_non_constant_p=*/true,
15787 if (!*non_constant_p)
15788 initializer = fold_non_dependent_expr (initializer);
15791 initializer = cp_parser_braced_list (parser, non_constant_p);
15793 return initializer;
15796 /* Parse a brace-enclosed initializer list.
15799 { initializer-list , [opt] }
15802 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15803 the elements of the initializer-list (or NULL, if the last
15804 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15805 NULL_TREE. There is no way to detect whether or not the optional
15806 trailing `,' was provided. NON_CONSTANT_P is as for
15807 cp_parser_initializer. */
15810 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15814 /* Consume the `{' token. */
15815 cp_lexer_consume_token (parser->lexer);
15816 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15817 initializer = make_node (CONSTRUCTOR);
15818 /* If it's not a `}', then there is a non-trivial initializer. */
15819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15821 /* Parse the initializer list. */
15822 CONSTRUCTOR_ELTS (initializer)
15823 = cp_parser_initializer_list (parser, non_constant_p);
15824 /* A trailing `,' token is allowed. */
15825 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15826 cp_lexer_consume_token (parser->lexer);
15828 /* Now, there should be a trailing `}'. */
15829 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15830 TREE_TYPE (initializer) = init_list_type_node;
15831 return initializer;
15834 /* Parse an initializer-list.
15837 initializer-clause ... [opt]
15838 initializer-list , initializer-clause ... [opt]
15843 identifier : initializer-clause
15844 initializer-list, identifier : initializer-clause
15846 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15847 for the initializer. If the INDEX of the elt is non-NULL, it is the
15848 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15849 as for cp_parser_initializer. */
15851 static VEC(constructor_elt,gc) *
15852 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15854 VEC(constructor_elt,gc) *v = NULL;
15856 /* Assume all of the expressions are constant. */
15857 *non_constant_p = false;
15859 /* Parse the rest of the list. */
15865 bool clause_non_constant_p;
15867 /* If the next token is an identifier and the following one is a
15868 colon, we are looking at the GNU designated-initializer
15870 if (cp_parser_allow_gnu_extensions_p (parser)
15871 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15872 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15874 /* Warn the user that they are using an extension. */
15875 pedwarn (input_location, OPT_pedantic,
15876 "ISO C++ does not allow designated initializers");
15877 /* Consume the identifier. */
15878 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15879 /* Consume the `:'. */
15880 cp_lexer_consume_token (parser->lexer);
15883 identifier = NULL_TREE;
15885 /* Parse the initializer. */
15886 initializer = cp_parser_initializer_clause (parser,
15887 &clause_non_constant_p);
15888 /* If any clause is non-constant, so is the entire initializer. */
15889 if (clause_non_constant_p)
15890 *non_constant_p = true;
15892 /* If we have an ellipsis, this is an initializer pack
15894 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15896 /* Consume the `...'. */
15897 cp_lexer_consume_token (parser->lexer);
15899 /* Turn the initializer into an initializer expansion. */
15900 initializer = make_pack_expansion (initializer);
15903 /* Add it to the vector. */
15904 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15906 /* If the next token is not a comma, we have reached the end of
15908 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15911 /* Peek at the next token. */
15912 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15913 /* If the next token is a `}', then we're still done. An
15914 initializer-clause can have a trailing `,' after the
15915 initializer-list and before the closing `}'. */
15916 if (token->type == CPP_CLOSE_BRACE)
15919 /* Consume the `,' token. */
15920 cp_lexer_consume_token (parser->lexer);
15926 /* Classes [gram.class] */
15928 /* Parse a class-name.
15934 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15935 to indicate that names looked up in dependent types should be
15936 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15937 keyword has been used to indicate that the name that appears next
15938 is a template. TAG_TYPE indicates the explicit tag given before
15939 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15940 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15941 is the class being defined in a class-head.
15943 Returns the TYPE_DECL representing the class. */
15946 cp_parser_class_name (cp_parser *parser,
15947 bool typename_keyword_p,
15948 bool template_keyword_p,
15949 enum tag_types tag_type,
15950 bool check_dependency_p,
15952 bool is_declaration)
15958 tree identifier = NULL_TREE;
15960 /* All class-names start with an identifier. */
15961 token = cp_lexer_peek_token (parser->lexer);
15962 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15964 cp_parser_error (parser, "expected class-name");
15965 return error_mark_node;
15968 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15969 to a template-id, so we save it here. */
15970 scope = parser->scope;
15971 if (scope == error_mark_node)
15972 return error_mark_node;
15974 /* Any name names a type if we're following the `typename' keyword
15975 in a qualified name where the enclosing scope is type-dependent. */
15976 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15977 && dependent_type_p (scope));
15978 /* Handle the common case (an identifier, but not a template-id)
15980 if (token->type == CPP_NAME
15981 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15983 cp_token *identifier_token;
15986 /* Look for the identifier. */
15987 identifier_token = cp_lexer_peek_token (parser->lexer);
15988 ambiguous_p = identifier_token->ambiguous_p;
15989 identifier = cp_parser_identifier (parser);
15990 /* If the next token isn't an identifier, we are certainly not
15991 looking at a class-name. */
15992 if (identifier == error_mark_node)
15993 decl = error_mark_node;
15994 /* If we know this is a type-name, there's no need to look it
15996 else if (typename_p)
16000 tree ambiguous_decls;
16001 /* If we already know that this lookup is ambiguous, then
16002 we've already issued an error message; there's no reason
16006 cp_parser_simulate_error (parser);
16007 return error_mark_node;
16009 /* If the next token is a `::', then the name must be a type
16012 [basic.lookup.qual]
16014 During the lookup for a name preceding the :: scope
16015 resolution operator, object, function, and enumerator
16016 names are ignored. */
16017 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16018 tag_type = typename_type;
16019 /* Look up the name. */
16020 decl = cp_parser_lookup_name (parser, identifier,
16022 /*is_template=*/false,
16023 /*is_namespace=*/false,
16024 check_dependency_p,
16026 identifier_token->location);
16027 if (ambiguous_decls)
16029 if (cp_parser_parsing_tentatively (parser))
16030 cp_parser_simulate_error (parser);
16031 return error_mark_node;
16037 /* Try a template-id. */
16038 decl = cp_parser_template_id (parser, template_keyword_p,
16039 check_dependency_p,
16041 if (decl == error_mark_node)
16042 return error_mark_node;
16045 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16047 /* If this is a typename, create a TYPENAME_TYPE. */
16048 if (typename_p && decl != error_mark_node)
16050 decl = make_typename_type (scope, decl, typename_type,
16051 /*complain=*/tf_error);
16052 if (decl != error_mark_node)
16053 decl = TYPE_NAME (decl);
16056 /* Check to see that it is really the name of a class. */
16057 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16058 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16059 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16060 /* Situations like this:
16062 template <typename T> struct A {
16063 typename T::template X<int>::I i;
16066 are problematic. Is `T::template X<int>' a class-name? The
16067 standard does not seem to be definitive, but there is no other
16068 valid interpretation of the following `::'. Therefore, those
16069 names are considered class-names. */
16071 decl = make_typename_type (scope, decl, tag_type, tf_error);
16072 if (decl != error_mark_node)
16073 decl = TYPE_NAME (decl);
16075 else if (TREE_CODE (decl) != TYPE_DECL
16076 || TREE_TYPE (decl) == error_mark_node
16077 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
16078 decl = error_mark_node;
16080 if (decl == error_mark_node)
16081 cp_parser_error (parser, "expected class-name");
16082 else if (identifier && !parser->scope)
16083 maybe_note_name_used_in_class (identifier, decl);
16088 /* Parse a class-specifier.
16091 class-head { member-specification [opt] }
16093 Returns the TREE_TYPE representing the class. */
16096 cp_parser_class_specifier (cp_parser* parser)
16099 tree attributes = NULL_TREE;
16100 bool nested_name_specifier_p;
16101 unsigned saved_num_template_parameter_lists;
16102 bool saved_in_function_body;
16103 bool saved_in_unbraced_linkage_specification_p;
16104 tree old_scope = NULL_TREE;
16105 tree scope = NULL_TREE;
16108 push_deferring_access_checks (dk_no_deferred);
16110 /* Parse the class-head. */
16111 type = cp_parser_class_head (parser,
16112 &nested_name_specifier_p,
16115 /* If the class-head was a semantic disaster, skip the entire body
16119 cp_parser_skip_to_end_of_block_or_statement (parser);
16120 pop_deferring_access_checks ();
16121 return error_mark_node;
16124 /* Look for the `{'. */
16125 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16127 pop_deferring_access_checks ();
16128 return error_mark_node;
16131 /* Process the base classes. If they're invalid, skip the
16132 entire class body. */
16133 if (!xref_basetypes (type, bases))
16135 /* Consuming the closing brace yields better error messages
16137 if (cp_parser_skip_to_closing_brace (parser))
16138 cp_lexer_consume_token (parser->lexer);
16139 pop_deferring_access_checks ();
16140 return error_mark_node;
16143 /* Issue an error message if type-definitions are forbidden here. */
16144 cp_parser_check_type_definition (parser);
16145 /* Remember that we are defining one more class. */
16146 ++parser->num_classes_being_defined;
16147 /* Inside the class, surrounding template-parameter-lists do not
16149 saved_num_template_parameter_lists
16150 = parser->num_template_parameter_lists;
16151 parser->num_template_parameter_lists = 0;
16152 /* We are not in a function body. */
16153 saved_in_function_body = parser->in_function_body;
16154 parser->in_function_body = false;
16155 /* We are not immediately inside an extern "lang" block. */
16156 saved_in_unbraced_linkage_specification_p
16157 = parser->in_unbraced_linkage_specification_p;
16158 parser->in_unbraced_linkage_specification_p = false;
16160 /* Start the class. */
16161 if (nested_name_specifier_p)
16163 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16164 old_scope = push_inner_scope (scope);
16166 type = begin_class_definition (type, attributes);
16168 if (type == error_mark_node)
16169 /* If the type is erroneous, skip the entire body of the class. */
16170 cp_parser_skip_to_closing_brace (parser);
16172 /* Parse the member-specification. */
16173 cp_parser_member_specification_opt (parser);
16175 /* Look for the trailing `}'. */
16176 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16177 /* Look for trailing attributes to apply to this class. */
16178 if (cp_parser_allow_gnu_extensions_p (parser))
16179 attributes = cp_parser_attributes_opt (parser);
16180 if (type != error_mark_node)
16181 type = finish_struct (type, attributes);
16182 if (nested_name_specifier_p)
16183 pop_inner_scope (old_scope, scope);
16184 /* If this class is not itself within the scope of another class,
16185 then we need to parse the bodies of all of the queued function
16186 definitions. Note that the queued functions defined in a class
16187 are not always processed immediately following the
16188 class-specifier for that class. Consider:
16191 struct B { void f() { sizeof (A); } };
16194 If `f' were processed before the processing of `A' were
16195 completed, there would be no way to compute the size of `A'.
16196 Note that the nesting we are interested in here is lexical --
16197 not the semantic nesting given by TYPE_CONTEXT. In particular,
16200 struct A { struct B; };
16201 struct A::B { void f() { } };
16203 there is no need to delay the parsing of `A::B::f'. */
16204 if (--parser->num_classes_being_defined == 0)
16208 tree class_type = NULL_TREE;
16209 tree pushed_scope = NULL_TREE;
16211 /* In a first pass, parse default arguments to the functions.
16212 Then, in a second pass, parse the bodies of the functions.
16213 This two-phased approach handles cases like:
16221 for (TREE_PURPOSE (parser->unparsed_functions_queues)
16222 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
16223 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
16224 TREE_PURPOSE (parser->unparsed_functions_queues)
16225 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
16227 fn = TREE_VALUE (queue_entry);
16228 /* If there are default arguments that have not yet been processed,
16229 take care of them now. */
16230 if (class_type != TREE_PURPOSE (queue_entry))
16233 pop_scope (pushed_scope);
16234 class_type = TREE_PURPOSE (queue_entry);
16235 pushed_scope = push_scope (class_type);
16237 /* Make sure that any template parameters are in scope. */
16238 maybe_begin_member_template_processing (fn);
16239 /* Parse the default argument expressions. */
16240 cp_parser_late_parsing_default_args (parser, fn);
16241 /* Remove any template parameters from the symbol table. */
16242 maybe_end_member_template_processing ();
16245 pop_scope (pushed_scope);
16246 /* Now parse the body of the functions. */
16247 for (TREE_VALUE (parser->unparsed_functions_queues)
16248 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
16249 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
16250 TREE_VALUE (parser->unparsed_functions_queues)
16251 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
16253 /* Figure out which function we need to process. */
16254 fn = TREE_VALUE (queue_entry);
16255 /* Parse the function. */
16256 cp_parser_late_parsing_for_member (parser, fn);
16260 /* Put back any saved access checks. */
16261 pop_deferring_access_checks ();
16263 /* Restore saved state. */
16264 parser->in_function_body = saved_in_function_body;
16265 parser->num_template_parameter_lists
16266 = saved_num_template_parameter_lists;
16267 parser->in_unbraced_linkage_specification_p
16268 = saved_in_unbraced_linkage_specification_p;
16273 /* Parse a class-head.
16276 class-key identifier [opt] base-clause [opt]
16277 class-key nested-name-specifier identifier base-clause [opt]
16278 class-key nested-name-specifier [opt] template-id
16282 class-key attributes identifier [opt] base-clause [opt]
16283 class-key attributes nested-name-specifier identifier base-clause [opt]
16284 class-key attributes nested-name-specifier [opt] template-id
16287 Upon return BASES is initialized to the list of base classes (or
16288 NULL, if there are none) in the same form returned by
16289 cp_parser_base_clause.
16291 Returns the TYPE of the indicated class. Sets
16292 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16293 involving a nested-name-specifier was used, and FALSE otherwise.
16295 Returns error_mark_node if this is not a class-head.
16297 Returns NULL_TREE if the class-head is syntactically valid, but
16298 semantically invalid in a way that means we should skip the entire
16299 body of the class. */
16302 cp_parser_class_head (cp_parser* parser,
16303 bool* nested_name_specifier_p,
16304 tree *attributes_p,
16307 tree nested_name_specifier;
16308 enum tag_types class_key;
16309 tree id = NULL_TREE;
16310 tree type = NULL_TREE;
16312 bool template_id_p = false;
16313 bool qualified_p = false;
16314 bool invalid_nested_name_p = false;
16315 bool invalid_explicit_specialization_p = false;
16316 tree pushed_scope = NULL_TREE;
16317 unsigned num_templates;
16318 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16319 /* Assume no nested-name-specifier will be present. */
16320 *nested_name_specifier_p = false;
16321 /* Assume no template parameter lists will be used in defining the
16325 *bases = NULL_TREE;
16327 /* Look for the class-key. */
16328 class_key = cp_parser_class_key (parser);
16329 if (class_key == none_type)
16330 return error_mark_node;
16332 /* Parse the attributes. */
16333 attributes = cp_parser_attributes_opt (parser);
16335 /* If the next token is `::', that is invalid -- but sometimes
16336 people do try to write:
16340 Handle this gracefully by accepting the extra qualifier, and then
16341 issuing an error about it later if this really is a
16342 class-head. If it turns out just to be an elaborated type
16343 specifier, remain silent. */
16344 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16345 qualified_p = true;
16347 push_deferring_access_checks (dk_no_check);
16349 /* Determine the name of the class. Begin by looking for an
16350 optional nested-name-specifier. */
16351 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16352 nested_name_specifier
16353 = cp_parser_nested_name_specifier_opt (parser,
16354 /*typename_keyword_p=*/false,
16355 /*check_dependency_p=*/false,
16357 /*is_declaration=*/false);
16358 /* If there was a nested-name-specifier, then there *must* be an
16360 if (nested_name_specifier)
16362 type_start_token = cp_lexer_peek_token (parser->lexer);
16363 /* Although the grammar says `identifier', it really means
16364 `class-name' or `template-name'. You are only allowed to
16365 define a class that has already been declared with this
16368 The proposed resolution for Core Issue 180 says that wherever
16369 you see `class T::X' you should treat `X' as a type-name.
16371 It is OK to define an inaccessible class; for example:
16373 class A { class B; };
16376 We do not know if we will see a class-name, or a
16377 template-name. We look for a class-name first, in case the
16378 class-name is a template-id; if we looked for the
16379 template-name first we would stop after the template-name. */
16380 cp_parser_parse_tentatively (parser);
16381 type = cp_parser_class_name (parser,
16382 /*typename_keyword_p=*/false,
16383 /*template_keyword_p=*/false,
16385 /*check_dependency_p=*/false,
16386 /*class_head_p=*/true,
16387 /*is_declaration=*/false);
16388 /* If that didn't work, ignore the nested-name-specifier. */
16389 if (!cp_parser_parse_definitely (parser))
16391 invalid_nested_name_p = true;
16392 type_start_token = cp_lexer_peek_token (parser->lexer);
16393 id = cp_parser_identifier (parser);
16394 if (id == error_mark_node)
16397 /* If we could not find a corresponding TYPE, treat this
16398 declaration like an unqualified declaration. */
16399 if (type == error_mark_node)
16400 nested_name_specifier = NULL_TREE;
16401 /* Otherwise, count the number of templates used in TYPE and its
16402 containing scopes. */
16407 for (scope = TREE_TYPE (type);
16408 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16409 scope = (TYPE_P (scope)
16410 ? TYPE_CONTEXT (scope)
16411 : DECL_CONTEXT (scope)))
16413 && CLASS_TYPE_P (scope)
16414 && CLASSTYPE_TEMPLATE_INFO (scope)
16415 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16416 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16420 /* Otherwise, the identifier is optional. */
16423 /* We don't know whether what comes next is a template-id,
16424 an identifier, or nothing at all. */
16425 cp_parser_parse_tentatively (parser);
16426 /* Check for a template-id. */
16427 type_start_token = cp_lexer_peek_token (parser->lexer);
16428 id = cp_parser_template_id (parser,
16429 /*template_keyword_p=*/false,
16430 /*check_dependency_p=*/true,
16431 /*is_declaration=*/true);
16432 /* If that didn't work, it could still be an identifier. */
16433 if (!cp_parser_parse_definitely (parser))
16435 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16437 type_start_token = cp_lexer_peek_token (parser->lexer);
16438 id = cp_parser_identifier (parser);
16445 template_id_p = true;
16450 pop_deferring_access_checks ();
16453 cp_parser_check_for_invalid_template_id (parser, id,
16454 type_start_token->location);
16456 /* If it's not a `:' or a `{' then we can't really be looking at a
16457 class-head, since a class-head only appears as part of a
16458 class-specifier. We have to detect this situation before calling
16459 xref_tag, since that has irreversible side-effects. */
16460 if (!cp_parser_next_token_starts_class_definition_p (parser))
16462 cp_parser_error (parser, "expected %<{%> or %<:%>");
16463 return error_mark_node;
16466 /* At this point, we're going ahead with the class-specifier, even
16467 if some other problem occurs. */
16468 cp_parser_commit_to_tentative_parse (parser);
16469 /* Issue the error about the overly-qualified name now. */
16472 cp_parser_error (parser,
16473 "global qualification of class name is invalid");
16474 return error_mark_node;
16476 else if (invalid_nested_name_p)
16478 cp_parser_error (parser,
16479 "qualified name does not name a class");
16480 return error_mark_node;
16482 else if (nested_name_specifier)
16486 /* Reject typedef-names in class heads. */
16487 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16489 error_at (type_start_token->location,
16490 "invalid class name in declaration of %qD",
16496 /* Figure out in what scope the declaration is being placed. */
16497 scope = current_scope ();
16498 /* If that scope does not contain the scope in which the
16499 class was originally declared, the program is invalid. */
16500 if (scope && !is_ancestor (scope, nested_name_specifier))
16502 if (at_namespace_scope_p ())
16503 error_at (type_start_token->location,
16504 "declaration of %qD in namespace %qD which does not "
16506 type, scope, nested_name_specifier);
16508 error_at (type_start_token->location,
16509 "declaration of %qD in %qD which does not enclose %qD",
16510 type, scope, nested_name_specifier);
16516 A declarator-id shall not be qualified except for the
16517 definition of a ... nested class outside of its class
16518 ... [or] the definition or explicit instantiation of a
16519 class member of a namespace outside of its namespace. */
16520 if (scope == nested_name_specifier)
16522 permerror (nested_name_specifier_token_start->location,
16523 "extra qualification not allowed");
16524 nested_name_specifier = NULL_TREE;
16528 /* An explicit-specialization must be preceded by "template <>". If
16529 it is not, try to recover gracefully. */
16530 if (at_namespace_scope_p ()
16531 && parser->num_template_parameter_lists == 0
16534 error_at (type_start_token->location,
16535 "an explicit specialization must be preceded by %<template <>%>");
16536 invalid_explicit_specialization_p = true;
16537 /* Take the same action that would have been taken by
16538 cp_parser_explicit_specialization. */
16539 ++parser->num_template_parameter_lists;
16540 begin_specialization ();
16542 /* There must be no "return" statements between this point and the
16543 end of this function; set "type "to the correct return value and
16544 use "goto done;" to return. */
16545 /* Make sure that the right number of template parameters were
16547 if (!cp_parser_check_template_parameters (parser, num_templates,
16548 type_start_token->location,
16549 /*declarator=*/NULL))
16551 /* If something went wrong, there is no point in even trying to
16552 process the class-definition. */
16557 /* Look up the type. */
16560 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16561 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16562 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16564 error_at (type_start_token->location,
16565 "function template %qD redeclared as a class template", id);
16566 type = error_mark_node;
16570 type = TREE_TYPE (id);
16571 type = maybe_process_partial_specialization (type);
16573 if (nested_name_specifier)
16574 pushed_scope = push_scope (nested_name_specifier);
16576 else if (nested_name_specifier)
16582 template <typename T> struct S { struct T };
16583 template <typename T> struct S<T>::T { };
16585 we will get a TYPENAME_TYPE when processing the definition of
16586 `S::T'. We need to resolve it to the actual type before we
16587 try to define it. */
16588 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16590 class_type = resolve_typename_type (TREE_TYPE (type),
16591 /*only_current_p=*/false);
16592 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16593 type = TYPE_NAME (class_type);
16596 cp_parser_error (parser, "could not resolve typename type");
16597 type = error_mark_node;
16601 if (maybe_process_partial_specialization (TREE_TYPE (type))
16602 == error_mark_node)
16608 class_type = current_class_type;
16609 /* Enter the scope indicated by the nested-name-specifier. */
16610 pushed_scope = push_scope (nested_name_specifier);
16611 /* Get the canonical version of this type. */
16612 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16613 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16614 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16616 type = push_template_decl (type);
16617 if (type == error_mark_node)
16624 type = TREE_TYPE (type);
16625 *nested_name_specifier_p = true;
16627 else /* The name is not a nested name. */
16629 /* If the class was unnamed, create a dummy name. */
16631 id = make_anon_name ();
16632 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16633 parser->num_template_parameter_lists);
16636 /* Indicate whether this class was declared as a `class' or as a
16638 if (TREE_CODE (type) == RECORD_TYPE)
16639 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16640 cp_parser_check_class_key (class_key, type);
16642 /* If this type was already complete, and we see another definition,
16643 that's an error. */
16644 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16646 error_at (type_start_token->location, "redefinition of %q#T",
16648 error_at (type_start_token->location, "previous definition of %q+#T",
16653 else if (type == error_mark_node)
16656 /* We will have entered the scope containing the class; the names of
16657 base classes should be looked up in that context. For example:
16659 struct A { struct B {}; struct C; };
16660 struct A::C : B {};
16664 /* Get the list of base-classes, if there is one. */
16665 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16666 *bases = cp_parser_base_clause (parser);
16669 /* Leave the scope given by the nested-name-specifier. We will
16670 enter the class scope itself while processing the members. */
16672 pop_scope (pushed_scope);
16674 if (invalid_explicit_specialization_p)
16676 end_specialization ();
16677 --parser->num_template_parameter_lists;
16681 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16682 *attributes_p = attributes;
16686 /* Parse a class-key.
16693 Returns the kind of class-key specified, or none_type to indicate
16696 static enum tag_types
16697 cp_parser_class_key (cp_parser* parser)
16700 enum tag_types tag_type;
16702 /* Look for the class-key. */
16703 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
16707 /* Check to see if the TOKEN is a class-key. */
16708 tag_type = cp_parser_token_is_class_key (token);
16710 cp_parser_error (parser, "expected class-key");
16714 /* Parse an (optional) member-specification.
16716 member-specification:
16717 member-declaration member-specification [opt]
16718 access-specifier : member-specification [opt] */
16721 cp_parser_member_specification_opt (cp_parser* parser)
16728 /* Peek at the next token. */
16729 token = cp_lexer_peek_token (parser->lexer);
16730 /* If it's a `}', or EOF then we've seen all the members. */
16731 if (token->type == CPP_CLOSE_BRACE
16732 || token->type == CPP_EOF
16733 || token->type == CPP_PRAGMA_EOL)
16736 /* See if this token is a keyword. */
16737 keyword = token->keyword;
16741 case RID_PROTECTED:
16743 /* Consume the access-specifier. */
16744 cp_lexer_consume_token (parser->lexer);
16745 /* Remember which access-specifier is active. */
16746 current_access_specifier = token->u.value;
16747 /* Look for the `:'. */
16748 cp_parser_require (parser, CPP_COLON, RT_COLON);
16752 /* Accept #pragmas at class scope. */
16753 if (token->type == CPP_PRAGMA)
16755 cp_parser_pragma (parser, pragma_external);
16759 /* Otherwise, the next construction must be a
16760 member-declaration. */
16761 cp_parser_member_declaration (parser);
16766 /* Parse a member-declaration.
16768 member-declaration:
16769 decl-specifier-seq [opt] member-declarator-list [opt] ;
16770 function-definition ; [opt]
16771 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16773 template-declaration
16775 member-declarator-list:
16777 member-declarator-list , member-declarator
16780 declarator pure-specifier [opt]
16781 declarator constant-initializer [opt]
16782 identifier [opt] : constant-expression
16786 member-declaration:
16787 __extension__ member-declaration
16790 declarator attributes [opt] pure-specifier [opt]
16791 declarator attributes [opt] constant-initializer [opt]
16792 identifier [opt] attributes [opt] : constant-expression
16796 member-declaration:
16797 static_assert-declaration */
16800 cp_parser_member_declaration (cp_parser* parser)
16802 cp_decl_specifier_seq decl_specifiers;
16803 tree prefix_attributes;
16805 int declares_class_or_enum;
16807 cp_token *token = NULL;
16808 cp_token *decl_spec_token_start = NULL;
16809 cp_token *initializer_token_start = NULL;
16810 int saved_pedantic;
16812 /* Check for the `__extension__' keyword. */
16813 if (cp_parser_extension_opt (parser, &saved_pedantic))
16816 cp_parser_member_declaration (parser);
16817 /* Restore the old value of the PEDANTIC flag. */
16818 pedantic = saved_pedantic;
16823 /* Check for a template-declaration. */
16824 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16826 /* An explicit specialization here is an error condition, and we
16827 expect the specialization handler to detect and report this. */
16828 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16829 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16830 cp_parser_explicit_specialization (parser);
16832 cp_parser_template_declaration (parser, /*member_p=*/true);
16837 /* Check for a using-declaration. */
16838 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16840 /* Parse the using-declaration. */
16841 cp_parser_using_declaration (parser,
16842 /*access_declaration_p=*/false);
16846 /* Check for @defs. */
16847 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16850 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16851 ivar = ivar_chains;
16855 ivar = TREE_CHAIN (member);
16856 TREE_CHAIN (member) = NULL_TREE;
16857 finish_member_declaration (member);
16862 /* If the next token is `static_assert' we have a static assertion. */
16863 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16865 cp_parser_static_assert (parser, /*member_p=*/true);
16869 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16872 /* Parse the decl-specifier-seq. */
16873 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16874 cp_parser_decl_specifier_seq (parser,
16875 CP_PARSER_FLAGS_OPTIONAL,
16877 &declares_class_or_enum);
16878 prefix_attributes = decl_specifiers.attributes;
16879 decl_specifiers.attributes = NULL_TREE;
16880 /* Check for an invalid type-name. */
16881 if (!decl_specifiers.any_type_specifiers_p
16882 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16884 /* If there is no declarator, then the decl-specifier-seq should
16886 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16888 /* If there was no decl-specifier-seq, and the next token is a
16889 `;', then we have something like:
16895 Each member-declaration shall declare at least one member
16896 name of the class. */
16897 if (!decl_specifiers.any_specifiers_p)
16899 cp_token *token = cp_lexer_peek_token (parser->lexer);
16900 if (!in_system_header_at (token->location))
16901 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16907 /* See if this declaration is a friend. */
16908 friend_p = cp_parser_friend_p (&decl_specifiers);
16909 /* If there were decl-specifiers, check to see if there was
16910 a class-declaration. */
16911 type = check_tag_decl (&decl_specifiers);
16912 /* Nested classes have already been added to the class, but
16913 a `friend' needs to be explicitly registered. */
16916 /* If the `friend' keyword was present, the friend must
16917 be introduced with a class-key. */
16918 if (!declares_class_or_enum)
16919 error_at (decl_spec_token_start->location,
16920 "a class-key must be used when declaring a friend");
16923 template <typename T> struct A {
16924 friend struct A<T>::B;
16927 A<T>::B will be represented by a TYPENAME_TYPE, and
16928 therefore not recognized by check_tag_decl. */
16930 && decl_specifiers.type
16931 && TYPE_P (decl_specifiers.type))
16932 type = decl_specifiers.type;
16933 if (!type || !TYPE_P (type))
16934 error_at (decl_spec_token_start->location,
16935 "friend declaration does not name a class or "
16938 make_friend_class (current_class_type, type,
16939 /*complain=*/true);
16941 /* If there is no TYPE, an error message will already have
16943 else if (!type || type == error_mark_node)
16945 /* An anonymous aggregate has to be handled specially; such
16946 a declaration really declares a data member (with a
16947 particular type), as opposed to a nested class. */
16948 else if (ANON_AGGR_TYPE_P (type))
16950 /* Remove constructors and such from TYPE, now that we
16951 know it is an anonymous aggregate. */
16952 fixup_anonymous_aggr (type);
16953 /* And make the corresponding data member. */
16954 decl = build_decl (decl_spec_token_start->location,
16955 FIELD_DECL, NULL_TREE, type);
16956 /* Add it to the class. */
16957 finish_member_declaration (decl);
16960 cp_parser_check_access_in_redeclaration
16962 decl_spec_token_start->location);
16967 /* See if these declarations will be friends. */
16968 friend_p = cp_parser_friend_p (&decl_specifiers);
16970 /* Keep going until we hit the `;' at the end of the
16972 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16974 tree attributes = NULL_TREE;
16975 tree first_attribute;
16977 /* Peek at the next token. */
16978 token = cp_lexer_peek_token (parser->lexer);
16980 /* Check for a bitfield declaration. */
16981 if (token->type == CPP_COLON
16982 || (token->type == CPP_NAME
16983 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16989 /* Get the name of the bitfield. Note that we cannot just
16990 check TOKEN here because it may have been invalidated by
16991 the call to cp_lexer_peek_nth_token above. */
16992 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16993 identifier = cp_parser_identifier (parser);
16995 identifier = NULL_TREE;
16997 /* Consume the `:' token. */
16998 cp_lexer_consume_token (parser->lexer);
16999 /* Get the width of the bitfield. */
17001 = cp_parser_constant_expression (parser,
17002 /*allow_non_constant=*/false,
17005 /* Look for attributes that apply to the bitfield. */
17006 attributes = cp_parser_attributes_opt (parser);
17007 /* Remember which attributes are prefix attributes and
17009 first_attribute = attributes;
17010 /* Combine the attributes. */
17011 attributes = chainon (prefix_attributes, attributes);
17013 /* Create the bitfield declaration. */
17014 decl = grokbitfield (identifier
17015 ? make_id_declarator (NULL_TREE,
17025 cp_declarator *declarator;
17027 tree asm_specification;
17028 int ctor_dtor_or_conv_p;
17030 /* Parse the declarator. */
17032 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17033 &ctor_dtor_or_conv_p,
17034 /*parenthesized_p=*/NULL,
17035 /*member_p=*/true);
17037 /* If something went wrong parsing the declarator, make sure
17038 that we at least consume some tokens. */
17039 if (declarator == cp_error_declarator)
17041 /* Skip to the end of the statement. */
17042 cp_parser_skip_to_end_of_statement (parser);
17043 /* If the next token is not a semicolon, that is
17044 probably because we just skipped over the body of
17045 a function. So, we consume a semicolon if
17046 present, but do not issue an error message if it
17048 if (cp_lexer_next_token_is (parser->lexer,
17050 cp_lexer_consume_token (parser->lexer);
17054 if (declares_class_or_enum & 2)
17055 cp_parser_check_for_definition_in_return_type
17056 (declarator, decl_specifiers.type,
17057 decl_specifiers.type_location);
17059 /* Look for an asm-specification. */
17060 asm_specification = cp_parser_asm_specification_opt (parser);
17061 /* Look for attributes that apply to the declaration. */
17062 attributes = cp_parser_attributes_opt (parser);
17063 /* Remember which attributes are prefix attributes and
17065 first_attribute = attributes;
17066 /* Combine the attributes. */
17067 attributes = chainon (prefix_attributes, attributes);
17069 /* If it's an `=', then we have a constant-initializer or a
17070 pure-specifier. It is not correct to parse the
17071 initializer before registering the member declaration
17072 since the member declaration should be in scope while
17073 its initializer is processed. However, the rest of the
17074 front end does not yet provide an interface that allows
17075 us to handle this correctly. */
17076 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17080 A pure-specifier shall be used only in the declaration of
17081 a virtual function.
17083 A member-declarator can contain a constant-initializer
17084 only if it declares a static member of integral or
17087 Therefore, if the DECLARATOR is for a function, we look
17088 for a pure-specifier; otherwise, we look for a
17089 constant-initializer. When we call `grokfield', it will
17090 perform more stringent semantics checks. */
17091 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17092 if (function_declarator_p (declarator))
17093 initializer = cp_parser_pure_specifier (parser);
17095 /* Parse the initializer. */
17096 initializer = cp_parser_constant_initializer (parser);
17098 /* Otherwise, there is no initializer. */
17100 initializer = NULL_TREE;
17102 /* See if we are probably looking at a function
17103 definition. We are certainly not looking at a
17104 member-declarator. Calling `grokfield' has
17105 side-effects, so we must not do it unless we are sure
17106 that we are looking at a member-declarator. */
17107 if (cp_parser_token_starts_function_definition_p
17108 (cp_lexer_peek_token (parser->lexer)))
17110 /* The grammar does not allow a pure-specifier to be
17111 used when a member function is defined. (It is
17112 possible that this fact is an oversight in the
17113 standard, since a pure function may be defined
17114 outside of the class-specifier. */
17116 error_at (initializer_token_start->location,
17117 "pure-specifier on function-definition");
17118 decl = cp_parser_save_member_function_body (parser,
17122 /* If the member was not a friend, declare it here. */
17124 finish_member_declaration (decl);
17125 /* Peek at the next token. */
17126 token = cp_lexer_peek_token (parser->lexer);
17127 /* If the next token is a semicolon, consume it. */
17128 if (token->type == CPP_SEMICOLON)
17129 cp_lexer_consume_token (parser->lexer);
17133 if (declarator->kind == cdk_function)
17134 declarator->id_loc = token->location;
17135 /* Create the declaration. */
17136 decl = grokfield (declarator, &decl_specifiers,
17137 initializer, /*init_const_expr_p=*/true,
17142 /* Reset PREFIX_ATTRIBUTES. */
17143 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17144 attributes = TREE_CHAIN (attributes);
17146 TREE_CHAIN (attributes) = NULL_TREE;
17148 /* If there is any qualification still in effect, clear it
17149 now; we will be starting fresh with the next declarator. */
17150 parser->scope = NULL_TREE;
17151 parser->qualifying_scope = NULL_TREE;
17152 parser->object_scope = NULL_TREE;
17153 /* If it's a `,', then there are more declarators. */
17154 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17155 cp_lexer_consume_token (parser->lexer);
17156 /* If the next token isn't a `;', then we have a parse error. */
17157 else if (cp_lexer_next_token_is_not (parser->lexer,
17160 cp_parser_error (parser, "expected %<;%>");
17161 /* Skip tokens until we find a `;'. */
17162 cp_parser_skip_to_end_of_statement (parser);
17169 /* Add DECL to the list of members. */
17171 finish_member_declaration (decl);
17173 if (TREE_CODE (decl) == FUNCTION_DECL)
17174 cp_parser_save_default_args (parser, decl);
17179 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17182 /* Parse a pure-specifier.
17187 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17188 Otherwise, ERROR_MARK_NODE is returned. */
17191 cp_parser_pure_specifier (cp_parser* parser)
17195 /* Look for the `=' token. */
17196 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17197 return error_mark_node;
17198 /* Look for the `0' token. */
17199 token = cp_lexer_peek_token (parser->lexer);
17201 if (token->type == CPP_EOF
17202 || token->type == CPP_PRAGMA_EOL)
17203 return error_mark_node;
17205 cp_lexer_consume_token (parser->lexer);
17207 /* Accept = default or = delete in c++0x mode. */
17208 if (token->keyword == RID_DEFAULT
17209 || token->keyword == RID_DELETE)
17211 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17212 return token->u.value;
17215 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17216 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17218 cp_parser_error (parser,
17219 "invalid pure specifier (only %<= 0%> is allowed)");
17220 cp_parser_skip_to_end_of_statement (parser);
17221 return error_mark_node;
17223 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17225 error_at (token->location, "templates may not be %<virtual%>");
17226 return error_mark_node;
17229 return integer_zero_node;
17232 /* Parse a constant-initializer.
17234 constant-initializer:
17235 = constant-expression
17237 Returns a representation of the constant-expression. */
17240 cp_parser_constant_initializer (cp_parser* parser)
17242 /* Look for the `=' token. */
17243 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17244 return error_mark_node;
17246 /* It is invalid to write:
17248 struct S { static const int i = { 7 }; };
17251 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17253 cp_parser_error (parser,
17254 "a brace-enclosed initializer is not allowed here");
17255 /* Consume the opening brace. */
17256 cp_lexer_consume_token (parser->lexer);
17257 /* Skip the initializer. */
17258 cp_parser_skip_to_closing_brace (parser);
17259 /* Look for the trailing `}'. */
17260 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17262 return error_mark_node;
17265 return cp_parser_constant_expression (parser,
17266 /*allow_non_constant=*/false,
17270 /* Derived classes [gram.class.derived] */
17272 /* Parse a base-clause.
17275 : base-specifier-list
17277 base-specifier-list:
17278 base-specifier ... [opt]
17279 base-specifier-list , base-specifier ... [opt]
17281 Returns a TREE_LIST representing the base-classes, in the order in
17282 which they were declared. The representation of each node is as
17283 described by cp_parser_base_specifier.
17285 In the case that no bases are specified, this function will return
17286 NULL_TREE, not ERROR_MARK_NODE. */
17289 cp_parser_base_clause (cp_parser* parser)
17291 tree bases = NULL_TREE;
17293 /* Look for the `:' that begins the list. */
17294 cp_parser_require (parser, CPP_COLON, RT_COLON);
17296 /* Scan the base-specifier-list. */
17301 bool pack_expansion_p = false;
17303 /* Look for the base-specifier. */
17304 base = cp_parser_base_specifier (parser);
17305 /* Look for the (optional) ellipsis. */
17306 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17308 /* Consume the `...'. */
17309 cp_lexer_consume_token (parser->lexer);
17311 pack_expansion_p = true;
17314 /* Add BASE to the front of the list. */
17315 if (base != error_mark_node)
17317 if (pack_expansion_p)
17318 /* Make this a pack expansion type. */
17319 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17322 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17324 TREE_CHAIN (base) = bases;
17328 /* Peek at the next token. */
17329 token = cp_lexer_peek_token (parser->lexer);
17330 /* If it's not a comma, then the list is complete. */
17331 if (token->type != CPP_COMMA)
17333 /* Consume the `,'. */
17334 cp_lexer_consume_token (parser->lexer);
17337 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17338 base class had a qualified name. However, the next name that
17339 appears is certainly not qualified. */
17340 parser->scope = NULL_TREE;
17341 parser->qualifying_scope = NULL_TREE;
17342 parser->object_scope = NULL_TREE;
17344 return nreverse (bases);
17347 /* Parse a base-specifier.
17350 :: [opt] nested-name-specifier [opt] class-name
17351 virtual access-specifier [opt] :: [opt] nested-name-specifier
17353 access-specifier virtual [opt] :: [opt] nested-name-specifier
17356 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17357 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17358 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17359 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17362 cp_parser_base_specifier (cp_parser* parser)
17366 bool virtual_p = false;
17367 bool duplicate_virtual_error_issued_p = false;
17368 bool duplicate_access_error_issued_p = false;
17369 bool class_scope_p, template_p;
17370 tree access = access_default_node;
17373 /* Process the optional `virtual' and `access-specifier'. */
17376 /* Peek at the next token. */
17377 token = cp_lexer_peek_token (parser->lexer);
17378 /* Process `virtual'. */
17379 switch (token->keyword)
17382 /* If `virtual' appears more than once, issue an error. */
17383 if (virtual_p && !duplicate_virtual_error_issued_p)
17385 cp_parser_error (parser,
17386 "%<virtual%> specified more than once in base-specified");
17387 duplicate_virtual_error_issued_p = true;
17392 /* Consume the `virtual' token. */
17393 cp_lexer_consume_token (parser->lexer);
17398 case RID_PROTECTED:
17400 /* If more than one access specifier appears, issue an
17402 if (access != access_default_node
17403 && !duplicate_access_error_issued_p)
17405 cp_parser_error (parser,
17406 "more than one access specifier in base-specified");
17407 duplicate_access_error_issued_p = true;
17410 access = ridpointers[(int) token->keyword];
17412 /* Consume the access-specifier. */
17413 cp_lexer_consume_token (parser->lexer);
17422 /* It is not uncommon to see programs mechanically, erroneously, use
17423 the 'typename' keyword to denote (dependent) qualified types
17424 as base classes. */
17425 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17427 token = cp_lexer_peek_token (parser->lexer);
17428 if (!processing_template_decl)
17429 error_at (token->location,
17430 "keyword %<typename%> not allowed outside of templates");
17432 error_at (token->location,
17433 "keyword %<typename%> not allowed in this context "
17434 "(the base class is implicitly a type)");
17435 cp_lexer_consume_token (parser->lexer);
17438 /* Look for the optional `::' operator. */
17439 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17440 /* Look for the nested-name-specifier. The simplest way to
17445 The keyword `typename' is not permitted in a base-specifier or
17446 mem-initializer; in these contexts a qualified name that
17447 depends on a template-parameter is implicitly assumed to be a
17450 is to pretend that we have seen the `typename' keyword at this
17452 cp_parser_nested_name_specifier_opt (parser,
17453 /*typename_keyword_p=*/true,
17454 /*check_dependency_p=*/true,
17456 /*is_declaration=*/true);
17457 /* If the base class is given by a qualified name, assume that names
17458 we see are type names or templates, as appropriate. */
17459 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17460 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17462 /* Finally, look for the class-name. */
17463 type = cp_parser_class_name (parser,
17467 /*check_dependency_p=*/true,
17468 /*class_head_p=*/false,
17469 /*is_declaration=*/true);
17471 if (type == error_mark_node)
17472 return error_mark_node;
17474 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17477 /* Exception handling [gram.exception] */
17479 /* Parse an (optional) exception-specification.
17481 exception-specification:
17482 throw ( type-id-list [opt] )
17484 Returns a TREE_LIST representing the exception-specification. The
17485 TREE_VALUE of each node is a type. */
17488 cp_parser_exception_specification_opt (cp_parser* parser)
17493 /* Peek at the next token. */
17494 token = cp_lexer_peek_token (parser->lexer);
17495 /* If it's not `throw', then there's no exception-specification. */
17496 if (!cp_parser_is_keyword (token, RID_THROW))
17499 /* Consume the `throw'. */
17500 cp_lexer_consume_token (parser->lexer);
17502 /* Look for the `('. */
17503 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17505 /* Peek at the next token. */
17506 token = cp_lexer_peek_token (parser->lexer);
17507 /* If it's not a `)', then there is a type-id-list. */
17508 if (token->type != CPP_CLOSE_PAREN)
17510 const char *saved_message;
17512 /* Types may not be defined in an exception-specification. */
17513 saved_message = parser->type_definition_forbidden_message;
17514 parser->type_definition_forbidden_message
17515 = G_("types may not be defined in an exception-specification");
17516 /* Parse the type-id-list. */
17517 type_id_list = cp_parser_type_id_list (parser);
17518 /* Restore the saved message. */
17519 parser->type_definition_forbidden_message = saved_message;
17522 type_id_list = empty_except_spec;
17524 /* Look for the `)'. */
17525 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17527 return type_id_list;
17530 /* Parse an (optional) type-id-list.
17534 type-id-list , type-id ... [opt]
17536 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17537 in the order that the types were presented. */
17540 cp_parser_type_id_list (cp_parser* parser)
17542 tree types = NULL_TREE;
17549 /* Get the next type-id. */
17550 type = cp_parser_type_id (parser);
17551 /* Parse the optional ellipsis. */
17552 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17554 /* Consume the `...'. */
17555 cp_lexer_consume_token (parser->lexer);
17557 /* Turn the type into a pack expansion expression. */
17558 type = make_pack_expansion (type);
17560 /* Add it to the list. */
17561 types = add_exception_specifier (types, type, /*complain=*/1);
17562 /* Peek at the next token. */
17563 token = cp_lexer_peek_token (parser->lexer);
17564 /* If it is not a `,', we are done. */
17565 if (token->type != CPP_COMMA)
17567 /* Consume the `,'. */
17568 cp_lexer_consume_token (parser->lexer);
17571 return nreverse (types);
17574 /* Parse a try-block.
17577 try compound-statement handler-seq */
17580 cp_parser_try_block (cp_parser* parser)
17584 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
17585 try_block = begin_try_block ();
17586 cp_parser_compound_statement (parser, NULL, true);
17587 finish_try_block (try_block);
17588 cp_parser_handler_seq (parser);
17589 finish_handler_sequence (try_block);
17594 /* Parse a function-try-block.
17596 function-try-block:
17597 try ctor-initializer [opt] function-body handler-seq */
17600 cp_parser_function_try_block (cp_parser* parser)
17602 tree compound_stmt;
17604 bool ctor_initializer_p;
17606 /* Look for the `try' keyword. */
17607 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
17609 /* Let the rest of the front end know where we are. */
17610 try_block = begin_function_try_block (&compound_stmt);
17611 /* Parse the function-body. */
17613 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17614 /* We're done with the `try' part. */
17615 finish_function_try_block (try_block);
17616 /* Parse the handlers. */
17617 cp_parser_handler_seq (parser);
17618 /* We're done with the handlers. */
17619 finish_function_handler_sequence (try_block, compound_stmt);
17621 return ctor_initializer_p;
17624 /* Parse a handler-seq.
17627 handler handler-seq [opt] */
17630 cp_parser_handler_seq (cp_parser* parser)
17636 /* Parse the handler. */
17637 cp_parser_handler (parser);
17638 /* Peek at the next token. */
17639 token = cp_lexer_peek_token (parser->lexer);
17640 /* If it's not `catch' then there are no more handlers. */
17641 if (!cp_parser_is_keyword (token, RID_CATCH))
17646 /* Parse a handler.
17649 catch ( exception-declaration ) compound-statement */
17652 cp_parser_handler (cp_parser* parser)
17657 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
17658 handler = begin_handler ();
17659 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17660 declaration = cp_parser_exception_declaration (parser);
17661 finish_handler_parms (declaration, handler);
17662 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17663 cp_parser_compound_statement (parser, NULL, false);
17664 finish_handler (handler);
17667 /* Parse an exception-declaration.
17669 exception-declaration:
17670 type-specifier-seq declarator
17671 type-specifier-seq abstract-declarator
17675 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17676 ellipsis variant is used. */
17679 cp_parser_exception_declaration (cp_parser* parser)
17681 cp_decl_specifier_seq type_specifiers;
17682 cp_declarator *declarator;
17683 const char *saved_message;
17685 /* If it's an ellipsis, it's easy to handle. */
17686 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17688 /* Consume the `...' token. */
17689 cp_lexer_consume_token (parser->lexer);
17693 /* Types may not be defined in exception-declarations. */
17694 saved_message = parser->type_definition_forbidden_message;
17695 parser->type_definition_forbidden_message
17696 = G_("types may not be defined in exception-declarations");
17698 /* Parse the type-specifier-seq. */
17699 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17700 /*is_trailing_return=*/false,
17702 /* If it's a `)', then there is no declarator. */
17703 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17706 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17707 /*ctor_dtor_or_conv_p=*/NULL,
17708 /*parenthesized_p=*/NULL,
17709 /*member_p=*/false);
17711 /* Restore the saved message. */
17712 parser->type_definition_forbidden_message = saved_message;
17714 if (!type_specifiers.any_specifiers_p)
17715 return error_mark_node;
17717 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17720 /* Parse a throw-expression.
17723 throw assignment-expression [opt]
17725 Returns a THROW_EXPR representing the throw-expression. */
17728 cp_parser_throw_expression (cp_parser* parser)
17733 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
17734 token = cp_lexer_peek_token (parser->lexer);
17735 /* Figure out whether or not there is an assignment-expression
17736 following the "throw" keyword. */
17737 if (token->type == CPP_COMMA
17738 || token->type == CPP_SEMICOLON
17739 || token->type == CPP_CLOSE_PAREN
17740 || token->type == CPP_CLOSE_SQUARE
17741 || token->type == CPP_CLOSE_BRACE
17742 || token->type == CPP_COLON)
17743 expression = NULL_TREE;
17745 expression = cp_parser_assignment_expression (parser,
17746 /*cast_p=*/false, NULL);
17748 return build_throw (expression);
17751 /* GNU Extensions */
17753 /* Parse an (optional) asm-specification.
17756 asm ( string-literal )
17758 If the asm-specification is present, returns a STRING_CST
17759 corresponding to the string-literal. Otherwise, returns
17763 cp_parser_asm_specification_opt (cp_parser* parser)
17766 tree asm_specification;
17768 /* Peek at the next token. */
17769 token = cp_lexer_peek_token (parser->lexer);
17770 /* If the next token isn't the `asm' keyword, then there's no
17771 asm-specification. */
17772 if (!cp_parser_is_keyword (token, RID_ASM))
17775 /* Consume the `asm' token. */
17776 cp_lexer_consume_token (parser->lexer);
17777 /* Look for the `('. */
17778 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17780 /* Look for the string-literal. */
17781 asm_specification = cp_parser_string_literal (parser, false, false);
17783 /* Look for the `)'. */
17784 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17786 return asm_specification;
17789 /* Parse an asm-operand-list.
17793 asm-operand-list , asm-operand
17796 string-literal ( expression )
17797 [ string-literal ] string-literal ( expression )
17799 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17800 each node is the expression. The TREE_PURPOSE is itself a
17801 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17802 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17803 is a STRING_CST for the string literal before the parenthesis. Returns
17804 ERROR_MARK_NODE if any of the operands are invalid. */
17807 cp_parser_asm_operand_list (cp_parser* parser)
17809 tree asm_operands = NULL_TREE;
17810 bool invalid_operands = false;
17814 tree string_literal;
17818 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17820 /* Consume the `[' token. */
17821 cp_lexer_consume_token (parser->lexer);
17822 /* Read the operand name. */
17823 name = cp_parser_identifier (parser);
17824 if (name != error_mark_node)
17825 name = build_string (IDENTIFIER_LENGTH (name),
17826 IDENTIFIER_POINTER (name));
17827 /* Look for the closing `]'. */
17828 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17832 /* Look for the string-literal. */
17833 string_literal = cp_parser_string_literal (parser, false, false);
17835 /* Look for the `('. */
17836 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17837 /* Parse the expression. */
17838 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17839 /* Look for the `)'. */
17840 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17842 if (name == error_mark_node
17843 || string_literal == error_mark_node
17844 || expression == error_mark_node)
17845 invalid_operands = true;
17847 /* Add this operand to the list. */
17848 asm_operands = tree_cons (build_tree_list (name, string_literal),
17851 /* If the next token is not a `,', there are no more
17853 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17855 /* Consume the `,'. */
17856 cp_lexer_consume_token (parser->lexer);
17859 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17862 /* Parse an asm-clobber-list.
17866 asm-clobber-list , string-literal
17868 Returns a TREE_LIST, indicating the clobbers in the order that they
17869 appeared. The TREE_VALUE of each node is a STRING_CST. */
17872 cp_parser_asm_clobber_list (cp_parser* parser)
17874 tree clobbers = NULL_TREE;
17878 tree string_literal;
17880 /* Look for the string literal. */
17881 string_literal = cp_parser_string_literal (parser, false, false);
17882 /* Add it to the list. */
17883 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17884 /* If the next token is not a `,', then the list is
17886 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17888 /* Consume the `,' token. */
17889 cp_lexer_consume_token (parser->lexer);
17895 /* Parse an asm-label-list.
17899 asm-label-list , identifier
17901 Returns a TREE_LIST, indicating the labels in the order that they
17902 appeared. The TREE_VALUE of each node is a label. */
17905 cp_parser_asm_label_list (cp_parser* parser)
17907 tree labels = NULL_TREE;
17911 tree identifier, label, name;
17913 /* Look for the identifier. */
17914 identifier = cp_parser_identifier (parser);
17915 if (!error_operand_p (identifier))
17917 label = lookup_label (identifier);
17918 if (TREE_CODE (label) == LABEL_DECL)
17920 TREE_USED (label) = 1;
17921 check_goto (label);
17922 name = build_string (IDENTIFIER_LENGTH (identifier),
17923 IDENTIFIER_POINTER (identifier));
17924 labels = tree_cons (name, label, labels);
17927 /* If the next token is not a `,', then the list is
17929 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17931 /* Consume the `,' token. */
17932 cp_lexer_consume_token (parser->lexer);
17935 return nreverse (labels);
17938 /* Parse an (optional) series of attributes.
17941 attributes attribute
17944 __attribute__ (( attribute-list [opt] ))
17946 The return value is as for cp_parser_attribute_list. */
17949 cp_parser_attributes_opt (cp_parser* parser)
17951 tree attributes = NULL_TREE;
17956 tree attribute_list;
17958 /* Peek at the next token. */
17959 token = cp_lexer_peek_token (parser->lexer);
17960 /* If it's not `__attribute__', then we're done. */
17961 if (token->keyword != RID_ATTRIBUTE)
17964 /* Consume the `__attribute__' keyword. */
17965 cp_lexer_consume_token (parser->lexer);
17966 /* Look for the two `(' tokens. */
17967 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17968 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17970 /* Peek at the next token. */
17971 token = cp_lexer_peek_token (parser->lexer);
17972 if (token->type != CPP_CLOSE_PAREN)
17973 /* Parse the attribute-list. */
17974 attribute_list = cp_parser_attribute_list (parser);
17976 /* If the next token is a `)', then there is no attribute
17978 attribute_list = NULL;
17980 /* Look for the two `)' tokens. */
17981 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17982 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17984 /* Add these new attributes to the list. */
17985 attributes = chainon (attributes, attribute_list);
17991 /* Parse an attribute-list.
17995 attribute-list , attribute
17999 identifier ( identifier )
18000 identifier ( identifier , expression-list )
18001 identifier ( expression-list )
18003 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18004 to an attribute. The TREE_PURPOSE of each node is the identifier
18005 indicating which attribute is in use. The TREE_VALUE represents
18006 the arguments, if any. */
18009 cp_parser_attribute_list (cp_parser* parser)
18011 tree attribute_list = NULL_TREE;
18012 bool save_translate_strings_p = parser->translate_strings_p;
18014 parser->translate_strings_p = false;
18021 /* Look for the identifier. We also allow keywords here; for
18022 example `__attribute__ ((const))' is legal. */
18023 token = cp_lexer_peek_token (parser->lexer);
18024 if (token->type == CPP_NAME
18025 || token->type == CPP_KEYWORD)
18027 tree arguments = NULL_TREE;
18029 /* Consume the token. */
18030 token = cp_lexer_consume_token (parser->lexer);
18032 /* Save away the identifier that indicates which attribute
18034 identifier = (token->type == CPP_KEYWORD)
18035 /* For keywords, use the canonical spelling, not the
18036 parsed identifier. */
18037 ? ridpointers[(int) token->keyword]
18040 attribute = build_tree_list (identifier, NULL_TREE);
18042 /* Peek at the next token. */
18043 token = cp_lexer_peek_token (parser->lexer);
18044 /* If it's an `(', then parse the attribute arguments. */
18045 if (token->type == CPP_OPEN_PAREN)
18048 int attr_flag = (attribute_takes_identifier_p (identifier)
18049 ? id_attr : normal_attr);
18050 vec = cp_parser_parenthesized_expression_list
18051 (parser, attr_flag, /*cast_p=*/false,
18052 /*allow_expansion_p=*/false,
18053 /*non_constant_p=*/NULL);
18055 arguments = error_mark_node;
18058 arguments = build_tree_list_vec (vec);
18059 release_tree_vector (vec);
18061 /* Save the arguments away. */
18062 TREE_VALUE (attribute) = arguments;
18065 if (arguments != error_mark_node)
18067 /* Add this attribute to the list. */
18068 TREE_CHAIN (attribute) = attribute_list;
18069 attribute_list = attribute;
18072 token = cp_lexer_peek_token (parser->lexer);
18074 /* Now, look for more attributes. If the next token isn't a
18075 `,', we're done. */
18076 if (token->type != CPP_COMMA)
18079 /* Consume the comma and keep going. */
18080 cp_lexer_consume_token (parser->lexer);
18082 parser->translate_strings_p = save_translate_strings_p;
18084 /* We built up the list in reverse order. */
18085 return nreverse (attribute_list);
18088 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18089 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18090 current value of the PEDANTIC flag, regardless of whether or not
18091 the `__extension__' keyword is present. The caller is responsible
18092 for restoring the value of the PEDANTIC flag. */
18095 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18097 /* Save the old value of the PEDANTIC flag. */
18098 *saved_pedantic = pedantic;
18100 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18102 /* Consume the `__extension__' token. */
18103 cp_lexer_consume_token (parser->lexer);
18104 /* We're not being pedantic while the `__extension__' keyword is
18114 /* Parse a label declaration.
18117 __label__ label-declarator-seq ;
18119 label-declarator-seq:
18120 identifier , label-declarator-seq
18124 cp_parser_label_declaration (cp_parser* parser)
18126 /* Look for the `__label__' keyword. */
18127 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18133 /* Look for an identifier. */
18134 identifier = cp_parser_identifier (parser);
18135 /* If we failed, stop. */
18136 if (identifier == error_mark_node)
18138 /* Declare it as a label. */
18139 finish_label_decl (identifier);
18140 /* If the next token is a `;', stop. */
18141 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18143 /* Look for the `,' separating the label declarations. */
18144 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18147 /* Look for the final `;'. */
18148 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18151 /* Support Functions */
18153 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18154 NAME should have one of the representations used for an
18155 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18156 is returned. If PARSER->SCOPE is a dependent type, then a
18157 SCOPE_REF is returned.
18159 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18160 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18161 was formed. Abstractly, such entities should not be passed to this
18162 function, because they do not need to be looked up, but it is
18163 simpler to check for this special case here, rather than at the
18166 In cases not explicitly covered above, this function returns a
18167 DECL, OVERLOAD, or baselink representing the result of the lookup.
18168 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18171 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18172 (e.g., "struct") that was used. In that case bindings that do not
18173 refer to types are ignored.
18175 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18178 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18181 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18184 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18185 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18186 NULL_TREE otherwise. */
18189 cp_parser_lookup_name (cp_parser *parser, tree name,
18190 enum tag_types tag_type,
18193 bool check_dependency,
18194 tree *ambiguous_decls,
18195 location_t name_location)
18199 tree object_type = parser->context->object_type;
18201 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18202 flags |= LOOKUP_COMPLAIN;
18204 /* Assume that the lookup will be unambiguous. */
18205 if (ambiguous_decls)
18206 *ambiguous_decls = NULL_TREE;
18208 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18209 no longer valid. Note that if we are parsing tentatively, and
18210 the parse fails, OBJECT_TYPE will be automatically restored. */
18211 parser->context->object_type = NULL_TREE;
18213 if (name == error_mark_node)
18214 return error_mark_node;
18216 /* A template-id has already been resolved; there is no lookup to
18218 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18220 if (BASELINK_P (name))
18222 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
18223 == TEMPLATE_ID_EXPR);
18227 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
18228 it should already have been checked to make sure that the name
18229 used matches the type being destroyed. */
18230 if (TREE_CODE (name) == BIT_NOT_EXPR)
18234 /* Figure out to which type this destructor applies. */
18236 type = parser->scope;
18237 else if (object_type)
18238 type = object_type;
18240 type = current_class_type;
18241 /* If that's not a class type, there is no destructor. */
18242 if (!type || !CLASS_TYPE_P (type))
18243 return error_mark_node;
18244 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
18245 lazily_declare_fn (sfk_destructor, type);
18246 if (!CLASSTYPE_DESTRUCTORS (type))
18247 return error_mark_node;
18248 /* If it was a class type, return the destructor. */
18249 return CLASSTYPE_DESTRUCTORS (type);
18252 /* By this point, the NAME should be an ordinary identifier. If
18253 the id-expression was a qualified name, the qualifying scope is
18254 stored in PARSER->SCOPE at this point. */
18255 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
18257 /* Perform the lookup. */
18262 if (parser->scope == error_mark_node)
18263 return error_mark_node;
18265 /* If the SCOPE is dependent, the lookup must be deferred until
18266 the template is instantiated -- unless we are explicitly
18267 looking up names in uninstantiated templates. Even then, we
18268 cannot look up the name if the scope is not a class type; it
18269 might, for example, be a template type parameter. */
18270 dependent_p = (TYPE_P (parser->scope)
18271 && dependent_scope_p (parser->scope));
18272 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18274 /* Defer lookup. */
18275 decl = error_mark_node;
18278 tree pushed_scope = NULL_TREE;
18280 /* If PARSER->SCOPE is a dependent type, then it must be a
18281 class type, and we must not be checking dependencies;
18282 otherwise, we would have processed this lookup above. So
18283 that PARSER->SCOPE is not considered a dependent base by
18284 lookup_member, we must enter the scope here. */
18286 pushed_scope = push_scope (parser->scope);
18288 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18289 lookup result and the nested-name-specifier nominates a class C:
18290 * if the name specified after the nested-name-specifier, when
18291 looked up in C, is the injected-class-name of C (Clause 9), or
18292 * if the name specified after the nested-name-specifier is the
18293 same as the identifier or the simple-template-id's template-
18294 name in the last component of the nested-name-specifier,
18295 the name is instead considered to name the constructor of
18296 class C. [ Note: for example, the constructor is not an
18297 acceptable lookup result in an elaborated-type-specifier so
18298 the constructor would not be used in place of the
18299 injected-class-name. --end note ] Such a constructor name
18300 shall be used only in the declarator-id of a declaration that
18301 names a constructor or in a using-declaration. */
18302 if (tag_type == none_type
18303 && CLASS_TYPE_P (parser->scope)
18304 && constructor_name_p (name, parser->scope))
18305 name = ctor_identifier;
18307 /* If the PARSER->SCOPE is a template specialization, it
18308 may be instantiated during name lookup. In that case,
18309 errors may be issued. Even if we rollback the current
18310 tentative parse, those errors are valid. */
18311 decl = lookup_qualified_name (parser->scope, name,
18312 tag_type != none_type,
18313 /*complain=*/true);
18315 /* If we have a single function from a using decl, pull it out. */
18316 if (TREE_CODE (decl) == OVERLOAD
18317 && !really_overloaded_fn (decl))
18318 decl = OVL_FUNCTION (decl);
18321 pop_scope (pushed_scope);
18324 /* If the scope is a dependent type and either we deferred lookup or
18325 we did lookup but didn't find the name, rememeber the name. */
18326 if (decl == error_mark_node && TYPE_P (parser->scope)
18327 && dependent_type_p (parser->scope))
18333 /* The resolution to Core Issue 180 says that `struct
18334 A::B' should be considered a type-name, even if `A'
18336 type = make_typename_type (parser->scope, name, tag_type,
18337 /*complain=*/tf_error);
18338 decl = TYPE_NAME (type);
18340 else if (is_template
18341 && (cp_parser_next_token_ends_template_argument_p (parser)
18342 || cp_lexer_next_token_is (parser->lexer,
18344 decl = make_unbound_class_template (parser->scope,
18346 /*complain=*/tf_error);
18348 decl = build_qualified_name (/*type=*/NULL_TREE,
18349 parser->scope, name,
18352 parser->qualifying_scope = parser->scope;
18353 parser->object_scope = NULL_TREE;
18355 else if (object_type)
18357 tree object_decl = NULL_TREE;
18358 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18359 OBJECT_TYPE is not a class. */
18360 if (CLASS_TYPE_P (object_type))
18361 /* If the OBJECT_TYPE is a template specialization, it may
18362 be instantiated during name lookup. In that case, errors
18363 may be issued. Even if we rollback the current tentative
18364 parse, those errors are valid. */
18365 object_decl = lookup_member (object_type,
18368 tag_type != none_type);
18369 /* Look it up in the enclosing context, too. */
18370 decl = lookup_name_real (name, tag_type != none_type,
18372 /*block_p=*/true, is_namespace, flags);
18373 parser->object_scope = object_type;
18374 parser->qualifying_scope = NULL_TREE;
18376 decl = object_decl;
18380 decl = lookup_name_real (name, tag_type != none_type,
18382 /*block_p=*/true, is_namespace, flags);
18383 parser->qualifying_scope = NULL_TREE;
18384 parser->object_scope = NULL_TREE;
18387 /* If the lookup failed, let our caller know. */
18388 if (!decl || decl == error_mark_node)
18389 return error_mark_node;
18391 /* Pull out the template from an injected-class-name (or multiple). */
18393 decl = maybe_get_template_decl_from_type_decl (decl);
18395 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18396 if (TREE_CODE (decl) == TREE_LIST)
18398 if (ambiguous_decls)
18399 *ambiguous_decls = decl;
18400 /* The error message we have to print is too complicated for
18401 cp_parser_error, so we incorporate its actions directly. */
18402 if (!cp_parser_simulate_error (parser))
18404 error_at (name_location, "reference to %qD is ambiguous",
18406 print_candidates (decl);
18408 return error_mark_node;
18411 gcc_assert (DECL_P (decl)
18412 || TREE_CODE (decl) == OVERLOAD
18413 || TREE_CODE (decl) == SCOPE_REF
18414 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18415 || BASELINK_P (decl));
18417 /* If we have resolved the name of a member declaration, check to
18418 see if the declaration is accessible. When the name resolves to
18419 set of overloaded functions, accessibility is checked when
18420 overload resolution is done.
18422 During an explicit instantiation, access is not checked at all,
18423 as per [temp.explicit]. */
18425 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18430 /* Like cp_parser_lookup_name, but for use in the typical case where
18431 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18432 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18435 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18437 return cp_parser_lookup_name (parser, name,
18439 /*is_template=*/false,
18440 /*is_namespace=*/false,
18441 /*check_dependency=*/true,
18442 /*ambiguous_decls=*/NULL,
18446 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18447 the current context, return the TYPE_DECL. If TAG_NAME_P is
18448 true, the DECL indicates the class being defined in a class-head,
18449 or declared in an elaborated-type-specifier.
18451 Otherwise, return DECL. */
18454 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18456 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18457 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18460 template <typename T> struct B;
18463 template <typename T> struct A::B {};
18465 Similarly, in an elaborated-type-specifier:
18467 namespace N { struct X{}; }
18470 template <typename T> friend struct N::X;
18473 However, if the DECL refers to a class type, and we are in
18474 the scope of the class, then the name lookup automatically
18475 finds the TYPE_DECL created by build_self_reference rather
18476 than a TEMPLATE_DECL. For example, in:
18478 template <class T> struct S {
18482 there is no need to handle such case. */
18484 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18485 return DECL_TEMPLATE_RESULT (decl);
18490 /* If too many, or too few, template-parameter lists apply to the
18491 declarator, issue an error message. Returns TRUE if all went well,
18492 and FALSE otherwise. */
18495 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18496 cp_declarator *declarator,
18497 location_t declarator_location)
18499 unsigned num_templates;
18501 /* We haven't seen any classes that involve template parameters yet. */
18504 switch (declarator->kind)
18507 if (declarator->u.id.qualifying_scope)
18511 scope = declarator->u.id.qualifying_scope;
18513 while (scope && CLASS_TYPE_P (scope))
18515 /* You're supposed to have one `template <...>'
18516 for every template class, but you don't need one
18517 for a full specialization. For example:
18519 template <class T> struct S{};
18520 template <> struct S<int> { void f(); };
18521 void S<int>::f () {}
18523 is correct; there shouldn't be a `template <>' for
18524 the definition of `S<int>::f'. */
18525 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18526 /* If SCOPE does not have template information of any
18527 kind, then it is not a template, nor is it nested
18528 within a template. */
18530 if (explicit_class_specialization_p (scope))
18532 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18535 scope = TYPE_CONTEXT (scope);
18538 else if (TREE_CODE (declarator->u.id.unqualified_name)
18539 == TEMPLATE_ID_EXPR)
18540 /* If the DECLARATOR has the form `X<y>' then it uses one
18541 additional level of template parameters. */
18544 return cp_parser_check_template_parameters
18545 (parser, num_templates, declarator_location, declarator);
18551 case cdk_reference:
18553 return (cp_parser_check_declarator_template_parameters
18554 (parser, declarator->declarator, declarator_location));
18560 gcc_unreachable ();
18565 /* NUM_TEMPLATES were used in the current declaration. If that is
18566 invalid, return FALSE and issue an error messages. Otherwise,
18567 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18568 declarator and we can print more accurate diagnostics. */
18571 cp_parser_check_template_parameters (cp_parser* parser,
18572 unsigned num_templates,
18573 location_t location,
18574 cp_declarator *declarator)
18576 /* If there are the same number of template classes and parameter
18577 lists, that's OK. */
18578 if (parser->num_template_parameter_lists == num_templates)
18580 /* If there are more, but only one more, then we are referring to a
18581 member template. That's OK too. */
18582 if (parser->num_template_parameter_lists == num_templates + 1)
18584 /* If there are more template classes than parameter lists, we have
18587 template <class T> void S<T>::R<T>::f (); */
18588 if (parser->num_template_parameter_lists < num_templates)
18590 if (declarator && !current_function_decl)
18591 error_at (location, "specializing member %<%T::%E%> "
18592 "requires %<template<>%> syntax",
18593 declarator->u.id.qualifying_scope,
18594 declarator->u.id.unqualified_name);
18595 else if (declarator)
18596 error_at (location, "invalid declaration of %<%T::%E%>",
18597 declarator->u.id.qualifying_scope,
18598 declarator->u.id.unqualified_name);
18600 error_at (location, "too few template-parameter-lists");
18603 /* Otherwise, there are too many template parameter lists. We have
18606 template <class T> template <class U> void S::f(); */
18607 error_at (location, "too many template-parameter-lists");
18611 /* Parse an optional `::' token indicating that the following name is
18612 from the global namespace. If so, PARSER->SCOPE is set to the
18613 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18614 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18615 Returns the new value of PARSER->SCOPE, if the `::' token is
18616 present, and NULL_TREE otherwise. */
18619 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18623 /* Peek at the next token. */
18624 token = cp_lexer_peek_token (parser->lexer);
18625 /* If we're looking at a `::' token then we're starting from the
18626 global namespace, not our current location. */
18627 if (token->type == CPP_SCOPE)
18629 /* Consume the `::' token. */
18630 cp_lexer_consume_token (parser->lexer);
18631 /* Set the SCOPE so that we know where to start the lookup. */
18632 parser->scope = global_namespace;
18633 parser->qualifying_scope = global_namespace;
18634 parser->object_scope = NULL_TREE;
18636 return parser->scope;
18638 else if (!current_scope_valid_p)
18640 parser->scope = NULL_TREE;
18641 parser->qualifying_scope = NULL_TREE;
18642 parser->object_scope = NULL_TREE;
18648 /* Returns TRUE if the upcoming token sequence is the start of a
18649 constructor declarator. If FRIEND_P is true, the declarator is
18650 preceded by the `friend' specifier. */
18653 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18655 bool constructor_p;
18656 tree nested_name_specifier;
18657 cp_token *next_token;
18659 /* The common case is that this is not a constructor declarator, so
18660 try to avoid doing lots of work if at all possible. It's not
18661 valid declare a constructor at function scope. */
18662 if (parser->in_function_body)
18664 /* And only certain tokens can begin a constructor declarator. */
18665 next_token = cp_lexer_peek_token (parser->lexer);
18666 if (next_token->type != CPP_NAME
18667 && next_token->type != CPP_SCOPE
18668 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18669 && next_token->type != CPP_TEMPLATE_ID)
18672 /* Parse tentatively; we are going to roll back all of the tokens
18674 cp_parser_parse_tentatively (parser);
18675 /* Assume that we are looking at a constructor declarator. */
18676 constructor_p = true;
18678 /* Look for the optional `::' operator. */
18679 cp_parser_global_scope_opt (parser,
18680 /*current_scope_valid_p=*/false);
18681 /* Look for the nested-name-specifier. */
18682 nested_name_specifier
18683 = (cp_parser_nested_name_specifier_opt (parser,
18684 /*typename_keyword_p=*/false,
18685 /*check_dependency_p=*/false,
18687 /*is_declaration=*/false));
18688 /* Outside of a class-specifier, there must be a
18689 nested-name-specifier. */
18690 if (!nested_name_specifier &&
18691 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18693 constructor_p = false;
18694 else if (nested_name_specifier == error_mark_node)
18695 constructor_p = false;
18697 /* If we have a class scope, this is easy; DR 147 says that S::S always
18698 names the constructor, and no other qualified name could. */
18699 if (constructor_p && nested_name_specifier
18700 && TYPE_P (nested_name_specifier))
18702 tree id = cp_parser_unqualified_id (parser,
18703 /*template_keyword_p=*/false,
18704 /*check_dependency_p=*/false,
18705 /*declarator_p=*/true,
18706 /*optional_p=*/false);
18707 if (is_overloaded_fn (id))
18708 id = DECL_NAME (get_first_fn (id));
18709 if (!constructor_name_p (id, nested_name_specifier))
18710 constructor_p = false;
18712 /* If we still think that this might be a constructor-declarator,
18713 look for a class-name. */
18714 else if (constructor_p)
18718 template <typename T> struct S {
18722 we must recognize that the nested `S' names a class. */
18724 type_decl = cp_parser_class_name (parser,
18725 /*typename_keyword_p=*/false,
18726 /*template_keyword_p=*/false,
18728 /*check_dependency_p=*/false,
18729 /*class_head_p=*/false,
18730 /*is_declaration=*/false);
18731 /* If there was no class-name, then this is not a constructor. */
18732 constructor_p = !cp_parser_error_occurred (parser);
18734 /* If we're still considering a constructor, we have to see a `(',
18735 to begin the parameter-declaration-clause, followed by either a
18736 `)', an `...', or a decl-specifier. We need to check for a
18737 type-specifier to avoid being fooled into thinking that:
18741 is a constructor. (It is actually a function named `f' that
18742 takes one parameter (of type `int') and returns a value of type
18745 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18746 constructor_p = false;
18749 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18750 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18751 /* A parameter declaration begins with a decl-specifier,
18752 which is either the "attribute" keyword, a storage class
18753 specifier, or (usually) a type-specifier. */
18754 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18757 tree pushed_scope = NULL_TREE;
18758 unsigned saved_num_template_parameter_lists;
18760 /* Names appearing in the type-specifier should be looked up
18761 in the scope of the class. */
18762 if (current_class_type)
18766 type = TREE_TYPE (type_decl);
18767 if (TREE_CODE (type) == TYPENAME_TYPE)
18769 type = resolve_typename_type (type,
18770 /*only_current_p=*/false);
18771 if (TREE_CODE (type) == TYPENAME_TYPE)
18773 cp_parser_abort_tentative_parse (parser);
18777 pushed_scope = push_scope (type);
18780 /* Inside the constructor parameter list, surrounding
18781 template-parameter-lists do not apply. */
18782 saved_num_template_parameter_lists
18783 = parser->num_template_parameter_lists;
18784 parser->num_template_parameter_lists = 0;
18786 /* Look for the type-specifier. */
18787 cp_parser_type_specifier (parser,
18788 CP_PARSER_FLAGS_NONE,
18789 /*decl_specs=*/NULL,
18790 /*is_declarator=*/true,
18791 /*declares_class_or_enum=*/NULL,
18792 /*is_cv_qualifier=*/NULL);
18794 parser->num_template_parameter_lists
18795 = saved_num_template_parameter_lists;
18797 /* Leave the scope of the class. */
18799 pop_scope (pushed_scope);
18801 constructor_p = !cp_parser_error_occurred (parser);
18805 /* We did not really want to consume any tokens. */
18806 cp_parser_abort_tentative_parse (parser);
18808 return constructor_p;
18811 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18812 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18813 they must be performed once we are in the scope of the function.
18815 Returns the function defined. */
18818 cp_parser_function_definition_from_specifiers_and_declarator
18819 (cp_parser* parser,
18820 cp_decl_specifier_seq *decl_specifiers,
18822 const cp_declarator *declarator)
18827 /* Begin the function-definition. */
18828 success_p = start_function (decl_specifiers, declarator, attributes);
18830 /* The things we're about to see are not directly qualified by any
18831 template headers we've seen thus far. */
18832 reset_specialization ();
18834 /* If there were names looked up in the decl-specifier-seq that we
18835 did not check, check them now. We must wait until we are in the
18836 scope of the function to perform the checks, since the function
18837 might be a friend. */
18838 perform_deferred_access_checks ();
18842 /* Skip the entire function. */
18843 cp_parser_skip_to_end_of_block_or_statement (parser);
18844 fn = error_mark_node;
18846 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18848 /* Seen already, skip it. An error message has already been output. */
18849 cp_parser_skip_to_end_of_block_or_statement (parser);
18850 fn = current_function_decl;
18851 current_function_decl = NULL_TREE;
18852 /* If this is a function from a class, pop the nested class. */
18853 if (current_class_name)
18854 pop_nested_class ();
18857 fn = cp_parser_function_definition_after_declarator (parser,
18858 /*inline_p=*/false);
18863 /* Parse the part of a function-definition that follows the
18864 declarator. INLINE_P is TRUE iff this function is an inline
18865 function defined within a class-specifier.
18867 Returns the function defined. */
18870 cp_parser_function_definition_after_declarator (cp_parser* parser,
18874 bool ctor_initializer_p = false;
18875 bool saved_in_unbraced_linkage_specification_p;
18876 bool saved_in_function_body;
18877 unsigned saved_num_template_parameter_lists;
18880 saved_in_function_body = parser->in_function_body;
18881 parser->in_function_body = true;
18882 /* If the next token is `return', then the code may be trying to
18883 make use of the "named return value" extension that G++ used to
18885 token = cp_lexer_peek_token (parser->lexer);
18886 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18888 /* Consume the `return' keyword. */
18889 cp_lexer_consume_token (parser->lexer);
18890 /* Look for the identifier that indicates what value is to be
18892 cp_parser_identifier (parser);
18893 /* Issue an error message. */
18894 error_at (token->location,
18895 "named return values are no longer supported");
18896 /* Skip tokens until we reach the start of the function body. */
18899 cp_token *token = cp_lexer_peek_token (parser->lexer);
18900 if (token->type == CPP_OPEN_BRACE
18901 || token->type == CPP_EOF
18902 || token->type == CPP_PRAGMA_EOL)
18904 cp_lexer_consume_token (parser->lexer);
18907 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18908 anything declared inside `f'. */
18909 saved_in_unbraced_linkage_specification_p
18910 = parser->in_unbraced_linkage_specification_p;
18911 parser->in_unbraced_linkage_specification_p = false;
18912 /* Inside the function, surrounding template-parameter-lists do not
18914 saved_num_template_parameter_lists
18915 = parser->num_template_parameter_lists;
18916 parser->num_template_parameter_lists = 0;
18918 start_lambda_scope (current_function_decl);
18920 /* If the next token is `try', then we are looking at a
18921 function-try-block. */
18922 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18923 ctor_initializer_p = cp_parser_function_try_block (parser);
18924 /* A function-try-block includes the function-body, so we only do
18925 this next part if we're not processing a function-try-block. */
18928 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18930 finish_lambda_scope ();
18932 /* Finish the function. */
18933 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18934 (inline_p ? 2 : 0));
18935 /* Generate code for it, if necessary. */
18936 expand_or_defer_fn (fn);
18937 /* Restore the saved values. */
18938 parser->in_unbraced_linkage_specification_p
18939 = saved_in_unbraced_linkage_specification_p;
18940 parser->num_template_parameter_lists
18941 = saved_num_template_parameter_lists;
18942 parser->in_function_body = saved_in_function_body;
18947 /* Parse a template-declaration, assuming that the `export' (and
18948 `extern') keywords, if present, has already been scanned. MEMBER_P
18949 is as for cp_parser_template_declaration. */
18952 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18954 tree decl = NULL_TREE;
18955 VEC (deferred_access_check,gc) *checks;
18956 tree parameter_list;
18957 bool friend_p = false;
18958 bool need_lang_pop;
18961 /* Look for the `template' keyword. */
18962 token = cp_lexer_peek_token (parser->lexer);
18963 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
18967 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
18969 if (at_class_scope_p () && current_function_decl)
18971 /* 14.5.2.2 [temp.mem]
18973 A local class shall not have member templates. */
18974 error_at (token->location,
18975 "invalid declaration of member template in local class");
18976 cp_parser_skip_to_end_of_block_or_statement (parser);
18981 A template ... shall not have C linkage. */
18982 if (current_lang_name == lang_name_c)
18984 error_at (token->location, "template with C linkage");
18985 /* Give it C++ linkage to avoid confusing other parts of the
18987 push_lang_context (lang_name_cplusplus);
18988 need_lang_pop = true;
18991 need_lang_pop = false;
18993 /* We cannot perform access checks on the template parameter
18994 declarations until we know what is being declared, just as we
18995 cannot check the decl-specifier list. */
18996 push_deferring_access_checks (dk_deferred);
18998 /* If the next token is `>', then we have an invalid
18999 specialization. Rather than complain about an invalid template
19000 parameter, issue an error message here. */
19001 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19003 cp_parser_error (parser, "invalid explicit specialization");
19004 begin_specialization ();
19005 parameter_list = NULL_TREE;
19008 /* Parse the template parameters. */
19009 parameter_list = cp_parser_template_parameter_list (parser);
19011 /* Get the deferred access checks from the parameter list. These
19012 will be checked once we know what is being declared, as for a
19013 member template the checks must be performed in the scope of the
19014 class containing the member. */
19015 checks = get_deferred_access_checks ();
19017 /* Look for the `>'. */
19018 cp_parser_skip_to_end_of_template_parameter_list (parser);
19019 /* We just processed one more parameter list. */
19020 ++parser->num_template_parameter_lists;
19021 /* If the next token is `template', there are more template
19023 if (cp_lexer_next_token_is_keyword (parser->lexer,
19025 cp_parser_template_declaration_after_export (parser, member_p);
19028 /* There are no access checks when parsing a template, as we do not
19029 know if a specialization will be a friend. */
19030 push_deferring_access_checks (dk_no_check);
19031 token = cp_lexer_peek_token (parser->lexer);
19032 decl = cp_parser_single_declaration (parser,
19035 /*explicit_specialization_p=*/false,
19037 pop_deferring_access_checks ();
19039 /* If this is a member template declaration, let the front
19041 if (member_p && !friend_p && decl)
19043 if (TREE_CODE (decl) == TYPE_DECL)
19044 cp_parser_check_access_in_redeclaration (decl, token->location);
19046 decl = finish_member_template_decl (decl);
19048 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19049 make_friend_class (current_class_type, TREE_TYPE (decl),
19050 /*complain=*/true);
19052 /* We are done with the current parameter list. */
19053 --parser->num_template_parameter_lists;
19055 pop_deferring_access_checks ();
19058 finish_template_decl (parameter_list);
19060 /* Register member declarations. */
19061 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19062 finish_member_declaration (decl);
19063 /* For the erroneous case of a template with C linkage, we pushed an
19064 implicit C++ linkage scope; exit that scope now. */
19066 pop_lang_context ();
19067 /* If DECL is a function template, we must return to parse it later.
19068 (Even though there is no definition, there might be default
19069 arguments that need handling.) */
19070 if (member_p && decl
19071 && (TREE_CODE (decl) == FUNCTION_DECL
19072 || DECL_FUNCTION_TEMPLATE_P (decl)))
19073 TREE_VALUE (parser->unparsed_functions_queues)
19074 = tree_cons (NULL_TREE, decl,
19075 TREE_VALUE (parser->unparsed_functions_queues));
19078 /* Perform the deferred access checks from a template-parameter-list.
19079 CHECKS is a TREE_LIST of access checks, as returned by
19080 get_deferred_access_checks. */
19083 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19085 ++processing_template_parmlist;
19086 perform_access_checks (checks);
19087 --processing_template_parmlist;
19090 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19091 `function-definition' sequence. MEMBER_P is true, this declaration
19092 appears in a class scope.
19094 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19095 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19098 cp_parser_single_declaration (cp_parser* parser,
19099 VEC (deferred_access_check,gc)* checks,
19101 bool explicit_specialization_p,
19104 int declares_class_or_enum;
19105 tree decl = NULL_TREE;
19106 cp_decl_specifier_seq decl_specifiers;
19107 bool function_definition_p = false;
19108 cp_token *decl_spec_token_start;
19110 /* This function is only used when processing a template
19112 gcc_assert (innermost_scope_kind () == sk_template_parms
19113 || innermost_scope_kind () == sk_template_spec);
19115 /* Defer access checks until we know what is being declared. */
19116 push_deferring_access_checks (dk_deferred);
19118 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19120 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19121 cp_parser_decl_specifier_seq (parser,
19122 CP_PARSER_FLAGS_OPTIONAL,
19124 &declares_class_or_enum);
19126 *friend_p = cp_parser_friend_p (&decl_specifiers);
19128 /* There are no template typedefs. */
19129 if (decl_specifiers.specs[(int) ds_typedef])
19131 error_at (decl_spec_token_start->location,
19132 "template declaration of %<typedef%>");
19133 decl = error_mark_node;
19136 /* Gather up the access checks that occurred the
19137 decl-specifier-seq. */
19138 stop_deferring_access_checks ();
19140 /* Check for the declaration of a template class. */
19141 if (declares_class_or_enum)
19143 if (cp_parser_declares_only_class_p (parser))
19145 decl = shadow_tag (&decl_specifiers);
19150 friend template <typename T> struct A<T>::B;
19153 A<T>::B will be represented by a TYPENAME_TYPE, and
19154 therefore not recognized by shadow_tag. */
19155 if (friend_p && *friend_p
19157 && decl_specifiers.type
19158 && TYPE_P (decl_specifiers.type))
19159 decl = decl_specifiers.type;
19161 if (decl && decl != error_mark_node)
19162 decl = TYPE_NAME (decl);
19164 decl = error_mark_node;
19166 /* Perform access checks for template parameters. */
19167 cp_parser_perform_template_parameter_access_checks (checks);
19171 /* Complain about missing 'typename' or other invalid type names. */
19172 if (!decl_specifiers.any_type_specifiers_p)
19173 cp_parser_parse_and_diagnose_invalid_type_name (parser);
19175 /* If it's not a template class, try for a template function. If
19176 the next token is a `;', then this declaration does not declare
19177 anything. But, if there were errors in the decl-specifiers, then
19178 the error might well have come from an attempted class-specifier.
19179 In that case, there's no need to warn about a missing declarator. */
19181 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19182 || decl_specifiers.type != error_mark_node))
19184 decl = cp_parser_init_declarator (parser,
19187 /*function_definition_allowed_p=*/true,
19189 declares_class_or_enum,
19190 &function_definition_p);
19192 /* 7.1.1-1 [dcl.stc]
19194 A storage-class-specifier shall not be specified in an explicit
19195 specialization... */
19197 && explicit_specialization_p
19198 && decl_specifiers.storage_class != sc_none)
19200 error_at (decl_spec_token_start->location,
19201 "explicit template specialization cannot have a storage class");
19202 decl = error_mark_node;
19206 pop_deferring_access_checks ();
19208 /* Clear any current qualification; whatever comes next is the start
19209 of something new. */
19210 parser->scope = NULL_TREE;
19211 parser->qualifying_scope = NULL_TREE;
19212 parser->object_scope = NULL_TREE;
19213 /* Look for a trailing `;' after the declaration. */
19214 if (!function_definition_p
19215 && (decl == error_mark_node
19216 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19217 cp_parser_skip_to_end_of_block_or_statement (parser);
19222 /* Parse a cast-expression that is not the operand of a unary "&". */
19225 cp_parser_simple_cast_expression (cp_parser *parser)
19227 return cp_parser_cast_expression (parser, /*address_p=*/false,
19228 /*cast_p=*/false, NULL);
19231 /* Parse a functional cast to TYPE. Returns an expression
19232 representing the cast. */
19235 cp_parser_functional_cast (cp_parser* parser, tree type)
19238 tree expression_list;
19242 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19244 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19245 expression_list = cp_parser_braced_list (parser, &nonconst_p);
19246 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
19247 if (TREE_CODE (type) == TYPE_DECL)
19248 type = TREE_TYPE (type);
19249 return finish_compound_literal (type, expression_list);
19253 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19255 /*allow_expansion_p=*/true,
19256 /*non_constant_p=*/NULL);
19258 expression_list = error_mark_node;
19261 expression_list = build_tree_list_vec (vec);
19262 release_tree_vector (vec);
19265 cast = build_functional_cast (type, expression_list,
19266 tf_warning_or_error);
19267 /* [expr.const]/1: In an integral constant expression "only type
19268 conversions to integral or enumeration type can be used". */
19269 if (TREE_CODE (type) == TYPE_DECL)
19270 type = TREE_TYPE (type);
19271 if (cast != error_mark_node
19272 && !cast_valid_in_integral_constant_expression_p (type)
19273 && cp_parser_non_integral_constant_expression (parser,
19275 return error_mark_node;
19279 /* Save the tokens that make up the body of a member function defined
19280 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19281 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19282 specifiers applied to the declaration. Returns the FUNCTION_DECL
19283 for the member function. */
19286 cp_parser_save_member_function_body (cp_parser* parser,
19287 cp_decl_specifier_seq *decl_specifiers,
19288 cp_declarator *declarator,
19295 /* Create the FUNCTION_DECL. */
19296 fn = grokmethod (decl_specifiers, declarator, attributes);
19297 /* If something went badly wrong, bail out now. */
19298 if (fn == error_mark_node)
19300 /* If there's a function-body, skip it. */
19301 if (cp_parser_token_starts_function_definition_p
19302 (cp_lexer_peek_token (parser->lexer)))
19303 cp_parser_skip_to_end_of_block_or_statement (parser);
19304 return error_mark_node;
19307 /* Remember it, if there default args to post process. */
19308 cp_parser_save_default_args (parser, fn);
19310 /* Save away the tokens that make up the body of the
19312 first = parser->lexer->next_token;
19313 /* We can have braced-init-list mem-initializers before the fn body. */
19314 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19316 cp_lexer_consume_token (parser->lexer);
19317 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19318 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19320 /* cache_group will stop after an un-nested { } pair, too. */
19321 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19324 /* variadic mem-inits have ... after the ')'. */
19325 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19326 cp_lexer_consume_token (parser->lexer);
19329 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19330 /* Handle function try blocks. */
19331 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19332 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19333 last = parser->lexer->next_token;
19335 /* Save away the inline definition; we will process it when the
19336 class is complete. */
19337 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19338 DECL_PENDING_INLINE_P (fn) = 1;
19340 /* We need to know that this was defined in the class, so that
19341 friend templates are handled correctly. */
19342 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19344 /* Add FN to the queue of functions to be parsed later. */
19345 TREE_VALUE (parser->unparsed_functions_queues)
19346 = tree_cons (NULL_TREE, fn,
19347 TREE_VALUE (parser->unparsed_functions_queues));
19352 /* Parse a template-argument-list, as well as the trailing ">" (but
19353 not the opening ">"). See cp_parser_template_argument_list for the
19357 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19361 tree saved_qualifying_scope;
19362 tree saved_object_scope;
19363 bool saved_greater_than_is_operator_p;
19364 int saved_unevaluated_operand;
19365 int saved_inhibit_evaluation_warnings;
19369 When parsing a template-id, the first non-nested `>' is taken as
19370 the end of the template-argument-list rather than a greater-than
19372 saved_greater_than_is_operator_p
19373 = parser->greater_than_is_operator_p;
19374 parser->greater_than_is_operator_p = false;
19375 /* Parsing the argument list may modify SCOPE, so we save it
19377 saved_scope = parser->scope;
19378 saved_qualifying_scope = parser->qualifying_scope;
19379 saved_object_scope = parser->object_scope;
19380 /* We need to evaluate the template arguments, even though this
19381 template-id may be nested within a "sizeof". */
19382 saved_unevaluated_operand = cp_unevaluated_operand;
19383 cp_unevaluated_operand = 0;
19384 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19385 c_inhibit_evaluation_warnings = 0;
19386 /* Parse the template-argument-list itself. */
19387 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19388 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19389 arguments = NULL_TREE;
19391 arguments = cp_parser_template_argument_list (parser);
19392 /* Look for the `>' that ends the template-argument-list. If we find
19393 a '>>' instead, it's probably just a typo. */
19394 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19396 if (cxx_dialect != cxx98)
19398 /* In C++0x, a `>>' in a template argument list or cast
19399 expression is considered to be two separate `>'
19400 tokens. So, change the current token to a `>', but don't
19401 consume it: it will be consumed later when the outer
19402 template argument list (or cast expression) is parsed.
19403 Note that this replacement of `>' for `>>' is necessary
19404 even if we are parsing tentatively: in the tentative
19405 case, after calling
19406 cp_parser_enclosed_template_argument_list we will always
19407 throw away all of the template arguments and the first
19408 closing `>', either because the template argument list
19409 was erroneous or because we are replacing those tokens
19410 with a CPP_TEMPLATE_ID token. The second `>' (which will
19411 not have been thrown away) is needed either to close an
19412 outer template argument list or to complete a new-style
19414 cp_token *token = cp_lexer_peek_token (parser->lexer);
19415 token->type = CPP_GREATER;
19417 else if (!saved_greater_than_is_operator_p)
19419 /* If we're in a nested template argument list, the '>>' has
19420 to be a typo for '> >'. We emit the error message, but we
19421 continue parsing and we push a '>' as next token, so that
19422 the argument list will be parsed correctly. Note that the
19423 global source location is still on the token before the
19424 '>>', so we need to say explicitly where we want it. */
19425 cp_token *token = cp_lexer_peek_token (parser->lexer);
19426 error_at (token->location, "%<>>%> should be %<> >%> "
19427 "within a nested template argument list");
19429 token->type = CPP_GREATER;
19433 /* If this is not a nested template argument list, the '>>'
19434 is a typo for '>'. Emit an error message and continue.
19435 Same deal about the token location, but here we can get it
19436 right by consuming the '>>' before issuing the diagnostic. */
19437 cp_token *token = cp_lexer_consume_token (parser->lexer);
19438 error_at (token->location,
19439 "spurious %<>>%>, use %<>%> to terminate "
19440 "a template argument list");
19444 cp_parser_skip_to_end_of_template_parameter_list (parser);
19445 /* The `>' token might be a greater-than operator again now. */
19446 parser->greater_than_is_operator_p
19447 = saved_greater_than_is_operator_p;
19448 /* Restore the SAVED_SCOPE. */
19449 parser->scope = saved_scope;
19450 parser->qualifying_scope = saved_qualifying_scope;
19451 parser->object_scope = saved_object_scope;
19452 cp_unevaluated_operand = saved_unevaluated_operand;
19453 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19458 /* MEMBER_FUNCTION is a member function, or a friend. If default
19459 arguments, or the body of the function have not yet been parsed,
19463 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19465 /* If this member is a template, get the underlying
19467 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19468 member_function = DECL_TEMPLATE_RESULT (member_function);
19470 /* There should not be any class definitions in progress at this
19471 point; the bodies of members are only parsed outside of all class
19473 gcc_assert (parser->num_classes_being_defined == 0);
19474 /* While we're parsing the member functions we might encounter more
19475 classes. We want to handle them right away, but we don't want
19476 them getting mixed up with functions that are currently in the
19478 parser->unparsed_functions_queues
19479 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19481 /* Make sure that any template parameters are in scope. */
19482 maybe_begin_member_template_processing (member_function);
19484 /* If the body of the function has not yet been parsed, parse it
19486 if (DECL_PENDING_INLINE_P (member_function))
19488 tree function_scope;
19489 cp_token_cache *tokens;
19491 /* The function is no longer pending; we are processing it. */
19492 tokens = DECL_PENDING_INLINE_INFO (member_function);
19493 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19494 DECL_PENDING_INLINE_P (member_function) = 0;
19496 /* If this is a local class, enter the scope of the containing
19498 function_scope = current_function_decl;
19499 if (function_scope)
19500 push_function_context ();
19502 /* Push the body of the function onto the lexer stack. */
19503 cp_parser_push_lexer_for_tokens (parser, tokens);
19505 /* Let the front end know that we going to be defining this
19507 start_preparsed_function (member_function, NULL_TREE,
19508 SF_PRE_PARSED | SF_INCLASS_INLINE);
19510 /* Don't do access checking if it is a templated function. */
19511 if (processing_template_decl)
19512 push_deferring_access_checks (dk_no_check);
19514 /* Now, parse the body of the function. */
19515 cp_parser_function_definition_after_declarator (parser,
19516 /*inline_p=*/true);
19518 if (processing_template_decl)
19519 pop_deferring_access_checks ();
19521 /* Leave the scope of the containing function. */
19522 if (function_scope)
19523 pop_function_context ();
19524 cp_parser_pop_lexer (parser);
19527 /* Remove any template parameters from the symbol table. */
19528 maybe_end_member_template_processing ();
19530 /* Restore the queue. */
19531 parser->unparsed_functions_queues
19532 = TREE_CHAIN (parser->unparsed_functions_queues);
19535 /* If DECL contains any default args, remember it on the unparsed
19536 functions queue. */
19539 cp_parser_save_default_args (cp_parser* parser, tree decl)
19543 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19545 probe = TREE_CHAIN (probe))
19546 if (TREE_PURPOSE (probe))
19548 TREE_PURPOSE (parser->unparsed_functions_queues)
19549 = tree_cons (current_class_type, decl,
19550 TREE_PURPOSE (parser->unparsed_functions_queues));
19555 /* FN is a FUNCTION_DECL which may contains a parameter with an
19556 unparsed DEFAULT_ARG. Parse the default args now. This function
19557 assumes that the current scope is the scope in which the default
19558 argument should be processed. */
19561 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19563 bool saved_local_variables_forbidden_p;
19564 tree parm, parmdecl;
19566 /* While we're parsing the default args, we might (due to the
19567 statement expression extension) encounter more classes. We want
19568 to handle them right away, but we don't want them getting mixed
19569 up with default args that are currently in the queue. */
19570 parser->unparsed_functions_queues
19571 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19573 /* Local variable names (and the `this' keyword) may not appear
19574 in a default argument. */
19575 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19576 parser->local_variables_forbidden_p = true;
19578 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19579 parmdecl = DECL_ARGUMENTS (fn);
19580 parm && parm != void_list_node;
19581 parm = TREE_CHAIN (parm),
19582 parmdecl = TREE_CHAIN (parmdecl))
19584 cp_token_cache *tokens;
19585 tree default_arg = TREE_PURPOSE (parm);
19587 VEC(tree,gc) *insts;
19594 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19595 /* This can happen for a friend declaration for a function
19596 already declared with default arguments. */
19599 /* Push the saved tokens for the default argument onto the parser's
19601 tokens = DEFARG_TOKENS (default_arg);
19602 cp_parser_push_lexer_for_tokens (parser, tokens);
19604 start_lambda_scope (parmdecl);
19606 /* Parse the assignment-expression. */
19607 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19608 if (parsed_arg == error_mark_node)
19610 cp_parser_pop_lexer (parser);
19614 if (!processing_template_decl)
19615 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19617 TREE_PURPOSE (parm) = parsed_arg;
19619 /* Update any instantiations we've already created. */
19620 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19621 VEC_iterate (tree, insts, ix, copy); ix++)
19622 TREE_PURPOSE (copy) = parsed_arg;
19624 finish_lambda_scope ();
19626 /* If the token stream has not been completely used up, then
19627 there was extra junk after the end of the default
19629 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19630 cp_parser_error (parser, "expected %<,%>");
19632 /* Revert to the main lexer. */
19633 cp_parser_pop_lexer (parser);
19636 /* Make sure no default arg is missing. */
19637 check_default_args (fn);
19639 /* Restore the state of local_variables_forbidden_p. */
19640 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19642 /* Restore the queue. */
19643 parser->unparsed_functions_queues
19644 = TREE_CHAIN (parser->unparsed_functions_queues);
19647 /* Parse the operand of `sizeof' (or a similar operator). Returns
19648 either a TYPE or an expression, depending on the form of the
19649 input. The KEYWORD indicates which kind of expression we have
19653 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19655 tree expr = NULL_TREE;
19656 const char *saved_message;
19658 bool saved_integral_constant_expression_p;
19659 bool saved_non_integral_constant_expression_p;
19660 bool pack_expansion_p = false;
19662 /* Types cannot be defined in a `sizeof' expression. Save away the
19664 saved_message = parser->type_definition_forbidden_message;
19665 /* And create the new one. */
19666 tmp = concat ("types may not be defined in %<",
19667 IDENTIFIER_POINTER (ridpointers[keyword]),
19668 "%> expressions", NULL);
19669 parser->type_definition_forbidden_message = tmp;
19671 /* The restrictions on constant-expressions do not apply inside
19672 sizeof expressions. */
19673 saved_integral_constant_expression_p
19674 = parser->integral_constant_expression_p;
19675 saved_non_integral_constant_expression_p
19676 = parser->non_integral_constant_expression_p;
19677 parser->integral_constant_expression_p = false;
19679 /* If it's a `...', then we are computing the length of a parameter
19681 if (keyword == RID_SIZEOF
19682 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19684 /* Consume the `...'. */
19685 cp_lexer_consume_token (parser->lexer);
19686 maybe_warn_variadic_templates ();
19688 /* Note that this is an expansion. */
19689 pack_expansion_p = true;
19692 /* Do not actually evaluate the expression. */
19693 ++cp_unevaluated_operand;
19694 ++c_inhibit_evaluation_warnings;
19695 /* If it's a `(', then we might be looking at the type-id
19697 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19700 bool saved_in_type_id_in_expr_p;
19702 /* We can't be sure yet whether we're looking at a type-id or an
19704 cp_parser_parse_tentatively (parser);
19705 /* Consume the `('. */
19706 cp_lexer_consume_token (parser->lexer);
19707 /* Parse the type-id. */
19708 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19709 parser->in_type_id_in_expr_p = true;
19710 type = cp_parser_type_id (parser);
19711 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19712 /* Now, look for the trailing `)'. */
19713 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19714 /* If all went well, then we're done. */
19715 if (cp_parser_parse_definitely (parser))
19717 cp_decl_specifier_seq decl_specs;
19719 /* Build a trivial decl-specifier-seq. */
19720 clear_decl_specs (&decl_specs);
19721 decl_specs.type = type;
19723 /* Call grokdeclarator to figure out what type this is. */
19724 expr = grokdeclarator (NULL,
19728 /*attrlist=*/NULL);
19732 /* If the type-id production did not work out, then we must be
19733 looking at the unary-expression production. */
19735 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19736 /*cast_p=*/false, NULL);
19738 if (pack_expansion_p)
19739 /* Build a pack expansion. */
19740 expr = make_pack_expansion (expr);
19742 /* Go back to evaluating expressions. */
19743 --cp_unevaluated_operand;
19744 --c_inhibit_evaluation_warnings;
19746 /* Free the message we created. */
19748 /* And restore the old one. */
19749 parser->type_definition_forbidden_message = saved_message;
19750 parser->integral_constant_expression_p
19751 = saved_integral_constant_expression_p;
19752 parser->non_integral_constant_expression_p
19753 = saved_non_integral_constant_expression_p;
19758 /* If the current declaration has no declarator, return true. */
19761 cp_parser_declares_only_class_p (cp_parser *parser)
19763 /* If the next token is a `;' or a `,' then there is no
19765 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19766 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19769 /* Update the DECL_SPECS to reflect the storage class indicated by
19773 cp_parser_set_storage_class (cp_parser *parser,
19774 cp_decl_specifier_seq *decl_specs,
19776 location_t location)
19778 cp_storage_class storage_class;
19780 if (parser->in_unbraced_linkage_specification_p)
19782 error_at (location, "invalid use of %qD in linkage specification",
19783 ridpointers[keyword]);
19786 else if (decl_specs->storage_class != sc_none)
19788 decl_specs->conflicting_specifiers_p = true;
19792 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19793 && decl_specs->specs[(int) ds_thread])
19795 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19796 decl_specs->specs[(int) ds_thread] = 0;
19802 storage_class = sc_auto;
19805 storage_class = sc_register;
19808 storage_class = sc_static;
19811 storage_class = sc_extern;
19814 storage_class = sc_mutable;
19817 gcc_unreachable ();
19819 decl_specs->storage_class = storage_class;
19821 /* A storage class specifier cannot be applied alongside a typedef
19822 specifier. If there is a typedef specifier present then set
19823 conflicting_specifiers_p which will trigger an error later
19824 on in grokdeclarator. */
19825 if (decl_specs->specs[(int)ds_typedef])
19826 decl_specs->conflicting_specifiers_p = true;
19829 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19830 is true, the type is a user-defined type; otherwise it is a
19831 built-in type specified by a keyword. */
19834 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19836 location_t location,
19837 bool user_defined_p)
19839 decl_specs->any_specifiers_p = true;
19841 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19842 (with, for example, in "typedef int wchar_t;") we remember that
19843 this is what happened. In system headers, we ignore these
19844 declarations so that G++ can work with system headers that are not
19846 if (decl_specs->specs[(int) ds_typedef]
19848 && (type_spec == boolean_type_node
19849 || type_spec == char16_type_node
19850 || type_spec == char32_type_node
19851 || type_spec == wchar_type_node)
19852 && (decl_specs->type
19853 || decl_specs->specs[(int) ds_long]
19854 || decl_specs->specs[(int) ds_short]
19855 || decl_specs->specs[(int) ds_unsigned]
19856 || decl_specs->specs[(int) ds_signed]))
19858 decl_specs->redefined_builtin_type = type_spec;
19859 if (!decl_specs->type)
19861 decl_specs->type = type_spec;
19862 decl_specs->user_defined_type_p = false;
19863 decl_specs->type_location = location;
19866 else if (decl_specs->type)
19867 decl_specs->multiple_types_p = true;
19870 decl_specs->type = type_spec;
19871 decl_specs->user_defined_type_p = user_defined_p;
19872 decl_specs->redefined_builtin_type = NULL_TREE;
19873 decl_specs->type_location = location;
19877 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19878 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19881 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19883 return decl_specifiers->specs[(int) ds_friend] != 0;
19886 /* Issue an error message indicating that TOKEN_DESC was expected.
19887 If KEYWORD is true, it indicated this function is called by
19888 cp_parser_require_keword and the required token can only be
19889 a indicated keyword. */
19892 cp_parser_required_error (cp_parser *parser,
19893 required_token token_desc,
19896 switch (token_desc)
19899 cp_parser_error (parser, "expected %<new%>");
19902 cp_parser_error (parser, "expected %<delete%>");
19905 cp_parser_error (parser, "expected %<return%>");
19908 cp_parser_error (parser, "expected %<while%>");
19911 cp_parser_error (parser, "expected %<extern%>");
19913 case RT_STATIC_ASSERT:
19914 cp_parser_error (parser, "expected %<static_assert%>");
19917 cp_parser_error (parser, "expected %<decltype%>");
19920 cp_parser_error (parser, "expected %<operator%>");
19923 cp_parser_error (parser, "expected %<class%>");
19926 cp_parser_error (parser, "expected %<template%>");
19929 cp_parser_error (parser, "expected %<namespace%>");
19932 cp_parser_error (parser, "expected %<using%>");
19935 cp_parser_error (parser, "expected %<asm%>");
19938 cp_parser_error (parser, "expected %<try%>");
19941 cp_parser_error (parser, "expected %<catch%>");
19944 cp_parser_error (parser, "expected %<throw%>");
19947 cp_parser_error (parser, "expected %<__label__%>");
19950 cp_parser_error (parser, "expected %<@try%>");
19952 case RT_AT_SYNCHRONIZED:
19953 cp_parser_error (parser, "expected %<@synchronized%>");
19956 cp_parser_error (parser, "expected %<@throw%>");
19963 switch (token_desc)
19966 cp_parser_error (parser, "expected %<;%>");
19968 case RT_OPEN_PAREN:
19969 cp_parser_error (parser, "expected %<(%>");
19971 case RT_CLOSE_BRACE:
19972 cp_parser_error (parser, "expected %<}%>");
19974 case RT_OPEN_BRACE:
19975 cp_parser_error (parser, "expected %<{%>");
19977 case RT_CLOSE_SQUARE:
19978 cp_parser_error (parser, "expected %<]%>");
19980 case RT_OPEN_SQUARE:
19981 cp_parser_error (parser, "expected %<[%>");
19984 cp_parser_error (parser, "expected %<,%>");
19987 cp_parser_error (parser, "expected %<::%>");
19990 cp_parser_error (parser, "expected %<<%>");
19993 cp_parser_error (parser, "expected %<>%>");
19996 cp_parser_error (parser, "expected %<=%>");
19999 cp_parser_error (parser, "expected %<...%>");
20002 cp_parser_error (parser, "expected %<*%>");
20005 cp_parser_error (parser, "expected %<~%>");
20008 cp_parser_error (parser, "expected %<:%>");
20010 case RT_COLON_SCOPE:
20011 cp_parser_error (parser, "expected %<:%> or %<::%>");
20013 case RT_CLOSE_PAREN:
20014 cp_parser_error (parser, "expected %<)%>");
20016 case RT_COMMA_CLOSE_PAREN:
20017 cp_parser_error (parser, "expected %<,%> or %<)%>");
20019 case RT_PRAGMA_EOL:
20020 cp_parser_error (parser, "expected end of line");
20023 cp_parser_error (parser, "expected identifier");
20026 cp_parser_error (parser, "expected selection-statement");
20028 case RT_INTERATION:
20029 cp_parser_error (parser, "expected iteration-statement");
20032 cp_parser_error (parser, "expected jump-statement");
20035 cp_parser_error (parser, "expected class-key");
20037 case RT_CLASS_TYPENAME_TEMPLATE:
20038 cp_parser_error (parser,
20039 "expected %<class%>, %<typename%>, or %<template%>");
20042 gcc_unreachable ();
20046 gcc_unreachable ();
20051 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20052 issue an error message indicating that TOKEN_DESC was expected.
20054 Returns the token consumed, if the token had the appropriate type.
20055 Otherwise, returns NULL. */
20058 cp_parser_require (cp_parser* parser,
20059 enum cpp_ttype type,
20060 required_token token_desc)
20062 if (cp_lexer_next_token_is (parser->lexer, type))
20063 return cp_lexer_consume_token (parser->lexer);
20066 /* Output the MESSAGE -- unless we're parsing tentatively. */
20067 if (!cp_parser_simulate_error (parser))
20068 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20073 /* An error message is produced if the next token is not '>'.
20074 All further tokens are skipped until the desired token is
20075 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20078 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20080 /* Current level of '< ... >'. */
20081 unsigned level = 0;
20082 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20083 unsigned nesting_depth = 0;
20085 /* Are we ready, yet? If not, issue error message. */
20086 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20089 /* Skip tokens until the desired token is found. */
20092 /* Peek at the next token. */
20093 switch (cp_lexer_peek_token (parser->lexer)->type)
20096 if (!nesting_depth)
20101 if (cxx_dialect == cxx98)
20102 /* C++0x views the `>>' operator as two `>' tokens, but
20105 else if (!nesting_depth && level-- == 0)
20107 /* We've hit a `>>' where the first `>' closes the
20108 template argument list, and the second `>' is
20109 spurious. Just consume the `>>' and stop; we've
20110 already produced at least one error. */
20111 cp_lexer_consume_token (parser->lexer);
20114 /* Fall through for C++0x, so we handle the second `>' in
20118 if (!nesting_depth && level-- == 0)
20120 /* We've reached the token we want, consume it and stop. */
20121 cp_lexer_consume_token (parser->lexer);
20126 case CPP_OPEN_PAREN:
20127 case CPP_OPEN_SQUARE:
20131 case CPP_CLOSE_PAREN:
20132 case CPP_CLOSE_SQUARE:
20133 if (nesting_depth-- == 0)
20138 case CPP_PRAGMA_EOL:
20139 case CPP_SEMICOLON:
20140 case CPP_OPEN_BRACE:
20141 case CPP_CLOSE_BRACE:
20142 /* The '>' was probably forgotten, don't look further. */
20149 /* Consume this token. */
20150 cp_lexer_consume_token (parser->lexer);
20154 /* If the next token is the indicated keyword, consume it. Otherwise,
20155 issue an error message indicating that TOKEN_DESC was expected.
20157 Returns the token consumed, if the token had the appropriate type.
20158 Otherwise, returns NULL. */
20161 cp_parser_require_keyword (cp_parser* parser,
20163 required_token token_desc)
20165 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20167 if (token && token->keyword != keyword)
20169 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
20176 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20177 function-definition. */
20180 cp_parser_token_starts_function_definition_p (cp_token* token)
20182 return (/* An ordinary function-body begins with an `{'. */
20183 token->type == CPP_OPEN_BRACE
20184 /* A ctor-initializer begins with a `:'. */
20185 || token->type == CPP_COLON
20186 /* A function-try-block begins with `try'. */
20187 || token->keyword == RID_TRY
20188 /* The named return value extension begins with `return'. */
20189 || token->keyword == RID_RETURN);
20192 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20196 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20200 token = cp_lexer_peek_token (parser->lexer);
20201 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20204 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20205 C++0x) ending a template-argument. */
20208 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20212 token = cp_lexer_peek_token (parser->lexer);
20213 return (token->type == CPP_COMMA
20214 || token->type == CPP_GREATER
20215 || token->type == CPP_ELLIPSIS
20216 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20219 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20220 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
20223 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
20228 token = cp_lexer_peek_nth_token (parser->lexer, n);
20229 if (token->type == CPP_LESS)
20231 /* Check for the sequence `<::' in the original code. It would be lexed as
20232 `[:', where `[' is a digraph, and there is no whitespace before
20234 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
20237 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
20238 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
20244 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20245 or none_type otherwise. */
20247 static enum tag_types
20248 cp_parser_token_is_class_key (cp_token* token)
20250 switch (token->keyword)
20255 return record_type;
20264 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
20267 cp_parser_check_class_key (enum tag_types class_key, tree type)
20269 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
20270 permerror (input_location, "%qs tag used in naming %q#T",
20271 class_key == union_type ? "union"
20272 : class_key == record_type ? "struct" : "class",
20276 /* Issue an error message if DECL is redeclared with different
20277 access than its original declaration [class.access.spec/3].
20278 This applies to nested classes and nested class templates.
20282 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
20284 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
20287 if ((TREE_PRIVATE (decl)
20288 != (current_access_specifier == access_private_node))
20289 || (TREE_PROTECTED (decl)
20290 != (current_access_specifier == access_protected_node)))
20291 error_at (location, "%qD redeclared with different access", decl);
20294 /* Look for the `template' keyword, as a syntactic disambiguator.
20295 Return TRUE iff it is present, in which case it will be
20299 cp_parser_optional_template_keyword (cp_parser *parser)
20301 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20303 /* The `template' keyword can only be used within templates;
20304 outside templates the parser can always figure out what is a
20305 template and what is not. */
20306 if (!processing_template_decl)
20308 cp_token *token = cp_lexer_peek_token (parser->lexer);
20309 error_at (token->location,
20310 "%<template%> (as a disambiguator) is only allowed "
20311 "within templates");
20312 /* If this part of the token stream is rescanned, the same
20313 error message would be generated. So, we purge the token
20314 from the stream. */
20315 cp_lexer_purge_token (parser->lexer);
20320 /* Consume the `template' keyword. */
20321 cp_lexer_consume_token (parser->lexer);
20329 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
20330 set PARSER->SCOPE, and perform other related actions. */
20333 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
20336 struct tree_check *check_value;
20337 deferred_access_check *chk;
20338 VEC (deferred_access_check,gc) *checks;
20340 /* Get the stored value. */
20341 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
20342 /* Perform any access checks that were deferred. */
20343 checks = check_value->checks;
20347 VEC_iterate (deferred_access_check, checks, i, chk) ;
20350 perform_or_defer_access_check (chk->binfo,
20355 /* Set the scope from the stored value. */
20356 parser->scope = check_value->value;
20357 parser->qualifying_scope = check_value->qualifying_scope;
20358 parser->object_scope = NULL_TREE;
20361 /* Consume tokens up through a non-nested END token. Returns TRUE if we
20362 encounter the end of a block before what we were looking for. */
20365 cp_parser_cache_group (cp_parser *parser,
20366 enum cpp_ttype end,
20371 cp_token *token = cp_lexer_peek_token (parser->lexer);
20373 /* Abort a parenthesized expression if we encounter a semicolon. */
20374 if ((end == CPP_CLOSE_PAREN || depth == 0)
20375 && token->type == CPP_SEMICOLON)
20377 /* If we've reached the end of the file, stop. */
20378 if (token->type == CPP_EOF
20379 || (end != CPP_PRAGMA_EOL
20380 && token->type == CPP_PRAGMA_EOL))
20382 if (token->type == CPP_CLOSE_BRACE && depth == 0)
20383 /* We've hit the end of an enclosing block, so there's been some
20384 kind of syntax error. */
20387 /* Consume the token. */
20388 cp_lexer_consume_token (parser->lexer);
20389 /* See if it starts a new group. */
20390 if (token->type == CPP_OPEN_BRACE)
20392 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
20393 /* In theory this should probably check end == '}', but
20394 cp_parser_save_member_function_body needs it to exit
20395 after either '}' or ')' when called with ')'. */
20399 else if (token->type == CPP_OPEN_PAREN)
20401 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
20402 if (depth == 0 && end == CPP_CLOSE_PAREN)
20405 else if (token->type == CPP_PRAGMA)
20406 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
20407 else if (token->type == end)
20412 /* Begin parsing tentatively. We always save tokens while parsing
20413 tentatively so that if the tentative parsing fails we can restore the
20417 cp_parser_parse_tentatively (cp_parser* parser)
20419 /* Enter a new parsing context. */
20420 parser->context = cp_parser_context_new (parser->context);
20421 /* Begin saving tokens. */
20422 cp_lexer_save_tokens (parser->lexer);
20423 /* In order to avoid repetitive access control error messages,
20424 access checks are queued up until we are no longer parsing
20426 push_deferring_access_checks (dk_deferred);
20429 /* Commit to the currently active tentative parse. */
20432 cp_parser_commit_to_tentative_parse (cp_parser* parser)
20434 cp_parser_context *context;
20437 /* Mark all of the levels as committed. */
20438 lexer = parser->lexer;
20439 for (context = parser->context; context->next; context = context->next)
20441 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
20443 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
20444 while (!cp_lexer_saving_tokens (lexer))
20445 lexer = lexer->next;
20446 cp_lexer_commit_tokens (lexer);
20450 /* Abort the currently active tentative parse. All consumed tokens
20451 will be rolled back, and no diagnostics will be issued. */
20454 cp_parser_abort_tentative_parse (cp_parser* parser)
20456 cp_parser_simulate_error (parser);
20457 /* Now, pretend that we want to see if the construct was
20458 successfully parsed. */
20459 cp_parser_parse_definitely (parser);
20462 /* Stop parsing tentatively. If a parse error has occurred, restore the
20463 token stream. Otherwise, commit to the tokens we have consumed.
20464 Returns true if no error occurred; false otherwise. */
20467 cp_parser_parse_definitely (cp_parser* parser)
20469 bool error_occurred;
20470 cp_parser_context *context;
20472 /* Remember whether or not an error occurred, since we are about to
20473 destroy that information. */
20474 error_occurred = cp_parser_error_occurred (parser);
20475 /* Remove the topmost context from the stack. */
20476 context = parser->context;
20477 parser->context = context->next;
20478 /* If no parse errors occurred, commit to the tentative parse. */
20479 if (!error_occurred)
20481 /* Commit to the tokens read tentatively, unless that was
20483 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20484 cp_lexer_commit_tokens (parser->lexer);
20486 pop_to_parent_deferring_access_checks ();
20488 /* Otherwise, if errors occurred, roll back our state so that things
20489 are just as they were before we began the tentative parse. */
20492 cp_lexer_rollback_tokens (parser->lexer);
20493 pop_deferring_access_checks ();
20495 /* Add the context to the front of the free list. */
20496 context->next = cp_parser_context_free_list;
20497 cp_parser_context_free_list = context;
20499 return !error_occurred;
20502 /* Returns true if we are parsing tentatively and are not committed to
20503 this tentative parse. */
20506 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20508 return (cp_parser_parsing_tentatively (parser)
20509 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20512 /* Returns nonzero iff an error has occurred during the most recent
20513 tentative parse. */
20516 cp_parser_error_occurred (cp_parser* parser)
20518 return (cp_parser_parsing_tentatively (parser)
20519 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20522 /* Returns nonzero if GNU extensions are allowed. */
20525 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20527 return parser->allow_gnu_extensions_p;
20530 /* Objective-C++ Productions */
20533 /* Parse an Objective-C expression, which feeds into a primary-expression
20537 objc-message-expression
20538 objc-string-literal
20539 objc-encode-expression
20540 objc-protocol-expression
20541 objc-selector-expression
20543 Returns a tree representation of the expression. */
20546 cp_parser_objc_expression (cp_parser* parser)
20548 /* Try to figure out what kind of declaration is present. */
20549 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20553 case CPP_OPEN_SQUARE:
20554 return cp_parser_objc_message_expression (parser);
20556 case CPP_OBJC_STRING:
20557 kwd = cp_lexer_consume_token (parser->lexer);
20558 return objc_build_string_object (kwd->u.value);
20561 switch (kwd->keyword)
20563 case RID_AT_ENCODE:
20564 return cp_parser_objc_encode_expression (parser);
20566 case RID_AT_PROTOCOL:
20567 return cp_parser_objc_protocol_expression (parser);
20569 case RID_AT_SELECTOR:
20570 return cp_parser_objc_selector_expression (parser);
20576 error_at (kwd->location,
20577 "misplaced %<@%D%> Objective-C++ construct",
20579 cp_parser_skip_to_end_of_block_or_statement (parser);
20582 return error_mark_node;
20585 /* Parse an Objective-C message expression.
20587 objc-message-expression:
20588 [ objc-message-receiver objc-message-args ]
20590 Returns a representation of an Objective-C message. */
20593 cp_parser_objc_message_expression (cp_parser* parser)
20595 tree receiver, messageargs;
20597 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20598 receiver = cp_parser_objc_message_receiver (parser);
20599 messageargs = cp_parser_objc_message_args (parser);
20600 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20602 return objc_build_message_expr (build_tree_list (receiver, messageargs));
20605 /* Parse an objc-message-receiver.
20607 objc-message-receiver:
20609 simple-type-specifier
20611 Returns a representation of the type or expression. */
20614 cp_parser_objc_message_receiver (cp_parser* parser)
20618 /* An Objective-C message receiver may be either (1) a type
20619 or (2) an expression. */
20620 cp_parser_parse_tentatively (parser);
20621 rcv = cp_parser_expression (parser, false, NULL);
20623 if (cp_parser_parse_definitely (parser))
20626 rcv = cp_parser_simple_type_specifier (parser,
20627 /*decl_specs=*/NULL,
20628 CP_PARSER_FLAGS_NONE);
20630 return objc_get_class_reference (rcv);
20633 /* Parse the arguments and selectors comprising an Objective-C message.
20638 objc-selector-args , objc-comma-args
20640 objc-selector-args:
20641 objc-selector [opt] : assignment-expression
20642 objc-selector-args objc-selector [opt] : assignment-expression
20645 assignment-expression
20646 objc-comma-args , assignment-expression
20648 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20649 selector arguments and TREE_VALUE containing a list of comma
20653 cp_parser_objc_message_args (cp_parser* parser)
20655 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20656 bool maybe_unary_selector_p = true;
20657 cp_token *token = cp_lexer_peek_token (parser->lexer);
20659 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20661 tree selector = NULL_TREE, arg;
20663 if (token->type != CPP_COLON)
20664 selector = cp_parser_objc_selector (parser);
20666 /* Detect if we have a unary selector. */
20667 if (maybe_unary_selector_p
20668 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20669 return build_tree_list (selector, NULL_TREE);
20671 maybe_unary_selector_p = false;
20672 cp_parser_require (parser, CPP_COLON, RT_COLON);
20673 arg = cp_parser_assignment_expression (parser, false, NULL);
20676 = chainon (sel_args,
20677 build_tree_list (selector, arg));
20679 token = cp_lexer_peek_token (parser->lexer);
20682 /* Handle non-selector arguments, if any. */
20683 while (token->type == CPP_COMMA)
20687 cp_lexer_consume_token (parser->lexer);
20688 arg = cp_parser_assignment_expression (parser, false, NULL);
20691 = chainon (addl_args,
20692 build_tree_list (NULL_TREE, arg));
20694 token = cp_lexer_peek_token (parser->lexer);
20697 return build_tree_list (sel_args, addl_args);
20700 /* Parse an Objective-C encode expression.
20702 objc-encode-expression:
20703 @encode objc-typename
20705 Returns an encoded representation of the type argument. */
20708 cp_parser_objc_encode_expression (cp_parser* parser)
20713 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20714 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20715 token = cp_lexer_peek_token (parser->lexer);
20716 type = complete_type (cp_parser_type_id (parser));
20717 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20721 error_at (token->location,
20722 "%<@encode%> must specify a type as an argument");
20723 return error_mark_node;
20726 return objc_build_encode_expr (type);
20729 /* Parse an Objective-C @defs expression. */
20732 cp_parser_objc_defs_expression (cp_parser *parser)
20736 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20737 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20738 name = cp_parser_identifier (parser);
20739 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20741 return objc_get_class_ivars (name);
20744 /* Parse an Objective-C protocol expression.
20746 objc-protocol-expression:
20747 @protocol ( identifier )
20749 Returns a representation of the protocol expression. */
20752 cp_parser_objc_protocol_expression (cp_parser* parser)
20756 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20757 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20758 proto = cp_parser_identifier (parser);
20759 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20761 return objc_build_protocol_expr (proto);
20764 /* Parse an Objective-C selector expression.
20766 objc-selector-expression:
20767 @selector ( objc-method-signature )
20769 objc-method-signature:
20775 objc-selector-seq objc-selector :
20777 Returns a representation of the method selector. */
20780 cp_parser_objc_selector_expression (cp_parser* parser)
20782 tree sel_seq = NULL_TREE;
20783 bool maybe_unary_selector_p = true;
20785 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20787 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20788 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20789 token = cp_lexer_peek_token (parser->lexer);
20791 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20792 || token->type == CPP_SCOPE)
20794 tree selector = NULL_TREE;
20796 if (token->type != CPP_COLON
20797 || token->type == CPP_SCOPE)
20798 selector = cp_parser_objc_selector (parser);
20800 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20801 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20803 /* Detect if we have a unary selector. */
20804 if (maybe_unary_selector_p)
20806 sel_seq = selector;
20807 goto finish_selector;
20811 cp_parser_error (parser, "expected %<:%>");
20814 maybe_unary_selector_p = false;
20815 token = cp_lexer_consume_token (parser->lexer);
20817 if (token->type == CPP_SCOPE)
20820 = chainon (sel_seq,
20821 build_tree_list (selector, NULL_TREE));
20823 = chainon (sel_seq,
20824 build_tree_list (NULL_TREE, NULL_TREE));
20828 = chainon (sel_seq,
20829 build_tree_list (selector, NULL_TREE));
20831 token = cp_lexer_peek_token (parser->lexer);
20835 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20837 return objc_build_selector_expr (loc, sel_seq);
20840 /* Parse a list of identifiers.
20842 objc-identifier-list:
20844 objc-identifier-list , identifier
20846 Returns a TREE_LIST of identifier nodes. */
20849 cp_parser_objc_identifier_list (cp_parser* parser)
20851 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20852 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20854 while (sep->type == CPP_COMMA)
20856 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20857 list = chainon (list,
20858 build_tree_list (NULL_TREE,
20859 cp_parser_identifier (parser)));
20860 sep = cp_lexer_peek_token (parser->lexer);
20866 /* Parse an Objective-C alias declaration.
20868 objc-alias-declaration:
20869 @compatibility_alias identifier identifier ;
20871 This function registers the alias mapping with the Objective-C front end.
20872 It returns nothing. */
20875 cp_parser_objc_alias_declaration (cp_parser* parser)
20879 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20880 alias = cp_parser_identifier (parser);
20881 orig = cp_parser_identifier (parser);
20882 objc_declare_alias (alias, orig);
20883 cp_parser_consume_semicolon_at_end_of_statement (parser);
20886 /* Parse an Objective-C class forward-declaration.
20888 objc-class-declaration:
20889 @class objc-identifier-list ;
20891 The function registers the forward declarations with the Objective-C
20892 front end. It returns nothing. */
20895 cp_parser_objc_class_declaration (cp_parser* parser)
20897 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20898 objc_declare_class (cp_parser_objc_identifier_list (parser));
20899 cp_parser_consume_semicolon_at_end_of_statement (parser);
20902 /* Parse a list of Objective-C protocol references.
20904 objc-protocol-refs-opt:
20905 objc-protocol-refs [opt]
20907 objc-protocol-refs:
20908 < objc-identifier-list >
20910 Returns a TREE_LIST of identifiers, if any. */
20913 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20915 tree protorefs = NULL_TREE;
20917 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20919 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20920 protorefs = cp_parser_objc_identifier_list (parser);
20921 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
20927 /* Parse a Objective-C visibility specification. */
20930 cp_parser_objc_visibility_spec (cp_parser* parser)
20932 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20934 switch (vis->keyword)
20936 case RID_AT_PRIVATE:
20937 objc_set_visibility (2);
20939 case RID_AT_PROTECTED:
20940 objc_set_visibility (0);
20942 case RID_AT_PUBLIC:
20943 objc_set_visibility (1);
20949 /* Eat '@private'/'@protected'/'@public'. */
20950 cp_lexer_consume_token (parser->lexer);
20953 /* Parse an Objective-C method type. */
20956 cp_parser_objc_method_type (cp_parser* parser)
20958 objc_set_method_type
20959 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20964 /* Parse an Objective-C protocol qualifier. */
20967 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20969 tree quals = NULL_TREE, node;
20970 cp_token *token = cp_lexer_peek_token (parser->lexer);
20972 node = token->u.value;
20974 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20975 && (node == ridpointers [(int) RID_IN]
20976 || node == ridpointers [(int) RID_OUT]
20977 || node == ridpointers [(int) RID_INOUT]
20978 || node == ridpointers [(int) RID_BYCOPY]
20979 || node == ridpointers [(int) RID_BYREF]
20980 || node == ridpointers [(int) RID_ONEWAY]))
20982 quals = tree_cons (NULL_TREE, node, quals);
20983 cp_lexer_consume_token (parser->lexer);
20984 token = cp_lexer_peek_token (parser->lexer);
20985 node = token->u.value;
20991 /* Parse an Objective-C typename. */
20994 cp_parser_objc_typename (cp_parser* parser)
20996 tree type_name = NULL_TREE;
20998 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21000 tree proto_quals, cp_type = NULL_TREE;
21002 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21003 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21005 /* An ObjC type name may consist of just protocol qualifiers, in which
21006 case the type shall default to 'id'. */
21007 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21008 cp_type = cp_parser_type_id (parser);
21010 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21011 type_name = build_tree_list (proto_quals, cp_type);
21017 /* Check to see if TYPE refers to an Objective-C selector name. */
21020 cp_parser_objc_selector_p (enum cpp_ttype type)
21022 return (type == CPP_NAME || type == CPP_KEYWORD
21023 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21024 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21025 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21026 || type == CPP_XOR || type == CPP_XOR_EQ);
21029 /* Parse an Objective-C selector. */
21032 cp_parser_objc_selector (cp_parser* parser)
21034 cp_token *token = cp_lexer_consume_token (parser->lexer);
21036 if (!cp_parser_objc_selector_p (token->type))
21038 error_at (token->location, "invalid Objective-C++ selector name");
21039 return error_mark_node;
21042 /* C++ operator names are allowed to appear in ObjC selectors. */
21043 switch (token->type)
21045 case CPP_AND_AND: return get_identifier ("and");
21046 case CPP_AND_EQ: return get_identifier ("and_eq");
21047 case CPP_AND: return get_identifier ("bitand");
21048 case CPP_OR: return get_identifier ("bitor");
21049 case CPP_COMPL: return get_identifier ("compl");
21050 case CPP_NOT: return get_identifier ("not");
21051 case CPP_NOT_EQ: return get_identifier ("not_eq");
21052 case CPP_OR_OR: return get_identifier ("or");
21053 case CPP_OR_EQ: return get_identifier ("or_eq");
21054 case CPP_XOR: return get_identifier ("xor");
21055 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21056 default: return token->u.value;
21060 /* Parse an Objective-C params list. */
21063 cp_parser_objc_method_keyword_params (cp_parser* parser)
21065 tree params = NULL_TREE;
21066 bool maybe_unary_selector_p = true;
21067 cp_token *token = cp_lexer_peek_token (parser->lexer);
21069 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21071 tree selector = NULL_TREE, type_name, identifier;
21073 if (token->type != CPP_COLON)
21074 selector = cp_parser_objc_selector (parser);
21076 /* Detect if we have a unary selector. */
21077 if (maybe_unary_selector_p
21078 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21081 maybe_unary_selector_p = false;
21082 cp_parser_require (parser, CPP_COLON, RT_COLON);
21083 type_name = cp_parser_objc_typename (parser);
21084 identifier = cp_parser_identifier (parser);
21088 objc_build_keyword_decl (selector,
21092 token = cp_lexer_peek_token (parser->lexer);
21098 /* Parse the non-keyword Objective-C params. */
21101 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
21103 tree params = make_node (TREE_LIST);
21104 cp_token *token = cp_lexer_peek_token (parser->lexer);
21105 *ellipsisp = false; /* Initially, assume no ellipsis. */
21107 while (token->type == CPP_COMMA)
21109 cp_parameter_declarator *parmdecl;
21112 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21113 token = cp_lexer_peek_token (parser->lexer);
21115 if (token->type == CPP_ELLIPSIS)
21117 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
21122 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21123 parm = grokdeclarator (parmdecl->declarator,
21124 &parmdecl->decl_specifiers,
21125 PARM, /*initialized=*/0,
21126 /*attrlist=*/NULL);
21128 chainon (params, build_tree_list (NULL_TREE, parm));
21129 token = cp_lexer_peek_token (parser->lexer);
21135 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
21138 cp_parser_objc_interstitial_code (cp_parser* parser)
21140 cp_token *token = cp_lexer_peek_token (parser->lexer);
21142 /* If the next token is `extern' and the following token is a string
21143 literal, then we have a linkage specification. */
21144 if (token->keyword == RID_EXTERN
21145 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
21146 cp_parser_linkage_specification (parser);
21147 /* Handle #pragma, if any. */
21148 else if (token->type == CPP_PRAGMA)
21149 cp_parser_pragma (parser, pragma_external);
21150 /* Allow stray semicolons. */
21151 else if (token->type == CPP_SEMICOLON)
21152 cp_lexer_consume_token (parser->lexer);
21153 /* Finally, try to parse a block-declaration, or a function-definition. */
21155 cp_parser_block_declaration (parser, /*statement_p=*/false);
21158 /* Parse a method signature. */
21161 cp_parser_objc_method_signature (cp_parser* parser)
21163 tree rettype, kwdparms, optparms;
21164 bool ellipsis = false;
21166 cp_parser_objc_method_type (parser);
21167 rettype = cp_parser_objc_typename (parser);
21168 kwdparms = cp_parser_objc_method_keyword_params (parser);
21169 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
21171 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
21174 /* Pars an Objective-C method prototype list. */
21177 cp_parser_objc_method_prototype_list (cp_parser* parser)
21179 cp_token *token = cp_lexer_peek_token (parser->lexer);
21181 while (token->keyword != RID_AT_END)
21183 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21185 objc_add_method_declaration
21186 (cp_parser_objc_method_signature (parser));
21187 cp_parser_consume_semicolon_at_end_of_statement (parser);
21190 /* Allow for interspersed non-ObjC++ code. */
21191 cp_parser_objc_interstitial_code (parser);
21193 token = cp_lexer_peek_token (parser->lexer);
21196 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21197 objc_finish_interface ();
21200 /* Parse an Objective-C method definition list. */
21203 cp_parser_objc_method_definition_list (cp_parser* parser)
21205 cp_token *token = cp_lexer_peek_token (parser->lexer);
21207 while (token->keyword != RID_AT_END)
21211 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21213 push_deferring_access_checks (dk_deferred);
21214 objc_start_method_definition
21215 (cp_parser_objc_method_signature (parser));
21217 /* For historical reasons, we accept an optional semicolon. */
21218 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21219 cp_lexer_consume_token (parser->lexer);
21221 perform_deferred_access_checks ();
21222 stop_deferring_access_checks ();
21223 meth = cp_parser_function_definition_after_declarator (parser,
21225 pop_deferring_access_checks ();
21226 objc_finish_method_definition (meth);
21229 /* Allow for interspersed non-ObjC++ code. */
21230 cp_parser_objc_interstitial_code (parser);
21232 token = cp_lexer_peek_token (parser->lexer);
21235 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21236 objc_finish_implementation ();
21239 /* Parse Objective-C ivars. */
21242 cp_parser_objc_class_ivars (cp_parser* parser)
21244 cp_token *token = cp_lexer_peek_token (parser->lexer);
21246 if (token->type != CPP_OPEN_BRACE)
21247 return; /* No ivars specified. */
21249 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
21250 token = cp_lexer_peek_token (parser->lexer);
21252 while (token->type != CPP_CLOSE_BRACE)
21254 cp_decl_specifier_seq declspecs;
21255 int decl_class_or_enum_p;
21256 tree prefix_attributes;
21258 cp_parser_objc_visibility_spec (parser);
21260 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21263 cp_parser_decl_specifier_seq (parser,
21264 CP_PARSER_FLAGS_OPTIONAL,
21266 &decl_class_or_enum_p);
21267 prefix_attributes = declspecs.attributes;
21268 declspecs.attributes = NULL_TREE;
21270 /* Keep going until we hit the `;' at the end of the
21272 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21274 tree width = NULL_TREE, attributes, first_attribute, decl;
21275 cp_declarator *declarator = NULL;
21276 int ctor_dtor_or_conv_p;
21278 /* Check for a (possibly unnamed) bitfield declaration. */
21279 token = cp_lexer_peek_token (parser->lexer);
21280 if (token->type == CPP_COLON)
21283 if (token->type == CPP_NAME
21284 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21287 /* Get the name of the bitfield. */
21288 declarator = make_id_declarator (NULL_TREE,
21289 cp_parser_identifier (parser),
21293 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
21294 /* Get the width of the bitfield. */
21296 = cp_parser_constant_expression (parser,
21297 /*allow_non_constant=*/false,
21302 /* Parse the declarator. */
21304 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21305 &ctor_dtor_or_conv_p,
21306 /*parenthesized_p=*/NULL,
21307 /*member_p=*/false);
21310 /* Look for attributes that apply to the ivar. */
21311 attributes = cp_parser_attributes_opt (parser);
21312 /* Remember which attributes are prefix attributes and
21314 first_attribute = attributes;
21315 /* Combine the attributes. */
21316 attributes = chainon (prefix_attributes, attributes);
21319 /* Create the bitfield declaration. */
21320 decl = grokbitfield (declarator, &declspecs,
21324 decl = grokfield (declarator, &declspecs,
21325 NULL_TREE, /*init_const_expr_p=*/false,
21326 NULL_TREE, attributes);
21328 /* Add the instance variable. */
21329 objc_add_instance_variable (decl);
21331 /* Reset PREFIX_ATTRIBUTES. */
21332 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21333 attributes = TREE_CHAIN (attributes);
21335 TREE_CHAIN (attributes) = NULL_TREE;
21337 token = cp_lexer_peek_token (parser->lexer);
21339 if (token->type == CPP_COMMA)
21341 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21347 cp_parser_consume_semicolon_at_end_of_statement (parser);
21348 token = cp_lexer_peek_token (parser->lexer);
21351 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
21352 /* For historical reasons, we accept an optional semicolon. */
21353 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21354 cp_lexer_consume_token (parser->lexer);
21357 /* Parse an Objective-C protocol declaration. */
21360 cp_parser_objc_protocol_declaration (cp_parser* parser)
21362 tree proto, protorefs;
21365 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21366 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
21368 tok = cp_lexer_peek_token (parser->lexer);
21369 error_at (tok->location, "identifier expected after %<@protocol%>");
21373 /* See if we have a forward declaration or a definition. */
21374 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
21376 /* Try a forward declaration first. */
21377 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
21379 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
21381 cp_parser_consume_semicolon_at_end_of_statement (parser);
21384 /* Ok, we got a full-fledged definition (or at least should). */
21387 proto = cp_parser_identifier (parser);
21388 protorefs = cp_parser_objc_protocol_refs_opt (parser);
21389 objc_start_protocol (proto, protorefs);
21390 cp_parser_objc_method_prototype_list (parser);
21394 /* Parse an Objective-C superclass or category. */
21397 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
21400 cp_token *next = cp_lexer_peek_token (parser->lexer);
21402 *super = *categ = NULL_TREE;
21403 if (next->type == CPP_COLON)
21405 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
21406 *super = cp_parser_identifier (parser);
21408 else if (next->type == CPP_OPEN_PAREN)
21410 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21411 *categ = cp_parser_identifier (parser);
21412 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21416 /* Parse an Objective-C class interface. */
21419 cp_parser_objc_class_interface (cp_parser* parser)
21421 tree name, super, categ, protos;
21423 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
21424 name = cp_parser_identifier (parser);
21425 cp_parser_objc_superclass_or_category (parser, &super, &categ);
21426 protos = cp_parser_objc_protocol_refs_opt (parser);
21428 /* We have either a class or a category on our hands. */
21430 objc_start_category_interface (name, categ, protos);
21433 objc_start_class_interface (name, super, protos);
21434 /* Handle instance variable declarations, if any. */
21435 cp_parser_objc_class_ivars (parser);
21436 objc_continue_interface ();
21439 cp_parser_objc_method_prototype_list (parser);
21442 /* Parse an Objective-C class implementation. */
21445 cp_parser_objc_class_implementation (cp_parser* parser)
21447 tree name, super, categ;
21449 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
21450 name = cp_parser_identifier (parser);
21451 cp_parser_objc_superclass_or_category (parser, &super, &categ);
21453 /* We have either a class or a category on our hands. */
21455 objc_start_category_implementation (name, categ);
21458 objc_start_class_implementation (name, super);
21459 /* Handle instance variable declarations, if any. */
21460 cp_parser_objc_class_ivars (parser);
21461 objc_continue_implementation ();
21464 cp_parser_objc_method_definition_list (parser);
21467 /* Consume the @end token and finish off the implementation. */
21470 cp_parser_objc_end_implementation (cp_parser* parser)
21472 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21473 objc_finish_implementation ();
21476 /* Parse an Objective-C declaration. */
21479 cp_parser_objc_declaration (cp_parser* parser)
21481 /* Try to figure out what kind of declaration is present. */
21482 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21484 switch (kwd->keyword)
21487 cp_parser_objc_alias_declaration (parser);
21490 cp_parser_objc_class_declaration (parser);
21492 case RID_AT_PROTOCOL:
21493 cp_parser_objc_protocol_declaration (parser);
21495 case RID_AT_INTERFACE:
21496 cp_parser_objc_class_interface (parser);
21498 case RID_AT_IMPLEMENTATION:
21499 cp_parser_objc_class_implementation (parser);
21502 cp_parser_objc_end_implementation (parser);
21505 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21507 cp_parser_skip_to_end_of_block_or_statement (parser);
21511 /* Parse an Objective-C try-catch-finally statement.
21513 objc-try-catch-finally-stmt:
21514 @try compound-statement objc-catch-clause-seq [opt]
21515 objc-finally-clause [opt]
21517 objc-catch-clause-seq:
21518 objc-catch-clause objc-catch-clause-seq [opt]
21521 @catch ( exception-declaration ) compound-statement
21523 objc-finally-clause
21524 @finally compound-statement
21526 Returns NULL_TREE. */
21529 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21530 location_t location;
21533 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
21534 location = cp_lexer_peek_token (parser->lexer)->location;
21535 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21536 node, lest it get absorbed into the surrounding block. */
21537 stmt = push_stmt_list ();
21538 cp_parser_compound_statement (parser, NULL, false);
21539 objc_begin_try_stmt (location, pop_stmt_list (stmt));
21541 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21543 cp_parameter_declarator *parmdecl;
21546 cp_lexer_consume_token (parser->lexer);
21547 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21548 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21549 parm = grokdeclarator (parmdecl->declarator,
21550 &parmdecl->decl_specifiers,
21551 PARM, /*initialized=*/0,
21552 /*attrlist=*/NULL);
21553 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21554 objc_begin_catch_clause (parm);
21555 cp_parser_compound_statement (parser, NULL, false);
21556 objc_finish_catch_clause ();
21559 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21561 cp_lexer_consume_token (parser->lexer);
21562 location = cp_lexer_peek_token (parser->lexer)->location;
21563 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21564 node, lest it get absorbed into the surrounding block. */
21565 stmt = push_stmt_list ();
21566 cp_parser_compound_statement (parser, NULL, false);
21567 objc_build_finally_clause (location, pop_stmt_list (stmt));
21570 return objc_finish_try_stmt ();
21573 /* Parse an Objective-C synchronized statement.
21575 objc-synchronized-stmt:
21576 @synchronized ( expression ) compound-statement
21578 Returns NULL_TREE. */
21581 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21582 location_t location;
21585 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
21587 location = cp_lexer_peek_token (parser->lexer)->location;
21588 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21589 lock = cp_parser_expression (parser, false, NULL);
21590 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21592 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21593 node, lest it get absorbed into the surrounding block. */
21594 stmt = push_stmt_list ();
21595 cp_parser_compound_statement (parser, NULL, false);
21597 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21600 /* Parse an Objective-C throw statement.
21603 @throw assignment-expression [opt] ;
21605 Returns a constructed '@throw' statement. */
21608 cp_parser_objc_throw_statement (cp_parser *parser) {
21609 tree expr = NULL_TREE;
21610 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21612 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
21614 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21615 expr = cp_parser_assignment_expression (parser, false, NULL);
21617 cp_parser_consume_semicolon_at_end_of_statement (parser);
21619 return objc_build_throw_stmt (loc, expr);
21622 /* Parse an Objective-C statement. */
21625 cp_parser_objc_statement (cp_parser * parser) {
21626 /* Try to figure out what kind of declaration is present. */
21627 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21629 switch (kwd->keyword)
21632 return cp_parser_objc_try_catch_finally_statement (parser);
21633 case RID_AT_SYNCHRONIZED:
21634 return cp_parser_objc_synchronized_statement (parser);
21636 return cp_parser_objc_throw_statement (parser);
21638 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21640 cp_parser_skip_to_end_of_block_or_statement (parser);
21643 return error_mark_node;
21646 /* OpenMP 2.5 parsing routines. */
21648 /* Returns name of the next clause.
21649 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21650 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21651 returned and the token is consumed. */
21653 static pragma_omp_clause
21654 cp_parser_omp_clause_name (cp_parser *parser)
21656 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21658 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21659 result = PRAGMA_OMP_CLAUSE_IF;
21660 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21661 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21662 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21663 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21664 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21666 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21667 const char *p = IDENTIFIER_POINTER (id);
21672 if (!strcmp ("collapse", p))
21673 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21674 else if (!strcmp ("copyin", p))
21675 result = PRAGMA_OMP_CLAUSE_COPYIN;
21676 else if (!strcmp ("copyprivate", p))
21677 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21680 if (!strcmp ("firstprivate", p))
21681 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21684 if (!strcmp ("lastprivate", p))
21685 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21688 if (!strcmp ("nowait", p))
21689 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21690 else if (!strcmp ("num_threads", p))
21691 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21694 if (!strcmp ("ordered", p))
21695 result = PRAGMA_OMP_CLAUSE_ORDERED;
21698 if (!strcmp ("reduction", p))
21699 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21702 if (!strcmp ("schedule", p))
21703 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21704 else if (!strcmp ("shared", p))
21705 result = PRAGMA_OMP_CLAUSE_SHARED;
21708 if (!strcmp ("untied", p))
21709 result = PRAGMA_OMP_CLAUSE_UNTIED;
21714 if (result != PRAGMA_OMP_CLAUSE_NONE)
21715 cp_lexer_consume_token (parser->lexer);
21720 /* Validate that a clause of the given type does not already exist. */
21723 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21724 const char *name, location_t location)
21728 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21729 if (OMP_CLAUSE_CODE (c) == code)
21731 error_at (location, "too many %qs clauses", name);
21739 variable-list , identifier
21741 In addition, we match a closing parenthesis. An opening parenthesis
21742 will have been consumed by the caller.
21744 If KIND is nonzero, create the appropriate node and install the decl
21745 in OMP_CLAUSE_DECL and add the node to the head of the list.
21747 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21748 return the list created. */
21751 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21759 token = cp_lexer_peek_token (parser->lexer);
21760 name = cp_parser_id_expression (parser, /*template_p=*/false,
21761 /*check_dependency_p=*/true,
21762 /*template_p=*/NULL,
21763 /*declarator_p=*/false,
21764 /*optional_p=*/false);
21765 if (name == error_mark_node)
21768 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21769 if (decl == error_mark_node)
21770 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
21772 else if (kind != 0)
21774 tree u = build_omp_clause (token->location, kind);
21775 OMP_CLAUSE_DECL (u) = decl;
21776 OMP_CLAUSE_CHAIN (u) = list;
21780 list = tree_cons (decl, NULL_TREE, list);
21783 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21785 cp_lexer_consume_token (parser->lexer);
21788 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21792 /* Try to resync to an unnested comma. Copied from
21793 cp_parser_parenthesized_expression_list. */
21795 ending = cp_parser_skip_to_closing_parenthesis (parser,
21796 /*recovering=*/true,
21798 /*consume_paren=*/true);
21806 /* Similarly, but expect leading and trailing parenthesis. This is a very
21807 common case for omp clauses. */
21810 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21812 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21813 return cp_parser_omp_var_list_no_open (parser, kind, list);
21818 collapse ( constant-expression ) */
21821 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21827 loc = cp_lexer_peek_token (parser->lexer)->location;
21828 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21831 num = cp_parser_constant_expression (parser, false, NULL);
21833 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21834 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21835 /*or_comma=*/false,
21836 /*consume_paren=*/true);
21838 if (num == error_mark_node)
21840 num = fold_non_dependent_expr (num);
21841 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21842 || !host_integerp (num, 0)
21843 || (n = tree_low_cst (num, 0)) <= 0
21846 error_at (loc, "collapse argument needs positive constant integer expression");
21850 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21851 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21852 OMP_CLAUSE_CHAIN (c) = list;
21853 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21859 default ( shared | none ) */
21862 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21864 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21867 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21869 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21871 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21872 const char *p = IDENTIFIER_POINTER (id);
21877 if (strcmp ("none", p) != 0)
21879 kind = OMP_CLAUSE_DEFAULT_NONE;
21883 if (strcmp ("shared", p) != 0)
21885 kind = OMP_CLAUSE_DEFAULT_SHARED;
21892 cp_lexer_consume_token (parser->lexer);
21897 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21900 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21901 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21902 /*or_comma=*/false,
21903 /*consume_paren=*/true);
21905 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21908 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21909 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21910 OMP_CLAUSE_CHAIN (c) = list;
21911 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21917 if ( expression ) */
21920 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21924 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21927 t = cp_parser_condition (parser);
21929 if (t == error_mark_node
21930 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21931 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21932 /*or_comma=*/false,
21933 /*consume_paren=*/true);
21935 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21937 c = build_omp_clause (location, OMP_CLAUSE_IF);
21938 OMP_CLAUSE_IF_EXPR (c) = t;
21939 OMP_CLAUSE_CHAIN (c) = list;
21948 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21949 tree list, location_t location)
21953 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21955 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21956 OMP_CLAUSE_CHAIN (c) = list;
21961 num_threads ( expression ) */
21964 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21965 location_t location)
21969 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21972 t = cp_parser_expression (parser, false, NULL);
21974 if (t == error_mark_node
21975 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21976 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21977 /*or_comma=*/false,
21978 /*consume_paren=*/true);
21980 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21981 "num_threads", location);
21983 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21984 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21985 OMP_CLAUSE_CHAIN (c) = list;
21994 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21995 tree list, location_t location)
21999 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
22000 "ordered", location);
22002 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
22003 OMP_CLAUSE_CHAIN (c) = list;
22008 reduction ( reduction-operator : variable-list )
22010 reduction-operator:
22011 One of: + * - & ^ | && || */
22014 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
22016 enum tree_code code;
22019 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22022 switch (cp_lexer_peek_token (parser->lexer)->type)
22034 code = BIT_AND_EXPR;
22037 code = BIT_XOR_EXPR;
22040 code = BIT_IOR_EXPR;
22043 code = TRUTH_ANDIF_EXPR;
22046 code = TRUTH_ORIF_EXPR;
22049 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
22050 "%<|%>, %<&&%>, or %<||%>");
22052 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22053 /*or_comma=*/false,
22054 /*consume_paren=*/true);
22057 cp_lexer_consume_token (parser->lexer);
22059 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22062 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
22063 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
22064 OMP_CLAUSE_REDUCTION_CODE (c) = code;
22070 schedule ( schedule-kind )
22071 schedule ( schedule-kind , expression )
22074 static | dynamic | guided | runtime | auto */
22077 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
22081 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22084 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
22086 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22088 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22089 const char *p = IDENTIFIER_POINTER (id);
22094 if (strcmp ("dynamic", p) != 0)
22096 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
22100 if (strcmp ("guided", p) != 0)
22102 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
22106 if (strcmp ("runtime", p) != 0)
22108 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
22115 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
22116 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
22117 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
22118 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
22121 cp_lexer_consume_token (parser->lexer);
22123 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22126 cp_lexer_consume_token (parser->lexer);
22128 token = cp_lexer_peek_token (parser->lexer);
22129 t = cp_parser_assignment_expression (parser, false, NULL);
22131 if (t == error_mark_node)
22133 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
22134 error_at (token->location, "schedule %<runtime%> does not take "
22135 "a %<chunk_size%> parameter");
22136 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
22137 error_at (token->location, "schedule %<auto%> does not take "
22138 "a %<chunk_size%> parameter");
22140 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
22142 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22145 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
22148 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
22149 OMP_CLAUSE_CHAIN (c) = list;
22153 cp_parser_error (parser, "invalid schedule kind");
22155 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22156 /*or_comma=*/false,
22157 /*consume_paren=*/true);
22165 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
22166 tree list, location_t location)
22170 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
22172 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
22173 OMP_CLAUSE_CHAIN (c) = list;
22177 /* Parse all OpenMP clauses. The set clauses allowed by the directive
22178 is a bitmask in MASK. Return the list of clauses found; the result
22179 of clause default goes in *pdefault. */
22182 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
22183 const char *where, cp_token *pragma_tok)
22185 tree clauses = NULL;
22187 cp_token *token = NULL;
22189 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
22191 pragma_omp_clause c_kind;
22192 const char *c_name;
22193 tree prev = clauses;
22195 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22196 cp_lexer_consume_token (parser->lexer);
22198 token = cp_lexer_peek_token (parser->lexer);
22199 c_kind = cp_parser_omp_clause_name (parser);
22204 case PRAGMA_OMP_CLAUSE_COLLAPSE:
22205 clauses = cp_parser_omp_clause_collapse (parser, clauses,
22207 c_name = "collapse";
22209 case PRAGMA_OMP_CLAUSE_COPYIN:
22210 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
22213 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
22214 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
22216 c_name = "copyprivate";
22218 case PRAGMA_OMP_CLAUSE_DEFAULT:
22219 clauses = cp_parser_omp_clause_default (parser, clauses,
22221 c_name = "default";
22223 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
22224 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
22226 c_name = "firstprivate";
22228 case PRAGMA_OMP_CLAUSE_IF:
22229 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
22232 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
22233 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
22235 c_name = "lastprivate";
22237 case PRAGMA_OMP_CLAUSE_NOWAIT:
22238 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
22241 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
22242 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
22244 c_name = "num_threads";
22246 case PRAGMA_OMP_CLAUSE_ORDERED:
22247 clauses = cp_parser_omp_clause_ordered (parser, clauses,
22249 c_name = "ordered";
22251 case PRAGMA_OMP_CLAUSE_PRIVATE:
22252 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
22254 c_name = "private";
22256 case PRAGMA_OMP_CLAUSE_REDUCTION:
22257 clauses = cp_parser_omp_clause_reduction (parser, clauses);
22258 c_name = "reduction";
22260 case PRAGMA_OMP_CLAUSE_SCHEDULE:
22261 clauses = cp_parser_omp_clause_schedule (parser, clauses,
22263 c_name = "schedule";
22265 case PRAGMA_OMP_CLAUSE_SHARED:
22266 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
22270 case PRAGMA_OMP_CLAUSE_UNTIED:
22271 clauses = cp_parser_omp_clause_untied (parser, clauses,
22276 cp_parser_error (parser, "expected %<#pragma omp%> clause");
22280 if (((mask >> c_kind) & 1) == 0)
22282 /* Remove the invalid clause(s) from the list to avoid
22283 confusing the rest of the compiler. */
22285 error_at (token->location, "%qs is not valid for %qs", c_name, where);
22289 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22290 return finish_omp_clauses (clauses);
22297 In practice, we're also interested in adding the statement to an
22298 outer node. So it is convenient if we work around the fact that
22299 cp_parser_statement calls add_stmt. */
22302 cp_parser_begin_omp_structured_block (cp_parser *parser)
22304 unsigned save = parser->in_statement;
22306 /* Only move the values to IN_OMP_BLOCK if they weren't false.
22307 This preserves the "not within loop or switch" style error messages
22308 for nonsense cases like
22314 if (parser->in_statement)
22315 parser->in_statement = IN_OMP_BLOCK;
22321 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
22323 parser->in_statement = save;
22327 cp_parser_omp_structured_block (cp_parser *parser)
22329 tree stmt = begin_omp_structured_block ();
22330 unsigned int save = cp_parser_begin_omp_structured_block (parser);
22332 cp_parser_statement (parser, NULL_TREE, false, NULL);
22334 cp_parser_end_omp_structured_block (parser, save);
22335 return finish_omp_structured_block (stmt);
22339 # pragma omp atomic new-line
22343 x binop= expr | x++ | ++x | x-- | --x
22345 +, *, -, /, &, ^, |, <<, >>
22347 where x is an lvalue expression with scalar type. */
22350 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
22353 enum tree_code code;
22355 cp_parser_require_pragma_eol (parser, pragma_tok);
22357 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
22358 /*cast_p=*/false, NULL);
22359 switch (TREE_CODE (lhs))
22364 case PREINCREMENT_EXPR:
22365 case POSTINCREMENT_EXPR:
22366 lhs = TREE_OPERAND (lhs, 0);
22368 rhs = integer_one_node;
22371 case PREDECREMENT_EXPR:
22372 case POSTDECREMENT_EXPR:
22373 lhs = TREE_OPERAND (lhs, 0);
22375 rhs = integer_one_node;
22379 switch (cp_lexer_peek_token (parser->lexer)->type)
22385 code = TRUNC_DIV_EXPR;
22393 case CPP_LSHIFT_EQ:
22394 code = LSHIFT_EXPR;
22396 case CPP_RSHIFT_EQ:
22397 code = RSHIFT_EXPR;
22400 code = BIT_AND_EXPR;
22403 code = BIT_IOR_EXPR;
22406 code = BIT_XOR_EXPR;
22409 cp_parser_error (parser,
22410 "invalid operator for %<#pragma omp atomic%>");
22413 cp_lexer_consume_token (parser->lexer);
22415 rhs = cp_parser_expression (parser, false, NULL);
22416 if (rhs == error_mark_node)
22420 finish_omp_atomic (code, lhs, rhs);
22421 cp_parser_consume_semicolon_at_end_of_statement (parser);
22425 cp_parser_skip_to_end_of_block_or_statement (parser);
22430 # pragma omp barrier new-line */
22433 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
22435 cp_parser_require_pragma_eol (parser, pragma_tok);
22436 finish_omp_barrier ();
22440 # pragma omp critical [(name)] new-line
22441 structured-block */
22444 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
22446 tree stmt, name = NULL;
22448 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22450 cp_lexer_consume_token (parser->lexer);
22452 name = cp_parser_identifier (parser);
22454 if (name == error_mark_node
22455 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22456 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22457 /*or_comma=*/false,
22458 /*consume_paren=*/true);
22459 if (name == error_mark_node)
22462 cp_parser_require_pragma_eol (parser, pragma_tok);
22464 stmt = cp_parser_omp_structured_block (parser);
22465 return c_finish_omp_critical (input_location, stmt, name);
22469 # pragma omp flush flush-vars[opt] new-line
22472 ( variable-list ) */
22475 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22477 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22478 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22479 cp_parser_require_pragma_eol (parser, pragma_tok);
22481 finish_omp_flush ();
22484 /* Helper function, to parse omp for increment expression. */
22487 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22489 tree cond = cp_parser_binary_expression (parser, false, true,
22490 PREC_NOT_OPERATOR, NULL);
22493 if (cond == error_mark_node
22494 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22496 cp_parser_skip_to_end_of_statement (parser);
22497 return error_mark_node;
22500 switch (TREE_CODE (cond))
22508 return error_mark_node;
22511 /* If decl is an iterator, preserve LHS and RHS of the relational
22512 expr until finish_omp_for. */
22514 && (type_dependent_expression_p (decl)
22515 || CLASS_TYPE_P (TREE_TYPE (decl))))
22518 return build_x_binary_op (TREE_CODE (cond),
22519 TREE_OPERAND (cond, 0), ERROR_MARK,
22520 TREE_OPERAND (cond, 1), ERROR_MARK,
22521 &overloaded_p, tf_warning_or_error);
22524 /* Helper function, to parse omp for increment expression. */
22527 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22529 cp_token *token = cp_lexer_peek_token (parser->lexer);
22535 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22537 op = (token->type == CPP_PLUS_PLUS
22538 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22539 cp_lexer_consume_token (parser->lexer);
22540 lhs = cp_parser_cast_expression (parser, false, false, NULL);
22542 return error_mark_node;
22543 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22546 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22548 return error_mark_node;
22550 token = cp_lexer_peek_token (parser->lexer);
22551 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22553 op = (token->type == CPP_PLUS_PLUS
22554 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22555 cp_lexer_consume_token (parser->lexer);
22556 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22559 op = cp_parser_assignment_operator_opt (parser);
22560 if (op == ERROR_MARK)
22561 return error_mark_node;
22563 if (op != NOP_EXPR)
22565 rhs = cp_parser_assignment_expression (parser, false, NULL);
22566 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22567 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22570 lhs = cp_parser_binary_expression (parser, false, false,
22571 PREC_ADDITIVE_EXPRESSION, NULL);
22572 token = cp_lexer_peek_token (parser->lexer);
22573 decl_first = lhs == decl;
22576 if (token->type != CPP_PLUS
22577 && token->type != CPP_MINUS)
22578 return error_mark_node;
22582 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22583 cp_lexer_consume_token (parser->lexer);
22584 rhs = cp_parser_binary_expression (parser, false, false,
22585 PREC_ADDITIVE_EXPRESSION, NULL);
22586 token = cp_lexer_peek_token (parser->lexer);
22587 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22589 if (lhs == NULL_TREE)
22591 if (op == PLUS_EXPR)
22594 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22597 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22598 NULL, tf_warning_or_error);
22601 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22605 if (rhs != decl || op == MINUS_EXPR)
22606 return error_mark_node;
22607 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22610 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22612 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22615 /* Parse the restricted form of the for statement allowed by OpenMP. */
22618 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22620 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22621 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22622 tree this_pre_body, cl;
22623 location_t loc_first;
22624 bool collapse_err = false;
22625 int i, collapse = 1, nbraces = 0;
22627 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22628 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22629 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22631 gcc_assert (collapse >= 1);
22633 declv = make_tree_vec (collapse);
22634 initv = make_tree_vec (collapse);
22635 condv = make_tree_vec (collapse);
22636 incrv = make_tree_vec (collapse);
22638 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22640 for (i = 0; i < collapse; i++)
22642 int bracecount = 0;
22643 bool add_private_clause = false;
22646 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22648 cp_parser_error (parser, "for statement expected");
22651 loc = cp_lexer_consume_token (parser->lexer)->location;
22653 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22656 init = decl = real_decl = NULL;
22657 this_pre_body = push_stmt_list ();
22658 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22660 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22664 integer-type var = lb
22665 random-access-iterator-type var = lb
22666 pointer-type var = lb
22668 cp_decl_specifier_seq type_specifiers;
22670 /* First, try to parse as an initialized declaration. See
22671 cp_parser_condition, from whence the bulk of this is copied. */
22673 cp_parser_parse_tentatively (parser);
22674 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22675 /*is_trailing_return=*/false,
22677 if (cp_parser_parse_definitely (parser))
22679 /* If parsing a type specifier seq succeeded, then this
22680 MUST be a initialized declaration. */
22681 tree asm_specification, attributes;
22682 cp_declarator *declarator;
22684 declarator = cp_parser_declarator (parser,
22685 CP_PARSER_DECLARATOR_NAMED,
22686 /*ctor_dtor_or_conv_p=*/NULL,
22687 /*parenthesized_p=*/NULL,
22688 /*member_p=*/false);
22689 attributes = cp_parser_attributes_opt (parser);
22690 asm_specification = cp_parser_asm_specification_opt (parser);
22692 if (declarator == cp_error_declarator)
22693 cp_parser_skip_to_end_of_statement (parser);
22697 tree pushed_scope, auto_node;
22699 decl = start_decl (declarator, &type_specifiers,
22700 SD_INITIALIZED, attributes,
22701 /*prefix_attributes=*/NULL_TREE,
22704 auto_node = type_uses_auto (TREE_TYPE (decl));
22705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22707 if (cp_lexer_next_token_is (parser->lexer,
22709 error ("parenthesized initialization is not allowed in "
22710 "OpenMP %<for%> loop");
22712 /* Trigger an error. */
22713 cp_parser_require (parser, CPP_EQ, RT_EQ);
22715 init = error_mark_node;
22716 cp_parser_skip_to_end_of_statement (parser);
22718 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22719 || type_dependent_expression_p (decl)
22722 bool is_direct_init, is_non_constant_init;
22724 init = cp_parser_initializer (parser,
22726 &is_non_constant_init);
22728 if (auto_node && describable_type (init))
22731 = do_auto_deduction (TREE_TYPE (decl), init,
22734 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22735 && !type_dependent_expression_p (decl))
22739 cp_finish_decl (decl, init, !is_non_constant_init,
22741 LOOKUP_ONLYCONVERTING);
22742 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22745 = tree_cons (NULL, this_pre_body, for_block);
22749 init = pop_stmt_list (this_pre_body);
22750 this_pre_body = NULL_TREE;
22755 cp_lexer_consume_token (parser->lexer);
22756 init = cp_parser_assignment_expression (parser, false, NULL);
22759 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22760 init = error_mark_node;
22762 cp_finish_decl (decl, NULL_TREE,
22763 /*init_const_expr_p=*/false,
22765 LOOKUP_ONLYCONVERTING);
22769 pop_scope (pushed_scope);
22775 /* If parsing a type specifier sequence failed, then
22776 this MUST be a simple expression. */
22777 cp_parser_parse_tentatively (parser);
22778 decl = cp_parser_primary_expression (parser, false, false,
22780 if (!cp_parser_error_occurred (parser)
22783 && CLASS_TYPE_P (TREE_TYPE (decl)))
22787 cp_parser_parse_definitely (parser);
22788 cp_parser_require (parser, CPP_EQ, RT_EQ);
22789 rhs = cp_parser_assignment_expression (parser, false, NULL);
22790 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22792 tf_warning_or_error));
22793 add_private_clause = true;
22798 cp_parser_abort_tentative_parse (parser);
22799 init = cp_parser_expression (parser, false, NULL);
22802 if (TREE_CODE (init) == MODIFY_EXPR
22803 || TREE_CODE (init) == MODOP_EXPR)
22804 real_decl = TREE_OPERAND (init, 0);
22809 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22812 this_pre_body = pop_stmt_list (this_pre_body);
22816 pre_body = push_stmt_list ();
22818 add_stmt (this_pre_body);
22819 pre_body = pop_stmt_list (pre_body);
22822 pre_body = this_pre_body;
22827 if (par_clauses != NULL && real_decl != NULL_TREE)
22830 for (c = par_clauses; *c ; )
22831 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22832 && OMP_CLAUSE_DECL (*c) == real_decl)
22834 error_at (loc, "iteration variable %qD"
22835 " should not be firstprivate", real_decl);
22836 *c = OMP_CLAUSE_CHAIN (*c);
22838 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22839 && OMP_CLAUSE_DECL (*c) == real_decl)
22841 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22842 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22843 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22844 OMP_CLAUSE_DECL (l) = real_decl;
22845 OMP_CLAUSE_CHAIN (l) = clauses;
22846 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22848 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22849 CP_OMP_CLAUSE_INFO (*c) = NULL;
22850 add_private_clause = false;
22854 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22855 && OMP_CLAUSE_DECL (*c) == real_decl)
22856 add_private_clause = false;
22857 c = &OMP_CLAUSE_CHAIN (*c);
22861 if (add_private_clause)
22864 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22866 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22867 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22868 && OMP_CLAUSE_DECL (c) == decl)
22870 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22871 && OMP_CLAUSE_DECL (c) == decl)
22872 error_at (loc, "iteration variable %qD "
22873 "should not be firstprivate",
22875 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22876 && OMP_CLAUSE_DECL (c) == decl)
22877 error_at (loc, "iteration variable %qD should not be reduction",
22882 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22883 OMP_CLAUSE_DECL (c) = decl;
22884 c = finish_omp_clauses (c);
22887 OMP_CLAUSE_CHAIN (c) = clauses;
22894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22895 cond = cp_parser_omp_for_cond (parser, decl);
22896 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22899 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22901 /* If decl is an iterator, preserve the operator on decl
22902 until finish_omp_for. */
22904 && (type_dependent_expression_p (decl)
22905 || CLASS_TYPE_P (TREE_TYPE (decl))))
22906 incr = cp_parser_omp_for_incr (parser, decl);
22908 incr = cp_parser_expression (parser, false, NULL);
22911 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22912 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22913 /*or_comma=*/false,
22914 /*consume_paren=*/true);
22916 TREE_VEC_ELT (declv, i) = decl;
22917 TREE_VEC_ELT (initv, i) = init;
22918 TREE_VEC_ELT (condv, i) = cond;
22919 TREE_VEC_ELT (incrv, i) = incr;
22921 if (i == collapse - 1)
22924 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22925 in between the collapsed for loops to be still considered perfectly
22926 nested. Hopefully the final version clarifies this.
22927 For now handle (multiple) {'s and empty statements. */
22928 cp_parser_parse_tentatively (parser);
22931 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22933 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22935 cp_lexer_consume_token (parser->lexer);
22938 else if (bracecount
22939 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22940 cp_lexer_consume_token (parser->lexer);
22943 loc = cp_lexer_peek_token (parser->lexer)->location;
22944 error_at (loc, "not enough collapsed for loops");
22945 collapse_err = true;
22946 cp_parser_abort_tentative_parse (parser);
22955 cp_parser_parse_definitely (parser);
22956 nbraces += bracecount;
22960 /* Note that we saved the original contents of this flag when we entered
22961 the structured block, and so we don't need to re-save it here. */
22962 parser->in_statement = IN_OMP_FOR;
22964 /* Note that the grammar doesn't call for a structured block here,
22965 though the loop as a whole is a structured block. */
22966 body = push_stmt_list ();
22967 cp_parser_statement (parser, NULL_TREE, false, NULL);
22968 body = pop_stmt_list (body);
22970 if (declv == NULL_TREE)
22973 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22974 pre_body, clauses);
22978 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22980 cp_lexer_consume_token (parser->lexer);
22983 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22984 cp_lexer_consume_token (parser->lexer);
22989 error_at (cp_lexer_peek_token (parser->lexer)->location,
22990 "collapsed loops not perfectly nested");
22992 collapse_err = true;
22993 cp_parser_statement_seq_opt (parser, NULL);
22994 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23001 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
23002 for_block = TREE_CHAIN (for_block);
23009 #pragma omp for for-clause[optseq] new-line
23012 #define OMP_FOR_CLAUSE_MASK \
23013 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23014 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23015 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
23016 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23017 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
23018 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
23019 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
23020 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
23023 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
23025 tree clauses, sb, ret;
23028 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
23029 "#pragma omp for", pragma_tok);
23031 sb = begin_omp_structured_block ();
23032 save = cp_parser_begin_omp_structured_block (parser);
23034 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
23036 cp_parser_end_omp_structured_block (parser, save);
23037 add_stmt (finish_omp_structured_block (sb));
23043 # pragma omp master new-line
23044 structured-block */
23047 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
23049 cp_parser_require_pragma_eol (parser, pragma_tok);
23050 return c_finish_omp_master (input_location,
23051 cp_parser_omp_structured_block (parser));
23055 # pragma omp ordered new-line
23056 structured-block */
23059 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
23061 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23062 cp_parser_require_pragma_eol (parser, pragma_tok);
23063 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
23069 { section-sequence }
23072 section-directive[opt] structured-block
23073 section-sequence section-directive structured-block */
23076 cp_parser_omp_sections_scope (cp_parser *parser)
23078 tree stmt, substmt;
23079 bool error_suppress = false;
23082 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23085 stmt = push_stmt_list ();
23087 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
23091 substmt = begin_omp_structured_block ();
23092 save = cp_parser_begin_omp_structured_block (parser);
23096 cp_parser_statement (parser, NULL_TREE, false, NULL);
23098 tok = cp_lexer_peek_token (parser->lexer);
23099 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23101 if (tok->type == CPP_CLOSE_BRACE)
23103 if (tok->type == CPP_EOF)
23107 cp_parser_end_omp_structured_block (parser, save);
23108 substmt = finish_omp_structured_block (substmt);
23109 substmt = build1 (OMP_SECTION, void_type_node, substmt);
23110 add_stmt (substmt);
23115 tok = cp_lexer_peek_token (parser->lexer);
23116 if (tok->type == CPP_CLOSE_BRACE)
23118 if (tok->type == CPP_EOF)
23121 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23123 cp_lexer_consume_token (parser->lexer);
23124 cp_parser_require_pragma_eol (parser, tok);
23125 error_suppress = false;
23127 else if (!error_suppress)
23129 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
23130 error_suppress = true;
23133 substmt = cp_parser_omp_structured_block (parser);
23134 substmt = build1 (OMP_SECTION, void_type_node, substmt);
23135 add_stmt (substmt);
23137 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23139 substmt = pop_stmt_list (stmt);
23141 stmt = make_node (OMP_SECTIONS);
23142 TREE_TYPE (stmt) = void_type_node;
23143 OMP_SECTIONS_BODY (stmt) = substmt;
23150 # pragma omp sections sections-clause[optseq] newline
23153 #define OMP_SECTIONS_CLAUSE_MASK \
23154 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23155 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23156 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
23157 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23158 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23161 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
23165 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
23166 "#pragma omp sections", pragma_tok);
23168 ret = cp_parser_omp_sections_scope (parser);
23170 OMP_SECTIONS_CLAUSES (ret) = clauses;
23176 # pragma parallel parallel-clause new-line
23177 # pragma parallel for parallel-for-clause new-line
23178 # pragma parallel sections parallel-sections-clause new-line */
23180 #define OMP_PARALLEL_CLAUSE_MASK \
23181 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
23182 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23183 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23184 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
23185 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
23186 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
23187 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23188 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
23191 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
23193 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
23194 const char *p_name = "#pragma omp parallel";
23195 tree stmt, clauses, par_clause, ws_clause, block;
23196 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
23198 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23200 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23202 cp_lexer_consume_token (parser->lexer);
23203 p_kind = PRAGMA_OMP_PARALLEL_FOR;
23204 p_name = "#pragma omp parallel for";
23205 mask |= OMP_FOR_CLAUSE_MASK;
23206 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
23208 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23210 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23211 const char *p = IDENTIFIER_POINTER (id);
23212 if (strcmp (p, "sections") == 0)
23214 cp_lexer_consume_token (parser->lexer);
23215 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
23216 p_name = "#pragma omp parallel sections";
23217 mask |= OMP_SECTIONS_CLAUSE_MASK;
23218 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
23222 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
23223 block = begin_omp_parallel ();
23224 save = cp_parser_begin_omp_structured_block (parser);
23228 case PRAGMA_OMP_PARALLEL:
23229 cp_parser_statement (parser, NULL_TREE, false, NULL);
23230 par_clause = clauses;
23233 case PRAGMA_OMP_PARALLEL_FOR:
23234 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
23235 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
23238 case PRAGMA_OMP_PARALLEL_SECTIONS:
23239 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
23240 stmt = cp_parser_omp_sections_scope (parser);
23242 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
23246 gcc_unreachable ();
23249 cp_parser_end_omp_structured_block (parser, save);
23250 stmt = finish_omp_parallel (par_clause, block);
23251 if (p_kind != PRAGMA_OMP_PARALLEL)
23252 OMP_PARALLEL_COMBINED (stmt) = 1;
23257 # pragma omp single single-clause[optseq] new-line
23258 structured-block */
23260 #define OMP_SINGLE_CLAUSE_MASK \
23261 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23262 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23263 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
23264 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23267 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
23269 tree stmt = make_node (OMP_SINGLE);
23270 TREE_TYPE (stmt) = void_type_node;
23272 OMP_SINGLE_CLAUSES (stmt)
23273 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
23274 "#pragma omp single", pragma_tok);
23275 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
23277 return add_stmt (stmt);
23281 # pragma omp task task-clause[optseq] new-line
23282 structured-block */
23284 #define OMP_TASK_CLAUSE_MASK \
23285 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
23286 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
23287 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
23288 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23289 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23290 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
23293 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
23295 tree clauses, block;
23298 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
23299 "#pragma omp task", pragma_tok);
23300 block = begin_omp_task ();
23301 save = cp_parser_begin_omp_structured_block (parser);
23302 cp_parser_statement (parser, NULL_TREE, false, NULL);
23303 cp_parser_end_omp_structured_block (parser, save);
23304 return finish_omp_task (clauses, block);
23308 # pragma omp taskwait new-line */
23311 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
23313 cp_parser_require_pragma_eol (parser, pragma_tok);
23314 finish_omp_taskwait ();
23318 # pragma omp threadprivate (variable-list) */
23321 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
23325 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23326 cp_parser_require_pragma_eol (parser, pragma_tok);
23328 finish_omp_threadprivate (vars);
23331 /* Main entry point to OpenMP statement pragmas. */
23334 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
23338 switch (pragma_tok->pragma_kind)
23340 case PRAGMA_OMP_ATOMIC:
23341 cp_parser_omp_atomic (parser, pragma_tok);
23343 case PRAGMA_OMP_CRITICAL:
23344 stmt = cp_parser_omp_critical (parser, pragma_tok);
23346 case PRAGMA_OMP_FOR:
23347 stmt = cp_parser_omp_for (parser, pragma_tok);
23349 case PRAGMA_OMP_MASTER:
23350 stmt = cp_parser_omp_master (parser, pragma_tok);
23352 case PRAGMA_OMP_ORDERED:
23353 stmt = cp_parser_omp_ordered (parser, pragma_tok);
23355 case PRAGMA_OMP_PARALLEL:
23356 stmt = cp_parser_omp_parallel (parser, pragma_tok);
23358 case PRAGMA_OMP_SECTIONS:
23359 stmt = cp_parser_omp_sections (parser, pragma_tok);
23361 case PRAGMA_OMP_SINGLE:
23362 stmt = cp_parser_omp_single (parser, pragma_tok);
23364 case PRAGMA_OMP_TASK:
23365 stmt = cp_parser_omp_task (parser, pragma_tok);
23368 gcc_unreachable ();
23372 SET_EXPR_LOCATION (stmt, pragma_tok->location);
23377 static GTY (()) cp_parser *the_parser;
23380 /* Special handling for the first token or line in the file. The first
23381 thing in the file might be #pragma GCC pch_preprocess, which loads a
23382 PCH file, which is a GC collection point. So we need to handle this
23383 first pragma without benefit of an existing lexer structure.
23385 Always returns one token to the caller in *FIRST_TOKEN. This is
23386 either the true first token of the file, or the first token after
23387 the initial pragma. */
23390 cp_parser_initial_pragma (cp_token *first_token)
23394 cp_lexer_get_preprocessor_token (NULL, first_token);
23395 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
23398 cp_lexer_get_preprocessor_token (NULL, first_token);
23399 if (first_token->type == CPP_STRING)
23401 name = first_token->u.value;
23403 cp_lexer_get_preprocessor_token (NULL, first_token);
23404 if (first_token->type != CPP_PRAGMA_EOL)
23405 error_at (first_token->location,
23406 "junk at end of %<#pragma GCC pch_preprocess%>");
23409 error_at (first_token->location, "expected string literal");
23411 /* Skip to the end of the pragma. */
23412 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
23413 cp_lexer_get_preprocessor_token (NULL, first_token);
23415 /* Now actually load the PCH file. */
23417 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
23419 /* Read one more token to return to our caller. We have to do this
23420 after reading the PCH file in, since its pointers have to be
23422 cp_lexer_get_preprocessor_token (NULL, first_token);
23425 /* Normal parsing of a pragma token. Here we can (and must) use the
23429 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
23431 cp_token *pragma_tok;
23434 pragma_tok = cp_lexer_consume_token (parser->lexer);
23435 gcc_assert (pragma_tok->type == CPP_PRAGMA);
23436 parser->lexer->in_pragma = true;
23438 id = pragma_tok->pragma_kind;
23441 case PRAGMA_GCC_PCH_PREPROCESS:
23442 error_at (pragma_tok->location,
23443 "%<#pragma GCC pch_preprocess%> must be first");
23446 case PRAGMA_OMP_BARRIER:
23449 case pragma_compound:
23450 cp_parser_omp_barrier (parser, pragma_tok);
23453 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23454 "used in compound statements");
23461 case PRAGMA_OMP_FLUSH:
23464 case pragma_compound:
23465 cp_parser_omp_flush (parser, pragma_tok);
23468 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23469 "used in compound statements");
23476 case PRAGMA_OMP_TASKWAIT:
23479 case pragma_compound:
23480 cp_parser_omp_taskwait (parser, pragma_tok);
23483 error_at (pragma_tok->location,
23484 "%<#pragma omp taskwait%> may only be "
23485 "used in compound statements");
23492 case PRAGMA_OMP_THREADPRIVATE:
23493 cp_parser_omp_threadprivate (parser, pragma_tok);
23496 case PRAGMA_OMP_ATOMIC:
23497 case PRAGMA_OMP_CRITICAL:
23498 case PRAGMA_OMP_FOR:
23499 case PRAGMA_OMP_MASTER:
23500 case PRAGMA_OMP_ORDERED:
23501 case PRAGMA_OMP_PARALLEL:
23502 case PRAGMA_OMP_SECTIONS:
23503 case PRAGMA_OMP_SINGLE:
23504 case PRAGMA_OMP_TASK:
23505 if (context == pragma_external)
23507 cp_parser_omp_construct (parser, pragma_tok);
23510 case PRAGMA_OMP_SECTION:
23511 error_at (pragma_tok->location,
23512 "%<#pragma omp section%> may only be used in "
23513 "%<#pragma omp sections%> construct");
23517 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23518 c_invoke_pragma_handler (id);
23522 cp_parser_error (parser, "expected declaration specifiers");
23526 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23530 /* The interface the pragma parsers have to the lexer. */
23533 pragma_lex (tree *value)
23536 enum cpp_ttype ret;
23538 tok = cp_lexer_peek_token (the_parser->lexer);
23541 *value = tok->u.value;
23543 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23545 else if (ret == CPP_STRING)
23546 *value = cp_parser_string_literal (the_parser, false, false);
23549 cp_lexer_consume_token (the_parser->lexer);
23550 if (ret == CPP_KEYWORD)
23558 /* External interface. */
23560 /* Parse one entire translation unit. */
23563 c_parse_file (void)
23565 static bool already_called = false;
23567 if (already_called)
23569 sorry ("inter-module optimizations not implemented for C++");
23572 already_called = true;
23574 the_parser = cp_parser_new ();
23575 push_deferring_access_checks (flag_access_control
23576 ? dk_no_deferred : dk_no_check);
23577 cp_parser_translation_unit (the_parser);
23581 #include "gt-cp-parser.h"