2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
30 #include "c-family/c-pragma.h"
33 #include "diagnostic-core.h"
38 #include "c-family/c-common.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct GTY(()) tree_check {
51 /* The value associated with the token. */
53 /* The checks that have been associated with value. */
54 VEC (deferred_access_check, gc)* checks;
55 /* The token's qualifying scope (used when it is a
56 CPP_NESTED_NAME_SPECIFIER). */
57 tree qualifying_scope;
62 typedef struct GTY (()) cp_token {
63 /* The kind of token. */
64 ENUM_BITFIELD (cpp_ttype) type : 8;
65 /* If this token is a keyword, this value indicates which keyword.
66 Otherwise, this value is RID_MAX. */
67 ENUM_BITFIELD (rid) keyword : 8;
70 /* Identifier for the pragma. */
71 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
72 /* True if this token is from a context where it is implicitly extern "C" */
73 BOOL_BITFIELD implicit_extern_c : 1;
74 /* True for a CPP_NAME token that is not a keyword (i.e., for which
75 KEYWORD is RID_MAX) iff this name was looked up and found to be
76 ambiguous. An error has already been reported. */
77 BOOL_BITFIELD ambiguous_p : 1;
78 /* The location at which this token was found. */
80 /* The value associated with this token, if any. */
81 union cp_token_value {
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check* GTY((tag ("1"))) tree_check_value;
84 /* Use for all other tokens. */
85 tree GTY((tag ("0"))) value;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 /* We use a stack of token pointer for saving token sets. */
90 typedef struct cp_token *cp_token_position;
91 DEF_VEC_P (cp_token_position);
92 DEF_VEC_ALLOC_P (cp_token_position,heap);
94 static cp_token eof_token =
96 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
99 /* The cp_lexer structure represents the C++ lexer. It is responsible
100 for managing the token stream from the preprocessor and supplying
101 it to the parser. Tokens are never added to the cp_lexer after
104 typedef struct GTY (()) cp_lexer {
105 /* The memory allocated for the buffer. NULL if this lexer does not
106 own the token buffer. */
107 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
108 /* If the lexer owns the buffer, this is the number of tokens in the
110 size_t buffer_length;
112 /* A pointer just past the last available token. The tokens
113 in this lexer are [buffer, last_token). */
114 cp_token_position GTY ((skip)) last_token;
116 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
117 no more available tokens. */
118 cp_token_position GTY ((skip)) next_token;
120 /* A stack indicating positions at which cp_lexer_save_tokens was
121 called. The top entry is the most recent position at which we
122 began saving tokens. If the stack is non-empty, we are saving
124 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
126 /* The next lexer in a linked list of lexers. */
127 struct cp_lexer *next;
129 /* True if we should output debugging information. */
132 /* True if we're in the context of parsing a pragma, and should not
133 increment past the end-of-line marker. */
137 /* cp_token_cache is a range of tokens. There is no need to represent
138 allocate heap memory for it, since tokens are never removed from the
139 lexer's array. There is also no need for the GC to walk through
140 a cp_token_cache, since everything in here is referenced through
143 typedef struct GTY(()) cp_token_cache {
144 /* The beginning of the token range. */
145 cp_token * GTY((skip)) first;
147 /* Points immediately after the last token in the range. */
148 cp_token * GTY ((skip)) last;
151 /* The various kinds of non integral constant we encounter. */
152 typedef enum non_integral_constant {
154 /* floating-point literal */
158 /* %<__FUNCTION__%> */
160 /* %<__PRETTY_FUNCTION__%> */
168 /* %<typeid%> operator */
170 /* non-constant compound literals */
172 /* a function call */
178 /* an array reference */
184 /* the address of a label */
198 /* calls to overloaded operators */
202 /* a comma operator */
204 /* a call to a constructor */
206 } non_integral_constant;
208 /* The various kinds of errors about name-lookup failing. */
209 typedef enum name_lookup_error {
214 /* is not a class or namespace */
216 /* is not a class, namespace, or enumeration */
220 /* The various kinds of required token */
221 typedef enum required_token {
223 RT_SEMICOLON, /* ';' */
224 RT_OPEN_PAREN, /* '(' */
225 RT_CLOSE_BRACE, /* '}' */
226 RT_OPEN_BRACE, /* '{' */
227 RT_CLOSE_SQUARE, /* ']' */
228 RT_OPEN_SQUARE, /* '[' */
232 RT_GREATER, /* '>' */
234 RT_ELLIPSIS, /* '...' */
238 RT_COLON_SCOPE, /* ':' or '::' */
239 RT_CLOSE_PAREN, /* ')' */
240 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
241 RT_PRAGMA_EOL, /* end of line */
242 RT_NAME, /* identifier */
244 /* The type is CPP_KEYWORD */
246 RT_DELETE, /* delete */
247 RT_RETURN, /* return */
248 RT_WHILE, /* while */
249 RT_EXTERN, /* extern */
250 RT_STATIC_ASSERT, /* static_assert */
251 RT_DECLTYPE, /* decltype */
252 RT_OPERATOR, /* operator */
253 RT_CLASS, /* class */
254 RT_TEMPLATE, /* template */
255 RT_NAMESPACE, /* namespace */
256 RT_USING, /* using */
259 RT_CATCH, /* catch */
260 RT_THROW, /* throw */
261 RT_LABEL, /* __label__ */
262 RT_AT_TRY, /* @try */
263 RT_AT_SYNCHRONIZED, /* @synchronized */
264 RT_AT_THROW, /* @throw */
266 RT_SELECT, /* selection-statement */
267 RT_INTERATION, /* iteration-statement */
268 RT_JUMP, /* jump-statement */
269 RT_CLASS_KEY, /* class-key */
270 RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
275 static cp_lexer *cp_lexer_new_main
277 static cp_lexer *cp_lexer_new_from_tokens
278 (cp_token_cache *tokens);
279 static void cp_lexer_destroy
281 static int cp_lexer_saving_tokens
283 static cp_token_position cp_lexer_token_position
285 static cp_token *cp_lexer_token_at
286 (cp_lexer *, cp_token_position);
287 static void cp_lexer_get_preprocessor_token
288 (cp_lexer *, cp_token *);
289 static inline cp_token *cp_lexer_peek_token
291 static cp_token *cp_lexer_peek_nth_token
292 (cp_lexer *, size_t);
293 static inline bool cp_lexer_next_token_is
294 (cp_lexer *, enum cpp_ttype);
295 static bool cp_lexer_next_token_is_not
296 (cp_lexer *, enum cpp_ttype);
297 static bool cp_lexer_next_token_is_keyword
298 (cp_lexer *, enum rid);
299 static cp_token *cp_lexer_consume_token
301 static void cp_lexer_purge_token
303 static void cp_lexer_purge_tokens_after
304 (cp_lexer *, cp_token_position);
305 static void cp_lexer_save_tokens
307 static void cp_lexer_commit_tokens
309 static void cp_lexer_rollback_tokens
311 #ifdef ENABLE_CHECKING
312 static void cp_lexer_print_token
313 (FILE *, cp_token *);
314 static inline bool cp_lexer_debugging_p
316 static void cp_lexer_start_debugging
317 (cp_lexer *) ATTRIBUTE_UNUSED;
318 static void cp_lexer_stop_debugging
319 (cp_lexer *) ATTRIBUTE_UNUSED;
321 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
322 about passing NULL to functions that require non-NULL arguments
323 (fputs, fprintf). It will never be used, so all we need is a value
324 of the right type that's guaranteed not to be NULL. */
325 #define cp_lexer_debug_stream stdout
326 #define cp_lexer_print_token(str, tok) (void) 0
327 #define cp_lexer_debugging_p(lexer) 0
328 #endif /* ENABLE_CHECKING */
330 static cp_token_cache *cp_token_cache_new
331 (cp_token *, cp_token *);
333 static void cp_parser_initial_pragma
336 /* Manifest constants. */
337 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
338 #define CP_SAVED_TOKEN_STACK 5
340 /* A token type for keywords, as opposed to ordinary identifiers. */
341 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
343 /* A token type for template-ids. If a template-id is processed while
344 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
345 the value of the CPP_TEMPLATE_ID is whatever was returned by
346 cp_parser_template_id. */
347 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
349 /* A token type for nested-name-specifiers. If a
350 nested-name-specifier is processed while parsing tentatively, it is
351 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
352 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
353 cp_parser_nested_name_specifier_opt. */
354 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
356 /* A token type for tokens that are not tokens at all; these are used
357 to represent slots in the array where there used to be a token
358 that has now been deleted. */
359 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
361 /* The number of token types, including C++-specific ones. */
362 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
366 #ifdef ENABLE_CHECKING
367 /* The stream to which debugging output should be written. */
368 static FILE *cp_lexer_debug_stream;
369 #endif /* ENABLE_CHECKING */
371 /* Nonzero if we are parsing an unevaluated operand: an operand to
372 sizeof, typeof, or alignof. */
373 int cp_unevaluated_operand;
375 /* Create a new main C++ lexer, the lexer that gets tokens from the
379 cp_lexer_new_main (void)
381 cp_token first_token;
388 /* It's possible that parsing the first pragma will load a PCH file,
389 which is a GC collection point. So we have to do that before
390 allocating any memory. */
391 cp_parser_initial_pragma (&first_token);
393 c_common_no_more_pch ();
395 /* Allocate the memory. */
396 lexer = ggc_alloc_cleared_cp_lexer ();
398 #ifdef ENABLE_CHECKING
399 /* Initially we are not debugging. */
400 lexer->debugging_p = false;
401 #endif /* ENABLE_CHECKING */
402 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
403 CP_SAVED_TOKEN_STACK);
405 /* Create the buffer. */
406 alloc = CP_LEXER_BUFFER_SIZE;
407 buffer = ggc_alloc_vec_cp_token (alloc);
409 /* Put the first token in the buffer. */
414 /* Get the remaining tokens from the preprocessor. */
415 while (pos->type != CPP_EOF)
422 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
423 pos = buffer + space;
425 cp_lexer_get_preprocessor_token (lexer, pos);
427 lexer->buffer = buffer;
428 lexer->buffer_length = alloc - space;
429 lexer->last_token = pos;
430 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
432 /* Subsequent preprocessor diagnostics should use compiler
433 diagnostic functions to get the compiler source location. */
436 gcc_assert (lexer->next_token->type != CPP_PURGED);
440 /* Create a new lexer whose token stream is primed with the tokens in
441 CACHE. When these tokens are exhausted, no new tokens will be read. */
444 cp_lexer_new_from_tokens (cp_token_cache *cache)
446 cp_token *first = cache->first;
447 cp_token *last = cache->last;
448 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
450 /* We do not own the buffer. */
451 lexer->buffer = NULL;
452 lexer->buffer_length = 0;
453 lexer->next_token = first == last ? &eof_token : first;
454 lexer->last_token = last;
456 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
457 CP_SAVED_TOKEN_STACK);
459 #ifdef ENABLE_CHECKING
460 /* Initially we are not debugging. */
461 lexer->debugging_p = false;
464 gcc_assert (lexer->next_token->type != CPP_PURGED);
468 /* Frees all resources associated with LEXER. */
471 cp_lexer_destroy (cp_lexer *lexer)
474 ggc_free (lexer->buffer);
475 VEC_free (cp_token_position, heap, lexer->saved_tokens);
479 /* Returns nonzero if debugging information should be output. */
481 #ifdef ENABLE_CHECKING
484 cp_lexer_debugging_p (cp_lexer *lexer)
486 return lexer->debugging_p;
489 #endif /* ENABLE_CHECKING */
491 static inline cp_token_position
492 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
494 gcc_assert (!previous_p || lexer->next_token != &eof_token);
496 return lexer->next_token - previous_p;
499 static inline cp_token *
500 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
505 /* nonzero if we are presently saving tokens. */
508 cp_lexer_saving_tokens (const cp_lexer* lexer)
510 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
513 /* Store the next token from the preprocessor in *TOKEN. Return true
514 if we reach EOF. If LEXER is NULL, assume we are handling an
515 initial #pragma pch_preprocess, and thus want the lexer to return
516 processed strings. */
519 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
521 static int is_extern_c = 0;
523 /* Get a new token from the preprocessor. */
525 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
526 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
527 token->keyword = RID_MAX;
528 token->pragma_kind = PRAGMA_NONE;
530 /* On some systems, some header files are surrounded by an
531 implicit extern "C" block. Set a flag in the token if it
532 comes from such a header. */
533 is_extern_c += pending_lang_change;
534 pending_lang_change = 0;
535 token->implicit_extern_c = is_extern_c > 0;
537 /* Check to see if this token is a keyword. */
538 if (token->type == CPP_NAME)
540 if (C_IS_RESERVED_WORD (token->u.value))
542 /* Mark this token as a keyword. */
543 token->type = CPP_KEYWORD;
544 /* Record which keyword. */
545 token->keyword = C_RID_CODE (token->u.value);
549 if (warn_cxx0x_compat
550 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
551 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
553 /* Warn about the C++0x keyword (but still treat it as
555 warning (OPT_Wc__0x_compat,
556 "identifier %qE will become a keyword in C++0x",
559 /* Clear out the C_RID_CODE so we don't warn about this
560 particular identifier-turned-keyword again. */
561 C_SET_RID_CODE (token->u.value, RID_MAX);
564 token->ambiguous_p = false;
565 token->keyword = RID_MAX;
568 /* 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_alloc_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_alloc_cleared_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 /* An entry in a queue of function arguments that require post-processing. */
1519 typedef struct GTY(()) cp_default_arg_entry_d {
1520 /* The current_class_type when we parsed this arg. */
1523 /* The function decl itself. */
1525 } cp_default_arg_entry;
1527 DEF_VEC_O(cp_default_arg_entry);
1528 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1530 /* An entry in a stack for member functions of local classes. */
1532 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1533 /* Functions with default arguments that require post-processing.
1534 Functions appear in this list in declaration order. */
1535 VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1537 /* Functions with defintions that require post-processing. Functions
1538 appear in this list in declaration order. */
1539 VEC(tree,gc) *funs_with_definitions;
1540 } cp_unparsed_functions_entry;
1542 DEF_VEC_O(cp_unparsed_functions_entry);
1543 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1545 /* The cp_parser structure represents the C++ parser. */
1547 typedef struct GTY(()) cp_parser {
1548 /* The lexer from which we are obtaining tokens. */
1551 /* The scope in which names should be looked up. If NULL_TREE, then
1552 we look up names in the scope that is currently open in the
1553 source program. If non-NULL, this is either a TYPE or
1554 NAMESPACE_DECL for the scope in which we should look. It can
1555 also be ERROR_MARK, when we've parsed a bogus scope.
1557 This value is not cleared automatically after a name is looked
1558 up, so we must be careful to clear it before starting a new look
1559 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1560 will look up `Z' in the scope of `X', rather than the current
1561 scope.) Unfortunately, it is difficult to tell when name lookup
1562 is complete, because we sometimes peek at a token, look it up,
1563 and then decide not to consume it. */
1566 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1567 last lookup took place. OBJECT_SCOPE is used if an expression
1568 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1569 respectively. QUALIFYING_SCOPE is used for an expression of the
1570 form "X::Y"; it refers to X. */
1572 tree qualifying_scope;
1574 /* A stack of parsing contexts. All but the bottom entry on the
1575 stack will be tentative contexts.
1577 We parse tentatively in order to determine which construct is in
1578 use in some situations. For example, in order to determine
1579 whether a statement is an expression-statement or a
1580 declaration-statement we parse it tentatively as a
1581 declaration-statement. If that fails, we then reparse the same
1582 token stream as an expression-statement. */
1583 cp_parser_context *context;
1585 /* True if we are parsing GNU C++. If this flag is not set, then
1586 GNU extensions are not recognized. */
1587 bool allow_gnu_extensions_p;
1589 /* TRUE if the `>' token should be interpreted as the greater-than
1590 operator. FALSE if it is the end of a template-id or
1591 template-parameter-list. In C++0x mode, this flag also applies to
1592 `>>' tokens, which are viewed as two consecutive `>' tokens when
1593 this flag is FALSE. */
1594 bool greater_than_is_operator_p;
1596 /* TRUE if default arguments are allowed within a parameter list
1597 that starts at this point. FALSE if only a gnu extension makes
1598 them permissible. */
1599 bool default_arg_ok_p;
1601 /* TRUE if we are parsing an integral constant-expression. See
1602 [expr.const] for a precise definition. */
1603 bool integral_constant_expression_p;
1605 /* TRUE if we are parsing an integral constant-expression -- but a
1606 non-constant expression should be permitted as well. This flag
1607 is used when parsing an array bound so that GNU variable-length
1608 arrays are tolerated. */
1609 bool allow_non_integral_constant_expression_p;
1611 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1612 been seen that makes the expression non-constant. */
1613 bool non_integral_constant_expression_p;
1615 /* TRUE if local variable names and `this' are forbidden in the
1617 bool local_variables_forbidden_p;
1619 /* TRUE if the declaration we are parsing is part of a
1620 linkage-specification of the form `extern string-literal
1622 bool in_unbraced_linkage_specification_p;
1624 /* TRUE if we are presently parsing a declarator, after the
1625 direct-declarator. */
1626 bool in_declarator_p;
1628 /* TRUE if we are presently parsing a template-argument-list. */
1629 bool in_template_argument_list_p;
1631 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1632 to IN_OMP_BLOCK if parsing OpenMP structured block and
1633 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1634 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1635 iteration-statement, OpenMP block or loop within that switch. */
1636 #define IN_SWITCH_STMT 1
1637 #define IN_ITERATION_STMT 2
1638 #define IN_OMP_BLOCK 4
1639 #define IN_OMP_FOR 8
1640 #define IN_IF_STMT 16
1641 unsigned char in_statement;
1643 /* TRUE if we are presently parsing the body of a switch statement.
1644 Note that this doesn't quite overlap with in_statement above.
1645 The difference relates to giving the right sets of error messages:
1646 "case not in switch" vs "break statement used with OpenMP...". */
1647 bool in_switch_statement_p;
1649 /* TRUE if we are parsing a type-id in an expression context. In
1650 such a situation, both "type (expr)" and "type (type)" are valid
1652 bool in_type_id_in_expr_p;
1654 /* TRUE if we are currently in a header file where declarations are
1655 implicitly extern "C". */
1656 bool implicit_extern_c;
1658 /* TRUE if strings in expressions should be translated to the execution
1660 bool translate_strings_p;
1662 /* TRUE if we are presently parsing the body of a function, but not
1664 bool in_function_body;
1666 /* If non-NULL, then we are parsing a construct where new type
1667 definitions are not permitted. The string stored here will be
1668 issued as an error message if a type is defined. */
1669 const char *type_definition_forbidden_message;
1671 /* A stack used for member functions of local classes. The lists
1672 contained in an individual entry can only be processed once the
1673 outermost class being defined is complete. */
1674 VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1676 /* The number of classes whose definitions are currently in
1678 unsigned num_classes_being_defined;
1680 /* The number of template parameter lists that apply directly to the
1681 current declaration. */
1682 unsigned num_template_parameter_lists;
1685 /* Managing the unparsed function queues. */
1687 #define unparsed_funs_with_default_args \
1688 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1689 #define unparsed_funs_with_definitions \
1690 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1693 push_unparsed_function_queues (cp_parser *parser)
1695 VEC_safe_push (cp_unparsed_functions_entry, gc,
1696 parser->unparsed_queues, NULL);
1697 unparsed_funs_with_default_args = NULL;
1698 unparsed_funs_with_definitions = make_tree_vector ();
1702 pop_unparsed_function_queues (cp_parser *parser)
1704 release_tree_vector (unparsed_funs_with_definitions);
1705 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1710 /* Constructors and destructors. */
1712 static cp_parser *cp_parser_new
1715 /* Routines to parse various constructs.
1717 Those that return `tree' will return the error_mark_node (rather
1718 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1719 Sometimes, they will return an ordinary node if error-recovery was
1720 attempted, even though a parse error occurred. So, to check
1721 whether or not a parse error occurred, you should always use
1722 cp_parser_error_occurred. If the construct is optional (indicated
1723 either by an `_opt' in the name of the function that does the
1724 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1725 the construct is not present. */
1727 /* Lexical conventions [gram.lex] */
1729 static tree cp_parser_identifier
1731 static tree cp_parser_string_literal
1732 (cp_parser *, bool, bool);
1734 /* Basic concepts [gram.basic] */
1736 static bool cp_parser_translation_unit
1739 /* Expressions [gram.expr] */
1741 static tree cp_parser_primary_expression
1742 (cp_parser *, bool, bool, bool, cp_id_kind *);
1743 static tree cp_parser_id_expression
1744 (cp_parser *, bool, bool, bool *, bool, bool);
1745 static tree cp_parser_unqualified_id
1746 (cp_parser *, bool, bool, bool, bool);
1747 static tree cp_parser_nested_name_specifier_opt
1748 (cp_parser *, bool, bool, bool, bool);
1749 static tree cp_parser_nested_name_specifier
1750 (cp_parser *, bool, bool, bool, bool);
1751 static tree cp_parser_qualifying_entity
1752 (cp_parser *, bool, bool, bool, bool, bool);
1753 static tree cp_parser_postfix_expression
1754 (cp_parser *, bool, bool, bool, cp_id_kind *);
1755 static tree cp_parser_postfix_open_square_expression
1756 (cp_parser *, tree, bool);
1757 static tree cp_parser_postfix_dot_deref_expression
1758 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1759 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1760 (cp_parser *, int, bool, bool, bool *);
1761 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1762 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1763 static void cp_parser_pseudo_destructor_name
1764 (cp_parser *, tree *, tree *);
1765 static tree cp_parser_unary_expression
1766 (cp_parser *, bool, bool, cp_id_kind *);
1767 static enum tree_code cp_parser_unary_operator
1769 static tree cp_parser_new_expression
1771 static VEC(tree,gc) *cp_parser_new_placement
1773 static tree cp_parser_new_type_id
1774 (cp_parser *, tree *);
1775 static cp_declarator *cp_parser_new_declarator_opt
1777 static cp_declarator *cp_parser_direct_new_declarator
1779 static VEC(tree,gc) *cp_parser_new_initializer
1781 static tree cp_parser_delete_expression
1783 static tree cp_parser_cast_expression
1784 (cp_parser *, bool, bool, cp_id_kind *);
1785 static tree cp_parser_binary_expression
1786 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1787 static tree cp_parser_question_colon_clause
1788 (cp_parser *, tree);
1789 static tree cp_parser_assignment_expression
1790 (cp_parser *, bool, cp_id_kind *);
1791 static enum tree_code cp_parser_assignment_operator_opt
1793 static tree cp_parser_expression
1794 (cp_parser *, bool, cp_id_kind *);
1795 static tree cp_parser_constant_expression
1796 (cp_parser *, bool, bool *);
1797 static tree cp_parser_builtin_offsetof
1799 static tree cp_parser_lambda_expression
1801 static void cp_parser_lambda_introducer
1802 (cp_parser *, tree);
1803 static void cp_parser_lambda_declarator_opt
1804 (cp_parser *, tree);
1805 static void cp_parser_lambda_body
1806 (cp_parser *, tree);
1808 /* Statements [gram.stmt.stmt] */
1810 static void cp_parser_statement
1811 (cp_parser *, tree, bool, bool *);
1812 static void cp_parser_label_for_labeled_statement
1814 static tree cp_parser_expression_statement
1815 (cp_parser *, tree);
1816 static tree cp_parser_compound_statement
1817 (cp_parser *, tree, bool);
1818 static void cp_parser_statement_seq_opt
1819 (cp_parser *, tree);
1820 static tree cp_parser_selection_statement
1821 (cp_parser *, bool *);
1822 static tree cp_parser_condition
1824 static tree cp_parser_iteration_statement
1826 static void cp_parser_for_init_statement
1828 static tree cp_parser_jump_statement
1830 static void cp_parser_declaration_statement
1833 static tree cp_parser_implicitly_scoped_statement
1834 (cp_parser *, bool *);
1835 static void cp_parser_already_scoped_statement
1838 /* Declarations [gram.dcl.dcl] */
1840 static void cp_parser_declaration_seq_opt
1842 static void cp_parser_declaration
1844 static void cp_parser_block_declaration
1845 (cp_parser *, bool);
1846 static void cp_parser_simple_declaration
1847 (cp_parser *, bool);
1848 static void cp_parser_decl_specifier_seq
1849 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1850 static tree cp_parser_storage_class_specifier_opt
1852 static tree cp_parser_function_specifier_opt
1853 (cp_parser *, cp_decl_specifier_seq *);
1854 static tree cp_parser_type_specifier
1855 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1857 static tree cp_parser_simple_type_specifier
1858 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1859 static tree cp_parser_type_name
1861 static tree cp_parser_nonclass_name
1862 (cp_parser* parser);
1863 static tree cp_parser_elaborated_type_specifier
1864 (cp_parser *, bool, bool);
1865 static tree cp_parser_enum_specifier
1867 static void cp_parser_enumerator_list
1868 (cp_parser *, tree);
1869 static void cp_parser_enumerator_definition
1870 (cp_parser *, tree);
1871 static tree cp_parser_namespace_name
1873 static void cp_parser_namespace_definition
1875 static void cp_parser_namespace_body
1877 static tree cp_parser_qualified_namespace_specifier
1879 static void cp_parser_namespace_alias_definition
1881 static bool cp_parser_using_declaration
1882 (cp_parser *, bool);
1883 static void cp_parser_using_directive
1885 static void cp_parser_asm_definition
1887 static void cp_parser_linkage_specification
1889 static void cp_parser_static_assert
1890 (cp_parser *, bool);
1891 static tree cp_parser_decltype
1894 /* Declarators [gram.dcl.decl] */
1896 static tree cp_parser_init_declarator
1897 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1898 static cp_declarator *cp_parser_declarator
1899 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1900 static cp_declarator *cp_parser_direct_declarator
1901 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1902 static enum tree_code cp_parser_ptr_operator
1903 (cp_parser *, tree *, cp_cv_quals *);
1904 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1906 static tree cp_parser_late_return_type_opt
1908 static tree cp_parser_declarator_id
1909 (cp_parser *, bool);
1910 static tree cp_parser_type_id
1912 static tree cp_parser_template_type_arg
1914 static tree cp_parser_trailing_type_id (cp_parser *);
1915 static tree cp_parser_type_id_1
1916 (cp_parser *, bool, bool);
1917 static void cp_parser_type_specifier_seq
1918 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1919 static tree cp_parser_parameter_declaration_clause
1921 static tree cp_parser_parameter_declaration_list
1922 (cp_parser *, bool *);
1923 static cp_parameter_declarator *cp_parser_parameter_declaration
1924 (cp_parser *, bool, bool *);
1925 static tree cp_parser_default_argument
1926 (cp_parser *, bool);
1927 static void cp_parser_function_body
1929 static tree cp_parser_initializer
1930 (cp_parser *, bool *, bool *);
1931 static tree cp_parser_initializer_clause
1932 (cp_parser *, bool *);
1933 static tree cp_parser_braced_list
1934 (cp_parser*, bool*);
1935 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1936 (cp_parser *, bool *);
1938 static bool cp_parser_ctor_initializer_opt_and_function_body
1941 /* Classes [gram.class] */
1943 static tree cp_parser_class_name
1944 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1945 static tree cp_parser_class_specifier
1947 static tree cp_parser_class_head
1948 (cp_parser *, bool *, tree *, tree *);
1949 static enum tag_types cp_parser_class_key
1951 static void cp_parser_member_specification_opt
1953 static void cp_parser_member_declaration
1955 static tree cp_parser_pure_specifier
1957 static tree cp_parser_constant_initializer
1960 /* Derived classes [gram.class.derived] */
1962 static tree cp_parser_base_clause
1964 static tree cp_parser_base_specifier
1967 /* Special member functions [gram.special] */
1969 static tree cp_parser_conversion_function_id
1971 static tree cp_parser_conversion_type_id
1973 static cp_declarator *cp_parser_conversion_declarator_opt
1975 static bool cp_parser_ctor_initializer_opt
1977 static void cp_parser_mem_initializer_list
1979 static tree cp_parser_mem_initializer
1981 static tree cp_parser_mem_initializer_id
1984 /* Overloading [gram.over] */
1986 static tree cp_parser_operator_function_id
1988 static tree cp_parser_operator
1991 /* Templates [gram.temp] */
1993 static void cp_parser_template_declaration
1994 (cp_parser *, bool);
1995 static tree cp_parser_template_parameter_list
1997 static tree cp_parser_template_parameter
1998 (cp_parser *, bool *, bool *);
1999 static tree cp_parser_type_parameter
2000 (cp_parser *, bool *);
2001 static tree cp_parser_template_id
2002 (cp_parser *, bool, bool, bool);
2003 static tree cp_parser_template_name
2004 (cp_parser *, bool, bool, bool, bool *);
2005 static tree cp_parser_template_argument_list
2007 static tree cp_parser_template_argument
2009 static void cp_parser_explicit_instantiation
2011 static void cp_parser_explicit_specialization
2014 /* Exception handling [gram.exception] */
2016 static tree cp_parser_try_block
2018 static bool cp_parser_function_try_block
2020 static void cp_parser_handler_seq
2022 static void cp_parser_handler
2024 static tree cp_parser_exception_declaration
2026 static tree cp_parser_throw_expression
2028 static tree cp_parser_exception_specification_opt
2030 static tree cp_parser_type_id_list
2033 /* GNU Extensions */
2035 static tree cp_parser_asm_specification_opt
2037 static tree cp_parser_asm_operand_list
2039 static tree cp_parser_asm_clobber_list
2041 static tree cp_parser_asm_label_list
2043 static tree cp_parser_attributes_opt
2045 static tree cp_parser_attribute_list
2047 static bool cp_parser_extension_opt
2048 (cp_parser *, int *);
2049 static void cp_parser_label_declaration
2052 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2053 static bool cp_parser_pragma
2054 (cp_parser *, enum pragma_context);
2056 /* Objective-C++ Productions */
2058 static tree cp_parser_objc_message_receiver
2060 static tree cp_parser_objc_message_args
2062 static tree cp_parser_objc_message_expression
2064 static tree cp_parser_objc_encode_expression
2066 static tree cp_parser_objc_defs_expression
2068 static tree cp_parser_objc_protocol_expression
2070 static tree cp_parser_objc_selector_expression
2072 static tree cp_parser_objc_expression
2074 static bool cp_parser_objc_selector_p
2076 static tree cp_parser_objc_selector
2078 static tree cp_parser_objc_protocol_refs_opt
2080 static void cp_parser_objc_declaration
2082 static tree cp_parser_objc_statement
2085 /* Utility Routines */
2087 static tree cp_parser_lookup_name
2088 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2089 static tree cp_parser_lookup_name_simple
2090 (cp_parser *, tree, location_t);
2091 static tree cp_parser_maybe_treat_template_as_class
2093 static bool cp_parser_check_declarator_template_parameters
2094 (cp_parser *, cp_declarator *, location_t);
2095 static bool cp_parser_check_template_parameters
2096 (cp_parser *, unsigned, location_t, cp_declarator *);
2097 static tree cp_parser_simple_cast_expression
2099 static tree cp_parser_global_scope_opt
2100 (cp_parser *, bool);
2101 static bool cp_parser_constructor_declarator_p
2102 (cp_parser *, bool);
2103 static tree cp_parser_function_definition_from_specifiers_and_declarator
2104 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2105 static tree cp_parser_function_definition_after_declarator
2106 (cp_parser *, bool);
2107 static void cp_parser_template_declaration_after_export
2108 (cp_parser *, bool);
2109 static void cp_parser_perform_template_parameter_access_checks
2110 (VEC (deferred_access_check,gc)*);
2111 static tree cp_parser_single_declaration
2112 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2113 static tree cp_parser_functional_cast
2114 (cp_parser *, tree);
2115 static tree cp_parser_save_member_function_body
2116 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2117 static tree cp_parser_enclosed_template_argument_list
2119 static void cp_parser_save_default_args
2120 (cp_parser *, tree);
2121 static void cp_parser_late_parsing_for_member
2122 (cp_parser *, tree);
2123 static void cp_parser_late_parsing_default_args
2124 (cp_parser *, tree);
2125 static tree cp_parser_sizeof_operand
2126 (cp_parser *, enum rid);
2127 static tree cp_parser_trait_expr
2128 (cp_parser *, enum rid);
2129 static bool cp_parser_declares_only_class_p
2131 static void cp_parser_set_storage_class
2132 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2133 static void cp_parser_set_decl_spec_type
2134 (cp_decl_specifier_seq *, tree, location_t, bool);
2135 static bool cp_parser_friend_p
2136 (const cp_decl_specifier_seq *);
2137 static void cp_parser_required_error
2138 (cp_parser *, required_token, bool);
2139 static cp_token *cp_parser_require
2140 (cp_parser *, enum cpp_ttype, required_token);
2141 static cp_token *cp_parser_require_keyword
2142 (cp_parser *, enum rid, required_token);
2143 static bool cp_parser_token_starts_function_definition_p
2145 static bool cp_parser_next_token_starts_class_definition_p
2147 static bool cp_parser_next_token_ends_template_argument_p
2149 static bool cp_parser_nth_token_starts_template_argument_list_p
2150 (cp_parser *, size_t);
2151 static enum tag_types cp_parser_token_is_class_key
2153 static void cp_parser_check_class_key
2154 (enum tag_types, tree type);
2155 static void cp_parser_check_access_in_redeclaration
2156 (tree type, location_t location);
2157 static bool cp_parser_optional_template_keyword
2159 static void cp_parser_pre_parsed_nested_name_specifier
2161 static bool cp_parser_cache_group
2162 (cp_parser *, enum cpp_ttype, unsigned);
2163 static void cp_parser_parse_tentatively
2165 static void cp_parser_commit_to_tentative_parse
2167 static void cp_parser_abort_tentative_parse
2169 static bool cp_parser_parse_definitely
2171 static inline bool cp_parser_parsing_tentatively
2173 static bool cp_parser_uncommitted_to_tentative_parse_p
2175 static void cp_parser_error
2176 (cp_parser *, const char *);
2177 static void cp_parser_name_lookup_error
2178 (cp_parser *, tree, tree, name_lookup_error, location_t);
2179 static bool cp_parser_simulate_error
2181 static bool cp_parser_check_type_definition
2183 static void cp_parser_check_for_definition_in_return_type
2184 (cp_declarator *, tree, location_t type_location);
2185 static void cp_parser_check_for_invalid_template_id
2186 (cp_parser *, tree, location_t location);
2187 static bool cp_parser_non_integral_constant_expression
2188 (cp_parser *, non_integral_constant);
2189 static void cp_parser_diagnose_invalid_type_name
2190 (cp_parser *, tree, tree, location_t);
2191 static bool cp_parser_parse_and_diagnose_invalid_type_name
2193 static int cp_parser_skip_to_closing_parenthesis
2194 (cp_parser *, bool, bool, bool);
2195 static void cp_parser_skip_to_end_of_statement
2197 static void cp_parser_consume_semicolon_at_end_of_statement
2199 static void cp_parser_skip_to_end_of_block_or_statement
2201 static bool cp_parser_skip_to_closing_brace
2203 static void cp_parser_skip_to_end_of_template_parameter_list
2205 static void cp_parser_skip_to_pragma_eol
2206 (cp_parser*, cp_token *);
2207 static bool cp_parser_error_occurred
2209 static bool cp_parser_allow_gnu_extensions_p
2211 static bool cp_parser_is_string_literal
2213 static bool cp_parser_is_keyword
2214 (cp_token *, enum rid);
2215 static tree cp_parser_make_typename_type
2216 (cp_parser *, tree, tree, location_t location);
2217 static cp_declarator * cp_parser_make_indirect_declarator
2218 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2220 /* Returns nonzero if we are parsing tentatively. */
2223 cp_parser_parsing_tentatively (cp_parser* parser)
2225 return parser->context->next != NULL;
2228 /* Returns nonzero if TOKEN is a string literal. */
2231 cp_parser_is_string_literal (cp_token* token)
2233 return (token->type == CPP_STRING ||
2234 token->type == CPP_STRING16 ||
2235 token->type == CPP_STRING32 ||
2236 token->type == CPP_WSTRING ||
2237 token->type == CPP_UTF8STRING);
2240 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2243 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2245 return token->keyword == keyword;
2248 /* If not parsing tentatively, issue a diagnostic of the form
2249 FILE:LINE: MESSAGE before TOKEN
2250 where TOKEN is the next token in the input stream. MESSAGE
2251 (specified by the caller) is usually of the form "expected
2255 cp_parser_error (cp_parser* parser, const char* gmsgid)
2257 if (!cp_parser_simulate_error (parser))
2259 cp_token *token = cp_lexer_peek_token (parser->lexer);
2260 /* This diagnostic makes more sense if it is tagged to the line
2261 of the token we just peeked at. */
2262 cp_lexer_set_source_position_from_token (token);
2264 if (token->type == CPP_PRAGMA)
2266 error_at (token->location,
2267 "%<#pragma%> is not allowed here");
2268 cp_parser_skip_to_pragma_eol (parser, token);
2272 c_parse_error (gmsgid,
2273 /* Because c_parser_error does not understand
2274 CPP_KEYWORD, keywords are treated like
2276 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2277 token->u.value, token->flags);
2281 /* Issue an error about name-lookup failing. NAME is the
2282 IDENTIFIER_NODE DECL is the result of
2283 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2284 the thing that we hoped to find. */
2287 cp_parser_name_lookup_error (cp_parser* parser,
2290 name_lookup_error desired,
2291 location_t location)
2293 /* If name lookup completely failed, tell the user that NAME was not
2295 if (decl == error_mark_node)
2297 if (parser->scope && parser->scope != global_namespace)
2298 error_at (location, "%<%E::%E%> has not been declared",
2299 parser->scope, name);
2300 else if (parser->scope == global_namespace)
2301 error_at (location, "%<::%E%> has not been declared", name);
2302 else if (parser->object_scope
2303 && !CLASS_TYPE_P (parser->object_scope))
2304 error_at (location, "request for member %qE in non-class type %qT",
2305 name, parser->object_scope);
2306 else if (parser->object_scope)
2307 error_at (location, "%<%T::%E%> has not been declared",
2308 parser->object_scope, name);
2310 error_at (location, "%qE has not been declared", name);
2312 else if (parser->scope && parser->scope != global_namespace)
2317 error_at (location, "%<%E::%E%> is not a type",
2318 parser->scope, name);
2321 error_at (location, "%<%E::%E%> is not a class or namespace",
2322 parser->scope, name);
2326 "%<%E::%E%> is not a class, namespace, or enumeration",
2327 parser->scope, name);
2334 else if (parser->scope == global_namespace)
2339 error_at (location, "%<::%E%> is not a type", name);
2342 error_at (location, "%<::%E%> is not a class or namespace", name);
2346 "%<::%E%> is not a class, namespace, or enumeration",
2358 error_at (location, "%qE is not a type", name);
2361 error_at (location, "%qE is not a class or namespace", name);
2365 "%qE is not a class, namespace, or enumeration", name);
2373 /* If we are parsing tentatively, remember that an error has occurred
2374 during this tentative parse. Returns true if the error was
2375 simulated; false if a message should be issued by the caller. */
2378 cp_parser_simulate_error (cp_parser* parser)
2380 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2382 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2388 /* Check for repeated decl-specifiers. */
2391 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2392 location_t location)
2396 for (ds = ds_first; ds != ds_last; ++ds)
2398 unsigned count = decl_specs->specs[ds];
2401 /* The "long" specifier is a special case because of "long long". */
2405 error_at (location, "%<long long long%> is too long for GCC");
2407 pedwarn_cxx98 (location, OPT_Wlong_long,
2408 "ISO C++ 1998 does not support %<long long%>");
2412 static const char *const decl_spec_names[] = {
2429 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2434 /* This function is called when a type is defined. If type
2435 definitions are forbidden at this point, an error message is
2439 cp_parser_check_type_definition (cp_parser* parser)
2441 /* If types are forbidden here, issue a message. */
2442 if (parser->type_definition_forbidden_message)
2444 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2445 in the message need to be interpreted. */
2446 error (parser->type_definition_forbidden_message);
2452 /* This function is called when the DECLARATOR is processed. The TYPE
2453 was a type defined in the decl-specifiers. If it is invalid to
2454 define a type in the decl-specifiers for DECLARATOR, an error is
2455 issued. TYPE_LOCATION is the location of TYPE and is used
2456 for error reporting. */
2459 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2460 tree type, location_t type_location)
2462 /* [dcl.fct] forbids type definitions in return types.
2463 Unfortunately, it's not easy to know whether or not we are
2464 processing a return type until after the fact. */
2466 && (declarator->kind == cdk_pointer
2467 || declarator->kind == cdk_reference
2468 || declarator->kind == cdk_ptrmem))
2469 declarator = declarator->declarator;
2471 && declarator->kind == cdk_function)
2473 error_at (type_location,
2474 "new types may not be defined in a return type");
2475 inform (type_location,
2476 "(perhaps a semicolon is missing after the definition of %qT)",
2481 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2482 "<" in any valid C++ program. If the next token is indeed "<",
2483 issue a message warning the user about what appears to be an
2484 invalid attempt to form a template-id. LOCATION is the location
2485 of the type-specifier (TYPE) */
2488 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2489 tree type, location_t location)
2491 cp_token_position start = 0;
2493 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2496 error_at (location, "%qT is not a template", type);
2497 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2498 error_at (location, "%qE is not a template", type);
2500 error_at (location, "invalid template-id");
2501 /* Remember the location of the invalid "<". */
2502 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2503 start = cp_lexer_token_position (parser->lexer, true);
2504 /* Consume the "<". */
2505 cp_lexer_consume_token (parser->lexer);
2506 /* Parse the template arguments. */
2507 cp_parser_enclosed_template_argument_list (parser);
2508 /* Permanently remove the invalid template arguments so that
2509 this error message is not issued again. */
2511 cp_lexer_purge_tokens_after (parser->lexer, start);
2515 /* If parsing an integral constant-expression, issue an error message
2516 about the fact that THING appeared and return true. Otherwise,
2517 return false. In either case, set
2518 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2521 cp_parser_non_integral_constant_expression (cp_parser *parser,
2522 non_integral_constant thing)
2524 parser->non_integral_constant_expression_p = true;
2525 if (parser->integral_constant_expression_p)
2527 if (!parser->allow_non_integral_constant_expression_p)
2529 const char *msg = NULL;
2533 error ("floating-point literal "
2534 "cannot appear in a constant-expression");
2537 error ("a cast to a type other than an integral or "
2538 "enumeration type cannot appear in a "
2539 "constant-expression");
2542 error ("%<typeid%> operator "
2543 "cannot appear in a constant-expression");
2546 error ("non-constant compound literals "
2547 "cannot appear in a constant-expression");
2550 error ("a function call "
2551 "cannot appear in a constant-expression");
2554 error ("an increment "
2555 "cannot appear in a constant-expression");
2558 error ("an decrement "
2559 "cannot appear in a constant-expression");
2562 error ("an array reference "
2563 "cannot appear in a constant-expression");
2565 case NIC_ADDR_LABEL:
2566 error ("the address of a label "
2567 "cannot appear in a constant-expression");
2569 case NIC_OVERLOADED:
2570 error ("calls to overloaded operators "
2571 "cannot appear in a constant-expression");
2573 case NIC_ASSIGNMENT:
2574 error ("an assignment cannot appear in a constant-expression");
2577 error ("a comma operator "
2578 "cannot appear in a constant-expression");
2580 case NIC_CONSTRUCTOR:
2581 error ("a call to a constructor "
2582 "cannot appear in a constant-expression");
2588 msg = "__FUNCTION__";
2590 case NIC_PRETTY_FUNC:
2591 msg = "__PRETTY_FUNCTION__";
2611 case NIC_PREINCREMENT:
2614 case NIC_PREDECREMENT:
2627 error ("%qs cannot appear in a constant-expression", msg);
2634 /* Emit a diagnostic for an invalid type name. SCOPE is the
2635 qualifying scope (or NULL, if none) for ID. This function commits
2636 to the current active tentative parse, if any. (Otherwise, the
2637 problematic construct might be encountered again later, resulting
2638 in duplicate error messages.) LOCATION is the location of ID. */
2641 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2642 tree scope, tree id,
2643 location_t location)
2645 tree decl, old_scope;
2646 /* Try to lookup the identifier. */
2647 old_scope = parser->scope;
2648 parser->scope = scope;
2649 decl = cp_parser_lookup_name_simple (parser, id, location);
2650 parser->scope = old_scope;
2651 /* If the lookup found a template-name, it means that the user forgot
2652 to specify an argument list. Emit a useful error message. */
2653 if (TREE_CODE (decl) == TEMPLATE_DECL)
2655 "invalid use of template-name %qE without an argument list",
2657 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2658 error_at (location, "invalid use of destructor %qD as a type", id);
2659 else if (TREE_CODE (decl) == TYPE_DECL)
2660 /* Something like 'unsigned A a;' */
2661 error_at (location, "invalid combination of multiple type-specifiers");
2662 else if (!parser->scope)
2664 /* Issue an error message. */
2665 error_at (location, "%qE does not name a type", id);
2666 /* If we're in a template class, it's possible that the user was
2667 referring to a type from a base class. For example:
2669 template <typename T> struct A { typedef T X; };
2670 template <typename T> struct B : public A<T> { X x; };
2672 The user should have said "typename A<T>::X". */
2673 if (processing_template_decl && current_class_type
2674 && TYPE_BINFO (current_class_type))
2678 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2682 tree base_type = BINFO_TYPE (b);
2683 if (CLASS_TYPE_P (base_type)
2684 && dependent_type_p (base_type))
2687 /* Go from a particular instantiation of the
2688 template (which will have an empty TYPE_FIELDs),
2689 to the main version. */
2690 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2691 for (field = TYPE_FIELDS (base_type);
2693 field = DECL_CHAIN (field))
2694 if (TREE_CODE (field) == TYPE_DECL
2695 && DECL_NAME (field) == id)
2698 "(perhaps %<typename %T::%E%> was intended)",
2699 BINFO_TYPE (b), id);
2708 /* Here we diagnose qualified-ids where the scope is actually correct,
2709 but the identifier does not resolve to a valid type name. */
2710 else if (parser->scope != error_mark_node)
2712 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2713 error_at (location, "%qE in namespace %qE does not name a type",
2715 else if (CLASS_TYPE_P (parser->scope)
2716 && constructor_name_p (id, parser->scope))
2719 error_at (location, "%<%T::%E%> names the constructor, not"
2720 " the type", parser->scope, id);
2721 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2722 error_at (location, "and %qT has no template constructors",
2725 else if (TYPE_P (parser->scope)
2726 && dependent_scope_p (parser->scope))
2727 error_at (location, "need %<typename%> before %<%T::%E%> because "
2728 "%qT is a dependent scope",
2729 parser->scope, id, parser->scope);
2730 else if (TYPE_P (parser->scope))
2731 error_at (location, "%qE in class %qT does not name a type",
2736 cp_parser_commit_to_tentative_parse (parser);
2739 /* Check for a common situation where a type-name should be present,
2740 but is not, and issue a sensible error message. Returns true if an
2741 invalid type-name was detected.
2743 The situation handled by this function are variable declarations of the
2744 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2745 Usually, `ID' should name a type, but if we got here it means that it
2746 does not. We try to emit the best possible error message depending on
2747 how exactly the id-expression looks like. */
2750 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2753 cp_token *token = cp_lexer_peek_token (parser->lexer);
2755 /* Avoid duplicate error about ambiguous lookup. */
2756 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2758 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2759 if (next->type == CPP_NAME && next->ambiguous_p)
2763 cp_parser_parse_tentatively (parser);
2764 id = cp_parser_id_expression (parser,
2765 /*template_keyword_p=*/false,
2766 /*check_dependency_p=*/true,
2767 /*template_p=*/NULL,
2768 /*declarator_p=*/true,
2769 /*optional_p=*/false);
2770 /* If the next token is a (, this is a function with no explicit return
2771 type, i.e. constructor, destructor or conversion op. */
2772 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2773 || TREE_CODE (id) == TYPE_DECL)
2775 cp_parser_abort_tentative_parse (parser);
2778 if (!cp_parser_parse_definitely (parser))
2781 /* Emit a diagnostic for the invalid type. */
2782 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2783 id, token->location);
2785 /* If we aren't in the middle of a declarator (i.e. in a
2786 parameter-declaration-clause), skip to the end of the declaration;
2787 there's no point in trying to process it. */
2788 if (!parser->in_declarator_p)
2789 cp_parser_skip_to_end_of_block_or_statement (parser);
2793 /* Consume tokens up to, and including, the next non-nested closing `)'.
2794 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2795 are doing error recovery. Returns -1 if OR_COMMA is true and we
2796 found an unnested comma. */
2799 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2804 unsigned paren_depth = 0;
2805 unsigned brace_depth = 0;
2806 unsigned square_depth = 0;
2808 if (recovering && !or_comma
2809 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2814 cp_token * token = cp_lexer_peek_token (parser->lexer);
2816 switch (token->type)
2819 case CPP_PRAGMA_EOL:
2820 /* If we've run out of tokens, then there is no closing `)'. */
2823 /* This is good for lambda expression capture-lists. */
2824 case CPP_OPEN_SQUARE:
2827 case CPP_CLOSE_SQUARE:
2828 if (!square_depth--)
2833 /* This matches the processing in skip_to_end_of_statement. */
2838 case CPP_OPEN_BRACE:
2841 case CPP_CLOSE_BRACE:
2847 if (recovering && or_comma && !brace_depth && !paren_depth
2852 case CPP_OPEN_PAREN:
2857 case CPP_CLOSE_PAREN:
2858 if (!brace_depth && !paren_depth--)
2861 cp_lexer_consume_token (parser->lexer);
2870 /* Consume the token. */
2871 cp_lexer_consume_token (parser->lexer);
2875 /* Consume tokens until we reach the end of the current statement.
2876 Normally, that will be just before consuming a `;'. However, if a
2877 non-nested `}' comes first, then we stop before consuming that. */
2880 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2882 unsigned nesting_depth = 0;
2886 cp_token *token = cp_lexer_peek_token (parser->lexer);
2888 switch (token->type)
2891 case CPP_PRAGMA_EOL:
2892 /* If we've run out of tokens, stop. */
2896 /* If the next token is a `;', we have reached the end of the
2902 case CPP_CLOSE_BRACE:
2903 /* If this is a non-nested '}', stop before consuming it.
2904 That way, when confronted with something like:
2908 we stop before consuming the closing '}', even though we
2909 have not yet reached a `;'. */
2910 if (nesting_depth == 0)
2913 /* If it is the closing '}' for a block that we have
2914 scanned, stop -- but only after consuming the token.
2920 we will stop after the body of the erroneously declared
2921 function, but before consuming the following `typedef'
2923 if (--nesting_depth == 0)
2925 cp_lexer_consume_token (parser->lexer);
2929 case CPP_OPEN_BRACE:
2937 /* Consume the token. */
2938 cp_lexer_consume_token (parser->lexer);
2942 /* This function is called at the end of a statement or declaration.
2943 If the next token is a semicolon, it is consumed; otherwise, error
2944 recovery is attempted. */
2947 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2949 /* Look for the trailing `;'. */
2950 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2952 /* If there is additional (erroneous) input, skip to the end of
2954 cp_parser_skip_to_end_of_statement (parser);
2955 /* If the next token is now a `;', consume it. */
2956 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2957 cp_lexer_consume_token (parser->lexer);
2961 /* Skip tokens until we have consumed an entire block, or until we
2962 have consumed a non-nested `;'. */
2965 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2967 int nesting_depth = 0;
2969 while (nesting_depth >= 0)
2971 cp_token *token = cp_lexer_peek_token (parser->lexer);
2973 switch (token->type)
2976 case CPP_PRAGMA_EOL:
2977 /* If we've run out of tokens, stop. */
2981 /* Stop if this is an unnested ';'. */
2986 case CPP_CLOSE_BRACE:
2987 /* Stop if this is an unnested '}', or closes the outermost
2990 if (nesting_depth < 0)
2996 case CPP_OPEN_BRACE:
3005 /* Consume the token. */
3006 cp_lexer_consume_token (parser->lexer);
3010 /* Skip tokens until a non-nested closing curly brace is the next
3011 token, or there are no more tokens. Return true in the first case,
3015 cp_parser_skip_to_closing_brace (cp_parser *parser)
3017 unsigned nesting_depth = 0;
3021 cp_token *token = cp_lexer_peek_token (parser->lexer);
3023 switch (token->type)
3026 case CPP_PRAGMA_EOL:
3027 /* If we've run out of tokens, stop. */
3030 case CPP_CLOSE_BRACE:
3031 /* If the next token is a non-nested `}', then we have reached
3032 the end of the current block. */
3033 if (nesting_depth-- == 0)
3037 case CPP_OPEN_BRACE:
3038 /* If it the next token is a `{', then we are entering a new
3039 block. Consume the entire block. */
3047 /* Consume the token. */
3048 cp_lexer_consume_token (parser->lexer);
3052 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3053 parameter is the PRAGMA token, allowing us to purge the entire pragma
3057 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3061 parser->lexer->in_pragma = false;
3064 token = cp_lexer_consume_token (parser->lexer);
3065 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3067 /* Ensure that the pragma is not parsed again. */
3068 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3071 /* Require pragma end of line, resyncing with it as necessary. The
3072 arguments are as for cp_parser_skip_to_pragma_eol. */
3075 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3077 parser->lexer->in_pragma = false;
3078 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3079 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3082 /* This is a simple wrapper around make_typename_type. When the id is
3083 an unresolved identifier node, we can provide a superior diagnostic
3084 using cp_parser_diagnose_invalid_type_name. */
3087 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3088 tree id, location_t id_location)
3091 if (TREE_CODE (id) == IDENTIFIER_NODE)
3093 result = make_typename_type (scope, id, typename_type,
3094 /*complain=*/tf_none);
3095 if (result == error_mark_node)
3096 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3099 return make_typename_type (scope, id, typename_type, tf_error);
3102 /* This is a wrapper around the
3103 make_{pointer,ptrmem,reference}_declarator functions that decides
3104 which one to call based on the CODE and CLASS_TYPE arguments. The
3105 CODE argument should be one of the values returned by
3106 cp_parser_ptr_operator. */
3107 static cp_declarator *
3108 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3109 cp_cv_quals cv_qualifiers,
3110 cp_declarator *target)
3112 if (code == ERROR_MARK)
3113 return cp_error_declarator;
3115 if (code == INDIRECT_REF)
3116 if (class_type == NULL_TREE)
3117 return make_pointer_declarator (cv_qualifiers, target);
3119 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3120 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3121 return make_reference_declarator (cv_qualifiers, target, false);
3122 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3123 return make_reference_declarator (cv_qualifiers, target, true);
3127 /* Create a new C++ parser. */
3130 cp_parser_new (void)
3136 /* cp_lexer_new_main is called before doing GC allocation because
3137 cp_lexer_new_main might load a PCH file. */
3138 lexer = cp_lexer_new_main ();
3140 /* Initialize the binops_by_token so that we can get the tree
3141 directly from the token. */
3142 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3143 binops_by_token[binops[i].token_type] = binops[i];
3145 parser = ggc_alloc_cleared_cp_parser ();
3146 parser->lexer = lexer;
3147 parser->context = cp_parser_context_new (NULL);
3149 /* For now, we always accept GNU extensions. */
3150 parser->allow_gnu_extensions_p = 1;
3152 /* The `>' token is a greater-than operator, not the end of a
3154 parser->greater_than_is_operator_p = true;
3156 parser->default_arg_ok_p = true;
3158 /* We are not parsing a constant-expression. */
3159 parser->integral_constant_expression_p = false;
3160 parser->allow_non_integral_constant_expression_p = false;
3161 parser->non_integral_constant_expression_p = false;
3163 /* Local variable names are not forbidden. */
3164 parser->local_variables_forbidden_p = false;
3166 /* We are not processing an `extern "C"' declaration. */
3167 parser->in_unbraced_linkage_specification_p = false;
3169 /* We are not processing a declarator. */
3170 parser->in_declarator_p = false;
3172 /* We are not processing a template-argument-list. */
3173 parser->in_template_argument_list_p = false;
3175 /* We are not in an iteration statement. */
3176 parser->in_statement = 0;
3178 /* We are not in a switch statement. */
3179 parser->in_switch_statement_p = false;
3181 /* We are not parsing a type-id inside an expression. */
3182 parser->in_type_id_in_expr_p = false;
3184 /* Declarations aren't implicitly extern "C". */
3185 parser->implicit_extern_c = false;
3187 /* String literals should be translated to the execution character set. */
3188 parser->translate_strings_p = true;
3190 /* We are not parsing a function body. */
3191 parser->in_function_body = false;
3193 /* The unparsed function queue is empty. */
3194 push_unparsed_function_queues (parser);
3196 /* There are no classes being defined. */
3197 parser->num_classes_being_defined = 0;
3199 /* No template parameters apply. */
3200 parser->num_template_parameter_lists = 0;
3205 /* Create a cp_lexer structure which will emit the tokens in CACHE
3206 and push it onto the parser's lexer stack. This is used for delayed
3207 parsing of in-class method bodies and default arguments, and should
3208 not be confused with tentative parsing. */
3210 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3212 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3213 lexer->next = parser->lexer;
3214 parser->lexer = lexer;
3216 /* Move the current source position to that of the first token in the
3218 cp_lexer_set_source_position_from_token (lexer->next_token);
3221 /* Pop the top lexer off the parser stack. This is never used for the
3222 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3224 cp_parser_pop_lexer (cp_parser *parser)
3226 cp_lexer *lexer = parser->lexer;
3227 parser->lexer = lexer->next;
3228 cp_lexer_destroy (lexer);
3230 /* Put the current source position back where it was before this
3231 lexer was pushed. */
3232 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3235 /* Lexical conventions [gram.lex] */
3237 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3241 cp_parser_identifier (cp_parser* parser)
3245 /* Look for the identifier. */
3246 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3247 /* Return the value. */
3248 return token ? token->u.value : error_mark_node;
3251 /* Parse a sequence of adjacent string constants. Returns a
3252 TREE_STRING representing the combined, nul-terminated string
3253 constant. If TRANSLATE is true, translate the string to the
3254 execution character set. If WIDE_OK is true, a wide string is
3257 C++98 [lex.string] says that if a narrow string literal token is
3258 adjacent to a wide string literal token, the behavior is undefined.
3259 However, C99 6.4.5p4 says that this results in a wide string literal.
3260 We follow C99 here, for consistency with the C front end.
3262 This code is largely lifted from lex_string() in c-lex.c.
3264 FUTURE: ObjC++ will need to handle @-strings here. */
3266 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3270 struct obstack str_ob;
3271 cpp_string str, istr, *strs;
3273 enum cpp_ttype type;
3275 tok = cp_lexer_peek_token (parser->lexer);
3276 if (!cp_parser_is_string_literal (tok))
3278 cp_parser_error (parser, "expected string-literal");
3279 return error_mark_node;
3284 /* Try to avoid the overhead of creating and destroying an obstack
3285 for the common case of just one string. */
3286 if (!cp_parser_is_string_literal
3287 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3289 cp_lexer_consume_token (parser->lexer);
3291 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3292 str.len = TREE_STRING_LENGTH (tok->u.value);
3299 gcc_obstack_init (&str_ob);
3304 cp_lexer_consume_token (parser->lexer);
3306 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3307 str.len = TREE_STRING_LENGTH (tok->u.value);
3309 if (type != tok->type)
3311 if (type == CPP_STRING)
3313 else if (tok->type != CPP_STRING)
3314 error_at (tok->location,
3315 "unsupported non-standard concatenation "
3316 "of string literals");
3319 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3321 tok = cp_lexer_peek_token (parser->lexer);
3323 while (cp_parser_is_string_literal (tok));
3325 strs = (cpp_string *) obstack_finish (&str_ob);
3328 if (type != CPP_STRING && !wide_ok)
3330 cp_parser_error (parser, "a wide string is invalid in this context");
3334 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3335 (parse_in, strs, count, &istr, type))
3337 value = build_string (istr.len, (const char *)istr.text);
3338 free (CONST_CAST (unsigned char *, istr.text));
3344 case CPP_UTF8STRING:
3345 TREE_TYPE (value) = char_array_type_node;
3348 TREE_TYPE (value) = char16_array_type_node;
3351 TREE_TYPE (value) = char32_array_type_node;
3354 TREE_TYPE (value) = wchar_array_type_node;
3358 value = fix_string_type (value);
3361 /* cpp_interpret_string has issued an error. */
3362 value = error_mark_node;
3365 obstack_free (&str_ob, 0);
3371 /* Basic concepts [gram.basic] */
3373 /* Parse a translation-unit.
3376 declaration-seq [opt]
3378 Returns TRUE if all went well. */
3381 cp_parser_translation_unit (cp_parser* parser)
3383 /* The address of the first non-permanent object on the declarator
3385 static void *declarator_obstack_base;
3389 /* Create the declarator obstack, if necessary. */
3390 if (!cp_error_declarator)
3392 gcc_obstack_init (&declarator_obstack);
3393 /* Create the error declarator. */
3394 cp_error_declarator = make_declarator (cdk_error);
3395 /* Create the empty parameter list. */
3396 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3397 /* Remember where the base of the declarator obstack lies. */
3398 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3401 cp_parser_declaration_seq_opt (parser);
3403 /* If there are no tokens left then all went well. */
3404 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3406 /* Get rid of the token array; we don't need it any more. */
3407 cp_lexer_destroy (parser->lexer);
3408 parser->lexer = NULL;
3410 /* This file might have been a context that's implicitly extern
3411 "C". If so, pop the lang context. (Only relevant for PCH.) */
3412 if (parser->implicit_extern_c)
3414 pop_lang_context ();
3415 parser->implicit_extern_c = false;
3419 finish_translation_unit ();
3425 cp_parser_error (parser, "expected declaration");
3429 /* Make sure the declarator obstack was fully cleaned up. */
3430 gcc_assert (obstack_next_free (&declarator_obstack)
3431 == declarator_obstack_base);
3433 /* All went well. */
3437 /* Expressions [gram.expr] */
3439 /* Parse a primary-expression.
3450 ( compound-statement )
3451 __builtin_va_arg ( assignment-expression , type-id )
3452 __builtin_offsetof ( type-id , offsetof-expression )
3455 __has_nothrow_assign ( type-id )
3456 __has_nothrow_constructor ( type-id )
3457 __has_nothrow_copy ( type-id )
3458 __has_trivial_assign ( type-id )
3459 __has_trivial_constructor ( type-id )
3460 __has_trivial_copy ( type-id )
3461 __has_trivial_destructor ( type-id )
3462 __has_virtual_destructor ( type-id )
3463 __is_abstract ( type-id )
3464 __is_base_of ( type-id , type-id )
3465 __is_class ( type-id )
3466 __is_convertible_to ( type-id , type-id )
3467 __is_empty ( type-id )
3468 __is_enum ( type-id )
3469 __is_pod ( type-id )
3470 __is_polymorphic ( type-id )
3471 __is_union ( type-id )
3473 Objective-C++ Extension:
3481 ADDRESS_P is true iff this expression was immediately preceded by
3482 "&" and therefore might denote a pointer-to-member. CAST_P is true
3483 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3484 true iff this expression is a template argument.
3486 Returns a representation of the expression. Upon return, *IDK
3487 indicates what kind of id-expression (if any) was present. */
3490 cp_parser_primary_expression (cp_parser *parser,
3493 bool template_arg_p,
3496 cp_token *token = NULL;
3498 /* Assume the primary expression is not an id-expression. */
3499 *idk = CP_ID_KIND_NONE;
3501 /* Peek at the next token. */
3502 token = cp_lexer_peek_token (parser->lexer);
3503 switch (token->type)
3516 token = cp_lexer_consume_token (parser->lexer);
3517 if (TREE_CODE (token->u.value) == FIXED_CST)
3519 error_at (token->location,
3520 "fixed-point types not supported in C++");
3521 return error_mark_node;
3523 /* Floating-point literals are only allowed in an integral
3524 constant expression if they are cast to an integral or
3525 enumeration type. */
3526 if (TREE_CODE (token->u.value) == REAL_CST
3527 && parser->integral_constant_expression_p
3530 /* CAST_P will be set even in invalid code like "int(2.7 +
3531 ...)". Therefore, we have to check that the next token
3532 is sure to end the cast. */
3535 cp_token *next_token;
3537 next_token = cp_lexer_peek_token (parser->lexer);
3538 if (/* The comma at the end of an
3539 enumerator-definition. */
3540 next_token->type != CPP_COMMA
3541 /* The curly brace at the end of an enum-specifier. */
3542 && next_token->type != CPP_CLOSE_BRACE
3543 /* The end of a statement. */
3544 && next_token->type != CPP_SEMICOLON
3545 /* The end of the cast-expression. */
3546 && next_token->type != CPP_CLOSE_PAREN
3547 /* The end of an array bound. */
3548 && next_token->type != CPP_CLOSE_SQUARE
3549 /* The closing ">" in a template-argument-list. */
3550 && (next_token->type != CPP_GREATER
3551 || parser->greater_than_is_operator_p)
3552 /* C++0x only: A ">>" treated like two ">" tokens,
3553 in a template-argument-list. */
3554 && (next_token->type != CPP_RSHIFT
3555 || (cxx_dialect == cxx98)
3556 || parser->greater_than_is_operator_p))
3560 /* If we are within a cast, then the constraint that the
3561 cast is to an integral or enumeration type will be
3562 checked at that point. If we are not within a cast, then
3563 this code is invalid. */
3565 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3567 return token->u.value;
3573 case CPP_UTF8STRING:
3574 /* ??? Should wide strings be allowed when parser->translate_strings_p
3575 is false (i.e. in attributes)? If not, we can kill the third
3576 argument to cp_parser_string_literal. */
3577 return cp_parser_string_literal (parser,
3578 parser->translate_strings_p,
3581 case CPP_OPEN_PAREN:
3584 bool saved_greater_than_is_operator_p;
3586 /* Consume the `('. */
3587 cp_lexer_consume_token (parser->lexer);
3588 /* Within a parenthesized expression, a `>' token is always
3589 the greater-than operator. */
3590 saved_greater_than_is_operator_p
3591 = parser->greater_than_is_operator_p;
3592 parser->greater_than_is_operator_p = true;
3593 /* If we see `( { ' then we are looking at the beginning of
3594 a GNU statement-expression. */
3595 if (cp_parser_allow_gnu_extensions_p (parser)
3596 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3598 /* Statement-expressions are not allowed by the standard. */
3599 pedwarn (token->location, OPT_pedantic,
3600 "ISO C++ forbids braced-groups within expressions");
3602 /* And they're not allowed outside of a function-body; you
3603 cannot, for example, write:
3605 int i = ({ int j = 3; j + 1; });
3607 at class or namespace scope. */
3608 if (!parser->in_function_body
3609 || parser->in_template_argument_list_p)
3611 error_at (token->location,
3612 "statement-expressions are not allowed outside "
3613 "functions nor in template-argument lists");
3614 cp_parser_skip_to_end_of_block_or_statement (parser);
3615 expr = error_mark_node;
3619 /* Start the statement-expression. */
3620 expr = begin_stmt_expr ();
3621 /* Parse the compound-statement. */
3622 cp_parser_compound_statement (parser, expr, false);
3624 expr = finish_stmt_expr (expr, false);
3629 /* Parse the parenthesized expression. */
3630 expr = cp_parser_expression (parser, cast_p, idk);
3631 /* Let the front end know that this expression was
3632 enclosed in parentheses. This matters in case, for
3633 example, the expression is of the form `A::B', since
3634 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3636 finish_parenthesized_expr (expr);
3638 /* The `>' token might be the end of a template-id or
3639 template-parameter-list now. */
3640 parser->greater_than_is_operator_p
3641 = saved_greater_than_is_operator_p;
3642 /* Consume the `)'. */
3643 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3644 cp_parser_skip_to_end_of_statement (parser);
3649 case CPP_OPEN_SQUARE:
3650 if (c_dialect_objc ())
3651 /* We have an Objective-C++ message. */
3652 return cp_parser_objc_expression (parser);
3653 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3654 return cp_parser_lambda_expression (parser);
3656 case CPP_OBJC_STRING:
3657 if (c_dialect_objc ())
3658 /* We have an Objective-C++ string literal. */
3659 return cp_parser_objc_expression (parser);
3660 cp_parser_error (parser, "expected primary-expression");
3661 return error_mark_node;
3664 switch (token->keyword)
3666 /* These two are the boolean literals. */
3668 cp_lexer_consume_token (parser->lexer);
3669 return boolean_true_node;
3671 cp_lexer_consume_token (parser->lexer);
3672 return boolean_false_node;
3674 /* The `__null' literal. */
3676 cp_lexer_consume_token (parser->lexer);
3679 /* The `nullptr' literal. */
3681 cp_lexer_consume_token (parser->lexer);
3682 return nullptr_node;
3684 /* Recognize the `this' keyword. */
3686 cp_lexer_consume_token (parser->lexer);
3687 if (parser->local_variables_forbidden_p)
3689 error_at (token->location,
3690 "%<this%> may not be used in this context");
3691 return error_mark_node;
3693 /* Pointers cannot appear in constant-expressions. */
3694 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3695 return error_mark_node;
3696 return finish_this_expr ();
3698 /* The `operator' keyword can be the beginning of an
3703 case RID_FUNCTION_NAME:
3704 case RID_PRETTY_FUNCTION_NAME:
3705 case RID_C99_FUNCTION_NAME:
3707 non_integral_constant name;
3709 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3710 __func__ are the names of variables -- but they are
3711 treated specially. Therefore, they are handled here,
3712 rather than relying on the generic id-expression logic
3713 below. Grammatically, these names are id-expressions.
3715 Consume the token. */
3716 token = cp_lexer_consume_token (parser->lexer);
3718 switch (token->keyword)
3720 case RID_FUNCTION_NAME:
3721 name = NIC_FUNC_NAME;
3723 case RID_PRETTY_FUNCTION_NAME:
3724 name = NIC_PRETTY_FUNC;
3726 case RID_C99_FUNCTION_NAME:
3727 name = NIC_C99_FUNC;
3733 if (cp_parser_non_integral_constant_expression (parser, name))
3734 return error_mark_node;
3736 /* Look up the name. */
3737 return finish_fname (token->u.value);
3745 /* The `__builtin_va_arg' construct is used to handle
3746 `va_arg'. Consume the `__builtin_va_arg' token. */
3747 cp_lexer_consume_token (parser->lexer);
3748 /* Look for the opening `('. */
3749 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3750 /* Now, parse the assignment-expression. */
3751 expression = cp_parser_assignment_expression (parser,
3752 /*cast_p=*/false, NULL);
3753 /* Look for the `,'. */
3754 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3755 /* Parse the type-id. */
3756 type = cp_parser_type_id (parser);
3757 /* Look for the closing `)'. */
3758 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3759 /* Using `va_arg' in a constant-expression is not
3761 if (cp_parser_non_integral_constant_expression (parser,
3763 return error_mark_node;
3764 return build_x_va_arg (expression, type);
3768 return cp_parser_builtin_offsetof (parser);
3770 case RID_HAS_NOTHROW_ASSIGN:
3771 case RID_HAS_NOTHROW_CONSTRUCTOR:
3772 case RID_HAS_NOTHROW_COPY:
3773 case RID_HAS_TRIVIAL_ASSIGN:
3774 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3775 case RID_HAS_TRIVIAL_COPY:
3776 case RID_HAS_TRIVIAL_DESTRUCTOR:
3777 case RID_HAS_VIRTUAL_DESTRUCTOR:
3778 case RID_IS_ABSTRACT:
3779 case RID_IS_BASE_OF:
3781 case RID_IS_CONVERTIBLE_TO:
3785 case RID_IS_POLYMORPHIC:
3786 case RID_IS_STD_LAYOUT:
3787 case RID_IS_TRIVIAL:
3789 return cp_parser_trait_expr (parser, token->keyword);
3791 /* Objective-C++ expressions. */
3793 case RID_AT_PROTOCOL:
3794 case RID_AT_SELECTOR:
3795 return cp_parser_objc_expression (parser);
3798 if (parser->in_function_body
3799 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3802 error_at (token->location,
3803 "a template declaration cannot appear at block scope");
3804 cp_parser_skip_to_end_of_block_or_statement (parser);
3805 return error_mark_node;
3808 cp_parser_error (parser, "expected primary-expression");
3809 return error_mark_node;
3812 /* An id-expression can start with either an identifier, a
3813 `::' as the beginning of a qualified-id, or the "operator"
3817 case CPP_TEMPLATE_ID:
3818 case CPP_NESTED_NAME_SPECIFIER:
3822 const char *error_msg;
3825 cp_token *id_expr_token;
3828 /* Parse the id-expression. */
3830 = cp_parser_id_expression (parser,
3831 /*template_keyword_p=*/false,
3832 /*check_dependency_p=*/true,
3834 /*declarator_p=*/false,
3835 /*optional_p=*/false);
3836 if (id_expression == error_mark_node)
3837 return error_mark_node;
3838 id_expr_token = token;
3839 token = cp_lexer_peek_token (parser->lexer);
3840 done = (token->type != CPP_OPEN_SQUARE
3841 && token->type != CPP_OPEN_PAREN
3842 && token->type != CPP_DOT
3843 && token->type != CPP_DEREF
3844 && token->type != CPP_PLUS_PLUS
3845 && token->type != CPP_MINUS_MINUS);
3846 /* If we have a template-id, then no further lookup is
3847 required. If the template-id was for a template-class, we
3848 will sometimes have a TYPE_DECL at this point. */
3849 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3850 || TREE_CODE (id_expression) == TYPE_DECL)
3851 decl = id_expression;
3852 /* Look up the name. */
3855 tree ambiguous_decls;
3857 /* If we already know that this lookup is ambiguous, then
3858 we've already issued an error message; there's no reason
3860 if (id_expr_token->type == CPP_NAME
3861 && id_expr_token->ambiguous_p)
3863 cp_parser_simulate_error (parser);
3864 return error_mark_node;
3867 decl = cp_parser_lookup_name (parser, id_expression,
3870 /*is_namespace=*/false,
3871 /*check_dependency=*/true,
3873 id_expr_token->location);
3874 /* If the lookup was ambiguous, an error will already have
3876 if (ambiguous_decls)
3877 return error_mark_node;
3879 /* In Objective-C++, an instance variable (ivar) may be preferred
3880 to whatever cp_parser_lookup_name() found. */
3881 decl = objc_lookup_ivar (decl, id_expression);
3883 /* If name lookup gives us a SCOPE_REF, then the
3884 qualifying scope was dependent. */
3885 if (TREE_CODE (decl) == SCOPE_REF)
3887 /* At this point, we do not know if DECL is a valid
3888 integral constant expression. We assume that it is
3889 in fact such an expression, so that code like:
3891 template <int N> struct A {
3895 is accepted. At template-instantiation time, we
3896 will check that B<N>::i is actually a constant. */
3899 /* Check to see if DECL is a local variable in a context
3900 where that is forbidden. */
3901 if (parser->local_variables_forbidden_p
3902 && local_variable_p (decl))
3904 /* It might be that we only found DECL because we are
3905 trying to be generous with pre-ISO scoping rules.
3906 For example, consider:
3910 for (int i = 0; i < 10; ++i) {}
3911 extern void f(int j = i);
3914 Here, name look up will originally find the out
3915 of scope `i'. We need to issue a warning message,
3916 but then use the global `i'. */
3917 decl = check_for_out_of_scope_variable (decl);
3918 if (local_variable_p (decl))
3920 error_at (id_expr_token->location,
3921 "local variable %qD may not appear in this context",
3923 return error_mark_node;
3928 decl = (finish_id_expression
3929 (id_expression, decl, parser->scope,
3931 parser->integral_constant_expression_p,
3932 parser->allow_non_integral_constant_expression_p,
3933 &parser->non_integral_constant_expression_p,
3934 template_p, done, address_p,
3937 id_expr_token->location));
3939 cp_parser_error (parser, error_msg);
3943 /* Anything else is an error. */
3945 cp_parser_error (parser, "expected primary-expression");
3946 return error_mark_node;
3950 /* Parse an id-expression.
3957 :: [opt] nested-name-specifier template [opt] unqualified-id
3959 :: operator-function-id
3962 Return a representation of the unqualified portion of the
3963 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3964 a `::' or nested-name-specifier.
3966 Often, if the id-expression was a qualified-id, the caller will
3967 want to make a SCOPE_REF to represent the qualified-id. This
3968 function does not do this in order to avoid wastefully creating
3969 SCOPE_REFs when they are not required.
3971 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3974 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3975 uninstantiated templates.
3977 If *TEMPLATE_P is non-NULL, it is set to true iff the
3978 `template' keyword is used to explicitly indicate that the entity
3979 named is a template.
3981 If DECLARATOR_P is true, the id-expression is appearing as part of
3982 a declarator, rather than as part of an expression. */
3985 cp_parser_id_expression (cp_parser *parser,
3986 bool template_keyword_p,
3987 bool check_dependency_p,
3992 bool global_scope_p;
3993 bool nested_name_specifier_p;
3995 /* Assume the `template' keyword was not used. */
3997 *template_p = template_keyword_p;
3999 /* Look for the optional `::' operator. */
4001 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4003 /* Look for the optional nested-name-specifier. */
4004 nested_name_specifier_p
4005 = (cp_parser_nested_name_specifier_opt (parser,
4006 /*typename_keyword_p=*/false,
4011 /* If there is a nested-name-specifier, then we are looking at
4012 the first qualified-id production. */
4013 if (nested_name_specifier_p)
4016 tree saved_object_scope;
4017 tree saved_qualifying_scope;
4018 tree unqualified_id;
4021 /* See if the next token is the `template' keyword. */
4023 template_p = &is_template;
4024 *template_p = cp_parser_optional_template_keyword (parser);
4025 /* Name lookup we do during the processing of the
4026 unqualified-id might obliterate SCOPE. */
4027 saved_scope = parser->scope;
4028 saved_object_scope = parser->object_scope;
4029 saved_qualifying_scope = parser->qualifying_scope;
4030 /* Process the final unqualified-id. */
4031 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4034 /*optional_p=*/false);
4035 /* Restore the SAVED_SCOPE for our caller. */
4036 parser->scope = saved_scope;
4037 parser->object_scope = saved_object_scope;
4038 parser->qualifying_scope = saved_qualifying_scope;
4040 return unqualified_id;
4042 /* Otherwise, if we are in global scope, then we are looking at one
4043 of the other qualified-id productions. */
4044 else if (global_scope_p)
4049 /* Peek at the next token. */
4050 token = cp_lexer_peek_token (parser->lexer);
4052 /* If it's an identifier, and the next token is not a "<", then
4053 we can avoid the template-id case. This is an optimization
4054 for this common case. */
4055 if (token->type == CPP_NAME
4056 && !cp_parser_nth_token_starts_template_argument_list_p
4058 return cp_parser_identifier (parser);
4060 cp_parser_parse_tentatively (parser);
4061 /* Try a template-id. */
4062 id = cp_parser_template_id (parser,
4063 /*template_keyword_p=*/false,
4064 /*check_dependency_p=*/true,
4066 /* If that worked, we're done. */
4067 if (cp_parser_parse_definitely (parser))
4070 /* Peek at the next token. (Changes in the token buffer may
4071 have invalidated the pointer obtained above.) */
4072 token = cp_lexer_peek_token (parser->lexer);
4074 switch (token->type)
4077 return cp_parser_identifier (parser);
4080 if (token->keyword == RID_OPERATOR)
4081 return cp_parser_operator_function_id (parser);
4085 cp_parser_error (parser, "expected id-expression");
4086 return error_mark_node;
4090 return cp_parser_unqualified_id (parser, template_keyword_p,
4091 /*check_dependency_p=*/true,
4096 /* Parse an unqualified-id.
4100 operator-function-id
4101 conversion-function-id
4105 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4106 keyword, in a construct like `A::template ...'.
4108 Returns a representation of unqualified-id. For the `identifier'
4109 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4110 production a BIT_NOT_EXPR is returned; the operand of the
4111 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4112 other productions, see the documentation accompanying the
4113 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4114 names are looked up in uninstantiated templates. If DECLARATOR_P
4115 is true, the unqualified-id is appearing as part of a declarator,
4116 rather than as part of an expression. */
4119 cp_parser_unqualified_id (cp_parser* parser,
4120 bool template_keyword_p,
4121 bool check_dependency_p,
4127 /* Peek at the next token. */
4128 token = cp_lexer_peek_token (parser->lexer);
4130 switch (token->type)
4136 /* We don't know yet whether or not this will be a
4138 cp_parser_parse_tentatively (parser);
4139 /* Try a template-id. */
4140 id = cp_parser_template_id (parser, template_keyword_p,
4143 /* If it worked, we're done. */
4144 if (cp_parser_parse_definitely (parser))
4146 /* Otherwise, it's an ordinary identifier. */
4147 return cp_parser_identifier (parser);
4150 case CPP_TEMPLATE_ID:
4151 return cp_parser_template_id (parser, template_keyword_p,
4158 tree qualifying_scope;
4163 /* Consume the `~' token. */
4164 cp_lexer_consume_token (parser->lexer);
4165 /* Parse the class-name. The standard, as written, seems to
4168 template <typename T> struct S { ~S (); };
4169 template <typename T> S<T>::~S() {}
4171 is invalid, since `~' must be followed by a class-name, but
4172 `S<T>' is dependent, and so not known to be a class.
4173 That's not right; we need to look in uninstantiated
4174 templates. A further complication arises from:
4176 template <typename T> void f(T t) {
4180 Here, it is not possible to look up `T' in the scope of `T'
4181 itself. We must look in both the current scope, and the
4182 scope of the containing complete expression.
4184 Yet another issue is:
4193 The standard does not seem to say that the `S' in `~S'
4194 should refer to the type `S' and not the data member
4197 /* DR 244 says that we look up the name after the "~" in the
4198 same scope as we looked up the qualifying name. That idea
4199 isn't fully worked out; it's more complicated than that. */
4200 scope = parser->scope;
4201 object_scope = parser->object_scope;
4202 qualifying_scope = parser->qualifying_scope;
4204 /* Check for invalid scopes. */
4205 if (scope == error_mark_node)
4207 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4208 cp_lexer_consume_token (parser->lexer);
4209 return error_mark_node;
4211 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4213 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4214 error_at (token->location,
4215 "scope %qT before %<~%> is not a class-name",
4217 cp_parser_simulate_error (parser);
4218 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4219 cp_lexer_consume_token (parser->lexer);
4220 return error_mark_node;
4222 gcc_assert (!scope || TYPE_P (scope));
4224 /* If the name is of the form "X::~X" it's OK even if X is a
4226 token = cp_lexer_peek_token (parser->lexer);
4228 && token->type == CPP_NAME
4229 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4231 && (token->u.value == TYPE_IDENTIFIER (scope)
4232 || constructor_name_p (token->u.value, scope)))
4234 cp_lexer_consume_token (parser->lexer);
4235 return build_nt (BIT_NOT_EXPR, scope);
4238 /* If there was an explicit qualification (S::~T), first look
4239 in the scope given by the qualification (i.e., S).
4241 Note: in the calls to cp_parser_class_name below we pass
4242 typename_type so that lookup finds the injected-class-name
4243 rather than the constructor. */
4245 type_decl = NULL_TREE;
4248 cp_parser_parse_tentatively (parser);
4249 type_decl = cp_parser_class_name (parser,
4250 /*typename_keyword_p=*/false,
4251 /*template_keyword_p=*/false,
4253 /*check_dependency=*/false,
4254 /*class_head_p=*/false,
4256 if (cp_parser_parse_definitely (parser))
4259 /* In "N::S::~S", look in "N" as well. */
4260 if (!done && scope && qualifying_scope)
4262 cp_parser_parse_tentatively (parser);
4263 parser->scope = qualifying_scope;
4264 parser->object_scope = NULL_TREE;
4265 parser->qualifying_scope = NULL_TREE;
4267 = cp_parser_class_name (parser,
4268 /*typename_keyword_p=*/false,
4269 /*template_keyword_p=*/false,
4271 /*check_dependency=*/false,
4272 /*class_head_p=*/false,
4274 if (cp_parser_parse_definitely (parser))
4277 /* In "p->S::~T", look in the scope given by "*p" as well. */
4278 else if (!done && object_scope)
4280 cp_parser_parse_tentatively (parser);
4281 parser->scope = object_scope;
4282 parser->object_scope = NULL_TREE;
4283 parser->qualifying_scope = NULL_TREE;
4285 = cp_parser_class_name (parser,
4286 /*typename_keyword_p=*/false,
4287 /*template_keyword_p=*/false,
4289 /*check_dependency=*/false,
4290 /*class_head_p=*/false,
4292 if (cp_parser_parse_definitely (parser))
4295 /* Look in the surrounding context. */
4298 parser->scope = NULL_TREE;
4299 parser->object_scope = NULL_TREE;
4300 parser->qualifying_scope = NULL_TREE;
4301 if (processing_template_decl)
4302 cp_parser_parse_tentatively (parser);
4304 = cp_parser_class_name (parser,
4305 /*typename_keyword_p=*/false,
4306 /*template_keyword_p=*/false,
4308 /*check_dependency=*/false,
4309 /*class_head_p=*/false,
4311 if (processing_template_decl
4312 && ! cp_parser_parse_definitely (parser))
4314 /* We couldn't find a type with this name, so just accept
4315 it and check for a match at instantiation time. */
4316 type_decl = cp_parser_identifier (parser);
4317 if (type_decl != error_mark_node)
4318 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4322 /* If an error occurred, assume that the name of the
4323 destructor is the same as the name of the qualifying
4324 class. That allows us to keep parsing after running
4325 into ill-formed destructor names. */
4326 if (type_decl == error_mark_node && scope)
4327 return build_nt (BIT_NOT_EXPR, scope);
4328 else if (type_decl == error_mark_node)
4329 return error_mark_node;
4331 /* Check that destructor name and scope match. */
4332 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4334 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4335 error_at (token->location,
4336 "declaration of %<~%T%> as member of %qT",
4338 cp_parser_simulate_error (parser);
4339 return error_mark_node;
4344 A typedef-name that names a class shall not be used as the
4345 identifier in the declarator for a destructor declaration. */
4347 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4348 && !DECL_SELF_REFERENCE_P (type_decl)
4349 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4350 error_at (token->location,
4351 "typedef-name %qD used as destructor declarator",
4354 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4358 if (token->keyword == RID_OPERATOR)
4362 /* This could be a template-id, so we try that first. */
4363 cp_parser_parse_tentatively (parser);
4364 /* Try a template-id. */
4365 id = cp_parser_template_id (parser, template_keyword_p,
4366 /*check_dependency_p=*/true,
4368 /* If that worked, we're done. */
4369 if (cp_parser_parse_definitely (parser))
4371 /* We still don't know whether we're looking at an
4372 operator-function-id or a conversion-function-id. */
4373 cp_parser_parse_tentatively (parser);
4374 /* Try an operator-function-id. */
4375 id = cp_parser_operator_function_id (parser);
4376 /* If that didn't work, try a conversion-function-id. */
4377 if (!cp_parser_parse_definitely (parser))
4378 id = cp_parser_conversion_function_id (parser);
4387 cp_parser_error (parser, "expected unqualified-id");
4388 return error_mark_node;
4392 /* Parse an (optional) nested-name-specifier.
4394 nested-name-specifier: [C++98]
4395 class-or-namespace-name :: nested-name-specifier [opt]
4396 class-or-namespace-name :: template nested-name-specifier [opt]
4398 nested-name-specifier: [C++0x]
4401 nested-name-specifier identifier ::
4402 nested-name-specifier template [opt] simple-template-id ::
4404 PARSER->SCOPE should be set appropriately before this function is
4405 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4406 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4409 Sets PARSER->SCOPE to the class (TYPE) or namespace
4410 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4411 it unchanged if there is no nested-name-specifier. Returns the new
4412 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4414 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4415 part of a declaration and/or decl-specifier. */
4418 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4419 bool typename_keyword_p,
4420 bool check_dependency_p,
4422 bool is_declaration)
4424 bool success = false;
4425 cp_token_position start = 0;
4428 /* Remember where the nested-name-specifier starts. */
4429 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4431 start = cp_lexer_token_position (parser->lexer, false);
4432 push_deferring_access_checks (dk_deferred);
4439 tree saved_qualifying_scope;
4440 bool template_keyword_p;
4442 /* Spot cases that cannot be the beginning of a
4443 nested-name-specifier. */
4444 token = cp_lexer_peek_token (parser->lexer);
4446 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4447 the already parsed nested-name-specifier. */
4448 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4450 /* Grab the nested-name-specifier and continue the loop. */
4451 cp_parser_pre_parsed_nested_name_specifier (parser);
4452 /* If we originally encountered this nested-name-specifier
4453 with IS_DECLARATION set to false, we will not have
4454 resolved TYPENAME_TYPEs, so we must do so here. */
4456 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4458 new_scope = resolve_typename_type (parser->scope,
4459 /*only_current_p=*/false);
4460 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4461 parser->scope = new_scope;
4467 /* Spot cases that cannot be the beginning of a
4468 nested-name-specifier. On the second and subsequent times
4469 through the loop, we look for the `template' keyword. */
4470 if (success && token->keyword == RID_TEMPLATE)
4472 /* A template-id can start a nested-name-specifier. */
4473 else if (token->type == CPP_TEMPLATE_ID)
4477 /* If the next token is not an identifier, then it is
4478 definitely not a type-name or namespace-name. */
4479 if (token->type != CPP_NAME)
4481 /* If the following token is neither a `<' (to begin a
4482 template-id), nor a `::', then we are not looking at a
4483 nested-name-specifier. */
4484 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4485 if (token->type != CPP_SCOPE
4486 && !cp_parser_nth_token_starts_template_argument_list_p
4491 /* The nested-name-specifier is optional, so we parse
4493 cp_parser_parse_tentatively (parser);
4495 /* Look for the optional `template' keyword, if this isn't the
4496 first time through the loop. */
4498 template_keyword_p = cp_parser_optional_template_keyword (parser);
4500 template_keyword_p = false;
4502 /* Save the old scope since the name lookup we are about to do
4503 might destroy it. */
4504 old_scope = parser->scope;
4505 saved_qualifying_scope = parser->qualifying_scope;
4506 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4507 look up names in "X<T>::I" in order to determine that "Y" is
4508 a template. So, if we have a typename at this point, we make
4509 an effort to look through it. */
4511 && !typename_keyword_p
4513 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4514 parser->scope = resolve_typename_type (parser->scope,
4515 /*only_current_p=*/false);
4516 /* Parse the qualifying entity. */
4518 = cp_parser_qualifying_entity (parser,
4524 /* Look for the `::' token. */
4525 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4527 /* If we found what we wanted, we keep going; otherwise, we're
4529 if (!cp_parser_parse_definitely (parser))
4531 bool error_p = false;
4533 /* Restore the OLD_SCOPE since it was valid before the
4534 failed attempt at finding the last
4535 class-or-namespace-name. */
4536 parser->scope = old_scope;
4537 parser->qualifying_scope = saved_qualifying_scope;
4538 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4540 /* If the next token is an identifier, and the one after
4541 that is a `::', then any valid interpretation would have
4542 found a class-or-namespace-name. */
4543 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4544 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4546 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4549 token = cp_lexer_consume_token (parser->lexer);
4552 if (!token->ambiguous_p)
4555 tree ambiguous_decls;
4557 decl = cp_parser_lookup_name (parser, token->u.value,
4559 /*is_template=*/false,
4560 /*is_namespace=*/false,
4561 /*check_dependency=*/true,
4564 if (TREE_CODE (decl) == TEMPLATE_DECL)
4565 error_at (token->location,
4566 "%qD used without template parameters",
4568 else if (ambiguous_decls)
4570 error_at (token->location,
4571 "reference to %qD is ambiguous",
4573 print_candidates (ambiguous_decls);
4574 decl = error_mark_node;
4578 if (cxx_dialect != cxx98)
4579 cp_parser_name_lookup_error
4580 (parser, token->u.value, decl, NLE_NOT_CXX98,
4583 cp_parser_name_lookup_error
4584 (parser, token->u.value, decl, NLE_CXX98,
4588 parser->scope = error_mark_node;
4590 /* Treat this as a successful nested-name-specifier
4595 If the name found is not a class-name (clause
4596 _class_) or namespace-name (_namespace.def_), the
4597 program is ill-formed. */
4600 cp_lexer_consume_token (parser->lexer);
4604 /* We've found one valid nested-name-specifier. */
4606 /* Name lookup always gives us a DECL. */
4607 if (TREE_CODE (new_scope) == TYPE_DECL)
4608 new_scope = TREE_TYPE (new_scope);
4609 /* Uses of "template" must be followed by actual templates. */
4610 if (template_keyword_p
4611 && !(CLASS_TYPE_P (new_scope)
4612 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4613 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4614 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4615 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4616 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4617 == TEMPLATE_ID_EXPR)))
4618 permerror (input_location, TYPE_P (new_scope)
4619 ? "%qT is not a template"
4620 : "%qD is not a template",
4622 /* If it is a class scope, try to complete it; we are about to
4623 be looking up names inside the class. */
4624 if (TYPE_P (new_scope)
4625 /* Since checking types for dependency can be expensive,
4626 avoid doing it if the type is already complete. */
4627 && !COMPLETE_TYPE_P (new_scope)
4628 /* Do not try to complete dependent types. */
4629 && !dependent_type_p (new_scope))
4631 new_scope = complete_type (new_scope);
4632 /* If it is a typedef to current class, use the current
4633 class instead, as the typedef won't have any names inside
4635 if (!COMPLETE_TYPE_P (new_scope)
4636 && currently_open_class (new_scope))
4637 new_scope = TYPE_MAIN_VARIANT (new_scope);
4639 /* Make sure we look in the right scope the next time through
4641 parser->scope = new_scope;
4644 /* If parsing tentatively, replace the sequence of tokens that makes
4645 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4646 token. That way, should we re-parse the token stream, we will
4647 not have to repeat the effort required to do the parse, nor will
4648 we issue duplicate error messages. */
4649 if (success && start)
4653 token = cp_lexer_token_at (parser->lexer, start);
4654 /* Reset the contents of the START token. */
4655 token->type = CPP_NESTED_NAME_SPECIFIER;
4656 /* Retrieve any deferred checks. Do not pop this access checks yet
4657 so the memory will not be reclaimed during token replacing below. */
4658 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4659 token->u.tree_check_value->value = parser->scope;
4660 token->u.tree_check_value->checks = get_deferred_access_checks ();
4661 token->u.tree_check_value->qualifying_scope =
4662 parser->qualifying_scope;
4663 token->keyword = RID_MAX;
4665 /* Purge all subsequent tokens. */
4666 cp_lexer_purge_tokens_after (parser->lexer, start);
4670 pop_to_parent_deferring_access_checks ();
4672 return success ? parser->scope : NULL_TREE;
4675 /* Parse a nested-name-specifier. See
4676 cp_parser_nested_name_specifier_opt for details. This function
4677 behaves identically, except that it will an issue an error if no
4678 nested-name-specifier is present. */
4681 cp_parser_nested_name_specifier (cp_parser *parser,
4682 bool typename_keyword_p,
4683 bool check_dependency_p,
4685 bool is_declaration)
4689 /* Look for the nested-name-specifier. */
4690 scope = cp_parser_nested_name_specifier_opt (parser,
4695 /* If it was not present, issue an error message. */
4698 cp_parser_error (parser, "expected nested-name-specifier");
4699 parser->scope = NULL_TREE;
4705 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4706 this is either a class-name or a namespace-name (which corresponds
4707 to the class-or-namespace-name production in the grammar). For
4708 C++0x, it can also be a type-name that refers to an enumeration
4711 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4712 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4713 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4714 TYPE_P is TRUE iff the next name should be taken as a class-name,
4715 even the same name is declared to be another entity in the same
4718 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4719 specified by the class-or-namespace-name. If neither is found the
4720 ERROR_MARK_NODE is returned. */
4723 cp_parser_qualifying_entity (cp_parser *parser,
4724 bool typename_keyword_p,
4725 bool template_keyword_p,
4726 bool check_dependency_p,
4728 bool is_declaration)
4731 tree saved_qualifying_scope;
4732 tree saved_object_scope;
4735 bool successful_parse_p;
4737 /* Before we try to parse the class-name, we must save away the
4738 current PARSER->SCOPE since cp_parser_class_name will destroy
4740 saved_scope = parser->scope;
4741 saved_qualifying_scope = parser->qualifying_scope;
4742 saved_object_scope = parser->object_scope;
4743 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4744 there is no need to look for a namespace-name. */
4745 only_class_p = template_keyword_p
4746 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4748 cp_parser_parse_tentatively (parser);
4749 scope = cp_parser_class_name (parser,
4752 type_p ? class_type : none_type,
4754 /*class_head_p=*/false,
4756 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4757 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4759 && cxx_dialect != cxx98
4760 && !successful_parse_p)
4762 /* Restore the saved scope. */
4763 parser->scope = saved_scope;
4764 parser->qualifying_scope = saved_qualifying_scope;
4765 parser->object_scope = saved_object_scope;
4767 /* Parse tentatively. */
4768 cp_parser_parse_tentatively (parser);
4770 /* Parse a typedef-name or enum-name. */
4771 scope = cp_parser_nonclass_name (parser);
4773 /* "If the name found does not designate a namespace or a class,
4774 enumeration, or dependent type, the program is ill-formed."
4776 We cover classes and dependent types above and namespaces below,
4777 so this code is only looking for enums. */
4778 if (!scope || TREE_CODE (scope) != TYPE_DECL
4779 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4780 cp_parser_simulate_error (parser);
4782 successful_parse_p = cp_parser_parse_definitely (parser);
4784 /* If that didn't work, try for a namespace-name. */
4785 if (!only_class_p && !successful_parse_p)
4787 /* Restore the saved scope. */
4788 parser->scope = saved_scope;
4789 parser->qualifying_scope = saved_qualifying_scope;
4790 parser->object_scope = saved_object_scope;
4791 /* If we are not looking at an identifier followed by the scope
4792 resolution operator, then this is not part of a
4793 nested-name-specifier. (Note that this function is only used
4794 to parse the components of a nested-name-specifier.) */
4795 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4796 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4797 return error_mark_node;
4798 scope = cp_parser_namespace_name (parser);
4804 /* Parse a postfix-expression.
4808 postfix-expression [ expression ]
4809 postfix-expression ( expression-list [opt] )
4810 simple-type-specifier ( expression-list [opt] )
4811 typename :: [opt] nested-name-specifier identifier
4812 ( expression-list [opt] )
4813 typename :: [opt] nested-name-specifier template [opt] template-id
4814 ( expression-list [opt] )
4815 postfix-expression . template [opt] id-expression
4816 postfix-expression -> template [opt] id-expression
4817 postfix-expression . pseudo-destructor-name
4818 postfix-expression -> pseudo-destructor-name
4819 postfix-expression ++
4820 postfix-expression --
4821 dynamic_cast < type-id > ( expression )
4822 static_cast < type-id > ( expression )
4823 reinterpret_cast < type-id > ( expression )
4824 const_cast < type-id > ( expression )
4825 typeid ( expression )
4831 ( type-id ) { initializer-list , [opt] }
4833 This extension is a GNU version of the C99 compound-literal
4834 construct. (The C99 grammar uses `type-name' instead of `type-id',
4835 but they are essentially the same concept.)
4837 If ADDRESS_P is true, the postfix expression is the operand of the
4838 `&' operator. CAST_P is true if this expression is the target of a
4841 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4842 class member access expressions [expr.ref].
4844 Returns a representation of the expression. */
4847 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4848 bool member_access_only_p,
4849 cp_id_kind * pidk_return)
4853 cp_id_kind idk = CP_ID_KIND_NONE;
4854 tree postfix_expression = NULL_TREE;
4855 bool is_member_access = false;
4857 /* Peek at the next token. */
4858 token = cp_lexer_peek_token (parser->lexer);
4859 /* Some of the productions are determined by keywords. */
4860 keyword = token->keyword;
4870 const char *saved_message;
4872 /* All of these can be handled in the same way from the point
4873 of view of parsing. Begin by consuming the token
4874 identifying the cast. */
4875 cp_lexer_consume_token (parser->lexer);
4877 /* New types cannot be defined in the cast. */
4878 saved_message = parser->type_definition_forbidden_message;
4879 parser->type_definition_forbidden_message
4880 = G_("types may not be defined in casts");
4882 /* Look for the opening `<'. */
4883 cp_parser_require (parser, CPP_LESS, RT_LESS);
4884 /* Parse the type to which we are casting. */
4885 type = cp_parser_type_id (parser);
4886 /* Look for the closing `>'. */
4887 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4888 /* Restore the old message. */
4889 parser->type_definition_forbidden_message = saved_message;
4891 /* And the expression which is being cast. */
4892 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4893 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4894 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4896 /* Only type conversions to integral or enumeration types
4897 can be used in constant-expressions. */
4898 if (!cast_valid_in_integral_constant_expression_p (type)
4899 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4900 return error_mark_node;
4906 = build_dynamic_cast (type, expression, tf_warning_or_error);
4910 = build_static_cast (type, expression, tf_warning_or_error);
4914 = build_reinterpret_cast (type, expression,
4915 tf_warning_or_error);
4919 = build_const_cast (type, expression, tf_warning_or_error);
4930 const char *saved_message;
4931 bool saved_in_type_id_in_expr_p;
4933 /* Consume the `typeid' token. */
4934 cp_lexer_consume_token (parser->lexer);
4935 /* Look for the `(' token. */
4936 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4937 /* Types cannot be defined in a `typeid' expression. */
4938 saved_message = parser->type_definition_forbidden_message;
4939 parser->type_definition_forbidden_message
4940 = G_("types may not be defined in a %<typeid%> expression");
4941 /* We can't be sure yet whether we're looking at a type-id or an
4943 cp_parser_parse_tentatively (parser);
4944 /* Try a type-id first. */
4945 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4946 parser->in_type_id_in_expr_p = true;
4947 type = cp_parser_type_id (parser);
4948 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4949 /* Look for the `)' token. Otherwise, we can't be sure that
4950 we're not looking at an expression: consider `typeid (int
4951 (3))', for example. */
4952 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4953 /* If all went well, simply lookup the type-id. */
4954 if (cp_parser_parse_definitely (parser))
4955 postfix_expression = get_typeid (type);
4956 /* Otherwise, fall back to the expression variant. */
4961 /* Look for an expression. */
4962 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4963 /* Compute its typeid. */
4964 postfix_expression = build_typeid (expression);
4965 /* Look for the `)' token. */
4966 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4968 /* Restore the saved message. */
4969 parser->type_definition_forbidden_message = saved_message;
4970 /* `typeid' may not appear in an integral constant expression. */
4971 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4972 return error_mark_node;
4979 /* The syntax permitted here is the same permitted for an
4980 elaborated-type-specifier. */
4981 type = cp_parser_elaborated_type_specifier (parser,
4982 /*is_friend=*/false,
4983 /*is_declaration=*/false);
4984 postfix_expression = cp_parser_functional_cast (parser, type);
4992 /* If the next thing is a simple-type-specifier, we may be
4993 looking at a functional cast. We could also be looking at
4994 an id-expression. So, we try the functional cast, and if
4995 that doesn't work we fall back to the primary-expression. */
4996 cp_parser_parse_tentatively (parser);
4997 /* Look for the simple-type-specifier. */
4998 type = cp_parser_simple_type_specifier (parser,
4999 /*decl_specs=*/NULL,
5000 CP_PARSER_FLAGS_NONE);
5001 /* Parse the cast itself. */
5002 if (!cp_parser_error_occurred (parser))
5004 = cp_parser_functional_cast (parser, type);
5005 /* If that worked, we're done. */
5006 if (cp_parser_parse_definitely (parser))
5009 /* If the functional-cast didn't work out, try a
5010 compound-literal. */
5011 if (cp_parser_allow_gnu_extensions_p (parser)
5012 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5014 VEC(constructor_elt,gc) *initializer_list = NULL;
5015 bool saved_in_type_id_in_expr_p;
5017 cp_parser_parse_tentatively (parser);
5018 /* Consume the `('. */
5019 cp_lexer_consume_token (parser->lexer);
5020 /* Parse the type. */
5021 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5022 parser->in_type_id_in_expr_p = true;
5023 type = cp_parser_type_id (parser);
5024 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5025 /* Look for the `)'. */
5026 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5027 /* Look for the `{'. */
5028 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5029 /* If things aren't going well, there's no need to
5031 if (!cp_parser_error_occurred (parser))
5033 bool non_constant_p;
5034 /* Parse the initializer-list. */
5036 = cp_parser_initializer_list (parser, &non_constant_p);
5037 /* Allow a trailing `,'. */
5038 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5039 cp_lexer_consume_token (parser->lexer);
5040 /* Look for the final `}'. */
5041 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5043 /* If that worked, we're definitely looking at a
5044 compound-literal expression. */
5045 if (cp_parser_parse_definitely (parser))
5047 /* Warn the user that a compound literal is not
5048 allowed in standard C++. */
5049 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5050 /* For simplicity, we disallow compound literals in
5051 constant-expressions. We could
5052 allow compound literals of integer type, whose
5053 initializer was a constant, in constant
5054 expressions. Permitting that usage, as a further
5055 extension, would not change the meaning of any
5056 currently accepted programs. (Of course, as
5057 compound literals are not part of ISO C++, the
5058 standard has nothing to say.) */
5059 if (cp_parser_non_integral_constant_expression (parser,
5062 postfix_expression = error_mark_node;
5065 /* Form the representation of the compound-literal. */
5067 = (finish_compound_literal
5068 (type, build_constructor (init_list_type_node,
5069 initializer_list)));
5074 /* It must be a primary-expression. */
5076 = cp_parser_primary_expression (parser, address_p, cast_p,
5077 /*template_arg_p=*/false,
5083 /* Keep looping until the postfix-expression is complete. */
5086 if (idk == CP_ID_KIND_UNQUALIFIED
5087 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5088 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5089 /* It is not a Koenig lookup function call. */
5091 = unqualified_name_lookup_error (postfix_expression);
5093 /* Peek at the next token. */
5094 token = cp_lexer_peek_token (parser->lexer);
5096 switch (token->type)
5098 case CPP_OPEN_SQUARE:
5100 = cp_parser_postfix_open_square_expression (parser,
5103 idk = CP_ID_KIND_NONE;
5104 is_member_access = false;
5107 case CPP_OPEN_PAREN:
5108 /* postfix-expression ( expression-list [opt] ) */
5111 bool is_builtin_constant_p;
5112 bool saved_integral_constant_expression_p = false;
5113 bool saved_non_integral_constant_expression_p = false;
5116 is_member_access = false;
5118 is_builtin_constant_p
5119 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5120 if (is_builtin_constant_p)
5122 /* The whole point of __builtin_constant_p is to allow
5123 non-constant expressions to appear as arguments. */
5124 saved_integral_constant_expression_p
5125 = parser->integral_constant_expression_p;
5126 saved_non_integral_constant_expression_p
5127 = parser->non_integral_constant_expression_p;
5128 parser->integral_constant_expression_p = false;
5130 args = (cp_parser_parenthesized_expression_list
5132 /*cast_p=*/false, /*allow_expansion_p=*/true,
5133 /*non_constant_p=*/NULL));
5134 if (is_builtin_constant_p)
5136 parser->integral_constant_expression_p
5137 = saved_integral_constant_expression_p;
5138 parser->non_integral_constant_expression_p
5139 = saved_non_integral_constant_expression_p;
5144 postfix_expression = error_mark_node;
5148 /* Function calls are not permitted in
5149 constant-expressions. */
5150 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5151 && cp_parser_non_integral_constant_expression (parser,
5154 postfix_expression = error_mark_node;
5155 release_tree_vector (args);
5160 if (idk == CP_ID_KIND_UNQUALIFIED
5161 || idk == CP_ID_KIND_TEMPLATE_ID)
5163 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5165 if (!VEC_empty (tree, args))
5168 if (!any_type_dependent_arguments_p (args))
5170 = perform_koenig_lookup (postfix_expression, args);
5174 = unqualified_fn_lookup_error (postfix_expression);
5176 /* We do not perform argument-dependent lookup if
5177 normal lookup finds a non-function, in accordance
5178 with the expected resolution of DR 218. */
5179 else if (!VEC_empty (tree, args)
5180 && is_overloaded_fn (postfix_expression))
5182 tree fn = get_first_fn (postfix_expression);
5183 fn = STRIP_TEMPLATE (fn);
5185 /* Do not do argument dependent lookup if regular
5186 lookup finds a member function or a block-scope
5187 function declaration. [basic.lookup.argdep]/3 */
5188 if (!DECL_FUNCTION_MEMBER_P (fn)
5189 && !DECL_LOCAL_FUNCTION_P (fn))
5192 if (!any_type_dependent_arguments_p (args))
5194 = perform_koenig_lookup (postfix_expression, args);
5199 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5201 tree instance = TREE_OPERAND (postfix_expression, 0);
5202 tree fn = TREE_OPERAND (postfix_expression, 1);
5204 if (processing_template_decl
5205 && (type_dependent_expression_p (instance)
5206 || (!BASELINK_P (fn)
5207 && TREE_CODE (fn) != FIELD_DECL)
5208 || type_dependent_expression_p (fn)
5209 || any_type_dependent_arguments_p (args)))
5212 = build_nt_call_vec (postfix_expression, args);
5213 release_tree_vector (args);
5217 if (BASELINK_P (fn))
5220 = (build_new_method_call
5221 (instance, fn, &args, NULL_TREE,
5222 (idk == CP_ID_KIND_QUALIFIED
5223 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5225 tf_warning_or_error));
5229 = finish_call_expr (postfix_expression, &args,
5230 /*disallow_virtual=*/false,
5232 tf_warning_or_error);
5234 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5235 || TREE_CODE (postfix_expression) == MEMBER_REF
5236 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5237 postfix_expression = (build_offset_ref_call_from_tree
5238 (postfix_expression, &args));
5239 else if (idk == CP_ID_KIND_QUALIFIED)
5240 /* A call to a static class member, or a namespace-scope
5243 = finish_call_expr (postfix_expression, &args,
5244 /*disallow_virtual=*/true,
5246 tf_warning_or_error);
5248 /* All other function calls. */
5250 = finish_call_expr (postfix_expression, &args,
5251 /*disallow_virtual=*/false,
5253 tf_warning_or_error);
5255 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5256 idk = CP_ID_KIND_NONE;
5258 release_tree_vector (args);
5264 /* postfix-expression . template [opt] id-expression
5265 postfix-expression . pseudo-destructor-name
5266 postfix-expression -> template [opt] id-expression
5267 postfix-expression -> pseudo-destructor-name */
5269 /* Consume the `.' or `->' operator. */
5270 cp_lexer_consume_token (parser->lexer);
5273 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5278 is_member_access = true;
5282 /* postfix-expression ++ */
5283 /* Consume the `++' token. */
5284 cp_lexer_consume_token (parser->lexer);
5285 /* Generate a representation for the complete expression. */
5287 = finish_increment_expr (postfix_expression,
5288 POSTINCREMENT_EXPR);
5289 /* Increments may not appear in constant-expressions. */
5290 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5291 postfix_expression = error_mark_node;
5292 idk = CP_ID_KIND_NONE;
5293 is_member_access = false;
5296 case CPP_MINUS_MINUS:
5297 /* postfix-expression -- */
5298 /* Consume the `--' token. */
5299 cp_lexer_consume_token (parser->lexer);
5300 /* Generate a representation for the complete expression. */
5302 = finish_increment_expr (postfix_expression,
5303 POSTDECREMENT_EXPR);
5304 /* Decrements may not appear in constant-expressions. */
5305 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5306 postfix_expression = error_mark_node;
5307 idk = CP_ID_KIND_NONE;
5308 is_member_access = false;
5312 if (pidk_return != NULL)
5313 * pidk_return = idk;
5314 if (member_access_only_p)
5315 return is_member_access? postfix_expression : error_mark_node;
5317 return postfix_expression;
5321 /* We should never get here. */
5323 return error_mark_node;
5326 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5327 by cp_parser_builtin_offsetof. We're looking for
5329 postfix-expression [ expression ]
5331 FOR_OFFSETOF is set if we're being called in that context, which
5332 changes how we deal with integer constant expressions. */
5335 cp_parser_postfix_open_square_expression (cp_parser *parser,
5336 tree postfix_expression,
5341 /* Consume the `[' token. */
5342 cp_lexer_consume_token (parser->lexer);
5344 /* Parse the index expression. */
5345 /* ??? For offsetof, there is a question of what to allow here. If
5346 offsetof is not being used in an integral constant expression context,
5347 then we *could* get the right answer by computing the value at runtime.
5348 If we are in an integral constant expression context, then we might
5349 could accept any constant expression; hard to say without analysis.
5350 Rather than open the barn door too wide right away, allow only integer
5351 constant expressions here. */
5353 index = cp_parser_constant_expression (parser, false, NULL);
5355 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5357 /* Look for the closing `]'. */
5358 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5360 /* Build the ARRAY_REF. */
5361 postfix_expression = grok_array_decl (postfix_expression, index);
5363 /* When not doing offsetof, array references are not permitted in
5364 constant-expressions. */
5366 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5367 postfix_expression = error_mark_node;
5369 return postfix_expression;
5372 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5373 by cp_parser_builtin_offsetof. We're looking for
5375 postfix-expression . template [opt] id-expression
5376 postfix-expression . pseudo-destructor-name
5377 postfix-expression -> template [opt] id-expression
5378 postfix-expression -> pseudo-destructor-name
5380 FOR_OFFSETOF is set if we're being called in that context. That sorta
5381 limits what of the above we'll actually accept, but nevermind.
5382 TOKEN_TYPE is the "." or "->" token, which will already have been
5383 removed from the stream. */
5386 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5387 enum cpp_ttype token_type,
5388 tree postfix_expression,
5389 bool for_offsetof, cp_id_kind *idk,
5390 location_t location)
5394 bool pseudo_destructor_p;
5395 tree scope = NULL_TREE;
5397 /* If this is a `->' operator, dereference the pointer. */
5398 if (token_type == CPP_DEREF)
5399 postfix_expression = build_x_arrow (postfix_expression);
5400 /* Check to see whether or not the expression is type-dependent. */
5401 dependent_p = type_dependent_expression_p (postfix_expression);
5402 /* The identifier following the `->' or `.' is not qualified. */
5403 parser->scope = NULL_TREE;
5404 parser->qualifying_scope = NULL_TREE;
5405 parser->object_scope = NULL_TREE;
5406 *idk = CP_ID_KIND_NONE;
5408 /* Enter the scope corresponding to the type of the object
5409 given by the POSTFIX_EXPRESSION. */
5410 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5412 scope = TREE_TYPE (postfix_expression);
5413 /* According to the standard, no expression should ever have
5414 reference type. Unfortunately, we do not currently match
5415 the standard in this respect in that our internal representation
5416 of an expression may have reference type even when the standard
5417 says it does not. Therefore, we have to manually obtain the
5418 underlying type here. */
5419 scope = non_reference (scope);
5420 /* The type of the POSTFIX_EXPRESSION must be complete. */
5421 if (scope == unknown_type_node)
5423 error_at (location, "%qE does not have class type",
5424 postfix_expression);
5428 scope = complete_type_or_else (scope, NULL_TREE);
5429 /* Let the name lookup machinery know that we are processing a
5430 class member access expression. */
5431 parser->context->object_type = scope;
5432 /* If something went wrong, we want to be able to discern that case,
5433 as opposed to the case where there was no SCOPE due to the type
5434 of expression being dependent. */
5436 scope = error_mark_node;
5437 /* If the SCOPE was erroneous, make the various semantic analysis
5438 functions exit quickly -- and without issuing additional error
5440 if (scope == error_mark_node)
5441 postfix_expression = error_mark_node;
5444 /* Assume this expression is not a pseudo-destructor access. */
5445 pseudo_destructor_p = false;
5447 /* If the SCOPE is a scalar type, then, if this is a valid program,
5448 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5449 is type dependent, it can be pseudo-destructor-name or something else.
5450 Try to parse it as pseudo-destructor-name first. */
5451 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5456 cp_parser_parse_tentatively (parser);
5457 /* Parse the pseudo-destructor-name. */
5459 cp_parser_pseudo_destructor_name (parser, &s, &type);
5461 && (cp_parser_error_occurred (parser)
5462 || TREE_CODE (type) != TYPE_DECL
5463 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5464 cp_parser_abort_tentative_parse (parser);
5465 else if (cp_parser_parse_definitely (parser))
5467 pseudo_destructor_p = true;
5469 = finish_pseudo_destructor_expr (postfix_expression,
5470 s, TREE_TYPE (type));
5474 if (!pseudo_destructor_p)
5476 /* If the SCOPE is not a scalar type, we are looking at an
5477 ordinary class member access expression, rather than a
5478 pseudo-destructor-name. */
5480 cp_token *token = cp_lexer_peek_token (parser->lexer);
5481 /* Parse the id-expression. */
5482 name = (cp_parser_id_expression
5484 cp_parser_optional_template_keyword (parser),
5485 /*check_dependency_p=*/true,
5487 /*declarator_p=*/false,
5488 /*optional_p=*/false));
5489 /* In general, build a SCOPE_REF if the member name is qualified.
5490 However, if the name was not dependent and has already been
5491 resolved; there is no need to build the SCOPE_REF. For example;
5493 struct X { void f(); };
5494 template <typename T> void f(T* t) { t->X::f(); }
5496 Even though "t" is dependent, "X::f" is not and has been resolved
5497 to a BASELINK; there is no need to include scope information. */
5499 /* But we do need to remember that there was an explicit scope for
5500 virtual function calls. */
5502 *idk = CP_ID_KIND_QUALIFIED;
5504 /* If the name is a template-id that names a type, we will get a
5505 TYPE_DECL here. That is invalid code. */
5506 if (TREE_CODE (name) == TYPE_DECL)
5508 error_at (token->location, "invalid use of %qD", name);
5509 postfix_expression = error_mark_node;
5513 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5515 name = build_qualified_name (/*type=*/NULL_TREE,
5519 parser->scope = NULL_TREE;
5520 parser->qualifying_scope = NULL_TREE;
5521 parser->object_scope = NULL_TREE;
5523 if (scope && name && BASELINK_P (name))
5524 adjust_result_of_qualified_name_lookup
5525 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5527 = finish_class_member_access_expr (postfix_expression, name,
5529 tf_warning_or_error);
5533 /* We no longer need to look up names in the scope of the object on
5534 the left-hand side of the `.' or `->' operator. */
5535 parser->context->object_type = NULL_TREE;
5537 /* Outside of offsetof, these operators may not appear in
5538 constant-expressions. */
5540 && (cp_parser_non_integral_constant_expression
5541 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5542 postfix_expression = error_mark_node;
5544 return postfix_expression;
5547 /* Parse a parenthesized expression-list.
5550 assignment-expression
5551 expression-list, assignment-expression
5556 identifier, expression-list
5558 CAST_P is true if this expression is the target of a cast.
5560 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5563 Returns a vector of trees. Each element is a representation of an
5564 assignment-expression. NULL is returned if the ( and or ) are
5565 missing. An empty, but allocated, vector is returned on no
5566 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5567 if we are parsing an attribute list for an attribute that wants a
5568 plain identifier argument, normal_attr for an attribute that wants
5569 an expression, or non_attr if we aren't parsing an attribute list. If
5570 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5571 not all of the expressions in the list were constant. */
5573 static VEC(tree,gc) *
5574 cp_parser_parenthesized_expression_list (cp_parser* parser,
5575 int is_attribute_list,
5577 bool allow_expansion_p,
5578 bool *non_constant_p)
5580 VEC(tree,gc) *expression_list;
5581 bool fold_expr_p = is_attribute_list != non_attr;
5582 tree identifier = NULL_TREE;
5583 bool saved_greater_than_is_operator_p;
5585 /* Assume all the expressions will be constant. */
5587 *non_constant_p = false;
5589 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5592 expression_list = make_tree_vector ();
5594 /* Within a parenthesized expression, a `>' token is always
5595 the greater-than operator. */
5596 saved_greater_than_is_operator_p
5597 = parser->greater_than_is_operator_p;
5598 parser->greater_than_is_operator_p = true;
5600 /* Consume expressions until there are no more. */
5601 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5606 /* At the beginning of attribute lists, check to see if the
5607 next token is an identifier. */
5608 if (is_attribute_list == id_attr
5609 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5613 /* Consume the identifier. */
5614 token = cp_lexer_consume_token (parser->lexer);
5615 /* Save the identifier. */
5616 identifier = token->u.value;
5620 bool expr_non_constant_p;
5622 /* Parse the next assignment-expression. */
5623 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5625 /* A braced-init-list. */
5626 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5627 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5628 if (non_constant_p && expr_non_constant_p)
5629 *non_constant_p = true;
5631 else if (non_constant_p)
5633 expr = (cp_parser_constant_expression
5634 (parser, /*allow_non_constant_p=*/true,
5635 &expr_non_constant_p));
5636 if (expr_non_constant_p)
5637 *non_constant_p = true;
5640 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5643 expr = fold_non_dependent_expr (expr);
5645 /* If we have an ellipsis, then this is an expression
5647 if (allow_expansion_p
5648 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5650 /* Consume the `...'. */
5651 cp_lexer_consume_token (parser->lexer);
5653 /* Build the argument pack. */
5654 expr = make_pack_expansion (expr);
5657 /* Add it to the list. We add error_mark_node
5658 expressions to the list, so that we can still tell if
5659 the correct form for a parenthesized expression-list
5660 is found. That gives better errors. */
5661 VEC_safe_push (tree, gc, expression_list, expr);
5663 if (expr == error_mark_node)
5667 /* After the first item, attribute lists look the same as
5668 expression lists. */
5669 is_attribute_list = non_attr;
5672 /* If the next token isn't a `,', then we are done. */
5673 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5676 /* Otherwise, consume the `,' and keep going. */
5677 cp_lexer_consume_token (parser->lexer);
5680 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5685 /* We try and resync to an unnested comma, as that will give the
5686 user better diagnostics. */
5687 ending = cp_parser_skip_to_closing_parenthesis (parser,
5688 /*recovering=*/true,
5690 /*consume_paren=*/true);
5695 parser->greater_than_is_operator_p
5696 = saved_greater_than_is_operator_p;
5701 parser->greater_than_is_operator_p
5702 = saved_greater_than_is_operator_p;
5705 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5707 return expression_list;
5710 /* Parse a pseudo-destructor-name.
5712 pseudo-destructor-name:
5713 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5714 :: [opt] nested-name-specifier template template-id :: ~ type-name
5715 :: [opt] nested-name-specifier [opt] ~ type-name
5717 If either of the first two productions is used, sets *SCOPE to the
5718 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5719 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5720 or ERROR_MARK_NODE if the parse fails. */
5723 cp_parser_pseudo_destructor_name (cp_parser* parser,
5727 bool nested_name_specifier_p;
5729 /* Assume that things will not work out. */
5730 *type = error_mark_node;
5732 /* Look for the optional `::' operator. */
5733 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5734 /* Look for the optional nested-name-specifier. */
5735 nested_name_specifier_p
5736 = (cp_parser_nested_name_specifier_opt (parser,
5737 /*typename_keyword_p=*/false,
5738 /*check_dependency_p=*/true,
5740 /*is_declaration=*/false)
5742 /* Now, if we saw a nested-name-specifier, we might be doing the
5743 second production. */
5744 if (nested_name_specifier_p
5745 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5747 /* Consume the `template' keyword. */
5748 cp_lexer_consume_token (parser->lexer);
5749 /* Parse the template-id. */
5750 cp_parser_template_id (parser,
5751 /*template_keyword_p=*/true,
5752 /*check_dependency_p=*/false,
5753 /*is_declaration=*/true);
5754 /* Look for the `::' token. */
5755 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5757 /* If the next token is not a `~', then there might be some
5758 additional qualification. */
5759 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5761 /* At this point, we're looking for "type-name :: ~". The type-name
5762 must not be a class-name, since this is a pseudo-destructor. So,
5763 it must be either an enum-name, or a typedef-name -- both of which
5764 are just identifiers. So, we peek ahead to check that the "::"
5765 and "~" tokens are present; if they are not, then we can avoid
5766 calling type_name. */
5767 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5768 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5769 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5771 cp_parser_error (parser, "non-scalar type");
5775 /* Look for the type-name. */
5776 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5777 if (*scope == error_mark_node)
5780 /* Look for the `::' token. */
5781 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5786 /* Look for the `~'. */
5787 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5788 /* Look for the type-name again. We are not responsible for
5789 checking that it matches the first type-name. */
5790 *type = cp_parser_nonclass_name (parser);
5793 /* Parse a unary-expression.
5799 unary-operator cast-expression
5800 sizeof unary-expression
5808 __extension__ cast-expression
5809 __alignof__ unary-expression
5810 __alignof__ ( type-id )
5811 __real__ cast-expression
5812 __imag__ cast-expression
5815 ADDRESS_P is true iff the unary-expression is appearing as the
5816 operand of the `&' operator. CAST_P is true if this expression is
5817 the target of a cast.
5819 Returns a representation of the expression. */
5822 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5826 enum tree_code unary_operator;
5828 /* Peek at the next token. */
5829 token = cp_lexer_peek_token (parser->lexer);
5830 /* Some keywords give away the kind of expression. */
5831 if (token->type == CPP_KEYWORD)
5833 enum rid keyword = token->keyword;
5843 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5844 /* Consume the token. */
5845 cp_lexer_consume_token (parser->lexer);
5846 /* Parse the operand. */
5847 operand = cp_parser_sizeof_operand (parser, keyword);
5849 if (TYPE_P (operand))
5850 return cxx_sizeof_or_alignof_type (operand, op, true);
5852 return cxx_sizeof_or_alignof_expr (operand, op, true);
5856 return cp_parser_new_expression (parser);
5859 return cp_parser_delete_expression (parser);
5863 /* The saved value of the PEDANTIC flag. */
5867 /* Save away the PEDANTIC flag. */
5868 cp_parser_extension_opt (parser, &saved_pedantic);
5869 /* Parse the cast-expression. */
5870 expr = cp_parser_simple_cast_expression (parser);
5871 /* Restore the PEDANTIC flag. */
5872 pedantic = saved_pedantic;
5882 /* Consume the `__real__' or `__imag__' token. */
5883 cp_lexer_consume_token (parser->lexer);
5884 /* Parse the cast-expression. */
5885 expression = cp_parser_simple_cast_expression (parser);
5886 /* Create the complete representation. */
5887 return build_x_unary_op ((keyword == RID_REALPART
5888 ? REALPART_EXPR : IMAGPART_EXPR),
5890 tf_warning_or_error);
5897 const char *saved_message;
5898 bool saved_integral_constant_expression_p;
5899 bool saved_non_integral_constant_expression_p;
5900 bool saved_greater_than_is_operator_p;
5902 cp_lexer_consume_token (parser->lexer);
5903 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5905 saved_message = parser->type_definition_forbidden_message;
5906 parser->type_definition_forbidden_message
5907 = G_("types may not be defined in %<noexcept%> expressions");
5909 saved_integral_constant_expression_p
5910 = parser->integral_constant_expression_p;
5911 saved_non_integral_constant_expression_p
5912 = parser->non_integral_constant_expression_p;
5913 parser->integral_constant_expression_p = false;
5915 saved_greater_than_is_operator_p
5916 = parser->greater_than_is_operator_p;
5917 parser->greater_than_is_operator_p = true;
5919 ++cp_unevaluated_operand;
5920 ++c_inhibit_evaluation_warnings;
5921 expr = cp_parser_expression (parser, false, NULL);
5922 --c_inhibit_evaluation_warnings;
5923 --cp_unevaluated_operand;
5925 parser->greater_than_is_operator_p
5926 = saved_greater_than_is_operator_p;
5928 parser->integral_constant_expression_p
5929 = saved_integral_constant_expression_p;
5930 parser->non_integral_constant_expression_p
5931 = saved_non_integral_constant_expression_p;
5933 parser->type_definition_forbidden_message = saved_message;
5935 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5936 return finish_noexcept_expr (expr, tf_warning_or_error);
5944 /* Look for the `:: new' and `:: delete', which also signal the
5945 beginning of a new-expression, or delete-expression,
5946 respectively. If the next token is `::', then it might be one of
5948 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5952 /* See if the token after the `::' is one of the keywords in
5953 which we're interested. */
5954 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5955 /* If it's `new', we have a new-expression. */
5956 if (keyword == RID_NEW)
5957 return cp_parser_new_expression (parser);
5958 /* Similarly, for `delete'. */
5959 else if (keyword == RID_DELETE)
5960 return cp_parser_delete_expression (parser);
5963 /* Look for a unary operator. */
5964 unary_operator = cp_parser_unary_operator (token);
5965 /* The `++' and `--' operators can be handled similarly, even though
5966 they are not technically unary-operators in the grammar. */
5967 if (unary_operator == ERROR_MARK)
5969 if (token->type == CPP_PLUS_PLUS)
5970 unary_operator = PREINCREMENT_EXPR;
5971 else if (token->type == CPP_MINUS_MINUS)
5972 unary_operator = PREDECREMENT_EXPR;
5973 /* Handle the GNU address-of-label extension. */
5974 else if (cp_parser_allow_gnu_extensions_p (parser)
5975 && token->type == CPP_AND_AND)
5979 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5981 /* Consume the '&&' token. */
5982 cp_lexer_consume_token (parser->lexer);
5983 /* Look for the identifier. */
5984 identifier = cp_parser_identifier (parser);
5985 /* Create an expression representing the address. */
5986 expression = finish_label_address_expr (identifier, loc);
5987 if (cp_parser_non_integral_constant_expression (parser,
5989 expression = error_mark_node;
5993 if (unary_operator != ERROR_MARK)
5995 tree cast_expression;
5996 tree expression = error_mark_node;
5997 non_integral_constant non_constant_p = NIC_NONE;
5999 /* Consume the operator token. */
6000 token = cp_lexer_consume_token (parser->lexer);
6001 /* Parse the cast-expression. */
6003 = cp_parser_cast_expression (parser,
6004 unary_operator == ADDR_EXPR,
6005 /*cast_p=*/false, pidk);
6006 /* Now, build an appropriate representation. */
6007 switch (unary_operator)
6010 non_constant_p = NIC_STAR;
6011 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6012 tf_warning_or_error);
6016 non_constant_p = NIC_ADDR;
6019 expression = build_x_unary_op (unary_operator, cast_expression,
6020 tf_warning_or_error);
6023 case PREINCREMENT_EXPR:
6024 case PREDECREMENT_EXPR:
6025 non_constant_p = unary_operator == PREINCREMENT_EXPR
6026 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6028 case UNARY_PLUS_EXPR:
6030 case TRUTH_NOT_EXPR:
6031 expression = finish_unary_op_expr (unary_operator, cast_expression);
6038 if (non_constant_p != NIC_NONE
6039 && cp_parser_non_integral_constant_expression (parser,
6041 expression = error_mark_node;
6046 return cp_parser_postfix_expression (parser, address_p, cast_p,
6047 /*member_access_only_p=*/false,
6051 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6052 unary-operator, the corresponding tree code is returned. */
6054 static enum tree_code
6055 cp_parser_unary_operator (cp_token* token)
6057 switch (token->type)
6060 return INDIRECT_REF;
6066 return UNARY_PLUS_EXPR;
6072 return TRUTH_NOT_EXPR;
6075 return BIT_NOT_EXPR;
6082 /* Parse a new-expression.
6085 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6086 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6088 Returns a representation of the expression. */
6091 cp_parser_new_expression (cp_parser* parser)
6093 bool global_scope_p;
6094 VEC(tree,gc) *placement;
6096 VEC(tree,gc) *initializer;
6100 /* Look for the optional `::' operator. */
6102 = (cp_parser_global_scope_opt (parser,
6103 /*current_scope_valid_p=*/false)
6105 /* Look for the `new' operator. */
6106 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6107 /* There's no easy way to tell a new-placement from the
6108 `( type-id )' construct. */
6109 cp_parser_parse_tentatively (parser);
6110 /* Look for a new-placement. */
6111 placement = cp_parser_new_placement (parser);
6112 /* If that didn't work out, there's no new-placement. */
6113 if (!cp_parser_parse_definitely (parser))
6115 if (placement != NULL)
6116 release_tree_vector (placement);
6120 /* If the next token is a `(', then we have a parenthesized
6122 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6125 /* Consume the `('. */
6126 cp_lexer_consume_token (parser->lexer);
6127 /* Parse the type-id. */
6128 type = cp_parser_type_id (parser);
6129 /* Look for the closing `)'. */
6130 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6131 token = cp_lexer_peek_token (parser->lexer);
6132 /* There should not be a direct-new-declarator in this production,
6133 but GCC used to allowed this, so we check and emit a sensible error
6134 message for this case. */
6135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6137 error_at (token->location,
6138 "array bound forbidden after parenthesized type-id");
6139 inform (token->location,
6140 "try removing the parentheses around the type-id");
6141 cp_parser_direct_new_declarator (parser);
6145 /* Otherwise, there must be a new-type-id. */
6147 type = cp_parser_new_type_id (parser, &nelts);
6149 /* If the next token is a `(' or '{', then we have a new-initializer. */
6150 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6151 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6152 initializer = cp_parser_new_initializer (parser);
6156 /* A new-expression may not appear in an integral constant
6158 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6159 ret = error_mark_node;
6162 /* Create a representation of the new-expression. */
6163 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6164 tf_warning_or_error);
6167 if (placement != NULL)
6168 release_tree_vector (placement);
6169 if (initializer != NULL)
6170 release_tree_vector (initializer);
6175 /* Parse a new-placement.
6180 Returns the same representation as for an expression-list. */
6182 static VEC(tree,gc) *
6183 cp_parser_new_placement (cp_parser* parser)
6185 VEC(tree,gc) *expression_list;
6187 /* Parse the expression-list. */
6188 expression_list = (cp_parser_parenthesized_expression_list
6189 (parser, non_attr, /*cast_p=*/false,
6190 /*allow_expansion_p=*/true,
6191 /*non_constant_p=*/NULL));
6193 return expression_list;
6196 /* Parse a new-type-id.
6199 type-specifier-seq new-declarator [opt]
6201 Returns the TYPE allocated. If the new-type-id indicates an array
6202 type, *NELTS is set to the number of elements in the last array
6203 bound; the TYPE will not include the last array bound. */
6206 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6208 cp_decl_specifier_seq type_specifier_seq;
6209 cp_declarator *new_declarator;
6210 cp_declarator *declarator;
6211 cp_declarator *outer_declarator;
6212 const char *saved_message;
6215 /* The type-specifier sequence must not contain type definitions.
6216 (It cannot contain declarations of new types either, but if they
6217 are not definitions we will catch that because they are not
6219 saved_message = parser->type_definition_forbidden_message;
6220 parser->type_definition_forbidden_message
6221 = G_("types may not be defined in a new-type-id");
6222 /* Parse the type-specifier-seq. */
6223 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6224 /*is_trailing_return=*/false,
6225 &type_specifier_seq);
6226 /* Restore the old message. */
6227 parser->type_definition_forbidden_message = saved_message;
6228 /* Parse the new-declarator. */
6229 new_declarator = cp_parser_new_declarator_opt (parser);
6231 /* Determine the number of elements in the last array dimension, if
6234 /* Skip down to the last array dimension. */
6235 declarator = new_declarator;
6236 outer_declarator = NULL;
6237 while (declarator && (declarator->kind == cdk_pointer
6238 || declarator->kind == cdk_ptrmem))
6240 outer_declarator = declarator;
6241 declarator = declarator->declarator;
6244 && declarator->kind == cdk_array
6245 && declarator->declarator
6246 && declarator->declarator->kind == cdk_array)
6248 outer_declarator = declarator;
6249 declarator = declarator->declarator;
6252 if (declarator && declarator->kind == cdk_array)
6254 *nelts = declarator->u.array.bounds;
6255 if (*nelts == error_mark_node)
6256 *nelts = integer_one_node;
6258 if (outer_declarator)
6259 outer_declarator->declarator = declarator->declarator;
6261 new_declarator = NULL;
6264 type = groktypename (&type_specifier_seq, new_declarator, false);
6268 /* Parse an (optional) new-declarator.
6271 ptr-operator new-declarator [opt]
6272 direct-new-declarator
6274 Returns the declarator. */
6276 static cp_declarator *
6277 cp_parser_new_declarator_opt (cp_parser* parser)
6279 enum tree_code code;
6281 cp_cv_quals cv_quals;
6283 /* We don't know if there's a ptr-operator next, or not. */
6284 cp_parser_parse_tentatively (parser);
6285 /* Look for a ptr-operator. */
6286 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6287 /* If that worked, look for more new-declarators. */
6288 if (cp_parser_parse_definitely (parser))
6290 cp_declarator *declarator;
6292 /* Parse another optional declarator. */
6293 declarator = cp_parser_new_declarator_opt (parser);
6295 return cp_parser_make_indirect_declarator
6296 (code, type, cv_quals, declarator);
6299 /* If the next token is a `[', there is a direct-new-declarator. */
6300 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6301 return cp_parser_direct_new_declarator (parser);
6306 /* Parse a direct-new-declarator.
6308 direct-new-declarator:
6310 direct-new-declarator [constant-expression]
6314 static cp_declarator *
6315 cp_parser_direct_new_declarator (cp_parser* parser)
6317 cp_declarator *declarator = NULL;
6323 /* Look for the opening `['. */
6324 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6325 /* The first expression is not required to be constant. */
6328 cp_token *token = cp_lexer_peek_token (parser->lexer);
6329 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6330 /* The standard requires that the expression have integral
6331 type. DR 74 adds enumeration types. We believe that the
6332 real intent is that these expressions be handled like the
6333 expression in a `switch' condition, which also allows
6334 classes with a single conversion to integral or
6335 enumeration type. */
6336 if (!processing_template_decl)
6339 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6344 error_at (token->location,
6345 "expression in new-declarator must have integral "
6346 "or enumeration type");
6347 expression = error_mark_node;
6351 /* But all the other expressions must be. */
6354 = cp_parser_constant_expression (parser,
6355 /*allow_non_constant=*/false,
6357 /* Look for the closing `]'. */
6358 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6360 /* Add this bound to the declarator. */
6361 declarator = make_array_declarator (declarator, expression);
6363 /* If the next token is not a `[', then there are no more
6365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6372 /* Parse a new-initializer.
6375 ( expression-list [opt] )
6378 Returns a representation of the expression-list. */
6380 static VEC(tree,gc) *
6381 cp_parser_new_initializer (cp_parser* parser)
6383 VEC(tree,gc) *expression_list;
6385 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6388 bool expr_non_constant_p;
6389 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6390 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6391 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6392 expression_list = make_tree_vector_single (t);
6395 expression_list = (cp_parser_parenthesized_expression_list
6396 (parser, non_attr, /*cast_p=*/false,
6397 /*allow_expansion_p=*/true,
6398 /*non_constant_p=*/NULL));
6400 return expression_list;
6403 /* Parse a delete-expression.
6406 :: [opt] delete cast-expression
6407 :: [opt] delete [ ] cast-expression
6409 Returns a representation of the expression. */
6412 cp_parser_delete_expression (cp_parser* parser)
6414 bool global_scope_p;
6418 /* Look for the optional `::' operator. */
6420 = (cp_parser_global_scope_opt (parser,
6421 /*current_scope_valid_p=*/false)
6423 /* Look for the `delete' keyword. */
6424 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6425 /* See if the array syntax is in use. */
6426 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6428 /* Consume the `[' token. */
6429 cp_lexer_consume_token (parser->lexer);
6430 /* Look for the `]' token. */
6431 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6432 /* Remember that this is the `[]' construct. */
6438 /* Parse the cast-expression. */
6439 expression = cp_parser_simple_cast_expression (parser);
6441 /* A delete-expression may not appear in an integral constant
6443 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6444 return error_mark_node;
6446 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6449 /* Returns true if TOKEN may start a cast-expression and false
6453 cp_parser_token_starts_cast_expression (cp_token *token)
6455 switch (token->type)
6461 case CPP_CLOSE_SQUARE:
6462 case CPP_CLOSE_PAREN:
6463 case CPP_CLOSE_BRACE:
6467 case CPP_DEREF_STAR:
6475 case CPP_GREATER_EQ:
6495 /* '[' may start a primary-expression in obj-c++. */
6496 case CPP_OPEN_SQUARE:
6497 return c_dialect_objc ();
6504 /* Parse a cast-expression.
6508 ( type-id ) cast-expression
6510 ADDRESS_P is true iff the unary-expression is appearing as the
6511 operand of the `&' operator. CAST_P is true if this expression is
6512 the target of a cast.
6514 Returns a representation of the expression. */
6517 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6520 /* If it's a `(', then we might be looking at a cast. */
6521 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6523 tree type = NULL_TREE;
6524 tree expr = NULL_TREE;
6525 bool compound_literal_p;
6526 const char *saved_message;
6528 /* There's no way to know yet whether or not this is a cast.
6529 For example, `(int (3))' is a unary-expression, while `(int)
6530 3' is a cast. So, we resort to parsing tentatively. */
6531 cp_parser_parse_tentatively (parser);
6532 /* Types may not be defined in a cast. */
6533 saved_message = parser->type_definition_forbidden_message;
6534 parser->type_definition_forbidden_message
6535 = G_("types may not be defined in casts");
6536 /* Consume the `('. */
6537 cp_lexer_consume_token (parser->lexer);
6538 /* A very tricky bit is that `(struct S) { 3 }' is a
6539 compound-literal (which we permit in C++ as an extension).
6540 But, that construct is not a cast-expression -- it is a
6541 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6542 is legal; if the compound-literal were a cast-expression,
6543 you'd need an extra set of parentheses.) But, if we parse
6544 the type-id, and it happens to be a class-specifier, then we
6545 will commit to the parse at that point, because we cannot
6546 undo the action that is done when creating a new class. So,
6547 then we cannot back up and do a postfix-expression.
6549 Therefore, we scan ahead to the closing `)', and check to see
6550 if the token after the `)' is a `{'. If so, we are not
6551 looking at a cast-expression.
6553 Save tokens so that we can put them back. */
6554 cp_lexer_save_tokens (parser->lexer);
6555 /* Skip tokens until the next token is a closing parenthesis.
6556 If we find the closing `)', and the next token is a `{', then
6557 we are looking at a compound-literal. */
6559 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6560 /*consume_paren=*/true)
6561 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6562 /* Roll back the tokens we skipped. */
6563 cp_lexer_rollback_tokens (parser->lexer);
6564 /* If we were looking at a compound-literal, simulate an error
6565 so that the call to cp_parser_parse_definitely below will
6567 if (compound_literal_p)
6568 cp_parser_simulate_error (parser);
6571 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6572 parser->in_type_id_in_expr_p = true;
6573 /* Look for the type-id. */
6574 type = cp_parser_type_id (parser);
6575 /* Look for the closing `)'. */
6576 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6577 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6580 /* Restore the saved message. */
6581 parser->type_definition_forbidden_message = saved_message;
6583 /* At this point this can only be either a cast or a
6584 parenthesized ctor such as `(T ())' that looks like a cast to
6585 function returning T. */
6586 if (!cp_parser_error_occurred (parser)
6587 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6590 cp_parser_parse_definitely (parser);
6591 expr = cp_parser_cast_expression (parser,
6592 /*address_p=*/false,
6593 /*cast_p=*/true, pidk);
6595 /* Warn about old-style casts, if so requested. */
6596 if (warn_old_style_cast
6597 && !in_system_header
6598 && !VOID_TYPE_P (type)
6599 && current_lang_name != lang_name_c)
6600 warning (OPT_Wold_style_cast, "use of old-style cast");
6602 /* Only type conversions to integral or enumeration types
6603 can be used in constant-expressions. */
6604 if (!cast_valid_in_integral_constant_expression_p (type)
6605 && cp_parser_non_integral_constant_expression (parser,
6607 return error_mark_node;
6609 /* Perform the cast. */
6610 expr = build_c_cast (input_location, type, expr);
6614 cp_parser_abort_tentative_parse (parser);
6617 /* If we get here, then it's not a cast, so it must be a
6618 unary-expression. */
6619 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6622 /* Parse a binary expression of the general form:
6626 pm-expression .* cast-expression
6627 pm-expression ->* cast-expression
6629 multiplicative-expression:
6631 multiplicative-expression * pm-expression
6632 multiplicative-expression / pm-expression
6633 multiplicative-expression % pm-expression
6635 additive-expression:
6636 multiplicative-expression
6637 additive-expression + multiplicative-expression
6638 additive-expression - multiplicative-expression
6642 shift-expression << additive-expression
6643 shift-expression >> additive-expression
6645 relational-expression:
6647 relational-expression < shift-expression
6648 relational-expression > shift-expression
6649 relational-expression <= shift-expression
6650 relational-expression >= shift-expression
6654 relational-expression:
6655 relational-expression <? shift-expression
6656 relational-expression >? shift-expression
6658 equality-expression:
6659 relational-expression
6660 equality-expression == relational-expression
6661 equality-expression != relational-expression
6665 and-expression & equality-expression
6667 exclusive-or-expression:
6669 exclusive-or-expression ^ and-expression
6671 inclusive-or-expression:
6672 exclusive-or-expression
6673 inclusive-or-expression | exclusive-or-expression
6675 logical-and-expression:
6676 inclusive-or-expression
6677 logical-and-expression && inclusive-or-expression
6679 logical-or-expression:
6680 logical-and-expression
6681 logical-or-expression || logical-and-expression
6683 All these are implemented with a single function like:
6686 simple-cast-expression
6687 binary-expression <token> binary-expression
6689 CAST_P is true if this expression is the target of a cast.
6691 The binops_by_token map is used to get the tree codes for each <token> type.
6692 binary-expressions are associated according to a precedence table. */
6694 #define TOKEN_PRECEDENCE(token) \
6695 (((token->type == CPP_GREATER \
6696 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6697 && !parser->greater_than_is_operator_p) \
6698 ? PREC_NOT_OPERATOR \
6699 : binops_by_token[token->type].prec)
6702 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6703 bool no_toplevel_fold_p,
6704 enum cp_parser_prec prec,
6707 cp_parser_expression_stack stack;
6708 cp_parser_expression_stack_entry *sp = &stack[0];
6711 enum tree_code tree_type, lhs_type, rhs_type;
6712 enum cp_parser_prec new_prec, lookahead_prec;
6715 /* Parse the first expression. */
6716 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6717 lhs_type = ERROR_MARK;
6721 /* Get an operator token. */
6722 token = cp_lexer_peek_token (parser->lexer);
6724 if (warn_cxx0x_compat
6725 && token->type == CPP_RSHIFT
6726 && !parser->greater_than_is_operator_p)
6728 if (warning_at (token->location, OPT_Wc__0x_compat,
6729 "%<>>%> operator will be treated as"
6730 " two right angle brackets in C++0x"))
6731 inform (token->location,
6732 "suggest parentheses around %<>>%> expression");
6735 new_prec = TOKEN_PRECEDENCE (token);
6737 /* Popping an entry off the stack means we completed a subexpression:
6738 - either we found a token which is not an operator (`>' where it is not
6739 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6740 will happen repeatedly;
6741 - or, we found an operator which has lower priority. This is the case
6742 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6744 if (new_prec <= prec)
6753 tree_type = binops_by_token[token->type].tree_type;
6755 /* We used the operator token. */
6756 cp_lexer_consume_token (parser->lexer);
6758 /* For "false && x" or "true || x", x will never be executed;
6759 disable warnings while evaluating it. */
6760 if (tree_type == TRUTH_ANDIF_EXPR)
6761 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6762 else if (tree_type == TRUTH_ORIF_EXPR)
6763 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6765 /* Extract another operand. It may be the RHS of this expression
6766 or the LHS of a new, higher priority expression. */
6767 rhs = cp_parser_simple_cast_expression (parser);
6768 rhs_type = ERROR_MARK;
6770 /* Get another operator token. Look up its precedence to avoid
6771 building a useless (immediately popped) stack entry for common
6772 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6773 token = cp_lexer_peek_token (parser->lexer);
6774 lookahead_prec = TOKEN_PRECEDENCE (token);
6775 if (lookahead_prec > new_prec)
6777 /* ... and prepare to parse the RHS of the new, higher priority
6778 expression. Since precedence levels on the stack are
6779 monotonically increasing, we do not have to care about
6782 sp->tree_type = tree_type;
6784 sp->lhs_type = lhs_type;
6787 lhs_type = rhs_type;
6789 new_prec = lookahead_prec;
6793 lookahead_prec = new_prec;
6794 /* If the stack is not empty, we have parsed into LHS the right side
6795 (`4' in the example above) of an expression we had suspended.
6796 We can use the information on the stack to recover the LHS (`3')
6797 from the stack together with the tree code (`MULT_EXPR'), and
6798 the precedence of the higher level subexpression
6799 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6800 which will be used to actually build the additive expression. */
6803 tree_type = sp->tree_type;
6805 rhs_type = lhs_type;
6807 lhs_type = sp->lhs_type;
6810 /* Undo the disabling of warnings done above. */
6811 if (tree_type == TRUTH_ANDIF_EXPR)
6812 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6813 else if (tree_type == TRUTH_ORIF_EXPR)
6814 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6816 overloaded_p = false;
6817 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6818 ERROR_MARK for everything that is not a binary expression.
6819 This makes warn_about_parentheses miss some warnings that
6820 involve unary operators. For unary expressions we should
6821 pass the correct tree_code unless the unary expression was
6822 surrounded by parentheses.
6824 if (no_toplevel_fold_p
6825 && lookahead_prec <= prec
6827 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6828 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6830 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6831 &overloaded_p, tf_warning_or_error);
6832 lhs_type = tree_type;
6834 /* If the binary operator required the use of an overloaded operator,
6835 then this expression cannot be an integral constant-expression.
6836 An overloaded operator can be used even if both operands are
6837 otherwise permissible in an integral constant-expression if at
6838 least one of the operands is of enumeration type. */
6841 && cp_parser_non_integral_constant_expression (parser,
6843 return error_mark_node;
6850 /* Parse the `? expression : assignment-expression' part of a
6851 conditional-expression. The LOGICAL_OR_EXPR is the
6852 logical-or-expression that started the conditional-expression.
6853 Returns a representation of the entire conditional-expression.
6855 This routine is used by cp_parser_assignment_expression.
6857 ? expression : assignment-expression
6861 ? : assignment-expression */
6864 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6867 tree assignment_expr;
6868 struct cp_token *token;
6870 /* Consume the `?' token. */
6871 cp_lexer_consume_token (parser->lexer);
6872 token = cp_lexer_peek_token (parser->lexer);
6873 if (cp_parser_allow_gnu_extensions_p (parser)
6874 && token->type == CPP_COLON)
6876 pedwarn (token->location, OPT_pedantic,
6877 "ISO C++ does not allow ?: with omitted middle operand");
6878 /* Implicit true clause. */
6880 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6881 warn_for_omitted_condop (token->location, logical_or_expr);
6885 /* Parse the expression. */
6886 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6887 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6888 c_inhibit_evaluation_warnings +=
6889 ((logical_or_expr == truthvalue_true_node)
6890 - (logical_or_expr == truthvalue_false_node));
6893 /* The next token should be a `:'. */
6894 cp_parser_require (parser, CPP_COLON, RT_COLON);
6895 /* Parse the assignment-expression. */
6896 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6897 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6899 /* Build the conditional-expression. */
6900 return build_x_conditional_expr (logical_or_expr,
6903 tf_warning_or_error);
6906 /* Parse an assignment-expression.
6908 assignment-expression:
6909 conditional-expression
6910 logical-or-expression assignment-operator assignment_expression
6913 CAST_P is true if this expression is the target of a cast.
6915 Returns a representation for the expression. */
6918 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6923 /* If the next token is the `throw' keyword, then we're looking at
6924 a throw-expression. */
6925 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6926 expr = cp_parser_throw_expression (parser);
6927 /* Otherwise, it must be that we are looking at a
6928 logical-or-expression. */
6931 /* Parse the binary expressions (logical-or-expression). */
6932 expr = cp_parser_binary_expression (parser, cast_p, false,
6933 PREC_NOT_OPERATOR, pidk);
6934 /* If the next token is a `?' then we're actually looking at a
6935 conditional-expression. */
6936 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6937 return cp_parser_question_colon_clause (parser, expr);
6940 enum tree_code assignment_operator;
6942 /* If it's an assignment-operator, we're using the second
6945 = cp_parser_assignment_operator_opt (parser);
6946 if (assignment_operator != ERROR_MARK)
6948 bool non_constant_p;
6950 /* Parse the right-hand side of the assignment. */
6951 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6953 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6954 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6956 /* An assignment may not appear in a
6957 constant-expression. */
6958 if (cp_parser_non_integral_constant_expression (parser,
6960 return error_mark_node;
6961 /* Build the assignment expression. */
6962 expr = build_x_modify_expr (expr,
6963 assignment_operator,
6965 tf_warning_or_error);
6973 /* Parse an (optional) assignment-operator.
6975 assignment-operator: one of
6976 = *= /= %= += -= >>= <<= &= ^= |=
6980 assignment-operator: one of
6983 If the next token is an assignment operator, the corresponding tree
6984 code is returned, and the token is consumed. For example, for
6985 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6986 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6987 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6988 operator, ERROR_MARK is returned. */
6990 static enum tree_code
6991 cp_parser_assignment_operator_opt (cp_parser* parser)
6996 /* Peek at the next token. */
6997 token = cp_lexer_peek_token (parser->lexer);
6999 switch (token->type)
7010 op = TRUNC_DIV_EXPR;
7014 op = TRUNC_MOD_EXPR;
7046 /* Nothing else is an assignment operator. */
7050 /* If it was an assignment operator, consume it. */
7051 if (op != ERROR_MARK)
7052 cp_lexer_consume_token (parser->lexer);
7057 /* Parse an expression.
7060 assignment-expression
7061 expression , assignment-expression
7063 CAST_P is true if this expression is the target of a cast.
7065 Returns a representation of the expression. */
7068 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7070 tree expression = NULL_TREE;
7074 tree assignment_expression;
7076 /* Parse the next assignment-expression. */
7077 assignment_expression
7078 = cp_parser_assignment_expression (parser, cast_p, pidk);
7079 /* If this is the first assignment-expression, we can just
7082 expression = assignment_expression;
7084 expression = build_x_compound_expr (expression,
7085 assignment_expression,
7086 tf_warning_or_error);
7087 /* If the next token is not a comma, then we are done with the
7089 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7091 /* Consume the `,'. */
7092 cp_lexer_consume_token (parser->lexer);
7093 /* A comma operator cannot appear in a constant-expression. */
7094 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7095 expression = error_mark_node;
7101 /* Parse a constant-expression.
7103 constant-expression:
7104 conditional-expression
7106 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7107 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7108 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7109 is false, NON_CONSTANT_P should be NULL. */
7112 cp_parser_constant_expression (cp_parser* parser,
7113 bool allow_non_constant_p,
7114 bool *non_constant_p)
7116 bool saved_integral_constant_expression_p;
7117 bool saved_allow_non_integral_constant_expression_p;
7118 bool saved_non_integral_constant_expression_p;
7121 /* It might seem that we could simply parse the
7122 conditional-expression, and then check to see if it were
7123 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7124 one that the compiler can figure out is constant, possibly after
7125 doing some simplifications or optimizations. The standard has a
7126 precise definition of constant-expression, and we must honor
7127 that, even though it is somewhat more restrictive.
7133 is not a legal declaration, because `(2, 3)' is not a
7134 constant-expression. The `,' operator is forbidden in a
7135 constant-expression. However, GCC's constant-folding machinery
7136 will fold this operation to an INTEGER_CST for `3'. */
7138 /* Save the old settings. */
7139 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7140 saved_allow_non_integral_constant_expression_p
7141 = parser->allow_non_integral_constant_expression_p;
7142 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7143 /* We are now parsing a constant-expression. */
7144 parser->integral_constant_expression_p = true;
7145 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
7146 parser->non_integral_constant_expression_p = false;
7147 /* Although the grammar says "conditional-expression", we parse an
7148 "assignment-expression", which also permits "throw-expression"
7149 and the use of assignment operators. In the case that
7150 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7151 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7152 actually essential that we look for an assignment-expression.
7153 For example, cp_parser_initializer_clauses uses this function to
7154 determine whether a particular assignment-expression is in fact
7156 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7157 /* Restore the old settings. */
7158 parser->integral_constant_expression_p
7159 = saved_integral_constant_expression_p;
7160 parser->allow_non_integral_constant_expression_p
7161 = saved_allow_non_integral_constant_expression_p;
7162 if (allow_non_constant_p)
7163 *non_constant_p = parser->non_integral_constant_expression_p;
7164 else if (parser->non_integral_constant_expression_p)
7165 expression = error_mark_node;
7166 parser->non_integral_constant_expression_p
7167 = saved_non_integral_constant_expression_p;
7172 /* Parse __builtin_offsetof.
7174 offsetof-expression:
7175 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7177 offsetof-member-designator:
7179 | offsetof-member-designator "." id-expression
7180 | offsetof-member-designator "[" expression "]"
7181 | offsetof-member-designator "->" id-expression */
7184 cp_parser_builtin_offsetof (cp_parser *parser)
7186 int save_ice_p, save_non_ice_p;
7191 /* We're about to accept non-integral-constant things, but will
7192 definitely yield an integral constant expression. Save and
7193 restore these values around our local parsing. */
7194 save_ice_p = parser->integral_constant_expression_p;
7195 save_non_ice_p = parser->non_integral_constant_expression_p;
7197 /* Consume the "__builtin_offsetof" token. */
7198 cp_lexer_consume_token (parser->lexer);
7199 /* Consume the opening `('. */
7200 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7201 /* Parse the type-id. */
7202 type = cp_parser_type_id (parser);
7203 /* Look for the `,'. */
7204 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7205 token = cp_lexer_peek_token (parser->lexer);
7207 /* Build the (type *)null that begins the traditional offsetof macro. */
7208 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7209 tf_warning_or_error);
7211 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7212 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7213 true, &dummy, token->location);
7216 token = cp_lexer_peek_token (parser->lexer);
7217 switch (token->type)
7219 case CPP_OPEN_SQUARE:
7220 /* offsetof-member-designator "[" expression "]" */
7221 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7225 /* offsetof-member-designator "->" identifier */
7226 expr = grok_array_decl (expr, integer_zero_node);
7230 /* offsetof-member-designator "." identifier */
7231 cp_lexer_consume_token (parser->lexer);
7232 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7237 case CPP_CLOSE_PAREN:
7238 /* Consume the ")" token. */
7239 cp_lexer_consume_token (parser->lexer);
7243 /* Error. We know the following require will fail, but
7244 that gives the proper error message. */
7245 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7246 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7247 expr = error_mark_node;
7253 /* If we're processing a template, we can't finish the semantics yet.
7254 Otherwise we can fold the entire expression now. */
7255 if (processing_template_decl)
7256 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7258 expr = finish_offsetof (expr);
7261 parser->integral_constant_expression_p = save_ice_p;
7262 parser->non_integral_constant_expression_p = save_non_ice_p;
7267 /* Parse a trait expression. */
7270 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7273 tree type1, type2 = NULL_TREE;
7274 bool binary = false;
7275 cp_decl_specifier_seq decl_specs;
7279 case RID_HAS_NOTHROW_ASSIGN:
7280 kind = CPTK_HAS_NOTHROW_ASSIGN;
7282 case RID_HAS_NOTHROW_CONSTRUCTOR:
7283 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7285 case RID_HAS_NOTHROW_COPY:
7286 kind = CPTK_HAS_NOTHROW_COPY;
7288 case RID_HAS_TRIVIAL_ASSIGN:
7289 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7291 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7292 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7294 case RID_HAS_TRIVIAL_COPY:
7295 kind = CPTK_HAS_TRIVIAL_COPY;
7297 case RID_HAS_TRIVIAL_DESTRUCTOR:
7298 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7300 case RID_HAS_VIRTUAL_DESTRUCTOR:
7301 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7303 case RID_IS_ABSTRACT:
7304 kind = CPTK_IS_ABSTRACT;
7306 case RID_IS_BASE_OF:
7307 kind = CPTK_IS_BASE_OF;
7311 kind = CPTK_IS_CLASS;
7313 case RID_IS_CONVERTIBLE_TO:
7314 kind = CPTK_IS_CONVERTIBLE_TO;
7318 kind = CPTK_IS_EMPTY;
7321 kind = CPTK_IS_ENUM;
7326 case RID_IS_POLYMORPHIC:
7327 kind = CPTK_IS_POLYMORPHIC;
7329 case RID_IS_STD_LAYOUT:
7330 kind = CPTK_IS_STD_LAYOUT;
7332 case RID_IS_TRIVIAL:
7333 kind = CPTK_IS_TRIVIAL;
7336 kind = CPTK_IS_UNION;
7342 /* Consume the token. */
7343 cp_lexer_consume_token (parser->lexer);
7345 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7347 type1 = cp_parser_type_id (parser);
7349 if (type1 == error_mark_node)
7350 return error_mark_node;
7352 /* Build a trivial decl-specifier-seq. */
7353 clear_decl_specs (&decl_specs);
7354 decl_specs.type = type1;
7356 /* Call grokdeclarator to figure out what type this is. */
7357 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7358 /*initialized=*/0, /*attrlist=*/NULL);
7362 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7364 type2 = cp_parser_type_id (parser);
7366 if (type2 == error_mark_node)
7367 return error_mark_node;
7369 /* Build a trivial decl-specifier-seq. */
7370 clear_decl_specs (&decl_specs);
7371 decl_specs.type = type2;
7373 /* Call grokdeclarator to figure out what type this is. */
7374 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7375 /*initialized=*/0, /*attrlist=*/NULL);
7378 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7380 /* Complete the trait expression, which may mean either processing
7381 the trait expr now or saving it for template instantiation. */
7382 return finish_trait_expr (kind, type1, type2);
7385 /* Lambdas that appear in variable initializer or default argument scope
7386 get that in their mangling, so we need to record it. We might as well
7387 use the count for function and namespace scopes as well. */
7388 static GTY(()) tree lambda_scope;
7389 static GTY(()) int lambda_count;
7390 typedef struct GTY(()) tree_int
7395 DEF_VEC_O(tree_int);
7396 DEF_VEC_ALLOC_O(tree_int,gc);
7397 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7400 start_lambda_scope (tree decl)
7404 /* Once we're inside a function, we ignore other scopes and just push
7405 the function again so that popping works properly. */
7406 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7407 decl = current_function_decl;
7408 ti.t = lambda_scope;
7409 ti.i = lambda_count;
7410 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7411 if (lambda_scope != decl)
7413 /* Don't reset the count if we're still in the same function. */
7414 lambda_scope = decl;
7420 record_lambda_scope (tree lambda)
7422 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7423 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7427 finish_lambda_scope (void)
7429 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7430 if (lambda_scope != p->t)
7432 lambda_scope = p->t;
7433 lambda_count = p->i;
7435 VEC_pop (tree_int, lambda_scope_stack);
7438 /* Parse a lambda expression.
7441 lambda-introducer lambda-declarator [opt] compound-statement
7443 Returns a representation of the expression. */
7446 cp_parser_lambda_expression (cp_parser* parser)
7448 tree lambda_expr = build_lambda_expr ();
7451 LAMBDA_EXPR_LOCATION (lambda_expr)
7452 = cp_lexer_peek_token (parser->lexer)->location;
7454 if (cp_unevaluated_operand)
7455 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7456 "lambda-expression in unevaluated context");
7458 /* We may be in the middle of deferred access check. Disable
7460 push_deferring_access_checks (dk_no_deferred);
7462 cp_parser_lambda_introducer (parser, lambda_expr);
7464 type = begin_lambda_type (lambda_expr);
7466 record_lambda_scope (lambda_expr);
7468 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7469 determine_visibility (TYPE_NAME (type));
7471 /* Now that we've started the type, add the capture fields for any
7472 explicit captures. */
7473 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7476 /* Inside the class, surrounding template-parameter-lists do not apply. */
7477 unsigned int saved_num_template_parameter_lists
7478 = parser->num_template_parameter_lists;
7480 parser->num_template_parameter_lists = 0;
7482 /* By virtue of defining a local class, a lambda expression has access to
7483 the private variables of enclosing classes. */
7485 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7487 cp_parser_lambda_body (parser, lambda_expr);
7489 /* The capture list was built up in reverse order; fix that now. */
7491 tree newlist = NULL_TREE;
7494 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7497 tree field = TREE_PURPOSE (elt);
7500 next = TREE_CHAIN (elt);
7501 TREE_CHAIN (elt) = newlist;
7504 /* Also add __ to the beginning of the field name so that code
7505 outside the lambda body can't see the captured name. We could
7506 just remove the name entirely, but this is more useful for
7508 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7509 /* The 'this' capture already starts with __. */
7512 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7513 buf[1] = buf[0] = '_';
7514 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7515 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7516 DECL_NAME (field) = get_identifier (buf);
7518 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7521 maybe_add_lambda_conv_op (type);
7523 type = finish_struct (type, /*attributes=*/NULL_TREE);
7525 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7528 pop_deferring_access_checks ();
7530 return build_lambda_object (lambda_expr);
7533 /* Parse the beginning of a lambda expression.
7536 [ lambda-capture [opt] ]
7538 LAMBDA_EXPR is the current representation of the lambda expression. */
7541 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7543 /* Need commas after the first capture. */
7546 /* Eat the leading `['. */
7547 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7549 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7550 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7551 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7552 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7553 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7554 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7556 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7558 cp_lexer_consume_token (parser->lexer);
7562 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7564 cp_token* capture_token;
7566 tree capture_init_expr;
7567 cp_id_kind idk = CP_ID_KIND_NONE;
7568 bool explicit_init_p = false;
7570 enum capture_kind_type
7575 enum capture_kind_type capture_kind = BY_COPY;
7577 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7579 error ("expected end of capture-list");
7586 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7588 /* Possibly capture `this'. */
7589 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7591 cp_lexer_consume_token (parser->lexer);
7592 add_capture (lambda_expr,
7593 /*id=*/get_identifier ("__this"),
7594 /*initializer=*/finish_this_expr(),
7595 /*by_reference_p=*/false,
7600 /* Remember whether we want to capture as a reference or not. */
7601 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7603 capture_kind = BY_REFERENCE;
7604 cp_lexer_consume_token (parser->lexer);
7607 /* Get the identifier. */
7608 capture_token = cp_lexer_peek_token (parser->lexer);
7609 capture_id = cp_parser_identifier (parser);
7611 if (capture_id == error_mark_node)
7612 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7613 delimiters, but I modified this to stop on unnested ']' as well. It
7614 was already changed to stop on unnested '}', so the
7615 "closing_parenthesis" name is no more misleading with my change. */
7617 cp_parser_skip_to_closing_parenthesis (parser,
7618 /*recovering=*/true,
7620 /*consume_paren=*/true);
7624 /* Find the initializer for this capture. */
7625 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7627 /* An explicit expression exists. */
7628 cp_lexer_consume_token (parser->lexer);
7629 pedwarn (input_location, OPT_pedantic,
7630 "ISO C++ does not allow initializers "
7631 "in lambda expression capture lists");
7632 capture_init_expr = cp_parser_assignment_expression (parser,
7635 explicit_init_p = true;
7639 const char* error_msg;
7641 /* Turn the identifier into an id-expression. */
7643 = cp_parser_lookup_name
7647 /*is_template=*/false,
7648 /*is_namespace=*/false,
7649 /*check_dependency=*/true,
7650 /*ambiguous_decls=*/NULL,
7651 capture_token->location);
7654 = finish_id_expression
7659 /*integral_constant_expression_p=*/false,
7660 /*allow_non_integral_constant_expression_p=*/false,
7661 /*non_integral_constant_expression_p=*/NULL,
7662 /*template_p=*/false,
7664 /*address_p=*/false,
7665 /*template_arg_p=*/false,
7667 capture_token->location);
7670 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7672 = unqualified_name_lookup_error (capture_init_expr);
7674 add_capture (lambda_expr,
7677 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7681 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7684 /* Parse the (optional) middle of a lambda expression.
7687 ( parameter-declaration-clause [opt] )
7688 attribute-specifier [opt]
7690 exception-specification [opt]
7691 lambda-return-type-clause [opt]
7693 LAMBDA_EXPR is the current representation of the lambda expression. */
7696 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7698 /* 5.1.1.4 of the standard says:
7699 If a lambda-expression does not include a lambda-declarator, it is as if
7700 the lambda-declarator were ().
7701 This means an empty parameter list, no attributes, and no exception
7703 tree param_list = void_list_node;
7704 tree attributes = NULL_TREE;
7705 tree exception_spec = NULL_TREE;
7708 /* The lambda-declarator is optional, but must begin with an opening
7709 parenthesis if present. */
7710 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7712 cp_lexer_consume_token (parser->lexer);
7714 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7716 /* Parse parameters. */
7717 param_list = cp_parser_parameter_declaration_clause (parser);
7719 /* Default arguments shall not be specified in the
7720 parameter-declaration-clause of a lambda-declarator. */
7721 for (t = param_list; t; t = TREE_CHAIN (t))
7722 if (TREE_PURPOSE (t))
7723 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7724 "default argument specified for lambda parameter");
7726 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7728 attributes = cp_parser_attributes_opt (parser);
7730 /* Parse optional `mutable' keyword. */
7731 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7733 cp_lexer_consume_token (parser->lexer);
7734 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7737 /* Parse optional exception specification. */
7738 exception_spec = cp_parser_exception_specification_opt (parser);
7740 /* Parse optional trailing return type. */
7741 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7743 cp_lexer_consume_token (parser->lexer);
7744 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7747 /* The function parameters must be in scope all the way until after the
7748 trailing-return-type in case of decltype. */
7749 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7750 pop_binding (DECL_NAME (t), t);
7755 /* Create the function call operator.
7757 Messing with declarators like this is no uglier than building up the
7758 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7761 cp_decl_specifier_seq return_type_specs;
7762 cp_declarator* declarator;
7767 clear_decl_specs (&return_type_specs);
7768 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7769 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7771 /* Maybe we will deduce the return type later, but we can use void
7772 as a placeholder return type anyways. */
7773 return_type_specs.type = void_type_node;
7775 p = obstack_alloc (&declarator_obstack, 0);
7777 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7780 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7781 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7782 declarator = make_call_declarator (declarator, param_list, quals,
7784 /*late_return_type=*/NULL_TREE);
7785 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7787 fco = grokmethod (&return_type_specs,
7790 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7791 DECL_ARTIFICIAL (fco) = 1;
7793 finish_member_declaration (fco);
7795 obstack_free (&declarator_obstack, p);
7799 /* Parse the body of a lambda expression, which is simply
7803 but which requires special handling.
7804 LAMBDA_EXPR is the current representation of the lambda expression. */
7807 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7809 bool nested = (current_function_decl != NULL_TREE);
7811 push_function_context ();
7813 /* Finish the function call operator
7815 + late_parsing_for_member
7816 + function_definition_after_declarator
7817 + ctor_initializer_opt_and_function_body */
7819 tree fco = lambda_function (lambda_expr);
7823 /* Let the front end know that we are going to be defining this
7825 start_preparsed_function (fco,
7827 SF_PRE_PARSED | SF_INCLASS_INLINE);
7829 start_lambda_scope (fco);
7830 body = begin_function_body ();
7832 /* 5.1.1.4 of the standard says:
7833 If a lambda-expression does not include a trailing-return-type, it
7834 is as if the trailing-return-type denotes the following type:
7835 * if the compound-statement is of the form
7836 { return attribute-specifier [opt] expression ; }
7837 the type of the returned expression after lvalue-to-rvalue
7838 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7839 (_conv.array_ 4.2), and function-to-pointer conversion
7841 * otherwise, void. */
7843 /* In a lambda that has neither a lambda-return-type-clause
7844 nor a deducible form, errors should be reported for return statements
7845 in the body. Since we used void as the placeholder return type, parsing
7846 the body as usual will give such desired behavior. */
7847 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7848 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7849 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7850 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7853 tree expr = NULL_TREE;
7854 cp_id_kind idk = CP_ID_KIND_NONE;
7856 /* Parse tentatively in case there's more after the initial return
7858 cp_parser_parse_tentatively (parser);
7860 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7861 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7863 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7865 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7866 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7868 if (cp_parser_parse_definitely (parser))
7870 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7872 compound_stmt = begin_compound_stmt (0);
7873 /* Will get error here if type not deduced yet. */
7874 finish_return_stmt (expr);
7875 finish_compound_stmt (compound_stmt);
7883 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7884 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7885 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7886 cp_parser_compound_stmt does not pass it. */
7887 cp_parser_function_body (parser);
7888 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7891 finish_function_body (body);
7892 finish_lambda_scope ();
7894 /* Finish the function and generate code for it if necessary. */
7895 expand_or_defer_fn (finish_function (/*inline*/2));
7899 pop_function_context();
7902 /* Statements [gram.stmt.stmt] */
7904 /* Parse a statement.
7908 expression-statement
7913 declaration-statement
7916 IN_COMPOUND is true when the statement is nested inside a
7917 cp_parser_compound_statement; this matters for certain pragmas.
7919 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7920 is a (possibly labeled) if statement which is not enclosed in braces
7921 and has an else clause. This is used to implement -Wparentheses. */
7924 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7925 bool in_compound, bool *if_p)
7929 location_t statement_location;
7934 /* There is no statement yet. */
7935 statement = NULL_TREE;
7936 /* Peek at the next token. */
7937 token = cp_lexer_peek_token (parser->lexer);
7938 /* Remember the location of the first token in the statement. */
7939 statement_location = token->location;
7940 /* If this is a keyword, then that will often determine what kind of
7941 statement we have. */
7942 if (token->type == CPP_KEYWORD)
7944 enum rid keyword = token->keyword;
7950 /* Looks like a labeled-statement with a case label.
7951 Parse the label, and then use tail recursion to parse
7953 cp_parser_label_for_labeled_statement (parser);
7958 statement = cp_parser_selection_statement (parser, if_p);
7964 statement = cp_parser_iteration_statement (parser);
7971 statement = cp_parser_jump_statement (parser);
7974 /* Objective-C++ exception-handling constructs. */
7977 case RID_AT_FINALLY:
7978 case RID_AT_SYNCHRONIZED:
7980 statement = cp_parser_objc_statement (parser);
7984 statement = cp_parser_try_block (parser);
7988 /* This must be a namespace alias definition. */
7989 cp_parser_declaration_statement (parser);
7993 /* It might be a keyword like `int' that can start a
7994 declaration-statement. */
7998 else if (token->type == CPP_NAME)
8000 /* If the next token is a `:', then we are looking at a
8001 labeled-statement. */
8002 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8003 if (token->type == CPP_COLON)
8005 /* Looks like a labeled-statement with an ordinary label.
8006 Parse the label, and then use tail recursion to parse
8008 cp_parser_label_for_labeled_statement (parser);
8012 /* Anything that starts with a `{' must be a compound-statement. */
8013 else if (token->type == CPP_OPEN_BRACE)
8014 statement = cp_parser_compound_statement (parser, NULL, false);
8015 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8016 a statement all its own. */
8017 else if (token->type == CPP_PRAGMA)
8019 /* Only certain OpenMP pragmas are attached to statements, and thus
8020 are considered statements themselves. All others are not. In
8021 the context of a compound, accept the pragma as a "statement" and
8022 return so that we can check for a close brace. Otherwise we
8023 require a real statement and must go back and read one. */
8025 cp_parser_pragma (parser, pragma_compound);
8026 else if (!cp_parser_pragma (parser, pragma_stmt))
8030 else if (token->type == CPP_EOF)
8032 cp_parser_error (parser, "expected statement");
8036 /* Everything else must be a declaration-statement or an
8037 expression-statement. Try for the declaration-statement
8038 first, unless we are looking at a `;', in which case we know that
8039 we have an expression-statement. */
8042 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8044 cp_parser_parse_tentatively (parser);
8045 /* Try to parse the declaration-statement. */
8046 cp_parser_declaration_statement (parser);
8047 /* If that worked, we're done. */
8048 if (cp_parser_parse_definitely (parser))
8051 /* Look for an expression-statement instead. */
8052 statement = cp_parser_expression_statement (parser, in_statement_expr);
8055 /* Set the line number for the statement. */
8056 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8057 SET_EXPR_LOCATION (statement, statement_location);
8060 /* Parse the label for a labeled-statement, i.e.
8063 case constant-expression :
8067 case constant-expression ... constant-expression : statement
8069 When a label is parsed without errors, the label is added to the
8070 parse tree by the finish_* functions, so this function doesn't
8071 have to return the label. */
8074 cp_parser_label_for_labeled_statement (cp_parser* parser)
8077 tree label = NULL_TREE;
8079 /* The next token should be an identifier. */
8080 token = cp_lexer_peek_token (parser->lexer);
8081 if (token->type != CPP_NAME
8082 && token->type != CPP_KEYWORD)
8084 cp_parser_error (parser, "expected labeled-statement");
8088 switch (token->keyword)
8095 /* Consume the `case' token. */
8096 cp_lexer_consume_token (parser->lexer);
8097 /* Parse the constant-expression. */
8098 expr = cp_parser_constant_expression (parser,
8099 /*allow_non_constant_p=*/false,
8102 ellipsis = cp_lexer_peek_token (parser->lexer);
8103 if (ellipsis->type == CPP_ELLIPSIS)
8105 /* Consume the `...' token. */
8106 cp_lexer_consume_token (parser->lexer);
8108 cp_parser_constant_expression (parser,
8109 /*allow_non_constant_p=*/false,
8111 /* We don't need to emit warnings here, as the common code
8112 will do this for us. */
8115 expr_hi = NULL_TREE;
8117 if (parser->in_switch_statement_p)
8118 finish_case_label (token->location, expr, expr_hi);
8120 error_at (token->location,
8121 "case label %qE not within a switch statement",
8127 /* Consume the `default' token. */
8128 cp_lexer_consume_token (parser->lexer);
8130 if (parser->in_switch_statement_p)
8131 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8133 error_at (token->location, "case label not within a switch statement");
8137 /* Anything else must be an ordinary label. */
8138 label = finish_label_stmt (cp_parser_identifier (parser));
8142 /* Require the `:' token. */
8143 cp_parser_require (parser, CPP_COLON, RT_COLON);
8145 /* An ordinary label may optionally be followed by attributes.
8146 However, this is only permitted if the attributes are then
8147 followed by a semicolon. This is because, for backward
8148 compatibility, when parsing
8149 lab: __attribute__ ((unused)) int i;
8150 we want the attribute to attach to "i", not "lab". */
8151 if (label != NULL_TREE
8152 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8156 cp_parser_parse_tentatively (parser);
8157 attrs = cp_parser_attributes_opt (parser);
8158 if (attrs == NULL_TREE
8159 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8160 cp_parser_abort_tentative_parse (parser);
8161 else if (!cp_parser_parse_definitely (parser))
8164 cplus_decl_attributes (&label, attrs, 0);
8168 /* Parse an expression-statement.
8170 expression-statement:
8173 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8174 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8175 indicates whether this expression-statement is part of an
8176 expression statement. */
8179 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8181 tree statement = NULL_TREE;
8182 cp_token *token = cp_lexer_peek_token (parser->lexer);
8184 /* If the next token is a ';', then there is no expression
8186 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8187 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8189 /* Give a helpful message for "A<T>::type t;" and the like. */
8190 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8191 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8193 if (TREE_CODE (statement) == SCOPE_REF)
8194 error_at (token->location, "need %<typename%> before %qE because "
8195 "%qT is a dependent scope",
8196 statement, TREE_OPERAND (statement, 0));
8197 else if (is_overloaded_fn (statement)
8198 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8201 tree fn = get_first_fn (statement);
8202 error_at (token->location,
8203 "%<%T::%D%> names the constructor, not the type",
8204 DECL_CONTEXT (fn), DECL_NAME (fn));
8208 /* Consume the final `;'. */
8209 cp_parser_consume_semicolon_at_end_of_statement (parser);
8211 if (in_statement_expr
8212 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8213 /* This is the final expression statement of a statement
8215 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8217 statement = finish_expr_stmt (statement);
8224 /* Parse a compound-statement.
8227 { statement-seq [opt] }
8232 { label-declaration-seq [opt] statement-seq [opt] }
8234 label-declaration-seq:
8236 label-declaration-seq label-declaration
8238 Returns a tree representing the statement. */
8241 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8246 /* Consume the `{'. */
8247 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8248 return error_mark_node;
8249 /* Begin the compound-statement. */
8250 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8251 /* If the next keyword is `__label__' we have a label declaration. */
8252 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8253 cp_parser_label_declaration (parser);
8254 /* Parse an (optional) statement-seq. */
8255 cp_parser_statement_seq_opt (parser, in_statement_expr);
8256 /* Finish the compound-statement. */
8257 finish_compound_stmt (compound_stmt);
8258 /* Consume the `}'. */
8259 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8261 return compound_stmt;
8264 /* Parse an (optional) statement-seq.
8268 statement-seq [opt] statement */
8271 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8273 /* Scan statements until there aren't any more. */
8276 cp_token *token = cp_lexer_peek_token (parser->lexer);
8278 /* If we're looking at a `}', then we've run out of statements. */
8279 if (token->type == CPP_CLOSE_BRACE
8280 || token->type == CPP_EOF
8281 || token->type == CPP_PRAGMA_EOL)
8284 /* If we are in a compound statement and find 'else' then
8285 something went wrong. */
8286 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8288 if (parser->in_statement & IN_IF_STMT)
8292 token = cp_lexer_consume_token (parser->lexer);
8293 error_at (token->location, "%<else%> without a previous %<if%>");
8297 /* Parse the statement. */
8298 cp_parser_statement (parser, in_statement_expr, true, NULL);
8302 /* Parse a selection-statement.
8304 selection-statement:
8305 if ( condition ) statement
8306 if ( condition ) statement else statement
8307 switch ( condition ) statement
8309 Returns the new IF_STMT or SWITCH_STMT.
8311 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8312 is a (possibly labeled) if statement which is not enclosed in
8313 braces and has an else clause. This is used to implement
8317 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8325 /* Peek at the next token. */
8326 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8328 /* See what kind of keyword it is. */
8329 keyword = token->keyword;
8338 /* Look for the `('. */
8339 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8341 cp_parser_skip_to_end_of_statement (parser);
8342 return error_mark_node;
8345 /* Begin the selection-statement. */
8346 if (keyword == RID_IF)
8347 statement = begin_if_stmt ();
8349 statement = begin_switch_stmt ();
8351 /* Parse the condition. */
8352 condition = cp_parser_condition (parser);
8353 /* Look for the `)'. */
8354 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8355 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8356 /*consume_paren=*/true);
8358 if (keyword == RID_IF)
8361 unsigned char in_statement;
8363 /* Add the condition. */
8364 finish_if_stmt_cond (condition, statement);
8366 /* Parse the then-clause. */
8367 in_statement = parser->in_statement;
8368 parser->in_statement |= IN_IF_STMT;
8369 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8371 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8372 add_stmt (build_empty_stmt (loc));
8373 cp_lexer_consume_token (parser->lexer);
8374 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8375 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8376 "empty body in an %<if%> statement");
8380 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8381 parser->in_statement = in_statement;
8383 finish_then_clause (statement);
8385 /* If the next token is `else', parse the else-clause. */
8386 if (cp_lexer_next_token_is_keyword (parser->lexer,
8389 /* Consume the `else' keyword. */
8390 cp_lexer_consume_token (parser->lexer);
8391 begin_else_clause (statement);
8392 /* Parse the else-clause. */
8393 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8396 loc = cp_lexer_peek_token (parser->lexer)->location;
8398 OPT_Wempty_body, "suggest braces around "
8399 "empty body in an %<else%> statement");
8400 add_stmt (build_empty_stmt (loc));
8401 cp_lexer_consume_token (parser->lexer);
8404 cp_parser_implicitly_scoped_statement (parser, NULL);
8406 finish_else_clause (statement);
8408 /* If we are currently parsing a then-clause, then
8409 IF_P will not be NULL. We set it to true to
8410 indicate that this if statement has an else clause.
8411 This may trigger the Wparentheses warning below
8412 when we get back up to the parent if statement. */
8418 /* This if statement does not have an else clause. If
8419 NESTED_IF is true, then the then-clause is an if
8420 statement which does have an else clause. We warn
8421 about the potential ambiguity. */
8423 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8424 "suggest explicit braces to avoid ambiguous"
8428 /* Now we're all done with the if-statement. */
8429 finish_if_stmt (statement);
8433 bool in_switch_statement_p;
8434 unsigned char in_statement;
8436 /* Add the condition. */
8437 finish_switch_cond (condition, statement);
8439 /* Parse the body of the switch-statement. */
8440 in_switch_statement_p = parser->in_switch_statement_p;
8441 in_statement = parser->in_statement;
8442 parser->in_switch_statement_p = true;
8443 parser->in_statement |= IN_SWITCH_STMT;
8444 cp_parser_implicitly_scoped_statement (parser, NULL);
8445 parser->in_switch_statement_p = in_switch_statement_p;
8446 parser->in_statement = in_statement;
8448 /* Now we're all done with the switch-statement. */
8449 finish_switch_stmt (statement);
8457 cp_parser_error (parser, "expected selection-statement");
8458 return error_mark_node;
8462 /* Parse a condition.
8466 type-specifier-seq declarator = initializer-clause
8467 type-specifier-seq declarator braced-init-list
8472 type-specifier-seq declarator asm-specification [opt]
8473 attributes [opt] = assignment-expression
8475 Returns the expression that should be tested. */
8478 cp_parser_condition (cp_parser* parser)
8480 cp_decl_specifier_seq type_specifiers;
8481 const char *saved_message;
8483 /* Try the declaration first. */
8484 cp_parser_parse_tentatively (parser);
8485 /* New types are not allowed in the type-specifier-seq for a
8487 saved_message = parser->type_definition_forbidden_message;
8488 parser->type_definition_forbidden_message
8489 = G_("types may not be defined in conditions");
8490 /* Parse the type-specifier-seq. */
8491 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8492 /*is_trailing_return=*/false,
8494 /* Restore the saved message. */
8495 parser->type_definition_forbidden_message = saved_message;
8496 /* If all is well, we might be looking at a declaration. */
8497 if (!cp_parser_error_occurred (parser))
8500 tree asm_specification;
8502 cp_declarator *declarator;
8503 tree initializer = NULL_TREE;
8505 /* Parse the declarator. */
8506 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8507 /*ctor_dtor_or_conv_p=*/NULL,
8508 /*parenthesized_p=*/NULL,
8509 /*member_p=*/false);
8510 /* Parse the attributes. */
8511 attributes = cp_parser_attributes_opt (parser);
8512 /* Parse the asm-specification. */
8513 asm_specification = cp_parser_asm_specification_opt (parser);
8514 /* If the next token is not an `=' or '{', then we might still be
8515 looking at an expression. For example:
8519 looks like a decl-specifier-seq and a declarator -- but then
8520 there is no `=', so this is an expression. */
8521 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8522 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8523 cp_parser_simulate_error (parser);
8525 /* If we did see an `=' or '{', then we are looking at a declaration
8527 if (cp_parser_parse_definitely (parser))
8530 bool non_constant_p;
8531 bool flags = LOOKUP_ONLYCONVERTING;
8533 /* Create the declaration. */
8534 decl = start_decl (declarator, &type_specifiers,
8535 /*initialized_p=*/true,
8536 attributes, /*prefix_attributes=*/NULL_TREE,
8539 /* Parse the initializer. */
8540 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8542 initializer = cp_parser_braced_list (parser, &non_constant_p);
8543 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8548 /* Consume the `='. */
8549 cp_parser_require (parser, CPP_EQ, RT_EQ);
8550 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8552 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8553 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8555 if (!non_constant_p)
8556 initializer = fold_non_dependent_expr (initializer);
8558 /* Process the initializer. */
8559 cp_finish_decl (decl,
8560 initializer, !non_constant_p,
8565 pop_scope (pushed_scope);
8567 return convert_from_reference (decl);
8570 /* If we didn't even get past the declarator successfully, we are
8571 definitely not looking at a declaration. */
8573 cp_parser_abort_tentative_parse (parser);
8575 /* Otherwise, we are looking at an expression. */
8576 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8579 /* Parse an iteration-statement.
8581 iteration-statement:
8582 while ( condition ) statement
8583 do statement while ( expression ) ;
8584 for ( for-init-statement condition [opt] ; expression [opt] )
8587 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8590 cp_parser_iteration_statement (cp_parser* parser)
8595 unsigned char in_statement;
8597 /* Peek at the next token. */
8598 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8600 return error_mark_node;
8602 /* Remember whether or not we are already within an iteration
8604 in_statement = parser->in_statement;
8606 /* See what kind of keyword it is. */
8607 keyword = token->keyword;
8614 /* Begin the while-statement. */
8615 statement = begin_while_stmt ();
8616 /* Look for the `('. */
8617 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8618 /* Parse the condition. */
8619 condition = cp_parser_condition (parser);
8620 finish_while_stmt_cond (condition, statement);
8621 /* Look for the `)'. */
8622 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8623 /* Parse the dependent statement. */
8624 parser->in_statement = IN_ITERATION_STMT;
8625 cp_parser_already_scoped_statement (parser);
8626 parser->in_statement = in_statement;
8627 /* We're done with the while-statement. */
8628 finish_while_stmt (statement);
8636 /* Begin the do-statement. */
8637 statement = begin_do_stmt ();
8638 /* Parse the body of the do-statement. */
8639 parser->in_statement = IN_ITERATION_STMT;
8640 cp_parser_implicitly_scoped_statement (parser, NULL);
8641 parser->in_statement = in_statement;
8642 finish_do_body (statement);
8643 /* Look for the `while' keyword. */
8644 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8645 /* Look for the `('. */
8646 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8647 /* Parse the expression. */
8648 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8649 /* We're done with the do-statement. */
8650 finish_do_stmt (expression, statement);
8651 /* Look for the `)'. */
8652 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8653 /* Look for the `;'. */
8654 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8660 tree condition = NULL_TREE;
8661 tree expression = NULL_TREE;
8663 /* Begin the for-statement. */
8664 statement = begin_for_stmt ();
8665 /* Look for the `('. */
8666 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8667 /* Parse the initialization. */
8668 cp_parser_for_init_statement (parser);
8669 finish_for_init_stmt (statement);
8671 /* If there's a condition, process it. */
8672 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8673 condition = cp_parser_condition (parser);
8674 finish_for_cond (condition, statement);
8675 /* Look for the `;'. */
8676 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8678 /* If there's an expression, process it. */
8679 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8680 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8681 finish_for_expr (expression, statement);
8682 /* Look for the `)'. */
8683 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8685 /* Parse the body of the for-statement. */
8686 parser->in_statement = IN_ITERATION_STMT;
8687 cp_parser_already_scoped_statement (parser);
8688 parser->in_statement = in_statement;
8690 /* We're done with the for-statement. */
8691 finish_for_stmt (statement);
8696 cp_parser_error (parser, "expected iteration-statement");
8697 statement = error_mark_node;
8704 /* Parse a for-init-statement.
8707 expression-statement
8708 simple-declaration */
8711 cp_parser_for_init_statement (cp_parser* parser)
8713 /* If the next token is a `;', then we have an empty
8714 expression-statement. Grammatically, this is also a
8715 simple-declaration, but an invalid one, because it does not
8716 declare anything. Therefore, if we did not handle this case
8717 specially, we would issue an error message about an invalid
8719 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8721 /* We're going to speculatively look for a declaration, falling back
8722 to an expression, if necessary. */
8723 cp_parser_parse_tentatively (parser);
8724 /* Parse the declaration. */
8725 cp_parser_simple_declaration (parser,
8726 /*function_definition_allowed_p=*/false);
8727 /* If the tentative parse failed, then we shall need to look for an
8728 expression-statement. */
8729 if (cp_parser_parse_definitely (parser))
8733 cp_parser_expression_statement (parser, NULL_TREE);
8736 /* Parse a jump-statement.
8741 return expression [opt] ;
8742 return braced-init-list ;
8750 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8753 cp_parser_jump_statement (cp_parser* parser)
8755 tree statement = error_mark_node;
8758 unsigned char in_statement;
8760 /* Peek at the next token. */
8761 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
8763 return error_mark_node;
8765 /* See what kind of keyword it is. */
8766 keyword = token->keyword;
8770 in_statement = parser->in_statement & ~IN_IF_STMT;
8771 switch (in_statement)
8774 error_at (token->location, "break statement not within loop or switch");
8777 gcc_assert ((in_statement & IN_SWITCH_STMT)
8778 || in_statement == IN_ITERATION_STMT);
8779 statement = finish_break_stmt ();
8782 error_at (token->location, "invalid exit from OpenMP structured block");
8785 error_at (token->location, "break statement used with OpenMP for loop");
8788 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8792 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8795 error_at (token->location, "continue statement not within a loop");
8797 case IN_ITERATION_STMT:
8799 statement = finish_continue_stmt ();
8802 error_at (token->location, "invalid exit from OpenMP structured block");
8807 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8813 bool expr_non_constant_p;
8815 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8817 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8818 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8820 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8821 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8823 /* If the next token is a `;', then there is no
8826 /* Build the return-statement. */
8827 statement = finish_return_stmt (expr);
8828 /* Look for the final `;'. */
8829 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8834 /* Create the goto-statement. */
8835 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8837 /* Issue a warning about this use of a GNU extension. */
8838 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8839 /* Consume the '*' token. */
8840 cp_lexer_consume_token (parser->lexer);
8841 /* Parse the dependent expression. */
8842 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8845 finish_goto_stmt (cp_parser_identifier (parser));
8846 /* Look for the final `;'. */
8847 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8851 cp_parser_error (parser, "expected jump-statement");
8858 /* Parse a declaration-statement.
8860 declaration-statement:
8861 block-declaration */
8864 cp_parser_declaration_statement (cp_parser* parser)
8868 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8869 p = obstack_alloc (&declarator_obstack, 0);
8871 /* Parse the block-declaration. */
8872 cp_parser_block_declaration (parser, /*statement_p=*/true);
8874 /* Free any declarators allocated. */
8875 obstack_free (&declarator_obstack, p);
8877 /* Finish off the statement. */
8881 /* Some dependent statements (like `if (cond) statement'), are
8882 implicitly in their own scope. In other words, if the statement is
8883 a single statement (as opposed to a compound-statement), it is
8884 none-the-less treated as if it were enclosed in braces. Any
8885 declarations appearing in the dependent statement are out of scope
8886 after control passes that point. This function parses a statement,
8887 but ensures that is in its own scope, even if it is not a
8890 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8891 is a (possibly labeled) if statement which is not enclosed in
8892 braces and has an else clause. This is used to implement
8895 Returns the new statement. */
8898 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8905 /* Mark if () ; with a special NOP_EXPR. */
8906 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8908 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8909 cp_lexer_consume_token (parser->lexer);
8910 statement = add_stmt (build_empty_stmt (loc));
8912 /* if a compound is opened, we simply parse the statement directly. */
8913 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8914 statement = cp_parser_compound_statement (parser, NULL, false);
8915 /* If the token is not a `{', then we must take special action. */
8918 /* Create a compound-statement. */
8919 statement = begin_compound_stmt (0);
8920 /* Parse the dependent-statement. */
8921 cp_parser_statement (parser, NULL_TREE, false, if_p);
8922 /* Finish the dummy compound-statement. */
8923 finish_compound_stmt (statement);
8926 /* Return the statement. */
8930 /* For some dependent statements (like `while (cond) statement'), we
8931 have already created a scope. Therefore, even if the dependent
8932 statement is a compound-statement, we do not want to create another
8936 cp_parser_already_scoped_statement (cp_parser* parser)
8938 /* If the token is a `{', then we must take special action. */
8939 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8940 cp_parser_statement (parser, NULL_TREE, false, NULL);
8943 /* Avoid calling cp_parser_compound_statement, so that we
8944 don't create a new scope. Do everything else by hand. */
8945 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
8946 /* If the next keyword is `__label__' we have a label declaration. */
8947 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8948 cp_parser_label_declaration (parser);
8949 /* Parse an (optional) statement-seq. */
8950 cp_parser_statement_seq_opt (parser, NULL_TREE);
8951 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8955 /* Declarations [gram.dcl.dcl] */
8957 /* Parse an optional declaration-sequence.
8961 declaration-seq declaration */
8964 cp_parser_declaration_seq_opt (cp_parser* parser)
8970 token = cp_lexer_peek_token (parser->lexer);
8972 if (token->type == CPP_CLOSE_BRACE
8973 || token->type == CPP_EOF
8974 || token->type == CPP_PRAGMA_EOL)
8977 if (token->type == CPP_SEMICOLON)
8979 /* A declaration consisting of a single semicolon is
8980 invalid. Allow it unless we're being pedantic. */
8981 cp_lexer_consume_token (parser->lexer);
8982 if (!in_system_header)
8983 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8987 /* If we're entering or exiting a region that's implicitly
8988 extern "C", modify the lang context appropriately. */
8989 if (!parser->implicit_extern_c && token->implicit_extern_c)
8991 push_lang_context (lang_name_c);
8992 parser->implicit_extern_c = true;
8994 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8996 pop_lang_context ();
8997 parser->implicit_extern_c = false;
9000 if (token->type == CPP_PRAGMA)
9002 /* A top-level declaration can consist solely of a #pragma.
9003 A nested declaration cannot, so this is done here and not
9004 in cp_parser_declaration. (A #pragma at block scope is
9005 handled in cp_parser_statement.) */
9006 cp_parser_pragma (parser, pragma_external);
9010 /* Parse the declaration itself. */
9011 cp_parser_declaration (parser);
9015 /* Parse a declaration.
9020 template-declaration
9021 explicit-instantiation
9022 explicit-specialization
9023 linkage-specification
9024 namespace-definition
9029 __extension__ declaration */
9032 cp_parser_declaration (cp_parser* parser)
9039 /* Check for the `__extension__' keyword. */
9040 if (cp_parser_extension_opt (parser, &saved_pedantic))
9042 /* Parse the qualified declaration. */
9043 cp_parser_declaration (parser);
9044 /* Restore the PEDANTIC flag. */
9045 pedantic = saved_pedantic;
9050 /* Try to figure out what kind of declaration is present. */
9051 token1 = *cp_lexer_peek_token (parser->lexer);
9053 if (token1.type != CPP_EOF)
9054 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9057 token2.type = CPP_EOF;
9058 token2.keyword = RID_MAX;
9061 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9062 p = obstack_alloc (&declarator_obstack, 0);
9064 /* If the next token is `extern' and the following token is a string
9065 literal, then we have a linkage specification. */
9066 if (token1.keyword == RID_EXTERN
9067 && cp_parser_is_string_literal (&token2))
9068 cp_parser_linkage_specification (parser);
9069 /* If the next token is `template', then we have either a template
9070 declaration, an explicit instantiation, or an explicit
9072 else if (token1.keyword == RID_TEMPLATE)
9074 /* `template <>' indicates a template specialization. */
9075 if (token2.type == CPP_LESS
9076 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9077 cp_parser_explicit_specialization (parser);
9078 /* `template <' indicates a template declaration. */
9079 else if (token2.type == CPP_LESS)
9080 cp_parser_template_declaration (parser, /*member_p=*/false);
9081 /* Anything else must be an explicit instantiation. */
9083 cp_parser_explicit_instantiation (parser);
9085 /* If the next token is `export', then we have a template
9087 else if (token1.keyword == RID_EXPORT)
9088 cp_parser_template_declaration (parser, /*member_p=*/false);
9089 /* If the next token is `extern', 'static' or 'inline' and the one
9090 after that is `template', we have a GNU extended explicit
9091 instantiation directive. */
9092 else if (cp_parser_allow_gnu_extensions_p (parser)
9093 && (token1.keyword == RID_EXTERN
9094 || token1.keyword == RID_STATIC
9095 || token1.keyword == RID_INLINE)
9096 && token2.keyword == RID_TEMPLATE)
9097 cp_parser_explicit_instantiation (parser);
9098 /* If the next token is `namespace', check for a named or unnamed
9099 namespace definition. */
9100 else if (token1.keyword == RID_NAMESPACE
9101 && (/* A named namespace definition. */
9102 (token2.type == CPP_NAME
9103 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9105 /* An unnamed namespace definition. */
9106 || token2.type == CPP_OPEN_BRACE
9107 || token2.keyword == RID_ATTRIBUTE))
9108 cp_parser_namespace_definition (parser);
9109 /* An inline (associated) namespace definition. */
9110 else if (token1.keyword == RID_INLINE
9111 && token2.keyword == RID_NAMESPACE)
9112 cp_parser_namespace_definition (parser);
9113 /* Objective-C++ declaration/definition. */
9114 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9115 cp_parser_objc_declaration (parser);
9116 /* We must have either a block declaration or a function
9119 /* Try to parse a block-declaration, or a function-definition. */
9120 cp_parser_block_declaration (parser, /*statement_p=*/false);
9122 /* Free any declarators allocated. */
9123 obstack_free (&declarator_obstack, p);
9126 /* Parse a block-declaration.
9131 namespace-alias-definition
9138 __extension__ block-declaration
9143 static_assert-declaration
9145 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9146 part of a declaration-statement. */
9149 cp_parser_block_declaration (cp_parser *parser,
9155 /* Check for the `__extension__' keyword. */
9156 if (cp_parser_extension_opt (parser, &saved_pedantic))
9158 /* Parse the qualified declaration. */
9159 cp_parser_block_declaration (parser, statement_p);
9160 /* Restore the PEDANTIC flag. */
9161 pedantic = saved_pedantic;
9166 /* Peek at the next token to figure out which kind of declaration is
9168 token1 = cp_lexer_peek_token (parser->lexer);
9170 /* If the next keyword is `asm', we have an asm-definition. */
9171 if (token1->keyword == RID_ASM)
9174 cp_parser_commit_to_tentative_parse (parser);
9175 cp_parser_asm_definition (parser);
9177 /* If the next keyword is `namespace', we have a
9178 namespace-alias-definition. */
9179 else if (token1->keyword == RID_NAMESPACE)
9180 cp_parser_namespace_alias_definition (parser);
9181 /* If the next keyword is `using', we have either a
9182 using-declaration or a using-directive. */
9183 else if (token1->keyword == RID_USING)
9188 cp_parser_commit_to_tentative_parse (parser);
9189 /* If the token after `using' is `namespace', then we have a
9191 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9192 if (token2->keyword == RID_NAMESPACE)
9193 cp_parser_using_directive (parser);
9194 /* Otherwise, it's a using-declaration. */
9196 cp_parser_using_declaration (parser,
9197 /*access_declaration_p=*/false);
9199 /* If the next keyword is `__label__' we have a misplaced label
9201 else if (token1->keyword == RID_LABEL)
9203 cp_lexer_consume_token (parser->lexer);
9204 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9205 cp_parser_skip_to_end_of_statement (parser);
9206 /* If the next token is now a `;', consume it. */
9207 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9208 cp_lexer_consume_token (parser->lexer);
9210 /* If the next token is `static_assert' we have a static assertion. */
9211 else if (token1->keyword == RID_STATIC_ASSERT)
9212 cp_parser_static_assert (parser, /*member_p=*/false);
9213 /* Anything else must be a simple-declaration. */
9215 cp_parser_simple_declaration (parser, !statement_p);
9218 /* Parse a simple-declaration.
9221 decl-specifier-seq [opt] init-declarator-list [opt] ;
9223 init-declarator-list:
9225 init-declarator-list , init-declarator
9227 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9228 function-definition as a simple-declaration. */
9231 cp_parser_simple_declaration (cp_parser* parser,
9232 bool function_definition_allowed_p)
9234 cp_decl_specifier_seq decl_specifiers;
9235 int declares_class_or_enum;
9236 bool saw_declarator;
9238 /* Defer access checks until we know what is being declared; the
9239 checks for names appearing in the decl-specifier-seq should be
9240 done as if we were in the scope of the thing being declared. */
9241 push_deferring_access_checks (dk_deferred);
9243 /* Parse the decl-specifier-seq. We have to keep track of whether
9244 or not the decl-specifier-seq declares a named class or
9245 enumeration type, since that is the only case in which the
9246 init-declarator-list is allowed to be empty.
9250 In a simple-declaration, the optional init-declarator-list can be
9251 omitted only when declaring a class or enumeration, that is when
9252 the decl-specifier-seq contains either a class-specifier, an
9253 elaborated-type-specifier, or an enum-specifier. */
9254 cp_parser_decl_specifier_seq (parser,
9255 CP_PARSER_FLAGS_OPTIONAL,
9257 &declares_class_or_enum);
9258 /* We no longer need to defer access checks. */
9259 stop_deferring_access_checks ();
9261 /* In a block scope, a valid declaration must always have a
9262 decl-specifier-seq. By not trying to parse declarators, we can
9263 resolve the declaration/expression ambiguity more quickly. */
9264 if (!function_definition_allowed_p
9265 && !decl_specifiers.any_specifiers_p)
9267 cp_parser_error (parser, "expected declaration");
9271 /* If the next two tokens are both identifiers, the code is
9272 erroneous. The usual cause of this situation is code like:
9276 where "T" should name a type -- but does not. */
9277 if (!decl_specifiers.any_type_specifiers_p
9278 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9280 /* If parsing tentatively, we should commit; we really are
9281 looking at a declaration. */
9282 cp_parser_commit_to_tentative_parse (parser);
9287 /* If we have seen at least one decl-specifier, and the next token
9288 is not a parenthesis, then we must be looking at a declaration.
9289 (After "int (" we might be looking at a functional cast.) */
9290 if (decl_specifiers.any_specifiers_p
9291 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9292 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9293 && !cp_parser_error_occurred (parser))
9294 cp_parser_commit_to_tentative_parse (parser);
9296 /* Keep going until we hit the `;' at the end of the simple
9298 saw_declarator = false;
9299 while (cp_lexer_next_token_is_not (parser->lexer,
9303 bool function_definition_p;
9308 /* If we are processing next declarator, coma is expected */
9309 token = cp_lexer_peek_token (parser->lexer);
9310 gcc_assert (token->type == CPP_COMMA);
9311 cp_lexer_consume_token (parser->lexer);
9314 saw_declarator = true;
9316 /* Parse the init-declarator. */
9317 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9319 function_definition_allowed_p,
9321 declares_class_or_enum,
9322 &function_definition_p);
9323 /* If an error occurred while parsing tentatively, exit quickly.
9324 (That usually happens when in the body of a function; each
9325 statement is treated as a declaration-statement until proven
9327 if (cp_parser_error_occurred (parser))
9329 /* Handle function definitions specially. */
9330 if (function_definition_p)
9332 /* If the next token is a `,', then we are probably
9333 processing something like:
9337 which is erroneous. */
9338 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9340 cp_token *token = cp_lexer_peek_token (parser->lexer);
9341 error_at (token->location,
9343 " declarations and function-definitions is forbidden");
9345 /* Otherwise, we're done with the list of declarators. */
9348 pop_deferring_access_checks ();
9352 /* The next token should be either a `,' or a `;'. */
9353 token = cp_lexer_peek_token (parser->lexer);
9354 /* If it's a `,', there are more declarators to come. */
9355 if (token->type == CPP_COMMA)
9356 /* will be consumed next time around */;
9357 /* If it's a `;', we are done. */
9358 else if (token->type == CPP_SEMICOLON)
9360 /* Anything else is an error. */
9363 /* If we have already issued an error message we don't need
9364 to issue another one. */
9365 if (decl != error_mark_node
9366 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9367 cp_parser_error (parser, "expected %<,%> or %<;%>");
9368 /* Skip tokens until we reach the end of the statement. */
9369 cp_parser_skip_to_end_of_statement (parser);
9370 /* If the next token is now a `;', consume it. */
9371 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9372 cp_lexer_consume_token (parser->lexer);
9375 /* After the first time around, a function-definition is not
9376 allowed -- even if it was OK at first. For example:
9381 function_definition_allowed_p = false;
9384 /* Issue an error message if no declarators are present, and the
9385 decl-specifier-seq does not itself declare a class or
9387 if (!saw_declarator)
9389 if (cp_parser_declares_only_class_p (parser))
9390 shadow_tag (&decl_specifiers);
9391 /* Perform any deferred access checks. */
9392 perform_deferred_access_checks ();
9395 /* Consume the `;'. */
9396 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9399 pop_deferring_access_checks ();
9402 /* Parse a decl-specifier-seq.
9405 decl-specifier-seq [opt] decl-specifier
9408 storage-class-specifier
9419 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9421 The parser flags FLAGS is used to control type-specifier parsing.
9423 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9426 1: one of the decl-specifiers is an elaborated-type-specifier
9427 (i.e., a type declaration)
9428 2: one of the decl-specifiers is an enum-specifier or a
9429 class-specifier (i.e., a type definition)
9434 cp_parser_decl_specifier_seq (cp_parser* parser,
9435 cp_parser_flags flags,
9436 cp_decl_specifier_seq *decl_specs,
9437 int* declares_class_or_enum)
9439 bool constructor_possible_p = !parser->in_declarator_p;
9440 cp_token *start_token = NULL;
9442 /* Clear DECL_SPECS. */
9443 clear_decl_specs (decl_specs);
9445 /* Assume no class or enumeration type is declared. */
9446 *declares_class_or_enum = 0;
9448 /* Keep reading specifiers until there are no more to read. */
9452 bool found_decl_spec;
9455 /* Peek at the next token. */
9456 token = cp_lexer_peek_token (parser->lexer);
9458 /* Save the first token of the decl spec list for error
9461 start_token = token;
9462 /* Handle attributes. */
9463 if (token->keyword == RID_ATTRIBUTE)
9465 /* Parse the attributes. */
9466 decl_specs->attributes
9467 = chainon (decl_specs->attributes,
9468 cp_parser_attributes_opt (parser));
9471 /* Assume we will find a decl-specifier keyword. */
9472 found_decl_spec = true;
9473 /* If the next token is an appropriate keyword, we can simply
9474 add it to the list. */
9475 switch (token->keyword)
9481 if (!at_class_scope_p ())
9483 error_at (token->location, "%<friend%> used outside of class");
9484 cp_lexer_purge_token (parser->lexer);
9488 ++decl_specs->specs[(int) ds_friend];
9489 /* Consume the token. */
9490 cp_lexer_consume_token (parser->lexer);
9495 ++decl_specs->specs[(int) ds_constexpr];
9496 cp_lexer_consume_token (parser->lexer);
9499 /* function-specifier:
9506 cp_parser_function_specifier_opt (parser, decl_specs);
9512 ++decl_specs->specs[(int) ds_typedef];
9513 /* Consume the token. */
9514 cp_lexer_consume_token (parser->lexer);
9515 /* A constructor declarator cannot appear in a typedef. */
9516 constructor_possible_p = false;
9517 /* The "typedef" keyword can only occur in a declaration; we
9518 may as well commit at this point. */
9519 cp_parser_commit_to_tentative_parse (parser);
9521 if (decl_specs->storage_class != sc_none)
9522 decl_specs->conflicting_specifiers_p = true;
9525 /* storage-class-specifier:
9535 if (cxx_dialect == cxx98)
9537 /* Consume the token. */
9538 cp_lexer_consume_token (parser->lexer);
9540 /* Complain about `auto' as a storage specifier, if
9541 we're complaining about C++0x compatibility. */
9542 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9543 " will change meaning in C++0x; please remove it");
9545 /* Set the storage class anyway. */
9546 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9550 /* C++0x auto type-specifier. */
9551 found_decl_spec = false;
9558 /* Consume the token. */
9559 cp_lexer_consume_token (parser->lexer);
9560 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9564 /* Consume the token. */
9565 cp_lexer_consume_token (parser->lexer);
9566 ++decl_specs->specs[(int) ds_thread];
9570 /* We did not yet find a decl-specifier yet. */
9571 found_decl_spec = false;
9575 /* Constructors are a special case. The `S' in `S()' is not a
9576 decl-specifier; it is the beginning of the declarator. */
9579 && constructor_possible_p
9580 && (cp_parser_constructor_declarator_p
9581 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9583 /* If we don't have a DECL_SPEC yet, then we must be looking at
9584 a type-specifier. */
9585 if (!found_decl_spec && !constructor_p)
9587 int decl_spec_declares_class_or_enum;
9588 bool is_cv_qualifier;
9592 = cp_parser_type_specifier (parser, flags,
9594 /*is_declaration=*/true,
9595 &decl_spec_declares_class_or_enum,
9597 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9599 /* If this type-specifier referenced a user-defined type
9600 (a typedef, class-name, etc.), then we can't allow any
9601 more such type-specifiers henceforth.
9605 The longest sequence of decl-specifiers that could
9606 possibly be a type name is taken as the
9607 decl-specifier-seq of a declaration. The sequence shall
9608 be self-consistent as described below.
9612 As a general rule, at most one type-specifier is allowed
9613 in the complete decl-specifier-seq of a declaration. The
9614 only exceptions are the following:
9616 -- const or volatile can be combined with any other
9619 -- signed or unsigned can be combined with char, long,
9627 void g (const int Pc);
9629 Here, Pc is *not* part of the decl-specifier seq; it's
9630 the declarator. Therefore, once we see a type-specifier
9631 (other than a cv-qualifier), we forbid any additional
9632 user-defined types. We *do* still allow things like `int
9633 int' to be considered a decl-specifier-seq, and issue the
9634 error message later. */
9635 if (type_spec && !is_cv_qualifier)
9636 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9637 /* A constructor declarator cannot follow a type-specifier. */
9640 constructor_possible_p = false;
9641 found_decl_spec = true;
9642 if (!is_cv_qualifier)
9643 decl_specs->any_type_specifiers_p = true;
9647 /* If we still do not have a DECL_SPEC, then there are no more
9649 if (!found_decl_spec)
9652 decl_specs->any_specifiers_p = true;
9653 /* After we see one decl-specifier, further decl-specifiers are
9655 flags |= CP_PARSER_FLAGS_OPTIONAL;
9658 cp_parser_check_decl_spec (decl_specs, start_token->location);
9660 /* Don't allow a friend specifier with a class definition. */
9661 if (decl_specs->specs[(int) ds_friend] != 0
9662 && (*declares_class_or_enum & 2))
9663 error_at (start_token->location,
9664 "class definition may not be declared a friend");
9667 /* Parse an (optional) storage-class-specifier.
9669 storage-class-specifier:
9678 storage-class-specifier:
9681 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9684 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9686 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9689 if (cxx_dialect != cxx98)
9691 /* Fall through for C++98. */
9698 /* Consume the token. */
9699 return cp_lexer_consume_token (parser->lexer)->u.value;
9706 /* Parse an (optional) function-specifier.
9713 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9714 Updates DECL_SPECS, if it is non-NULL. */
9717 cp_parser_function_specifier_opt (cp_parser* parser,
9718 cp_decl_specifier_seq *decl_specs)
9720 cp_token *token = cp_lexer_peek_token (parser->lexer);
9721 switch (token->keyword)
9725 ++decl_specs->specs[(int) ds_inline];
9729 /* 14.5.2.3 [temp.mem]
9731 A member function template shall not be virtual. */
9732 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9733 error_at (token->location, "templates may not be %<virtual%>");
9734 else if (decl_specs)
9735 ++decl_specs->specs[(int) ds_virtual];
9740 ++decl_specs->specs[(int) ds_explicit];
9747 /* Consume the token. */
9748 return cp_lexer_consume_token (parser->lexer)->u.value;
9751 /* Parse a linkage-specification.
9753 linkage-specification:
9754 extern string-literal { declaration-seq [opt] }
9755 extern string-literal declaration */
9758 cp_parser_linkage_specification (cp_parser* parser)
9762 /* Look for the `extern' keyword. */
9763 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
9765 /* Look for the string-literal. */
9766 linkage = cp_parser_string_literal (parser, false, false);
9768 /* Transform the literal into an identifier. If the literal is a
9769 wide-character string, or contains embedded NULs, then we can't
9770 handle it as the user wants. */
9771 if (strlen (TREE_STRING_POINTER (linkage))
9772 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9774 cp_parser_error (parser, "invalid linkage-specification");
9775 /* Assume C++ linkage. */
9776 linkage = lang_name_cplusplus;
9779 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9781 /* We're now using the new linkage. */
9782 push_lang_context (linkage);
9784 /* If the next token is a `{', then we're using the first
9786 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9788 /* Consume the `{' token. */
9789 cp_lexer_consume_token (parser->lexer);
9790 /* Parse the declarations. */
9791 cp_parser_declaration_seq_opt (parser);
9792 /* Look for the closing `}'. */
9793 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9795 /* Otherwise, there's just one declaration. */
9798 bool saved_in_unbraced_linkage_specification_p;
9800 saved_in_unbraced_linkage_specification_p
9801 = parser->in_unbraced_linkage_specification_p;
9802 parser->in_unbraced_linkage_specification_p = true;
9803 cp_parser_declaration (parser);
9804 parser->in_unbraced_linkage_specification_p
9805 = saved_in_unbraced_linkage_specification_p;
9808 /* We're done with the linkage-specification. */
9809 pop_lang_context ();
9812 /* Parse a static_assert-declaration.
9814 static_assert-declaration:
9815 static_assert ( constant-expression , string-literal ) ;
9817 If MEMBER_P, this static_assert is a class member. */
9820 cp_parser_static_assert(cp_parser *parser, bool member_p)
9825 location_t saved_loc;
9827 /* Peek at the `static_assert' token so we can keep track of exactly
9828 where the static assertion started. */
9829 token = cp_lexer_peek_token (parser->lexer);
9830 saved_loc = token->location;
9832 /* Look for the `static_assert' keyword. */
9833 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9837 /* We know we are in a static assertion; commit to any tentative
9839 if (cp_parser_parsing_tentatively (parser))
9840 cp_parser_commit_to_tentative_parse (parser);
9842 /* Parse the `(' starting the static assertion condition. */
9843 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9845 /* Parse the constant-expression. */
9847 cp_parser_constant_expression (parser,
9848 /*allow_non_constant_p=*/false,
9849 /*non_constant_p=*/NULL);
9851 /* Parse the separating `,'. */
9852 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9854 /* Parse the string-literal message. */
9855 message = cp_parser_string_literal (parser,
9856 /*translate=*/false,
9859 /* A `)' completes the static assertion. */
9860 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9861 cp_parser_skip_to_closing_parenthesis (parser,
9862 /*recovering=*/true,
9864 /*consume_paren=*/true);
9866 /* A semicolon terminates the declaration. */
9867 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9869 /* Complete the static assertion, which may mean either processing
9870 the static assert now or saving it for template instantiation. */
9871 finish_static_assert (condition, message, saved_loc, member_p);
9874 /* Parse a `decltype' type. Returns the type.
9876 simple-type-specifier:
9877 decltype ( expression ) */
9880 cp_parser_decltype (cp_parser *parser)
9883 bool id_expression_or_member_access_p = false;
9884 const char *saved_message;
9885 bool saved_integral_constant_expression_p;
9886 bool saved_non_integral_constant_expression_p;
9887 cp_token *id_expr_start_token;
9889 /* Look for the `decltype' token. */
9890 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
9891 return error_mark_node;
9893 /* Types cannot be defined in a `decltype' expression. Save away the
9895 saved_message = parser->type_definition_forbidden_message;
9897 /* And create the new one. */
9898 parser->type_definition_forbidden_message
9899 = G_("types may not be defined in %<decltype%> expressions");
9901 /* The restrictions on constant-expressions do not apply inside
9902 decltype expressions. */
9903 saved_integral_constant_expression_p
9904 = parser->integral_constant_expression_p;
9905 saved_non_integral_constant_expression_p
9906 = parser->non_integral_constant_expression_p;
9907 parser->integral_constant_expression_p = false;
9909 /* Do not actually evaluate the expression. */
9910 ++cp_unevaluated_operand;
9912 /* Do not warn about problems with the expression. */
9913 ++c_inhibit_evaluation_warnings;
9915 /* Parse the opening `('. */
9916 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9917 return error_mark_node;
9919 /* First, try parsing an id-expression. */
9920 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9921 cp_parser_parse_tentatively (parser);
9922 expr = cp_parser_id_expression (parser,
9923 /*template_keyword_p=*/false,
9924 /*check_dependency_p=*/true,
9925 /*template_p=*/NULL,
9926 /*declarator_p=*/false,
9927 /*optional_p=*/false);
9929 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9931 bool non_integral_constant_expression_p = false;
9932 tree id_expression = expr;
9934 const char *error_msg;
9936 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9937 /* Lookup the name we got back from the id-expression. */
9938 expr = cp_parser_lookup_name (parser, expr,
9940 /*is_template=*/false,
9941 /*is_namespace=*/false,
9942 /*check_dependency=*/true,
9943 /*ambiguous_decls=*/NULL,
9944 id_expr_start_token->location);
9947 && expr != error_mark_node
9948 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9949 && TREE_CODE (expr) != TYPE_DECL
9950 && (TREE_CODE (expr) != BIT_NOT_EXPR
9951 || !TYPE_P (TREE_OPERAND (expr, 0)))
9952 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9954 /* Complete lookup of the id-expression. */
9955 expr = (finish_id_expression
9956 (id_expression, expr, parser->scope, &idk,
9957 /*integral_constant_expression_p=*/false,
9958 /*allow_non_integral_constant_expression_p=*/true,
9959 &non_integral_constant_expression_p,
9960 /*template_p=*/false,
9962 /*address_p=*/false,
9963 /*template_arg_p=*/false,
9965 id_expr_start_token->location));
9967 if (expr == error_mark_node)
9968 /* We found an id-expression, but it was something that we
9969 should not have found. This is an error, not something
9970 we can recover from, so note that we found an
9971 id-expression and we'll recover as gracefully as
9973 id_expression_or_member_access_p = true;
9977 && expr != error_mark_node
9978 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9979 /* We have an id-expression. */
9980 id_expression_or_member_access_p = true;
9983 if (!id_expression_or_member_access_p)
9985 /* Abort the id-expression parse. */
9986 cp_parser_abort_tentative_parse (parser);
9988 /* Parsing tentatively, again. */
9989 cp_parser_parse_tentatively (parser);
9991 /* Parse a class member access. */
9992 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9994 /*member_access_only_p=*/true, NULL);
9997 && expr != error_mark_node
9998 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9999 /* We have an id-expression. */
10000 id_expression_or_member_access_p = true;
10003 if (id_expression_or_member_access_p)
10004 /* We have parsed the complete id-expression or member access. */
10005 cp_parser_parse_definitely (parser);
10008 bool saved_greater_than_is_operator_p;
10010 /* Abort our attempt to parse an id-expression or member access
10012 cp_parser_abort_tentative_parse (parser);
10014 /* Within a parenthesized expression, a `>' token is always
10015 the greater-than operator. */
10016 saved_greater_than_is_operator_p
10017 = parser->greater_than_is_operator_p;
10018 parser->greater_than_is_operator_p = true;
10020 /* Parse a full expression. */
10021 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10023 /* The `>' token might be the end of a template-id or
10024 template-parameter-list now. */
10025 parser->greater_than_is_operator_p
10026 = saved_greater_than_is_operator_p;
10029 /* Go back to evaluating expressions. */
10030 --cp_unevaluated_operand;
10031 --c_inhibit_evaluation_warnings;
10033 /* Restore the old message and the integral constant expression
10035 parser->type_definition_forbidden_message = saved_message;
10036 parser->integral_constant_expression_p
10037 = saved_integral_constant_expression_p;
10038 parser->non_integral_constant_expression_p
10039 = saved_non_integral_constant_expression_p;
10041 if (expr == error_mark_node)
10043 /* Skip everything up to the closing `)'. */
10044 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10045 /*consume_paren=*/true);
10046 return error_mark_node;
10049 /* Parse to the closing `)'. */
10050 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10052 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10053 /*consume_paren=*/true);
10054 return error_mark_node;
10057 return finish_decltype_type (expr, id_expression_or_member_access_p);
10060 /* Special member functions [gram.special] */
10062 /* Parse a conversion-function-id.
10064 conversion-function-id:
10065 operator conversion-type-id
10067 Returns an IDENTIFIER_NODE representing the operator. */
10070 cp_parser_conversion_function_id (cp_parser* parser)
10074 tree saved_qualifying_scope;
10075 tree saved_object_scope;
10076 tree pushed_scope = NULL_TREE;
10078 /* Look for the `operator' token. */
10079 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10080 return error_mark_node;
10081 /* When we parse the conversion-type-id, the current scope will be
10082 reset. However, we need that information in able to look up the
10083 conversion function later, so we save it here. */
10084 saved_scope = parser->scope;
10085 saved_qualifying_scope = parser->qualifying_scope;
10086 saved_object_scope = parser->object_scope;
10087 /* We must enter the scope of the class so that the names of
10088 entities declared within the class are available in the
10089 conversion-type-id. For example, consider:
10096 S::operator I() { ... }
10098 In order to see that `I' is a type-name in the definition, we
10099 must be in the scope of `S'. */
10101 pushed_scope = push_scope (saved_scope);
10102 /* Parse the conversion-type-id. */
10103 type = cp_parser_conversion_type_id (parser);
10104 /* Leave the scope of the class, if any. */
10106 pop_scope (pushed_scope);
10107 /* Restore the saved scope. */
10108 parser->scope = saved_scope;
10109 parser->qualifying_scope = saved_qualifying_scope;
10110 parser->object_scope = saved_object_scope;
10111 /* If the TYPE is invalid, indicate failure. */
10112 if (type == error_mark_node)
10113 return error_mark_node;
10114 return mangle_conv_op_name_for_type (type);
10117 /* Parse a conversion-type-id:
10119 conversion-type-id:
10120 type-specifier-seq conversion-declarator [opt]
10122 Returns the TYPE specified. */
10125 cp_parser_conversion_type_id (cp_parser* parser)
10128 cp_decl_specifier_seq type_specifiers;
10129 cp_declarator *declarator;
10130 tree type_specified;
10132 /* Parse the attributes. */
10133 attributes = cp_parser_attributes_opt (parser);
10134 /* Parse the type-specifiers. */
10135 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10136 /*is_trailing_return=*/false,
10138 /* If that didn't work, stop. */
10139 if (type_specifiers.type == error_mark_node)
10140 return error_mark_node;
10141 /* Parse the conversion-declarator. */
10142 declarator = cp_parser_conversion_declarator_opt (parser);
10144 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10145 /*initialized=*/0, &attributes);
10147 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10149 /* Don't give this error when parsing tentatively. This happens to
10150 work because we always parse this definitively once. */
10151 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10152 && type_uses_auto (type_specified))
10154 error ("invalid use of %<auto%> in conversion operator");
10155 return error_mark_node;
10158 return type_specified;
10161 /* Parse an (optional) conversion-declarator.
10163 conversion-declarator:
10164 ptr-operator conversion-declarator [opt]
10168 static cp_declarator *
10169 cp_parser_conversion_declarator_opt (cp_parser* parser)
10171 enum tree_code code;
10173 cp_cv_quals cv_quals;
10175 /* We don't know if there's a ptr-operator next, or not. */
10176 cp_parser_parse_tentatively (parser);
10177 /* Try the ptr-operator. */
10178 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10179 /* If it worked, look for more conversion-declarators. */
10180 if (cp_parser_parse_definitely (parser))
10182 cp_declarator *declarator;
10184 /* Parse another optional declarator. */
10185 declarator = cp_parser_conversion_declarator_opt (parser);
10187 return cp_parser_make_indirect_declarator
10188 (code, class_type, cv_quals, declarator);
10194 /* Parse an (optional) ctor-initializer.
10197 : mem-initializer-list
10199 Returns TRUE iff the ctor-initializer was actually present. */
10202 cp_parser_ctor_initializer_opt (cp_parser* parser)
10204 /* If the next token is not a `:', then there is no
10205 ctor-initializer. */
10206 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10208 /* Do default initialization of any bases and members. */
10209 if (DECL_CONSTRUCTOR_P (current_function_decl))
10210 finish_mem_initializers (NULL_TREE);
10215 /* Consume the `:' token. */
10216 cp_lexer_consume_token (parser->lexer);
10217 /* And the mem-initializer-list. */
10218 cp_parser_mem_initializer_list (parser);
10223 /* Parse a mem-initializer-list.
10225 mem-initializer-list:
10226 mem-initializer ... [opt]
10227 mem-initializer ... [opt] , mem-initializer-list */
10230 cp_parser_mem_initializer_list (cp_parser* parser)
10232 tree mem_initializer_list = NULL_TREE;
10233 cp_token *token = cp_lexer_peek_token (parser->lexer);
10235 /* Let the semantic analysis code know that we are starting the
10236 mem-initializer-list. */
10237 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10238 error_at (token->location,
10239 "only constructors take member initializers");
10241 /* Loop through the list. */
10244 tree mem_initializer;
10246 token = cp_lexer_peek_token (parser->lexer);
10247 /* Parse the mem-initializer. */
10248 mem_initializer = cp_parser_mem_initializer (parser);
10249 /* If the next token is a `...', we're expanding member initializers. */
10250 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10252 /* Consume the `...'. */
10253 cp_lexer_consume_token (parser->lexer);
10255 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10256 can be expanded but members cannot. */
10257 if (mem_initializer != error_mark_node
10258 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10260 error_at (token->location,
10261 "cannot expand initializer for member %<%D%>",
10262 TREE_PURPOSE (mem_initializer));
10263 mem_initializer = error_mark_node;
10266 /* Construct the pack expansion type. */
10267 if (mem_initializer != error_mark_node)
10268 mem_initializer = make_pack_expansion (mem_initializer);
10270 /* Add it to the list, unless it was erroneous. */
10271 if (mem_initializer != error_mark_node)
10273 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10274 mem_initializer_list = mem_initializer;
10276 /* If the next token is not a `,', we're done. */
10277 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10279 /* Consume the `,' token. */
10280 cp_lexer_consume_token (parser->lexer);
10283 /* Perform semantic analysis. */
10284 if (DECL_CONSTRUCTOR_P (current_function_decl))
10285 finish_mem_initializers (mem_initializer_list);
10288 /* Parse a mem-initializer.
10291 mem-initializer-id ( expression-list [opt] )
10292 mem-initializer-id braced-init-list
10297 ( expression-list [opt] )
10299 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10300 class) or FIELD_DECL (for a non-static data member) to initialize;
10301 the TREE_VALUE is the expression-list. An empty initialization
10302 list is represented by void_list_node. */
10305 cp_parser_mem_initializer (cp_parser* parser)
10307 tree mem_initializer_id;
10308 tree expression_list;
10310 cp_token *token = cp_lexer_peek_token (parser->lexer);
10312 /* Find out what is being initialized. */
10313 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10315 permerror (token->location,
10316 "anachronistic old-style base class initializer");
10317 mem_initializer_id = NULL_TREE;
10321 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10322 if (mem_initializer_id == error_mark_node)
10323 return mem_initializer_id;
10325 member = expand_member_init (mem_initializer_id);
10326 if (member && !DECL_P (member))
10327 in_base_initializer = 1;
10329 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10331 bool expr_non_constant_p;
10332 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10333 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10334 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10335 expression_list = build_tree_list (NULL_TREE, expression_list);
10340 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10342 /*allow_expansion_p=*/true,
10343 /*non_constant_p=*/NULL);
10345 return error_mark_node;
10346 expression_list = build_tree_list_vec (vec);
10347 release_tree_vector (vec);
10350 if (expression_list == error_mark_node)
10351 return error_mark_node;
10352 if (!expression_list)
10353 expression_list = void_type_node;
10355 in_base_initializer = 0;
10357 return member ? build_tree_list (member, expression_list) : error_mark_node;
10360 /* Parse a mem-initializer-id.
10362 mem-initializer-id:
10363 :: [opt] nested-name-specifier [opt] class-name
10366 Returns a TYPE indicating the class to be initializer for the first
10367 production. Returns an IDENTIFIER_NODE indicating the data member
10368 to be initialized for the second production. */
10371 cp_parser_mem_initializer_id (cp_parser* parser)
10373 bool global_scope_p;
10374 bool nested_name_specifier_p;
10375 bool template_p = false;
10378 cp_token *token = cp_lexer_peek_token (parser->lexer);
10380 /* `typename' is not allowed in this context ([temp.res]). */
10381 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10383 error_at (token->location,
10384 "keyword %<typename%> not allowed in this context (a qualified "
10385 "member initializer is implicitly a type)");
10386 cp_lexer_consume_token (parser->lexer);
10388 /* Look for the optional `::' operator. */
10390 = (cp_parser_global_scope_opt (parser,
10391 /*current_scope_valid_p=*/false)
10393 /* Look for the optional nested-name-specifier. The simplest way to
10398 The keyword `typename' is not permitted in a base-specifier or
10399 mem-initializer; in these contexts a qualified name that
10400 depends on a template-parameter is implicitly assumed to be a
10403 is to assume that we have seen the `typename' keyword at this
10405 nested_name_specifier_p
10406 = (cp_parser_nested_name_specifier_opt (parser,
10407 /*typename_keyword_p=*/true,
10408 /*check_dependency_p=*/true,
10410 /*is_declaration=*/true)
10412 if (nested_name_specifier_p)
10413 template_p = cp_parser_optional_template_keyword (parser);
10414 /* If there is a `::' operator or a nested-name-specifier, then we
10415 are definitely looking for a class-name. */
10416 if (global_scope_p || nested_name_specifier_p)
10417 return cp_parser_class_name (parser,
10418 /*typename_keyword_p=*/true,
10419 /*template_keyword_p=*/template_p,
10421 /*check_dependency_p=*/true,
10422 /*class_head_p=*/false,
10423 /*is_declaration=*/true);
10424 /* Otherwise, we could also be looking for an ordinary identifier. */
10425 cp_parser_parse_tentatively (parser);
10426 /* Try a class-name. */
10427 id = cp_parser_class_name (parser,
10428 /*typename_keyword_p=*/true,
10429 /*template_keyword_p=*/false,
10431 /*check_dependency_p=*/true,
10432 /*class_head_p=*/false,
10433 /*is_declaration=*/true);
10434 /* If we found one, we're done. */
10435 if (cp_parser_parse_definitely (parser))
10437 /* Otherwise, look for an ordinary identifier. */
10438 return cp_parser_identifier (parser);
10441 /* Overloading [gram.over] */
10443 /* Parse an operator-function-id.
10445 operator-function-id:
10448 Returns an IDENTIFIER_NODE for the operator which is a
10449 human-readable spelling of the identifier, e.g., `operator +'. */
10452 cp_parser_operator_function_id (cp_parser* parser)
10454 /* Look for the `operator' keyword. */
10455 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10456 return error_mark_node;
10457 /* And then the name of the operator itself. */
10458 return cp_parser_operator (parser);
10461 /* Parse an operator.
10464 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10465 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10466 || ++ -- , ->* -> () []
10473 Returns an IDENTIFIER_NODE for the operator which is a
10474 human-readable spelling of the identifier, e.g., `operator +'. */
10477 cp_parser_operator (cp_parser* parser)
10479 tree id = NULL_TREE;
10482 /* Peek at the next token. */
10483 token = cp_lexer_peek_token (parser->lexer);
10484 /* Figure out which operator we have. */
10485 switch (token->type)
10491 /* The keyword should be either `new' or `delete'. */
10492 if (token->keyword == RID_NEW)
10494 else if (token->keyword == RID_DELETE)
10499 /* Consume the `new' or `delete' token. */
10500 cp_lexer_consume_token (parser->lexer);
10502 /* Peek at the next token. */
10503 token = cp_lexer_peek_token (parser->lexer);
10504 /* If it's a `[' token then this is the array variant of the
10506 if (token->type == CPP_OPEN_SQUARE)
10508 /* Consume the `[' token. */
10509 cp_lexer_consume_token (parser->lexer);
10510 /* Look for the `]' token. */
10511 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10512 id = ansi_opname (op == NEW_EXPR
10513 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10515 /* Otherwise, we have the non-array variant. */
10517 id = ansi_opname (op);
10523 id = ansi_opname (PLUS_EXPR);
10527 id = ansi_opname (MINUS_EXPR);
10531 id = ansi_opname (MULT_EXPR);
10535 id = ansi_opname (TRUNC_DIV_EXPR);
10539 id = ansi_opname (TRUNC_MOD_EXPR);
10543 id = ansi_opname (BIT_XOR_EXPR);
10547 id = ansi_opname (BIT_AND_EXPR);
10551 id = ansi_opname (BIT_IOR_EXPR);
10555 id = ansi_opname (BIT_NOT_EXPR);
10559 id = ansi_opname (TRUTH_NOT_EXPR);
10563 id = ansi_assopname (NOP_EXPR);
10567 id = ansi_opname (LT_EXPR);
10571 id = ansi_opname (GT_EXPR);
10575 id = ansi_assopname (PLUS_EXPR);
10579 id = ansi_assopname (MINUS_EXPR);
10583 id = ansi_assopname (MULT_EXPR);
10587 id = ansi_assopname (TRUNC_DIV_EXPR);
10591 id = ansi_assopname (TRUNC_MOD_EXPR);
10595 id = ansi_assopname (BIT_XOR_EXPR);
10599 id = ansi_assopname (BIT_AND_EXPR);
10603 id = ansi_assopname (BIT_IOR_EXPR);
10607 id = ansi_opname (LSHIFT_EXPR);
10611 id = ansi_opname (RSHIFT_EXPR);
10614 case CPP_LSHIFT_EQ:
10615 id = ansi_assopname (LSHIFT_EXPR);
10618 case CPP_RSHIFT_EQ:
10619 id = ansi_assopname (RSHIFT_EXPR);
10623 id = ansi_opname (EQ_EXPR);
10627 id = ansi_opname (NE_EXPR);
10631 id = ansi_opname (LE_EXPR);
10634 case CPP_GREATER_EQ:
10635 id = ansi_opname (GE_EXPR);
10639 id = ansi_opname (TRUTH_ANDIF_EXPR);
10643 id = ansi_opname (TRUTH_ORIF_EXPR);
10646 case CPP_PLUS_PLUS:
10647 id = ansi_opname (POSTINCREMENT_EXPR);
10650 case CPP_MINUS_MINUS:
10651 id = ansi_opname (PREDECREMENT_EXPR);
10655 id = ansi_opname (COMPOUND_EXPR);
10658 case CPP_DEREF_STAR:
10659 id = ansi_opname (MEMBER_REF);
10663 id = ansi_opname (COMPONENT_REF);
10666 case CPP_OPEN_PAREN:
10667 /* Consume the `('. */
10668 cp_lexer_consume_token (parser->lexer);
10669 /* Look for the matching `)'. */
10670 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10671 return ansi_opname (CALL_EXPR);
10673 case CPP_OPEN_SQUARE:
10674 /* Consume the `['. */
10675 cp_lexer_consume_token (parser->lexer);
10676 /* Look for the matching `]'. */
10677 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10678 return ansi_opname (ARRAY_REF);
10681 /* Anything else is an error. */
10685 /* If we have selected an identifier, we need to consume the
10688 cp_lexer_consume_token (parser->lexer);
10689 /* Otherwise, no valid operator name was present. */
10692 cp_parser_error (parser, "expected operator");
10693 id = error_mark_node;
10699 /* Parse a template-declaration.
10701 template-declaration:
10702 export [opt] template < template-parameter-list > declaration
10704 If MEMBER_P is TRUE, this template-declaration occurs within a
10707 The grammar rule given by the standard isn't correct. What
10708 is really meant is:
10710 template-declaration:
10711 export [opt] template-parameter-list-seq
10712 decl-specifier-seq [opt] init-declarator [opt] ;
10713 export [opt] template-parameter-list-seq
10714 function-definition
10716 template-parameter-list-seq:
10717 template-parameter-list-seq [opt]
10718 template < template-parameter-list > */
10721 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10723 /* Check for `export'. */
10724 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10726 /* Consume the `export' token. */
10727 cp_lexer_consume_token (parser->lexer);
10728 /* Warn that we do not support `export'. */
10729 warning (0, "keyword %<export%> not implemented, and will be ignored");
10732 cp_parser_template_declaration_after_export (parser, member_p);
10735 /* Parse a template-parameter-list.
10737 template-parameter-list:
10739 template-parameter-list , template-parameter
10741 Returns a TREE_LIST. Each node represents a template parameter.
10742 The nodes are connected via their TREE_CHAINs. */
10745 cp_parser_template_parameter_list (cp_parser* parser)
10747 tree parameter_list = NULL_TREE;
10749 begin_template_parm_list ();
10754 bool is_parameter_pack;
10755 location_t parm_loc;
10757 /* Parse the template-parameter. */
10758 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10759 parameter = cp_parser_template_parameter (parser,
10761 &is_parameter_pack);
10762 /* Add it to the list. */
10763 if (parameter != error_mark_node)
10764 parameter_list = process_template_parm (parameter_list,
10768 is_parameter_pack);
10771 tree err_parm = build_tree_list (parameter, parameter);
10772 TREE_VALUE (err_parm) = error_mark_node;
10773 parameter_list = chainon (parameter_list, err_parm);
10776 /* If the next token is not a `,', we're done. */
10777 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10779 /* Otherwise, consume the `,' token. */
10780 cp_lexer_consume_token (parser->lexer);
10783 return end_template_parm_list (parameter_list);
10786 /* Parse a template-parameter.
10788 template-parameter:
10790 parameter-declaration
10792 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10793 the parameter. The TREE_PURPOSE is the default value, if any.
10794 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10795 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10796 set to true iff this parameter is a parameter pack. */
10799 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10800 bool *is_parameter_pack)
10803 cp_parameter_declarator *parameter_declarator;
10804 cp_declarator *id_declarator;
10807 /* Assume it is a type parameter or a template parameter. */
10808 *is_non_type = false;
10809 /* Assume it not a parameter pack. */
10810 *is_parameter_pack = false;
10811 /* Peek at the next token. */
10812 token = cp_lexer_peek_token (parser->lexer);
10813 /* If it is `class' or `template', we have a type-parameter. */
10814 if (token->keyword == RID_TEMPLATE)
10815 return cp_parser_type_parameter (parser, is_parameter_pack);
10816 /* If it is `class' or `typename' we do not know yet whether it is a
10817 type parameter or a non-type parameter. Consider:
10819 template <typename T, typename T::X X> ...
10823 template <class C, class D*> ...
10825 Here, the first parameter is a type parameter, and the second is
10826 a non-type parameter. We can tell by looking at the token after
10827 the identifier -- if it is a `,', `=', or `>' then we have a type
10829 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10831 /* Peek at the token after `class' or `typename'. */
10832 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10833 /* If it's an ellipsis, we have a template type parameter
10835 if (token->type == CPP_ELLIPSIS)
10836 return cp_parser_type_parameter (parser, is_parameter_pack);
10837 /* If it's an identifier, skip it. */
10838 if (token->type == CPP_NAME)
10839 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10840 /* Now, see if the token looks like the end of a template
10842 if (token->type == CPP_COMMA
10843 || token->type == CPP_EQ
10844 || token->type == CPP_GREATER)
10845 return cp_parser_type_parameter (parser, is_parameter_pack);
10848 /* Otherwise, it is a non-type parameter.
10852 When parsing a default template-argument for a non-type
10853 template-parameter, the first non-nested `>' is taken as the end
10854 of the template parameter-list rather than a greater-than
10856 *is_non_type = true;
10857 parameter_declarator
10858 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10859 /*parenthesized_p=*/NULL);
10861 /* If the parameter declaration is marked as a parameter pack, set
10862 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10863 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10865 if (parameter_declarator
10866 && parameter_declarator->declarator
10867 && parameter_declarator->declarator->parameter_pack_p)
10869 *is_parameter_pack = true;
10870 parameter_declarator->declarator->parameter_pack_p = false;
10873 /* If the next token is an ellipsis, and we don't already have it
10874 marked as a parameter pack, then we have a parameter pack (that
10875 has no declarator). */
10876 if (!*is_parameter_pack
10877 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10878 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10880 /* Consume the `...'. */
10881 cp_lexer_consume_token (parser->lexer);
10882 maybe_warn_variadic_templates ();
10884 *is_parameter_pack = true;
10886 /* We might end up with a pack expansion as the type of the non-type
10887 template parameter, in which case this is a non-type template
10889 else if (parameter_declarator
10890 && parameter_declarator->decl_specifiers.type
10891 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10893 *is_parameter_pack = true;
10894 parameter_declarator->decl_specifiers.type =
10895 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10898 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10900 /* Parameter packs cannot have default arguments. However, a
10901 user may try to do so, so we'll parse them and give an
10902 appropriate diagnostic here. */
10904 /* Consume the `='. */
10905 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10906 cp_lexer_consume_token (parser->lexer);
10908 /* Find the name of the parameter pack. */
10909 id_declarator = parameter_declarator->declarator;
10910 while (id_declarator && id_declarator->kind != cdk_id)
10911 id_declarator = id_declarator->declarator;
10913 if (id_declarator && id_declarator->kind == cdk_id)
10914 error_at (start_token->location,
10915 "template parameter pack %qD cannot have a default argument",
10916 id_declarator->u.id.unqualified_name);
10918 error_at (start_token->location,
10919 "template parameter pack cannot have a default argument");
10921 /* Parse the default argument, but throw away the result. */
10922 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10925 parm = grokdeclarator (parameter_declarator->declarator,
10926 ¶meter_declarator->decl_specifiers,
10927 TPARM, /*initialized=*/0,
10928 /*attrlist=*/NULL);
10929 if (parm == error_mark_node)
10930 return error_mark_node;
10932 return build_tree_list (parameter_declarator->default_argument, parm);
10935 /* Parse a type-parameter.
10938 class identifier [opt]
10939 class identifier [opt] = type-id
10940 typename identifier [opt]
10941 typename identifier [opt] = type-id
10942 template < template-parameter-list > class identifier [opt]
10943 template < template-parameter-list > class identifier [opt]
10946 GNU Extension (variadic templates):
10949 class ... identifier [opt]
10950 typename ... identifier [opt]
10952 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10953 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10954 the declaration of the parameter.
10956 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10959 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10964 /* Look for a keyword to tell us what kind of parameter this is. */
10965 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
10967 return error_mark_node;
10969 switch (token->keyword)
10975 tree default_argument;
10977 /* If the next token is an ellipsis, we have a template
10979 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10981 /* Consume the `...' token. */
10982 cp_lexer_consume_token (parser->lexer);
10983 maybe_warn_variadic_templates ();
10985 *is_parameter_pack = true;
10988 /* If the next token is an identifier, then it names the
10990 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10991 identifier = cp_parser_identifier (parser);
10993 identifier = NULL_TREE;
10995 /* Create the parameter. */
10996 parameter = finish_template_type_parm (class_type_node, identifier);
10998 /* If the next token is an `=', we have a default argument. */
10999 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11001 /* Consume the `=' token. */
11002 cp_lexer_consume_token (parser->lexer);
11003 /* Parse the default-argument. */
11004 push_deferring_access_checks (dk_no_deferred);
11005 default_argument = cp_parser_type_id (parser);
11007 /* Template parameter packs cannot have default
11009 if (*is_parameter_pack)
11012 error_at (token->location,
11013 "template parameter pack %qD cannot have a "
11014 "default argument", identifier);
11016 error_at (token->location,
11017 "template parameter packs cannot have "
11018 "default arguments");
11019 default_argument = NULL_TREE;
11021 pop_deferring_access_checks ();
11024 default_argument = NULL_TREE;
11026 /* Create the combined representation of the parameter and the
11027 default argument. */
11028 parameter = build_tree_list (default_argument, parameter);
11035 tree default_argument;
11037 /* Look for the `<'. */
11038 cp_parser_require (parser, CPP_LESS, RT_LESS);
11039 /* Parse the template-parameter-list. */
11040 cp_parser_template_parameter_list (parser);
11041 /* Look for the `>'. */
11042 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11043 /* Look for the `class' keyword. */
11044 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11045 /* If the next token is an ellipsis, we have a template
11047 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11049 /* Consume the `...' token. */
11050 cp_lexer_consume_token (parser->lexer);
11051 maybe_warn_variadic_templates ();
11053 *is_parameter_pack = true;
11055 /* If the next token is an `=', then there is a
11056 default-argument. If the next token is a `>', we are at
11057 the end of the parameter-list. If the next token is a `,',
11058 then we are at the end of this parameter. */
11059 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11060 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11061 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11063 identifier = cp_parser_identifier (parser);
11064 /* Treat invalid names as if the parameter were nameless. */
11065 if (identifier == error_mark_node)
11066 identifier = NULL_TREE;
11069 identifier = NULL_TREE;
11071 /* Create the template parameter. */
11072 parameter = finish_template_template_parm (class_type_node,
11075 /* If the next token is an `=', then there is a
11076 default-argument. */
11077 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11081 /* Consume the `='. */
11082 cp_lexer_consume_token (parser->lexer);
11083 /* Parse the id-expression. */
11084 push_deferring_access_checks (dk_no_deferred);
11085 /* save token before parsing the id-expression, for error
11087 token = cp_lexer_peek_token (parser->lexer);
11089 = cp_parser_id_expression (parser,
11090 /*template_keyword_p=*/false,
11091 /*check_dependency_p=*/true,
11092 /*template_p=*/&is_template,
11093 /*declarator_p=*/false,
11094 /*optional_p=*/false);
11095 if (TREE_CODE (default_argument) == TYPE_DECL)
11096 /* If the id-expression was a template-id that refers to
11097 a template-class, we already have the declaration here,
11098 so no further lookup is needed. */
11101 /* Look up the name. */
11103 = cp_parser_lookup_name (parser, default_argument,
11105 /*is_template=*/is_template,
11106 /*is_namespace=*/false,
11107 /*check_dependency=*/true,
11108 /*ambiguous_decls=*/NULL,
11110 /* See if the default argument is valid. */
11112 = check_template_template_default_arg (default_argument);
11114 /* Template parameter packs cannot have default
11116 if (*is_parameter_pack)
11119 error_at (token->location,
11120 "template parameter pack %qD cannot "
11121 "have a default argument",
11124 error_at (token->location, "template parameter packs cannot "
11125 "have default arguments");
11126 default_argument = NULL_TREE;
11128 pop_deferring_access_checks ();
11131 default_argument = NULL_TREE;
11133 /* Create the combined representation of the parameter and the
11134 default argument. */
11135 parameter = build_tree_list (default_argument, parameter);
11140 gcc_unreachable ();
11147 /* Parse a template-id.
11150 template-name < template-argument-list [opt] >
11152 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11153 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11154 returned. Otherwise, if the template-name names a function, or set
11155 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11156 names a class, returns a TYPE_DECL for the specialization.
11158 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11159 uninstantiated templates. */
11162 cp_parser_template_id (cp_parser *parser,
11163 bool template_keyword_p,
11164 bool check_dependency_p,
11165 bool is_declaration)
11171 cp_token_position start_of_id = 0;
11172 deferred_access_check *chk;
11173 VEC (deferred_access_check,gc) *access_check;
11174 cp_token *next_token = NULL, *next_token_2 = NULL;
11175 bool is_identifier;
11177 /* If the next token corresponds to a template-id, there is no need
11179 next_token = cp_lexer_peek_token (parser->lexer);
11180 if (next_token->type == CPP_TEMPLATE_ID)
11182 struct tree_check *check_value;
11184 /* Get the stored value. */
11185 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11186 /* Perform any access checks that were deferred. */
11187 access_check = check_value->checks;
11191 VEC_iterate (deferred_access_check, access_check, i, chk) ;
11194 perform_or_defer_access_check (chk->binfo,
11199 /* Return the stored value. */
11200 return check_value->value;
11203 /* Avoid performing name lookup if there is no possibility of
11204 finding a template-id. */
11205 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11206 || (next_token->type == CPP_NAME
11207 && !cp_parser_nth_token_starts_template_argument_list_p
11210 cp_parser_error (parser, "expected template-id");
11211 return error_mark_node;
11214 /* Remember where the template-id starts. */
11215 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11216 start_of_id = cp_lexer_token_position (parser->lexer, false);
11218 push_deferring_access_checks (dk_deferred);
11220 /* Parse the template-name. */
11221 is_identifier = false;
11222 templ = cp_parser_template_name (parser, template_keyword_p,
11223 check_dependency_p,
11226 if (templ == error_mark_node || is_identifier)
11228 pop_deferring_access_checks ();
11232 /* If we find the sequence `[:' after a template-name, it's probably
11233 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11234 parse correctly the argument list. */
11235 next_token = cp_lexer_peek_token (parser->lexer);
11236 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11237 if (next_token->type == CPP_OPEN_SQUARE
11238 && next_token->flags & DIGRAPH
11239 && next_token_2->type == CPP_COLON
11240 && !(next_token_2->flags & PREV_WHITE))
11242 cp_parser_parse_tentatively (parser);
11243 /* Change `:' into `::'. */
11244 next_token_2->type = CPP_SCOPE;
11245 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11247 cp_lexer_consume_token (parser->lexer);
11249 /* Parse the arguments. */
11250 arguments = cp_parser_enclosed_template_argument_list (parser);
11251 if (!cp_parser_parse_definitely (parser))
11253 /* If we couldn't parse an argument list, then we revert our changes
11254 and return simply an error. Maybe this is not a template-id
11256 next_token_2->type = CPP_COLON;
11257 cp_parser_error (parser, "expected %<<%>");
11258 pop_deferring_access_checks ();
11259 return error_mark_node;
11261 /* Otherwise, emit an error about the invalid digraph, but continue
11262 parsing because we got our argument list. */
11263 if (permerror (next_token->location,
11264 "%<<::%> cannot begin a template-argument list"))
11266 static bool hint = false;
11267 inform (next_token->location,
11268 "%<<:%> is an alternate spelling for %<[%>."
11269 " Insert whitespace between %<<%> and %<::%>");
11270 if (!hint && !flag_permissive)
11272 inform (next_token->location, "(if you use %<-fpermissive%>"
11273 " G++ will accept your code)");
11280 /* Look for the `<' that starts the template-argument-list. */
11281 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11283 pop_deferring_access_checks ();
11284 return error_mark_node;
11286 /* Parse the arguments. */
11287 arguments = cp_parser_enclosed_template_argument_list (parser);
11290 /* Build a representation of the specialization. */
11291 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11292 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11293 else if (DECL_CLASS_TEMPLATE_P (templ)
11294 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11296 bool entering_scope;
11297 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11298 template (rather than some instantiation thereof) only if
11299 is not nested within some other construct. For example, in
11300 "template <typename T> void f(T) { A<T>::", A<T> is just an
11301 instantiation of A. */
11302 entering_scope = (template_parm_scope_p ()
11303 && cp_lexer_next_token_is (parser->lexer,
11306 = finish_template_type (templ, arguments, entering_scope);
11310 /* If it's not a class-template or a template-template, it should be
11311 a function-template. */
11312 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11313 || TREE_CODE (templ) == OVERLOAD
11314 || BASELINK_P (templ)));
11316 template_id = lookup_template_function (templ, arguments);
11319 /* If parsing tentatively, replace the sequence of tokens that makes
11320 up the template-id with a CPP_TEMPLATE_ID token. That way,
11321 should we re-parse the token stream, we will not have to repeat
11322 the effort required to do the parse, nor will we issue duplicate
11323 error messages about problems during instantiation of the
11327 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11329 /* Reset the contents of the START_OF_ID token. */
11330 token->type = CPP_TEMPLATE_ID;
11331 /* Retrieve any deferred checks. Do not pop this access checks yet
11332 so the memory will not be reclaimed during token replacing below. */
11333 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11334 token->u.tree_check_value->value = template_id;
11335 token->u.tree_check_value->checks = get_deferred_access_checks ();
11336 token->keyword = RID_MAX;
11338 /* Purge all subsequent tokens. */
11339 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11341 /* ??? Can we actually assume that, if template_id ==
11342 error_mark_node, we will have issued a diagnostic to the
11343 user, as opposed to simply marking the tentative parse as
11345 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11346 error_at (token->location, "parse error in template argument list");
11349 pop_deferring_access_checks ();
11350 return template_id;
11353 /* Parse a template-name.
11358 The standard should actually say:
11362 operator-function-id
11364 A defect report has been filed about this issue.
11366 A conversion-function-id cannot be a template name because they cannot
11367 be part of a template-id. In fact, looking at this code:
11369 a.operator K<int>()
11371 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11372 It is impossible to call a templated conversion-function-id with an
11373 explicit argument list, since the only allowed template parameter is
11374 the type to which it is converting.
11376 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11377 `template' keyword, in a construction like:
11381 In that case `f' is taken to be a template-name, even though there
11382 is no way of knowing for sure.
11384 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11385 name refers to a set of overloaded functions, at least one of which
11386 is a template, or an IDENTIFIER_NODE with the name of the template,
11387 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11388 names are looked up inside uninstantiated templates. */
11391 cp_parser_template_name (cp_parser* parser,
11392 bool template_keyword_p,
11393 bool check_dependency_p,
11394 bool is_declaration,
11395 bool *is_identifier)
11400 cp_token *token = cp_lexer_peek_token (parser->lexer);
11402 /* If the next token is `operator', then we have either an
11403 operator-function-id or a conversion-function-id. */
11404 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11406 /* We don't know whether we're looking at an
11407 operator-function-id or a conversion-function-id. */
11408 cp_parser_parse_tentatively (parser);
11409 /* Try an operator-function-id. */
11410 identifier = cp_parser_operator_function_id (parser);
11411 /* If that didn't work, try a conversion-function-id. */
11412 if (!cp_parser_parse_definitely (parser))
11414 cp_parser_error (parser, "expected template-name");
11415 return error_mark_node;
11418 /* Look for the identifier. */
11420 identifier = cp_parser_identifier (parser);
11422 /* If we didn't find an identifier, we don't have a template-id. */
11423 if (identifier == error_mark_node)
11424 return error_mark_node;
11426 /* If the name immediately followed the `template' keyword, then it
11427 is a template-name. However, if the next token is not `<', then
11428 we do not treat it as a template-name, since it is not being used
11429 as part of a template-id. This enables us to handle constructs
11432 template <typename T> struct S { S(); };
11433 template <typename T> S<T>::S();
11435 correctly. We would treat `S' as a template -- if it were `S<T>'
11436 -- but we do not if there is no `<'. */
11438 if (processing_template_decl
11439 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11441 /* In a declaration, in a dependent context, we pretend that the
11442 "template" keyword was present in order to improve error
11443 recovery. For example, given:
11445 template <typename T> void f(T::X<int>);
11447 we want to treat "X<int>" as a template-id. */
11449 && !template_keyword_p
11450 && parser->scope && TYPE_P (parser->scope)
11451 && check_dependency_p
11452 && dependent_scope_p (parser->scope)
11453 /* Do not do this for dtors (or ctors), since they never
11454 need the template keyword before their name. */
11455 && !constructor_name_p (identifier, parser->scope))
11457 cp_token_position start = 0;
11459 /* Explain what went wrong. */
11460 error_at (token->location, "non-template %qD used as template",
11462 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11463 parser->scope, identifier);
11464 /* If parsing tentatively, find the location of the "<" token. */
11465 if (cp_parser_simulate_error (parser))
11466 start = cp_lexer_token_position (parser->lexer, true);
11467 /* Parse the template arguments so that we can issue error
11468 messages about them. */
11469 cp_lexer_consume_token (parser->lexer);
11470 cp_parser_enclosed_template_argument_list (parser);
11471 /* Skip tokens until we find a good place from which to
11472 continue parsing. */
11473 cp_parser_skip_to_closing_parenthesis (parser,
11474 /*recovering=*/true,
11476 /*consume_paren=*/false);
11477 /* If parsing tentatively, permanently remove the
11478 template argument list. That will prevent duplicate
11479 error messages from being issued about the missing
11480 "template" keyword. */
11482 cp_lexer_purge_tokens_after (parser->lexer, start);
11484 *is_identifier = true;
11488 /* If the "template" keyword is present, then there is generally
11489 no point in doing name-lookup, so we just return IDENTIFIER.
11490 But, if the qualifying scope is non-dependent then we can
11491 (and must) do name-lookup normally. */
11492 if (template_keyword_p
11494 || (TYPE_P (parser->scope)
11495 && dependent_type_p (parser->scope))))
11499 /* Look up the name. */
11500 decl = cp_parser_lookup_name (parser, identifier,
11502 /*is_template=*/true,
11503 /*is_namespace=*/false,
11504 check_dependency_p,
11505 /*ambiguous_decls=*/NULL,
11508 /* If DECL is a template, then the name was a template-name. */
11509 if (TREE_CODE (decl) == TEMPLATE_DECL)
11513 tree fn = NULL_TREE;
11515 /* The standard does not explicitly indicate whether a name that
11516 names a set of overloaded declarations, some of which are
11517 templates, is a template-name. However, such a name should
11518 be a template-name; otherwise, there is no way to form a
11519 template-id for the overloaded templates. */
11520 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11521 if (TREE_CODE (fns) == OVERLOAD)
11522 for (fn = fns; fn; fn = OVL_NEXT (fn))
11523 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11528 /* The name does not name a template. */
11529 cp_parser_error (parser, "expected template-name");
11530 return error_mark_node;
11534 /* If DECL is dependent, and refers to a function, then just return
11535 its name; we will look it up again during template instantiation. */
11536 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11538 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11539 if (TYPE_P (scope) && dependent_type_p (scope))
11546 /* Parse a template-argument-list.
11548 template-argument-list:
11549 template-argument ... [opt]
11550 template-argument-list , template-argument ... [opt]
11552 Returns a TREE_VEC containing the arguments. */
11555 cp_parser_template_argument_list (cp_parser* parser)
11557 tree fixed_args[10];
11558 unsigned n_args = 0;
11559 unsigned alloced = 10;
11560 tree *arg_ary = fixed_args;
11562 bool saved_in_template_argument_list_p;
11564 bool saved_non_ice_p;
11566 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11567 parser->in_template_argument_list_p = true;
11568 /* Even if the template-id appears in an integral
11569 constant-expression, the contents of the argument list do
11571 saved_ice_p = parser->integral_constant_expression_p;
11572 parser->integral_constant_expression_p = false;
11573 saved_non_ice_p = parser->non_integral_constant_expression_p;
11574 parser->non_integral_constant_expression_p = false;
11575 /* Parse the arguments. */
11581 /* Consume the comma. */
11582 cp_lexer_consume_token (parser->lexer);
11584 /* Parse the template-argument. */
11585 argument = cp_parser_template_argument (parser);
11587 /* If the next token is an ellipsis, we're expanding a template
11589 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11591 if (argument == error_mark_node)
11593 cp_token *token = cp_lexer_peek_token (parser->lexer);
11594 error_at (token->location,
11595 "expected parameter pack before %<...%>");
11597 /* Consume the `...' token. */
11598 cp_lexer_consume_token (parser->lexer);
11600 /* Make the argument into a TYPE_PACK_EXPANSION or
11601 EXPR_PACK_EXPANSION. */
11602 argument = make_pack_expansion (argument);
11605 if (n_args == alloced)
11609 if (arg_ary == fixed_args)
11611 arg_ary = XNEWVEC (tree, alloced);
11612 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11615 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11617 arg_ary[n_args++] = argument;
11619 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11621 vec = make_tree_vec (n_args);
11624 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11626 if (arg_ary != fixed_args)
11628 parser->non_integral_constant_expression_p = saved_non_ice_p;
11629 parser->integral_constant_expression_p = saved_ice_p;
11630 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11631 #ifdef ENABLE_CHECKING
11632 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11637 /* Parse a template-argument.
11640 assignment-expression
11644 The representation is that of an assignment-expression, type-id, or
11645 id-expression -- except that the qualified id-expression is
11646 evaluated, so that the value returned is either a DECL or an
11649 Although the standard says "assignment-expression", it forbids
11650 throw-expressions or assignments in the template argument.
11651 Therefore, we use "conditional-expression" instead. */
11654 cp_parser_template_argument (cp_parser* parser)
11659 bool maybe_type_id = false;
11660 cp_token *token = NULL, *argument_start_token = NULL;
11663 /* There's really no way to know what we're looking at, so we just
11664 try each alternative in order.
11668 In a template-argument, an ambiguity between a type-id and an
11669 expression is resolved to a type-id, regardless of the form of
11670 the corresponding template-parameter.
11672 Therefore, we try a type-id first. */
11673 cp_parser_parse_tentatively (parser);
11674 argument = cp_parser_template_type_arg (parser);
11675 /* If there was no error parsing the type-id but the next token is a
11676 '>>', our behavior depends on which dialect of C++ we're
11677 parsing. In C++98, we probably found a typo for '> >'. But there
11678 are type-id which are also valid expressions. For instance:
11680 struct X { int operator >> (int); };
11681 template <int V> struct Foo {};
11684 Here 'X()' is a valid type-id of a function type, but the user just
11685 wanted to write the expression "X() >> 5". Thus, we remember that we
11686 found a valid type-id, but we still try to parse the argument as an
11687 expression to see what happens.
11689 In C++0x, the '>>' will be considered two separate '>'
11691 if (!cp_parser_error_occurred (parser)
11692 && cxx_dialect == cxx98
11693 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11695 maybe_type_id = true;
11696 cp_parser_abort_tentative_parse (parser);
11700 /* If the next token isn't a `,' or a `>', then this argument wasn't
11701 really finished. This means that the argument is not a valid
11703 if (!cp_parser_next_token_ends_template_argument_p (parser))
11704 cp_parser_error (parser, "expected template-argument");
11705 /* If that worked, we're done. */
11706 if (cp_parser_parse_definitely (parser))
11709 /* We're still not sure what the argument will be. */
11710 cp_parser_parse_tentatively (parser);
11711 /* Try a template. */
11712 argument_start_token = cp_lexer_peek_token (parser->lexer);
11713 argument = cp_parser_id_expression (parser,
11714 /*template_keyword_p=*/false,
11715 /*check_dependency_p=*/true,
11717 /*declarator_p=*/false,
11718 /*optional_p=*/false);
11719 /* If the next token isn't a `,' or a `>', then this argument wasn't
11720 really finished. */
11721 if (!cp_parser_next_token_ends_template_argument_p (parser))
11722 cp_parser_error (parser, "expected template-argument");
11723 if (!cp_parser_error_occurred (parser))
11725 /* Figure out what is being referred to. If the id-expression
11726 was for a class template specialization, then we will have a
11727 TYPE_DECL at this point. There is no need to do name lookup
11728 at this point in that case. */
11729 if (TREE_CODE (argument) != TYPE_DECL)
11730 argument = cp_parser_lookup_name (parser, argument,
11732 /*is_template=*/template_p,
11733 /*is_namespace=*/false,
11734 /*check_dependency=*/true,
11735 /*ambiguous_decls=*/NULL,
11736 argument_start_token->location);
11737 if (TREE_CODE (argument) != TEMPLATE_DECL
11738 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11739 cp_parser_error (parser, "expected template-name");
11741 if (cp_parser_parse_definitely (parser))
11743 /* It must be a non-type argument. There permitted cases are given
11744 in [temp.arg.nontype]:
11746 -- an integral constant-expression of integral or enumeration
11749 -- the name of a non-type template-parameter; or
11751 -- the name of an object or function with external linkage...
11753 -- the address of an object or function with external linkage...
11755 -- a pointer to member... */
11756 /* Look for a non-type template parameter. */
11757 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11759 cp_parser_parse_tentatively (parser);
11760 argument = cp_parser_primary_expression (parser,
11761 /*address_p=*/false,
11763 /*template_arg_p=*/true,
11765 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11766 || !cp_parser_next_token_ends_template_argument_p (parser))
11767 cp_parser_simulate_error (parser);
11768 if (cp_parser_parse_definitely (parser))
11772 /* If the next token is "&", the argument must be the address of an
11773 object or function with external linkage. */
11774 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11776 cp_lexer_consume_token (parser->lexer);
11777 /* See if we might have an id-expression. */
11778 token = cp_lexer_peek_token (parser->lexer);
11779 if (token->type == CPP_NAME
11780 || token->keyword == RID_OPERATOR
11781 || token->type == CPP_SCOPE
11782 || token->type == CPP_TEMPLATE_ID
11783 || token->type == CPP_NESTED_NAME_SPECIFIER)
11785 cp_parser_parse_tentatively (parser);
11786 argument = cp_parser_primary_expression (parser,
11789 /*template_arg_p=*/true,
11791 if (cp_parser_error_occurred (parser)
11792 || !cp_parser_next_token_ends_template_argument_p (parser))
11793 cp_parser_abort_tentative_parse (parser);
11798 if (TREE_CODE (argument) == INDIRECT_REF)
11800 gcc_assert (REFERENCE_REF_P (argument));
11801 argument = TREE_OPERAND (argument, 0);
11804 /* If we're in a template, we represent a qualified-id referring
11805 to a static data member as a SCOPE_REF even if the scope isn't
11806 dependent so that we can check access control later. */
11808 if (TREE_CODE (probe) == SCOPE_REF)
11809 probe = TREE_OPERAND (probe, 1);
11810 if (TREE_CODE (probe) == VAR_DECL)
11812 /* A variable without external linkage might still be a
11813 valid constant-expression, so no error is issued here
11814 if the external-linkage check fails. */
11815 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11816 cp_parser_simulate_error (parser);
11818 else if (is_overloaded_fn (argument))
11819 /* All overloaded functions are allowed; if the external
11820 linkage test does not pass, an error will be issued
11824 && (TREE_CODE (argument) == OFFSET_REF
11825 || TREE_CODE (argument) == SCOPE_REF))
11826 /* A pointer-to-member. */
11828 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11831 cp_parser_simulate_error (parser);
11833 if (cp_parser_parse_definitely (parser))
11836 argument = build_x_unary_op (ADDR_EXPR, argument,
11837 tf_warning_or_error);
11842 /* If the argument started with "&", there are no other valid
11843 alternatives at this point. */
11846 cp_parser_error (parser, "invalid non-type template argument");
11847 return error_mark_node;
11850 /* If the argument wasn't successfully parsed as a type-id followed
11851 by '>>', the argument can only be a constant expression now.
11852 Otherwise, we try parsing the constant-expression tentatively,
11853 because the argument could really be a type-id. */
11855 cp_parser_parse_tentatively (parser);
11856 argument = cp_parser_constant_expression (parser,
11857 /*allow_non_constant_p=*/false,
11858 /*non_constant_p=*/NULL);
11859 argument = fold_non_dependent_expr (argument);
11860 if (!maybe_type_id)
11862 if (!cp_parser_next_token_ends_template_argument_p (parser))
11863 cp_parser_error (parser, "expected template-argument");
11864 if (cp_parser_parse_definitely (parser))
11866 /* We did our best to parse the argument as a non type-id, but that
11867 was the only alternative that matched (albeit with a '>' after
11868 it). We can assume it's just a typo from the user, and a
11869 diagnostic will then be issued. */
11870 return cp_parser_template_type_arg (parser);
11873 /* Parse an explicit-instantiation.
11875 explicit-instantiation:
11876 template declaration
11878 Although the standard says `declaration', what it really means is:
11880 explicit-instantiation:
11881 template decl-specifier-seq [opt] declarator [opt] ;
11883 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11884 supposed to be allowed. A defect report has been filed about this
11889 explicit-instantiation:
11890 storage-class-specifier template
11891 decl-specifier-seq [opt] declarator [opt] ;
11892 function-specifier template
11893 decl-specifier-seq [opt] declarator [opt] ; */
11896 cp_parser_explicit_instantiation (cp_parser* parser)
11898 int declares_class_or_enum;
11899 cp_decl_specifier_seq decl_specifiers;
11900 tree extension_specifier = NULL_TREE;
11902 /* Look for an (optional) storage-class-specifier or
11903 function-specifier. */
11904 if (cp_parser_allow_gnu_extensions_p (parser))
11906 extension_specifier
11907 = cp_parser_storage_class_specifier_opt (parser);
11908 if (!extension_specifier)
11909 extension_specifier
11910 = cp_parser_function_specifier_opt (parser,
11911 /*decl_specs=*/NULL);
11914 /* Look for the `template' keyword. */
11915 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
11916 /* Let the front end know that we are processing an explicit
11918 begin_explicit_instantiation ();
11919 /* [temp.explicit] says that we are supposed to ignore access
11920 control while processing explicit instantiation directives. */
11921 push_deferring_access_checks (dk_no_check);
11922 /* Parse a decl-specifier-seq. */
11923 cp_parser_decl_specifier_seq (parser,
11924 CP_PARSER_FLAGS_OPTIONAL,
11926 &declares_class_or_enum);
11927 /* If there was exactly one decl-specifier, and it declared a class,
11928 and there's no declarator, then we have an explicit type
11930 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11934 type = check_tag_decl (&decl_specifiers);
11935 /* Turn access control back on for names used during
11936 template instantiation. */
11937 pop_deferring_access_checks ();
11939 do_type_instantiation (type, extension_specifier,
11940 /*complain=*/tf_error);
11944 cp_declarator *declarator;
11947 /* Parse the declarator. */
11949 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11950 /*ctor_dtor_or_conv_p=*/NULL,
11951 /*parenthesized_p=*/NULL,
11952 /*member_p=*/false);
11953 if (declares_class_or_enum & 2)
11954 cp_parser_check_for_definition_in_return_type (declarator,
11955 decl_specifiers.type,
11956 decl_specifiers.type_location);
11957 if (declarator != cp_error_declarator)
11959 decl = grokdeclarator (declarator, &decl_specifiers,
11960 NORMAL, 0, &decl_specifiers.attributes);
11961 /* Turn access control back on for names used during
11962 template instantiation. */
11963 pop_deferring_access_checks ();
11964 /* Do the explicit instantiation. */
11965 do_decl_instantiation (decl, extension_specifier);
11969 pop_deferring_access_checks ();
11970 /* Skip the body of the explicit instantiation. */
11971 cp_parser_skip_to_end_of_statement (parser);
11974 /* We're done with the instantiation. */
11975 end_explicit_instantiation ();
11977 cp_parser_consume_semicolon_at_end_of_statement (parser);
11980 /* Parse an explicit-specialization.
11982 explicit-specialization:
11983 template < > declaration
11985 Although the standard says `declaration', what it really means is:
11987 explicit-specialization:
11988 template <> decl-specifier [opt] init-declarator [opt] ;
11989 template <> function-definition
11990 template <> explicit-specialization
11991 template <> template-declaration */
11994 cp_parser_explicit_specialization (cp_parser* parser)
11996 bool need_lang_pop;
11997 cp_token *token = cp_lexer_peek_token (parser->lexer);
11999 /* Look for the `template' keyword. */
12000 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12001 /* Look for the `<'. */
12002 cp_parser_require (parser, CPP_LESS, RT_LESS);
12003 /* Look for the `>'. */
12004 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12005 /* We have processed another parameter list. */
12006 ++parser->num_template_parameter_lists;
12009 A template ... explicit specialization ... shall not have C
12011 if (current_lang_name == lang_name_c)
12013 error_at (token->location, "template specialization with C linkage");
12014 /* Give it C++ linkage to avoid confusing other parts of the
12016 push_lang_context (lang_name_cplusplus);
12017 need_lang_pop = true;
12020 need_lang_pop = false;
12021 /* Let the front end know that we are beginning a specialization. */
12022 if (!begin_specialization ())
12024 end_specialization ();
12028 /* If the next keyword is `template', we need to figure out whether
12029 or not we're looking a template-declaration. */
12030 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12032 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12033 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12034 cp_parser_template_declaration_after_export (parser,
12035 /*member_p=*/false);
12037 cp_parser_explicit_specialization (parser);
12040 /* Parse the dependent declaration. */
12041 cp_parser_single_declaration (parser,
12043 /*member_p=*/false,
12044 /*explicit_specialization_p=*/true,
12045 /*friend_p=*/NULL);
12046 /* We're done with the specialization. */
12047 end_specialization ();
12048 /* For the erroneous case of a template with C linkage, we pushed an
12049 implicit C++ linkage scope; exit that scope now. */
12051 pop_lang_context ();
12052 /* We're done with this parameter list. */
12053 --parser->num_template_parameter_lists;
12056 /* Parse a type-specifier.
12059 simple-type-specifier
12062 elaborated-type-specifier
12070 Returns a representation of the type-specifier. For a
12071 class-specifier, enum-specifier, or elaborated-type-specifier, a
12072 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12074 The parser flags FLAGS is used to control type-specifier parsing.
12076 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12077 in a decl-specifier-seq.
12079 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12080 class-specifier, enum-specifier, or elaborated-type-specifier, then
12081 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12082 if a type is declared; 2 if it is defined. Otherwise, it is set to
12085 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12086 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12087 is set to FALSE. */
12090 cp_parser_type_specifier (cp_parser* parser,
12091 cp_parser_flags flags,
12092 cp_decl_specifier_seq *decl_specs,
12093 bool is_declaration,
12094 int* declares_class_or_enum,
12095 bool* is_cv_qualifier)
12097 tree type_spec = NULL_TREE;
12100 cp_decl_spec ds = ds_last;
12102 /* Assume this type-specifier does not declare a new type. */
12103 if (declares_class_or_enum)
12104 *declares_class_or_enum = 0;
12105 /* And that it does not specify a cv-qualifier. */
12106 if (is_cv_qualifier)
12107 *is_cv_qualifier = false;
12108 /* Peek at the next token. */
12109 token = cp_lexer_peek_token (parser->lexer);
12111 /* If we're looking at a keyword, we can use that to guide the
12112 production we choose. */
12113 keyword = token->keyword;
12117 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12118 goto elaborated_type_specifier;
12120 /* Look for the enum-specifier. */
12121 type_spec = cp_parser_enum_specifier (parser);
12122 /* If that worked, we're done. */
12125 if (declares_class_or_enum)
12126 *declares_class_or_enum = 2;
12128 cp_parser_set_decl_spec_type (decl_specs,
12131 /*user_defined_p=*/true);
12135 goto elaborated_type_specifier;
12137 /* Any of these indicate either a class-specifier, or an
12138 elaborated-type-specifier. */
12142 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12143 goto elaborated_type_specifier;
12145 /* Parse tentatively so that we can back up if we don't find a
12146 class-specifier. */
12147 cp_parser_parse_tentatively (parser);
12148 /* Look for the class-specifier. */
12149 type_spec = cp_parser_class_specifier (parser);
12150 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12151 /* If that worked, we're done. */
12152 if (cp_parser_parse_definitely (parser))
12154 if (declares_class_or_enum)
12155 *declares_class_or_enum = 2;
12157 cp_parser_set_decl_spec_type (decl_specs,
12160 /*user_defined_p=*/true);
12164 /* Fall through. */
12165 elaborated_type_specifier:
12166 /* We're declaring (not defining) a class or enum. */
12167 if (declares_class_or_enum)
12168 *declares_class_or_enum = 1;
12170 /* Fall through. */
12172 /* Look for an elaborated-type-specifier. */
12174 = (cp_parser_elaborated_type_specifier
12176 decl_specs && decl_specs->specs[(int) ds_friend],
12179 cp_parser_set_decl_spec_type (decl_specs,
12182 /*user_defined_p=*/true);
12187 if (is_cv_qualifier)
12188 *is_cv_qualifier = true;
12193 if (is_cv_qualifier)
12194 *is_cv_qualifier = true;
12199 if (is_cv_qualifier)
12200 *is_cv_qualifier = true;
12204 /* The `__complex__' keyword is a GNU extension. */
12212 /* Handle simple keywords. */
12217 ++decl_specs->specs[(int)ds];
12218 decl_specs->any_specifiers_p = true;
12220 return cp_lexer_consume_token (parser->lexer)->u.value;
12223 /* If we do not already have a type-specifier, assume we are looking
12224 at a simple-type-specifier. */
12225 type_spec = cp_parser_simple_type_specifier (parser,
12229 /* If we didn't find a type-specifier, and a type-specifier was not
12230 optional in this context, issue an error message. */
12231 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12233 cp_parser_error (parser, "expected type specifier");
12234 return error_mark_node;
12240 /* Parse a simple-type-specifier.
12242 simple-type-specifier:
12243 :: [opt] nested-name-specifier [opt] type-name
12244 :: [opt] nested-name-specifier template template-id
12259 simple-type-specifier:
12261 decltype ( expression )
12267 simple-type-specifier:
12269 __typeof__ unary-expression
12270 __typeof__ ( type-id )
12272 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12273 appropriately updated. */
12276 cp_parser_simple_type_specifier (cp_parser* parser,
12277 cp_decl_specifier_seq *decl_specs,
12278 cp_parser_flags flags)
12280 tree type = NULL_TREE;
12283 /* Peek at the next token. */
12284 token = cp_lexer_peek_token (parser->lexer);
12286 /* If we're looking at a keyword, things are easy. */
12287 switch (token->keyword)
12291 decl_specs->explicit_char_p = true;
12292 type = char_type_node;
12295 type = char16_type_node;
12298 type = char32_type_node;
12301 type = wchar_type_node;
12304 type = boolean_type_node;
12308 ++decl_specs->specs[(int) ds_short];
12309 type = short_integer_type_node;
12313 decl_specs->explicit_int_p = true;
12314 type = integer_type_node;
12317 if (!int128_integer_type_node)
12320 decl_specs->explicit_int128_p = true;
12321 type = int128_integer_type_node;
12325 ++decl_specs->specs[(int) ds_long];
12326 type = long_integer_type_node;
12330 ++decl_specs->specs[(int) ds_signed];
12331 type = integer_type_node;
12335 ++decl_specs->specs[(int) ds_unsigned];
12336 type = unsigned_type_node;
12339 type = float_type_node;
12342 type = double_type_node;
12345 type = void_type_node;
12349 maybe_warn_cpp0x (CPP0X_AUTO);
12350 type = make_auto ();
12354 /* Parse the `decltype' type. */
12355 type = cp_parser_decltype (parser);
12358 cp_parser_set_decl_spec_type (decl_specs, type,
12360 /*user_defined_p=*/true);
12365 /* Consume the `typeof' token. */
12366 cp_lexer_consume_token (parser->lexer);
12367 /* Parse the operand to `typeof'. */
12368 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12369 /* If it is not already a TYPE, take its type. */
12370 if (!TYPE_P (type))
12371 type = finish_typeof (type);
12374 cp_parser_set_decl_spec_type (decl_specs, type,
12376 /*user_defined_p=*/true);
12384 /* If the type-specifier was for a built-in type, we're done. */
12387 /* Record the type. */
12389 && (token->keyword != RID_SIGNED
12390 && token->keyword != RID_UNSIGNED
12391 && token->keyword != RID_SHORT
12392 && token->keyword != RID_LONG))
12393 cp_parser_set_decl_spec_type (decl_specs,
12396 /*user_defined=*/false);
12398 decl_specs->any_specifiers_p = true;
12400 /* Consume the token. */
12401 cp_lexer_consume_token (parser->lexer);
12403 /* There is no valid C++ program where a non-template type is
12404 followed by a "<". That usually indicates that the user thought
12405 that the type was a template. */
12406 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12408 return TYPE_NAME (type);
12411 /* The type-specifier must be a user-defined type. */
12412 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12417 /* Don't gobble tokens or issue error messages if this is an
12418 optional type-specifier. */
12419 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12420 cp_parser_parse_tentatively (parser);
12422 /* Look for the optional `::' operator. */
12424 = (cp_parser_global_scope_opt (parser,
12425 /*current_scope_valid_p=*/false)
12427 /* Look for the nested-name specifier. */
12429 = (cp_parser_nested_name_specifier_opt (parser,
12430 /*typename_keyword_p=*/false,
12431 /*check_dependency_p=*/true,
12433 /*is_declaration=*/false)
12435 token = cp_lexer_peek_token (parser->lexer);
12436 /* If we have seen a nested-name-specifier, and the next token
12437 is `template', then we are using the template-id production. */
12439 && cp_parser_optional_template_keyword (parser))
12441 /* Look for the template-id. */
12442 type = cp_parser_template_id (parser,
12443 /*template_keyword_p=*/true,
12444 /*check_dependency_p=*/true,
12445 /*is_declaration=*/false);
12446 /* If the template-id did not name a type, we are out of
12448 if (TREE_CODE (type) != TYPE_DECL)
12450 cp_parser_error (parser, "expected template-id for type");
12454 /* Otherwise, look for a type-name. */
12456 type = cp_parser_type_name (parser);
12457 /* Keep track of all name-lookups performed in class scopes. */
12461 && TREE_CODE (type) == TYPE_DECL
12462 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12463 maybe_note_name_used_in_class (DECL_NAME (type), type);
12464 /* If it didn't work out, we don't have a TYPE. */
12465 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12466 && !cp_parser_parse_definitely (parser))
12468 if (type && decl_specs)
12469 cp_parser_set_decl_spec_type (decl_specs, type,
12471 /*user_defined=*/true);
12474 /* If we didn't get a type-name, issue an error message. */
12475 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12477 cp_parser_error (parser, "expected type-name");
12478 return error_mark_node;
12481 /* There is no valid C++ program where a non-template type is
12482 followed by a "<". That usually indicates that the user thought
12483 that the type was a template. */
12484 if (type && type != error_mark_node)
12486 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12487 If it is, then the '<'...'>' enclose protocol names rather than
12488 template arguments, and so everything is fine. */
12489 if (c_dialect_objc ()
12490 && (objc_is_id (type) || objc_is_class_name (type)))
12492 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12493 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12495 /* Clobber the "unqualified" type previously entered into
12496 DECL_SPECS with the new, improved protocol-qualified version. */
12498 decl_specs->type = qual_type;
12503 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12510 /* Parse a type-name.
12523 Returns a TYPE_DECL for the type. */
12526 cp_parser_type_name (cp_parser* parser)
12530 /* We can't know yet whether it is a class-name or not. */
12531 cp_parser_parse_tentatively (parser);
12532 /* Try a class-name. */
12533 type_decl = cp_parser_class_name (parser,
12534 /*typename_keyword_p=*/false,
12535 /*template_keyword_p=*/false,
12537 /*check_dependency_p=*/true,
12538 /*class_head_p=*/false,
12539 /*is_declaration=*/false);
12540 /* If it's not a class-name, keep looking. */
12541 if (!cp_parser_parse_definitely (parser))
12543 /* It must be a typedef-name or an enum-name. */
12544 return cp_parser_nonclass_name (parser);
12550 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12558 Returns a TYPE_DECL for the type. */
12561 cp_parser_nonclass_name (cp_parser* parser)
12566 cp_token *token = cp_lexer_peek_token (parser->lexer);
12567 identifier = cp_parser_identifier (parser);
12568 if (identifier == error_mark_node)
12569 return error_mark_node;
12571 /* Look up the type-name. */
12572 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12574 if (TREE_CODE (type_decl) != TYPE_DECL
12575 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12577 /* See if this is an Objective-C type. */
12578 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12579 tree type = objc_get_protocol_qualified_type (identifier, protos);
12581 type_decl = TYPE_NAME (type);
12584 /* Issue an error if we did not find a type-name. */
12585 if (TREE_CODE (type_decl) != TYPE_DECL)
12587 if (!cp_parser_simulate_error (parser))
12588 cp_parser_name_lookup_error (parser, identifier, type_decl,
12589 NLE_TYPE, token->location);
12590 return error_mark_node;
12592 /* Remember that the name was used in the definition of the
12593 current class so that we can check later to see if the
12594 meaning would have been different after the class was
12595 entirely defined. */
12596 else if (type_decl != error_mark_node
12598 maybe_note_name_used_in_class (identifier, type_decl);
12603 /* Parse an elaborated-type-specifier. Note that the grammar given
12604 here incorporates the resolution to DR68.
12606 elaborated-type-specifier:
12607 class-key :: [opt] nested-name-specifier [opt] identifier
12608 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12609 enum-key :: [opt] nested-name-specifier [opt] identifier
12610 typename :: [opt] nested-name-specifier identifier
12611 typename :: [opt] nested-name-specifier template [opt]
12616 elaborated-type-specifier:
12617 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12618 class-key attributes :: [opt] nested-name-specifier [opt]
12619 template [opt] template-id
12620 enum attributes :: [opt] nested-name-specifier [opt] identifier
12622 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12623 declared `friend'. If IS_DECLARATION is TRUE, then this
12624 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12625 something is being declared.
12627 Returns the TYPE specified. */
12630 cp_parser_elaborated_type_specifier (cp_parser* parser,
12632 bool is_declaration)
12634 enum tag_types tag_type;
12636 tree type = NULL_TREE;
12637 tree attributes = NULL_TREE;
12639 cp_token *token = NULL;
12641 /* See if we're looking at the `enum' keyword. */
12642 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12644 /* Consume the `enum' token. */
12645 cp_lexer_consume_token (parser->lexer);
12646 /* Remember that it's an enumeration type. */
12647 tag_type = enum_type;
12648 /* Parse the optional `struct' or `class' key (for C++0x scoped
12650 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12651 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12653 if (cxx_dialect == cxx98)
12654 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12656 /* Consume the `struct' or `class'. */
12657 cp_lexer_consume_token (parser->lexer);
12659 /* Parse the attributes. */
12660 attributes = cp_parser_attributes_opt (parser);
12662 /* Or, it might be `typename'. */
12663 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12666 /* Consume the `typename' token. */
12667 cp_lexer_consume_token (parser->lexer);
12668 /* Remember that it's a `typename' type. */
12669 tag_type = typename_type;
12671 /* Otherwise it must be a class-key. */
12674 tag_type = cp_parser_class_key (parser);
12675 if (tag_type == none_type)
12676 return error_mark_node;
12677 /* Parse the attributes. */
12678 attributes = cp_parser_attributes_opt (parser);
12681 /* Look for the `::' operator. */
12682 globalscope = cp_parser_global_scope_opt (parser,
12683 /*current_scope_valid_p=*/false);
12684 /* Look for the nested-name-specifier. */
12685 if (tag_type == typename_type && !globalscope)
12687 if (!cp_parser_nested_name_specifier (parser,
12688 /*typename_keyword_p=*/true,
12689 /*check_dependency_p=*/true,
12692 return error_mark_node;
12695 /* Even though `typename' is not present, the proposed resolution
12696 to Core Issue 180 says that in `class A<T>::B', `B' should be
12697 considered a type-name, even if `A<T>' is dependent. */
12698 cp_parser_nested_name_specifier_opt (parser,
12699 /*typename_keyword_p=*/true,
12700 /*check_dependency_p=*/true,
12703 /* For everything but enumeration types, consider a template-id.
12704 For an enumeration type, consider only a plain identifier. */
12705 if (tag_type != enum_type)
12707 bool template_p = false;
12710 /* Allow the `template' keyword. */
12711 template_p = cp_parser_optional_template_keyword (parser);
12712 /* If we didn't see `template', we don't know if there's a
12713 template-id or not. */
12715 cp_parser_parse_tentatively (parser);
12716 /* Parse the template-id. */
12717 token = cp_lexer_peek_token (parser->lexer);
12718 decl = cp_parser_template_id (parser, template_p,
12719 /*check_dependency_p=*/true,
12721 /* If we didn't find a template-id, look for an ordinary
12723 if (!template_p && !cp_parser_parse_definitely (parser))
12725 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12726 in effect, then we must assume that, upon instantiation, the
12727 template will correspond to a class. */
12728 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12729 && tag_type == typename_type)
12730 type = make_typename_type (parser->scope, decl,
12732 /*complain=*/tf_error);
12733 /* If the `typename' keyword is in effect and DECL is not a type
12734 decl. Then type is non existant. */
12735 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12738 type = TREE_TYPE (decl);
12743 token = cp_lexer_peek_token (parser->lexer);
12744 identifier = cp_parser_identifier (parser);
12746 if (identifier == error_mark_node)
12748 parser->scope = NULL_TREE;
12749 return error_mark_node;
12752 /* For a `typename', we needn't call xref_tag. */
12753 if (tag_type == typename_type
12754 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12755 return cp_parser_make_typename_type (parser, parser->scope,
12758 /* Look up a qualified name in the usual way. */
12762 tree ambiguous_decls;
12764 decl = cp_parser_lookup_name (parser, identifier,
12766 /*is_template=*/false,
12767 /*is_namespace=*/false,
12768 /*check_dependency=*/true,
12772 /* If the lookup was ambiguous, an error will already have been
12774 if (ambiguous_decls)
12775 return error_mark_node;
12777 /* If we are parsing friend declaration, DECL may be a
12778 TEMPLATE_DECL tree node here. However, we need to check
12779 whether this TEMPLATE_DECL results in valid code. Consider
12780 the following example:
12783 template <class T> class C {};
12786 template <class T> friend class N::C; // #1, valid code
12788 template <class T> class Y {
12789 friend class N::C; // #2, invalid code
12792 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12793 name lookup of `N::C'. We see that friend declaration must
12794 be template for the code to be valid. Note that
12795 processing_template_decl does not work here since it is
12796 always 1 for the above two cases. */
12798 decl = (cp_parser_maybe_treat_template_as_class
12799 (decl, /*tag_name_p=*/is_friend
12800 && parser->num_template_parameter_lists));
12802 if (TREE_CODE (decl) != TYPE_DECL)
12804 cp_parser_diagnose_invalid_type_name (parser,
12808 return error_mark_node;
12811 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12813 bool allow_template = (parser->num_template_parameter_lists
12814 || DECL_SELF_REFERENCE_P (decl));
12815 type = check_elaborated_type_specifier (tag_type, decl,
12818 if (type == error_mark_node)
12819 return error_mark_node;
12822 /* Forward declarations of nested types, such as
12827 are invalid unless all components preceding the final '::'
12828 are complete. If all enclosing types are complete, these
12829 declarations become merely pointless.
12831 Invalid forward declarations of nested types are errors
12832 caught elsewhere in parsing. Those that are pointless arrive
12835 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12836 && !is_friend && !processing_explicit_instantiation)
12837 warning (0, "declaration %qD does not declare anything", decl);
12839 type = TREE_TYPE (decl);
12843 /* An elaborated-type-specifier sometimes introduces a new type and
12844 sometimes names an existing type. Normally, the rule is that it
12845 introduces a new type only if there is not an existing type of
12846 the same name already in scope. For example, given:
12849 void f() { struct S s; }
12851 the `struct S' in the body of `f' is the same `struct S' as in
12852 the global scope; the existing definition is used. However, if
12853 there were no global declaration, this would introduce a new
12854 local class named `S'.
12856 An exception to this rule applies to the following code:
12858 namespace N { struct S; }
12860 Here, the elaborated-type-specifier names a new type
12861 unconditionally; even if there is already an `S' in the
12862 containing scope this declaration names a new type.
12863 This exception only applies if the elaborated-type-specifier
12864 forms the complete declaration:
12868 A declaration consisting solely of `class-key identifier ;' is
12869 either a redeclaration of the name in the current scope or a
12870 forward declaration of the identifier as a class name. It
12871 introduces the name into the current scope.
12873 We are in this situation precisely when the next token is a `;'.
12875 An exception to the exception is that a `friend' declaration does
12876 *not* name a new type; i.e., given:
12878 struct S { friend struct T; };
12880 `T' is not a new type in the scope of `S'.
12882 Also, `new struct S' or `sizeof (struct S)' never results in the
12883 definition of a new type; a new type can only be declared in a
12884 declaration context. */
12890 /* Friends have special name lookup rules. */
12891 ts = ts_within_enclosing_non_class;
12892 else if (is_declaration
12893 && cp_lexer_next_token_is (parser->lexer,
12895 /* This is a `class-key identifier ;' */
12901 (parser->num_template_parameter_lists
12902 && (cp_parser_next_token_starts_class_definition_p (parser)
12903 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12904 /* An unqualified name was used to reference this type, so
12905 there were no qualifying templates. */
12906 if (!cp_parser_check_template_parameters (parser,
12907 /*num_templates=*/0,
12909 /*declarator=*/NULL))
12910 return error_mark_node;
12911 type = xref_tag (tag_type, identifier, ts, template_p);
12915 if (type == error_mark_node)
12916 return error_mark_node;
12918 /* Allow attributes on forward declarations of classes. */
12921 if (TREE_CODE (type) == TYPENAME_TYPE)
12922 warning (OPT_Wattributes,
12923 "attributes ignored on uninstantiated type");
12924 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12925 && ! processing_explicit_instantiation)
12926 warning (OPT_Wattributes,
12927 "attributes ignored on template instantiation");
12928 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12929 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12931 warning (OPT_Wattributes,
12932 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12935 if (tag_type != enum_type)
12936 cp_parser_check_class_key (tag_type, type);
12938 /* A "<" cannot follow an elaborated type specifier. If that
12939 happens, the user was probably trying to form a template-id. */
12940 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12945 /* Parse an enum-specifier.
12948 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12953 enum struct [C++0x]
12956 : type-specifier-seq
12959 enum-key attributes[opt] identifier [opt] enum-base [opt]
12960 { enumerator-list [opt] }attributes[opt]
12962 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12963 if the token stream isn't an enum-specifier after all. */
12966 cp_parser_enum_specifier (cp_parser* parser)
12971 bool scoped_enum_p = false;
12972 bool has_underlying_type = false;
12973 tree underlying_type = NULL_TREE;
12975 /* Parse tentatively so that we can back up if we don't find a
12977 cp_parser_parse_tentatively (parser);
12979 /* Caller guarantees that the current token is 'enum', an identifier
12980 possibly follows, and the token after that is an opening brace.
12981 If we don't have an identifier, fabricate an anonymous name for
12982 the enumeration being defined. */
12983 cp_lexer_consume_token (parser->lexer);
12985 /* Parse the "class" or "struct", which indicates a scoped
12986 enumeration type in C++0x. */
12987 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12988 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12990 if (cxx_dialect == cxx98)
12991 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12993 /* Consume the `struct' or `class' token. */
12994 cp_lexer_consume_token (parser->lexer);
12996 scoped_enum_p = true;
12999 attributes = cp_parser_attributes_opt (parser);
13001 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13002 identifier = cp_parser_identifier (parser);
13004 identifier = make_anon_name ();
13006 /* Check for the `:' that denotes a specified underlying type in C++0x.
13007 Note that a ':' could also indicate a bitfield width, however. */
13008 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13010 cp_decl_specifier_seq type_specifiers;
13012 /* Consume the `:'. */
13013 cp_lexer_consume_token (parser->lexer);
13015 /* Parse the type-specifier-seq. */
13016 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13017 /*is_trailing_return=*/false,
13020 /* At this point this is surely not elaborated type specifier. */
13021 if (!cp_parser_parse_definitely (parser))
13024 if (cxx_dialect == cxx98)
13025 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13027 has_underlying_type = true;
13029 /* If that didn't work, stop. */
13030 if (type_specifiers.type != error_mark_node)
13032 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13033 /*initialized=*/0, NULL);
13034 if (underlying_type == error_mark_node)
13035 underlying_type = NULL_TREE;
13039 /* Look for the `{' but don't consume it yet. */
13040 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13042 cp_parser_error (parser, "expected %<{%>");
13043 if (has_underlying_type)
13047 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13050 /* Issue an error message if type-definitions are forbidden here. */
13051 if (!cp_parser_check_type_definition (parser))
13052 type = error_mark_node;
13054 /* Create the new type. We do this before consuming the opening
13055 brace so the enum will be recorded as being on the line of its
13056 tag (or the 'enum' keyword, if there is no tag). */
13057 type = start_enum (identifier, underlying_type, scoped_enum_p);
13059 /* Consume the opening brace. */
13060 cp_lexer_consume_token (parser->lexer);
13062 if (type == error_mark_node)
13064 cp_parser_skip_to_end_of_block_or_statement (parser);
13065 return error_mark_node;
13068 /* If the next token is not '}', then there are some enumerators. */
13069 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13070 cp_parser_enumerator_list (parser, type);
13072 /* Consume the final '}'. */
13073 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13075 /* Look for trailing attributes to apply to this enumeration, and
13076 apply them if appropriate. */
13077 if (cp_parser_allow_gnu_extensions_p (parser))
13079 tree trailing_attr = cp_parser_attributes_opt (parser);
13080 trailing_attr = chainon (trailing_attr, attributes);
13081 cplus_decl_attributes (&type,
13083 (int) ATTR_FLAG_TYPE_IN_PLACE);
13086 /* Finish up the enumeration. */
13087 finish_enum (type);
13092 /* Parse an enumerator-list. The enumerators all have the indicated
13096 enumerator-definition
13097 enumerator-list , enumerator-definition */
13100 cp_parser_enumerator_list (cp_parser* parser, tree type)
13104 /* Parse an enumerator-definition. */
13105 cp_parser_enumerator_definition (parser, type);
13107 /* If the next token is not a ',', we've reached the end of
13109 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13111 /* Otherwise, consume the `,' and keep going. */
13112 cp_lexer_consume_token (parser->lexer);
13113 /* If the next token is a `}', there is a trailing comma. */
13114 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13116 if (!in_system_header)
13117 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13123 /* Parse an enumerator-definition. The enumerator has the indicated
13126 enumerator-definition:
13128 enumerator = constant-expression
13134 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13139 /* Look for the identifier. */
13140 identifier = cp_parser_identifier (parser);
13141 if (identifier == error_mark_node)
13144 /* If the next token is an '=', then there is an explicit value. */
13145 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13147 /* Consume the `=' token. */
13148 cp_lexer_consume_token (parser->lexer);
13149 /* Parse the value. */
13150 value = cp_parser_constant_expression (parser,
13151 /*allow_non_constant_p=*/false,
13157 /* If we are processing a template, make sure the initializer of the
13158 enumerator doesn't contain any bare template parameter pack. */
13159 if (check_for_bare_parameter_packs (value))
13160 value = error_mark_node;
13162 /* Create the enumerator. */
13163 build_enumerator (identifier, value, type);
13166 /* Parse a namespace-name.
13169 original-namespace-name
13172 Returns the NAMESPACE_DECL for the namespace. */
13175 cp_parser_namespace_name (cp_parser* parser)
13178 tree namespace_decl;
13180 cp_token *token = cp_lexer_peek_token (parser->lexer);
13182 /* Get the name of the namespace. */
13183 identifier = cp_parser_identifier (parser);
13184 if (identifier == error_mark_node)
13185 return error_mark_node;
13187 /* Look up the identifier in the currently active scope. Look only
13188 for namespaces, due to:
13190 [basic.lookup.udir]
13192 When looking up a namespace-name in a using-directive or alias
13193 definition, only namespace names are considered.
13197 [basic.lookup.qual]
13199 During the lookup of a name preceding the :: scope resolution
13200 operator, object, function, and enumerator names are ignored.
13202 (Note that cp_parser_qualifying_entity only calls this
13203 function if the token after the name is the scope resolution
13205 namespace_decl = cp_parser_lookup_name (parser, identifier,
13207 /*is_template=*/false,
13208 /*is_namespace=*/true,
13209 /*check_dependency=*/true,
13210 /*ambiguous_decls=*/NULL,
13212 /* If it's not a namespace, issue an error. */
13213 if (namespace_decl == error_mark_node
13214 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13216 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13217 error_at (token->location, "%qD is not a namespace-name", identifier);
13218 cp_parser_error (parser, "expected namespace-name");
13219 namespace_decl = error_mark_node;
13222 return namespace_decl;
13225 /* Parse a namespace-definition.
13227 namespace-definition:
13228 named-namespace-definition
13229 unnamed-namespace-definition
13231 named-namespace-definition:
13232 original-namespace-definition
13233 extension-namespace-definition
13235 original-namespace-definition:
13236 namespace identifier { namespace-body }
13238 extension-namespace-definition:
13239 namespace original-namespace-name { namespace-body }
13241 unnamed-namespace-definition:
13242 namespace { namespace-body } */
13245 cp_parser_namespace_definition (cp_parser* parser)
13247 tree identifier, attribs;
13248 bool has_visibility;
13251 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13254 cp_lexer_consume_token (parser->lexer);
13259 /* Look for the `namespace' keyword. */
13260 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13262 /* Get the name of the namespace. We do not attempt to distinguish
13263 between an original-namespace-definition and an
13264 extension-namespace-definition at this point. The semantic
13265 analysis routines are responsible for that. */
13266 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13267 identifier = cp_parser_identifier (parser);
13269 identifier = NULL_TREE;
13271 /* Parse any specified attributes. */
13272 attribs = cp_parser_attributes_opt (parser);
13274 /* Look for the `{' to start the namespace. */
13275 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13276 /* Start the namespace. */
13277 push_namespace (identifier);
13279 /* "inline namespace" is equivalent to a stub namespace definition
13280 followed by a strong using directive. */
13283 tree name_space = current_namespace;
13284 /* Set up namespace association. */
13285 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13286 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13287 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13288 /* Import the contents of the inline namespace. */
13290 do_using_directive (name_space);
13291 push_namespace (identifier);
13294 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13296 /* Parse the body of the namespace. */
13297 cp_parser_namespace_body (parser);
13299 #ifdef HANDLE_PRAGMA_VISIBILITY
13300 if (has_visibility)
13301 pop_visibility (1);
13304 /* Finish the namespace. */
13306 /* Look for the final `}'. */
13307 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13310 /* Parse a namespace-body.
13313 declaration-seq [opt] */
13316 cp_parser_namespace_body (cp_parser* parser)
13318 cp_parser_declaration_seq_opt (parser);
13321 /* Parse a namespace-alias-definition.
13323 namespace-alias-definition:
13324 namespace identifier = qualified-namespace-specifier ; */
13327 cp_parser_namespace_alias_definition (cp_parser* parser)
13330 tree namespace_specifier;
13332 cp_token *token = cp_lexer_peek_token (parser->lexer);
13334 /* Look for the `namespace' keyword. */
13335 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13336 /* Look for the identifier. */
13337 identifier = cp_parser_identifier (parser);
13338 if (identifier == error_mark_node)
13340 /* Look for the `=' token. */
13341 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13342 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13344 error_at (token->location, "%<namespace%> definition is not allowed here");
13345 /* Skip the definition. */
13346 cp_lexer_consume_token (parser->lexer);
13347 if (cp_parser_skip_to_closing_brace (parser))
13348 cp_lexer_consume_token (parser->lexer);
13351 cp_parser_require (parser, CPP_EQ, RT_EQ);
13352 /* Look for the qualified-namespace-specifier. */
13353 namespace_specifier
13354 = cp_parser_qualified_namespace_specifier (parser);
13355 /* Look for the `;' token. */
13356 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13358 /* Register the alias in the symbol table. */
13359 do_namespace_alias (identifier, namespace_specifier);
13362 /* Parse a qualified-namespace-specifier.
13364 qualified-namespace-specifier:
13365 :: [opt] nested-name-specifier [opt] namespace-name
13367 Returns a NAMESPACE_DECL corresponding to the specified
13371 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13373 /* Look for the optional `::'. */
13374 cp_parser_global_scope_opt (parser,
13375 /*current_scope_valid_p=*/false);
13377 /* Look for the optional nested-name-specifier. */
13378 cp_parser_nested_name_specifier_opt (parser,
13379 /*typename_keyword_p=*/false,
13380 /*check_dependency_p=*/true,
13382 /*is_declaration=*/true);
13384 return cp_parser_namespace_name (parser);
13387 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13388 access declaration.
13391 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13392 using :: unqualified-id ;
13394 access-declaration:
13400 cp_parser_using_declaration (cp_parser* parser,
13401 bool access_declaration_p)
13404 bool typename_p = false;
13405 bool global_scope_p;
13410 if (access_declaration_p)
13411 cp_parser_parse_tentatively (parser);
13414 /* Look for the `using' keyword. */
13415 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13417 /* Peek at the next token. */
13418 token = cp_lexer_peek_token (parser->lexer);
13419 /* See if it's `typename'. */
13420 if (token->keyword == RID_TYPENAME)
13422 /* Remember that we've seen it. */
13424 /* Consume the `typename' token. */
13425 cp_lexer_consume_token (parser->lexer);
13429 /* Look for the optional global scope qualification. */
13431 = (cp_parser_global_scope_opt (parser,
13432 /*current_scope_valid_p=*/false)
13435 /* If we saw `typename', or didn't see `::', then there must be a
13436 nested-name-specifier present. */
13437 if (typename_p || !global_scope_p)
13438 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13439 /*check_dependency_p=*/true,
13441 /*is_declaration=*/true);
13442 /* Otherwise, we could be in either of the two productions. In that
13443 case, treat the nested-name-specifier as optional. */
13445 qscope = cp_parser_nested_name_specifier_opt (parser,
13446 /*typename_keyword_p=*/false,
13447 /*check_dependency_p=*/true,
13449 /*is_declaration=*/true);
13451 qscope = global_namespace;
13453 if (access_declaration_p && cp_parser_error_occurred (parser))
13454 /* Something has already gone wrong; there's no need to parse
13455 further. Since an error has occurred, the return value of
13456 cp_parser_parse_definitely will be false, as required. */
13457 return cp_parser_parse_definitely (parser);
13459 token = cp_lexer_peek_token (parser->lexer);
13460 /* Parse the unqualified-id. */
13461 identifier = cp_parser_unqualified_id (parser,
13462 /*template_keyword_p=*/false,
13463 /*check_dependency_p=*/true,
13464 /*declarator_p=*/true,
13465 /*optional_p=*/false);
13467 if (access_declaration_p)
13469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13470 cp_parser_simulate_error (parser);
13471 if (!cp_parser_parse_definitely (parser))
13475 /* The function we call to handle a using-declaration is different
13476 depending on what scope we are in. */
13477 if (qscope == error_mark_node || identifier == error_mark_node)
13479 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13480 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13481 /* [namespace.udecl]
13483 A using declaration shall not name a template-id. */
13484 error_at (token->location,
13485 "a template-id may not appear in a using-declaration");
13488 if (at_class_scope_p ())
13490 /* Create the USING_DECL. */
13491 decl = do_class_using_decl (parser->scope, identifier);
13493 if (check_for_bare_parameter_packs (decl))
13496 /* Add it to the list of members in this class. */
13497 finish_member_declaration (decl);
13501 decl = cp_parser_lookup_name_simple (parser,
13504 if (decl == error_mark_node)
13505 cp_parser_name_lookup_error (parser, identifier,
13508 else if (check_for_bare_parameter_packs (decl))
13510 else if (!at_namespace_scope_p ())
13511 do_local_using_decl (decl, qscope, identifier);
13513 do_toplevel_using_decl (decl, qscope, identifier);
13517 /* Look for the final `;'. */
13518 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13523 /* Parse a using-directive.
13526 using namespace :: [opt] nested-name-specifier [opt]
13527 namespace-name ; */
13530 cp_parser_using_directive (cp_parser* parser)
13532 tree namespace_decl;
13535 /* Look for the `using' keyword. */
13536 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13537 /* And the `namespace' keyword. */
13538 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13539 /* Look for the optional `::' operator. */
13540 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13541 /* And the optional nested-name-specifier. */
13542 cp_parser_nested_name_specifier_opt (parser,
13543 /*typename_keyword_p=*/false,
13544 /*check_dependency_p=*/true,
13546 /*is_declaration=*/true);
13547 /* Get the namespace being used. */
13548 namespace_decl = cp_parser_namespace_name (parser);
13549 /* And any specified attributes. */
13550 attribs = cp_parser_attributes_opt (parser);
13551 /* Update the symbol table. */
13552 parse_using_directive (namespace_decl, attribs);
13553 /* Look for the final `;'. */
13554 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13557 /* Parse an asm-definition.
13560 asm ( string-literal ) ;
13565 asm volatile [opt] ( string-literal ) ;
13566 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13567 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13568 : asm-operand-list [opt] ) ;
13569 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13570 : asm-operand-list [opt]
13571 : asm-clobber-list [opt] ) ;
13572 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13573 : asm-clobber-list [opt]
13574 : asm-goto-list ) ; */
13577 cp_parser_asm_definition (cp_parser* parser)
13580 tree outputs = NULL_TREE;
13581 tree inputs = NULL_TREE;
13582 tree clobbers = NULL_TREE;
13583 tree labels = NULL_TREE;
13585 bool volatile_p = false;
13586 bool extended_p = false;
13587 bool invalid_inputs_p = false;
13588 bool invalid_outputs_p = false;
13589 bool goto_p = false;
13590 required_token missing = RT_NONE;
13592 /* Look for the `asm' keyword. */
13593 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
13594 /* See if the next token is `volatile'. */
13595 if (cp_parser_allow_gnu_extensions_p (parser)
13596 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13598 /* Remember that we saw the `volatile' keyword. */
13600 /* Consume the token. */
13601 cp_lexer_consume_token (parser->lexer);
13603 if (cp_parser_allow_gnu_extensions_p (parser)
13604 && parser->in_function_body
13605 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13607 /* Remember that we saw the `goto' keyword. */
13609 /* Consume the token. */
13610 cp_lexer_consume_token (parser->lexer);
13612 /* Look for the opening `('. */
13613 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13615 /* Look for the string. */
13616 string = cp_parser_string_literal (parser, false, false);
13617 if (string == error_mark_node)
13619 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13620 /*consume_paren=*/true);
13624 /* If we're allowing GNU extensions, check for the extended assembly
13625 syntax. Unfortunately, the `:' tokens need not be separated by
13626 a space in C, and so, for compatibility, we tolerate that here
13627 too. Doing that means that we have to treat the `::' operator as
13629 if (cp_parser_allow_gnu_extensions_p (parser)
13630 && parser->in_function_body
13631 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13632 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13634 bool inputs_p = false;
13635 bool clobbers_p = false;
13636 bool labels_p = false;
13638 /* The extended syntax was used. */
13641 /* Look for outputs. */
13642 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13644 /* Consume the `:'. */
13645 cp_lexer_consume_token (parser->lexer);
13646 /* Parse the output-operands. */
13647 if (cp_lexer_next_token_is_not (parser->lexer,
13649 && cp_lexer_next_token_is_not (parser->lexer,
13651 && cp_lexer_next_token_is_not (parser->lexer,
13654 outputs = cp_parser_asm_operand_list (parser);
13656 if (outputs == error_mark_node)
13657 invalid_outputs_p = true;
13659 /* If the next token is `::', there are no outputs, and the
13660 next token is the beginning of the inputs. */
13661 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13662 /* The inputs are coming next. */
13665 /* Look for inputs. */
13667 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13669 /* Consume the `:' or `::'. */
13670 cp_lexer_consume_token (parser->lexer);
13671 /* Parse the output-operands. */
13672 if (cp_lexer_next_token_is_not (parser->lexer,
13674 && cp_lexer_next_token_is_not (parser->lexer,
13676 && cp_lexer_next_token_is_not (parser->lexer,
13678 inputs = cp_parser_asm_operand_list (parser);
13680 if (inputs == error_mark_node)
13681 invalid_inputs_p = true;
13683 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13684 /* The clobbers are coming next. */
13687 /* Look for clobbers. */
13689 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13692 /* Consume the `:' or `::'. */
13693 cp_lexer_consume_token (parser->lexer);
13694 /* Parse the clobbers. */
13695 if (cp_lexer_next_token_is_not (parser->lexer,
13697 && cp_lexer_next_token_is_not (parser->lexer,
13699 clobbers = cp_parser_asm_clobber_list (parser);
13702 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13703 /* The labels are coming next. */
13706 /* Look for labels. */
13708 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13711 /* Consume the `:' or `::'. */
13712 cp_lexer_consume_token (parser->lexer);
13713 /* Parse the labels. */
13714 labels = cp_parser_asm_label_list (parser);
13717 if (goto_p && !labels_p)
13718 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
13721 missing = RT_COLON_SCOPE;
13723 /* Look for the closing `)'. */
13724 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13725 missing ? missing : RT_CLOSE_PAREN))
13726 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13727 /*consume_paren=*/true);
13728 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13730 if (!invalid_inputs_p && !invalid_outputs_p)
13732 /* Create the ASM_EXPR. */
13733 if (parser->in_function_body)
13735 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13736 inputs, clobbers, labels);
13737 /* If the extended syntax was not used, mark the ASM_EXPR. */
13740 tree temp = asm_stmt;
13741 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13742 temp = TREE_OPERAND (temp, 0);
13744 ASM_INPUT_P (temp) = 1;
13748 cgraph_add_asm_node (string);
13752 /* Declarators [gram.dcl.decl] */
13754 /* Parse an init-declarator.
13757 declarator initializer [opt]
13762 declarator asm-specification [opt] attributes [opt] initializer [opt]
13764 function-definition:
13765 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13767 decl-specifier-seq [opt] declarator function-try-block
13771 function-definition:
13772 __extension__ function-definition
13774 The DECL_SPECIFIERS apply to this declarator. Returns a
13775 representation of the entity declared. If MEMBER_P is TRUE, then
13776 this declarator appears in a class scope. The new DECL created by
13777 this declarator is returned.
13779 The CHECKS are access checks that should be performed once we know
13780 what entity is being declared (and, therefore, what classes have
13783 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13784 for a function-definition here as well. If the declarator is a
13785 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13786 be TRUE upon return. By that point, the function-definition will
13787 have been completely parsed.
13789 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13793 cp_parser_init_declarator (cp_parser* parser,
13794 cp_decl_specifier_seq *decl_specifiers,
13795 VEC (deferred_access_check,gc)* checks,
13796 bool function_definition_allowed_p,
13798 int declares_class_or_enum,
13799 bool* function_definition_p)
13801 cp_token *token = NULL, *asm_spec_start_token = NULL,
13802 *attributes_start_token = NULL;
13803 cp_declarator *declarator;
13804 tree prefix_attributes;
13806 tree asm_specification;
13808 tree decl = NULL_TREE;
13810 int is_initialized;
13811 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13812 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13814 enum cpp_ttype initialization_kind;
13815 bool is_direct_init = false;
13816 bool is_non_constant_init;
13817 int ctor_dtor_or_conv_p;
13819 tree pushed_scope = NULL;
13821 /* Gather the attributes that were provided with the
13822 decl-specifiers. */
13823 prefix_attributes = decl_specifiers->attributes;
13825 /* Assume that this is not the declarator for a function
13827 if (function_definition_p)
13828 *function_definition_p = false;
13830 /* Defer access checks while parsing the declarator; we cannot know
13831 what names are accessible until we know what is being
13833 resume_deferring_access_checks ();
13835 /* Parse the declarator. */
13836 token = cp_lexer_peek_token (parser->lexer);
13838 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13839 &ctor_dtor_or_conv_p,
13840 /*parenthesized_p=*/NULL,
13841 /*member_p=*/false);
13842 /* Gather up the deferred checks. */
13843 stop_deferring_access_checks ();
13845 /* If the DECLARATOR was erroneous, there's no need to go
13847 if (declarator == cp_error_declarator)
13848 return error_mark_node;
13850 /* Check that the number of template-parameter-lists is OK. */
13851 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13853 return error_mark_node;
13855 if (declares_class_or_enum & 2)
13856 cp_parser_check_for_definition_in_return_type (declarator,
13857 decl_specifiers->type,
13858 decl_specifiers->type_location);
13860 /* Figure out what scope the entity declared by the DECLARATOR is
13861 located in. `grokdeclarator' sometimes changes the scope, so
13862 we compute it now. */
13863 scope = get_scope_of_declarator (declarator);
13865 /* Perform any lookups in the declared type which were thought to be
13866 dependent, but are not in the scope of the declarator. */
13867 decl_specifiers->type
13868 = maybe_update_decl_type (decl_specifiers->type, scope);
13870 /* If we're allowing GNU extensions, look for an asm-specification
13872 if (cp_parser_allow_gnu_extensions_p (parser))
13874 /* Look for an asm-specification. */
13875 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13876 asm_specification = cp_parser_asm_specification_opt (parser);
13877 /* And attributes. */
13878 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13879 attributes = cp_parser_attributes_opt (parser);
13883 asm_specification = NULL_TREE;
13884 attributes = NULL_TREE;
13887 /* Peek at the next token. */
13888 token = cp_lexer_peek_token (parser->lexer);
13889 /* Check to see if the token indicates the start of a
13890 function-definition. */
13891 if (function_declarator_p (declarator)
13892 && cp_parser_token_starts_function_definition_p (token))
13894 if (!function_definition_allowed_p)
13896 /* If a function-definition should not appear here, issue an
13898 cp_parser_error (parser,
13899 "a function-definition is not allowed here");
13900 return error_mark_node;
13904 location_t func_brace_location
13905 = cp_lexer_peek_token (parser->lexer)->location;
13907 /* Neither attributes nor an asm-specification are allowed
13908 on a function-definition. */
13909 if (asm_specification)
13910 error_at (asm_spec_start_token->location,
13911 "an asm-specification is not allowed "
13912 "on a function-definition");
13914 error_at (attributes_start_token->location,
13915 "attributes are not allowed on a function-definition");
13916 /* This is a function-definition. */
13917 *function_definition_p = true;
13919 /* Parse the function definition. */
13921 decl = cp_parser_save_member_function_body (parser,
13924 prefix_attributes);
13927 = (cp_parser_function_definition_from_specifiers_and_declarator
13928 (parser, decl_specifiers, prefix_attributes, declarator));
13930 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13932 /* This is where the prologue starts... */
13933 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13934 = func_brace_location;
13943 Only in function declarations for constructors, destructors, and
13944 type conversions can the decl-specifier-seq be omitted.
13946 We explicitly postpone this check past the point where we handle
13947 function-definitions because we tolerate function-definitions
13948 that are missing their return types in some modes. */
13949 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13951 cp_parser_error (parser,
13952 "expected constructor, destructor, or type conversion");
13953 return error_mark_node;
13956 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13957 if (token->type == CPP_EQ
13958 || token->type == CPP_OPEN_PAREN
13959 || token->type == CPP_OPEN_BRACE)
13961 is_initialized = SD_INITIALIZED;
13962 initialization_kind = token->type;
13964 if (token->type == CPP_EQ
13965 && function_declarator_p (declarator))
13967 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13968 if (t2->keyword == RID_DEFAULT)
13969 is_initialized = SD_DEFAULTED;
13970 else if (t2->keyword == RID_DELETE)
13971 is_initialized = SD_DELETED;
13976 /* If the init-declarator isn't initialized and isn't followed by a
13977 `,' or `;', it's not a valid init-declarator. */
13978 if (token->type != CPP_COMMA
13979 && token->type != CPP_SEMICOLON)
13981 cp_parser_error (parser, "expected initializer");
13982 return error_mark_node;
13984 is_initialized = SD_UNINITIALIZED;
13985 initialization_kind = CPP_EOF;
13988 /* Because start_decl has side-effects, we should only call it if we
13989 know we're going ahead. By this point, we know that we cannot
13990 possibly be looking at any other construct. */
13991 cp_parser_commit_to_tentative_parse (parser);
13993 /* If the decl specifiers were bad, issue an error now that we're
13994 sure this was intended to be a declarator. Then continue
13995 declaring the variable(s), as int, to try to cut down on further
13997 if (decl_specifiers->any_specifiers_p
13998 && decl_specifiers->type == error_mark_node)
14000 cp_parser_error (parser, "invalid type in declaration");
14001 decl_specifiers->type = integer_type_node;
14004 /* Check to see whether or not this declaration is a friend. */
14005 friend_p = cp_parser_friend_p (decl_specifiers);
14007 /* Enter the newly declared entry in the symbol table. If we're
14008 processing a declaration in a class-specifier, we wait until
14009 after processing the initializer. */
14012 if (parser->in_unbraced_linkage_specification_p)
14013 decl_specifiers->storage_class = sc_extern;
14014 decl = start_decl (declarator, decl_specifiers,
14015 is_initialized, attributes, prefix_attributes,
14019 /* Enter the SCOPE. That way unqualified names appearing in the
14020 initializer will be looked up in SCOPE. */
14021 pushed_scope = push_scope (scope);
14023 /* Perform deferred access control checks, now that we know in which
14024 SCOPE the declared entity resides. */
14025 if (!member_p && decl)
14027 tree saved_current_function_decl = NULL_TREE;
14029 /* If the entity being declared is a function, pretend that we
14030 are in its scope. If it is a `friend', it may have access to
14031 things that would not otherwise be accessible. */
14032 if (TREE_CODE (decl) == FUNCTION_DECL)
14034 saved_current_function_decl = current_function_decl;
14035 current_function_decl = decl;
14038 /* Perform access checks for template parameters. */
14039 cp_parser_perform_template_parameter_access_checks (checks);
14041 /* Perform the access control checks for the declarator and the
14042 decl-specifiers. */
14043 perform_deferred_access_checks ();
14045 /* Restore the saved value. */
14046 if (TREE_CODE (decl) == FUNCTION_DECL)
14047 current_function_decl = saved_current_function_decl;
14050 /* Parse the initializer. */
14051 initializer = NULL_TREE;
14052 is_direct_init = false;
14053 is_non_constant_init = true;
14054 if (is_initialized)
14056 if (function_declarator_p (declarator))
14058 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14059 if (initialization_kind == CPP_EQ)
14060 initializer = cp_parser_pure_specifier (parser);
14063 /* If the declaration was erroneous, we don't really
14064 know what the user intended, so just silently
14065 consume the initializer. */
14066 if (decl != error_mark_node)
14067 error_at (initializer_start_token->location,
14068 "initializer provided for function");
14069 cp_parser_skip_to_closing_parenthesis (parser,
14070 /*recovering=*/true,
14071 /*or_comma=*/false,
14072 /*consume_paren=*/true);
14077 /* We want to record the extra mangling scope for in-class
14078 initializers of class members and initializers of static data
14079 member templates. The former is a C++0x feature which isn't
14080 implemented yet, and I expect it will involve deferring
14081 parsing of the initializer until end of class as with default
14082 arguments. So right here we only handle the latter. */
14083 if (!member_p && processing_template_decl)
14084 start_lambda_scope (decl);
14085 initializer = cp_parser_initializer (parser,
14087 &is_non_constant_init);
14088 if (!member_p && processing_template_decl)
14089 finish_lambda_scope ();
14093 /* The old parser allows attributes to appear after a parenthesized
14094 initializer. Mark Mitchell proposed removing this functionality
14095 on the GCC mailing lists on 2002-08-13. This parser accepts the
14096 attributes -- but ignores them. */
14097 if (cp_parser_allow_gnu_extensions_p (parser)
14098 && initialization_kind == CPP_OPEN_PAREN)
14099 if (cp_parser_attributes_opt (parser))
14100 warning (OPT_Wattributes,
14101 "attributes after parenthesized initializer ignored");
14103 /* For an in-class declaration, use `grokfield' to create the
14109 pop_scope (pushed_scope);
14110 pushed_scope = false;
14112 decl = grokfield (declarator, decl_specifiers,
14113 initializer, !is_non_constant_init,
14114 /*asmspec=*/NULL_TREE,
14115 prefix_attributes);
14116 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14117 cp_parser_save_default_args (parser, decl);
14120 /* Finish processing the declaration. But, skip friend
14122 if (!friend_p && decl && decl != error_mark_node)
14124 cp_finish_decl (decl,
14125 initializer, !is_non_constant_init,
14127 /* If the initializer is in parentheses, then this is
14128 a direct-initialization, which means that an
14129 `explicit' constructor is OK. Otherwise, an
14130 `explicit' constructor cannot be used. */
14131 ((is_direct_init || !is_initialized)
14132 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14134 else if ((cxx_dialect != cxx98) && friend_p
14135 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14136 /* Core issue #226 (C++0x only): A default template-argument
14137 shall not be specified in a friend class template
14139 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14140 /*is_partial=*/0, /*is_friend_decl=*/1);
14142 if (!friend_p && pushed_scope)
14143 pop_scope (pushed_scope);
14148 /* Parse a declarator.
14152 ptr-operator declarator
14154 abstract-declarator:
14155 ptr-operator abstract-declarator [opt]
14156 direct-abstract-declarator
14161 attributes [opt] direct-declarator
14162 attributes [opt] ptr-operator declarator
14164 abstract-declarator:
14165 attributes [opt] ptr-operator abstract-declarator [opt]
14166 attributes [opt] direct-abstract-declarator
14168 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14169 detect constructor, destructor or conversion operators. It is set
14170 to -1 if the declarator is a name, and +1 if it is a
14171 function. Otherwise it is set to zero. Usually you just want to
14172 test for >0, but internally the negative value is used.
14174 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14175 a decl-specifier-seq unless it declares a constructor, destructor,
14176 or conversion. It might seem that we could check this condition in
14177 semantic analysis, rather than parsing, but that makes it difficult
14178 to handle something like `f()'. We want to notice that there are
14179 no decl-specifiers, and therefore realize that this is an
14180 expression, not a declaration.)
14182 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14183 the declarator is a direct-declarator of the form "(...)".
14185 MEMBER_P is true iff this declarator is a member-declarator. */
14187 static cp_declarator *
14188 cp_parser_declarator (cp_parser* parser,
14189 cp_parser_declarator_kind dcl_kind,
14190 int* ctor_dtor_or_conv_p,
14191 bool* parenthesized_p,
14194 cp_declarator *declarator;
14195 enum tree_code code;
14196 cp_cv_quals cv_quals;
14198 tree attributes = NULL_TREE;
14200 /* Assume this is not a constructor, destructor, or type-conversion
14202 if (ctor_dtor_or_conv_p)
14203 *ctor_dtor_or_conv_p = 0;
14205 if (cp_parser_allow_gnu_extensions_p (parser))
14206 attributes = cp_parser_attributes_opt (parser);
14208 /* Check for the ptr-operator production. */
14209 cp_parser_parse_tentatively (parser);
14210 /* Parse the ptr-operator. */
14211 code = cp_parser_ptr_operator (parser,
14214 /* If that worked, then we have a ptr-operator. */
14215 if (cp_parser_parse_definitely (parser))
14217 /* If a ptr-operator was found, then this declarator was not
14219 if (parenthesized_p)
14220 *parenthesized_p = true;
14221 /* The dependent declarator is optional if we are parsing an
14222 abstract-declarator. */
14223 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14224 cp_parser_parse_tentatively (parser);
14226 /* Parse the dependent declarator. */
14227 declarator = cp_parser_declarator (parser, dcl_kind,
14228 /*ctor_dtor_or_conv_p=*/NULL,
14229 /*parenthesized_p=*/NULL,
14230 /*member_p=*/false);
14232 /* If we are parsing an abstract-declarator, we must handle the
14233 case where the dependent declarator is absent. */
14234 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14235 && !cp_parser_parse_definitely (parser))
14238 declarator = cp_parser_make_indirect_declarator
14239 (code, class_type, cv_quals, declarator);
14241 /* Everything else is a direct-declarator. */
14244 if (parenthesized_p)
14245 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14247 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14248 ctor_dtor_or_conv_p,
14252 if (attributes && declarator && declarator != cp_error_declarator)
14253 declarator->attributes = attributes;
14258 /* Parse a direct-declarator or direct-abstract-declarator.
14262 direct-declarator ( parameter-declaration-clause )
14263 cv-qualifier-seq [opt]
14264 exception-specification [opt]
14265 direct-declarator [ constant-expression [opt] ]
14268 direct-abstract-declarator:
14269 direct-abstract-declarator [opt]
14270 ( parameter-declaration-clause )
14271 cv-qualifier-seq [opt]
14272 exception-specification [opt]
14273 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14274 ( abstract-declarator )
14276 Returns a representation of the declarator. DCL_KIND is
14277 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14278 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14279 we are parsing a direct-declarator. It is
14280 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14281 of ambiguity we prefer an abstract declarator, as per
14282 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14283 cp_parser_declarator. */
14285 static cp_declarator *
14286 cp_parser_direct_declarator (cp_parser* parser,
14287 cp_parser_declarator_kind dcl_kind,
14288 int* ctor_dtor_or_conv_p,
14292 cp_declarator *declarator = NULL;
14293 tree scope = NULL_TREE;
14294 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14295 bool saved_in_declarator_p = parser->in_declarator_p;
14297 tree pushed_scope = NULL_TREE;
14301 /* Peek at the next token. */
14302 token = cp_lexer_peek_token (parser->lexer);
14303 if (token->type == CPP_OPEN_PAREN)
14305 /* This is either a parameter-declaration-clause, or a
14306 parenthesized declarator. When we know we are parsing a
14307 named declarator, it must be a parenthesized declarator
14308 if FIRST is true. For instance, `(int)' is a
14309 parameter-declaration-clause, with an omitted
14310 direct-abstract-declarator. But `((*))', is a
14311 parenthesized abstract declarator. Finally, when T is a
14312 template parameter `(T)' is a
14313 parameter-declaration-clause, and not a parenthesized
14316 We first try and parse a parameter-declaration-clause,
14317 and then try a nested declarator (if FIRST is true).
14319 It is not an error for it not to be a
14320 parameter-declaration-clause, even when FIRST is
14326 The first is the declaration of a function while the
14327 second is the definition of a variable, including its
14330 Having seen only the parenthesis, we cannot know which of
14331 these two alternatives should be selected. Even more
14332 complex are examples like:
14337 The former is a function-declaration; the latter is a
14338 variable initialization.
14340 Thus again, we try a parameter-declaration-clause, and if
14341 that fails, we back out and return. */
14343 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14346 unsigned saved_num_template_parameter_lists;
14347 bool is_declarator = false;
14350 /* In a member-declarator, the only valid interpretation
14351 of a parenthesis is the start of a
14352 parameter-declaration-clause. (It is invalid to
14353 initialize a static data member with a parenthesized
14354 initializer; only the "=" form of initialization is
14357 cp_parser_parse_tentatively (parser);
14359 /* Consume the `('. */
14360 cp_lexer_consume_token (parser->lexer);
14363 /* If this is going to be an abstract declarator, we're
14364 in a declarator and we can't have default args. */
14365 parser->default_arg_ok_p = false;
14366 parser->in_declarator_p = true;
14369 /* Inside the function parameter list, surrounding
14370 template-parameter-lists do not apply. */
14371 saved_num_template_parameter_lists
14372 = parser->num_template_parameter_lists;
14373 parser->num_template_parameter_lists = 0;
14375 begin_scope (sk_function_parms, NULL_TREE);
14377 /* Parse the parameter-declaration-clause. */
14378 params = cp_parser_parameter_declaration_clause (parser);
14380 parser->num_template_parameter_lists
14381 = saved_num_template_parameter_lists;
14383 /* If all went well, parse the cv-qualifier-seq and the
14384 exception-specification. */
14385 if (member_p || cp_parser_parse_definitely (parser))
14387 cp_cv_quals cv_quals;
14388 tree exception_specification;
14391 is_declarator = true;
14393 if (ctor_dtor_or_conv_p)
14394 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14396 /* Consume the `)'. */
14397 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14399 /* Parse the cv-qualifier-seq. */
14400 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14401 /* And the exception-specification. */
14402 exception_specification
14403 = cp_parser_exception_specification_opt (parser);
14406 = cp_parser_late_return_type_opt (parser);
14408 /* Create the function-declarator. */
14409 declarator = make_call_declarator (declarator,
14412 exception_specification,
14414 /* Any subsequent parameter lists are to do with
14415 return type, so are not those of the declared
14417 parser->default_arg_ok_p = false;
14420 /* Remove the function parms from scope. */
14421 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14422 pop_binding (DECL_NAME (t), t);
14426 /* Repeat the main loop. */
14430 /* If this is the first, we can try a parenthesized
14434 bool saved_in_type_id_in_expr_p;
14436 parser->default_arg_ok_p = saved_default_arg_ok_p;
14437 parser->in_declarator_p = saved_in_declarator_p;
14439 /* Consume the `('. */
14440 cp_lexer_consume_token (parser->lexer);
14441 /* Parse the nested declarator. */
14442 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14443 parser->in_type_id_in_expr_p = true;
14445 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14446 /*parenthesized_p=*/NULL,
14448 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14450 /* Expect a `)'. */
14451 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14452 declarator = cp_error_declarator;
14453 if (declarator == cp_error_declarator)
14456 goto handle_declarator;
14458 /* Otherwise, we must be done. */
14462 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14463 && token->type == CPP_OPEN_SQUARE)
14465 /* Parse an array-declarator. */
14468 if (ctor_dtor_or_conv_p)
14469 *ctor_dtor_or_conv_p = 0;
14472 parser->default_arg_ok_p = false;
14473 parser->in_declarator_p = true;
14474 /* Consume the `['. */
14475 cp_lexer_consume_token (parser->lexer);
14476 /* Peek at the next token. */
14477 token = cp_lexer_peek_token (parser->lexer);
14478 /* If the next token is `]', then there is no
14479 constant-expression. */
14480 if (token->type != CPP_CLOSE_SQUARE)
14482 bool non_constant_p;
14485 = cp_parser_constant_expression (parser,
14486 /*allow_non_constant=*/true,
14488 if (!non_constant_p)
14489 bounds = fold_non_dependent_expr (bounds);
14490 /* Normally, the array bound must be an integral constant
14491 expression. However, as an extension, we allow VLAs
14492 in function scopes as long as they aren't part of a
14493 parameter declaration. */
14494 else if (!parser->in_function_body
14495 || current_binding_level->kind == sk_function_parms)
14497 cp_parser_error (parser,
14498 "array bound is not an integer constant");
14499 bounds = error_mark_node;
14501 else if (processing_template_decl && !error_operand_p (bounds))
14503 /* Remember this wasn't a constant-expression. */
14504 bounds = build_nop (TREE_TYPE (bounds), bounds);
14505 TREE_SIDE_EFFECTS (bounds) = 1;
14509 bounds = NULL_TREE;
14510 /* Look for the closing `]'. */
14511 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14513 declarator = cp_error_declarator;
14517 declarator = make_array_declarator (declarator, bounds);
14519 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14522 tree qualifying_scope;
14523 tree unqualified_name;
14524 special_function_kind sfk;
14526 bool pack_expansion_p = false;
14527 cp_token *declarator_id_start_token;
14529 /* Parse a declarator-id */
14530 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14533 cp_parser_parse_tentatively (parser);
14535 /* If we see an ellipsis, we should be looking at a
14537 if (token->type == CPP_ELLIPSIS)
14539 /* Consume the `...' */
14540 cp_lexer_consume_token (parser->lexer);
14542 pack_expansion_p = true;
14546 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14548 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14549 qualifying_scope = parser->scope;
14554 if (!unqualified_name && pack_expansion_p)
14556 /* Check whether an error occurred. */
14557 okay = !cp_parser_error_occurred (parser);
14559 /* We already consumed the ellipsis to mark a
14560 parameter pack, but we have no way to report it,
14561 so abort the tentative parse. We will be exiting
14562 immediately anyway. */
14563 cp_parser_abort_tentative_parse (parser);
14566 okay = cp_parser_parse_definitely (parser);
14569 unqualified_name = error_mark_node;
14570 else if (unqualified_name
14571 && (qualifying_scope
14572 || (TREE_CODE (unqualified_name)
14573 != IDENTIFIER_NODE)))
14575 cp_parser_error (parser, "expected unqualified-id");
14576 unqualified_name = error_mark_node;
14580 if (!unqualified_name)
14582 if (unqualified_name == error_mark_node)
14584 declarator = cp_error_declarator;
14585 pack_expansion_p = false;
14586 declarator->parameter_pack_p = false;
14590 if (qualifying_scope && at_namespace_scope_p ()
14591 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14593 /* In the declaration of a member of a template class
14594 outside of the class itself, the SCOPE will sometimes
14595 be a TYPENAME_TYPE. For example, given:
14597 template <typename T>
14598 int S<T>::R::i = 3;
14600 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14601 this context, we must resolve S<T>::R to an ordinary
14602 type, rather than a typename type.
14604 The reason we normally avoid resolving TYPENAME_TYPEs
14605 is that a specialization of `S' might render
14606 `S<T>::R' not a type. However, if `S' is
14607 specialized, then this `i' will not be used, so there
14608 is no harm in resolving the types here. */
14611 /* Resolve the TYPENAME_TYPE. */
14612 type = resolve_typename_type (qualifying_scope,
14613 /*only_current_p=*/false);
14614 /* If that failed, the declarator is invalid. */
14615 if (TREE_CODE (type) == TYPENAME_TYPE)
14617 if (typedef_variant_p (type))
14618 error_at (declarator_id_start_token->location,
14619 "cannot define member of dependent typedef "
14622 error_at (declarator_id_start_token->location,
14623 "%<%T::%E%> is not a type",
14624 TYPE_CONTEXT (qualifying_scope),
14625 TYPE_IDENTIFIER (qualifying_scope));
14627 qualifying_scope = type;
14632 if (unqualified_name)
14636 if (qualifying_scope
14637 && CLASS_TYPE_P (qualifying_scope))
14638 class_type = qualifying_scope;
14640 class_type = current_class_type;
14642 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14644 tree name_type = TREE_TYPE (unqualified_name);
14645 if (class_type && same_type_p (name_type, class_type))
14647 if (qualifying_scope
14648 && CLASSTYPE_USE_TEMPLATE (name_type))
14650 error_at (declarator_id_start_token->location,
14651 "invalid use of constructor as a template");
14652 inform (declarator_id_start_token->location,
14653 "use %<%T::%D%> instead of %<%T::%D%> to "
14654 "name the constructor in a qualified name",
14656 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14657 class_type, name_type);
14658 declarator = cp_error_declarator;
14662 unqualified_name = constructor_name (class_type);
14666 /* We do not attempt to print the declarator
14667 here because we do not have enough
14668 information about its original syntactic
14670 cp_parser_error (parser, "invalid declarator");
14671 declarator = cp_error_declarator;
14678 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14679 sfk = sfk_destructor;
14680 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14681 sfk = sfk_conversion;
14682 else if (/* There's no way to declare a constructor
14683 for an anonymous type, even if the type
14684 got a name for linkage purposes. */
14685 !TYPE_WAS_ANONYMOUS (class_type)
14686 && constructor_name_p (unqualified_name,
14689 unqualified_name = constructor_name (class_type);
14690 sfk = sfk_constructor;
14692 else if (is_overloaded_fn (unqualified_name)
14693 && DECL_CONSTRUCTOR_P (get_first_fn
14694 (unqualified_name)))
14695 sfk = sfk_constructor;
14697 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14698 *ctor_dtor_or_conv_p = -1;
14701 declarator = make_id_declarator (qualifying_scope,
14704 declarator->id_loc = token->location;
14705 declarator->parameter_pack_p = pack_expansion_p;
14707 if (pack_expansion_p)
14708 maybe_warn_variadic_templates ();
14711 handle_declarator:;
14712 scope = get_scope_of_declarator (declarator);
14714 /* Any names that appear after the declarator-id for a
14715 member are looked up in the containing scope. */
14716 pushed_scope = push_scope (scope);
14717 parser->in_declarator_p = true;
14718 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14719 || (declarator && declarator->kind == cdk_id))
14720 /* Default args are only allowed on function
14722 parser->default_arg_ok_p = saved_default_arg_ok_p;
14724 parser->default_arg_ok_p = false;
14733 /* For an abstract declarator, we might wind up with nothing at this
14734 point. That's an error; the declarator is not optional. */
14736 cp_parser_error (parser, "expected declarator");
14738 /* If we entered a scope, we must exit it now. */
14740 pop_scope (pushed_scope);
14742 parser->default_arg_ok_p = saved_default_arg_ok_p;
14743 parser->in_declarator_p = saved_in_declarator_p;
14748 /* Parse a ptr-operator.
14751 * cv-qualifier-seq [opt]
14753 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14758 & cv-qualifier-seq [opt]
14760 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14761 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14762 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14763 filled in with the TYPE containing the member. *CV_QUALS is
14764 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14765 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14766 Note that the tree codes returned by this function have nothing
14767 to do with the types of trees that will be eventually be created
14768 to represent the pointer or reference type being parsed. They are
14769 just constants with suggestive names. */
14770 static enum tree_code
14771 cp_parser_ptr_operator (cp_parser* parser,
14773 cp_cv_quals *cv_quals)
14775 enum tree_code code = ERROR_MARK;
14778 /* Assume that it's not a pointer-to-member. */
14780 /* And that there are no cv-qualifiers. */
14781 *cv_quals = TYPE_UNQUALIFIED;
14783 /* Peek at the next token. */
14784 token = cp_lexer_peek_token (parser->lexer);
14786 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14787 if (token->type == CPP_MULT)
14788 code = INDIRECT_REF;
14789 else if (token->type == CPP_AND)
14791 else if ((cxx_dialect != cxx98) &&
14792 token->type == CPP_AND_AND) /* C++0x only */
14793 code = NON_LVALUE_EXPR;
14795 if (code != ERROR_MARK)
14797 /* Consume the `*', `&' or `&&'. */
14798 cp_lexer_consume_token (parser->lexer);
14800 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14801 `&', if we are allowing GNU extensions. (The only qualifier
14802 that can legally appear after `&' is `restrict', but that is
14803 enforced during semantic analysis. */
14804 if (code == INDIRECT_REF
14805 || cp_parser_allow_gnu_extensions_p (parser))
14806 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14810 /* Try the pointer-to-member case. */
14811 cp_parser_parse_tentatively (parser);
14812 /* Look for the optional `::' operator. */
14813 cp_parser_global_scope_opt (parser,
14814 /*current_scope_valid_p=*/false);
14815 /* Look for the nested-name specifier. */
14816 token = cp_lexer_peek_token (parser->lexer);
14817 cp_parser_nested_name_specifier (parser,
14818 /*typename_keyword_p=*/false,
14819 /*check_dependency_p=*/true,
14821 /*is_declaration=*/false);
14822 /* If we found it, and the next token is a `*', then we are
14823 indeed looking at a pointer-to-member operator. */
14824 if (!cp_parser_error_occurred (parser)
14825 && cp_parser_require (parser, CPP_MULT, RT_MULT))
14827 /* Indicate that the `*' operator was used. */
14828 code = INDIRECT_REF;
14830 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14831 error_at (token->location, "%qD is a namespace", parser->scope);
14834 /* The type of which the member is a member is given by the
14836 *type = parser->scope;
14837 /* The next name will not be qualified. */
14838 parser->scope = NULL_TREE;
14839 parser->qualifying_scope = NULL_TREE;
14840 parser->object_scope = NULL_TREE;
14841 /* Look for the optional cv-qualifier-seq. */
14842 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14845 /* If that didn't work we don't have a ptr-operator. */
14846 if (!cp_parser_parse_definitely (parser))
14847 cp_parser_error (parser, "expected ptr-operator");
14853 /* Parse an (optional) cv-qualifier-seq.
14856 cv-qualifier cv-qualifier-seq [opt]
14867 Returns a bitmask representing the cv-qualifiers. */
14870 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14872 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14877 cp_cv_quals cv_qualifier;
14879 /* Peek at the next token. */
14880 token = cp_lexer_peek_token (parser->lexer);
14881 /* See if it's a cv-qualifier. */
14882 switch (token->keyword)
14885 cv_qualifier = TYPE_QUAL_CONST;
14889 cv_qualifier = TYPE_QUAL_VOLATILE;
14893 cv_qualifier = TYPE_QUAL_RESTRICT;
14897 cv_qualifier = TYPE_UNQUALIFIED;
14904 if (cv_quals & cv_qualifier)
14906 error_at (token->location, "duplicate cv-qualifier");
14907 cp_lexer_purge_token (parser->lexer);
14911 cp_lexer_consume_token (parser->lexer);
14912 cv_quals |= cv_qualifier;
14919 /* Parse a late-specified return type, if any. This is not a separate
14920 non-terminal, but part of a function declarator, which looks like
14922 -> trailing-type-specifier-seq abstract-declarator(opt)
14924 Returns the type indicated by the type-id. */
14927 cp_parser_late_return_type_opt (cp_parser* parser)
14931 /* Peek at the next token. */
14932 token = cp_lexer_peek_token (parser->lexer);
14933 /* A late-specified return type is indicated by an initial '->'. */
14934 if (token->type != CPP_DEREF)
14937 /* Consume the ->. */
14938 cp_lexer_consume_token (parser->lexer);
14940 return cp_parser_trailing_type_id (parser);
14943 /* Parse a declarator-id.
14947 :: [opt] nested-name-specifier [opt] type-name
14949 In the `id-expression' case, the value returned is as for
14950 cp_parser_id_expression if the id-expression was an unqualified-id.
14951 If the id-expression was a qualified-id, then a SCOPE_REF is
14952 returned. The first operand is the scope (either a NAMESPACE_DECL
14953 or TREE_TYPE), but the second is still just a representation of an
14957 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14960 /* The expression must be an id-expression. Assume that qualified
14961 names are the names of types so that:
14964 int S<T>::R::i = 3;
14966 will work; we must treat `S<T>::R' as the name of a type.
14967 Similarly, assume that qualified names are templates, where
14971 int S<T>::R<T>::i = 3;
14974 id = cp_parser_id_expression (parser,
14975 /*template_keyword_p=*/false,
14976 /*check_dependency_p=*/false,
14977 /*template_p=*/NULL,
14978 /*declarator_p=*/true,
14980 if (id && BASELINK_P (id))
14981 id = BASELINK_FUNCTIONS (id);
14985 /* Parse a type-id.
14988 type-specifier-seq abstract-declarator [opt]
14990 Returns the TYPE specified. */
14993 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14994 bool is_trailing_return)
14996 cp_decl_specifier_seq type_specifier_seq;
14997 cp_declarator *abstract_declarator;
14999 /* Parse the type-specifier-seq. */
15000 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15001 is_trailing_return,
15002 &type_specifier_seq);
15003 if (type_specifier_seq.type == error_mark_node)
15004 return error_mark_node;
15006 /* There might or might not be an abstract declarator. */
15007 cp_parser_parse_tentatively (parser);
15008 /* Look for the declarator. */
15009 abstract_declarator
15010 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15011 /*parenthesized_p=*/NULL,
15012 /*member_p=*/false);
15013 /* Check to see if there really was a declarator. */
15014 if (!cp_parser_parse_definitely (parser))
15015 abstract_declarator = NULL;
15017 if (type_specifier_seq.type
15018 && type_uses_auto (type_specifier_seq.type))
15020 /* A type-id with type 'auto' is only ok if the abstract declarator
15021 is a function declarator with a late-specified return type. */
15022 if (abstract_declarator
15023 && abstract_declarator->kind == cdk_function
15024 && abstract_declarator->u.function.late_return_type)
15028 error ("invalid use of %<auto%>");
15029 return error_mark_node;
15033 return groktypename (&type_specifier_seq, abstract_declarator,
15037 static tree cp_parser_type_id (cp_parser *parser)
15039 return cp_parser_type_id_1 (parser, false, false);
15042 static tree cp_parser_template_type_arg (cp_parser *parser)
15044 return cp_parser_type_id_1 (parser, true, false);
15047 static tree cp_parser_trailing_type_id (cp_parser *parser)
15049 return cp_parser_type_id_1 (parser, false, true);
15052 /* Parse a type-specifier-seq.
15054 type-specifier-seq:
15055 type-specifier type-specifier-seq [opt]
15059 type-specifier-seq:
15060 attributes type-specifier-seq [opt]
15062 If IS_DECLARATION is true, we are at the start of a "condition" or
15063 exception-declaration, so we might be followed by a declarator-id.
15065 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15066 i.e. we've just seen "->".
15068 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15071 cp_parser_type_specifier_seq (cp_parser* parser,
15072 bool is_declaration,
15073 bool is_trailing_return,
15074 cp_decl_specifier_seq *type_specifier_seq)
15076 bool seen_type_specifier = false;
15077 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15078 cp_token *start_token = NULL;
15080 /* Clear the TYPE_SPECIFIER_SEQ. */
15081 clear_decl_specs (type_specifier_seq);
15083 /* In the context of a trailing return type, enum E { } is an
15084 elaborated-type-specifier followed by a function-body, not an
15086 if (is_trailing_return)
15087 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15089 /* Parse the type-specifiers and attributes. */
15092 tree type_specifier;
15093 bool is_cv_qualifier;
15095 /* Check for attributes first. */
15096 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15098 type_specifier_seq->attributes =
15099 chainon (type_specifier_seq->attributes,
15100 cp_parser_attributes_opt (parser));
15104 /* record the token of the beginning of the type specifier seq,
15105 for error reporting purposes*/
15107 start_token = cp_lexer_peek_token (parser->lexer);
15109 /* Look for the type-specifier. */
15110 type_specifier = cp_parser_type_specifier (parser,
15112 type_specifier_seq,
15113 /*is_declaration=*/false,
15116 if (!type_specifier)
15118 /* If the first type-specifier could not be found, this is not a
15119 type-specifier-seq at all. */
15120 if (!seen_type_specifier)
15122 cp_parser_error (parser, "expected type-specifier");
15123 type_specifier_seq->type = error_mark_node;
15126 /* If subsequent type-specifiers could not be found, the
15127 type-specifier-seq is complete. */
15131 seen_type_specifier = true;
15132 /* The standard says that a condition can be:
15134 type-specifier-seq declarator = assignment-expression
15141 we should treat the "S" as a declarator, not as a
15142 type-specifier. The standard doesn't say that explicitly for
15143 type-specifier-seq, but it does say that for
15144 decl-specifier-seq in an ordinary declaration. Perhaps it
15145 would be clearer just to allow a decl-specifier-seq here, and
15146 then add a semantic restriction that if any decl-specifiers
15147 that are not type-specifiers appear, the program is invalid. */
15148 if (is_declaration && !is_cv_qualifier)
15149 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15152 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15155 /* Parse a parameter-declaration-clause.
15157 parameter-declaration-clause:
15158 parameter-declaration-list [opt] ... [opt]
15159 parameter-declaration-list , ...
15161 Returns a representation for the parameter declarations. A return
15162 value of NULL indicates a parameter-declaration-clause consisting
15163 only of an ellipsis. */
15166 cp_parser_parameter_declaration_clause (cp_parser* parser)
15173 /* Peek at the next token. */
15174 token = cp_lexer_peek_token (parser->lexer);
15175 /* Check for trivial parameter-declaration-clauses. */
15176 if (token->type == CPP_ELLIPSIS)
15178 /* Consume the `...' token. */
15179 cp_lexer_consume_token (parser->lexer);
15182 else if (token->type == CPP_CLOSE_PAREN)
15183 /* There are no parameters. */
15185 #ifndef NO_IMPLICIT_EXTERN_C
15186 if (in_system_header && current_class_type == NULL
15187 && current_lang_name == lang_name_c)
15191 return void_list_node;
15193 /* Check for `(void)', too, which is a special case. */
15194 else if (token->keyword == RID_VOID
15195 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15196 == CPP_CLOSE_PAREN))
15198 /* Consume the `void' token. */
15199 cp_lexer_consume_token (parser->lexer);
15200 /* There are no parameters. */
15201 return void_list_node;
15204 /* Parse the parameter-declaration-list. */
15205 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15206 /* If a parse error occurred while parsing the
15207 parameter-declaration-list, then the entire
15208 parameter-declaration-clause is erroneous. */
15212 /* Peek at the next token. */
15213 token = cp_lexer_peek_token (parser->lexer);
15214 /* If it's a `,', the clause should terminate with an ellipsis. */
15215 if (token->type == CPP_COMMA)
15217 /* Consume the `,'. */
15218 cp_lexer_consume_token (parser->lexer);
15219 /* Expect an ellipsis. */
15221 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15223 /* It might also be `...' if the optional trailing `,' was
15225 else if (token->type == CPP_ELLIPSIS)
15227 /* Consume the `...' token. */
15228 cp_lexer_consume_token (parser->lexer);
15229 /* And remember that we saw it. */
15233 ellipsis_p = false;
15235 /* Finish the parameter list. */
15237 parameters = chainon (parameters, void_list_node);
15242 /* Parse a parameter-declaration-list.
15244 parameter-declaration-list:
15245 parameter-declaration
15246 parameter-declaration-list , parameter-declaration
15248 Returns a representation of the parameter-declaration-list, as for
15249 cp_parser_parameter_declaration_clause. However, the
15250 `void_list_node' is never appended to the list. Upon return,
15251 *IS_ERROR will be true iff an error occurred. */
15254 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15256 tree parameters = NULL_TREE;
15257 tree *tail = ¶meters;
15258 bool saved_in_unbraced_linkage_specification_p;
15261 /* Assume all will go well. */
15263 /* The special considerations that apply to a function within an
15264 unbraced linkage specifications do not apply to the parameters
15265 to the function. */
15266 saved_in_unbraced_linkage_specification_p
15267 = parser->in_unbraced_linkage_specification_p;
15268 parser->in_unbraced_linkage_specification_p = false;
15270 /* Look for more parameters. */
15273 cp_parameter_declarator *parameter;
15274 tree decl = error_mark_node;
15275 bool parenthesized_p;
15276 /* Parse the parameter. */
15278 = cp_parser_parameter_declaration (parser,
15279 /*template_parm_p=*/false,
15282 /* We don't know yet if the enclosing context is deprecated, so wait
15283 and warn in grokparms if appropriate. */
15284 deprecated_state = DEPRECATED_SUPPRESS;
15287 decl = grokdeclarator (parameter->declarator,
15288 ¶meter->decl_specifiers,
15290 parameter->default_argument != NULL_TREE,
15291 ¶meter->decl_specifiers.attributes);
15293 deprecated_state = DEPRECATED_NORMAL;
15295 /* If a parse error occurred parsing the parameter declaration,
15296 then the entire parameter-declaration-list is erroneous. */
15297 if (decl == error_mark_node)
15300 parameters = error_mark_node;
15304 if (parameter->decl_specifiers.attributes)
15305 cplus_decl_attributes (&decl,
15306 parameter->decl_specifiers.attributes,
15308 if (DECL_NAME (decl))
15309 decl = pushdecl (decl);
15311 if (decl != error_mark_node)
15313 retrofit_lang_decl (decl);
15314 DECL_PARM_INDEX (decl) = ++index;
15317 /* Add the new parameter to the list. */
15318 *tail = build_tree_list (parameter->default_argument, decl);
15319 tail = &TREE_CHAIN (*tail);
15321 /* Peek at the next token. */
15322 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15323 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15324 /* These are for Objective-C++ */
15325 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15326 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15327 /* The parameter-declaration-list is complete. */
15329 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15333 /* Peek at the next token. */
15334 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15335 /* If it's an ellipsis, then the list is complete. */
15336 if (token->type == CPP_ELLIPSIS)
15338 /* Otherwise, there must be more parameters. Consume the
15340 cp_lexer_consume_token (parser->lexer);
15341 /* When parsing something like:
15343 int i(float f, double d)
15345 we can tell after seeing the declaration for "f" that we
15346 are not looking at an initialization of a variable "i",
15347 but rather at the declaration of a function "i".
15349 Due to the fact that the parsing of template arguments
15350 (as specified to a template-id) requires backtracking we
15351 cannot use this technique when inside a template argument
15353 if (!parser->in_template_argument_list_p
15354 && !parser->in_type_id_in_expr_p
15355 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15356 /* However, a parameter-declaration of the form
15357 "foat(f)" (which is a valid declaration of a
15358 parameter "f") can also be interpreted as an
15359 expression (the conversion of "f" to "float"). */
15360 && !parenthesized_p)
15361 cp_parser_commit_to_tentative_parse (parser);
15365 cp_parser_error (parser, "expected %<,%> or %<...%>");
15366 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15367 cp_parser_skip_to_closing_parenthesis (parser,
15368 /*recovering=*/true,
15369 /*or_comma=*/false,
15370 /*consume_paren=*/false);
15375 parser->in_unbraced_linkage_specification_p
15376 = saved_in_unbraced_linkage_specification_p;
15381 /* Parse a parameter declaration.
15383 parameter-declaration:
15384 decl-specifier-seq ... [opt] declarator
15385 decl-specifier-seq declarator = assignment-expression
15386 decl-specifier-seq ... [opt] abstract-declarator [opt]
15387 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15389 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15390 declares a template parameter. (In that case, a non-nested `>'
15391 token encountered during the parsing of the assignment-expression
15392 is not interpreted as a greater-than operator.)
15394 Returns a representation of the parameter, or NULL if an error
15395 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15396 true iff the declarator is of the form "(p)". */
15398 static cp_parameter_declarator *
15399 cp_parser_parameter_declaration (cp_parser *parser,
15400 bool template_parm_p,
15401 bool *parenthesized_p)
15403 int declares_class_or_enum;
15404 cp_decl_specifier_seq decl_specifiers;
15405 cp_declarator *declarator;
15406 tree default_argument;
15407 cp_token *token = NULL, *declarator_token_start = NULL;
15408 const char *saved_message;
15410 /* In a template parameter, `>' is not an operator.
15414 When parsing a default template-argument for a non-type
15415 template-parameter, the first non-nested `>' is taken as the end
15416 of the template parameter-list rather than a greater-than
15419 /* Type definitions may not appear in parameter types. */
15420 saved_message = parser->type_definition_forbidden_message;
15421 parser->type_definition_forbidden_message
15422 = G_("types may not be defined in parameter types");
15424 /* Parse the declaration-specifiers. */
15425 cp_parser_decl_specifier_seq (parser,
15426 CP_PARSER_FLAGS_NONE,
15428 &declares_class_or_enum);
15430 /* Complain about missing 'typename' or other invalid type names. */
15431 if (!decl_specifiers.any_type_specifiers_p)
15432 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15434 /* If an error occurred, there's no reason to attempt to parse the
15435 rest of the declaration. */
15436 if (cp_parser_error_occurred (parser))
15438 parser->type_definition_forbidden_message = saved_message;
15442 /* Peek at the next token. */
15443 token = cp_lexer_peek_token (parser->lexer);
15445 /* If the next token is a `)', `,', `=', `>', or `...', then there
15446 is no declarator. However, when variadic templates are enabled,
15447 there may be a declarator following `...'. */
15448 if (token->type == CPP_CLOSE_PAREN
15449 || token->type == CPP_COMMA
15450 || token->type == CPP_EQ
15451 || token->type == CPP_GREATER)
15454 if (parenthesized_p)
15455 *parenthesized_p = false;
15457 /* Otherwise, there should be a declarator. */
15460 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15461 parser->default_arg_ok_p = false;
15463 /* After seeing a decl-specifier-seq, if the next token is not a
15464 "(", there is no possibility that the code is a valid
15465 expression. Therefore, if parsing tentatively, we commit at
15467 if (!parser->in_template_argument_list_p
15468 /* In an expression context, having seen:
15472 we cannot be sure whether we are looking at a
15473 function-type (taking a "char" as a parameter) or a cast
15474 of some object of type "char" to "int". */
15475 && !parser->in_type_id_in_expr_p
15476 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15477 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15478 cp_parser_commit_to_tentative_parse (parser);
15479 /* Parse the declarator. */
15480 declarator_token_start = token;
15481 declarator = cp_parser_declarator (parser,
15482 CP_PARSER_DECLARATOR_EITHER,
15483 /*ctor_dtor_or_conv_p=*/NULL,
15485 /*member_p=*/false);
15486 parser->default_arg_ok_p = saved_default_arg_ok_p;
15487 /* After the declarator, allow more attributes. */
15488 decl_specifiers.attributes
15489 = chainon (decl_specifiers.attributes,
15490 cp_parser_attributes_opt (parser));
15493 /* If the next token is an ellipsis, and we have not seen a
15494 declarator name, and the type of the declarator contains parameter
15495 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15496 a parameter pack expansion expression. Otherwise, leave the
15497 ellipsis for a C-style variadic function. */
15498 token = cp_lexer_peek_token (parser->lexer);
15499 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15501 tree type = decl_specifiers.type;
15503 if (type && DECL_P (type))
15504 type = TREE_TYPE (type);
15507 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15508 && declarator_can_be_parameter_pack (declarator)
15509 && (!declarator || !declarator->parameter_pack_p)
15510 && uses_parameter_packs (type))
15512 /* Consume the `...'. */
15513 cp_lexer_consume_token (parser->lexer);
15514 maybe_warn_variadic_templates ();
15516 /* Build a pack expansion type */
15518 declarator->parameter_pack_p = true;
15520 decl_specifiers.type = make_pack_expansion (type);
15524 /* The restriction on defining new types applies only to the type
15525 of the parameter, not to the default argument. */
15526 parser->type_definition_forbidden_message = saved_message;
15528 /* If the next token is `=', then process a default argument. */
15529 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15531 /* Consume the `='. */
15532 cp_lexer_consume_token (parser->lexer);
15534 /* If we are defining a class, then the tokens that make up the
15535 default argument must be saved and processed later. */
15536 if (!template_parm_p && at_class_scope_p ()
15537 && TYPE_BEING_DEFINED (current_class_type)
15538 && !LAMBDA_TYPE_P (current_class_type))
15540 unsigned depth = 0;
15541 int maybe_template_id = 0;
15542 cp_token *first_token;
15545 /* Add tokens until we have processed the entire default
15546 argument. We add the range [first_token, token). */
15547 first_token = cp_lexer_peek_token (parser->lexer);
15552 /* Peek at the next token. */
15553 token = cp_lexer_peek_token (parser->lexer);
15554 /* What we do depends on what token we have. */
15555 switch (token->type)
15557 /* In valid code, a default argument must be
15558 immediately followed by a `,' `)', or `...'. */
15560 if (depth == 0 && maybe_template_id)
15562 /* If we've seen a '<', we might be in a
15563 template-argument-list. Until Core issue 325 is
15564 resolved, we don't know how this situation ought
15565 to be handled, so try to DTRT. We check whether
15566 what comes after the comma is a valid parameter
15567 declaration list. If it is, then the comma ends
15568 the default argument; otherwise the default
15569 argument continues. */
15570 bool error = false;
15572 /* Set ITALP so cp_parser_parameter_declaration_list
15573 doesn't decide to commit to this parse. */
15574 bool saved_italp = parser->in_template_argument_list_p;
15575 parser->in_template_argument_list_p = true;
15577 cp_parser_parse_tentatively (parser);
15578 cp_lexer_consume_token (parser->lexer);
15579 cp_parser_parameter_declaration_list (parser, &error);
15580 if (!cp_parser_error_occurred (parser) && !error)
15582 cp_parser_abort_tentative_parse (parser);
15584 parser->in_template_argument_list_p = saved_italp;
15587 case CPP_CLOSE_PAREN:
15589 /* If we run into a non-nested `;', `}', or `]',
15590 then the code is invalid -- but the default
15591 argument is certainly over. */
15592 case CPP_SEMICOLON:
15593 case CPP_CLOSE_BRACE:
15594 case CPP_CLOSE_SQUARE:
15597 /* Update DEPTH, if necessary. */
15598 else if (token->type == CPP_CLOSE_PAREN
15599 || token->type == CPP_CLOSE_BRACE
15600 || token->type == CPP_CLOSE_SQUARE)
15604 case CPP_OPEN_PAREN:
15605 case CPP_OPEN_SQUARE:
15606 case CPP_OPEN_BRACE:
15612 /* This might be the comparison operator, or it might
15613 start a template argument list. */
15614 ++maybe_template_id;
15618 if (cxx_dialect == cxx98)
15620 /* Fall through for C++0x, which treats the `>>'
15621 operator like two `>' tokens in certain
15627 /* This might be an operator, or it might close a
15628 template argument list. But if a previous '<'
15629 started a template argument list, this will have
15630 closed it, so we can't be in one anymore. */
15631 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15632 if (maybe_template_id < 0)
15633 maybe_template_id = 0;
15637 /* If we run out of tokens, issue an error message. */
15639 case CPP_PRAGMA_EOL:
15640 error_at (token->location, "file ends in default argument");
15646 /* In these cases, we should look for template-ids.
15647 For example, if the default argument is
15648 `X<int, double>()', we need to do name lookup to
15649 figure out whether or not `X' is a template; if
15650 so, the `,' does not end the default argument.
15652 That is not yet done. */
15659 /* If we've reached the end, stop. */
15663 /* Add the token to the token block. */
15664 token = cp_lexer_consume_token (parser->lexer);
15667 /* Create a DEFAULT_ARG to represent the unparsed default
15669 default_argument = make_node (DEFAULT_ARG);
15670 DEFARG_TOKENS (default_argument)
15671 = cp_token_cache_new (first_token, token);
15672 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15674 /* Outside of a class definition, we can just parse the
15675 assignment-expression. */
15678 token = cp_lexer_peek_token (parser->lexer);
15680 = cp_parser_default_argument (parser, template_parm_p);
15683 if (!parser->default_arg_ok_p)
15685 if (flag_permissive)
15686 warning (0, "deprecated use of default argument for parameter of non-function");
15689 error_at (token->location,
15690 "default arguments are only "
15691 "permitted for function parameters");
15692 default_argument = NULL_TREE;
15695 else if ((declarator && declarator->parameter_pack_p)
15696 || (decl_specifiers.type
15697 && PACK_EXPANSION_P (decl_specifiers.type)))
15699 /* Find the name of the parameter pack. */
15700 cp_declarator *id_declarator = declarator;
15701 while (id_declarator && id_declarator->kind != cdk_id)
15702 id_declarator = id_declarator->declarator;
15704 if (id_declarator && id_declarator->kind == cdk_id)
15705 error_at (declarator_token_start->location,
15707 ? "template parameter pack %qD"
15708 " cannot have a default argument"
15709 : "parameter pack %qD cannot have a default argument",
15710 id_declarator->u.id.unqualified_name);
15712 error_at (declarator_token_start->location,
15714 ? "template parameter pack cannot have a default argument"
15715 : "parameter pack cannot have a default argument");
15717 default_argument = NULL_TREE;
15721 default_argument = NULL_TREE;
15723 return make_parameter_declarator (&decl_specifiers,
15728 /* Parse a default argument and return it.
15730 TEMPLATE_PARM_P is true if this is a default argument for a
15731 non-type template parameter. */
15733 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15735 tree default_argument = NULL_TREE;
15736 bool saved_greater_than_is_operator_p;
15737 bool saved_local_variables_forbidden_p;
15739 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15741 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15742 parser->greater_than_is_operator_p = !template_parm_p;
15743 /* Local variable names (and the `this' keyword) may not
15744 appear in a default argument. */
15745 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15746 parser->local_variables_forbidden_p = true;
15747 /* Parse the assignment-expression. */
15748 if (template_parm_p)
15749 push_deferring_access_checks (dk_no_deferred);
15751 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15752 if (template_parm_p)
15753 pop_deferring_access_checks ();
15754 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15755 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15757 return default_argument;
15760 /* Parse a function-body.
15763 compound_statement */
15766 cp_parser_function_body (cp_parser *parser)
15768 cp_parser_compound_statement (parser, NULL, false);
15771 /* Parse a ctor-initializer-opt followed by a function-body. Return
15772 true if a ctor-initializer was present. */
15775 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15778 bool ctor_initializer_p;
15780 /* Begin the function body. */
15781 body = begin_function_body ();
15782 /* Parse the optional ctor-initializer. */
15783 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15784 /* Parse the function-body. */
15785 cp_parser_function_body (parser);
15786 /* Finish the function body. */
15787 finish_function_body (body);
15789 return ctor_initializer_p;
15792 /* Parse an initializer.
15795 = initializer-clause
15796 ( expression-list )
15798 Returns an expression representing the initializer. If no
15799 initializer is present, NULL_TREE is returned.
15801 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15802 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15803 set to TRUE if there is no initializer present. If there is an
15804 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15805 is set to true; otherwise it is set to false. */
15808 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15809 bool* non_constant_p)
15814 /* Peek at the next token. */
15815 token = cp_lexer_peek_token (parser->lexer);
15817 /* Let our caller know whether or not this initializer was
15819 *is_direct_init = (token->type != CPP_EQ);
15820 /* Assume that the initializer is constant. */
15821 *non_constant_p = false;
15823 if (token->type == CPP_EQ)
15825 /* Consume the `='. */
15826 cp_lexer_consume_token (parser->lexer);
15827 /* Parse the initializer-clause. */
15828 init = cp_parser_initializer_clause (parser, non_constant_p);
15830 else if (token->type == CPP_OPEN_PAREN)
15833 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15835 /*allow_expansion_p=*/true,
15838 return error_mark_node;
15839 init = build_tree_list_vec (vec);
15840 release_tree_vector (vec);
15842 else if (token->type == CPP_OPEN_BRACE)
15844 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15845 init = cp_parser_braced_list (parser, non_constant_p);
15846 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15850 /* Anything else is an error. */
15851 cp_parser_error (parser, "expected initializer");
15852 init = error_mark_node;
15858 /* Parse an initializer-clause.
15860 initializer-clause:
15861 assignment-expression
15864 Returns an expression representing the initializer.
15866 If the `assignment-expression' production is used the value
15867 returned is simply a representation for the expression.
15869 Otherwise, calls cp_parser_braced_list. */
15872 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15876 /* Assume the expression is constant. */
15877 *non_constant_p = false;
15879 /* If it is not a `{', then we are looking at an
15880 assignment-expression. */
15881 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15884 = cp_parser_constant_expression (parser,
15885 /*allow_non_constant_p=*/true,
15887 if (!*non_constant_p)
15888 initializer = fold_non_dependent_expr (initializer);
15891 initializer = cp_parser_braced_list (parser, non_constant_p);
15893 return initializer;
15896 /* Parse a brace-enclosed initializer list.
15899 { initializer-list , [opt] }
15902 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15903 the elements of the initializer-list (or NULL, if the last
15904 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15905 NULL_TREE. There is no way to detect whether or not the optional
15906 trailing `,' was provided. NON_CONSTANT_P is as for
15907 cp_parser_initializer. */
15910 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15914 /* Consume the `{' token. */
15915 cp_lexer_consume_token (parser->lexer);
15916 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15917 initializer = make_node (CONSTRUCTOR);
15918 /* If it's not a `}', then there is a non-trivial initializer. */
15919 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15921 /* Parse the initializer list. */
15922 CONSTRUCTOR_ELTS (initializer)
15923 = cp_parser_initializer_list (parser, non_constant_p);
15924 /* A trailing `,' token is allowed. */
15925 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15926 cp_lexer_consume_token (parser->lexer);
15928 /* Now, there should be a trailing `}'. */
15929 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15930 TREE_TYPE (initializer) = init_list_type_node;
15931 return initializer;
15934 /* Parse an initializer-list.
15937 initializer-clause ... [opt]
15938 initializer-list , initializer-clause ... [opt]
15943 identifier : initializer-clause
15944 initializer-list, identifier : initializer-clause
15946 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15947 for the initializer. If the INDEX of the elt is non-NULL, it is the
15948 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15949 as for cp_parser_initializer. */
15951 static VEC(constructor_elt,gc) *
15952 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15954 VEC(constructor_elt,gc) *v = NULL;
15956 /* Assume all of the expressions are constant. */
15957 *non_constant_p = false;
15959 /* Parse the rest of the list. */
15965 bool clause_non_constant_p;
15967 /* If the next token is an identifier and the following one is a
15968 colon, we are looking at the GNU designated-initializer
15970 if (cp_parser_allow_gnu_extensions_p (parser)
15971 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15972 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15974 /* Warn the user that they are using an extension. */
15975 pedwarn (input_location, OPT_pedantic,
15976 "ISO C++ does not allow designated initializers");
15977 /* Consume the identifier. */
15978 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15979 /* Consume the `:'. */
15980 cp_lexer_consume_token (parser->lexer);
15983 identifier = NULL_TREE;
15985 /* Parse the initializer. */
15986 initializer = cp_parser_initializer_clause (parser,
15987 &clause_non_constant_p);
15988 /* If any clause is non-constant, so is the entire initializer. */
15989 if (clause_non_constant_p)
15990 *non_constant_p = true;
15992 /* If we have an ellipsis, this is an initializer pack
15994 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15996 /* Consume the `...'. */
15997 cp_lexer_consume_token (parser->lexer);
15999 /* Turn the initializer into an initializer expansion. */
16000 initializer = make_pack_expansion (initializer);
16003 /* Add it to the vector. */
16004 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16006 /* If the next token is not a comma, we have reached the end of
16008 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16011 /* Peek at the next token. */
16012 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16013 /* If the next token is a `}', then we're still done. An
16014 initializer-clause can have a trailing `,' after the
16015 initializer-list and before the closing `}'. */
16016 if (token->type == CPP_CLOSE_BRACE)
16019 /* Consume the `,' token. */
16020 cp_lexer_consume_token (parser->lexer);
16026 /* Classes [gram.class] */
16028 /* Parse a class-name.
16034 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16035 to indicate that names looked up in dependent types should be
16036 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16037 keyword has been used to indicate that the name that appears next
16038 is a template. TAG_TYPE indicates the explicit tag given before
16039 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16040 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16041 is the class being defined in a class-head.
16043 Returns the TYPE_DECL representing the class. */
16046 cp_parser_class_name (cp_parser *parser,
16047 bool typename_keyword_p,
16048 bool template_keyword_p,
16049 enum tag_types tag_type,
16050 bool check_dependency_p,
16052 bool is_declaration)
16058 tree identifier = NULL_TREE;
16060 /* All class-names start with an identifier. */
16061 token = cp_lexer_peek_token (parser->lexer);
16062 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16064 cp_parser_error (parser, "expected class-name");
16065 return error_mark_node;
16068 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16069 to a template-id, so we save it here. */
16070 scope = parser->scope;
16071 if (scope == error_mark_node)
16072 return error_mark_node;
16074 /* Any name names a type if we're following the `typename' keyword
16075 in a qualified name where the enclosing scope is type-dependent. */
16076 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16077 && dependent_type_p (scope));
16078 /* Handle the common case (an identifier, but not a template-id)
16080 if (token->type == CPP_NAME
16081 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16083 cp_token *identifier_token;
16086 /* Look for the identifier. */
16087 identifier_token = cp_lexer_peek_token (parser->lexer);
16088 ambiguous_p = identifier_token->ambiguous_p;
16089 identifier = cp_parser_identifier (parser);
16090 /* If the next token isn't an identifier, we are certainly not
16091 looking at a class-name. */
16092 if (identifier == error_mark_node)
16093 decl = error_mark_node;
16094 /* If we know this is a type-name, there's no need to look it
16096 else if (typename_p)
16100 tree ambiguous_decls;
16101 /* If we already know that this lookup is ambiguous, then
16102 we've already issued an error message; there's no reason
16106 cp_parser_simulate_error (parser);
16107 return error_mark_node;
16109 /* If the next token is a `::', then the name must be a type
16112 [basic.lookup.qual]
16114 During the lookup for a name preceding the :: scope
16115 resolution operator, object, function, and enumerator
16116 names are ignored. */
16117 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16118 tag_type = typename_type;
16119 /* Look up the name. */
16120 decl = cp_parser_lookup_name (parser, identifier,
16122 /*is_template=*/false,
16123 /*is_namespace=*/false,
16124 check_dependency_p,
16126 identifier_token->location);
16127 if (ambiguous_decls)
16129 if (cp_parser_parsing_tentatively (parser))
16130 cp_parser_simulate_error (parser);
16131 return error_mark_node;
16137 /* Try a template-id. */
16138 decl = cp_parser_template_id (parser, template_keyword_p,
16139 check_dependency_p,
16141 if (decl == error_mark_node)
16142 return error_mark_node;
16145 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16147 /* If this is a typename, create a TYPENAME_TYPE. */
16148 if (typename_p && decl != error_mark_node)
16150 decl = make_typename_type (scope, decl, typename_type,
16151 /*complain=*/tf_error);
16152 if (decl != error_mark_node)
16153 decl = TYPE_NAME (decl);
16156 /* Check to see that it is really the name of a class. */
16157 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16158 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16159 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16160 /* Situations like this:
16162 template <typename T> struct A {
16163 typename T::template X<int>::I i;
16166 are problematic. Is `T::template X<int>' a class-name? The
16167 standard does not seem to be definitive, but there is no other
16168 valid interpretation of the following `::'. Therefore, those
16169 names are considered class-names. */
16171 decl = make_typename_type (scope, decl, tag_type, tf_error);
16172 if (decl != error_mark_node)
16173 decl = TYPE_NAME (decl);
16175 else if (TREE_CODE (decl) != TYPE_DECL
16176 || TREE_TYPE (decl) == error_mark_node
16177 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
16178 decl = error_mark_node;
16180 if (decl == error_mark_node)
16181 cp_parser_error (parser, "expected class-name");
16182 else if (identifier && !parser->scope)
16183 maybe_note_name_used_in_class (identifier, decl);
16188 /* Parse a class-specifier.
16191 class-head { member-specification [opt] }
16193 Returns the TREE_TYPE representing the class. */
16196 cp_parser_class_specifier (cp_parser* parser)
16199 tree attributes = NULL_TREE;
16200 bool nested_name_specifier_p;
16201 unsigned saved_num_template_parameter_lists;
16202 bool saved_in_function_body;
16203 bool saved_in_unbraced_linkage_specification_p;
16204 tree old_scope = NULL_TREE;
16205 tree scope = NULL_TREE;
16208 push_deferring_access_checks (dk_no_deferred);
16210 /* Parse the class-head. */
16211 type = cp_parser_class_head (parser,
16212 &nested_name_specifier_p,
16215 /* If the class-head was a semantic disaster, skip the entire body
16219 cp_parser_skip_to_end_of_block_or_statement (parser);
16220 pop_deferring_access_checks ();
16221 return error_mark_node;
16224 /* Look for the `{'. */
16225 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16227 pop_deferring_access_checks ();
16228 return error_mark_node;
16231 /* Process the base classes. If they're invalid, skip the
16232 entire class body. */
16233 if (!xref_basetypes (type, bases))
16235 /* Consuming the closing brace yields better error messages
16237 if (cp_parser_skip_to_closing_brace (parser))
16238 cp_lexer_consume_token (parser->lexer);
16239 pop_deferring_access_checks ();
16240 return error_mark_node;
16243 /* Issue an error message if type-definitions are forbidden here. */
16244 cp_parser_check_type_definition (parser);
16245 /* Remember that we are defining one more class. */
16246 ++parser->num_classes_being_defined;
16247 /* Inside the class, surrounding template-parameter-lists do not
16249 saved_num_template_parameter_lists
16250 = parser->num_template_parameter_lists;
16251 parser->num_template_parameter_lists = 0;
16252 /* We are not in a function body. */
16253 saved_in_function_body = parser->in_function_body;
16254 parser->in_function_body = false;
16255 /* We are not immediately inside an extern "lang" block. */
16256 saved_in_unbraced_linkage_specification_p
16257 = parser->in_unbraced_linkage_specification_p;
16258 parser->in_unbraced_linkage_specification_p = false;
16260 /* Start the class. */
16261 if (nested_name_specifier_p)
16263 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16264 old_scope = push_inner_scope (scope);
16266 type = begin_class_definition (type, attributes);
16268 if (type == error_mark_node)
16269 /* If the type is erroneous, skip the entire body of the class. */
16270 cp_parser_skip_to_closing_brace (parser);
16272 /* Parse the member-specification. */
16273 cp_parser_member_specification_opt (parser);
16275 /* Look for the trailing `}'. */
16276 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16277 /* Look for trailing attributes to apply to this class. */
16278 if (cp_parser_allow_gnu_extensions_p (parser))
16279 attributes = cp_parser_attributes_opt (parser);
16280 if (type != error_mark_node)
16281 type = finish_struct (type, attributes);
16282 if (nested_name_specifier_p)
16283 pop_inner_scope (old_scope, scope);
16284 /* If this class is not itself within the scope of another class,
16285 then we need to parse the bodies of all of the queued function
16286 definitions. Note that the queued functions defined in a class
16287 are not always processed immediately following the
16288 class-specifier for that class. Consider:
16291 struct B { void f() { sizeof (A); } };
16294 If `f' were processed before the processing of `A' were
16295 completed, there would be no way to compute the size of `A'.
16296 Note that the nesting we are interested in here is lexical --
16297 not the semantic nesting given by TYPE_CONTEXT. In particular,
16300 struct A { struct B; };
16301 struct A::B { void f() { } };
16303 there is no need to delay the parsing of `A::B::f'. */
16304 if (--parser->num_classes_being_defined == 0)
16307 tree class_type = NULL_TREE;
16308 tree pushed_scope = NULL_TREE;
16310 cp_default_arg_entry *e;
16312 /* In a first pass, parse default arguments to the functions.
16313 Then, in a second pass, parse the bodies of the functions.
16314 This two-phased approach handles cases like:
16323 VEC_iterate (cp_default_arg_entry, unparsed_funs_with_default_args,
16328 /* If there are default arguments that have not yet been processed,
16329 take care of them now. */
16330 if (class_type != e->class_type)
16333 pop_scope (pushed_scope);
16334 class_type = e->class_type;
16335 pushed_scope = push_scope (class_type);
16337 /* Make sure that any template parameters are in scope. */
16338 maybe_begin_member_template_processing (fn);
16339 /* Parse the default argument expressions. */
16340 cp_parser_late_parsing_default_args (parser, fn);
16341 /* Remove any template parameters from the symbol table. */
16342 maybe_end_member_template_processing ();
16345 pop_scope (pushed_scope);
16346 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16347 /* Now parse the body of the functions. */
16349 VEC_iterate (tree, unparsed_funs_with_definitions, ix, fn);
16351 cp_parser_late_parsing_for_member (parser, fn);
16352 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
16355 /* Put back any saved access checks. */
16356 pop_deferring_access_checks ();
16358 /* Restore saved state. */
16359 parser->in_function_body = saved_in_function_body;
16360 parser->num_template_parameter_lists
16361 = saved_num_template_parameter_lists;
16362 parser->in_unbraced_linkage_specification_p
16363 = saved_in_unbraced_linkage_specification_p;
16368 /* Parse a class-head.
16371 class-key identifier [opt] base-clause [opt]
16372 class-key nested-name-specifier identifier base-clause [opt]
16373 class-key nested-name-specifier [opt] template-id
16377 class-key attributes identifier [opt] base-clause [opt]
16378 class-key attributes nested-name-specifier identifier base-clause [opt]
16379 class-key attributes nested-name-specifier [opt] template-id
16382 Upon return BASES is initialized to the list of base classes (or
16383 NULL, if there are none) in the same form returned by
16384 cp_parser_base_clause.
16386 Returns the TYPE of the indicated class. Sets
16387 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16388 involving a nested-name-specifier was used, and FALSE otherwise.
16390 Returns error_mark_node if this is not a class-head.
16392 Returns NULL_TREE if the class-head is syntactically valid, but
16393 semantically invalid in a way that means we should skip the entire
16394 body of the class. */
16397 cp_parser_class_head (cp_parser* parser,
16398 bool* nested_name_specifier_p,
16399 tree *attributes_p,
16402 tree nested_name_specifier;
16403 enum tag_types class_key;
16404 tree id = NULL_TREE;
16405 tree type = NULL_TREE;
16407 bool template_id_p = false;
16408 bool qualified_p = false;
16409 bool invalid_nested_name_p = false;
16410 bool invalid_explicit_specialization_p = false;
16411 tree pushed_scope = NULL_TREE;
16412 unsigned num_templates;
16413 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16414 /* Assume no nested-name-specifier will be present. */
16415 *nested_name_specifier_p = false;
16416 /* Assume no template parameter lists will be used in defining the
16420 *bases = NULL_TREE;
16422 /* Look for the class-key. */
16423 class_key = cp_parser_class_key (parser);
16424 if (class_key == none_type)
16425 return error_mark_node;
16427 /* Parse the attributes. */
16428 attributes = cp_parser_attributes_opt (parser);
16430 /* If the next token is `::', that is invalid -- but sometimes
16431 people do try to write:
16435 Handle this gracefully by accepting the extra qualifier, and then
16436 issuing an error about it later if this really is a
16437 class-head. If it turns out just to be an elaborated type
16438 specifier, remain silent. */
16439 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16440 qualified_p = true;
16442 push_deferring_access_checks (dk_no_check);
16444 /* Determine the name of the class. Begin by looking for an
16445 optional nested-name-specifier. */
16446 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16447 nested_name_specifier
16448 = cp_parser_nested_name_specifier_opt (parser,
16449 /*typename_keyword_p=*/false,
16450 /*check_dependency_p=*/false,
16452 /*is_declaration=*/false);
16453 /* If there was a nested-name-specifier, then there *must* be an
16455 if (nested_name_specifier)
16457 type_start_token = cp_lexer_peek_token (parser->lexer);
16458 /* Although the grammar says `identifier', it really means
16459 `class-name' or `template-name'. You are only allowed to
16460 define a class that has already been declared with this
16463 The proposed resolution for Core Issue 180 says that wherever
16464 you see `class T::X' you should treat `X' as a type-name.
16466 It is OK to define an inaccessible class; for example:
16468 class A { class B; };
16471 We do not know if we will see a class-name, or a
16472 template-name. We look for a class-name first, in case the
16473 class-name is a template-id; if we looked for the
16474 template-name first we would stop after the template-name. */
16475 cp_parser_parse_tentatively (parser);
16476 type = cp_parser_class_name (parser,
16477 /*typename_keyword_p=*/false,
16478 /*template_keyword_p=*/false,
16480 /*check_dependency_p=*/false,
16481 /*class_head_p=*/true,
16482 /*is_declaration=*/false);
16483 /* If that didn't work, ignore the nested-name-specifier. */
16484 if (!cp_parser_parse_definitely (parser))
16486 invalid_nested_name_p = true;
16487 type_start_token = cp_lexer_peek_token (parser->lexer);
16488 id = cp_parser_identifier (parser);
16489 if (id == error_mark_node)
16492 /* If we could not find a corresponding TYPE, treat this
16493 declaration like an unqualified declaration. */
16494 if (type == error_mark_node)
16495 nested_name_specifier = NULL_TREE;
16496 /* Otherwise, count the number of templates used in TYPE and its
16497 containing scopes. */
16502 for (scope = TREE_TYPE (type);
16503 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16504 scope = (TYPE_P (scope)
16505 ? TYPE_CONTEXT (scope)
16506 : DECL_CONTEXT (scope)))
16508 && CLASS_TYPE_P (scope)
16509 && CLASSTYPE_TEMPLATE_INFO (scope)
16510 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16511 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16515 /* Otherwise, the identifier is optional. */
16518 /* We don't know whether what comes next is a template-id,
16519 an identifier, or nothing at all. */
16520 cp_parser_parse_tentatively (parser);
16521 /* Check for a template-id. */
16522 type_start_token = cp_lexer_peek_token (parser->lexer);
16523 id = cp_parser_template_id (parser,
16524 /*template_keyword_p=*/false,
16525 /*check_dependency_p=*/true,
16526 /*is_declaration=*/true);
16527 /* If that didn't work, it could still be an identifier. */
16528 if (!cp_parser_parse_definitely (parser))
16530 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16532 type_start_token = cp_lexer_peek_token (parser->lexer);
16533 id = cp_parser_identifier (parser);
16540 template_id_p = true;
16545 pop_deferring_access_checks ();
16548 cp_parser_check_for_invalid_template_id (parser, id,
16549 type_start_token->location);
16551 /* If it's not a `:' or a `{' then we can't really be looking at a
16552 class-head, since a class-head only appears as part of a
16553 class-specifier. We have to detect this situation before calling
16554 xref_tag, since that has irreversible side-effects. */
16555 if (!cp_parser_next_token_starts_class_definition_p (parser))
16557 cp_parser_error (parser, "expected %<{%> or %<:%>");
16558 return error_mark_node;
16561 /* At this point, we're going ahead with the class-specifier, even
16562 if some other problem occurs. */
16563 cp_parser_commit_to_tentative_parse (parser);
16564 /* Issue the error about the overly-qualified name now. */
16567 cp_parser_error (parser,
16568 "global qualification of class name is invalid");
16569 return error_mark_node;
16571 else if (invalid_nested_name_p)
16573 cp_parser_error (parser,
16574 "qualified name does not name a class");
16575 return error_mark_node;
16577 else if (nested_name_specifier)
16581 /* Reject typedef-names in class heads. */
16582 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16584 error_at (type_start_token->location,
16585 "invalid class name in declaration of %qD",
16591 /* Figure out in what scope the declaration is being placed. */
16592 scope = current_scope ();
16593 /* If that scope does not contain the scope in which the
16594 class was originally declared, the program is invalid. */
16595 if (scope && !is_ancestor (scope, nested_name_specifier))
16597 if (at_namespace_scope_p ())
16598 error_at (type_start_token->location,
16599 "declaration of %qD in namespace %qD which does not "
16601 type, scope, nested_name_specifier);
16603 error_at (type_start_token->location,
16604 "declaration of %qD in %qD which does not enclose %qD",
16605 type, scope, nested_name_specifier);
16611 A declarator-id shall not be qualified except for the
16612 definition of a ... nested class outside of its class
16613 ... [or] the definition or explicit instantiation of a
16614 class member of a namespace outside of its namespace. */
16615 if (scope == nested_name_specifier)
16617 permerror (nested_name_specifier_token_start->location,
16618 "extra qualification not allowed");
16619 nested_name_specifier = NULL_TREE;
16623 /* An explicit-specialization must be preceded by "template <>". If
16624 it is not, try to recover gracefully. */
16625 if (at_namespace_scope_p ()
16626 && parser->num_template_parameter_lists == 0
16629 error_at (type_start_token->location,
16630 "an explicit specialization must be preceded by %<template <>%>");
16631 invalid_explicit_specialization_p = true;
16632 /* Take the same action that would have been taken by
16633 cp_parser_explicit_specialization. */
16634 ++parser->num_template_parameter_lists;
16635 begin_specialization ();
16637 /* There must be no "return" statements between this point and the
16638 end of this function; set "type "to the correct return value and
16639 use "goto done;" to return. */
16640 /* Make sure that the right number of template parameters were
16642 if (!cp_parser_check_template_parameters (parser, num_templates,
16643 type_start_token->location,
16644 /*declarator=*/NULL))
16646 /* If something went wrong, there is no point in even trying to
16647 process the class-definition. */
16652 /* Look up the type. */
16655 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16656 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16657 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16659 error_at (type_start_token->location,
16660 "function template %qD redeclared as a class template", id);
16661 type = error_mark_node;
16665 type = TREE_TYPE (id);
16666 type = maybe_process_partial_specialization (type);
16668 if (nested_name_specifier)
16669 pushed_scope = push_scope (nested_name_specifier);
16671 else if (nested_name_specifier)
16677 template <typename T> struct S { struct T };
16678 template <typename T> struct S<T>::T { };
16680 we will get a TYPENAME_TYPE when processing the definition of
16681 `S::T'. We need to resolve it to the actual type before we
16682 try to define it. */
16683 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16685 class_type = resolve_typename_type (TREE_TYPE (type),
16686 /*only_current_p=*/false);
16687 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16688 type = TYPE_NAME (class_type);
16691 cp_parser_error (parser, "could not resolve typename type");
16692 type = error_mark_node;
16696 if (maybe_process_partial_specialization (TREE_TYPE (type))
16697 == error_mark_node)
16703 class_type = current_class_type;
16704 /* Enter the scope indicated by the nested-name-specifier. */
16705 pushed_scope = push_scope (nested_name_specifier);
16706 /* Get the canonical version of this type. */
16707 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16708 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16709 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16711 type = push_template_decl (type);
16712 if (type == error_mark_node)
16719 type = TREE_TYPE (type);
16720 *nested_name_specifier_p = true;
16722 else /* The name is not a nested name. */
16724 /* If the class was unnamed, create a dummy name. */
16726 id = make_anon_name ();
16727 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16728 parser->num_template_parameter_lists);
16731 /* Indicate whether this class was declared as a `class' or as a
16733 if (TREE_CODE (type) == RECORD_TYPE)
16734 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16735 cp_parser_check_class_key (class_key, type);
16737 /* If this type was already complete, and we see another definition,
16738 that's an error. */
16739 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16741 error_at (type_start_token->location, "redefinition of %q#T",
16743 error_at (type_start_token->location, "previous definition of %q+#T",
16748 else if (type == error_mark_node)
16751 /* We will have entered the scope containing the class; the names of
16752 base classes should be looked up in that context. For example:
16754 struct A { struct B {}; struct C; };
16755 struct A::C : B {};
16759 /* Get the list of base-classes, if there is one. */
16760 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16761 *bases = cp_parser_base_clause (parser);
16764 /* Leave the scope given by the nested-name-specifier. We will
16765 enter the class scope itself while processing the members. */
16767 pop_scope (pushed_scope);
16769 if (invalid_explicit_specialization_p)
16771 end_specialization ();
16772 --parser->num_template_parameter_lists;
16776 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16777 *attributes_p = attributes;
16781 /* Parse a class-key.
16788 Returns the kind of class-key specified, or none_type to indicate
16791 static enum tag_types
16792 cp_parser_class_key (cp_parser* parser)
16795 enum tag_types tag_type;
16797 /* Look for the class-key. */
16798 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
16802 /* Check to see if the TOKEN is a class-key. */
16803 tag_type = cp_parser_token_is_class_key (token);
16805 cp_parser_error (parser, "expected class-key");
16809 /* Parse an (optional) member-specification.
16811 member-specification:
16812 member-declaration member-specification [opt]
16813 access-specifier : member-specification [opt] */
16816 cp_parser_member_specification_opt (cp_parser* parser)
16823 /* Peek at the next token. */
16824 token = cp_lexer_peek_token (parser->lexer);
16825 /* If it's a `}', or EOF then we've seen all the members. */
16826 if (token->type == CPP_CLOSE_BRACE
16827 || token->type == CPP_EOF
16828 || token->type == CPP_PRAGMA_EOL)
16831 /* See if this token is a keyword. */
16832 keyword = token->keyword;
16836 case RID_PROTECTED:
16838 /* Consume the access-specifier. */
16839 cp_lexer_consume_token (parser->lexer);
16840 /* Remember which access-specifier is active. */
16841 current_access_specifier = token->u.value;
16842 /* Look for the `:'. */
16843 cp_parser_require (parser, CPP_COLON, RT_COLON);
16847 /* Accept #pragmas at class scope. */
16848 if (token->type == CPP_PRAGMA)
16850 cp_parser_pragma (parser, pragma_external);
16854 /* Otherwise, the next construction must be a
16855 member-declaration. */
16856 cp_parser_member_declaration (parser);
16861 /* Parse a member-declaration.
16863 member-declaration:
16864 decl-specifier-seq [opt] member-declarator-list [opt] ;
16865 function-definition ; [opt]
16866 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16868 template-declaration
16870 member-declarator-list:
16872 member-declarator-list , member-declarator
16875 declarator pure-specifier [opt]
16876 declarator constant-initializer [opt]
16877 identifier [opt] : constant-expression
16881 member-declaration:
16882 __extension__ member-declaration
16885 declarator attributes [opt] pure-specifier [opt]
16886 declarator attributes [opt] constant-initializer [opt]
16887 identifier [opt] attributes [opt] : constant-expression
16891 member-declaration:
16892 static_assert-declaration */
16895 cp_parser_member_declaration (cp_parser* parser)
16897 cp_decl_specifier_seq decl_specifiers;
16898 tree prefix_attributes;
16900 int declares_class_or_enum;
16902 cp_token *token = NULL;
16903 cp_token *decl_spec_token_start = NULL;
16904 cp_token *initializer_token_start = NULL;
16905 int saved_pedantic;
16907 /* Check for the `__extension__' keyword. */
16908 if (cp_parser_extension_opt (parser, &saved_pedantic))
16911 cp_parser_member_declaration (parser);
16912 /* Restore the old value of the PEDANTIC flag. */
16913 pedantic = saved_pedantic;
16918 /* Check for a template-declaration. */
16919 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16921 /* An explicit specialization here is an error condition, and we
16922 expect the specialization handler to detect and report this. */
16923 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16924 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16925 cp_parser_explicit_specialization (parser);
16927 cp_parser_template_declaration (parser, /*member_p=*/true);
16932 /* Check for a using-declaration. */
16933 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16935 /* Parse the using-declaration. */
16936 cp_parser_using_declaration (parser,
16937 /*access_declaration_p=*/false);
16941 /* Check for @defs. */
16942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16945 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16946 ivar = ivar_chains;
16950 ivar = TREE_CHAIN (member);
16951 TREE_CHAIN (member) = NULL_TREE;
16952 finish_member_declaration (member);
16957 /* If the next token is `static_assert' we have a static assertion. */
16958 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16960 cp_parser_static_assert (parser, /*member_p=*/true);
16964 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16967 /* Parse the decl-specifier-seq. */
16968 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16969 cp_parser_decl_specifier_seq (parser,
16970 CP_PARSER_FLAGS_OPTIONAL,
16972 &declares_class_or_enum);
16973 prefix_attributes = decl_specifiers.attributes;
16974 decl_specifiers.attributes = NULL_TREE;
16975 /* Check for an invalid type-name. */
16976 if (!decl_specifiers.any_type_specifiers_p
16977 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16979 /* If there is no declarator, then the decl-specifier-seq should
16981 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16983 /* If there was no decl-specifier-seq, and the next token is a
16984 `;', then we have something like:
16990 Each member-declaration shall declare at least one member
16991 name of the class. */
16992 if (!decl_specifiers.any_specifiers_p)
16994 cp_token *token = cp_lexer_peek_token (parser->lexer);
16995 if (!in_system_header_at (token->location))
16996 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17002 /* See if this declaration is a friend. */
17003 friend_p = cp_parser_friend_p (&decl_specifiers);
17004 /* If there were decl-specifiers, check to see if there was
17005 a class-declaration. */
17006 type = check_tag_decl (&decl_specifiers);
17007 /* Nested classes have already been added to the class, but
17008 a `friend' needs to be explicitly registered. */
17011 /* If the `friend' keyword was present, the friend must
17012 be introduced with a class-key. */
17013 if (!declares_class_or_enum)
17014 error_at (decl_spec_token_start->location,
17015 "a class-key must be used when declaring a friend");
17018 template <typename T> struct A {
17019 friend struct A<T>::B;
17022 A<T>::B will be represented by a TYPENAME_TYPE, and
17023 therefore not recognized by check_tag_decl. */
17025 && decl_specifiers.type
17026 && TYPE_P (decl_specifiers.type))
17027 type = decl_specifiers.type;
17028 if (!type || !TYPE_P (type))
17029 error_at (decl_spec_token_start->location,
17030 "friend declaration does not name a class or "
17033 make_friend_class (current_class_type, type,
17034 /*complain=*/true);
17036 /* If there is no TYPE, an error message will already have
17038 else if (!type || type == error_mark_node)
17040 /* An anonymous aggregate has to be handled specially; such
17041 a declaration really declares a data member (with a
17042 particular type), as opposed to a nested class. */
17043 else if (ANON_AGGR_TYPE_P (type))
17045 /* Remove constructors and such from TYPE, now that we
17046 know it is an anonymous aggregate. */
17047 fixup_anonymous_aggr (type);
17048 /* And make the corresponding data member. */
17049 decl = build_decl (decl_spec_token_start->location,
17050 FIELD_DECL, NULL_TREE, type);
17051 /* Add it to the class. */
17052 finish_member_declaration (decl);
17055 cp_parser_check_access_in_redeclaration
17057 decl_spec_token_start->location);
17062 /* See if these declarations will be friends. */
17063 friend_p = cp_parser_friend_p (&decl_specifiers);
17065 /* Keep going until we hit the `;' at the end of the
17067 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17069 tree attributes = NULL_TREE;
17070 tree first_attribute;
17072 /* Peek at the next token. */
17073 token = cp_lexer_peek_token (parser->lexer);
17075 /* Check for a bitfield declaration. */
17076 if (token->type == CPP_COLON
17077 || (token->type == CPP_NAME
17078 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17084 /* Get the name of the bitfield. Note that we cannot just
17085 check TOKEN here because it may have been invalidated by
17086 the call to cp_lexer_peek_nth_token above. */
17087 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17088 identifier = cp_parser_identifier (parser);
17090 identifier = NULL_TREE;
17092 /* Consume the `:' token. */
17093 cp_lexer_consume_token (parser->lexer);
17094 /* Get the width of the bitfield. */
17096 = cp_parser_constant_expression (parser,
17097 /*allow_non_constant=*/false,
17100 /* Look for attributes that apply to the bitfield. */
17101 attributes = cp_parser_attributes_opt (parser);
17102 /* Remember which attributes are prefix attributes and
17104 first_attribute = attributes;
17105 /* Combine the attributes. */
17106 attributes = chainon (prefix_attributes, attributes);
17108 /* Create the bitfield declaration. */
17109 decl = grokbitfield (identifier
17110 ? make_id_declarator (NULL_TREE,
17120 cp_declarator *declarator;
17122 tree asm_specification;
17123 int ctor_dtor_or_conv_p;
17125 /* Parse the declarator. */
17127 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17128 &ctor_dtor_or_conv_p,
17129 /*parenthesized_p=*/NULL,
17130 /*member_p=*/true);
17132 /* If something went wrong parsing the declarator, make sure
17133 that we at least consume some tokens. */
17134 if (declarator == cp_error_declarator)
17136 /* Skip to the end of the statement. */
17137 cp_parser_skip_to_end_of_statement (parser);
17138 /* If the next token is not a semicolon, that is
17139 probably because we just skipped over the body of
17140 a function. So, we consume a semicolon if
17141 present, but do not issue an error message if it
17143 if (cp_lexer_next_token_is (parser->lexer,
17145 cp_lexer_consume_token (parser->lexer);
17149 if (declares_class_or_enum & 2)
17150 cp_parser_check_for_definition_in_return_type
17151 (declarator, decl_specifiers.type,
17152 decl_specifiers.type_location);
17154 /* Look for an asm-specification. */
17155 asm_specification = cp_parser_asm_specification_opt (parser);
17156 /* Look for attributes that apply to the declaration. */
17157 attributes = cp_parser_attributes_opt (parser);
17158 /* Remember which attributes are prefix attributes and
17160 first_attribute = attributes;
17161 /* Combine the attributes. */
17162 attributes = chainon (prefix_attributes, attributes);
17164 /* If it's an `=', then we have a constant-initializer or a
17165 pure-specifier. It is not correct to parse the
17166 initializer before registering the member declaration
17167 since the member declaration should be in scope while
17168 its initializer is processed. However, the rest of the
17169 front end does not yet provide an interface that allows
17170 us to handle this correctly. */
17171 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17175 A pure-specifier shall be used only in the declaration of
17176 a virtual function.
17178 A member-declarator can contain a constant-initializer
17179 only if it declares a static member of integral or
17182 Therefore, if the DECLARATOR is for a function, we look
17183 for a pure-specifier; otherwise, we look for a
17184 constant-initializer. When we call `grokfield', it will
17185 perform more stringent semantics checks. */
17186 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17187 if (function_declarator_p (declarator))
17188 initializer = cp_parser_pure_specifier (parser);
17190 /* Parse the initializer. */
17191 initializer = cp_parser_constant_initializer (parser);
17193 /* Otherwise, there is no initializer. */
17195 initializer = NULL_TREE;
17197 /* See if we are probably looking at a function
17198 definition. We are certainly not looking at a
17199 member-declarator. Calling `grokfield' has
17200 side-effects, so we must not do it unless we are sure
17201 that we are looking at a member-declarator. */
17202 if (cp_parser_token_starts_function_definition_p
17203 (cp_lexer_peek_token (parser->lexer)))
17205 /* The grammar does not allow a pure-specifier to be
17206 used when a member function is defined. (It is
17207 possible that this fact is an oversight in the
17208 standard, since a pure function may be defined
17209 outside of the class-specifier. */
17211 error_at (initializer_token_start->location,
17212 "pure-specifier on function-definition");
17213 decl = cp_parser_save_member_function_body (parser,
17217 /* If the member was not a friend, declare it here. */
17219 finish_member_declaration (decl);
17220 /* Peek at the next token. */
17221 token = cp_lexer_peek_token (parser->lexer);
17222 /* If the next token is a semicolon, consume it. */
17223 if (token->type == CPP_SEMICOLON)
17224 cp_lexer_consume_token (parser->lexer);
17228 if (declarator->kind == cdk_function)
17229 declarator->id_loc = token->location;
17230 /* Create the declaration. */
17231 decl = grokfield (declarator, &decl_specifiers,
17232 initializer, /*init_const_expr_p=*/true,
17237 /* Reset PREFIX_ATTRIBUTES. */
17238 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17239 attributes = TREE_CHAIN (attributes);
17241 TREE_CHAIN (attributes) = NULL_TREE;
17243 /* If there is any qualification still in effect, clear it
17244 now; we will be starting fresh with the next declarator. */
17245 parser->scope = NULL_TREE;
17246 parser->qualifying_scope = NULL_TREE;
17247 parser->object_scope = NULL_TREE;
17248 /* If it's a `,', then there are more declarators. */
17249 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17250 cp_lexer_consume_token (parser->lexer);
17251 /* If the next token isn't a `;', then we have a parse error. */
17252 else if (cp_lexer_next_token_is_not (parser->lexer,
17255 cp_parser_error (parser, "expected %<;%>");
17256 /* Skip tokens until we find a `;'. */
17257 cp_parser_skip_to_end_of_statement (parser);
17264 /* Add DECL to the list of members. */
17266 finish_member_declaration (decl);
17268 if (TREE_CODE (decl) == FUNCTION_DECL)
17269 cp_parser_save_default_args (parser, decl);
17274 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17277 /* Parse a pure-specifier.
17282 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17283 Otherwise, ERROR_MARK_NODE is returned. */
17286 cp_parser_pure_specifier (cp_parser* parser)
17290 /* Look for the `=' token. */
17291 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17292 return error_mark_node;
17293 /* Look for the `0' token. */
17294 token = cp_lexer_peek_token (parser->lexer);
17296 if (token->type == CPP_EOF
17297 || token->type == CPP_PRAGMA_EOL)
17298 return error_mark_node;
17300 cp_lexer_consume_token (parser->lexer);
17302 /* Accept = default or = delete in c++0x mode. */
17303 if (token->keyword == RID_DEFAULT
17304 || token->keyword == RID_DELETE)
17306 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17307 return token->u.value;
17310 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17311 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17313 cp_parser_error (parser,
17314 "invalid pure specifier (only %<= 0%> is allowed)");
17315 cp_parser_skip_to_end_of_statement (parser);
17316 return error_mark_node;
17318 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17320 error_at (token->location, "templates may not be %<virtual%>");
17321 return error_mark_node;
17324 return integer_zero_node;
17327 /* Parse a constant-initializer.
17329 constant-initializer:
17330 = constant-expression
17332 Returns a representation of the constant-expression. */
17335 cp_parser_constant_initializer (cp_parser* parser)
17337 /* Look for the `=' token. */
17338 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17339 return error_mark_node;
17341 /* It is invalid to write:
17343 struct S { static const int i = { 7 }; };
17346 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17348 cp_parser_error (parser,
17349 "a brace-enclosed initializer is not allowed here");
17350 /* Consume the opening brace. */
17351 cp_lexer_consume_token (parser->lexer);
17352 /* Skip the initializer. */
17353 cp_parser_skip_to_closing_brace (parser);
17354 /* Look for the trailing `}'. */
17355 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17357 return error_mark_node;
17360 return cp_parser_constant_expression (parser,
17361 /*allow_non_constant=*/false,
17365 /* Derived classes [gram.class.derived] */
17367 /* Parse a base-clause.
17370 : base-specifier-list
17372 base-specifier-list:
17373 base-specifier ... [opt]
17374 base-specifier-list , base-specifier ... [opt]
17376 Returns a TREE_LIST representing the base-classes, in the order in
17377 which they were declared. The representation of each node is as
17378 described by cp_parser_base_specifier.
17380 In the case that no bases are specified, this function will return
17381 NULL_TREE, not ERROR_MARK_NODE. */
17384 cp_parser_base_clause (cp_parser* parser)
17386 tree bases = NULL_TREE;
17388 /* Look for the `:' that begins the list. */
17389 cp_parser_require (parser, CPP_COLON, RT_COLON);
17391 /* Scan the base-specifier-list. */
17396 bool pack_expansion_p = false;
17398 /* Look for the base-specifier. */
17399 base = cp_parser_base_specifier (parser);
17400 /* Look for the (optional) ellipsis. */
17401 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17403 /* Consume the `...'. */
17404 cp_lexer_consume_token (parser->lexer);
17406 pack_expansion_p = true;
17409 /* Add BASE to the front of the list. */
17410 if (base != error_mark_node)
17412 if (pack_expansion_p)
17413 /* Make this a pack expansion type. */
17414 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17417 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17419 TREE_CHAIN (base) = bases;
17423 /* Peek at the next token. */
17424 token = cp_lexer_peek_token (parser->lexer);
17425 /* If it's not a comma, then the list is complete. */
17426 if (token->type != CPP_COMMA)
17428 /* Consume the `,'. */
17429 cp_lexer_consume_token (parser->lexer);
17432 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17433 base class had a qualified name. However, the next name that
17434 appears is certainly not qualified. */
17435 parser->scope = NULL_TREE;
17436 parser->qualifying_scope = NULL_TREE;
17437 parser->object_scope = NULL_TREE;
17439 return nreverse (bases);
17442 /* Parse a base-specifier.
17445 :: [opt] nested-name-specifier [opt] class-name
17446 virtual access-specifier [opt] :: [opt] nested-name-specifier
17448 access-specifier virtual [opt] :: [opt] nested-name-specifier
17451 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17452 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17453 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17454 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17457 cp_parser_base_specifier (cp_parser* parser)
17461 bool virtual_p = false;
17462 bool duplicate_virtual_error_issued_p = false;
17463 bool duplicate_access_error_issued_p = false;
17464 bool class_scope_p, template_p;
17465 tree access = access_default_node;
17468 /* Process the optional `virtual' and `access-specifier'. */
17471 /* Peek at the next token. */
17472 token = cp_lexer_peek_token (parser->lexer);
17473 /* Process `virtual'. */
17474 switch (token->keyword)
17477 /* If `virtual' appears more than once, issue an error. */
17478 if (virtual_p && !duplicate_virtual_error_issued_p)
17480 cp_parser_error (parser,
17481 "%<virtual%> specified more than once in base-specified");
17482 duplicate_virtual_error_issued_p = true;
17487 /* Consume the `virtual' token. */
17488 cp_lexer_consume_token (parser->lexer);
17493 case RID_PROTECTED:
17495 /* If more than one access specifier appears, issue an
17497 if (access != access_default_node
17498 && !duplicate_access_error_issued_p)
17500 cp_parser_error (parser,
17501 "more than one access specifier in base-specified");
17502 duplicate_access_error_issued_p = true;
17505 access = ridpointers[(int) token->keyword];
17507 /* Consume the access-specifier. */
17508 cp_lexer_consume_token (parser->lexer);
17517 /* It is not uncommon to see programs mechanically, erroneously, use
17518 the 'typename' keyword to denote (dependent) qualified types
17519 as base classes. */
17520 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17522 token = cp_lexer_peek_token (parser->lexer);
17523 if (!processing_template_decl)
17524 error_at (token->location,
17525 "keyword %<typename%> not allowed outside of templates");
17527 error_at (token->location,
17528 "keyword %<typename%> not allowed in this context "
17529 "(the base class is implicitly a type)");
17530 cp_lexer_consume_token (parser->lexer);
17533 /* Look for the optional `::' operator. */
17534 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17535 /* Look for the nested-name-specifier. The simplest way to
17540 The keyword `typename' is not permitted in a base-specifier or
17541 mem-initializer; in these contexts a qualified name that
17542 depends on a template-parameter is implicitly assumed to be a
17545 is to pretend that we have seen the `typename' keyword at this
17547 cp_parser_nested_name_specifier_opt (parser,
17548 /*typename_keyword_p=*/true,
17549 /*check_dependency_p=*/true,
17551 /*is_declaration=*/true);
17552 /* If the base class is given by a qualified name, assume that names
17553 we see are type names or templates, as appropriate. */
17554 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17555 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17557 /* Finally, look for the class-name. */
17558 type = cp_parser_class_name (parser,
17562 /*check_dependency_p=*/true,
17563 /*class_head_p=*/false,
17564 /*is_declaration=*/true);
17566 if (type == error_mark_node)
17567 return error_mark_node;
17569 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17572 /* Exception handling [gram.exception] */
17574 /* Parse an (optional) exception-specification.
17576 exception-specification:
17577 throw ( type-id-list [opt] )
17579 Returns a TREE_LIST representing the exception-specification. The
17580 TREE_VALUE of each node is a type. */
17583 cp_parser_exception_specification_opt (cp_parser* parser)
17587 const char *saved_message;
17589 /* Peek at the next token. */
17590 token = cp_lexer_peek_token (parser->lexer);
17592 /* Is it a noexcept-specification? */
17593 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
17596 cp_lexer_consume_token (parser->lexer);
17598 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
17600 cp_lexer_consume_token (parser->lexer);
17602 /* Types may not be defined in an exception-specification. */
17603 saved_message = parser->type_definition_forbidden_message;
17604 parser->type_definition_forbidden_message
17605 = G_("types may not be defined in an exception-specification");
17607 expr = cp_parser_constant_expression (parser, false, NULL);
17609 /* Restore the saved message. */
17610 parser->type_definition_forbidden_message = saved_message;
17612 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17615 expr = boolean_true_node;
17617 return build_noexcept_spec (expr, tf_warning_or_error);
17620 /* If it's not `throw', then there's no exception-specification. */
17621 if (!cp_parser_is_keyword (token, RID_THROW))
17625 /* Enable this once a lot of code has transitioned to noexcept? */
17626 if (cxx_dialect == cxx0x && !in_system_header)
17627 warning (OPT_Wdeprecated, "dynamic exception specifications are "
17628 "deprecated in C++0x; use %<noexcept%> instead.");
17631 /* Consume the `throw'. */
17632 cp_lexer_consume_token (parser->lexer);
17634 /* Look for the `('. */
17635 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17637 /* Peek at the next token. */
17638 token = cp_lexer_peek_token (parser->lexer);
17639 /* If it's not a `)', then there is a type-id-list. */
17640 if (token->type != CPP_CLOSE_PAREN)
17642 /* Types may not be defined in an exception-specification. */
17643 saved_message = parser->type_definition_forbidden_message;
17644 parser->type_definition_forbidden_message
17645 = G_("types may not be defined in an exception-specification");
17646 /* Parse the type-id-list. */
17647 type_id_list = cp_parser_type_id_list (parser);
17648 /* Restore the saved message. */
17649 parser->type_definition_forbidden_message = saved_message;
17652 type_id_list = empty_except_spec;
17654 /* Look for the `)'. */
17655 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17657 return type_id_list;
17660 /* Parse an (optional) type-id-list.
17664 type-id-list , type-id ... [opt]
17666 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17667 in the order that the types were presented. */
17670 cp_parser_type_id_list (cp_parser* parser)
17672 tree types = NULL_TREE;
17679 /* Get the next type-id. */
17680 type = cp_parser_type_id (parser);
17681 /* Parse the optional ellipsis. */
17682 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17684 /* Consume the `...'. */
17685 cp_lexer_consume_token (parser->lexer);
17687 /* Turn the type into a pack expansion expression. */
17688 type = make_pack_expansion (type);
17690 /* Add it to the list. */
17691 types = add_exception_specifier (types, type, /*complain=*/1);
17692 /* Peek at the next token. */
17693 token = cp_lexer_peek_token (parser->lexer);
17694 /* If it is not a `,', we are done. */
17695 if (token->type != CPP_COMMA)
17697 /* Consume the `,'. */
17698 cp_lexer_consume_token (parser->lexer);
17701 return nreverse (types);
17704 /* Parse a try-block.
17707 try compound-statement handler-seq */
17710 cp_parser_try_block (cp_parser* parser)
17714 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
17715 try_block = begin_try_block ();
17716 cp_parser_compound_statement (parser, NULL, true);
17717 finish_try_block (try_block);
17718 cp_parser_handler_seq (parser);
17719 finish_handler_sequence (try_block);
17724 /* Parse a function-try-block.
17726 function-try-block:
17727 try ctor-initializer [opt] function-body handler-seq */
17730 cp_parser_function_try_block (cp_parser* parser)
17732 tree compound_stmt;
17734 bool ctor_initializer_p;
17736 /* Look for the `try' keyword. */
17737 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
17739 /* Let the rest of the front end know where we are. */
17740 try_block = begin_function_try_block (&compound_stmt);
17741 /* Parse the function-body. */
17743 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17744 /* We're done with the `try' part. */
17745 finish_function_try_block (try_block);
17746 /* Parse the handlers. */
17747 cp_parser_handler_seq (parser);
17748 /* We're done with the handlers. */
17749 finish_function_handler_sequence (try_block, compound_stmt);
17751 return ctor_initializer_p;
17754 /* Parse a handler-seq.
17757 handler handler-seq [opt] */
17760 cp_parser_handler_seq (cp_parser* parser)
17766 /* Parse the handler. */
17767 cp_parser_handler (parser);
17768 /* Peek at the next token. */
17769 token = cp_lexer_peek_token (parser->lexer);
17770 /* If it's not `catch' then there are no more handlers. */
17771 if (!cp_parser_is_keyword (token, RID_CATCH))
17776 /* Parse a handler.
17779 catch ( exception-declaration ) compound-statement */
17782 cp_parser_handler (cp_parser* parser)
17787 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
17788 handler = begin_handler ();
17789 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17790 declaration = cp_parser_exception_declaration (parser);
17791 finish_handler_parms (declaration, handler);
17792 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17793 cp_parser_compound_statement (parser, NULL, false);
17794 finish_handler (handler);
17797 /* Parse an exception-declaration.
17799 exception-declaration:
17800 type-specifier-seq declarator
17801 type-specifier-seq abstract-declarator
17805 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17806 ellipsis variant is used. */
17809 cp_parser_exception_declaration (cp_parser* parser)
17811 cp_decl_specifier_seq type_specifiers;
17812 cp_declarator *declarator;
17813 const char *saved_message;
17815 /* If it's an ellipsis, it's easy to handle. */
17816 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17818 /* Consume the `...' token. */
17819 cp_lexer_consume_token (parser->lexer);
17823 /* Types may not be defined in exception-declarations. */
17824 saved_message = parser->type_definition_forbidden_message;
17825 parser->type_definition_forbidden_message
17826 = G_("types may not be defined in exception-declarations");
17828 /* Parse the type-specifier-seq. */
17829 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17830 /*is_trailing_return=*/false,
17832 /* If it's a `)', then there is no declarator. */
17833 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17836 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17837 /*ctor_dtor_or_conv_p=*/NULL,
17838 /*parenthesized_p=*/NULL,
17839 /*member_p=*/false);
17841 /* Restore the saved message. */
17842 parser->type_definition_forbidden_message = saved_message;
17844 if (!type_specifiers.any_specifiers_p)
17845 return error_mark_node;
17847 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17850 /* Parse a throw-expression.
17853 throw assignment-expression [opt]
17855 Returns a THROW_EXPR representing the throw-expression. */
17858 cp_parser_throw_expression (cp_parser* parser)
17863 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
17864 token = cp_lexer_peek_token (parser->lexer);
17865 /* Figure out whether or not there is an assignment-expression
17866 following the "throw" keyword. */
17867 if (token->type == CPP_COMMA
17868 || token->type == CPP_SEMICOLON
17869 || token->type == CPP_CLOSE_PAREN
17870 || token->type == CPP_CLOSE_SQUARE
17871 || token->type == CPP_CLOSE_BRACE
17872 || token->type == CPP_COLON)
17873 expression = NULL_TREE;
17875 expression = cp_parser_assignment_expression (parser,
17876 /*cast_p=*/false, NULL);
17878 return build_throw (expression);
17881 /* GNU Extensions */
17883 /* Parse an (optional) asm-specification.
17886 asm ( string-literal )
17888 If the asm-specification is present, returns a STRING_CST
17889 corresponding to the string-literal. Otherwise, returns
17893 cp_parser_asm_specification_opt (cp_parser* parser)
17896 tree asm_specification;
17898 /* Peek at the next token. */
17899 token = cp_lexer_peek_token (parser->lexer);
17900 /* If the next token isn't the `asm' keyword, then there's no
17901 asm-specification. */
17902 if (!cp_parser_is_keyword (token, RID_ASM))
17905 /* Consume the `asm' token. */
17906 cp_lexer_consume_token (parser->lexer);
17907 /* Look for the `('. */
17908 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17910 /* Look for the string-literal. */
17911 asm_specification = cp_parser_string_literal (parser, false, false);
17913 /* Look for the `)'. */
17914 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17916 return asm_specification;
17919 /* Parse an asm-operand-list.
17923 asm-operand-list , asm-operand
17926 string-literal ( expression )
17927 [ string-literal ] string-literal ( expression )
17929 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17930 each node is the expression. The TREE_PURPOSE is itself a
17931 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17932 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17933 is a STRING_CST for the string literal before the parenthesis. Returns
17934 ERROR_MARK_NODE if any of the operands are invalid. */
17937 cp_parser_asm_operand_list (cp_parser* parser)
17939 tree asm_operands = NULL_TREE;
17940 bool invalid_operands = false;
17944 tree string_literal;
17948 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17950 /* Consume the `[' token. */
17951 cp_lexer_consume_token (parser->lexer);
17952 /* Read the operand name. */
17953 name = cp_parser_identifier (parser);
17954 if (name != error_mark_node)
17955 name = build_string (IDENTIFIER_LENGTH (name),
17956 IDENTIFIER_POINTER (name));
17957 /* Look for the closing `]'. */
17958 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17962 /* Look for the string-literal. */
17963 string_literal = cp_parser_string_literal (parser, false, false);
17965 /* Look for the `('. */
17966 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17967 /* Parse the expression. */
17968 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17969 /* Look for the `)'. */
17970 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17972 if (name == error_mark_node
17973 || string_literal == error_mark_node
17974 || expression == error_mark_node)
17975 invalid_operands = true;
17977 /* Add this operand to the list. */
17978 asm_operands = tree_cons (build_tree_list (name, string_literal),
17981 /* If the next token is not a `,', there are no more
17983 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17985 /* Consume the `,'. */
17986 cp_lexer_consume_token (parser->lexer);
17989 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17992 /* Parse an asm-clobber-list.
17996 asm-clobber-list , string-literal
17998 Returns a TREE_LIST, indicating the clobbers in the order that they
17999 appeared. The TREE_VALUE of each node is a STRING_CST. */
18002 cp_parser_asm_clobber_list (cp_parser* parser)
18004 tree clobbers = NULL_TREE;
18008 tree string_literal;
18010 /* Look for the string literal. */
18011 string_literal = cp_parser_string_literal (parser, false, false);
18012 /* Add it to the list. */
18013 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18014 /* If the next token is not a `,', then the list is
18016 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18018 /* Consume the `,' token. */
18019 cp_lexer_consume_token (parser->lexer);
18025 /* Parse an asm-label-list.
18029 asm-label-list , identifier
18031 Returns a TREE_LIST, indicating the labels in the order that they
18032 appeared. The TREE_VALUE of each node is a label. */
18035 cp_parser_asm_label_list (cp_parser* parser)
18037 tree labels = NULL_TREE;
18041 tree identifier, label, name;
18043 /* Look for the identifier. */
18044 identifier = cp_parser_identifier (parser);
18045 if (!error_operand_p (identifier))
18047 label = lookup_label (identifier);
18048 if (TREE_CODE (label) == LABEL_DECL)
18050 TREE_USED (label) = 1;
18051 check_goto (label);
18052 name = build_string (IDENTIFIER_LENGTH (identifier),
18053 IDENTIFIER_POINTER (identifier));
18054 labels = tree_cons (name, label, labels);
18057 /* If the next token is not a `,', then the list is
18059 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18061 /* Consume the `,' token. */
18062 cp_lexer_consume_token (parser->lexer);
18065 return nreverse (labels);
18068 /* Parse an (optional) series of attributes.
18071 attributes attribute
18074 __attribute__ (( attribute-list [opt] ))
18076 The return value is as for cp_parser_attribute_list. */
18079 cp_parser_attributes_opt (cp_parser* parser)
18081 tree attributes = NULL_TREE;
18086 tree attribute_list;
18088 /* Peek at the next token. */
18089 token = cp_lexer_peek_token (parser->lexer);
18090 /* If it's not `__attribute__', then we're done. */
18091 if (token->keyword != RID_ATTRIBUTE)
18094 /* Consume the `__attribute__' keyword. */
18095 cp_lexer_consume_token (parser->lexer);
18096 /* Look for the two `(' tokens. */
18097 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18098 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18100 /* Peek at the next token. */
18101 token = cp_lexer_peek_token (parser->lexer);
18102 if (token->type != CPP_CLOSE_PAREN)
18103 /* Parse the attribute-list. */
18104 attribute_list = cp_parser_attribute_list (parser);
18106 /* If the next token is a `)', then there is no attribute
18108 attribute_list = NULL;
18110 /* Look for the two `)' tokens. */
18111 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18112 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18114 /* Add these new attributes to the list. */
18115 attributes = chainon (attributes, attribute_list);
18121 /* Parse an attribute-list.
18125 attribute-list , attribute
18129 identifier ( identifier )
18130 identifier ( identifier , expression-list )
18131 identifier ( expression-list )
18133 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18134 to an attribute. The TREE_PURPOSE of each node is the identifier
18135 indicating which attribute is in use. The TREE_VALUE represents
18136 the arguments, if any. */
18139 cp_parser_attribute_list (cp_parser* parser)
18141 tree attribute_list = NULL_TREE;
18142 bool save_translate_strings_p = parser->translate_strings_p;
18144 parser->translate_strings_p = false;
18151 /* Look for the identifier. We also allow keywords here; for
18152 example `__attribute__ ((const))' is legal. */
18153 token = cp_lexer_peek_token (parser->lexer);
18154 if (token->type == CPP_NAME
18155 || token->type == CPP_KEYWORD)
18157 tree arguments = NULL_TREE;
18159 /* Consume the token. */
18160 token = cp_lexer_consume_token (parser->lexer);
18162 /* Save away the identifier that indicates which attribute
18164 identifier = (token->type == CPP_KEYWORD)
18165 /* For keywords, use the canonical spelling, not the
18166 parsed identifier. */
18167 ? ridpointers[(int) token->keyword]
18170 attribute = build_tree_list (identifier, NULL_TREE);
18172 /* Peek at the next token. */
18173 token = cp_lexer_peek_token (parser->lexer);
18174 /* If it's an `(', then parse the attribute arguments. */
18175 if (token->type == CPP_OPEN_PAREN)
18178 int attr_flag = (attribute_takes_identifier_p (identifier)
18179 ? id_attr : normal_attr);
18180 vec = cp_parser_parenthesized_expression_list
18181 (parser, attr_flag, /*cast_p=*/false,
18182 /*allow_expansion_p=*/false,
18183 /*non_constant_p=*/NULL);
18185 arguments = error_mark_node;
18188 arguments = build_tree_list_vec (vec);
18189 release_tree_vector (vec);
18191 /* Save the arguments away. */
18192 TREE_VALUE (attribute) = arguments;
18195 if (arguments != error_mark_node)
18197 /* Add this attribute to the list. */
18198 TREE_CHAIN (attribute) = attribute_list;
18199 attribute_list = attribute;
18202 token = cp_lexer_peek_token (parser->lexer);
18204 /* Now, look for more attributes. If the next token isn't a
18205 `,', we're done. */
18206 if (token->type != CPP_COMMA)
18209 /* Consume the comma and keep going. */
18210 cp_lexer_consume_token (parser->lexer);
18212 parser->translate_strings_p = save_translate_strings_p;
18214 /* We built up the list in reverse order. */
18215 return nreverse (attribute_list);
18218 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18219 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18220 current value of the PEDANTIC flag, regardless of whether or not
18221 the `__extension__' keyword is present. The caller is responsible
18222 for restoring the value of the PEDANTIC flag. */
18225 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18227 /* Save the old value of the PEDANTIC flag. */
18228 *saved_pedantic = pedantic;
18230 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18232 /* Consume the `__extension__' token. */
18233 cp_lexer_consume_token (parser->lexer);
18234 /* We're not being pedantic while the `__extension__' keyword is
18244 /* Parse a label declaration.
18247 __label__ label-declarator-seq ;
18249 label-declarator-seq:
18250 identifier , label-declarator-seq
18254 cp_parser_label_declaration (cp_parser* parser)
18256 /* Look for the `__label__' keyword. */
18257 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18263 /* Look for an identifier. */
18264 identifier = cp_parser_identifier (parser);
18265 /* If we failed, stop. */
18266 if (identifier == error_mark_node)
18268 /* Declare it as a label. */
18269 finish_label_decl (identifier);
18270 /* If the next token is a `;', stop. */
18271 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18273 /* Look for the `,' separating the label declarations. */
18274 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18277 /* Look for the final `;'. */
18278 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18281 /* Support Functions */
18283 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18284 NAME should have one of the representations used for an
18285 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18286 is returned. If PARSER->SCOPE is a dependent type, then a
18287 SCOPE_REF is returned.
18289 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18290 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18291 was formed. Abstractly, such entities should not be passed to this
18292 function, because they do not need to be looked up, but it is
18293 simpler to check for this special case here, rather than at the
18296 In cases not explicitly covered above, this function returns a
18297 DECL, OVERLOAD, or baselink representing the result of the lookup.
18298 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18301 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18302 (e.g., "struct") that was used. In that case bindings that do not
18303 refer to types are ignored.
18305 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18308 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18311 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18314 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18315 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18316 NULL_TREE otherwise. */
18319 cp_parser_lookup_name (cp_parser *parser, tree name,
18320 enum tag_types tag_type,
18323 bool check_dependency,
18324 tree *ambiguous_decls,
18325 location_t name_location)
18329 tree object_type = parser->context->object_type;
18331 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18332 flags |= LOOKUP_COMPLAIN;
18334 /* Assume that the lookup will be unambiguous. */
18335 if (ambiguous_decls)
18336 *ambiguous_decls = NULL_TREE;
18338 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18339 no longer valid. Note that if we are parsing tentatively, and
18340 the parse fails, OBJECT_TYPE will be automatically restored. */
18341 parser->context->object_type = NULL_TREE;
18343 if (name == error_mark_node)
18344 return error_mark_node;
18346 /* A template-id has already been resolved; there is no lookup to
18348 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18350 if (BASELINK_P (name))
18352 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
18353 == TEMPLATE_ID_EXPR);
18357 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
18358 it should already have been checked to make sure that the name
18359 used matches the type being destroyed. */
18360 if (TREE_CODE (name) == BIT_NOT_EXPR)
18364 /* Figure out to which type this destructor applies. */
18366 type = parser->scope;
18367 else if (object_type)
18368 type = object_type;
18370 type = current_class_type;
18371 /* If that's not a class type, there is no destructor. */
18372 if (!type || !CLASS_TYPE_P (type))
18373 return error_mark_node;
18374 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
18375 lazily_declare_fn (sfk_destructor, type);
18376 if (!CLASSTYPE_DESTRUCTORS (type))
18377 return error_mark_node;
18378 /* If it was a class type, return the destructor. */
18379 return CLASSTYPE_DESTRUCTORS (type);
18382 /* By this point, the NAME should be an ordinary identifier. If
18383 the id-expression was a qualified name, the qualifying scope is
18384 stored in PARSER->SCOPE at this point. */
18385 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
18387 /* Perform the lookup. */
18392 if (parser->scope == error_mark_node)
18393 return error_mark_node;
18395 /* If the SCOPE is dependent, the lookup must be deferred until
18396 the template is instantiated -- unless we are explicitly
18397 looking up names in uninstantiated templates. Even then, we
18398 cannot look up the name if the scope is not a class type; it
18399 might, for example, be a template type parameter. */
18400 dependent_p = (TYPE_P (parser->scope)
18401 && dependent_scope_p (parser->scope));
18402 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18404 /* Defer lookup. */
18405 decl = error_mark_node;
18408 tree pushed_scope = NULL_TREE;
18410 /* If PARSER->SCOPE is a dependent type, then it must be a
18411 class type, and we must not be checking dependencies;
18412 otherwise, we would have processed this lookup above. So
18413 that PARSER->SCOPE is not considered a dependent base by
18414 lookup_member, we must enter the scope here. */
18416 pushed_scope = push_scope (parser->scope);
18418 /* If the PARSER->SCOPE is a template specialization, it
18419 may be instantiated during name lookup. In that case,
18420 errors may be issued. Even if we rollback the current
18421 tentative parse, those errors are valid. */
18422 decl = lookup_qualified_name (parser->scope, name,
18423 tag_type != none_type,
18424 /*complain=*/true);
18426 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18427 lookup result and the nested-name-specifier nominates a class C:
18428 * if the name specified after the nested-name-specifier, when
18429 looked up in C, is the injected-class-name of C (Clause 9), or
18430 * if the name specified after the nested-name-specifier is the
18431 same as the identifier or the simple-template-id's template-
18432 name in the last component of the nested-name-specifier,
18433 the name is instead considered to name the constructor of
18434 class C. [ Note: for example, the constructor is not an
18435 acceptable lookup result in an elaborated-type-specifier so
18436 the constructor would not be used in place of the
18437 injected-class-name. --end note ] Such a constructor name
18438 shall be used only in the declarator-id of a declaration that
18439 names a constructor or in a using-declaration. */
18440 if (tag_type == none_type
18441 && DECL_SELF_REFERENCE_P (decl)
18442 && same_type_p (DECL_CONTEXT (decl), parser->scope))
18443 decl = lookup_qualified_name (parser->scope, ctor_identifier,
18444 tag_type != none_type,
18445 /*complain=*/true);
18447 /* If we have a single function from a using decl, pull it out. */
18448 if (TREE_CODE (decl) == OVERLOAD
18449 && !really_overloaded_fn (decl))
18450 decl = OVL_FUNCTION (decl);
18453 pop_scope (pushed_scope);
18456 /* If the scope is a dependent type and either we deferred lookup or
18457 we did lookup but didn't find the name, rememeber the name. */
18458 if (decl == error_mark_node && TYPE_P (parser->scope)
18459 && dependent_type_p (parser->scope))
18465 /* The resolution to Core Issue 180 says that `struct
18466 A::B' should be considered a type-name, even if `A'
18468 type = make_typename_type (parser->scope, name, tag_type,
18469 /*complain=*/tf_error);
18470 decl = TYPE_NAME (type);
18472 else if (is_template
18473 && (cp_parser_next_token_ends_template_argument_p (parser)
18474 || cp_lexer_next_token_is (parser->lexer,
18476 decl = make_unbound_class_template (parser->scope,
18478 /*complain=*/tf_error);
18480 decl = build_qualified_name (/*type=*/NULL_TREE,
18481 parser->scope, name,
18484 parser->qualifying_scope = parser->scope;
18485 parser->object_scope = NULL_TREE;
18487 else if (object_type)
18489 tree object_decl = NULL_TREE;
18490 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18491 OBJECT_TYPE is not a class. */
18492 if (CLASS_TYPE_P (object_type))
18493 /* If the OBJECT_TYPE is a template specialization, it may
18494 be instantiated during name lookup. In that case, errors
18495 may be issued. Even if we rollback the current tentative
18496 parse, those errors are valid. */
18497 object_decl = lookup_member (object_type,
18500 tag_type != none_type);
18501 /* Look it up in the enclosing context, too. */
18502 decl = lookup_name_real (name, tag_type != none_type,
18504 /*block_p=*/true, is_namespace, flags);
18505 parser->object_scope = object_type;
18506 parser->qualifying_scope = NULL_TREE;
18508 decl = object_decl;
18512 decl = lookup_name_real (name, tag_type != none_type,
18514 /*block_p=*/true, is_namespace, flags);
18515 parser->qualifying_scope = NULL_TREE;
18516 parser->object_scope = NULL_TREE;
18519 /* If the lookup failed, let our caller know. */
18520 if (!decl || decl == error_mark_node)
18521 return error_mark_node;
18523 /* Pull out the template from an injected-class-name (or multiple). */
18525 decl = maybe_get_template_decl_from_type_decl (decl);
18527 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18528 if (TREE_CODE (decl) == TREE_LIST)
18530 if (ambiguous_decls)
18531 *ambiguous_decls = decl;
18532 /* The error message we have to print is too complicated for
18533 cp_parser_error, so we incorporate its actions directly. */
18534 if (!cp_parser_simulate_error (parser))
18536 error_at (name_location, "reference to %qD is ambiguous",
18538 print_candidates (decl);
18540 return error_mark_node;
18543 gcc_assert (DECL_P (decl)
18544 || TREE_CODE (decl) == OVERLOAD
18545 || TREE_CODE (decl) == SCOPE_REF
18546 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18547 || BASELINK_P (decl));
18549 /* If we have resolved the name of a member declaration, check to
18550 see if the declaration is accessible. When the name resolves to
18551 set of overloaded functions, accessibility is checked when
18552 overload resolution is done.
18554 During an explicit instantiation, access is not checked at all,
18555 as per [temp.explicit]. */
18557 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18562 /* Like cp_parser_lookup_name, but for use in the typical case where
18563 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18564 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18567 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18569 return cp_parser_lookup_name (parser, name,
18571 /*is_template=*/false,
18572 /*is_namespace=*/false,
18573 /*check_dependency=*/true,
18574 /*ambiguous_decls=*/NULL,
18578 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18579 the current context, return the TYPE_DECL. If TAG_NAME_P is
18580 true, the DECL indicates the class being defined in a class-head,
18581 or declared in an elaborated-type-specifier.
18583 Otherwise, return DECL. */
18586 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18588 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18589 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18592 template <typename T> struct B;
18595 template <typename T> struct A::B {};
18597 Similarly, in an elaborated-type-specifier:
18599 namespace N { struct X{}; }
18602 template <typename T> friend struct N::X;
18605 However, if the DECL refers to a class type, and we are in
18606 the scope of the class, then the name lookup automatically
18607 finds the TYPE_DECL created by build_self_reference rather
18608 than a TEMPLATE_DECL. For example, in:
18610 template <class T> struct S {
18614 there is no need to handle such case. */
18616 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18617 return DECL_TEMPLATE_RESULT (decl);
18622 /* If too many, or too few, template-parameter lists apply to the
18623 declarator, issue an error message. Returns TRUE if all went well,
18624 and FALSE otherwise. */
18627 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18628 cp_declarator *declarator,
18629 location_t declarator_location)
18631 unsigned num_templates;
18633 /* We haven't seen any classes that involve template parameters yet. */
18636 switch (declarator->kind)
18639 if (declarator->u.id.qualifying_scope)
18643 scope = declarator->u.id.qualifying_scope;
18645 while (scope && CLASS_TYPE_P (scope))
18647 /* You're supposed to have one `template <...>'
18648 for every template class, but you don't need one
18649 for a full specialization. For example:
18651 template <class T> struct S{};
18652 template <> struct S<int> { void f(); };
18653 void S<int>::f () {}
18655 is correct; there shouldn't be a `template <>' for
18656 the definition of `S<int>::f'. */
18657 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18658 /* If SCOPE does not have template information of any
18659 kind, then it is not a template, nor is it nested
18660 within a template. */
18662 if (explicit_class_specialization_p (scope))
18664 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18667 scope = TYPE_CONTEXT (scope);
18670 else if (TREE_CODE (declarator->u.id.unqualified_name)
18671 == TEMPLATE_ID_EXPR)
18672 /* If the DECLARATOR has the form `X<y>' then it uses one
18673 additional level of template parameters. */
18676 return cp_parser_check_template_parameters
18677 (parser, num_templates, declarator_location, declarator);
18683 case cdk_reference:
18685 return (cp_parser_check_declarator_template_parameters
18686 (parser, declarator->declarator, declarator_location));
18692 gcc_unreachable ();
18697 /* NUM_TEMPLATES were used in the current declaration. If that is
18698 invalid, return FALSE and issue an error messages. Otherwise,
18699 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18700 declarator and we can print more accurate diagnostics. */
18703 cp_parser_check_template_parameters (cp_parser* parser,
18704 unsigned num_templates,
18705 location_t location,
18706 cp_declarator *declarator)
18708 /* If there are the same number of template classes and parameter
18709 lists, that's OK. */
18710 if (parser->num_template_parameter_lists == num_templates)
18712 /* If there are more, but only one more, then we are referring to a
18713 member template. That's OK too. */
18714 if (parser->num_template_parameter_lists == num_templates + 1)
18716 /* If there are more template classes than parameter lists, we have
18719 template <class T> void S<T>::R<T>::f (); */
18720 if (parser->num_template_parameter_lists < num_templates)
18722 if (declarator && !current_function_decl)
18723 error_at (location, "specializing member %<%T::%E%> "
18724 "requires %<template<>%> syntax",
18725 declarator->u.id.qualifying_scope,
18726 declarator->u.id.unqualified_name);
18727 else if (declarator)
18728 error_at (location, "invalid declaration of %<%T::%E%>",
18729 declarator->u.id.qualifying_scope,
18730 declarator->u.id.unqualified_name);
18732 error_at (location, "too few template-parameter-lists");
18735 /* Otherwise, there are too many template parameter lists. We have
18738 template <class T> template <class U> void S::f(); */
18739 error_at (location, "too many template-parameter-lists");
18743 /* Parse an optional `::' token indicating that the following name is
18744 from the global namespace. If so, PARSER->SCOPE is set to the
18745 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18746 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18747 Returns the new value of PARSER->SCOPE, if the `::' token is
18748 present, and NULL_TREE otherwise. */
18751 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18755 /* Peek at the next token. */
18756 token = cp_lexer_peek_token (parser->lexer);
18757 /* If we're looking at a `::' token then we're starting from the
18758 global namespace, not our current location. */
18759 if (token->type == CPP_SCOPE)
18761 /* Consume the `::' token. */
18762 cp_lexer_consume_token (parser->lexer);
18763 /* Set the SCOPE so that we know where to start the lookup. */
18764 parser->scope = global_namespace;
18765 parser->qualifying_scope = global_namespace;
18766 parser->object_scope = NULL_TREE;
18768 return parser->scope;
18770 else if (!current_scope_valid_p)
18772 parser->scope = NULL_TREE;
18773 parser->qualifying_scope = NULL_TREE;
18774 parser->object_scope = NULL_TREE;
18780 /* Returns TRUE if the upcoming token sequence is the start of a
18781 constructor declarator. If FRIEND_P is true, the declarator is
18782 preceded by the `friend' specifier. */
18785 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18787 bool constructor_p;
18788 tree nested_name_specifier;
18789 cp_token *next_token;
18791 /* The common case is that this is not a constructor declarator, so
18792 try to avoid doing lots of work if at all possible. It's not
18793 valid declare a constructor at function scope. */
18794 if (parser->in_function_body)
18796 /* And only certain tokens can begin a constructor declarator. */
18797 next_token = cp_lexer_peek_token (parser->lexer);
18798 if (next_token->type != CPP_NAME
18799 && next_token->type != CPP_SCOPE
18800 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18801 && next_token->type != CPP_TEMPLATE_ID)
18804 /* Parse tentatively; we are going to roll back all of the tokens
18806 cp_parser_parse_tentatively (parser);
18807 /* Assume that we are looking at a constructor declarator. */
18808 constructor_p = true;
18810 /* Look for the optional `::' operator. */
18811 cp_parser_global_scope_opt (parser,
18812 /*current_scope_valid_p=*/false);
18813 /* Look for the nested-name-specifier. */
18814 nested_name_specifier
18815 = (cp_parser_nested_name_specifier_opt (parser,
18816 /*typename_keyword_p=*/false,
18817 /*check_dependency_p=*/false,
18819 /*is_declaration=*/false));
18820 /* Outside of a class-specifier, there must be a
18821 nested-name-specifier. */
18822 if (!nested_name_specifier &&
18823 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18825 constructor_p = false;
18826 else if (nested_name_specifier == error_mark_node)
18827 constructor_p = false;
18829 /* If we have a class scope, this is easy; DR 147 says that S::S always
18830 names the constructor, and no other qualified name could. */
18831 if (constructor_p && nested_name_specifier
18832 && TYPE_P (nested_name_specifier))
18834 tree id = cp_parser_unqualified_id (parser,
18835 /*template_keyword_p=*/false,
18836 /*check_dependency_p=*/false,
18837 /*declarator_p=*/true,
18838 /*optional_p=*/false);
18839 if (is_overloaded_fn (id))
18840 id = DECL_NAME (get_first_fn (id));
18841 if (!constructor_name_p (id, nested_name_specifier))
18842 constructor_p = false;
18844 /* If we still think that this might be a constructor-declarator,
18845 look for a class-name. */
18846 else if (constructor_p)
18850 template <typename T> struct S {
18854 we must recognize that the nested `S' names a class. */
18856 type_decl = cp_parser_class_name (parser,
18857 /*typename_keyword_p=*/false,
18858 /*template_keyword_p=*/false,
18860 /*check_dependency_p=*/false,
18861 /*class_head_p=*/false,
18862 /*is_declaration=*/false);
18863 /* If there was no class-name, then this is not a constructor. */
18864 constructor_p = !cp_parser_error_occurred (parser);
18866 /* If we're still considering a constructor, we have to see a `(',
18867 to begin the parameter-declaration-clause, followed by either a
18868 `)', an `...', or a decl-specifier. We need to check for a
18869 type-specifier to avoid being fooled into thinking that:
18873 is a constructor. (It is actually a function named `f' that
18874 takes one parameter (of type `int') and returns a value of type
18877 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18878 constructor_p = false;
18881 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18882 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18883 /* A parameter declaration begins with a decl-specifier,
18884 which is either the "attribute" keyword, a storage class
18885 specifier, or (usually) a type-specifier. */
18886 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18889 tree pushed_scope = NULL_TREE;
18890 unsigned saved_num_template_parameter_lists;
18892 /* Names appearing in the type-specifier should be looked up
18893 in the scope of the class. */
18894 if (current_class_type)
18898 type = TREE_TYPE (type_decl);
18899 if (TREE_CODE (type) == TYPENAME_TYPE)
18901 type = resolve_typename_type (type,
18902 /*only_current_p=*/false);
18903 if (TREE_CODE (type) == TYPENAME_TYPE)
18905 cp_parser_abort_tentative_parse (parser);
18909 pushed_scope = push_scope (type);
18912 /* Inside the constructor parameter list, surrounding
18913 template-parameter-lists do not apply. */
18914 saved_num_template_parameter_lists
18915 = parser->num_template_parameter_lists;
18916 parser->num_template_parameter_lists = 0;
18918 /* Look for the type-specifier. */
18919 cp_parser_type_specifier (parser,
18920 CP_PARSER_FLAGS_NONE,
18921 /*decl_specs=*/NULL,
18922 /*is_declarator=*/true,
18923 /*declares_class_or_enum=*/NULL,
18924 /*is_cv_qualifier=*/NULL);
18926 parser->num_template_parameter_lists
18927 = saved_num_template_parameter_lists;
18929 /* Leave the scope of the class. */
18931 pop_scope (pushed_scope);
18933 constructor_p = !cp_parser_error_occurred (parser);
18937 /* We did not really want to consume any tokens. */
18938 cp_parser_abort_tentative_parse (parser);
18940 return constructor_p;
18943 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18944 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18945 they must be performed once we are in the scope of the function.
18947 Returns the function defined. */
18950 cp_parser_function_definition_from_specifiers_and_declarator
18951 (cp_parser* parser,
18952 cp_decl_specifier_seq *decl_specifiers,
18954 const cp_declarator *declarator)
18959 /* Begin the function-definition. */
18960 success_p = start_function (decl_specifiers, declarator, attributes);
18962 /* The things we're about to see are not directly qualified by any
18963 template headers we've seen thus far. */
18964 reset_specialization ();
18966 /* If there were names looked up in the decl-specifier-seq that we
18967 did not check, check them now. We must wait until we are in the
18968 scope of the function to perform the checks, since the function
18969 might be a friend. */
18970 perform_deferred_access_checks ();
18974 /* Skip the entire function. */
18975 cp_parser_skip_to_end_of_block_or_statement (parser);
18976 fn = error_mark_node;
18978 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18980 /* Seen already, skip it. An error message has already been output. */
18981 cp_parser_skip_to_end_of_block_or_statement (parser);
18982 fn = current_function_decl;
18983 current_function_decl = NULL_TREE;
18984 /* If this is a function from a class, pop the nested class. */
18985 if (current_class_name)
18986 pop_nested_class ();
18989 fn = cp_parser_function_definition_after_declarator (parser,
18990 /*inline_p=*/false);
18995 /* Parse the part of a function-definition that follows the
18996 declarator. INLINE_P is TRUE iff this function is an inline
18997 function defined within a class-specifier.
18999 Returns the function defined. */
19002 cp_parser_function_definition_after_declarator (cp_parser* parser,
19006 bool ctor_initializer_p = false;
19007 bool saved_in_unbraced_linkage_specification_p;
19008 bool saved_in_function_body;
19009 unsigned saved_num_template_parameter_lists;
19012 saved_in_function_body = parser->in_function_body;
19013 parser->in_function_body = true;
19014 /* If the next token is `return', then the code may be trying to
19015 make use of the "named return value" extension that G++ used to
19017 token = cp_lexer_peek_token (parser->lexer);
19018 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19020 /* Consume the `return' keyword. */
19021 cp_lexer_consume_token (parser->lexer);
19022 /* Look for the identifier that indicates what value is to be
19024 cp_parser_identifier (parser);
19025 /* Issue an error message. */
19026 error_at (token->location,
19027 "named return values are no longer supported");
19028 /* Skip tokens until we reach the start of the function body. */
19031 cp_token *token = cp_lexer_peek_token (parser->lexer);
19032 if (token->type == CPP_OPEN_BRACE
19033 || token->type == CPP_EOF
19034 || token->type == CPP_PRAGMA_EOL)
19036 cp_lexer_consume_token (parser->lexer);
19039 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19040 anything declared inside `f'. */
19041 saved_in_unbraced_linkage_specification_p
19042 = parser->in_unbraced_linkage_specification_p;
19043 parser->in_unbraced_linkage_specification_p = false;
19044 /* Inside the function, surrounding template-parameter-lists do not
19046 saved_num_template_parameter_lists
19047 = parser->num_template_parameter_lists;
19048 parser->num_template_parameter_lists = 0;
19050 start_lambda_scope (current_function_decl);
19052 /* If the next token is `try', then we are looking at a
19053 function-try-block. */
19054 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19055 ctor_initializer_p = cp_parser_function_try_block (parser);
19056 /* A function-try-block includes the function-body, so we only do
19057 this next part if we're not processing a function-try-block. */
19060 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19062 finish_lambda_scope ();
19064 /* Finish the function. */
19065 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19066 (inline_p ? 2 : 0));
19067 /* Generate code for it, if necessary. */
19068 expand_or_defer_fn (fn);
19069 /* Restore the saved values. */
19070 parser->in_unbraced_linkage_specification_p
19071 = saved_in_unbraced_linkage_specification_p;
19072 parser->num_template_parameter_lists
19073 = saved_num_template_parameter_lists;
19074 parser->in_function_body = saved_in_function_body;
19079 /* Parse a template-declaration, assuming that the `export' (and
19080 `extern') keywords, if present, has already been scanned. MEMBER_P
19081 is as for cp_parser_template_declaration. */
19084 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19086 tree decl = NULL_TREE;
19087 VEC (deferred_access_check,gc) *checks;
19088 tree parameter_list;
19089 bool friend_p = false;
19090 bool need_lang_pop;
19093 /* Look for the `template' keyword. */
19094 token = cp_lexer_peek_token (parser->lexer);
19095 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19099 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19101 if (at_class_scope_p () && current_function_decl)
19103 /* 14.5.2.2 [temp.mem]
19105 A local class shall not have member templates. */
19106 error_at (token->location,
19107 "invalid declaration of member template in local class");
19108 cp_parser_skip_to_end_of_block_or_statement (parser);
19113 A template ... shall not have C linkage. */
19114 if (current_lang_name == lang_name_c)
19116 error_at (token->location, "template with C linkage");
19117 /* Give it C++ linkage to avoid confusing other parts of the
19119 push_lang_context (lang_name_cplusplus);
19120 need_lang_pop = true;
19123 need_lang_pop = false;
19125 /* We cannot perform access checks on the template parameter
19126 declarations until we know what is being declared, just as we
19127 cannot check the decl-specifier list. */
19128 push_deferring_access_checks (dk_deferred);
19130 /* If the next token is `>', then we have an invalid
19131 specialization. Rather than complain about an invalid template
19132 parameter, issue an error message here. */
19133 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19135 cp_parser_error (parser, "invalid explicit specialization");
19136 begin_specialization ();
19137 parameter_list = NULL_TREE;
19140 /* Parse the template parameters. */
19141 parameter_list = cp_parser_template_parameter_list (parser);
19143 /* Get the deferred access checks from the parameter list. These
19144 will be checked once we know what is being declared, as for a
19145 member template the checks must be performed in the scope of the
19146 class containing the member. */
19147 checks = get_deferred_access_checks ();
19149 /* Look for the `>'. */
19150 cp_parser_skip_to_end_of_template_parameter_list (parser);
19151 /* We just processed one more parameter list. */
19152 ++parser->num_template_parameter_lists;
19153 /* If the next token is `template', there are more template
19155 if (cp_lexer_next_token_is_keyword (parser->lexer,
19157 cp_parser_template_declaration_after_export (parser, member_p);
19160 /* There are no access checks when parsing a template, as we do not
19161 know if a specialization will be a friend. */
19162 push_deferring_access_checks (dk_no_check);
19163 token = cp_lexer_peek_token (parser->lexer);
19164 decl = cp_parser_single_declaration (parser,
19167 /*explicit_specialization_p=*/false,
19169 pop_deferring_access_checks ();
19171 /* If this is a member template declaration, let the front
19173 if (member_p && !friend_p && decl)
19175 if (TREE_CODE (decl) == TYPE_DECL)
19176 cp_parser_check_access_in_redeclaration (decl, token->location);
19178 decl = finish_member_template_decl (decl);
19180 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19181 make_friend_class (current_class_type, TREE_TYPE (decl),
19182 /*complain=*/true);
19184 /* We are done with the current parameter list. */
19185 --parser->num_template_parameter_lists;
19187 pop_deferring_access_checks ();
19190 finish_template_decl (parameter_list);
19192 /* Register member declarations. */
19193 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19194 finish_member_declaration (decl);
19195 /* For the erroneous case of a template with C linkage, we pushed an
19196 implicit C++ linkage scope; exit that scope now. */
19198 pop_lang_context ();
19199 /* If DECL is a function template, we must return to parse it later.
19200 (Even though there is no definition, there might be default
19201 arguments that need handling.) */
19202 if (member_p && decl
19203 && (TREE_CODE (decl) == FUNCTION_DECL
19204 || DECL_FUNCTION_TEMPLATE_P (decl)))
19205 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19208 /* Perform the deferred access checks from a template-parameter-list.
19209 CHECKS is a TREE_LIST of access checks, as returned by
19210 get_deferred_access_checks. */
19213 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19215 ++processing_template_parmlist;
19216 perform_access_checks (checks);
19217 --processing_template_parmlist;
19220 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19221 `function-definition' sequence. MEMBER_P is true, this declaration
19222 appears in a class scope.
19224 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19225 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19228 cp_parser_single_declaration (cp_parser* parser,
19229 VEC (deferred_access_check,gc)* checks,
19231 bool explicit_specialization_p,
19234 int declares_class_or_enum;
19235 tree decl = NULL_TREE;
19236 cp_decl_specifier_seq decl_specifiers;
19237 bool function_definition_p = false;
19238 cp_token *decl_spec_token_start;
19240 /* This function is only used when processing a template
19242 gcc_assert (innermost_scope_kind () == sk_template_parms
19243 || innermost_scope_kind () == sk_template_spec);
19245 /* Defer access checks until we know what is being declared. */
19246 push_deferring_access_checks (dk_deferred);
19248 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19250 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19251 cp_parser_decl_specifier_seq (parser,
19252 CP_PARSER_FLAGS_OPTIONAL,
19254 &declares_class_or_enum);
19256 *friend_p = cp_parser_friend_p (&decl_specifiers);
19258 /* There are no template typedefs. */
19259 if (decl_specifiers.specs[(int) ds_typedef])
19261 error_at (decl_spec_token_start->location,
19262 "template declaration of %<typedef%>");
19263 decl = error_mark_node;
19266 /* Gather up the access checks that occurred the
19267 decl-specifier-seq. */
19268 stop_deferring_access_checks ();
19270 /* Check for the declaration of a template class. */
19271 if (declares_class_or_enum)
19273 if (cp_parser_declares_only_class_p (parser))
19275 decl = shadow_tag (&decl_specifiers);
19280 friend template <typename T> struct A<T>::B;
19283 A<T>::B will be represented by a TYPENAME_TYPE, and
19284 therefore not recognized by shadow_tag. */
19285 if (friend_p && *friend_p
19287 && decl_specifiers.type
19288 && TYPE_P (decl_specifiers.type))
19289 decl = decl_specifiers.type;
19291 if (decl && decl != error_mark_node)
19292 decl = TYPE_NAME (decl);
19294 decl = error_mark_node;
19296 /* Perform access checks for template parameters. */
19297 cp_parser_perform_template_parameter_access_checks (checks);
19301 /* Complain about missing 'typename' or other invalid type names. */
19302 if (!decl_specifiers.any_type_specifiers_p)
19303 cp_parser_parse_and_diagnose_invalid_type_name (parser);
19305 /* If it's not a template class, try for a template function. If
19306 the next token is a `;', then this declaration does not declare
19307 anything. But, if there were errors in the decl-specifiers, then
19308 the error might well have come from an attempted class-specifier.
19309 In that case, there's no need to warn about a missing declarator. */
19311 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19312 || decl_specifiers.type != error_mark_node))
19314 decl = cp_parser_init_declarator (parser,
19317 /*function_definition_allowed_p=*/true,
19319 declares_class_or_enum,
19320 &function_definition_p);
19322 /* 7.1.1-1 [dcl.stc]
19324 A storage-class-specifier shall not be specified in an explicit
19325 specialization... */
19327 && explicit_specialization_p
19328 && decl_specifiers.storage_class != sc_none)
19330 error_at (decl_spec_token_start->location,
19331 "explicit template specialization cannot have a storage class");
19332 decl = error_mark_node;
19336 pop_deferring_access_checks ();
19338 /* Clear any current qualification; whatever comes next is the start
19339 of something new. */
19340 parser->scope = NULL_TREE;
19341 parser->qualifying_scope = NULL_TREE;
19342 parser->object_scope = NULL_TREE;
19343 /* Look for a trailing `;' after the declaration. */
19344 if (!function_definition_p
19345 && (decl == error_mark_node
19346 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19347 cp_parser_skip_to_end_of_block_or_statement (parser);
19352 /* Parse a cast-expression that is not the operand of a unary "&". */
19355 cp_parser_simple_cast_expression (cp_parser *parser)
19357 return cp_parser_cast_expression (parser, /*address_p=*/false,
19358 /*cast_p=*/false, NULL);
19361 /* Parse a functional cast to TYPE. Returns an expression
19362 representing the cast. */
19365 cp_parser_functional_cast (cp_parser* parser, tree type)
19368 tree expression_list;
19372 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19374 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19375 expression_list = cp_parser_braced_list (parser, &nonconst_p);
19376 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
19377 if (TREE_CODE (type) == TYPE_DECL)
19378 type = TREE_TYPE (type);
19379 return finish_compound_literal (type, expression_list);
19383 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19385 /*allow_expansion_p=*/true,
19386 /*non_constant_p=*/NULL);
19388 expression_list = error_mark_node;
19391 expression_list = build_tree_list_vec (vec);
19392 release_tree_vector (vec);
19395 cast = build_functional_cast (type, expression_list,
19396 tf_warning_or_error);
19397 /* [expr.const]/1: In an integral constant expression "only type
19398 conversions to integral or enumeration type can be used". */
19399 if (TREE_CODE (type) == TYPE_DECL)
19400 type = TREE_TYPE (type);
19401 if (cast != error_mark_node
19402 && !cast_valid_in_integral_constant_expression_p (type)
19403 && cp_parser_non_integral_constant_expression (parser,
19405 return error_mark_node;
19409 /* Save the tokens that make up the body of a member function defined
19410 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19411 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19412 specifiers applied to the declaration. Returns the FUNCTION_DECL
19413 for the member function. */
19416 cp_parser_save_member_function_body (cp_parser* parser,
19417 cp_decl_specifier_seq *decl_specifiers,
19418 cp_declarator *declarator,
19425 /* Create the FUNCTION_DECL. */
19426 fn = grokmethod (decl_specifiers, declarator, attributes);
19427 /* If something went badly wrong, bail out now. */
19428 if (fn == error_mark_node)
19430 /* If there's a function-body, skip it. */
19431 if (cp_parser_token_starts_function_definition_p
19432 (cp_lexer_peek_token (parser->lexer)))
19433 cp_parser_skip_to_end_of_block_or_statement (parser);
19434 return error_mark_node;
19437 /* Remember it, if there default args to post process. */
19438 cp_parser_save_default_args (parser, fn);
19440 /* Save away the tokens that make up the body of the
19442 first = parser->lexer->next_token;
19443 /* We can have braced-init-list mem-initializers before the fn body. */
19444 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19446 cp_lexer_consume_token (parser->lexer);
19447 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19448 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19450 /* cache_group will stop after an un-nested { } pair, too. */
19451 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19454 /* variadic mem-inits have ... after the ')'. */
19455 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19456 cp_lexer_consume_token (parser->lexer);
19459 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19460 /* Handle function try blocks. */
19461 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19462 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19463 last = parser->lexer->next_token;
19465 /* Save away the inline definition; we will process it when the
19466 class is complete. */
19467 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19468 DECL_PENDING_INLINE_P (fn) = 1;
19470 /* We need to know that this was defined in the class, so that
19471 friend templates are handled correctly. */
19472 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19474 /* Add FN to the queue of functions to be parsed later. */
19475 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
19480 /* Parse a template-argument-list, as well as the trailing ">" (but
19481 not the opening ">"). See cp_parser_template_argument_list for the
19485 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19489 tree saved_qualifying_scope;
19490 tree saved_object_scope;
19491 bool saved_greater_than_is_operator_p;
19492 int saved_unevaluated_operand;
19493 int saved_inhibit_evaluation_warnings;
19497 When parsing a template-id, the first non-nested `>' is taken as
19498 the end of the template-argument-list rather than a greater-than
19500 saved_greater_than_is_operator_p
19501 = parser->greater_than_is_operator_p;
19502 parser->greater_than_is_operator_p = false;
19503 /* Parsing the argument list may modify SCOPE, so we save it
19505 saved_scope = parser->scope;
19506 saved_qualifying_scope = parser->qualifying_scope;
19507 saved_object_scope = parser->object_scope;
19508 /* We need to evaluate the template arguments, even though this
19509 template-id may be nested within a "sizeof". */
19510 saved_unevaluated_operand = cp_unevaluated_operand;
19511 cp_unevaluated_operand = 0;
19512 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19513 c_inhibit_evaluation_warnings = 0;
19514 /* Parse the template-argument-list itself. */
19515 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19516 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19517 arguments = NULL_TREE;
19519 arguments = cp_parser_template_argument_list (parser);
19520 /* Look for the `>' that ends the template-argument-list. If we find
19521 a '>>' instead, it's probably just a typo. */
19522 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19524 if (cxx_dialect != cxx98)
19526 /* In C++0x, a `>>' in a template argument list or cast
19527 expression is considered to be two separate `>'
19528 tokens. So, change the current token to a `>', but don't
19529 consume it: it will be consumed later when the outer
19530 template argument list (or cast expression) is parsed.
19531 Note that this replacement of `>' for `>>' is necessary
19532 even if we are parsing tentatively: in the tentative
19533 case, after calling
19534 cp_parser_enclosed_template_argument_list we will always
19535 throw away all of the template arguments and the first
19536 closing `>', either because the template argument list
19537 was erroneous or because we are replacing those tokens
19538 with a CPP_TEMPLATE_ID token. The second `>' (which will
19539 not have been thrown away) is needed either to close an
19540 outer template argument list or to complete a new-style
19542 cp_token *token = cp_lexer_peek_token (parser->lexer);
19543 token->type = CPP_GREATER;
19545 else if (!saved_greater_than_is_operator_p)
19547 /* If we're in a nested template argument list, the '>>' has
19548 to be a typo for '> >'. We emit the error message, but we
19549 continue parsing and we push a '>' as next token, so that
19550 the argument list will be parsed correctly. Note that the
19551 global source location is still on the token before the
19552 '>>', so we need to say explicitly where we want it. */
19553 cp_token *token = cp_lexer_peek_token (parser->lexer);
19554 error_at (token->location, "%<>>%> should be %<> >%> "
19555 "within a nested template argument list");
19557 token->type = CPP_GREATER;
19561 /* If this is not a nested template argument list, the '>>'
19562 is a typo for '>'. Emit an error message and continue.
19563 Same deal about the token location, but here we can get it
19564 right by consuming the '>>' before issuing the diagnostic. */
19565 cp_token *token = cp_lexer_consume_token (parser->lexer);
19566 error_at (token->location,
19567 "spurious %<>>%>, use %<>%> to terminate "
19568 "a template argument list");
19572 cp_parser_skip_to_end_of_template_parameter_list (parser);
19573 /* The `>' token might be a greater-than operator again now. */
19574 parser->greater_than_is_operator_p
19575 = saved_greater_than_is_operator_p;
19576 /* Restore the SAVED_SCOPE. */
19577 parser->scope = saved_scope;
19578 parser->qualifying_scope = saved_qualifying_scope;
19579 parser->object_scope = saved_object_scope;
19580 cp_unevaluated_operand = saved_unevaluated_operand;
19581 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19586 /* MEMBER_FUNCTION is a member function, or a friend. If default
19587 arguments, or the body of the function have not yet been parsed,
19591 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19593 /* If this member is a template, get the underlying
19595 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19596 member_function = DECL_TEMPLATE_RESULT (member_function);
19598 /* There should not be any class definitions in progress at this
19599 point; the bodies of members are only parsed outside of all class
19601 gcc_assert (parser->num_classes_being_defined == 0);
19602 /* While we're parsing the member functions we might encounter more
19603 classes. We want to handle them right away, but we don't want
19604 them getting mixed up with functions that are currently in the
19606 push_unparsed_function_queues (parser);
19608 /* Make sure that any template parameters are in scope. */
19609 maybe_begin_member_template_processing (member_function);
19611 /* If the body of the function has not yet been parsed, parse it
19613 if (DECL_PENDING_INLINE_P (member_function))
19615 tree function_scope;
19616 cp_token_cache *tokens;
19618 /* The function is no longer pending; we are processing it. */
19619 tokens = DECL_PENDING_INLINE_INFO (member_function);
19620 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19621 DECL_PENDING_INLINE_P (member_function) = 0;
19623 /* If this is a local class, enter the scope of the containing
19625 function_scope = current_function_decl;
19626 if (function_scope)
19627 push_function_context ();
19629 /* Push the body of the function onto the lexer stack. */
19630 cp_parser_push_lexer_for_tokens (parser, tokens);
19632 /* Let the front end know that we going to be defining this
19634 start_preparsed_function (member_function, NULL_TREE,
19635 SF_PRE_PARSED | SF_INCLASS_INLINE);
19637 /* Don't do access checking if it is a templated function. */
19638 if (processing_template_decl)
19639 push_deferring_access_checks (dk_no_check);
19641 /* Now, parse the body of the function. */
19642 cp_parser_function_definition_after_declarator (parser,
19643 /*inline_p=*/true);
19645 if (processing_template_decl)
19646 pop_deferring_access_checks ();
19648 /* Leave the scope of the containing function. */
19649 if (function_scope)
19650 pop_function_context ();
19651 cp_parser_pop_lexer (parser);
19654 /* Remove any template parameters from the symbol table. */
19655 maybe_end_member_template_processing ();
19657 /* Restore the queue. */
19658 pop_unparsed_function_queues (parser);
19661 /* If DECL contains any default args, remember it on the unparsed
19662 functions queue. */
19665 cp_parser_save_default_args (cp_parser* parser, tree decl)
19669 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19671 probe = TREE_CHAIN (probe))
19672 if (TREE_PURPOSE (probe))
19674 cp_default_arg_entry *entry
19675 = VEC_safe_push (cp_default_arg_entry, gc,
19676 unparsed_funs_with_default_args, NULL);
19677 entry->class_type = current_class_type;
19678 entry->decl = decl;
19683 /* FN is a FUNCTION_DECL which may contains a parameter with an
19684 unparsed DEFAULT_ARG. Parse the default args now. This function
19685 assumes that the current scope is the scope in which the default
19686 argument should be processed. */
19689 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19691 bool saved_local_variables_forbidden_p;
19692 tree parm, parmdecl;
19694 /* While we're parsing the default args, we might (due to the
19695 statement expression extension) encounter more classes. We want
19696 to handle them right away, but we don't want them getting mixed
19697 up with default args that are currently in the queue. */
19698 push_unparsed_function_queues (parser);
19700 /* Local variable names (and the `this' keyword) may not appear
19701 in a default argument. */
19702 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19703 parser->local_variables_forbidden_p = true;
19705 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19706 parmdecl = DECL_ARGUMENTS (fn);
19707 parm && parm != void_list_node;
19708 parm = TREE_CHAIN (parm),
19709 parmdecl = DECL_CHAIN (parmdecl))
19711 cp_token_cache *tokens;
19712 tree default_arg = TREE_PURPOSE (parm);
19714 VEC(tree,gc) *insts;
19721 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19722 /* This can happen for a friend declaration for a function
19723 already declared with default arguments. */
19726 /* Push the saved tokens for the default argument onto the parser's
19728 tokens = DEFARG_TOKENS (default_arg);
19729 cp_parser_push_lexer_for_tokens (parser, tokens);
19731 start_lambda_scope (parmdecl);
19733 /* Parse the assignment-expression. */
19734 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19735 if (parsed_arg == error_mark_node)
19737 cp_parser_pop_lexer (parser);
19741 if (!processing_template_decl)
19742 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19744 TREE_PURPOSE (parm) = parsed_arg;
19746 /* Update any instantiations we've already created. */
19747 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19748 VEC_iterate (tree, insts, ix, copy); ix++)
19749 TREE_PURPOSE (copy) = parsed_arg;
19751 finish_lambda_scope ();
19753 /* If the token stream has not been completely used up, then
19754 there was extra junk after the end of the default
19756 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19757 cp_parser_error (parser, "expected %<,%>");
19759 /* Revert to the main lexer. */
19760 cp_parser_pop_lexer (parser);
19763 /* Make sure no default arg is missing. */
19764 check_default_args (fn);
19766 /* Restore the state of local_variables_forbidden_p. */
19767 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19769 /* Restore the queue. */
19770 pop_unparsed_function_queues (parser);
19773 /* Parse the operand of `sizeof' (or a similar operator). Returns
19774 either a TYPE or an expression, depending on the form of the
19775 input. The KEYWORD indicates which kind of expression we have
19779 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19781 tree expr = NULL_TREE;
19782 const char *saved_message;
19784 bool saved_integral_constant_expression_p;
19785 bool saved_non_integral_constant_expression_p;
19786 bool pack_expansion_p = false;
19788 /* Types cannot be defined in a `sizeof' expression. Save away the
19790 saved_message = parser->type_definition_forbidden_message;
19791 /* And create the new one. */
19792 tmp = concat ("types may not be defined in %<",
19793 IDENTIFIER_POINTER (ridpointers[keyword]),
19794 "%> expressions", NULL);
19795 parser->type_definition_forbidden_message = tmp;
19797 /* The restrictions on constant-expressions do not apply inside
19798 sizeof expressions. */
19799 saved_integral_constant_expression_p
19800 = parser->integral_constant_expression_p;
19801 saved_non_integral_constant_expression_p
19802 = parser->non_integral_constant_expression_p;
19803 parser->integral_constant_expression_p = false;
19805 /* If it's a `...', then we are computing the length of a parameter
19807 if (keyword == RID_SIZEOF
19808 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19810 /* Consume the `...'. */
19811 cp_lexer_consume_token (parser->lexer);
19812 maybe_warn_variadic_templates ();
19814 /* Note that this is an expansion. */
19815 pack_expansion_p = true;
19818 /* Do not actually evaluate the expression. */
19819 ++cp_unevaluated_operand;
19820 ++c_inhibit_evaluation_warnings;
19821 /* If it's a `(', then we might be looking at the type-id
19823 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19826 bool saved_in_type_id_in_expr_p;
19828 /* We can't be sure yet whether we're looking at a type-id or an
19830 cp_parser_parse_tentatively (parser);
19831 /* Consume the `('. */
19832 cp_lexer_consume_token (parser->lexer);
19833 /* Parse the type-id. */
19834 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19835 parser->in_type_id_in_expr_p = true;
19836 type = cp_parser_type_id (parser);
19837 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19838 /* Now, look for the trailing `)'. */
19839 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19840 /* If all went well, then we're done. */
19841 if (cp_parser_parse_definitely (parser))
19843 cp_decl_specifier_seq decl_specs;
19845 /* Build a trivial decl-specifier-seq. */
19846 clear_decl_specs (&decl_specs);
19847 decl_specs.type = type;
19849 /* Call grokdeclarator to figure out what type this is. */
19850 expr = grokdeclarator (NULL,
19854 /*attrlist=*/NULL);
19858 /* If the type-id production did not work out, then we must be
19859 looking at the unary-expression production. */
19861 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19862 /*cast_p=*/false, NULL);
19864 if (pack_expansion_p)
19865 /* Build a pack expansion. */
19866 expr = make_pack_expansion (expr);
19868 /* Go back to evaluating expressions. */
19869 --cp_unevaluated_operand;
19870 --c_inhibit_evaluation_warnings;
19872 /* Free the message we created. */
19874 /* And restore the old one. */
19875 parser->type_definition_forbidden_message = saved_message;
19876 parser->integral_constant_expression_p
19877 = saved_integral_constant_expression_p;
19878 parser->non_integral_constant_expression_p
19879 = saved_non_integral_constant_expression_p;
19884 /* If the current declaration has no declarator, return true. */
19887 cp_parser_declares_only_class_p (cp_parser *parser)
19889 /* If the next token is a `;' or a `,' then there is no
19891 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19892 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19895 /* Update the DECL_SPECS to reflect the storage class indicated by
19899 cp_parser_set_storage_class (cp_parser *parser,
19900 cp_decl_specifier_seq *decl_specs,
19902 location_t location)
19904 cp_storage_class storage_class;
19906 if (parser->in_unbraced_linkage_specification_p)
19908 error_at (location, "invalid use of %qD in linkage specification",
19909 ridpointers[keyword]);
19912 else if (decl_specs->storage_class != sc_none)
19914 decl_specs->conflicting_specifiers_p = true;
19918 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19919 && decl_specs->specs[(int) ds_thread])
19921 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19922 decl_specs->specs[(int) ds_thread] = 0;
19928 storage_class = sc_auto;
19931 storage_class = sc_register;
19934 storage_class = sc_static;
19937 storage_class = sc_extern;
19940 storage_class = sc_mutable;
19943 gcc_unreachable ();
19945 decl_specs->storage_class = storage_class;
19947 /* A storage class specifier cannot be applied alongside a typedef
19948 specifier. If there is a typedef specifier present then set
19949 conflicting_specifiers_p which will trigger an error later
19950 on in grokdeclarator. */
19951 if (decl_specs->specs[(int)ds_typedef])
19952 decl_specs->conflicting_specifiers_p = true;
19955 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19956 is true, the type is a user-defined type; otherwise it is a
19957 built-in type specified by a keyword. */
19960 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19962 location_t location,
19963 bool user_defined_p)
19965 decl_specs->any_specifiers_p = true;
19967 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19968 (with, for example, in "typedef int wchar_t;") we remember that
19969 this is what happened. In system headers, we ignore these
19970 declarations so that G++ can work with system headers that are not
19972 if (decl_specs->specs[(int) ds_typedef]
19974 && (type_spec == boolean_type_node
19975 || type_spec == char16_type_node
19976 || type_spec == char32_type_node
19977 || type_spec == wchar_type_node)
19978 && (decl_specs->type
19979 || decl_specs->specs[(int) ds_long]
19980 || decl_specs->specs[(int) ds_short]
19981 || decl_specs->specs[(int) ds_unsigned]
19982 || decl_specs->specs[(int) ds_signed]))
19984 decl_specs->redefined_builtin_type = type_spec;
19985 if (!decl_specs->type)
19987 decl_specs->type = type_spec;
19988 decl_specs->user_defined_type_p = false;
19989 decl_specs->type_location = location;
19992 else if (decl_specs->type)
19993 decl_specs->multiple_types_p = true;
19996 decl_specs->type = type_spec;
19997 decl_specs->user_defined_type_p = user_defined_p;
19998 decl_specs->redefined_builtin_type = NULL_TREE;
19999 decl_specs->type_location = location;
20003 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20004 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20007 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20009 return decl_specifiers->specs[(int) ds_friend] != 0;
20012 /* Issue an error message indicating that TOKEN_DESC was expected.
20013 If KEYWORD is true, it indicated this function is called by
20014 cp_parser_require_keword and the required token can only be
20015 a indicated keyword. */
20018 cp_parser_required_error (cp_parser *parser,
20019 required_token token_desc,
20022 switch (token_desc)
20025 cp_parser_error (parser, "expected %<new%>");
20028 cp_parser_error (parser, "expected %<delete%>");
20031 cp_parser_error (parser, "expected %<return%>");
20034 cp_parser_error (parser, "expected %<while%>");
20037 cp_parser_error (parser, "expected %<extern%>");
20039 case RT_STATIC_ASSERT:
20040 cp_parser_error (parser, "expected %<static_assert%>");
20043 cp_parser_error (parser, "expected %<decltype%>");
20046 cp_parser_error (parser, "expected %<operator%>");
20049 cp_parser_error (parser, "expected %<class%>");
20052 cp_parser_error (parser, "expected %<template%>");
20055 cp_parser_error (parser, "expected %<namespace%>");
20058 cp_parser_error (parser, "expected %<using%>");
20061 cp_parser_error (parser, "expected %<asm%>");
20064 cp_parser_error (parser, "expected %<try%>");
20067 cp_parser_error (parser, "expected %<catch%>");
20070 cp_parser_error (parser, "expected %<throw%>");
20073 cp_parser_error (parser, "expected %<__label__%>");
20076 cp_parser_error (parser, "expected %<@try%>");
20078 case RT_AT_SYNCHRONIZED:
20079 cp_parser_error (parser, "expected %<@synchronized%>");
20082 cp_parser_error (parser, "expected %<@throw%>");
20089 switch (token_desc)
20092 cp_parser_error (parser, "expected %<;%>");
20094 case RT_OPEN_PAREN:
20095 cp_parser_error (parser, "expected %<(%>");
20097 case RT_CLOSE_BRACE:
20098 cp_parser_error (parser, "expected %<}%>");
20100 case RT_OPEN_BRACE:
20101 cp_parser_error (parser, "expected %<{%>");
20103 case RT_CLOSE_SQUARE:
20104 cp_parser_error (parser, "expected %<]%>");
20106 case RT_OPEN_SQUARE:
20107 cp_parser_error (parser, "expected %<[%>");
20110 cp_parser_error (parser, "expected %<,%>");
20113 cp_parser_error (parser, "expected %<::%>");
20116 cp_parser_error (parser, "expected %<<%>");
20119 cp_parser_error (parser, "expected %<>%>");
20122 cp_parser_error (parser, "expected %<=%>");
20125 cp_parser_error (parser, "expected %<...%>");
20128 cp_parser_error (parser, "expected %<*%>");
20131 cp_parser_error (parser, "expected %<~%>");
20134 cp_parser_error (parser, "expected %<:%>");
20136 case RT_COLON_SCOPE:
20137 cp_parser_error (parser, "expected %<:%> or %<::%>");
20139 case RT_CLOSE_PAREN:
20140 cp_parser_error (parser, "expected %<)%>");
20142 case RT_COMMA_CLOSE_PAREN:
20143 cp_parser_error (parser, "expected %<,%> or %<)%>");
20145 case RT_PRAGMA_EOL:
20146 cp_parser_error (parser, "expected end of line");
20149 cp_parser_error (parser, "expected identifier");
20152 cp_parser_error (parser, "expected selection-statement");
20154 case RT_INTERATION:
20155 cp_parser_error (parser, "expected iteration-statement");
20158 cp_parser_error (parser, "expected jump-statement");
20161 cp_parser_error (parser, "expected class-key");
20163 case RT_CLASS_TYPENAME_TEMPLATE:
20164 cp_parser_error (parser,
20165 "expected %<class%>, %<typename%>, or %<template%>");
20168 gcc_unreachable ();
20172 gcc_unreachable ();
20177 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20178 issue an error message indicating that TOKEN_DESC was expected.
20180 Returns the token consumed, if the token had the appropriate type.
20181 Otherwise, returns NULL. */
20184 cp_parser_require (cp_parser* parser,
20185 enum cpp_ttype type,
20186 required_token token_desc)
20188 if (cp_lexer_next_token_is (parser->lexer, type))
20189 return cp_lexer_consume_token (parser->lexer);
20192 /* Output the MESSAGE -- unless we're parsing tentatively. */
20193 if (!cp_parser_simulate_error (parser))
20194 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20199 /* An error message is produced if the next token is not '>'.
20200 All further tokens are skipped until the desired token is
20201 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20204 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20206 /* Current level of '< ... >'. */
20207 unsigned level = 0;
20208 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20209 unsigned nesting_depth = 0;
20211 /* Are we ready, yet? If not, issue error message. */
20212 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20215 /* Skip tokens until the desired token is found. */
20218 /* Peek at the next token. */
20219 switch (cp_lexer_peek_token (parser->lexer)->type)
20222 if (!nesting_depth)
20227 if (cxx_dialect == cxx98)
20228 /* C++0x views the `>>' operator as two `>' tokens, but
20231 else if (!nesting_depth && level-- == 0)
20233 /* We've hit a `>>' where the first `>' closes the
20234 template argument list, and the second `>' is
20235 spurious. Just consume the `>>' and stop; we've
20236 already produced at least one error. */
20237 cp_lexer_consume_token (parser->lexer);
20240 /* Fall through for C++0x, so we handle the second `>' in
20244 if (!nesting_depth && level-- == 0)
20246 /* We've reached the token we want, consume it and stop. */
20247 cp_lexer_consume_token (parser->lexer);
20252 case CPP_OPEN_PAREN:
20253 case CPP_OPEN_SQUARE:
20257 case CPP_CLOSE_PAREN:
20258 case CPP_CLOSE_SQUARE:
20259 if (nesting_depth-- == 0)
20264 case CPP_PRAGMA_EOL:
20265 case CPP_SEMICOLON:
20266 case CPP_OPEN_BRACE:
20267 case CPP_CLOSE_BRACE:
20268 /* The '>' was probably forgotten, don't look further. */
20275 /* Consume this token. */
20276 cp_lexer_consume_token (parser->lexer);
20280 /* If the next token is the indicated keyword, consume it. Otherwise,
20281 issue an error message indicating that TOKEN_DESC was expected.
20283 Returns the token consumed, if the token had the appropriate type.
20284 Otherwise, returns NULL. */
20287 cp_parser_require_keyword (cp_parser* parser,
20289 required_token token_desc)
20291 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20293 if (token && token->keyword != keyword)
20295 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
20302 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20303 function-definition. */
20306 cp_parser_token_starts_function_definition_p (cp_token* token)
20308 return (/* An ordinary function-body begins with an `{'. */
20309 token->type == CPP_OPEN_BRACE
20310 /* A ctor-initializer begins with a `:'. */
20311 || token->type == CPP_COLON
20312 /* A function-try-block begins with `try'. */
20313 || token->keyword == RID_TRY
20314 /* The named return value extension begins with `return'. */
20315 || token->keyword == RID_RETURN);
20318 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20322 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20326 token = cp_lexer_peek_token (parser->lexer);
20327 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20330 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20331 C++0x) ending a template-argument. */
20334 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20338 token = cp_lexer_peek_token (parser->lexer);
20339 return (token->type == CPP_COMMA
20340 || token->type == CPP_GREATER
20341 || token->type == CPP_ELLIPSIS
20342 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20345 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20346 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
20349 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
20354 token = cp_lexer_peek_nth_token (parser->lexer, n);
20355 if (token->type == CPP_LESS)
20357 /* Check for the sequence `<::' in the original code. It would be lexed as
20358 `[:', where `[' is a digraph, and there is no whitespace before
20360 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
20363 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
20364 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
20370 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20371 or none_type otherwise. */
20373 static enum tag_types
20374 cp_parser_token_is_class_key (cp_token* token)
20376 switch (token->keyword)
20381 return record_type;
20390 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
20393 cp_parser_check_class_key (enum tag_types class_key, tree type)
20395 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
20396 permerror (input_location, "%qs tag used in naming %q#T",
20397 class_key == union_type ? "union"
20398 : class_key == record_type ? "struct" : "class",
20402 /* Issue an error message if DECL is redeclared with different
20403 access than its original declaration [class.access.spec/3].
20404 This applies to nested classes and nested class templates.
20408 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
20410 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
20413 if ((TREE_PRIVATE (decl)
20414 != (current_access_specifier == access_private_node))
20415 || (TREE_PROTECTED (decl)
20416 != (current_access_specifier == access_protected_node)))
20417 error_at (location, "%qD redeclared with different access", decl);
20420 /* Look for the `template' keyword, as a syntactic disambiguator.
20421 Return TRUE iff it is present, in which case it will be
20425 cp_parser_optional_template_keyword (cp_parser *parser)
20427 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20429 /* The `template' keyword can only be used within templates;
20430 outside templates the parser can always figure out what is a
20431 template and what is not. */
20432 if (!processing_template_decl)
20434 cp_token *token = cp_lexer_peek_token (parser->lexer);
20435 error_at (token->location,
20436 "%<template%> (as a disambiguator) is only allowed "
20437 "within templates");
20438 /* If this part of the token stream is rescanned, the same
20439 error message would be generated. So, we purge the token
20440 from the stream. */
20441 cp_lexer_purge_token (parser->lexer);
20446 /* Consume the `template' keyword. */
20447 cp_lexer_consume_token (parser->lexer);
20455 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
20456 set PARSER->SCOPE, and perform other related actions. */
20459 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
20462 struct tree_check *check_value;
20463 deferred_access_check *chk;
20464 VEC (deferred_access_check,gc) *checks;
20466 /* Get the stored value. */
20467 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
20468 /* Perform any access checks that were deferred. */
20469 checks = check_value->checks;
20473 VEC_iterate (deferred_access_check, checks, i, chk) ;
20476 perform_or_defer_access_check (chk->binfo,
20481 /* Set the scope from the stored value. */
20482 parser->scope = check_value->value;
20483 parser->qualifying_scope = check_value->qualifying_scope;
20484 parser->object_scope = NULL_TREE;
20487 /* Consume tokens up through a non-nested END token. Returns TRUE if we
20488 encounter the end of a block before what we were looking for. */
20491 cp_parser_cache_group (cp_parser *parser,
20492 enum cpp_ttype end,
20497 cp_token *token = cp_lexer_peek_token (parser->lexer);
20499 /* Abort a parenthesized expression if we encounter a semicolon. */
20500 if ((end == CPP_CLOSE_PAREN || depth == 0)
20501 && token->type == CPP_SEMICOLON)
20503 /* If we've reached the end of the file, stop. */
20504 if (token->type == CPP_EOF
20505 || (end != CPP_PRAGMA_EOL
20506 && token->type == CPP_PRAGMA_EOL))
20508 if (token->type == CPP_CLOSE_BRACE && depth == 0)
20509 /* We've hit the end of an enclosing block, so there's been some
20510 kind of syntax error. */
20513 /* Consume the token. */
20514 cp_lexer_consume_token (parser->lexer);
20515 /* See if it starts a new group. */
20516 if (token->type == CPP_OPEN_BRACE)
20518 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
20519 /* In theory this should probably check end == '}', but
20520 cp_parser_save_member_function_body needs it to exit
20521 after either '}' or ')' when called with ')'. */
20525 else if (token->type == CPP_OPEN_PAREN)
20527 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
20528 if (depth == 0 && end == CPP_CLOSE_PAREN)
20531 else if (token->type == CPP_PRAGMA)
20532 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
20533 else if (token->type == end)
20538 /* Begin parsing tentatively. We always save tokens while parsing
20539 tentatively so that if the tentative parsing fails we can restore the
20543 cp_parser_parse_tentatively (cp_parser* parser)
20545 /* Enter a new parsing context. */
20546 parser->context = cp_parser_context_new (parser->context);
20547 /* Begin saving tokens. */
20548 cp_lexer_save_tokens (parser->lexer);
20549 /* In order to avoid repetitive access control error messages,
20550 access checks are queued up until we are no longer parsing
20552 push_deferring_access_checks (dk_deferred);
20555 /* Commit to the currently active tentative parse. */
20558 cp_parser_commit_to_tentative_parse (cp_parser* parser)
20560 cp_parser_context *context;
20563 /* Mark all of the levels as committed. */
20564 lexer = parser->lexer;
20565 for (context = parser->context; context->next; context = context->next)
20567 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
20569 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
20570 while (!cp_lexer_saving_tokens (lexer))
20571 lexer = lexer->next;
20572 cp_lexer_commit_tokens (lexer);
20576 /* Abort the currently active tentative parse. All consumed tokens
20577 will be rolled back, and no diagnostics will be issued. */
20580 cp_parser_abort_tentative_parse (cp_parser* parser)
20582 cp_parser_simulate_error (parser);
20583 /* Now, pretend that we want to see if the construct was
20584 successfully parsed. */
20585 cp_parser_parse_definitely (parser);
20588 /* Stop parsing tentatively. If a parse error has occurred, restore the
20589 token stream. Otherwise, commit to the tokens we have consumed.
20590 Returns true if no error occurred; false otherwise. */
20593 cp_parser_parse_definitely (cp_parser* parser)
20595 bool error_occurred;
20596 cp_parser_context *context;
20598 /* Remember whether or not an error occurred, since we are about to
20599 destroy that information. */
20600 error_occurred = cp_parser_error_occurred (parser);
20601 /* Remove the topmost context from the stack. */
20602 context = parser->context;
20603 parser->context = context->next;
20604 /* If no parse errors occurred, commit to the tentative parse. */
20605 if (!error_occurred)
20607 /* Commit to the tokens read tentatively, unless that was
20609 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20610 cp_lexer_commit_tokens (parser->lexer);
20612 pop_to_parent_deferring_access_checks ();
20614 /* Otherwise, if errors occurred, roll back our state so that things
20615 are just as they were before we began the tentative parse. */
20618 cp_lexer_rollback_tokens (parser->lexer);
20619 pop_deferring_access_checks ();
20621 /* Add the context to the front of the free list. */
20622 context->next = cp_parser_context_free_list;
20623 cp_parser_context_free_list = context;
20625 return !error_occurred;
20628 /* Returns true if we are parsing tentatively and are not committed to
20629 this tentative parse. */
20632 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20634 return (cp_parser_parsing_tentatively (parser)
20635 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20638 /* Returns nonzero iff an error has occurred during the most recent
20639 tentative parse. */
20642 cp_parser_error_occurred (cp_parser* parser)
20644 return (cp_parser_parsing_tentatively (parser)
20645 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20648 /* Returns nonzero if GNU extensions are allowed. */
20651 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20653 return parser->allow_gnu_extensions_p;
20656 /* Objective-C++ Productions */
20659 /* Parse an Objective-C expression, which feeds into a primary-expression
20663 objc-message-expression
20664 objc-string-literal
20665 objc-encode-expression
20666 objc-protocol-expression
20667 objc-selector-expression
20669 Returns a tree representation of the expression. */
20672 cp_parser_objc_expression (cp_parser* parser)
20674 /* Try to figure out what kind of declaration is present. */
20675 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20679 case CPP_OPEN_SQUARE:
20680 return cp_parser_objc_message_expression (parser);
20682 case CPP_OBJC_STRING:
20683 kwd = cp_lexer_consume_token (parser->lexer);
20684 return objc_build_string_object (kwd->u.value);
20687 switch (kwd->keyword)
20689 case RID_AT_ENCODE:
20690 return cp_parser_objc_encode_expression (parser);
20692 case RID_AT_PROTOCOL:
20693 return cp_parser_objc_protocol_expression (parser);
20695 case RID_AT_SELECTOR:
20696 return cp_parser_objc_selector_expression (parser);
20702 error_at (kwd->location,
20703 "misplaced %<@%D%> Objective-C++ construct",
20705 cp_parser_skip_to_end_of_block_or_statement (parser);
20708 return error_mark_node;
20711 /* Parse an Objective-C message expression.
20713 objc-message-expression:
20714 [ objc-message-receiver objc-message-args ]
20716 Returns a representation of an Objective-C message. */
20719 cp_parser_objc_message_expression (cp_parser* parser)
20721 tree receiver, messageargs;
20723 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20724 receiver = cp_parser_objc_message_receiver (parser);
20725 messageargs = cp_parser_objc_message_args (parser);
20726 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20728 return objc_build_message_expr (build_tree_list (receiver, messageargs));
20731 /* Parse an objc-message-receiver.
20733 objc-message-receiver:
20735 simple-type-specifier
20737 Returns a representation of the type or expression. */
20740 cp_parser_objc_message_receiver (cp_parser* parser)
20744 /* An Objective-C message receiver may be either (1) a type
20745 or (2) an expression. */
20746 cp_parser_parse_tentatively (parser);
20747 rcv = cp_parser_expression (parser, false, NULL);
20749 if (cp_parser_parse_definitely (parser))
20752 rcv = cp_parser_simple_type_specifier (parser,
20753 /*decl_specs=*/NULL,
20754 CP_PARSER_FLAGS_NONE);
20756 return objc_get_class_reference (rcv);
20759 /* Parse the arguments and selectors comprising an Objective-C message.
20764 objc-selector-args , objc-comma-args
20766 objc-selector-args:
20767 objc-selector [opt] : assignment-expression
20768 objc-selector-args objc-selector [opt] : assignment-expression
20771 assignment-expression
20772 objc-comma-args , assignment-expression
20774 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20775 selector arguments and TREE_VALUE containing a list of comma
20779 cp_parser_objc_message_args (cp_parser* parser)
20781 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20782 bool maybe_unary_selector_p = true;
20783 cp_token *token = cp_lexer_peek_token (parser->lexer);
20785 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20787 tree selector = NULL_TREE, arg;
20789 if (token->type != CPP_COLON)
20790 selector = cp_parser_objc_selector (parser);
20792 /* Detect if we have a unary selector. */
20793 if (maybe_unary_selector_p
20794 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20795 return build_tree_list (selector, NULL_TREE);
20797 maybe_unary_selector_p = false;
20798 cp_parser_require (parser, CPP_COLON, RT_COLON);
20799 arg = cp_parser_assignment_expression (parser, false, NULL);
20802 = chainon (sel_args,
20803 build_tree_list (selector, arg));
20805 token = cp_lexer_peek_token (parser->lexer);
20808 /* Handle non-selector arguments, if any. */
20809 while (token->type == CPP_COMMA)
20813 cp_lexer_consume_token (parser->lexer);
20814 arg = cp_parser_assignment_expression (parser, false, NULL);
20817 = chainon (addl_args,
20818 build_tree_list (NULL_TREE, arg));
20820 token = cp_lexer_peek_token (parser->lexer);
20823 return build_tree_list (sel_args, addl_args);
20826 /* Parse an Objective-C encode expression.
20828 objc-encode-expression:
20829 @encode objc-typename
20831 Returns an encoded representation of the type argument. */
20834 cp_parser_objc_encode_expression (cp_parser* parser)
20839 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20840 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20841 token = cp_lexer_peek_token (parser->lexer);
20842 type = complete_type (cp_parser_type_id (parser));
20843 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20847 error_at (token->location,
20848 "%<@encode%> must specify a type as an argument");
20849 return error_mark_node;
20852 return objc_build_encode_expr (type);
20855 /* Parse an Objective-C @defs expression. */
20858 cp_parser_objc_defs_expression (cp_parser *parser)
20862 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20863 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20864 name = cp_parser_identifier (parser);
20865 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20867 return objc_get_class_ivars (name);
20870 /* Parse an Objective-C protocol expression.
20872 objc-protocol-expression:
20873 @protocol ( identifier )
20875 Returns a representation of the protocol expression. */
20878 cp_parser_objc_protocol_expression (cp_parser* parser)
20882 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20883 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20884 proto = cp_parser_identifier (parser);
20885 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20887 return objc_build_protocol_expr (proto);
20890 /* Parse an Objective-C selector expression.
20892 objc-selector-expression:
20893 @selector ( objc-method-signature )
20895 objc-method-signature:
20901 objc-selector-seq objc-selector :
20903 Returns a representation of the method selector. */
20906 cp_parser_objc_selector_expression (cp_parser* parser)
20908 tree sel_seq = NULL_TREE;
20909 bool maybe_unary_selector_p = true;
20911 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20913 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20914 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20915 token = cp_lexer_peek_token (parser->lexer);
20917 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20918 || token->type == CPP_SCOPE)
20920 tree selector = NULL_TREE;
20922 if (token->type != CPP_COLON
20923 || token->type == CPP_SCOPE)
20924 selector = cp_parser_objc_selector (parser);
20926 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20927 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20929 /* Detect if we have a unary selector. */
20930 if (maybe_unary_selector_p)
20932 sel_seq = selector;
20933 goto finish_selector;
20937 cp_parser_error (parser, "expected %<:%>");
20940 maybe_unary_selector_p = false;
20941 token = cp_lexer_consume_token (parser->lexer);
20943 if (token->type == CPP_SCOPE)
20946 = chainon (sel_seq,
20947 build_tree_list (selector, NULL_TREE));
20949 = chainon (sel_seq,
20950 build_tree_list (NULL_TREE, NULL_TREE));
20954 = chainon (sel_seq,
20955 build_tree_list (selector, NULL_TREE));
20957 token = cp_lexer_peek_token (parser->lexer);
20961 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20963 return objc_build_selector_expr (loc, sel_seq);
20966 /* Parse a list of identifiers.
20968 objc-identifier-list:
20970 objc-identifier-list , identifier
20972 Returns a TREE_LIST of identifier nodes. */
20975 cp_parser_objc_identifier_list (cp_parser* parser)
20977 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20978 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20980 while (sep->type == CPP_COMMA)
20982 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20983 list = chainon (list,
20984 build_tree_list (NULL_TREE,
20985 cp_parser_identifier (parser)));
20986 sep = cp_lexer_peek_token (parser->lexer);
20992 /* Parse an Objective-C alias declaration.
20994 objc-alias-declaration:
20995 @compatibility_alias identifier identifier ;
20997 This function registers the alias mapping with the Objective-C front end.
20998 It returns nothing. */
21001 cp_parser_objc_alias_declaration (cp_parser* parser)
21005 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21006 alias = cp_parser_identifier (parser);
21007 orig = cp_parser_identifier (parser);
21008 objc_declare_alias (alias, orig);
21009 cp_parser_consume_semicolon_at_end_of_statement (parser);
21012 /* Parse an Objective-C class forward-declaration.
21014 objc-class-declaration:
21015 @class objc-identifier-list ;
21017 The function registers the forward declarations with the Objective-C
21018 front end. It returns nothing. */
21021 cp_parser_objc_class_declaration (cp_parser* parser)
21023 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21024 objc_declare_class (cp_parser_objc_identifier_list (parser));
21025 cp_parser_consume_semicolon_at_end_of_statement (parser);
21028 /* Parse a list of Objective-C protocol references.
21030 objc-protocol-refs-opt:
21031 objc-protocol-refs [opt]
21033 objc-protocol-refs:
21034 < objc-identifier-list >
21036 Returns a TREE_LIST of identifiers, if any. */
21039 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21041 tree protorefs = NULL_TREE;
21043 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21045 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21046 protorefs = cp_parser_objc_identifier_list (parser);
21047 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21053 /* Parse a Objective-C visibility specification. */
21056 cp_parser_objc_visibility_spec (cp_parser* parser)
21058 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21060 switch (vis->keyword)
21062 case RID_AT_PRIVATE:
21063 objc_set_visibility (2);
21065 case RID_AT_PROTECTED:
21066 objc_set_visibility (0);
21068 case RID_AT_PUBLIC:
21069 objc_set_visibility (1);
21075 /* Eat '@private'/'@protected'/'@public'. */
21076 cp_lexer_consume_token (parser->lexer);
21079 /* Parse an Objective-C method type. */
21082 cp_parser_objc_method_type (cp_parser* parser)
21084 objc_set_method_type
21085 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
21090 /* Parse an Objective-C protocol qualifier. */
21093 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21095 tree quals = NULL_TREE, node;
21096 cp_token *token = cp_lexer_peek_token (parser->lexer);
21098 node = token->u.value;
21100 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21101 && (node == ridpointers [(int) RID_IN]
21102 || node == ridpointers [(int) RID_OUT]
21103 || node == ridpointers [(int) RID_INOUT]
21104 || node == ridpointers [(int) RID_BYCOPY]
21105 || node == ridpointers [(int) RID_BYREF]
21106 || node == ridpointers [(int) RID_ONEWAY]))
21108 quals = tree_cons (NULL_TREE, node, quals);
21109 cp_lexer_consume_token (parser->lexer);
21110 token = cp_lexer_peek_token (parser->lexer);
21111 node = token->u.value;
21117 /* Parse an Objective-C typename. */
21120 cp_parser_objc_typename (cp_parser* parser)
21122 tree type_name = NULL_TREE;
21124 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21126 tree proto_quals, cp_type = NULL_TREE;
21128 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21129 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21131 /* An ObjC type name may consist of just protocol qualifiers, in which
21132 case the type shall default to 'id'. */
21133 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21134 cp_type = cp_parser_type_id (parser);
21136 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21137 type_name = build_tree_list (proto_quals, cp_type);
21143 /* Check to see if TYPE refers to an Objective-C selector name. */
21146 cp_parser_objc_selector_p (enum cpp_ttype type)
21148 return (type == CPP_NAME || type == CPP_KEYWORD
21149 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21150 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21151 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21152 || type == CPP_XOR || type == CPP_XOR_EQ);
21155 /* Parse an Objective-C selector. */
21158 cp_parser_objc_selector (cp_parser* parser)
21160 cp_token *token = cp_lexer_consume_token (parser->lexer);
21162 if (!cp_parser_objc_selector_p (token->type))
21164 error_at (token->location, "invalid Objective-C++ selector name");
21165 return error_mark_node;
21168 /* C++ operator names are allowed to appear in ObjC selectors. */
21169 switch (token->type)
21171 case CPP_AND_AND: return get_identifier ("and");
21172 case CPP_AND_EQ: return get_identifier ("and_eq");
21173 case CPP_AND: return get_identifier ("bitand");
21174 case CPP_OR: return get_identifier ("bitor");
21175 case CPP_COMPL: return get_identifier ("compl");
21176 case CPP_NOT: return get_identifier ("not");
21177 case CPP_NOT_EQ: return get_identifier ("not_eq");
21178 case CPP_OR_OR: return get_identifier ("or");
21179 case CPP_OR_EQ: return get_identifier ("or_eq");
21180 case CPP_XOR: return get_identifier ("xor");
21181 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21182 default: return token->u.value;
21186 /* Parse an Objective-C params list. */
21189 cp_parser_objc_method_keyword_params (cp_parser* parser)
21191 tree params = NULL_TREE;
21192 bool maybe_unary_selector_p = true;
21193 cp_token *token = cp_lexer_peek_token (parser->lexer);
21195 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21197 tree selector = NULL_TREE, type_name, identifier;
21199 if (token->type != CPP_COLON)
21200 selector = cp_parser_objc_selector (parser);
21202 /* Detect if we have a unary selector. */
21203 if (maybe_unary_selector_p
21204 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21207 maybe_unary_selector_p = false;
21208 cp_parser_require (parser, CPP_COLON, RT_COLON);
21209 type_name = cp_parser_objc_typename (parser);
21210 identifier = cp_parser_identifier (parser);
21214 objc_build_keyword_decl (selector,
21218 token = cp_lexer_peek_token (parser->lexer);
21224 /* Parse the non-keyword Objective-C params. */
21227 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
21229 tree params = make_node (TREE_LIST);
21230 cp_token *token = cp_lexer_peek_token (parser->lexer);
21231 *ellipsisp = false; /* Initially, assume no ellipsis. */
21233 while (token->type == CPP_COMMA)
21235 cp_parameter_declarator *parmdecl;
21238 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21239 token = cp_lexer_peek_token (parser->lexer);
21241 if (token->type == CPP_ELLIPSIS)
21243 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
21248 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21249 parm = grokdeclarator (parmdecl->declarator,
21250 &parmdecl->decl_specifiers,
21251 PARM, /*initialized=*/0,
21252 /*attrlist=*/NULL);
21254 chainon (params, build_tree_list (NULL_TREE, parm));
21255 token = cp_lexer_peek_token (parser->lexer);
21261 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
21264 cp_parser_objc_interstitial_code (cp_parser* parser)
21266 cp_token *token = cp_lexer_peek_token (parser->lexer);
21268 /* If the next token is `extern' and the following token is a string
21269 literal, then we have a linkage specification. */
21270 if (token->keyword == RID_EXTERN
21271 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
21272 cp_parser_linkage_specification (parser);
21273 /* Handle #pragma, if any. */
21274 else if (token->type == CPP_PRAGMA)
21275 cp_parser_pragma (parser, pragma_external);
21276 /* Allow stray semicolons. */
21277 else if (token->type == CPP_SEMICOLON)
21278 cp_lexer_consume_token (parser->lexer);
21279 /* Finally, try to parse a block-declaration, or a function-definition. */
21281 cp_parser_block_declaration (parser, /*statement_p=*/false);
21284 /* Parse a method signature. */
21287 cp_parser_objc_method_signature (cp_parser* parser)
21289 tree rettype, kwdparms, optparms;
21290 bool ellipsis = false;
21292 cp_parser_objc_method_type (parser);
21293 rettype = cp_parser_objc_typename (parser);
21294 kwdparms = cp_parser_objc_method_keyword_params (parser);
21295 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
21297 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
21300 /* Pars an Objective-C method prototype list. */
21303 cp_parser_objc_method_prototype_list (cp_parser* parser)
21305 cp_token *token = cp_lexer_peek_token (parser->lexer);
21307 while (token->keyword != RID_AT_END)
21309 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21311 objc_add_method_declaration
21312 (cp_parser_objc_method_signature (parser));
21313 cp_parser_consume_semicolon_at_end_of_statement (parser);
21316 /* Allow for interspersed non-ObjC++ code. */
21317 cp_parser_objc_interstitial_code (parser);
21319 token = cp_lexer_peek_token (parser->lexer);
21322 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21323 objc_finish_interface ();
21326 /* Parse an Objective-C method definition list. */
21329 cp_parser_objc_method_definition_list (cp_parser* parser)
21331 cp_token *token = cp_lexer_peek_token (parser->lexer);
21333 while (token->keyword != RID_AT_END)
21337 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21339 push_deferring_access_checks (dk_deferred);
21340 objc_start_method_definition
21341 (cp_parser_objc_method_signature (parser));
21343 /* For historical reasons, we accept an optional semicolon. */
21344 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21345 cp_lexer_consume_token (parser->lexer);
21347 perform_deferred_access_checks ();
21348 stop_deferring_access_checks ();
21349 meth = cp_parser_function_definition_after_declarator (parser,
21351 pop_deferring_access_checks ();
21352 objc_finish_method_definition (meth);
21355 /* Allow for interspersed non-ObjC++ code. */
21356 cp_parser_objc_interstitial_code (parser);
21358 token = cp_lexer_peek_token (parser->lexer);
21361 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21362 objc_finish_implementation ();
21365 /* Parse Objective-C ivars. */
21368 cp_parser_objc_class_ivars (cp_parser* parser)
21370 cp_token *token = cp_lexer_peek_token (parser->lexer);
21372 if (token->type != CPP_OPEN_BRACE)
21373 return; /* No ivars specified. */
21375 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
21376 token = cp_lexer_peek_token (parser->lexer);
21378 while (token->type != CPP_CLOSE_BRACE)
21380 cp_decl_specifier_seq declspecs;
21381 int decl_class_or_enum_p;
21382 tree prefix_attributes;
21384 cp_parser_objc_visibility_spec (parser);
21386 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21389 cp_parser_decl_specifier_seq (parser,
21390 CP_PARSER_FLAGS_OPTIONAL,
21392 &decl_class_or_enum_p);
21393 prefix_attributes = declspecs.attributes;
21394 declspecs.attributes = NULL_TREE;
21396 /* Keep going until we hit the `;' at the end of the
21398 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21400 tree width = NULL_TREE, attributes, first_attribute, decl;
21401 cp_declarator *declarator = NULL;
21402 int ctor_dtor_or_conv_p;
21404 /* Check for a (possibly unnamed) bitfield declaration. */
21405 token = cp_lexer_peek_token (parser->lexer);
21406 if (token->type == CPP_COLON)
21409 if (token->type == CPP_NAME
21410 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21413 /* Get the name of the bitfield. */
21414 declarator = make_id_declarator (NULL_TREE,
21415 cp_parser_identifier (parser),
21419 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
21420 /* Get the width of the bitfield. */
21422 = cp_parser_constant_expression (parser,
21423 /*allow_non_constant=*/false,
21428 /* Parse the declarator. */
21430 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21431 &ctor_dtor_or_conv_p,
21432 /*parenthesized_p=*/NULL,
21433 /*member_p=*/false);
21436 /* Look for attributes that apply to the ivar. */
21437 attributes = cp_parser_attributes_opt (parser);
21438 /* Remember which attributes are prefix attributes and
21440 first_attribute = attributes;
21441 /* Combine the attributes. */
21442 attributes = chainon (prefix_attributes, attributes);
21445 /* Create the bitfield declaration. */
21446 decl = grokbitfield (declarator, &declspecs,
21450 decl = grokfield (declarator, &declspecs,
21451 NULL_TREE, /*init_const_expr_p=*/false,
21452 NULL_TREE, attributes);
21454 /* Add the instance variable. */
21455 objc_add_instance_variable (decl);
21457 /* Reset PREFIX_ATTRIBUTES. */
21458 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21459 attributes = TREE_CHAIN (attributes);
21461 TREE_CHAIN (attributes) = NULL_TREE;
21463 token = cp_lexer_peek_token (parser->lexer);
21465 if (token->type == CPP_COMMA)
21467 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21473 cp_parser_consume_semicolon_at_end_of_statement (parser);
21474 token = cp_lexer_peek_token (parser->lexer);
21477 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
21478 /* For historical reasons, we accept an optional semicolon. */
21479 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21480 cp_lexer_consume_token (parser->lexer);
21483 /* Parse an Objective-C protocol declaration. */
21486 cp_parser_objc_protocol_declaration (cp_parser* parser)
21488 tree proto, protorefs;
21491 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21492 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
21494 tok = cp_lexer_peek_token (parser->lexer);
21495 error_at (tok->location, "identifier expected after %<@protocol%>");
21499 /* See if we have a forward declaration or a definition. */
21500 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
21502 /* Try a forward declaration first. */
21503 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
21505 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
21507 cp_parser_consume_semicolon_at_end_of_statement (parser);
21510 /* Ok, we got a full-fledged definition (or at least should). */
21513 proto = cp_parser_identifier (parser);
21514 protorefs = cp_parser_objc_protocol_refs_opt (parser);
21515 objc_start_protocol (proto, protorefs);
21516 cp_parser_objc_method_prototype_list (parser);
21520 /* Parse an Objective-C superclass or category. */
21523 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
21526 cp_token *next = cp_lexer_peek_token (parser->lexer);
21528 *super = *categ = NULL_TREE;
21529 if (next->type == CPP_COLON)
21531 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
21532 *super = cp_parser_identifier (parser);
21534 else if (next->type == CPP_OPEN_PAREN)
21536 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21537 *categ = cp_parser_identifier (parser);
21538 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21542 /* Parse an Objective-C class interface. */
21545 cp_parser_objc_class_interface (cp_parser* parser)
21547 tree name, super, categ, protos;
21549 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
21550 name = cp_parser_identifier (parser);
21551 cp_parser_objc_superclass_or_category (parser, &super, &categ);
21552 protos = cp_parser_objc_protocol_refs_opt (parser);
21554 /* We have either a class or a category on our hands. */
21556 objc_start_category_interface (name, categ, protos);
21559 objc_start_class_interface (name, super, protos);
21560 /* Handle instance variable declarations, if any. */
21561 cp_parser_objc_class_ivars (parser);
21562 objc_continue_interface ();
21565 cp_parser_objc_method_prototype_list (parser);
21568 /* Parse an Objective-C class implementation. */
21571 cp_parser_objc_class_implementation (cp_parser* parser)
21573 tree name, super, categ;
21575 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
21576 name = cp_parser_identifier (parser);
21577 cp_parser_objc_superclass_or_category (parser, &super, &categ);
21579 /* We have either a class or a category on our hands. */
21581 objc_start_category_implementation (name, categ);
21584 objc_start_class_implementation (name, super);
21585 /* Handle instance variable declarations, if any. */
21586 cp_parser_objc_class_ivars (parser);
21587 objc_continue_implementation ();
21590 cp_parser_objc_method_definition_list (parser);
21593 /* Consume the @end token and finish off the implementation. */
21596 cp_parser_objc_end_implementation (cp_parser* parser)
21598 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21599 objc_finish_implementation ();
21602 /* Parse an Objective-C declaration. */
21605 cp_parser_objc_declaration (cp_parser* parser)
21607 /* Try to figure out what kind of declaration is present. */
21608 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21610 switch (kwd->keyword)
21613 cp_parser_objc_alias_declaration (parser);
21616 cp_parser_objc_class_declaration (parser);
21618 case RID_AT_PROTOCOL:
21619 cp_parser_objc_protocol_declaration (parser);
21621 case RID_AT_INTERFACE:
21622 cp_parser_objc_class_interface (parser);
21624 case RID_AT_IMPLEMENTATION:
21625 cp_parser_objc_class_implementation (parser);
21628 cp_parser_objc_end_implementation (parser);
21631 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21633 cp_parser_skip_to_end_of_block_or_statement (parser);
21637 /* Parse an Objective-C try-catch-finally statement.
21639 objc-try-catch-finally-stmt:
21640 @try compound-statement objc-catch-clause-seq [opt]
21641 objc-finally-clause [opt]
21643 objc-catch-clause-seq:
21644 objc-catch-clause objc-catch-clause-seq [opt]
21647 @catch ( exception-declaration ) compound-statement
21649 objc-finally-clause
21650 @finally compound-statement
21652 Returns NULL_TREE. */
21655 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21656 location_t location;
21659 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
21660 location = cp_lexer_peek_token (parser->lexer)->location;
21661 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21662 node, lest it get absorbed into the surrounding block. */
21663 stmt = push_stmt_list ();
21664 cp_parser_compound_statement (parser, NULL, false);
21665 objc_begin_try_stmt (location, pop_stmt_list (stmt));
21667 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21669 cp_parameter_declarator *parmdecl;
21672 cp_lexer_consume_token (parser->lexer);
21673 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21674 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21675 parm = grokdeclarator (parmdecl->declarator,
21676 &parmdecl->decl_specifiers,
21677 PARM, /*initialized=*/0,
21678 /*attrlist=*/NULL);
21679 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21680 objc_begin_catch_clause (parm);
21681 cp_parser_compound_statement (parser, NULL, false);
21682 objc_finish_catch_clause ();
21685 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21687 cp_lexer_consume_token (parser->lexer);
21688 location = cp_lexer_peek_token (parser->lexer)->location;
21689 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21690 node, lest it get absorbed into the surrounding block. */
21691 stmt = push_stmt_list ();
21692 cp_parser_compound_statement (parser, NULL, false);
21693 objc_build_finally_clause (location, pop_stmt_list (stmt));
21696 return objc_finish_try_stmt ();
21699 /* Parse an Objective-C synchronized statement.
21701 objc-synchronized-stmt:
21702 @synchronized ( expression ) compound-statement
21704 Returns NULL_TREE. */
21707 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21708 location_t location;
21711 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
21713 location = cp_lexer_peek_token (parser->lexer)->location;
21714 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21715 lock = cp_parser_expression (parser, false, NULL);
21716 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21718 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21719 node, lest it get absorbed into the surrounding block. */
21720 stmt = push_stmt_list ();
21721 cp_parser_compound_statement (parser, NULL, false);
21723 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21726 /* Parse an Objective-C throw statement.
21729 @throw assignment-expression [opt] ;
21731 Returns a constructed '@throw' statement. */
21734 cp_parser_objc_throw_statement (cp_parser *parser) {
21735 tree expr = NULL_TREE;
21736 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21738 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
21740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21741 expr = cp_parser_assignment_expression (parser, false, NULL);
21743 cp_parser_consume_semicolon_at_end_of_statement (parser);
21745 return objc_build_throw_stmt (loc, expr);
21748 /* Parse an Objective-C statement. */
21751 cp_parser_objc_statement (cp_parser * parser) {
21752 /* Try to figure out what kind of declaration is present. */
21753 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21755 switch (kwd->keyword)
21758 return cp_parser_objc_try_catch_finally_statement (parser);
21759 case RID_AT_SYNCHRONIZED:
21760 return cp_parser_objc_synchronized_statement (parser);
21762 return cp_parser_objc_throw_statement (parser);
21764 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21766 cp_parser_skip_to_end_of_block_or_statement (parser);
21769 return error_mark_node;
21772 /* OpenMP 2.5 parsing routines. */
21774 /* Returns name of the next clause.
21775 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21776 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21777 returned and the token is consumed. */
21779 static pragma_omp_clause
21780 cp_parser_omp_clause_name (cp_parser *parser)
21782 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21784 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21785 result = PRAGMA_OMP_CLAUSE_IF;
21786 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21787 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21788 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21789 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21790 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21792 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21793 const char *p = IDENTIFIER_POINTER (id);
21798 if (!strcmp ("collapse", p))
21799 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21800 else if (!strcmp ("copyin", p))
21801 result = PRAGMA_OMP_CLAUSE_COPYIN;
21802 else if (!strcmp ("copyprivate", p))
21803 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21806 if (!strcmp ("firstprivate", p))
21807 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21810 if (!strcmp ("lastprivate", p))
21811 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21814 if (!strcmp ("nowait", p))
21815 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21816 else if (!strcmp ("num_threads", p))
21817 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21820 if (!strcmp ("ordered", p))
21821 result = PRAGMA_OMP_CLAUSE_ORDERED;
21824 if (!strcmp ("reduction", p))
21825 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21828 if (!strcmp ("schedule", p))
21829 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21830 else if (!strcmp ("shared", p))
21831 result = PRAGMA_OMP_CLAUSE_SHARED;
21834 if (!strcmp ("untied", p))
21835 result = PRAGMA_OMP_CLAUSE_UNTIED;
21840 if (result != PRAGMA_OMP_CLAUSE_NONE)
21841 cp_lexer_consume_token (parser->lexer);
21846 /* Validate that a clause of the given type does not already exist. */
21849 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21850 const char *name, location_t location)
21854 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21855 if (OMP_CLAUSE_CODE (c) == code)
21857 error_at (location, "too many %qs clauses", name);
21865 variable-list , identifier
21867 In addition, we match a closing parenthesis. An opening parenthesis
21868 will have been consumed by the caller.
21870 If KIND is nonzero, create the appropriate node and install the decl
21871 in OMP_CLAUSE_DECL and add the node to the head of the list.
21873 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21874 return the list created. */
21877 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21885 token = cp_lexer_peek_token (parser->lexer);
21886 name = cp_parser_id_expression (parser, /*template_p=*/false,
21887 /*check_dependency_p=*/true,
21888 /*template_p=*/NULL,
21889 /*declarator_p=*/false,
21890 /*optional_p=*/false);
21891 if (name == error_mark_node)
21894 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21895 if (decl == error_mark_node)
21896 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
21898 else if (kind != 0)
21900 tree u = build_omp_clause (token->location, kind);
21901 OMP_CLAUSE_DECL (u) = decl;
21902 OMP_CLAUSE_CHAIN (u) = list;
21906 list = tree_cons (decl, NULL_TREE, list);
21909 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21911 cp_lexer_consume_token (parser->lexer);
21914 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21918 /* Try to resync to an unnested comma. Copied from
21919 cp_parser_parenthesized_expression_list. */
21921 ending = cp_parser_skip_to_closing_parenthesis (parser,
21922 /*recovering=*/true,
21924 /*consume_paren=*/true);
21932 /* Similarly, but expect leading and trailing parenthesis. This is a very
21933 common case for omp clauses. */
21936 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21938 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21939 return cp_parser_omp_var_list_no_open (parser, kind, list);
21944 collapse ( constant-expression ) */
21947 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21953 loc = cp_lexer_peek_token (parser->lexer)->location;
21954 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21957 num = cp_parser_constant_expression (parser, false, NULL);
21959 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21960 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21961 /*or_comma=*/false,
21962 /*consume_paren=*/true);
21964 if (num == error_mark_node)
21966 num = fold_non_dependent_expr (num);
21967 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21968 || !host_integerp (num, 0)
21969 || (n = tree_low_cst (num, 0)) <= 0
21972 error_at (loc, "collapse argument needs positive constant integer expression");
21976 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21977 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21978 OMP_CLAUSE_CHAIN (c) = list;
21979 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21985 default ( shared | none ) */
21988 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21990 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21993 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21995 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21997 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21998 const char *p = IDENTIFIER_POINTER (id);
22003 if (strcmp ("none", p) != 0)
22005 kind = OMP_CLAUSE_DEFAULT_NONE;
22009 if (strcmp ("shared", p) != 0)
22011 kind = OMP_CLAUSE_DEFAULT_SHARED;
22018 cp_lexer_consume_token (parser->lexer);
22023 cp_parser_error (parser, "expected %<none%> or %<shared%>");
22026 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22027 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22028 /*or_comma=*/false,
22029 /*consume_paren=*/true);
22031 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
22034 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
22035 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
22036 OMP_CLAUSE_CHAIN (c) = list;
22037 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
22043 if ( expression ) */
22046 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
22050 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22053 t = cp_parser_condition (parser);
22055 if (t == error_mark_node
22056 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22057 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22058 /*or_comma=*/false,
22059 /*consume_paren=*/true);
22061 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
22063 c = build_omp_clause (location, OMP_CLAUSE_IF);
22064 OMP_CLAUSE_IF_EXPR (c) = t;
22065 OMP_CLAUSE_CHAIN (c) = list;
22074 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
22075 tree list, location_t location)
22079 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
22081 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
22082 OMP_CLAUSE_CHAIN (c) = list;
22087 num_threads ( expression ) */
22090 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
22091 location_t location)
22095 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22098 t = cp_parser_expression (parser, false, NULL);
22100 if (t == error_mark_node
22101 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22102 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22103 /*or_comma=*/false,
22104 /*consume_paren=*/true);
22106 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
22107 "num_threads", location);
22109 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
22110 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
22111 OMP_CLAUSE_CHAIN (c) = list;
22120 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
22121 tree list, location_t location)
22125 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
22126 "ordered", location);
22128 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
22129 OMP_CLAUSE_CHAIN (c) = list;
22134 reduction ( reduction-operator : variable-list )
22136 reduction-operator:
22137 One of: + * - & ^ | && || */
22140 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
22142 enum tree_code code;
22145 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22148 switch (cp_lexer_peek_token (parser->lexer)->type)
22160 code = BIT_AND_EXPR;
22163 code = BIT_XOR_EXPR;
22166 code = BIT_IOR_EXPR;
22169 code = TRUTH_ANDIF_EXPR;
22172 code = TRUTH_ORIF_EXPR;
22175 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
22176 "%<|%>, %<&&%>, or %<||%>");
22178 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22179 /*or_comma=*/false,
22180 /*consume_paren=*/true);
22183 cp_lexer_consume_token (parser->lexer);
22185 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22188 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
22189 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
22190 OMP_CLAUSE_REDUCTION_CODE (c) = code;
22196 schedule ( schedule-kind )
22197 schedule ( schedule-kind , expression )
22200 static | dynamic | guided | runtime | auto */
22203 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
22207 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22210 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
22212 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22214 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22215 const char *p = IDENTIFIER_POINTER (id);
22220 if (strcmp ("dynamic", p) != 0)
22222 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
22226 if (strcmp ("guided", p) != 0)
22228 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
22232 if (strcmp ("runtime", p) != 0)
22234 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
22241 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
22242 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
22243 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
22244 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
22247 cp_lexer_consume_token (parser->lexer);
22249 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22252 cp_lexer_consume_token (parser->lexer);
22254 token = cp_lexer_peek_token (parser->lexer);
22255 t = cp_parser_assignment_expression (parser, false, NULL);
22257 if (t == error_mark_node)
22259 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
22260 error_at (token->location, "schedule %<runtime%> does not take "
22261 "a %<chunk_size%> parameter");
22262 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
22263 error_at (token->location, "schedule %<auto%> does not take "
22264 "a %<chunk_size%> parameter");
22266 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
22268 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22271 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
22274 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
22275 OMP_CLAUSE_CHAIN (c) = list;
22279 cp_parser_error (parser, "invalid schedule kind");
22281 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22282 /*or_comma=*/false,
22283 /*consume_paren=*/true);
22291 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
22292 tree list, location_t location)
22296 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
22298 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
22299 OMP_CLAUSE_CHAIN (c) = list;
22303 /* Parse all OpenMP clauses. The set clauses allowed by the directive
22304 is a bitmask in MASK. Return the list of clauses found; the result
22305 of clause default goes in *pdefault. */
22308 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
22309 const char *where, cp_token *pragma_tok)
22311 tree clauses = NULL;
22313 cp_token *token = NULL;
22315 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
22317 pragma_omp_clause c_kind;
22318 const char *c_name;
22319 tree prev = clauses;
22321 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22322 cp_lexer_consume_token (parser->lexer);
22324 token = cp_lexer_peek_token (parser->lexer);
22325 c_kind = cp_parser_omp_clause_name (parser);
22330 case PRAGMA_OMP_CLAUSE_COLLAPSE:
22331 clauses = cp_parser_omp_clause_collapse (parser, clauses,
22333 c_name = "collapse";
22335 case PRAGMA_OMP_CLAUSE_COPYIN:
22336 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
22339 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
22340 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
22342 c_name = "copyprivate";
22344 case PRAGMA_OMP_CLAUSE_DEFAULT:
22345 clauses = cp_parser_omp_clause_default (parser, clauses,
22347 c_name = "default";
22349 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
22350 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
22352 c_name = "firstprivate";
22354 case PRAGMA_OMP_CLAUSE_IF:
22355 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
22358 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
22359 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
22361 c_name = "lastprivate";
22363 case PRAGMA_OMP_CLAUSE_NOWAIT:
22364 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
22367 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
22368 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
22370 c_name = "num_threads";
22372 case PRAGMA_OMP_CLAUSE_ORDERED:
22373 clauses = cp_parser_omp_clause_ordered (parser, clauses,
22375 c_name = "ordered";
22377 case PRAGMA_OMP_CLAUSE_PRIVATE:
22378 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
22380 c_name = "private";
22382 case PRAGMA_OMP_CLAUSE_REDUCTION:
22383 clauses = cp_parser_omp_clause_reduction (parser, clauses);
22384 c_name = "reduction";
22386 case PRAGMA_OMP_CLAUSE_SCHEDULE:
22387 clauses = cp_parser_omp_clause_schedule (parser, clauses,
22389 c_name = "schedule";
22391 case PRAGMA_OMP_CLAUSE_SHARED:
22392 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
22396 case PRAGMA_OMP_CLAUSE_UNTIED:
22397 clauses = cp_parser_omp_clause_untied (parser, clauses,
22402 cp_parser_error (parser, "expected %<#pragma omp%> clause");
22406 if (((mask >> c_kind) & 1) == 0)
22408 /* Remove the invalid clause(s) from the list to avoid
22409 confusing the rest of the compiler. */
22411 error_at (token->location, "%qs is not valid for %qs", c_name, where);
22415 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22416 return finish_omp_clauses (clauses);
22423 In practice, we're also interested in adding the statement to an
22424 outer node. So it is convenient if we work around the fact that
22425 cp_parser_statement calls add_stmt. */
22428 cp_parser_begin_omp_structured_block (cp_parser *parser)
22430 unsigned save = parser->in_statement;
22432 /* Only move the values to IN_OMP_BLOCK if they weren't false.
22433 This preserves the "not within loop or switch" style error messages
22434 for nonsense cases like
22440 if (parser->in_statement)
22441 parser->in_statement = IN_OMP_BLOCK;
22447 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
22449 parser->in_statement = save;
22453 cp_parser_omp_structured_block (cp_parser *parser)
22455 tree stmt = begin_omp_structured_block ();
22456 unsigned int save = cp_parser_begin_omp_structured_block (parser);
22458 cp_parser_statement (parser, NULL_TREE, false, NULL);
22460 cp_parser_end_omp_structured_block (parser, save);
22461 return finish_omp_structured_block (stmt);
22465 # pragma omp atomic new-line
22469 x binop= expr | x++ | ++x | x-- | --x
22471 +, *, -, /, &, ^, |, <<, >>
22473 where x is an lvalue expression with scalar type. */
22476 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
22479 enum tree_code code;
22481 cp_parser_require_pragma_eol (parser, pragma_tok);
22483 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
22484 /*cast_p=*/false, NULL);
22485 switch (TREE_CODE (lhs))
22490 case PREINCREMENT_EXPR:
22491 case POSTINCREMENT_EXPR:
22492 lhs = TREE_OPERAND (lhs, 0);
22494 rhs = integer_one_node;
22497 case PREDECREMENT_EXPR:
22498 case POSTDECREMENT_EXPR:
22499 lhs = TREE_OPERAND (lhs, 0);
22501 rhs = integer_one_node;
22505 switch (cp_lexer_peek_token (parser->lexer)->type)
22511 code = TRUNC_DIV_EXPR;
22519 case CPP_LSHIFT_EQ:
22520 code = LSHIFT_EXPR;
22522 case CPP_RSHIFT_EQ:
22523 code = RSHIFT_EXPR;
22526 code = BIT_AND_EXPR;
22529 code = BIT_IOR_EXPR;
22532 code = BIT_XOR_EXPR;
22535 cp_parser_error (parser,
22536 "invalid operator for %<#pragma omp atomic%>");
22539 cp_lexer_consume_token (parser->lexer);
22541 rhs = cp_parser_expression (parser, false, NULL);
22542 if (rhs == error_mark_node)
22546 finish_omp_atomic (code, lhs, rhs);
22547 cp_parser_consume_semicolon_at_end_of_statement (parser);
22551 cp_parser_skip_to_end_of_block_or_statement (parser);
22556 # pragma omp barrier new-line */
22559 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
22561 cp_parser_require_pragma_eol (parser, pragma_tok);
22562 finish_omp_barrier ();
22566 # pragma omp critical [(name)] new-line
22567 structured-block */
22570 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
22572 tree stmt, name = NULL;
22574 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22576 cp_lexer_consume_token (parser->lexer);
22578 name = cp_parser_identifier (parser);
22580 if (name == error_mark_node
22581 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22582 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22583 /*or_comma=*/false,
22584 /*consume_paren=*/true);
22585 if (name == error_mark_node)
22588 cp_parser_require_pragma_eol (parser, pragma_tok);
22590 stmt = cp_parser_omp_structured_block (parser);
22591 return c_finish_omp_critical (input_location, stmt, name);
22595 # pragma omp flush flush-vars[opt] new-line
22598 ( variable-list ) */
22601 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22603 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22604 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22605 cp_parser_require_pragma_eol (parser, pragma_tok);
22607 finish_omp_flush ();
22610 /* Helper function, to parse omp for increment expression. */
22613 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22615 tree cond = cp_parser_binary_expression (parser, false, true,
22616 PREC_NOT_OPERATOR, NULL);
22619 if (cond == error_mark_node
22620 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22622 cp_parser_skip_to_end_of_statement (parser);
22623 return error_mark_node;
22626 switch (TREE_CODE (cond))
22634 return error_mark_node;
22637 /* If decl is an iterator, preserve LHS and RHS of the relational
22638 expr until finish_omp_for. */
22640 && (type_dependent_expression_p (decl)
22641 || CLASS_TYPE_P (TREE_TYPE (decl))))
22644 return build_x_binary_op (TREE_CODE (cond),
22645 TREE_OPERAND (cond, 0), ERROR_MARK,
22646 TREE_OPERAND (cond, 1), ERROR_MARK,
22647 &overloaded_p, tf_warning_or_error);
22650 /* Helper function, to parse omp for increment expression. */
22653 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22655 cp_token *token = cp_lexer_peek_token (parser->lexer);
22661 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22663 op = (token->type == CPP_PLUS_PLUS
22664 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22665 cp_lexer_consume_token (parser->lexer);
22666 lhs = cp_parser_cast_expression (parser, false, false, NULL);
22668 return error_mark_node;
22669 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22672 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22674 return error_mark_node;
22676 token = cp_lexer_peek_token (parser->lexer);
22677 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22679 op = (token->type == CPP_PLUS_PLUS
22680 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22681 cp_lexer_consume_token (parser->lexer);
22682 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22685 op = cp_parser_assignment_operator_opt (parser);
22686 if (op == ERROR_MARK)
22687 return error_mark_node;
22689 if (op != NOP_EXPR)
22691 rhs = cp_parser_assignment_expression (parser, false, NULL);
22692 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22693 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22696 lhs = cp_parser_binary_expression (parser, false, false,
22697 PREC_ADDITIVE_EXPRESSION, NULL);
22698 token = cp_lexer_peek_token (parser->lexer);
22699 decl_first = lhs == decl;
22702 if (token->type != CPP_PLUS
22703 && token->type != CPP_MINUS)
22704 return error_mark_node;
22708 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22709 cp_lexer_consume_token (parser->lexer);
22710 rhs = cp_parser_binary_expression (parser, false, false,
22711 PREC_ADDITIVE_EXPRESSION, NULL);
22712 token = cp_lexer_peek_token (parser->lexer);
22713 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22715 if (lhs == NULL_TREE)
22717 if (op == PLUS_EXPR)
22720 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22723 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22724 NULL, tf_warning_or_error);
22727 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22731 if (rhs != decl || op == MINUS_EXPR)
22732 return error_mark_node;
22733 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22736 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22738 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22741 /* Parse the restricted form of the for statement allowed by OpenMP. */
22744 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22746 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22747 tree real_decl, initv, condv, incrv, declv;
22748 tree this_pre_body, cl;
22749 location_t loc_first;
22750 bool collapse_err = false;
22751 int i, collapse = 1, nbraces = 0;
22752 VEC(tree,gc) *for_block = make_tree_vector ();
22754 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22755 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22756 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22758 gcc_assert (collapse >= 1);
22760 declv = make_tree_vec (collapse);
22761 initv = make_tree_vec (collapse);
22762 condv = make_tree_vec (collapse);
22763 incrv = make_tree_vec (collapse);
22765 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22767 for (i = 0; i < collapse; i++)
22769 int bracecount = 0;
22770 bool add_private_clause = false;
22773 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22775 cp_parser_error (parser, "for statement expected");
22778 loc = cp_lexer_consume_token (parser->lexer)->location;
22780 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22783 init = decl = real_decl = NULL;
22784 this_pre_body = push_stmt_list ();
22785 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22787 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22791 integer-type var = lb
22792 random-access-iterator-type var = lb
22793 pointer-type var = lb
22795 cp_decl_specifier_seq type_specifiers;
22797 /* First, try to parse as an initialized declaration. See
22798 cp_parser_condition, from whence the bulk of this is copied. */
22800 cp_parser_parse_tentatively (parser);
22801 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22802 /*is_trailing_return=*/false,
22804 if (cp_parser_parse_definitely (parser))
22806 /* If parsing a type specifier seq succeeded, then this
22807 MUST be a initialized declaration. */
22808 tree asm_specification, attributes;
22809 cp_declarator *declarator;
22811 declarator = cp_parser_declarator (parser,
22812 CP_PARSER_DECLARATOR_NAMED,
22813 /*ctor_dtor_or_conv_p=*/NULL,
22814 /*parenthesized_p=*/NULL,
22815 /*member_p=*/false);
22816 attributes = cp_parser_attributes_opt (parser);
22817 asm_specification = cp_parser_asm_specification_opt (parser);
22819 if (declarator == cp_error_declarator)
22820 cp_parser_skip_to_end_of_statement (parser);
22824 tree pushed_scope, auto_node;
22826 decl = start_decl (declarator, &type_specifiers,
22827 SD_INITIALIZED, attributes,
22828 /*prefix_attributes=*/NULL_TREE,
22831 auto_node = type_uses_auto (TREE_TYPE (decl));
22832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22834 if (cp_lexer_next_token_is (parser->lexer,
22836 error ("parenthesized initialization is not allowed in "
22837 "OpenMP %<for%> loop");
22839 /* Trigger an error. */
22840 cp_parser_require (parser, CPP_EQ, RT_EQ);
22842 init = error_mark_node;
22843 cp_parser_skip_to_end_of_statement (parser);
22845 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22846 || type_dependent_expression_p (decl)
22849 bool is_direct_init, is_non_constant_init;
22851 init = cp_parser_initializer (parser,
22853 &is_non_constant_init);
22855 if (auto_node && describable_type (init))
22858 = do_auto_deduction (TREE_TYPE (decl), init,
22861 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22862 && !type_dependent_expression_p (decl))
22866 cp_finish_decl (decl, init, !is_non_constant_init,
22868 LOOKUP_ONLYCONVERTING);
22869 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22871 VEC_safe_push (tree, gc, for_block, this_pre_body);
22875 init = pop_stmt_list (this_pre_body);
22876 this_pre_body = NULL_TREE;
22881 cp_lexer_consume_token (parser->lexer);
22882 init = cp_parser_assignment_expression (parser, false, NULL);
22885 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22886 init = error_mark_node;
22888 cp_finish_decl (decl, NULL_TREE,
22889 /*init_const_expr_p=*/false,
22891 LOOKUP_ONLYCONVERTING);
22895 pop_scope (pushed_scope);
22901 /* If parsing a type specifier sequence failed, then
22902 this MUST be a simple expression. */
22903 cp_parser_parse_tentatively (parser);
22904 decl = cp_parser_primary_expression (parser, false, false,
22906 if (!cp_parser_error_occurred (parser)
22909 && CLASS_TYPE_P (TREE_TYPE (decl)))
22913 cp_parser_parse_definitely (parser);
22914 cp_parser_require (parser, CPP_EQ, RT_EQ);
22915 rhs = cp_parser_assignment_expression (parser, false, NULL);
22916 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22918 tf_warning_or_error));
22919 add_private_clause = true;
22924 cp_parser_abort_tentative_parse (parser);
22925 init = cp_parser_expression (parser, false, NULL);
22928 if (TREE_CODE (init) == MODIFY_EXPR
22929 || TREE_CODE (init) == MODOP_EXPR)
22930 real_decl = TREE_OPERAND (init, 0);
22935 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22938 this_pre_body = pop_stmt_list (this_pre_body);
22942 pre_body = push_stmt_list ();
22944 add_stmt (this_pre_body);
22945 pre_body = pop_stmt_list (pre_body);
22948 pre_body = this_pre_body;
22953 if (par_clauses != NULL && real_decl != NULL_TREE)
22956 for (c = par_clauses; *c ; )
22957 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22958 && OMP_CLAUSE_DECL (*c) == real_decl)
22960 error_at (loc, "iteration variable %qD"
22961 " should not be firstprivate", real_decl);
22962 *c = OMP_CLAUSE_CHAIN (*c);
22964 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22965 && OMP_CLAUSE_DECL (*c) == real_decl)
22967 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22968 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22969 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22970 OMP_CLAUSE_DECL (l) = real_decl;
22971 OMP_CLAUSE_CHAIN (l) = clauses;
22972 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22974 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22975 CP_OMP_CLAUSE_INFO (*c) = NULL;
22976 add_private_clause = false;
22980 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22981 && OMP_CLAUSE_DECL (*c) == real_decl)
22982 add_private_clause = false;
22983 c = &OMP_CLAUSE_CHAIN (*c);
22987 if (add_private_clause)
22990 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22992 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22993 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22994 && OMP_CLAUSE_DECL (c) == decl)
22996 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22997 && OMP_CLAUSE_DECL (c) == decl)
22998 error_at (loc, "iteration variable %qD "
22999 "should not be firstprivate",
23001 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
23002 && OMP_CLAUSE_DECL (c) == decl)
23003 error_at (loc, "iteration variable %qD should not be reduction",
23008 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
23009 OMP_CLAUSE_DECL (c) = decl;
23010 c = finish_omp_clauses (c);
23013 OMP_CLAUSE_CHAIN (c) = clauses;
23020 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23021 cond = cp_parser_omp_for_cond (parser, decl);
23022 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23025 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23027 /* If decl is an iterator, preserve the operator on decl
23028 until finish_omp_for. */
23030 && (type_dependent_expression_p (decl)
23031 || CLASS_TYPE_P (TREE_TYPE (decl))))
23032 incr = cp_parser_omp_for_incr (parser, decl);
23034 incr = cp_parser_expression (parser, false, NULL);
23037 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23038 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23039 /*or_comma=*/false,
23040 /*consume_paren=*/true);
23042 TREE_VEC_ELT (declv, i) = decl;
23043 TREE_VEC_ELT (initv, i) = init;
23044 TREE_VEC_ELT (condv, i) = cond;
23045 TREE_VEC_ELT (incrv, i) = incr;
23047 if (i == collapse - 1)
23050 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
23051 in between the collapsed for loops to be still considered perfectly
23052 nested. Hopefully the final version clarifies this.
23053 For now handle (multiple) {'s and empty statements. */
23054 cp_parser_parse_tentatively (parser);
23057 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23059 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23061 cp_lexer_consume_token (parser->lexer);
23064 else if (bracecount
23065 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23066 cp_lexer_consume_token (parser->lexer);
23069 loc = cp_lexer_peek_token (parser->lexer)->location;
23070 error_at (loc, "not enough collapsed for loops");
23071 collapse_err = true;
23072 cp_parser_abort_tentative_parse (parser);
23081 cp_parser_parse_definitely (parser);
23082 nbraces += bracecount;
23086 /* Note that we saved the original contents of this flag when we entered
23087 the structured block, and so we don't need to re-save it here. */
23088 parser->in_statement = IN_OMP_FOR;
23090 /* Note that the grammar doesn't call for a structured block here,
23091 though the loop as a whole is a structured block. */
23092 body = push_stmt_list ();
23093 cp_parser_statement (parser, NULL_TREE, false, NULL);
23094 body = pop_stmt_list (body);
23096 if (declv == NULL_TREE)
23099 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
23100 pre_body, clauses);
23104 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23106 cp_lexer_consume_token (parser->lexer);
23109 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23110 cp_lexer_consume_token (parser->lexer);
23115 error_at (cp_lexer_peek_token (parser->lexer)->location,
23116 "collapsed loops not perfectly nested");
23118 collapse_err = true;
23119 cp_parser_statement_seq_opt (parser, NULL);
23120 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23125 while (!VEC_empty (tree, for_block))
23126 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
23127 release_tree_vector (for_block);
23133 #pragma omp for for-clause[optseq] new-line
23136 #define OMP_FOR_CLAUSE_MASK \
23137 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23138 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23139 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
23140 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23141 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
23142 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
23143 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
23144 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
23147 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
23149 tree clauses, sb, ret;
23152 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
23153 "#pragma omp for", pragma_tok);
23155 sb = begin_omp_structured_block ();
23156 save = cp_parser_begin_omp_structured_block (parser);
23158 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
23160 cp_parser_end_omp_structured_block (parser, save);
23161 add_stmt (finish_omp_structured_block (sb));
23167 # pragma omp master new-line
23168 structured-block */
23171 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
23173 cp_parser_require_pragma_eol (parser, pragma_tok);
23174 return c_finish_omp_master (input_location,
23175 cp_parser_omp_structured_block (parser));
23179 # pragma omp ordered new-line
23180 structured-block */
23183 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
23185 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23186 cp_parser_require_pragma_eol (parser, pragma_tok);
23187 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
23193 { section-sequence }
23196 section-directive[opt] structured-block
23197 section-sequence section-directive structured-block */
23200 cp_parser_omp_sections_scope (cp_parser *parser)
23202 tree stmt, substmt;
23203 bool error_suppress = false;
23206 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23209 stmt = push_stmt_list ();
23211 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
23215 substmt = begin_omp_structured_block ();
23216 save = cp_parser_begin_omp_structured_block (parser);
23220 cp_parser_statement (parser, NULL_TREE, false, NULL);
23222 tok = cp_lexer_peek_token (parser->lexer);
23223 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23225 if (tok->type == CPP_CLOSE_BRACE)
23227 if (tok->type == CPP_EOF)
23231 cp_parser_end_omp_structured_block (parser, save);
23232 substmt = finish_omp_structured_block (substmt);
23233 substmt = build1 (OMP_SECTION, void_type_node, substmt);
23234 add_stmt (substmt);
23239 tok = cp_lexer_peek_token (parser->lexer);
23240 if (tok->type == CPP_CLOSE_BRACE)
23242 if (tok->type == CPP_EOF)
23245 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23247 cp_lexer_consume_token (parser->lexer);
23248 cp_parser_require_pragma_eol (parser, tok);
23249 error_suppress = false;
23251 else if (!error_suppress)
23253 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
23254 error_suppress = true;
23257 substmt = cp_parser_omp_structured_block (parser);
23258 substmt = build1 (OMP_SECTION, void_type_node, substmt);
23259 add_stmt (substmt);
23261 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23263 substmt = pop_stmt_list (stmt);
23265 stmt = make_node (OMP_SECTIONS);
23266 TREE_TYPE (stmt) = void_type_node;
23267 OMP_SECTIONS_BODY (stmt) = substmt;
23274 # pragma omp sections sections-clause[optseq] newline
23277 #define OMP_SECTIONS_CLAUSE_MASK \
23278 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23279 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23280 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
23281 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23282 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23285 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
23289 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
23290 "#pragma omp sections", pragma_tok);
23292 ret = cp_parser_omp_sections_scope (parser);
23294 OMP_SECTIONS_CLAUSES (ret) = clauses;
23300 # pragma parallel parallel-clause new-line
23301 # pragma parallel for parallel-for-clause new-line
23302 # pragma parallel sections parallel-sections-clause new-line */
23304 #define OMP_PARALLEL_CLAUSE_MASK \
23305 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
23306 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23307 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23308 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
23309 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
23310 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
23311 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23312 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
23315 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
23317 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
23318 const char *p_name = "#pragma omp parallel";
23319 tree stmt, clauses, par_clause, ws_clause, block;
23320 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
23322 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23324 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23326 cp_lexer_consume_token (parser->lexer);
23327 p_kind = PRAGMA_OMP_PARALLEL_FOR;
23328 p_name = "#pragma omp parallel for";
23329 mask |= OMP_FOR_CLAUSE_MASK;
23330 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
23332 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23334 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23335 const char *p = IDENTIFIER_POINTER (id);
23336 if (strcmp (p, "sections") == 0)
23338 cp_lexer_consume_token (parser->lexer);
23339 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
23340 p_name = "#pragma omp parallel sections";
23341 mask |= OMP_SECTIONS_CLAUSE_MASK;
23342 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
23346 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
23347 block = begin_omp_parallel ();
23348 save = cp_parser_begin_omp_structured_block (parser);
23352 case PRAGMA_OMP_PARALLEL:
23353 cp_parser_statement (parser, NULL_TREE, false, NULL);
23354 par_clause = clauses;
23357 case PRAGMA_OMP_PARALLEL_FOR:
23358 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
23359 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
23362 case PRAGMA_OMP_PARALLEL_SECTIONS:
23363 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
23364 stmt = cp_parser_omp_sections_scope (parser);
23366 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
23370 gcc_unreachable ();
23373 cp_parser_end_omp_structured_block (parser, save);
23374 stmt = finish_omp_parallel (par_clause, block);
23375 if (p_kind != PRAGMA_OMP_PARALLEL)
23376 OMP_PARALLEL_COMBINED (stmt) = 1;
23381 # pragma omp single single-clause[optseq] new-line
23382 structured-block */
23384 #define OMP_SINGLE_CLAUSE_MASK \
23385 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23386 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23387 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
23388 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23391 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
23393 tree stmt = make_node (OMP_SINGLE);
23394 TREE_TYPE (stmt) = void_type_node;
23396 OMP_SINGLE_CLAUSES (stmt)
23397 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
23398 "#pragma omp single", pragma_tok);
23399 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
23401 return add_stmt (stmt);
23405 # pragma omp task task-clause[optseq] new-line
23406 structured-block */
23408 #define OMP_TASK_CLAUSE_MASK \
23409 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
23410 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
23411 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
23412 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23413 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23414 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
23417 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
23419 tree clauses, block;
23422 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
23423 "#pragma omp task", pragma_tok);
23424 block = begin_omp_task ();
23425 save = cp_parser_begin_omp_structured_block (parser);
23426 cp_parser_statement (parser, NULL_TREE, false, NULL);
23427 cp_parser_end_omp_structured_block (parser, save);
23428 return finish_omp_task (clauses, block);
23432 # pragma omp taskwait new-line */
23435 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
23437 cp_parser_require_pragma_eol (parser, pragma_tok);
23438 finish_omp_taskwait ();
23442 # pragma omp threadprivate (variable-list) */
23445 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
23449 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23450 cp_parser_require_pragma_eol (parser, pragma_tok);
23452 finish_omp_threadprivate (vars);
23455 /* Main entry point to OpenMP statement pragmas. */
23458 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
23462 switch (pragma_tok->pragma_kind)
23464 case PRAGMA_OMP_ATOMIC:
23465 cp_parser_omp_atomic (parser, pragma_tok);
23467 case PRAGMA_OMP_CRITICAL:
23468 stmt = cp_parser_omp_critical (parser, pragma_tok);
23470 case PRAGMA_OMP_FOR:
23471 stmt = cp_parser_omp_for (parser, pragma_tok);
23473 case PRAGMA_OMP_MASTER:
23474 stmt = cp_parser_omp_master (parser, pragma_tok);
23476 case PRAGMA_OMP_ORDERED:
23477 stmt = cp_parser_omp_ordered (parser, pragma_tok);
23479 case PRAGMA_OMP_PARALLEL:
23480 stmt = cp_parser_omp_parallel (parser, pragma_tok);
23482 case PRAGMA_OMP_SECTIONS:
23483 stmt = cp_parser_omp_sections (parser, pragma_tok);
23485 case PRAGMA_OMP_SINGLE:
23486 stmt = cp_parser_omp_single (parser, pragma_tok);
23488 case PRAGMA_OMP_TASK:
23489 stmt = cp_parser_omp_task (parser, pragma_tok);
23492 gcc_unreachable ();
23496 SET_EXPR_LOCATION (stmt, pragma_tok->location);
23501 static GTY (()) cp_parser *the_parser;
23504 /* Special handling for the first token or line in the file. The first
23505 thing in the file might be #pragma GCC pch_preprocess, which loads a
23506 PCH file, which is a GC collection point. So we need to handle this
23507 first pragma without benefit of an existing lexer structure.
23509 Always returns one token to the caller in *FIRST_TOKEN. This is
23510 either the true first token of the file, or the first token after
23511 the initial pragma. */
23514 cp_parser_initial_pragma (cp_token *first_token)
23518 cp_lexer_get_preprocessor_token (NULL, first_token);
23519 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
23522 cp_lexer_get_preprocessor_token (NULL, first_token);
23523 if (first_token->type == CPP_STRING)
23525 name = first_token->u.value;
23527 cp_lexer_get_preprocessor_token (NULL, first_token);
23528 if (first_token->type != CPP_PRAGMA_EOL)
23529 error_at (first_token->location,
23530 "junk at end of %<#pragma GCC pch_preprocess%>");
23533 error_at (first_token->location, "expected string literal");
23535 /* Skip to the end of the pragma. */
23536 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
23537 cp_lexer_get_preprocessor_token (NULL, first_token);
23539 /* Now actually load the PCH file. */
23541 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
23543 /* Read one more token to return to our caller. We have to do this
23544 after reading the PCH file in, since its pointers have to be
23546 cp_lexer_get_preprocessor_token (NULL, first_token);
23549 /* Normal parsing of a pragma token. Here we can (and must) use the
23553 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
23555 cp_token *pragma_tok;
23558 pragma_tok = cp_lexer_consume_token (parser->lexer);
23559 gcc_assert (pragma_tok->type == CPP_PRAGMA);
23560 parser->lexer->in_pragma = true;
23562 id = pragma_tok->pragma_kind;
23565 case PRAGMA_GCC_PCH_PREPROCESS:
23566 error_at (pragma_tok->location,
23567 "%<#pragma GCC pch_preprocess%> must be first");
23570 case PRAGMA_OMP_BARRIER:
23573 case pragma_compound:
23574 cp_parser_omp_barrier (parser, pragma_tok);
23577 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23578 "used in compound statements");
23585 case PRAGMA_OMP_FLUSH:
23588 case pragma_compound:
23589 cp_parser_omp_flush (parser, pragma_tok);
23592 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23593 "used in compound statements");
23600 case PRAGMA_OMP_TASKWAIT:
23603 case pragma_compound:
23604 cp_parser_omp_taskwait (parser, pragma_tok);
23607 error_at (pragma_tok->location,
23608 "%<#pragma omp taskwait%> may only be "
23609 "used in compound statements");
23616 case PRAGMA_OMP_THREADPRIVATE:
23617 cp_parser_omp_threadprivate (parser, pragma_tok);
23620 case PRAGMA_OMP_ATOMIC:
23621 case PRAGMA_OMP_CRITICAL:
23622 case PRAGMA_OMP_FOR:
23623 case PRAGMA_OMP_MASTER:
23624 case PRAGMA_OMP_ORDERED:
23625 case PRAGMA_OMP_PARALLEL:
23626 case PRAGMA_OMP_SECTIONS:
23627 case PRAGMA_OMP_SINGLE:
23628 case PRAGMA_OMP_TASK:
23629 if (context == pragma_external)
23631 cp_parser_omp_construct (parser, pragma_tok);
23634 case PRAGMA_OMP_SECTION:
23635 error_at (pragma_tok->location,
23636 "%<#pragma omp section%> may only be used in "
23637 "%<#pragma omp sections%> construct");
23641 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23642 c_invoke_pragma_handler (id);
23646 cp_parser_error (parser, "expected declaration specifiers");
23650 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23654 /* The interface the pragma parsers have to the lexer. */
23657 pragma_lex (tree *value)
23660 enum cpp_ttype ret;
23662 tok = cp_lexer_peek_token (the_parser->lexer);
23665 *value = tok->u.value;
23667 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23669 else if (ret == CPP_STRING)
23670 *value = cp_parser_string_literal (the_parser, false, false);
23673 cp_lexer_consume_token (the_parser->lexer);
23674 if (ret == CPP_KEYWORD)
23682 /* External interface. */
23684 /* Parse one entire translation unit. */
23687 c_parse_file (void)
23689 static bool already_called = false;
23691 if (already_called)
23693 sorry ("inter-module optimizations not implemented for C++");
23696 already_called = true;
23698 the_parser = cp_parser_new ();
23699 push_deferring_access_checks (flag_access_control
23700 ? dk_no_deferred : dk_no_check);
23701 cp_parser_translation_unit (the_parser);
23705 #include "gt-cp-parser.h"