2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
33 #include "diagnostic.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 {
153 /* floating-point literal */
157 /* %<__FUNCTION__%> */
159 /* %<__PRETTY_FUNCTION__%> */
167 /* %<typeid%> operator */
169 /* non-constant compound literals */
171 /* a function call */
177 /* an array reference */
183 /* the address of a label */
197 /* calls to overloaded operators */
201 /* a comma operator */
203 /* a call to a constructor */
205 } non_integral_constant;
207 /* The various kinds of errors about name-lookup failing. */
208 typedef enum name_lookup_error {
213 /* is not a class or namespace */
215 /* is not a class, namespace, or enumeration */
219 /* The various kinds of required token */
220 typedef enum required_token {
221 RT_SEMICOLON = 1, /* ';' */
222 RT_OPEN_PAREN, /* '(' */
223 RT_CLOSE_BRACE, /* '}' */
224 RT_OPEN_BRACE, /* '{' */
225 RT_CLOSE_SQUARE, /* ']' */
226 RT_OPEN_SQUARE, /* '[' */
230 RT_GREATER, /* '>' */
232 RT_ELLIPSIS, /* '...' */
236 RT_COLON_SCOPE, /* ':' or '::' */
237 RT_CLOSE_PAREN, /* ')' */
238 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
239 RT_PRAGMA_EOL, /* end of line */
240 RT_NAME, /* identifier */
242 /* The type is CPP_KEYWORD */
244 RT_DELETE, /* delete */
245 RT_RETURN, /* return */
246 RT_WHILE, /* while */
247 RT_EXTERN, /* extern */
248 RT_STATIC_ASSERT, /* static_assert */
249 RT_DECLTYPE, /* decltype */
250 RT_OPERATOR, /* operator */
251 RT_CLASS, /* class */
252 RT_TEMPLATE, /* template */
253 RT_NAMESPACE, /* namespace */
254 RT_USING, /* using */
257 RT_CATCH, /* catch */
258 RT_THROW, /* throw */
259 RT_LABEL, /* __label__ */
260 RT_AT_TRY, /* @try */
261 RT_AT_SYNCHRONIZED, /* @synchronized */
262 RT_AT_THROW, /* @throw */
264 RT_SELECT, /* selection-statement */
265 RT_INTERATION, /* iteration-statement */
266 RT_JUMP, /* jump-statement */
267 RT_CLASS_KEY, /* class-key */
268 RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
273 static cp_lexer *cp_lexer_new_main
275 static cp_lexer *cp_lexer_new_from_tokens
276 (cp_token_cache *tokens);
277 static void cp_lexer_destroy
279 static int cp_lexer_saving_tokens
281 static cp_token_position cp_lexer_token_position
283 static cp_token *cp_lexer_token_at
284 (cp_lexer *, cp_token_position);
285 static void cp_lexer_get_preprocessor_token
286 (cp_lexer *, cp_token *);
287 static inline cp_token *cp_lexer_peek_token
289 static cp_token *cp_lexer_peek_nth_token
290 (cp_lexer *, size_t);
291 static inline bool cp_lexer_next_token_is
292 (cp_lexer *, enum cpp_ttype);
293 static bool cp_lexer_next_token_is_not
294 (cp_lexer *, enum cpp_ttype);
295 static bool cp_lexer_next_token_is_keyword
296 (cp_lexer *, enum rid);
297 static cp_token *cp_lexer_consume_token
299 static void cp_lexer_purge_token
301 static void cp_lexer_purge_tokens_after
302 (cp_lexer *, cp_token_position);
303 static void cp_lexer_save_tokens
305 static void cp_lexer_commit_tokens
307 static void cp_lexer_rollback_tokens
309 #ifdef ENABLE_CHECKING
310 static void cp_lexer_print_token
311 (FILE *, cp_token *);
312 static inline bool cp_lexer_debugging_p
314 static void cp_lexer_start_debugging
315 (cp_lexer *) ATTRIBUTE_UNUSED;
316 static void cp_lexer_stop_debugging
317 (cp_lexer *) ATTRIBUTE_UNUSED;
319 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
320 about passing NULL to functions that require non-NULL arguments
321 (fputs, fprintf). It will never be used, so all we need is a value
322 of the right type that's guaranteed not to be NULL. */
323 #define cp_lexer_debug_stream stdout
324 #define cp_lexer_print_token(str, tok) (void) 0
325 #define cp_lexer_debugging_p(lexer) 0
326 #endif /* ENABLE_CHECKING */
328 static cp_token_cache *cp_token_cache_new
329 (cp_token *, cp_token *);
331 static void cp_parser_initial_pragma
334 /* Manifest constants. */
335 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
336 #define CP_SAVED_TOKEN_STACK 5
338 /* A token type for keywords, as opposed to ordinary identifiers. */
339 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
341 /* A token type for template-ids. If a template-id is processed while
342 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
343 the value of the CPP_TEMPLATE_ID is whatever was returned by
344 cp_parser_template_id. */
345 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
347 /* A token type for nested-name-specifiers. If a
348 nested-name-specifier is processed while parsing tentatively, it is
349 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
350 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
351 cp_parser_nested_name_specifier_opt. */
352 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
354 /* A token type for tokens that are not tokens at all; these are used
355 to represent slots in the array where there used to be a token
356 that has now been deleted. */
357 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
359 /* The number of token types, including C++-specific ones. */
360 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
364 #ifdef ENABLE_CHECKING
365 /* The stream to which debugging output should be written. */
366 static FILE *cp_lexer_debug_stream;
367 #endif /* ENABLE_CHECKING */
369 /* Nonzero if we are parsing an unevaluated operand: an operand to
370 sizeof, typeof, or alignof. */
371 int cp_unevaluated_operand;
373 /* Create a new main C++ lexer, the lexer that gets tokens from the
377 cp_lexer_new_main (void)
379 cp_token first_token;
386 /* It's possible that parsing the first pragma will load a PCH file,
387 which is a GC collection point. So we have to do that before
388 allocating any memory. */
389 cp_parser_initial_pragma (&first_token);
391 c_common_no_more_pch ();
393 /* Allocate the memory. */
394 lexer = GGC_CNEW (cp_lexer);
396 #ifdef ENABLE_CHECKING
397 /* Initially we are not debugging. */
398 lexer->debugging_p = false;
399 #endif /* ENABLE_CHECKING */
400 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
401 CP_SAVED_TOKEN_STACK);
403 /* Create the buffer. */
404 alloc = CP_LEXER_BUFFER_SIZE;
405 buffer = GGC_NEWVEC (cp_token, alloc);
407 /* Put the first token in the buffer. */
412 /* Get the remaining tokens from the preprocessor. */
413 while (pos->type != CPP_EOF)
420 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
421 pos = buffer + space;
423 cp_lexer_get_preprocessor_token (lexer, pos);
425 lexer->buffer = buffer;
426 lexer->buffer_length = alloc - space;
427 lexer->last_token = pos;
428 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
430 /* Subsequent preprocessor diagnostics should use compiler
431 diagnostic functions to get the compiler source location. */
434 gcc_assert (lexer->next_token->type != CPP_PURGED);
438 /* Create a new lexer whose token stream is primed with the tokens in
439 CACHE. When these tokens are exhausted, no new tokens will be read. */
442 cp_lexer_new_from_tokens (cp_token_cache *cache)
444 cp_token *first = cache->first;
445 cp_token *last = cache->last;
446 cp_lexer *lexer = GGC_CNEW (cp_lexer);
448 /* We do not own the buffer. */
449 lexer->buffer = NULL;
450 lexer->buffer_length = 0;
451 lexer->next_token = first == last ? &eof_token : first;
452 lexer->last_token = last;
454 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
455 CP_SAVED_TOKEN_STACK);
457 #ifdef ENABLE_CHECKING
458 /* Initially we are not debugging. */
459 lexer->debugging_p = false;
462 gcc_assert (lexer->next_token->type != CPP_PURGED);
466 /* Frees all resources associated with LEXER. */
469 cp_lexer_destroy (cp_lexer *lexer)
472 ggc_free (lexer->buffer);
473 VEC_free (cp_token_position, heap, lexer->saved_tokens);
477 /* Returns nonzero if debugging information should be output. */
479 #ifdef ENABLE_CHECKING
482 cp_lexer_debugging_p (cp_lexer *lexer)
484 return lexer->debugging_p;
487 #endif /* ENABLE_CHECKING */
489 static inline cp_token_position
490 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
492 gcc_assert (!previous_p || lexer->next_token != &eof_token);
494 return lexer->next_token - previous_p;
497 static inline cp_token *
498 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
503 /* nonzero if we are presently saving tokens. */
506 cp_lexer_saving_tokens (const cp_lexer* lexer)
508 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
511 /* Store the next token from the preprocessor in *TOKEN. Return true
512 if we reach EOF. If LEXER is NULL, assume we are handling an
513 initial #pragma pch_preprocess, and thus want the lexer to return
514 processed strings. */
517 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
519 static int is_extern_c = 0;
521 /* Get a new token from the preprocessor. */
523 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
524 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
525 token->keyword = RID_MAX;
526 token->pragma_kind = PRAGMA_NONE;
528 /* On some systems, some header files are surrounded by an
529 implicit extern "C" block. Set a flag in the token if it
530 comes from such a header. */
531 is_extern_c += pending_lang_change;
532 pending_lang_change = 0;
533 token->implicit_extern_c = is_extern_c > 0;
535 /* Check to see if this token is a keyword. */
536 if (token->type == CPP_NAME)
538 if (C_IS_RESERVED_WORD (token->u.value))
540 /* Mark this token as a keyword. */
541 token->type = CPP_KEYWORD;
542 /* Record which keyword. */
543 token->keyword = C_RID_CODE (token->u.value);
547 if (warn_cxx0x_compat
548 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
549 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
551 /* Warn about the C++0x keyword (but still treat it as
553 warning (OPT_Wc__0x_compat,
554 "identifier %qE will become a keyword in C++0x",
557 /* Clear out the C_RID_CODE so we don't warn about this
558 particular identifier-turned-keyword again. */
559 C_SET_RID_CODE (token->u.value, RID_MAX);
562 token->ambiguous_p = false;
563 token->keyword = RID_MAX;
566 /* Handle Objective-C++ keywords. */
567 else if (token->type == CPP_AT_NAME)
569 token->type = CPP_KEYWORD;
570 switch (C_RID_CODE (token->u.value))
572 /* Map 'class' to '@class', 'private' to '@private', etc. */
573 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
574 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
575 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
576 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
577 case RID_THROW: token->keyword = RID_AT_THROW; break;
578 case RID_TRY: token->keyword = RID_AT_TRY; break;
579 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
580 default: token->keyword = C_RID_CODE (token->u.value);
583 else if (token->type == CPP_PRAGMA)
585 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
586 token->pragma_kind = ((enum pragma_kind)
587 TREE_INT_CST_LOW (token->u.value));
588 token->u.value = NULL_TREE;
592 /* Update the globals input_location and the input file stack from TOKEN. */
594 cp_lexer_set_source_position_from_token (cp_token *token)
596 if (token->type != CPP_EOF)
598 input_location = token->location;
602 /* Return a pointer to the next token in the token stream, but do not
605 static inline cp_token *
606 cp_lexer_peek_token (cp_lexer *lexer)
608 if (cp_lexer_debugging_p (lexer))
610 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
611 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
612 putc ('\n', cp_lexer_debug_stream);
614 return lexer->next_token;
617 /* Return true if the next token has the indicated TYPE. */
620 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
622 return cp_lexer_peek_token (lexer)->type == type;
625 /* Return true if the next token does not have the indicated TYPE. */
628 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
630 return !cp_lexer_next_token_is (lexer, type);
633 /* Return true if the next token is the indicated KEYWORD. */
636 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
638 return cp_lexer_peek_token (lexer)->keyword == keyword;
641 /* Return true if the next token is not the indicated KEYWORD. */
644 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
646 return cp_lexer_peek_token (lexer)->keyword != keyword;
649 /* Return true if the next token is a keyword for a decl-specifier. */
652 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
656 token = cp_lexer_peek_token (lexer);
657 switch (token->keyword)
659 /* auto specifier: storage-class-specifier in C++,
660 simple-type-specifier in C++0x. */
662 /* Storage classes. */
668 /* Elaborated type specifiers. */
674 /* Simple type specifiers. */
688 /* GNU extensions. */
691 /* C++0x extensions. */
700 /* Return a pointer to the Nth token in the token stream. If N is 1,
701 then this is precisely equivalent to cp_lexer_peek_token (except
702 that it is not inline). One would like to disallow that case, but
703 there is one case (cp_parser_nth_token_starts_template_id) where
704 the caller passes a variable for N and it might be 1. */
707 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
711 /* N is 1-based, not zero-based. */
714 if (cp_lexer_debugging_p (lexer))
715 fprintf (cp_lexer_debug_stream,
716 "cp_lexer: peeking ahead %ld at token: ", (long)n);
719 token = lexer->next_token;
720 gcc_assert (!n || token != &eof_token);
724 if (token == lexer->last_token)
730 if (token->type != CPP_PURGED)
734 if (cp_lexer_debugging_p (lexer))
736 cp_lexer_print_token (cp_lexer_debug_stream, token);
737 putc ('\n', cp_lexer_debug_stream);
743 /* Return the next token, and advance the lexer's next_token pointer
744 to point to the next non-purged token. */
747 cp_lexer_consume_token (cp_lexer* lexer)
749 cp_token *token = lexer->next_token;
751 gcc_assert (token != &eof_token);
752 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
757 if (lexer->next_token == lexer->last_token)
759 lexer->next_token = &eof_token;
764 while (lexer->next_token->type == CPP_PURGED);
766 cp_lexer_set_source_position_from_token (token);
768 /* Provide debugging output. */
769 if (cp_lexer_debugging_p (lexer))
771 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
772 cp_lexer_print_token (cp_lexer_debug_stream, token);
773 putc ('\n', cp_lexer_debug_stream);
779 /* Permanently remove the next token from the token stream, and
780 advance the next_token pointer to refer to the next non-purged
784 cp_lexer_purge_token (cp_lexer *lexer)
786 cp_token *tok = lexer->next_token;
788 gcc_assert (tok != &eof_token);
789 tok->type = CPP_PURGED;
790 tok->location = UNKNOWN_LOCATION;
791 tok->u.value = NULL_TREE;
792 tok->keyword = RID_MAX;
797 if (tok == lexer->last_token)
803 while (tok->type == CPP_PURGED);
804 lexer->next_token = tok;
807 /* Permanently remove all tokens after TOK, up to, but not
808 including, the token that will be returned next by
809 cp_lexer_peek_token. */
812 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
814 cp_token *peek = lexer->next_token;
816 if (peek == &eof_token)
817 peek = lexer->last_token;
819 gcc_assert (tok < peek);
821 for ( tok += 1; tok != peek; tok += 1)
823 tok->type = CPP_PURGED;
824 tok->location = UNKNOWN_LOCATION;
825 tok->u.value = NULL_TREE;
826 tok->keyword = RID_MAX;
830 /* Begin saving tokens. All tokens consumed after this point will be
834 cp_lexer_save_tokens (cp_lexer* lexer)
836 /* Provide debugging output. */
837 if (cp_lexer_debugging_p (lexer))
838 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
840 VEC_safe_push (cp_token_position, heap,
841 lexer->saved_tokens, lexer->next_token);
844 /* Commit to the portion of the token stream most recently saved. */
847 cp_lexer_commit_tokens (cp_lexer* lexer)
849 /* Provide debugging output. */
850 if (cp_lexer_debugging_p (lexer))
851 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
853 VEC_pop (cp_token_position, lexer->saved_tokens);
856 /* Return all tokens saved since the last call to cp_lexer_save_tokens
857 to the token stream. Stop saving tokens. */
860 cp_lexer_rollback_tokens (cp_lexer* lexer)
862 /* Provide debugging output. */
863 if (cp_lexer_debugging_p (lexer))
864 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
866 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
869 /* Print a representation of the TOKEN on the STREAM. */
871 #ifdef ENABLE_CHECKING
874 cp_lexer_print_token (FILE * stream, cp_token *token)
876 /* We don't use cpp_type2name here because the parser defines
877 a few tokens of its own. */
878 static const char *const token_names[] = {
879 /* cpplib-defined token types */
885 /* C++ parser token types - see "Manifest constants", above. */
888 "NESTED_NAME_SPECIFIER",
892 /* If we have a name for the token, print it out. Otherwise, we
893 simply give the numeric code. */
894 gcc_assert (token->type < ARRAY_SIZE(token_names));
895 fputs (token_names[token->type], stream);
897 /* For some tokens, print the associated data. */
901 /* Some keywords have a value that is not an IDENTIFIER_NODE.
902 For example, `struct' is mapped to an INTEGER_CST. */
903 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
905 /* else fall through */
907 fputs (IDENTIFIER_POINTER (token->u.value), stream);
915 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
923 /* Start emitting debugging information. */
926 cp_lexer_start_debugging (cp_lexer* lexer)
928 lexer->debugging_p = true;
931 /* Stop emitting debugging information. */
934 cp_lexer_stop_debugging (cp_lexer* lexer)
936 lexer->debugging_p = false;
939 #endif /* ENABLE_CHECKING */
941 /* Create a new cp_token_cache, representing a range of tokens. */
943 static cp_token_cache *
944 cp_token_cache_new (cp_token *first, cp_token *last)
946 cp_token_cache *cache = GGC_NEW (cp_token_cache);
947 cache->first = first;
953 /* Decl-specifiers. */
955 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
958 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
960 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
965 /* Nothing other than the parser should be creating declarators;
966 declarators are a semi-syntactic representation of C++ entities.
967 Other parts of the front end that need to create entities (like
968 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
970 static cp_declarator *make_call_declarator
971 (cp_declarator *, tree, cp_cv_quals, tree, tree);
972 static cp_declarator *make_array_declarator
973 (cp_declarator *, tree);
974 static cp_declarator *make_pointer_declarator
975 (cp_cv_quals, cp_declarator *);
976 static cp_declarator *make_reference_declarator
977 (cp_cv_quals, cp_declarator *, bool);
978 static cp_parameter_declarator *make_parameter_declarator
979 (cp_decl_specifier_seq *, cp_declarator *, tree);
980 static cp_declarator *make_ptrmem_declarator
981 (cp_cv_quals, tree, cp_declarator *);
983 /* An erroneous declarator. */
984 static cp_declarator *cp_error_declarator;
986 /* The obstack on which declarators and related data structures are
988 static struct obstack declarator_obstack;
990 /* Alloc BYTES from the declarator memory pool. */
993 alloc_declarator (size_t bytes)
995 return obstack_alloc (&declarator_obstack, bytes);
998 /* Allocate a declarator of the indicated KIND. Clear fields that are
999 common to all declarators. */
1001 static cp_declarator *
1002 make_declarator (cp_declarator_kind kind)
1004 cp_declarator *declarator;
1006 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1007 declarator->kind = kind;
1008 declarator->attributes = NULL_TREE;
1009 declarator->declarator = NULL;
1010 declarator->parameter_pack_p = false;
1011 declarator->id_loc = UNKNOWN_LOCATION;
1016 /* Make a declarator for a generalized identifier. If
1017 QUALIFYING_SCOPE is non-NULL, the identifier is
1018 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1019 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1022 static cp_declarator *
1023 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1024 special_function_kind sfk)
1026 cp_declarator *declarator;
1028 /* It is valid to write:
1030 class C { void f(); };
1034 The standard is not clear about whether `typedef const C D' is
1035 legal; as of 2002-09-15 the committee is considering that
1036 question. EDG 3.0 allows that syntax. Therefore, we do as
1038 if (qualifying_scope && TYPE_P (qualifying_scope))
1039 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1041 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1042 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1043 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1045 declarator = make_declarator (cdk_id);
1046 declarator->u.id.qualifying_scope = qualifying_scope;
1047 declarator->u.id.unqualified_name = unqualified_name;
1048 declarator->u.id.sfk = sfk;
1053 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1054 of modifiers such as const or volatile to apply to the pointer
1055 type, represented as identifiers. */
1058 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1060 cp_declarator *declarator;
1062 declarator = make_declarator (cdk_pointer);
1063 declarator->declarator = target;
1064 declarator->u.pointer.qualifiers = cv_qualifiers;
1065 declarator->u.pointer.class_type = NULL_TREE;
1068 declarator->parameter_pack_p = target->parameter_pack_p;
1069 target->parameter_pack_p = false;
1072 declarator->parameter_pack_p = false;
1077 /* Like make_pointer_declarator -- but for references. */
1080 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1083 cp_declarator *declarator;
1085 declarator = make_declarator (cdk_reference);
1086 declarator->declarator = target;
1087 declarator->u.reference.qualifiers = cv_qualifiers;
1088 declarator->u.reference.rvalue_ref = rvalue_ref;
1091 declarator->parameter_pack_p = target->parameter_pack_p;
1092 target->parameter_pack_p = false;
1095 declarator->parameter_pack_p = false;
1100 /* Like make_pointer_declarator -- but for a pointer to a non-static
1101 member of CLASS_TYPE. */
1104 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1105 cp_declarator *pointee)
1107 cp_declarator *declarator;
1109 declarator = make_declarator (cdk_ptrmem);
1110 declarator->declarator = pointee;
1111 declarator->u.pointer.qualifiers = cv_qualifiers;
1112 declarator->u.pointer.class_type = class_type;
1116 declarator->parameter_pack_p = pointee->parameter_pack_p;
1117 pointee->parameter_pack_p = false;
1120 declarator->parameter_pack_p = false;
1125 /* Make a declarator for the function given by TARGET, with the
1126 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1127 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1128 indicates what exceptions can be thrown. */
1131 make_call_declarator (cp_declarator *target,
1133 cp_cv_quals cv_qualifiers,
1134 tree exception_specification,
1135 tree late_return_type)
1137 cp_declarator *declarator;
1139 declarator = make_declarator (cdk_function);
1140 declarator->declarator = target;
1141 declarator->u.function.parameters = parms;
1142 declarator->u.function.qualifiers = cv_qualifiers;
1143 declarator->u.function.exception_specification = exception_specification;
1144 declarator->u.function.late_return_type = late_return_type;
1147 declarator->parameter_pack_p = target->parameter_pack_p;
1148 target->parameter_pack_p = false;
1151 declarator->parameter_pack_p = false;
1156 /* Make a declarator for an array of BOUNDS elements, each of which is
1157 defined by ELEMENT. */
1160 make_array_declarator (cp_declarator *element, tree bounds)
1162 cp_declarator *declarator;
1164 declarator = make_declarator (cdk_array);
1165 declarator->declarator = element;
1166 declarator->u.array.bounds = bounds;
1169 declarator->parameter_pack_p = element->parameter_pack_p;
1170 element->parameter_pack_p = false;
1173 declarator->parameter_pack_p = false;
1178 /* Determine whether the declarator we've seen so far can be a
1179 parameter pack, when followed by an ellipsis. */
1181 declarator_can_be_parameter_pack (cp_declarator *declarator)
1183 /* Search for a declarator name, or any other declarator that goes
1184 after the point where the ellipsis could appear in a parameter
1185 pack. If we find any of these, then this declarator can not be
1186 made into a parameter pack. */
1188 while (declarator && !found)
1190 switch ((int)declarator->kind)
1201 declarator = declarator->declarator;
1209 cp_parameter_declarator *no_parameters;
1211 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1212 DECLARATOR and DEFAULT_ARGUMENT. */
1214 cp_parameter_declarator *
1215 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1216 cp_declarator *declarator,
1217 tree default_argument)
1219 cp_parameter_declarator *parameter;
1221 parameter = ((cp_parameter_declarator *)
1222 alloc_declarator (sizeof (cp_parameter_declarator)));
1223 parameter->next = NULL;
1224 if (decl_specifiers)
1225 parameter->decl_specifiers = *decl_specifiers;
1227 clear_decl_specs (¶meter->decl_specifiers);
1228 parameter->declarator = declarator;
1229 parameter->default_argument = default_argument;
1230 parameter->ellipsis_p = false;
1235 /* Returns true iff DECLARATOR is a declaration for a function. */
1238 function_declarator_p (const cp_declarator *declarator)
1242 if (declarator->kind == cdk_function
1243 && declarator->declarator->kind == cdk_id)
1245 if (declarator->kind == cdk_id
1246 || declarator->kind == cdk_error)
1248 declarator = declarator->declarator;
1258 A cp_parser parses the token stream as specified by the C++
1259 grammar. Its job is purely parsing, not semantic analysis. For
1260 example, the parser breaks the token stream into declarators,
1261 expressions, statements, and other similar syntactic constructs.
1262 It does not check that the types of the expressions on either side
1263 of an assignment-statement are compatible, or that a function is
1264 not declared with a parameter of type `void'.
1266 The parser invokes routines elsewhere in the compiler to perform
1267 semantic analysis and to build up the abstract syntax tree for the
1270 The parser (and the template instantiation code, which is, in a
1271 way, a close relative of parsing) are the only parts of the
1272 compiler that should be calling push_scope and pop_scope, or
1273 related functions. The parser (and template instantiation code)
1274 keeps track of what scope is presently active; everything else
1275 should simply honor that. (The code that generates static
1276 initializers may also need to set the scope, in order to check
1277 access control correctly when emitting the initializers.)
1282 The parser is of the standard recursive-descent variety. Upcoming
1283 tokens in the token stream are examined in order to determine which
1284 production to use when parsing a non-terminal. Some C++ constructs
1285 require arbitrary look ahead to disambiguate. For example, it is
1286 impossible, in the general case, to tell whether a statement is an
1287 expression or declaration without scanning the entire statement.
1288 Therefore, the parser is capable of "parsing tentatively." When the
1289 parser is not sure what construct comes next, it enters this mode.
1290 Then, while we attempt to parse the construct, the parser queues up
1291 error messages, rather than issuing them immediately, and saves the
1292 tokens it consumes. If the construct is parsed successfully, the
1293 parser "commits", i.e., it issues any queued error messages and
1294 the tokens that were being preserved are permanently discarded.
1295 If, however, the construct is not parsed successfully, the parser
1296 rolls back its state completely so that it can resume parsing using
1297 a different alternative.
1302 The performance of the parser could probably be improved substantially.
1303 We could often eliminate the need to parse tentatively by looking ahead
1304 a little bit. In some places, this approach might not entirely eliminate
1305 the need to parse tentatively, but it might still speed up the average
1308 /* Flags that are passed to some parsing functions. These values can
1309 be bitwise-ored together. */
1314 CP_PARSER_FLAGS_NONE = 0x0,
1315 /* The construct is optional. If it is not present, then no error
1316 should be issued. */
1317 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1318 /* When parsing a type-specifier, treat user-defined type-names
1319 as non-type identifiers. */
1320 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1321 /* When parsing a type-specifier, do not try to parse a class-specifier
1322 or enum-specifier. */
1323 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1326 /* This type is used for parameters and variables which hold
1327 combinations of the above flags. */
1328 typedef int cp_parser_flags;
1330 /* The different kinds of declarators we want to parse. */
1332 typedef enum cp_parser_declarator_kind
1334 /* We want an abstract declarator. */
1335 CP_PARSER_DECLARATOR_ABSTRACT,
1336 /* We want a named declarator. */
1337 CP_PARSER_DECLARATOR_NAMED,
1338 /* We don't mind, but the name must be an unqualified-id. */
1339 CP_PARSER_DECLARATOR_EITHER
1340 } cp_parser_declarator_kind;
1342 /* The precedence values used to parse binary expressions. The minimum value
1343 of PREC must be 1, because zero is reserved to quickly discriminate
1344 binary operators from other tokens. */
1349 PREC_LOGICAL_OR_EXPRESSION,
1350 PREC_LOGICAL_AND_EXPRESSION,
1351 PREC_INCLUSIVE_OR_EXPRESSION,
1352 PREC_EXCLUSIVE_OR_EXPRESSION,
1353 PREC_AND_EXPRESSION,
1354 PREC_EQUALITY_EXPRESSION,
1355 PREC_RELATIONAL_EXPRESSION,
1356 PREC_SHIFT_EXPRESSION,
1357 PREC_ADDITIVE_EXPRESSION,
1358 PREC_MULTIPLICATIVE_EXPRESSION,
1360 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1363 /* A mapping from a token type to a corresponding tree node type, with a
1364 precedence value. */
1366 typedef struct cp_parser_binary_operations_map_node
1368 /* The token type. */
1369 enum cpp_ttype token_type;
1370 /* The corresponding tree code. */
1371 enum tree_code tree_type;
1372 /* The precedence of this operator. */
1373 enum cp_parser_prec prec;
1374 } cp_parser_binary_operations_map_node;
1376 /* The status of a tentative parse. */
1378 typedef enum cp_parser_status_kind
1380 /* No errors have occurred. */
1381 CP_PARSER_STATUS_KIND_NO_ERROR,
1382 /* An error has occurred. */
1383 CP_PARSER_STATUS_KIND_ERROR,
1384 /* We are committed to this tentative parse, whether or not an error
1386 CP_PARSER_STATUS_KIND_COMMITTED
1387 } cp_parser_status_kind;
1389 typedef struct cp_parser_expression_stack_entry
1391 /* Left hand side of the binary operation we are currently
1394 /* Original tree code for left hand side, if it was a binary
1395 expression itself (used for -Wparentheses). */
1396 enum tree_code lhs_type;
1397 /* Tree code for the binary operation we are parsing. */
1398 enum tree_code tree_type;
1399 /* Precedence of the binary operation we are parsing. */
1400 enum cp_parser_prec prec;
1401 } cp_parser_expression_stack_entry;
1403 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1404 entries because precedence levels on the stack are monotonically
1406 typedef struct cp_parser_expression_stack_entry
1407 cp_parser_expression_stack[NUM_PREC_VALUES];
1409 /* Context that is saved and restored when parsing tentatively. */
1410 typedef struct GTY (()) cp_parser_context {
1411 /* If this is a tentative parsing context, the status of the
1413 enum cp_parser_status_kind status;
1414 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1415 that are looked up in this context must be looked up both in the
1416 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1417 the context of the containing expression. */
1420 /* The next parsing context in the stack. */
1421 struct cp_parser_context *next;
1422 } cp_parser_context;
1426 /* Constructors and destructors. */
1428 static cp_parser_context *cp_parser_context_new
1429 (cp_parser_context *);
1431 /* Class variables. */
1433 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1435 /* The operator-precedence table used by cp_parser_binary_expression.
1436 Transformed into an associative array (binops_by_token) by
1439 static const cp_parser_binary_operations_map_node binops[] = {
1440 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1441 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1443 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1444 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1445 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1447 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1448 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1450 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1451 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1453 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1454 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1455 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1456 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1458 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1459 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1461 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1463 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1465 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1467 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1469 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1472 /* The same as binops, but initialized by cp_parser_new so that
1473 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1475 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1477 /* Constructors and destructors. */
1479 /* Construct a new context. The context below this one on the stack
1480 is given by NEXT. */
1482 static cp_parser_context *
1483 cp_parser_context_new (cp_parser_context* next)
1485 cp_parser_context *context;
1487 /* Allocate the storage. */
1488 if (cp_parser_context_free_list != NULL)
1490 /* Pull the first entry from the free list. */
1491 context = cp_parser_context_free_list;
1492 cp_parser_context_free_list = context->next;
1493 memset (context, 0, sizeof (*context));
1496 context = GGC_CNEW (cp_parser_context);
1498 /* No errors have occurred yet in this context. */
1499 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1500 /* If this is not the bottommost context, copy information that we
1501 need from the previous context. */
1504 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1505 expression, then we are parsing one in this context, too. */
1506 context->object_type = next->object_type;
1507 /* Thread the stack. */
1508 context->next = next;
1514 /* The cp_parser structure represents the C++ parser. */
1516 typedef struct GTY(()) cp_parser {
1517 /* The lexer from which we are obtaining tokens. */
1520 /* The scope in which names should be looked up. If NULL_TREE, then
1521 we look up names in the scope that is currently open in the
1522 source program. If non-NULL, this is either a TYPE or
1523 NAMESPACE_DECL for the scope in which we should look. It can
1524 also be ERROR_MARK, when we've parsed a bogus scope.
1526 This value is not cleared automatically after a name is looked
1527 up, so we must be careful to clear it before starting a new look
1528 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1529 will look up `Z' in the scope of `X', rather than the current
1530 scope.) Unfortunately, it is difficult to tell when name lookup
1531 is complete, because we sometimes peek at a token, look it up,
1532 and then decide not to consume it. */
1535 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1536 last lookup took place. OBJECT_SCOPE is used if an expression
1537 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1538 respectively. QUALIFYING_SCOPE is used for an expression of the
1539 form "X::Y"; it refers to X. */
1541 tree qualifying_scope;
1543 /* A stack of parsing contexts. All but the bottom entry on the
1544 stack will be tentative contexts.
1546 We parse tentatively in order to determine which construct is in
1547 use in some situations. For example, in order to determine
1548 whether a statement is an expression-statement or a
1549 declaration-statement we parse it tentatively as a
1550 declaration-statement. If that fails, we then reparse the same
1551 token stream as an expression-statement. */
1552 cp_parser_context *context;
1554 /* True if we are parsing GNU C++. If this flag is not set, then
1555 GNU extensions are not recognized. */
1556 bool allow_gnu_extensions_p;
1558 /* TRUE if the `>' token should be interpreted as the greater-than
1559 operator. FALSE if it is the end of a template-id or
1560 template-parameter-list. In C++0x mode, this flag also applies to
1561 `>>' tokens, which are viewed as two consecutive `>' tokens when
1562 this flag is FALSE. */
1563 bool greater_than_is_operator_p;
1565 /* TRUE if default arguments are allowed within a parameter list
1566 that starts at this point. FALSE if only a gnu extension makes
1567 them permissible. */
1568 bool default_arg_ok_p;
1570 /* TRUE if we are parsing an integral constant-expression. See
1571 [expr.const] for a precise definition. */
1572 bool integral_constant_expression_p;
1574 /* TRUE if we are parsing an integral constant-expression -- but a
1575 non-constant expression should be permitted as well. This flag
1576 is used when parsing an array bound so that GNU variable-length
1577 arrays are tolerated. */
1578 bool allow_non_integral_constant_expression_p;
1580 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1581 been seen that makes the expression non-constant. */
1582 bool non_integral_constant_expression_p;
1584 /* TRUE if local variable names and `this' are forbidden in the
1586 bool local_variables_forbidden_p;
1588 /* TRUE if the declaration we are parsing is part of a
1589 linkage-specification of the form `extern string-literal
1591 bool in_unbraced_linkage_specification_p;
1593 /* TRUE if we are presently parsing a declarator, after the
1594 direct-declarator. */
1595 bool in_declarator_p;
1597 /* TRUE if we are presently parsing a template-argument-list. */
1598 bool in_template_argument_list_p;
1600 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1601 to IN_OMP_BLOCK if parsing OpenMP structured block and
1602 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1603 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1604 iteration-statement, OpenMP block or loop within that switch. */
1605 #define IN_SWITCH_STMT 1
1606 #define IN_ITERATION_STMT 2
1607 #define IN_OMP_BLOCK 4
1608 #define IN_OMP_FOR 8
1609 #define IN_IF_STMT 16
1610 unsigned char in_statement;
1612 /* TRUE if we are presently parsing the body of a switch statement.
1613 Note that this doesn't quite overlap with in_statement above.
1614 The difference relates to giving the right sets of error messages:
1615 "case not in switch" vs "break statement used with OpenMP...". */
1616 bool in_switch_statement_p;
1618 /* TRUE if we are parsing a type-id in an expression context. In
1619 such a situation, both "type (expr)" and "type (type)" are valid
1621 bool in_type_id_in_expr_p;
1623 /* TRUE if we are currently in a header file where declarations are
1624 implicitly extern "C". */
1625 bool implicit_extern_c;
1627 /* TRUE if strings in expressions should be translated to the execution
1629 bool translate_strings_p;
1631 /* TRUE if we are presently parsing the body of a function, but not
1633 bool in_function_body;
1635 /* If non-NULL, then we are parsing a construct where new type
1636 definitions are not permitted. The string stored here will be
1637 issued as an error message if a type is defined. */
1638 const char *type_definition_forbidden_message;
1640 /* A list of lists. The outer list is a stack, used for member
1641 functions of local classes. At each level there are two sub-list,
1642 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1643 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1644 TREE_VALUE's. The functions are chained in reverse declaration
1647 The TREE_PURPOSE sublist contains those functions with default
1648 arguments that need post processing, and the TREE_VALUE sublist
1649 contains those functions with definitions that need post
1652 These lists can only be processed once the outermost class being
1653 defined is complete. */
1654 tree unparsed_functions_queues;
1656 /* The number of classes whose definitions are currently in
1658 unsigned num_classes_being_defined;
1660 /* The number of template parameter lists that apply directly to the
1661 current declaration. */
1662 unsigned num_template_parameter_lists;
1667 /* Constructors and destructors. */
1669 static cp_parser *cp_parser_new
1672 /* Routines to parse various constructs.
1674 Those that return `tree' will return the error_mark_node (rather
1675 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1676 Sometimes, they will return an ordinary node if error-recovery was
1677 attempted, even though a parse error occurred. So, to check
1678 whether or not a parse error occurred, you should always use
1679 cp_parser_error_occurred. If the construct is optional (indicated
1680 either by an `_opt' in the name of the function that does the
1681 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1682 the construct is not present. */
1684 /* Lexical conventions [gram.lex] */
1686 static tree cp_parser_identifier
1688 static tree cp_parser_string_literal
1689 (cp_parser *, bool, bool);
1691 /* Basic concepts [gram.basic] */
1693 static bool cp_parser_translation_unit
1696 /* Expressions [gram.expr] */
1698 static tree cp_parser_primary_expression
1699 (cp_parser *, bool, bool, bool, cp_id_kind *);
1700 static tree cp_parser_id_expression
1701 (cp_parser *, bool, bool, bool *, bool, bool);
1702 static tree cp_parser_unqualified_id
1703 (cp_parser *, bool, bool, bool, bool);
1704 static tree cp_parser_nested_name_specifier_opt
1705 (cp_parser *, bool, bool, bool, bool);
1706 static tree cp_parser_nested_name_specifier
1707 (cp_parser *, bool, bool, bool, bool);
1708 static tree cp_parser_qualifying_entity
1709 (cp_parser *, bool, bool, bool, bool, bool);
1710 static tree cp_parser_postfix_expression
1711 (cp_parser *, bool, bool, bool, cp_id_kind *);
1712 static tree cp_parser_postfix_open_square_expression
1713 (cp_parser *, tree, bool);
1714 static tree cp_parser_postfix_dot_deref_expression
1715 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1716 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1717 (cp_parser *, int, bool, bool, bool *);
1718 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1719 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1720 static void cp_parser_pseudo_destructor_name
1721 (cp_parser *, tree *, tree *);
1722 static tree cp_parser_unary_expression
1723 (cp_parser *, bool, bool, cp_id_kind *);
1724 static enum tree_code cp_parser_unary_operator
1726 static tree cp_parser_new_expression
1728 static VEC(tree,gc) *cp_parser_new_placement
1730 static tree cp_parser_new_type_id
1731 (cp_parser *, tree *);
1732 static cp_declarator *cp_parser_new_declarator_opt
1734 static cp_declarator *cp_parser_direct_new_declarator
1736 static VEC(tree,gc) *cp_parser_new_initializer
1738 static tree cp_parser_delete_expression
1740 static tree cp_parser_cast_expression
1741 (cp_parser *, bool, bool, cp_id_kind *);
1742 static tree cp_parser_binary_expression
1743 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1744 static tree cp_parser_question_colon_clause
1745 (cp_parser *, tree);
1746 static tree cp_parser_assignment_expression
1747 (cp_parser *, bool, cp_id_kind *);
1748 static enum tree_code cp_parser_assignment_operator_opt
1750 static tree cp_parser_expression
1751 (cp_parser *, bool, cp_id_kind *);
1752 static tree cp_parser_constant_expression
1753 (cp_parser *, bool, bool *);
1754 static tree cp_parser_builtin_offsetof
1756 static tree cp_parser_lambda_expression
1758 static void cp_parser_lambda_introducer
1759 (cp_parser *, tree);
1760 static void cp_parser_lambda_declarator_opt
1761 (cp_parser *, tree);
1762 static void cp_parser_lambda_body
1763 (cp_parser *, tree);
1765 /* Statements [gram.stmt.stmt] */
1767 static void cp_parser_statement
1768 (cp_parser *, tree, bool, bool *);
1769 static void cp_parser_label_for_labeled_statement
1771 static tree cp_parser_expression_statement
1772 (cp_parser *, tree);
1773 static tree cp_parser_compound_statement
1774 (cp_parser *, tree, bool);
1775 static void cp_parser_statement_seq_opt
1776 (cp_parser *, tree);
1777 static tree cp_parser_selection_statement
1778 (cp_parser *, bool *);
1779 static tree cp_parser_condition
1781 static tree cp_parser_iteration_statement
1783 static void cp_parser_for_init_statement
1785 static tree cp_parser_jump_statement
1787 static void cp_parser_declaration_statement
1790 static tree cp_parser_implicitly_scoped_statement
1791 (cp_parser *, bool *);
1792 static void cp_parser_already_scoped_statement
1795 /* Declarations [gram.dcl.dcl] */
1797 static void cp_parser_declaration_seq_opt
1799 static void cp_parser_declaration
1801 static void cp_parser_block_declaration
1802 (cp_parser *, bool);
1803 static void cp_parser_simple_declaration
1804 (cp_parser *, bool);
1805 static void cp_parser_decl_specifier_seq
1806 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1807 static tree cp_parser_storage_class_specifier_opt
1809 static tree cp_parser_function_specifier_opt
1810 (cp_parser *, cp_decl_specifier_seq *);
1811 static tree cp_parser_type_specifier
1812 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1814 static tree cp_parser_simple_type_specifier
1815 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1816 static tree cp_parser_type_name
1818 static tree cp_parser_nonclass_name
1819 (cp_parser* parser);
1820 static tree cp_parser_elaborated_type_specifier
1821 (cp_parser *, bool, bool);
1822 static tree cp_parser_enum_specifier
1824 static void cp_parser_enumerator_list
1825 (cp_parser *, tree);
1826 static void cp_parser_enumerator_definition
1827 (cp_parser *, tree);
1828 static tree cp_parser_namespace_name
1830 static void cp_parser_namespace_definition
1832 static void cp_parser_namespace_body
1834 static tree cp_parser_qualified_namespace_specifier
1836 static void cp_parser_namespace_alias_definition
1838 static bool cp_parser_using_declaration
1839 (cp_parser *, bool);
1840 static void cp_parser_using_directive
1842 static void cp_parser_asm_definition
1844 static void cp_parser_linkage_specification
1846 static void cp_parser_static_assert
1847 (cp_parser *, bool);
1848 static tree cp_parser_decltype
1851 /* Declarators [gram.dcl.decl] */
1853 static tree cp_parser_init_declarator
1854 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1855 static cp_declarator *cp_parser_declarator
1856 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1857 static cp_declarator *cp_parser_direct_declarator
1858 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1859 static enum tree_code cp_parser_ptr_operator
1860 (cp_parser *, tree *, cp_cv_quals *);
1861 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1863 static tree cp_parser_late_return_type_opt
1865 static tree cp_parser_declarator_id
1866 (cp_parser *, bool);
1867 static tree cp_parser_type_id
1869 static tree cp_parser_template_type_arg
1871 static tree cp_parser_trailing_type_id (cp_parser *);
1872 static tree cp_parser_type_id_1
1873 (cp_parser *, bool, bool);
1874 static void cp_parser_type_specifier_seq
1875 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1876 static tree cp_parser_parameter_declaration_clause
1878 static tree cp_parser_parameter_declaration_list
1879 (cp_parser *, bool *);
1880 static cp_parameter_declarator *cp_parser_parameter_declaration
1881 (cp_parser *, bool, bool *);
1882 static tree cp_parser_default_argument
1883 (cp_parser *, bool);
1884 static void cp_parser_function_body
1886 static tree cp_parser_initializer
1887 (cp_parser *, bool *, bool *);
1888 static tree cp_parser_initializer_clause
1889 (cp_parser *, bool *);
1890 static tree cp_parser_braced_list
1891 (cp_parser*, bool*);
1892 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1893 (cp_parser *, bool *);
1895 static bool cp_parser_ctor_initializer_opt_and_function_body
1898 /* Classes [gram.class] */
1900 static tree cp_parser_class_name
1901 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1902 static tree cp_parser_class_specifier
1904 static tree cp_parser_class_head
1905 (cp_parser *, bool *, tree *, tree *);
1906 static enum tag_types cp_parser_class_key
1908 static void cp_parser_member_specification_opt
1910 static void cp_parser_member_declaration
1912 static tree cp_parser_pure_specifier
1914 static tree cp_parser_constant_initializer
1917 /* Derived classes [gram.class.derived] */
1919 static tree cp_parser_base_clause
1921 static tree cp_parser_base_specifier
1924 /* Special member functions [gram.special] */
1926 static tree cp_parser_conversion_function_id
1928 static tree cp_parser_conversion_type_id
1930 static cp_declarator *cp_parser_conversion_declarator_opt
1932 static bool cp_parser_ctor_initializer_opt
1934 static void cp_parser_mem_initializer_list
1936 static tree cp_parser_mem_initializer
1938 static tree cp_parser_mem_initializer_id
1941 /* Overloading [gram.over] */
1943 static tree cp_parser_operator_function_id
1945 static tree cp_parser_operator
1948 /* Templates [gram.temp] */
1950 static void cp_parser_template_declaration
1951 (cp_parser *, bool);
1952 static tree cp_parser_template_parameter_list
1954 static tree cp_parser_template_parameter
1955 (cp_parser *, bool *, bool *);
1956 static tree cp_parser_type_parameter
1957 (cp_parser *, bool *);
1958 static tree cp_parser_template_id
1959 (cp_parser *, bool, bool, bool);
1960 static tree cp_parser_template_name
1961 (cp_parser *, bool, bool, bool, bool *);
1962 static tree cp_parser_template_argument_list
1964 static tree cp_parser_template_argument
1966 static void cp_parser_explicit_instantiation
1968 static void cp_parser_explicit_specialization
1971 /* Exception handling [gram.exception] */
1973 static tree cp_parser_try_block
1975 static bool cp_parser_function_try_block
1977 static void cp_parser_handler_seq
1979 static void cp_parser_handler
1981 static tree cp_parser_exception_declaration
1983 static tree cp_parser_throw_expression
1985 static tree cp_parser_exception_specification_opt
1987 static tree cp_parser_type_id_list
1990 /* GNU Extensions */
1992 static tree cp_parser_asm_specification_opt
1994 static tree cp_parser_asm_operand_list
1996 static tree cp_parser_asm_clobber_list
1998 static tree cp_parser_asm_label_list
2000 static tree cp_parser_attributes_opt
2002 static tree cp_parser_attribute_list
2004 static bool cp_parser_extension_opt
2005 (cp_parser *, int *);
2006 static void cp_parser_label_declaration
2009 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2010 static bool cp_parser_pragma
2011 (cp_parser *, enum pragma_context);
2013 /* Objective-C++ Productions */
2015 static tree cp_parser_objc_message_receiver
2017 static tree cp_parser_objc_message_args
2019 static tree cp_parser_objc_message_expression
2021 static tree cp_parser_objc_encode_expression
2023 static tree cp_parser_objc_defs_expression
2025 static tree cp_parser_objc_protocol_expression
2027 static tree cp_parser_objc_selector_expression
2029 static tree cp_parser_objc_expression
2031 static bool cp_parser_objc_selector_p
2033 static tree cp_parser_objc_selector
2035 static tree cp_parser_objc_protocol_refs_opt
2037 static void cp_parser_objc_declaration
2039 static tree cp_parser_objc_statement
2042 /* Utility Routines */
2044 static tree cp_parser_lookup_name
2045 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2046 static tree cp_parser_lookup_name_simple
2047 (cp_parser *, tree, location_t);
2048 static tree cp_parser_maybe_treat_template_as_class
2050 static bool cp_parser_check_declarator_template_parameters
2051 (cp_parser *, cp_declarator *, location_t);
2052 static bool cp_parser_check_template_parameters
2053 (cp_parser *, unsigned, location_t, cp_declarator *);
2054 static tree cp_parser_simple_cast_expression
2056 static tree cp_parser_global_scope_opt
2057 (cp_parser *, bool);
2058 static bool cp_parser_constructor_declarator_p
2059 (cp_parser *, bool);
2060 static tree cp_parser_function_definition_from_specifiers_and_declarator
2061 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2062 static tree cp_parser_function_definition_after_declarator
2063 (cp_parser *, bool);
2064 static void cp_parser_template_declaration_after_export
2065 (cp_parser *, bool);
2066 static void cp_parser_perform_template_parameter_access_checks
2067 (VEC (deferred_access_check,gc)*);
2068 static tree cp_parser_single_declaration
2069 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2070 static tree cp_parser_functional_cast
2071 (cp_parser *, tree);
2072 static tree cp_parser_save_member_function_body
2073 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2074 static tree cp_parser_enclosed_template_argument_list
2076 static void cp_parser_save_default_args
2077 (cp_parser *, tree);
2078 static void cp_parser_late_parsing_for_member
2079 (cp_parser *, tree);
2080 static void cp_parser_late_parsing_default_args
2081 (cp_parser *, tree);
2082 static tree cp_parser_sizeof_operand
2083 (cp_parser *, enum rid);
2084 static tree cp_parser_trait_expr
2085 (cp_parser *, enum rid);
2086 static bool cp_parser_declares_only_class_p
2088 static void cp_parser_set_storage_class
2089 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2090 static void cp_parser_set_decl_spec_type
2091 (cp_decl_specifier_seq *, tree, location_t, bool);
2092 static bool cp_parser_friend_p
2093 (const cp_decl_specifier_seq *);
2094 static void cp_parser_required_error
2095 (cp_parser *, required_token, bool);
2096 static cp_token *cp_parser_require
2097 (cp_parser *, enum cpp_ttype, required_token);
2098 static cp_token *cp_parser_require_keyword
2099 (cp_parser *, enum rid, required_token);
2100 static bool cp_parser_token_starts_function_definition_p
2102 static bool cp_parser_next_token_starts_class_definition_p
2104 static bool cp_parser_next_token_ends_template_argument_p
2106 static bool cp_parser_nth_token_starts_template_argument_list_p
2107 (cp_parser *, size_t);
2108 static enum tag_types cp_parser_token_is_class_key
2110 static void cp_parser_check_class_key
2111 (enum tag_types, tree type);
2112 static void cp_parser_check_access_in_redeclaration
2113 (tree type, location_t location);
2114 static bool cp_parser_optional_template_keyword
2116 static void cp_parser_pre_parsed_nested_name_specifier
2118 static bool cp_parser_cache_group
2119 (cp_parser *, enum cpp_ttype, unsigned);
2120 static void cp_parser_parse_tentatively
2122 static void cp_parser_commit_to_tentative_parse
2124 static void cp_parser_abort_tentative_parse
2126 static bool cp_parser_parse_definitely
2128 static inline bool cp_parser_parsing_tentatively
2130 static bool cp_parser_uncommitted_to_tentative_parse_p
2132 static void cp_parser_error
2133 (cp_parser *, const char *);
2134 static void cp_parser_name_lookup_error
2135 (cp_parser *, tree, tree, name_lookup_error, location_t);
2136 static bool cp_parser_simulate_error
2138 static bool cp_parser_check_type_definition
2140 static void cp_parser_check_for_definition_in_return_type
2141 (cp_declarator *, tree, location_t type_location);
2142 static void cp_parser_check_for_invalid_template_id
2143 (cp_parser *, tree, location_t location);
2144 static bool cp_parser_non_integral_constant_expression
2145 (cp_parser *, non_integral_constant);
2146 static void cp_parser_diagnose_invalid_type_name
2147 (cp_parser *, tree, tree, location_t);
2148 static bool cp_parser_parse_and_diagnose_invalid_type_name
2150 static int cp_parser_skip_to_closing_parenthesis
2151 (cp_parser *, bool, bool, bool);
2152 static void cp_parser_skip_to_end_of_statement
2154 static void cp_parser_consume_semicolon_at_end_of_statement
2156 static void cp_parser_skip_to_end_of_block_or_statement
2158 static bool cp_parser_skip_to_closing_brace
2160 static void cp_parser_skip_to_end_of_template_parameter_list
2162 static void cp_parser_skip_to_pragma_eol
2163 (cp_parser*, cp_token *);
2164 static bool cp_parser_error_occurred
2166 static bool cp_parser_allow_gnu_extensions_p
2168 static bool cp_parser_is_string_literal
2170 static bool cp_parser_is_keyword
2171 (cp_token *, enum rid);
2172 static tree cp_parser_make_typename_type
2173 (cp_parser *, tree, tree, location_t location);
2174 static cp_declarator * cp_parser_make_indirect_declarator
2175 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2177 /* Returns nonzero if we are parsing tentatively. */
2180 cp_parser_parsing_tentatively (cp_parser* parser)
2182 return parser->context->next != NULL;
2185 /* Returns nonzero if TOKEN is a string literal. */
2188 cp_parser_is_string_literal (cp_token* token)
2190 return (token->type == CPP_STRING ||
2191 token->type == CPP_STRING16 ||
2192 token->type == CPP_STRING32 ||
2193 token->type == CPP_WSTRING ||
2194 token->type == CPP_UTF8STRING);
2197 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2200 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2202 return token->keyword == keyword;
2205 /* If not parsing tentatively, issue a diagnostic of the form
2206 FILE:LINE: MESSAGE before TOKEN
2207 where TOKEN is the next token in the input stream. MESSAGE
2208 (specified by the caller) is usually of the form "expected
2212 cp_parser_error (cp_parser* parser, const char* gmsgid)
2214 if (!cp_parser_simulate_error (parser))
2216 cp_token *token = cp_lexer_peek_token (parser->lexer);
2217 /* This diagnostic makes more sense if it is tagged to the line
2218 of the token we just peeked at. */
2219 cp_lexer_set_source_position_from_token (token);
2221 if (token->type == CPP_PRAGMA)
2223 error_at (token->location,
2224 "%<#pragma%> is not allowed here");
2225 cp_parser_skip_to_pragma_eol (parser, token);
2229 c_parse_error (gmsgid,
2230 /* Because c_parser_error does not understand
2231 CPP_KEYWORD, keywords are treated like
2233 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2234 token->u.value, token->flags);
2238 /* Issue an error about name-lookup failing. NAME is the
2239 IDENTIFIER_NODE DECL is the result of
2240 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2241 the thing that we hoped to find. */
2244 cp_parser_name_lookup_error (cp_parser* parser,
2247 name_lookup_error desired,
2248 location_t location)
2250 /* If name lookup completely failed, tell the user that NAME was not
2252 if (decl == error_mark_node)
2254 if (parser->scope && parser->scope != global_namespace)
2255 error_at (location, "%<%E::%E%> has not been declared",
2256 parser->scope, name);
2257 else if (parser->scope == global_namespace)
2258 error_at (location, "%<::%E%> has not been declared", name);
2259 else if (parser->object_scope
2260 && !CLASS_TYPE_P (parser->object_scope))
2261 error_at (location, "request for member %qE in non-class type %qT",
2262 name, parser->object_scope);
2263 else if (parser->object_scope)
2264 error_at (location, "%<%T::%E%> has not been declared",
2265 parser->object_scope, name);
2267 error_at (location, "%qE has not been declared", name);
2269 else if (parser->scope && parser->scope != global_namespace)
2274 error_at (location, "%<%E::%E%> is not a type",
2275 parser->scope, name);
2278 error_at (location, "%<%E::%E%> is not a class or namespace",
2279 parser->scope, name);
2283 "%<%E::%E%> is not a class, namespace, or enumeration",
2284 parser->scope, name);
2291 else if (parser->scope == global_namespace)
2296 error_at (location, "%<::%E%> is not a type", name);
2299 error_at (location, "%<::%E%> is not a class or namespace", name);
2303 "%<::%E%> is not a class, namespace, or enumeration",
2315 error_at (location, "%qE is not a type", name);
2318 error_at (location, "%qE is not a class or namespace", name);
2322 "%qE is not a class, namespace, or enumeration", name);
2330 /* If we are parsing tentatively, remember that an error has occurred
2331 during this tentative parse. Returns true if the error was
2332 simulated; false if a message should be issued by the caller. */
2335 cp_parser_simulate_error (cp_parser* parser)
2337 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2339 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2345 /* Check for repeated decl-specifiers. */
2348 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2349 location_t location)
2353 for (ds = ds_first; ds != ds_last; ++ds)
2355 unsigned count = decl_specs->specs[ds];
2358 /* The "long" specifier is a special case because of "long long". */
2362 error_at (location, "%<long long long%> is too long for GCC");
2364 pedwarn_cxx98 (location, OPT_Wlong_long,
2365 "ISO C++ 1998 does not support %<long long%>");
2369 static const char *const decl_spec_names[] = {
2386 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2391 /* This function is called when a type is defined. If type
2392 definitions are forbidden at this point, an error message is
2396 cp_parser_check_type_definition (cp_parser* parser)
2398 /* If types are forbidden here, issue a message. */
2399 if (parser->type_definition_forbidden_message)
2401 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2402 in the message need to be interpreted. */
2403 error (parser->type_definition_forbidden_message);
2409 /* This function is called when the DECLARATOR is processed. The TYPE
2410 was a type defined in the decl-specifiers. If it is invalid to
2411 define a type in the decl-specifiers for DECLARATOR, an error is
2412 issued. TYPE_LOCATION is the location of TYPE and is used
2413 for error reporting. */
2416 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2417 tree type, location_t type_location)
2419 /* [dcl.fct] forbids type definitions in return types.
2420 Unfortunately, it's not easy to know whether or not we are
2421 processing a return type until after the fact. */
2423 && (declarator->kind == cdk_pointer
2424 || declarator->kind == cdk_reference
2425 || declarator->kind == cdk_ptrmem))
2426 declarator = declarator->declarator;
2428 && declarator->kind == cdk_function)
2430 error_at (type_location,
2431 "new types may not be defined in a return type");
2432 inform (type_location,
2433 "(perhaps a semicolon is missing after the definition of %qT)",
2438 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2439 "<" in any valid C++ program. If the next token is indeed "<",
2440 issue a message warning the user about what appears to be an
2441 invalid attempt to form a template-id. LOCATION is the location
2442 of the type-specifier (TYPE) */
2445 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2446 tree type, location_t location)
2448 cp_token_position start = 0;
2450 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2453 error_at (location, "%qT is not a template", type);
2454 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2455 error_at (location, "%qE is not a template", type);
2457 error_at (location, "invalid template-id");
2458 /* Remember the location of the invalid "<". */
2459 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2460 start = cp_lexer_token_position (parser->lexer, true);
2461 /* Consume the "<". */
2462 cp_lexer_consume_token (parser->lexer);
2463 /* Parse the template arguments. */
2464 cp_parser_enclosed_template_argument_list (parser);
2465 /* Permanently remove the invalid template arguments so that
2466 this error message is not issued again. */
2468 cp_lexer_purge_tokens_after (parser->lexer, start);
2472 /* If parsing an integral constant-expression, issue an error message
2473 about the fact that THING appeared and return true. Otherwise,
2474 return false. In either case, set
2475 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2478 cp_parser_non_integral_constant_expression (cp_parser *parser,
2479 non_integral_constant thing)
2481 parser->non_integral_constant_expression_p = true;
2482 if (parser->integral_constant_expression_p)
2484 if (!parser->allow_non_integral_constant_expression_p)
2486 const char *msg = NULL;
2490 error ("floating-point literal "
2491 "cannot appear in a constant-expression");
2494 error ("a cast to a type other than an integral or "
2495 "enumeration type cannot appear in a "
2496 "constant-expression");
2499 error ("%<typeid%> operator "
2500 "cannot appear in a constant-expression");
2503 error ("non-constant compound literals "
2504 "cannot appear in a constant-expression");
2507 error ("a function call "
2508 "cannot appear in a constant-expression");
2511 error ("an increment "
2512 "cannot appear in a constant-expression");
2515 error ("an decrement "
2516 "cannot appear in a constant-expression");
2519 error ("an array reference "
2520 "cannot appear in a constant-expression");
2522 case NIC_ADDR_LABEL:
2523 error ("the address of a label "
2524 "cannot appear in a constant-expression");
2526 case NIC_OVERLOADED:
2527 error ("calls to overloaded operators "
2528 "cannot appear in a constant-expression");
2530 case NIC_ASSIGNMENT:
2531 error ("an assignment cannot appear in a constant-expression");
2534 error ("a comma operator "
2535 "cannot appear in a constant-expression");
2537 case NIC_CONSTRUCTOR:
2538 error ("a call to a constructor "
2539 "cannot appear in a constant-expression");
2545 msg = "__FUNCTION__";
2547 case NIC_PRETTY_FUNC:
2548 msg = "__PRETTY_FUNCTION__";
2568 case NIC_PREINCREMENT:
2571 case NIC_PREDECREMENT:
2584 error ("%qs cannot appear in a constant-expression", msg);
2591 /* Emit a diagnostic for an invalid type name. SCOPE is the
2592 qualifying scope (or NULL, if none) for ID. This function commits
2593 to the current active tentative parse, if any. (Otherwise, the
2594 problematic construct might be encountered again later, resulting
2595 in duplicate error messages.) LOCATION is the location of ID. */
2598 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2599 tree scope, tree id,
2600 location_t location)
2602 tree decl, old_scope;
2603 /* Try to lookup the identifier. */
2604 old_scope = parser->scope;
2605 parser->scope = scope;
2606 decl = cp_parser_lookup_name_simple (parser, id, location);
2607 parser->scope = old_scope;
2608 /* If the lookup found a template-name, it means that the user forgot
2609 to specify an argument list. Emit a useful error message. */
2610 if (TREE_CODE (decl) == TEMPLATE_DECL)
2612 "invalid use of template-name %qE without an argument list",
2614 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2615 error_at (location, "invalid use of destructor %qD as a type", id);
2616 else if (TREE_CODE (decl) == TYPE_DECL)
2617 /* Something like 'unsigned A a;' */
2618 error_at (location, "invalid combination of multiple type-specifiers");
2619 else if (!parser->scope)
2621 /* Issue an error message. */
2622 error_at (location, "%qE does not name a type", id);
2623 /* If we're in a template class, it's possible that the user was
2624 referring to a type from a base class. For example:
2626 template <typename T> struct A { typedef T X; };
2627 template <typename T> struct B : public A<T> { X x; };
2629 The user should have said "typename A<T>::X". */
2630 if (processing_template_decl && current_class_type
2631 && TYPE_BINFO (current_class_type))
2635 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2639 tree base_type = BINFO_TYPE (b);
2640 if (CLASS_TYPE_P (base_type)
2641 && dependent_type_p (base_type))
2644 /* Go from a particular instantiation of the
2645 template (which will have an empty TYPE_FIELDs),
2646 to the main version. */
2647 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2648 for (field = TYPE_FIELDS (base_type);
2650 field = TREE_CHAIN (field))
2651 if (TREE_CODE (field) == TYPE_DECL
2652 && DECL_NAME (field) == id)
2655 "(perhaps %<typename %T::%E%> was intended)",
2656 BINFO_TYPE (b), id);
2665 /* Here we diagnose qualified-ids where the scope is actually correct,
2666 but the identifier does not resolve to a valid type name. */
2667 else if (parser->scope != error_mark_node)
2669 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2670 error_at (location, "%qE in namespace %qE does not name a type",
2672 else if (CLASS_TYPE_P (parser->scope)
2673 && constructor_name_p (id, parser->scope))
2676 error_at (location, "%<%T::%E%> names the constructor, not"
2677 " the type", parser->scope, id);
2678 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2679 error_at (location, "and %qT has no template constructors",
2682 else if (TYPE_P (parser->scope)
2683 && dependent_scope_p (parser->scope))
2684 error_at (location, "need %<typename%> before %<%T::%E%> because "
2685 "%qT is a dependent scope",
2686 parser->scope, id, parser->scope);
2687 else if (TYPE_P (parser->scope))
2688 error_at (location, "%qE in class %qT does not name a type",
2693 cp_parser_commit_to_tentative_parse (parser);
2696 /* Check for a common situation where a type-name should be present,
2697 but is not, and issue a sensible error message. Returns true if an
2698 invalid type-name was detected.
2700 The situation handled by this function are variable declarations of the
2701 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2702 Usually, `ID' should name a type, but if we got here it means that it
2703 does not. We try to emit the best possible error message depending on
2704 how exactly the id-expression looks like. */
2707 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2710 cp_token *token = cp_lexer_peek_token (parser->lexer);
2712 /* Avoid duplicate error about ambiguous lookup. */
2713 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2715 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2716 if (next->type == CPP_NAME && next->ambiguous_p)
2720 cp_parser_parse_tentatively (parser);
2721 id = cp_parser_id_expression (parser,
2722 /*template_keyword_p=*/false,
2723 /*check_dependency_p=*/true,
2724 /*template_p=*/NULL,
2725 /*declarator_p=*/true,
2726 /*optional_p=*/false);
2727 /* If the next token is a (, this is a function with no explicit return
2728 type, i.e. constructor, destructor or conversion op. */
2729 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2730 || TREE_CODE (id) == TYPE_DECL)
2732 cp_parser_abort_tentative_parse (parser);
2735 if (!cp_parser_parse_definitely (parser))
2738 /* Emit a diagnostic for the invalid type. */
2739 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2740 id, token->location);
2742 /* If we aren't in the middle of a declarator (i.e. in a
2743 parameter-declaration-clause), skip to the end of the declaration;
2744 there's no point in trying to process it. */
2745 if (!parser->in_declarator_p)
2746 cp_parser_skip_to_end_of_block_or_statement (parser);
2750 /* Consume tokens up to, and including, the next non-nested closing `)'.
2751 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2752 are doing error recovery. Returns -1 if OR_COMMA is true and we
2753 found an unnested comma. */
2756 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2761 unsigned paren_depth = 0;
2762 unsigned brace_depth = 0;
2763 unsigned square_depth = 0;
2765 if (recovering && !or_comma
2766 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2771 cp_token * token = cp_lexer_peek_token (parser->lexer);
2773 switch (token->type)
2776 case CPP_PRAGMA_EOL:
2777 /* If we've run out of tokens, then there is no closing `)'. */
2780 /* This is good for lambda expression capture-lists. */
2781 case CPP_OPEN_SQUARE:
2784 case CPP_CLOSE_SQUARE:
2785 if (!square_depth--)
2790 /* This matches the processing in skip_to_end_of_statement. */
2795 case CPP_OPEN_BRACE:
2798 case CPP_CLOSE_BRACE:
2804 if (recovering && or_comma && !brace_depth && !paren_depth
2809 case CPP_OPEN_PAREN:
2814 case CPP_CLOSE_PAREN:
2815 if (!brace_depth && !paren_depth--)
2818 cp_lexer_consume_token (parser->lexer);
2827 /* Consume the token. */
2828 cp_lexer_consume_token (parser->lexer);
2832 /* Consume tokens until we reach the end of the current statement.
2833 Normally, that will be just before consuming a `;'. However, if a
2834 non-nested `}' comes first, then we stop before consuming that. */
2837 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2839 unsigned nesting_depth = 0;
2843 cp_token *token = cp_lexer_peek_token (parser->lexer);
2845 switch (token->type)
2848 case CPP_PRAGMA_EOL:
2849 /* If we've run out of tokens, stop. */
2853 /* If the next token is a `;', we have reached the end of the
2859 case CPP_CLOSE_BRACE:
2860 /* If this is a non-nested '}', stop before consuming it.
2861 That way, when confronted with something like:
2865 we stop before consuming the closing '}', even though we
2866 have not yet reached a `;'. */
2867 if (nesting_depth == 0)
2870 /* If it is the closing '}' for a block that we have
2871 scanned, stop -- but only after consuming the token.
2877 we will stop after the body of the erroneously declared
2878 function, but before consuming the following `typedef'
2880 if (--nesting_depth == 0)
2882 cp_lexer_consume_token (parser->lexer);
2886 case CPP_OPEN_BRACE:
2894 /* Consume the token. */
2895 cp_lexer_consume_token (parser->lexer);
2899 /* This function is called at the end of a statement or declaration.
2900 If the next token is a semicolon, it is consumed; otherwise, error
2901 recovery is attempted. */
2904 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2906 /* Look for the trailing `;'. */
2907 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2909 /* If there is additional (erroneous) input, skip to the end of
2911 cp_parser_skip_to_end_of_statement (parser);
2912 /* If the next token is now a `;', consume it. */
2913 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2914 cp_lexer_consume_token (parser->lexer);
2918 /* Skip tokens until we have consumed an entire block, or until we
2919 have consumed a non-nested `;'. */
2922 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2924 int nesting_depth = 0;
2926 while (nesting_depth >= 0)
2928 cp_token *token = cp_lexer_peek_token (parser->lexer);
2930 switch (token->type)
2933 case CPP_PRAGMA_EOL:
2934 /* If we've run out of tokens, stop. */
2938 /* Stop if this is an unnested ';'. */
2943 case CPP_CLOSE_BRACE:
2944 /* Stop if this is an unnested '}', or closes the outermost
2947 if (nesting_depth < 0)
2953 case CPP_OPEN_BRACE:
2962 /* Consume the token. */
2963 cp_lexer_consume_token (parser->lexer);
2967 /* Skip tokens until a non-nested closing curly brace is the next
2968 token, or there are no more tokens. Return true in the first case,
2972 cp_parser_skip_to_closing_brace (cp_parser *parser)
2974 unsigned nesting_depth = 0;
2978 cp_token *token = cp_lexer_peek_token (parser->lexer);
2980 switch (token->type)
2983 case CPP_PRAGMA_EOL:
2984 /* If we've run out of tokens, stop. */
2987 case CPP_CLOSE_BRACE:
2988 /* If the next token is a non-nested `}', then we have reached
2989 the end of the current block. */
2990 if (nesting_depth-- == 0)
2994 case CPP_OPEN_BRACE:
2995 /* If it the next token is a `{', then we are entering a new
2996 block. Consume the entire block. */
3004 /* Consume the token. */
3005 cp_lexer_consume_token (parser->lexer);
3009 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3010 parameter is the PRAGMA token, allowing us to purge the entire pragma
3014 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3018 parser->lexer->in_pragma = false;
3021 token = cp_lexer_consume_token (parser->lexer);
3022 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3024 /* Ensure that the pragma is not parsed again. */
3025 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3028 /* Require pragma end of line, resyncing with it as necessary. The
3029 arguments are as for cp_parser_skip_to_pragma_eol. */
3032 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3034 parser->lexer->in_pragma = false;
3035 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3036 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3039 /* This is a simple wrapper around make_typename_type. When the id is
3040 an unresolved identifier node, we can provide a superior diagnostic
3041 using cp_parser_diagnose_invalid_type_name. */
3044 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3045 tree id, location_t id_location)
3048 if (TREE_CODE (id) == IDENTIFIER_NODE)
3050 result = make_typename_type (scope, id, typename_type,
3051 /*complain=*/tf_none);
3052 if (result == error_mark_node)
3053 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3056 return make_typename_type (scope, id, typename_type, tf_error);
3059 /* This is a wrapper around the
3060 make_{pointer,ptrmem,reference}_declarator functions that decides
3061 which one to call based on the CODE and CLASS_TYPE arguments. The
3062 CODE argument should be one of the values returned by
3063 cp_parser_ptr_operator. */
3064 static cp_declarator *
3065 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3066 cp_cv_quals cv_qualifiers,
3067 cp_declarator *target)
3069 if (code == ERROR_MARK)
3070 return cp_error_declarator;
3072 if (code == INDIRECT_REF)
3073 if (class_type == NULL_TREE)
3074 return make_pointer_declarator (cv_qualifiers, target);
3076 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3077 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3078 return make_reference_declarator (cv_qualifiers, target, false);
3079 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3080 return make_reference_declarator (cv_qualifiers, target, true);
3084 /* Create a new C++ parser. */
3087 cp_parser_new (void)
3093 /* cp_lexer_new_main is called before calling ggc_alloc because
3094 cp_lexer_new_main might load a PCH file. */
3095 lexer = cp_lexer_new_main ();
3097 /* Initialize the binops_by_token so that we can get the tree
3098 directly from the token. */
3099 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3100 binops_by_token[binops[i].token_type] = binops[i];
3102 parser = GGC_CNEW (cp_parser);
3103 parser->lexer = lexer;
3104 parser->context = cp_parser_context_new (NULL);
3106 /* For now, we always accept GNU extensions. */
3107 parser->allow_gnu_extensions_p = 1;
3109 /* The `>' token is a greater-than operator, not the end of a
3111 parser->greater_than_is_operator_p = true;
3113 parser->default_arg_ok_p = true;
3115 /* We are not parsing a constant-expression. */
3116 parser->integral_constant_expression_p = false;
3117 parser->allow_non_integral_constant_expression_p = false;
3118 parser->non_integral_constant_expression_p = false;
3120 /* Local variable names are not forbidden. */
3121 parser->local_variables_forbidden_p = false;
3123 /* We are not processing an `extern "C"' declaration. */
3124 parser->in_unbraced_linkage_specification_p = false;
3126 /* We are not processing a declarator. */
3127 parser->in_declarator_p = false;
3129 /* We are not processing a template-argument-list. */
3130 parser->in_template_argument_list_p = false;
3132 /* We are not in an iteration statement. */
3133 parser->in_statement = 0;
3135 /* We are not in a switch statement. */
3136 parser->in_switch_statement_p = false;
3138 /* We are not parsing a type-id inside an expression. */
3139 parser->in_type_id_in_expr_p = false;
3141 /* Declarations aren't implicitly extern "C". */
3142 parser->implicit_extern_c = false;
3144 /* String literals should be translated to the execution character set. */
3145 parser->translate_strings_p = true;
3147 /* We are not parsing a function body. */
3148 parser->in_function_body = false;
3150 /* The unparsed function queue is empty. */
3151 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
3153 /* There are no classes being defined. */
3154 parser->num_classes_being_defined = 0;
3156 /* No template parameters apply. */
3157 parser->num_template_parameter_lists = 0;
3162 /* Create a cp_lexer structure which will emit the tokens in CACHE
3163 and push it onto the parser's lexer stack. This is used for delayed
3164 parsing of in-class method bodies and default arguments, and should
3165 not be confused with tentative parsing. */
3167 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3169 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3170 lexer->next = parser->lexer;
3171 parser->lexer = lexer;
3173 /* Move the current source position to that of the first token in the
3175 cp_lexer_set_source_position_from_token (lexer->next_token);
3178 /* Pop the top lexer off the parser stack. This is never used for the
3179 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3181 cp_parser_pop_lexer (cp_parser *parser)
3183 cp_lexer *lexer = parser->lexer;
3184 parser->lexer = lexer->next;
3185 cp_lexer_destroy (lexer);
3187 /* Put the current source position back where it was before this
3188 lexer was pushed. */
3189 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3192 /* Lexical conventions [gram.lex] */
3194 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3198 cp_parser_identifier (cp_parser* parser)
3202 /* Look for the identifier. */
3203 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3204 /* Return the value. */
3205 return token ? token->u.value : error_mark_node;
3208 /* Parse a sequence of adjacent string constants. Returns a
3209 TREE_STRING representing the combined, nul-terminated string
3210 constant. If TRANSLATE is true, translate the string to the
3211 execution character set. If WIDE_OK is true, a wide string is
3214 C++98 [lex.string] says that if a narrow string literal token is
3215 adjacent to a wide string literal token, the behavior is undefined.
3216 However, C99 6.4.5p4 says that this results in a wide string literal.
3217 We follow C99 here, for consistency with the C front end.
3219 This code is largely lifted from lex_string() in c-lex.c.
3221 FUTURE: ObjC++ will need to handle @-strings here. */
3223 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3227 struct obstack str_ob;
3228 cpp_string str, istr, *strs;
3230 enum cpp_ttype type;
3232 tok = cp_lexer_peek_token (parser->lexer);
3233 if (!cp_parser_is_string_literal (tok))
3235 cp_parser_error (parser, "expected string-literal");
3236 return error_mark_node;
3241 /* Try to avoid the overhead of creating and destroying an obstack
3242 for the common case of just one string. */
3243 if (!cp_parser_is_string_literal
3244 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3246 cp_lexer_consume_token (parser->lexer);
3248 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3249 str.len = TREE_STRING_LENGTH (tok->u.value);
3256 gcc_obstack_init (&str_ob);
3261 cp_lexer_consume_token (parser->lexer);
3263 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3264 str.len = TREE_STRING_LENGTH (tok->u.value);
3266 if (type != tok->type)
3268 if (type == CPP_STRING)
3270 else if (tok->type != CPP_STRING)
3271 error_at (tok->location,
3272 "unsupported non-standard concatenation "
3273 "of string literals");
3276 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3278 tok = cp_lexer_peek_token (parser->lexer);
3280 while (cp_parser_is_string_literal (tok));
3282 strs = (cpp_string *) obstack_finish (&str_ob);
3285 if (type != CPP_STRING && !wide_ok)
3287 cp_parser_error (parser, "a wide string is invalid in this context");
3291 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3292 (parse_in, strs, count, &istr, type))
3294 value = build_string (istr.len, (const char *)istr.text);
3295 free (CONST_CAST (unsigned char *, istr.text));
3301 case CPP_UTF8STRING:
3302 TREE_TYPE (value) = char_array_type_node;
3305 TREE_TYPE (value) = char16_array_type_node;
3308 TREE_TYPE (value) = char32_array_type_node;
3311 TREE_TYPE (value) = wchar_array_type_node;
3315 value = fix_string_type (value);
3318 /* cpp_interpret_string has issued an error. */
3319 value = error_mark_node;
3322 obstack_free (&str_ob, 0);
3328 /* Basic concepts [gram.basic] */
3330 /* Parse a translation-unit.
3333 declaration-seq [opt]
3335 Returns TRUE if all went well. */
3338 cp_parser_translation_unit (cp_parser* parser)
3340 /* The address of the first non-permanent object on the declarator
3342 static void *declarator_obstack_base;
3346 /* Create the declarator obstack, if necessary. */
3347 if (!cp_error_declarator)
3349 gcc_obstack_init (&declarator_obstack);
3350 /* Create the error declarator. */
3351 cp_error_declarator = make_declarator (cdk_error);
3352 /* Create the empty parameter list. */
3353 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3354 /* Remember where the base of the declarator obstack lies. */
3355 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3358 cp_parser_declaration_seq_opt (parser);
3360 /* If there are no tokens left then all went well. */
3361 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3363 /* Get rid of the token array; we don't need it any more. */
3364 cp_lexer_destroy (parser->lexer);
3365 parser->lexer = NULL;
3367 /* This file might have been a context that's implicitly extern
3368 "C". If so, pop the lang context. (Only relevant for PCH.) */
3369 if (parser->implicit_extern_c)
3371 pop_lang_context ();
3372 parser->implicit_extern_c = false;
3376 finish_translation_unit ();
3382 cp_parser_error (parser, "expected declaration");
3386 /* Make sure the declarator obstack was fully cleaned up. */
3387 gcc_assert (obstack_next_free (&declarator_obstack)
3388 == declarator_obstack_base);
3390 /* All went well. */
3394 /* Expressions [gram.expr] */
3396 /* Parse a primary-expression.
3407 ( compound-statement )
3408 __builtin_va_arg ( assignment-expression , type-id )
3409 __builtin_offsetof ( type-id , offsetof-expression )
3412 __has_nothrow_assign ( type-id )
3413 __has_nothrow_constructor ( type-id )
3414 __has_nothrow_copy ( type-id )
3415 __has_trivial_assign ( type-id )
3416 __has_trivial_constructor ( type-id )
3417 __has_trivial_copy ( type-id )
3418 __has_trivial_destructor ( type-id )
3419 __has_virtual_destructor ( type-id )
3420 __is_abstract ( type-id )
3421 __is_base_of ( type-id , type-id )
3422 __is_class ( type-id )
3423 __is_convertible_to ( type-id , type-id )
3424 __is_empty ( type-id )
3425 __is_enum ( type-id )
3426 __is_pod ( type-id )
3427 __is_polymorphic ( type-id )
3428 __is_union ( type-id )
3430 Objective-C++ Extension:
3438 ADDRESS_P is true iff this expression was immediately preceded by
3439 "&" and therefore might denote a pointer-to-member. CAST_P is true
3440 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3441 true iff this expression is a template argument.
3443 Returns a representation of the expression. Upon return, *IDK
3444 indicates what kind of id-expression (if any) was present. */
3447 cp_parser_primary_expression (cp_parser *parser,
3450 bool template_arg_p,
3453 cp_token *token = NULL;
3455 /* Assume the primary expression is not an id-expression. */
3456 *idk = CP_ID_KIND_NONE;
3458 /* Peek at the next token. */
3459 token = cp_lexer_peek_token (parser->lexer);
3460 switch (token->type)
3473 token = cp_lexer_consume_token (parser->lexer);
3474 if (TREE_CODE (token->u.value) == FIXED_CST)
3476 error_at (token->location,
3477 "fixed-point types not supported in C++");
3478 return error_mark_node;
3480 /* Floating-point literals are only allowed in an integral
3481 constant expression if they are cast to an integral or
3482 enumeration type. */
3483 if (TREE_CODE (token->u.value) == REAL_CST
3484 && parser->integral_constant_expression_p
3487 /* CAST_P will be set even in invalid code like "int(2.7 +
3488 ...)". Therefore, we have to check that the next token
3489 is sure to end the cast. */
3492 cp_token *next_token;
3494 next_token = cp_lexer_peek_token (parser->lexer);
3495 if (/* The comma at the end of an
3496 enumerator-definition. */
3497 next_token->type != CPP_COMMA
3498 /* The curly brace at the end of an enum-specifier. */
3499 && next_token->type != CPP_CLOSE_BRACE
3500 /* The end of a statement. */
3501 && next_token->type != CPP_SEMICOLON
3502 /* The end of the cast-expression. */
3503 && next_token->type != CPP_CLOSE_PAREN
3504 /* The end of an array bound. */
3505 && next_token->type != CPP_CLOSE_SQUARE
3506 /* The closing ">" in a template-argument-list. */
3507 && (next_token->type != CPP_GREATER
3508 || parser->greater_than_is_operator_p)
3509 /* C++0x only: A ">>" treated like two ">" tokens,
3510 in a template-argument-list. */
3511 && (next_token->type != CPP_RSHIFT
3512 || (cxx_dialect == cxx98)
3513 || parser->greater_than_is_operator_p))
3517 /* If we are within a cast, then the constraint that the
3518 cast is to an integral or enumeration type will be
3519 checked at that point. If we are not within a cast, then
3520 this code is invalid. */
3522 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3524 return token->u.value;
3530 case CPP_UTF8STRING:
3531 /* ??? Should wide strings be allowed when parser->translate_strings_p
3532 is false (i.e. in attributes)? If not, we can kill the third
3533 argument to cp_parser_string_literal. */
3534 return cp_parser_string_literal (parser,
3535 parser->translate_strings_p,
3538 case CPP_OPEN_PAREN:
3541 bool saved_greater_than_is_operator_p;
3543 /* Consume the `('. */
3544 cp_lexer_consume_token (parser->lexer);
3545 /* Within a parenthesized expression, a `>' token is always
3546 the greater-than operator. */
3547 saved_greater_than_is_operator_p
3548 = parser->greater_than_is_operator_p;
3549 parser->greater_than_is_operator_p = true;
3550 /* If we see `( { ' then we are looking at the beginning of
3551 a GNU statement-expression. */
3552 if (cp_parser_allow_gnu_extensions_p (parser)
3553 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3555 /* Statement-expressions are not allowed by the standard. */
3556 pedwarn (token->location, OPT_pedantic,
3557 "ISO C++ forbids braced-groups within expressions");
3559 /* And they're not allowed outside of a function-body; you
3560 cannot, for example, write:
3562 int i = ({ int j = 3; j + 1; });
3564 at class or namespace scope. */
3565 if (!parser->in_function_body
3566 || parser->in_template_argument_list_p)
3568 error_at (token->location,
3569 "statement-expressions are not allowed outside "
3570 "functions nor in template-argument lists");
3571 cp_parser_skip_to_end_of_block_or_statement (parser);
3572 expr = error_mark_node;
3576 /* Start the statement-expression. */
3577 expr = begin_stmt_expr ();
3578 /* Parse the compound-statement. */
3579 cp_parser_compound_statement (parser, expr, false);
3581 expr = finish_stmt_expr (expr, false);
3586 /* Parse the parenthesized expression. */
3587 expr = cp_parser_expression (parser, cast_p, idk);
3588 /* Let the front end know that this expression was
3589 enclosed in parentheses. This matters in case, for
3590 example, the expression is of the form `A::B', since
3591 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3593 finish_parenthesized_expr (expr);
3595 /* The `>' token might be the end of a template-id or
3596 template-parameter-list now. */
3597 parser->greater_than_is_operator_p
3598 = saved_greater_than_is_operator_p;
3599 /* Consume the `)'. */
3600 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3601 cp_parser_skip_to_end_of_statement (parser);
3606 case CPP_OPEN_SQUARE:
3607 if (c_dialect_objc ())
3608 /* We have an Objective-C++ message. */
3609 return cp_parser_objc_expression (parser);
3610 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3611 return cp_parser_lambda_expression (parser);
3613 case CPP_OBJC_STRING:
3614 if (c_dialect_objc ())
3615 /* We have an Objective-C++ string literal. */
3616 return cp_parser_objc_expression (parser);
3617 cp_parser_error (parser, "expected primary-expression");
3618 return error_mark_node;
3621 switch (token->keyword)
3623 /* These two are the boolean literals. */
3625 cp_lexer_consume_token (parser->lexer);
3626 return boolean_true_node;
3628 cp_lexer_consume_token (parser->lexer);
3629 return boolean_false_node;
3631 /* The `__null' literal. */
3633 cp_lexer_consume_token (parser->lexer);
3636 /* The `nullptr' literal. */
3638 cp_lexer_consume_token (parser->lexer);
3639 return nullptr_node;
3641 /* Recognize the `this' keyword. */
3643 cp_lexer_consume_token (parser->lexer);
3644 if (parser->local_variables_forbidden_p)
3646 error_at (token->location,
3647 "%<this%> may not be used in this context");
3648 return error_mark_node;
3650 /* Pointers cannot appear in constant-expressions. */
3651 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3652 return error_mark_node;
3653 return finish_this_expr ();
3655 /* The `operator' keyword can be the beginning of an
3660 case RID_FUNCTION_NAME:
3661 case RID_PRETTY_FUNCTION_NAME:
3662 case RID_C99_FUNCTION_NAME:
3664 non_integral_constant name;
3666 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3667 __func__ are the names of variables -- but they are
3668 treated specially. Therefore, they are handled here,
3669 rather than relying on the generic id-expression logic
3670 below. Grammatically, these names are id-expressions.
3672 Consume the token. */
3673 token = cp_lexer_consume_token (parser->lexer);
3675 switch (token->keyword)
3677 case RID_FUNCTION_NAME:
3678 name = NIC_FUNC_NAME;
3680 case RID_PRETTY_FUNCTION_NAME:
3681 name = NIC_PRETTY_FUNC;
3683 case RID_C99_FUNCTION_NAME:
3684 name = NIC_C99_FUNC;
3690 if (cp_parser_non_integral_constant_expression (parser, name))
3691 return error_mark_node;
3693 /* Look up the name. */
3694 return finish_fname (token->u.value);
3702 /* The `__builtin_va_arg' construct is used to handle
3703 `va_arg'. Consume the `__builtin_va_arg' token. */
3704 cp_lexer_consume_token (parser->lexer);
3705 /* Look for the opening `('. */
3706 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3707 /* Now, parse the assignment-expression. */
3708 expression = cp_parser_assignment_expression (parser,
3709 /*cast_p=*/false, NULL);
3710 /* Look for the `,'. */
3711 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3712 /* Parse the type-id. */
3713 type = cp_parser_type_id (parser);
3714 /* Look for the closing `)'. */
3715 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3716 /* Using `va_arg' in a constant-expression is not
3718 if (cp_parser_non_integral_constant_expression (parser,NIC_VA_ARG))
3719 return error_mark_node;
3720 return build_x_va_arg (expression, type);
3724 return cp_parser_builtin_offsetof (parser);
3726 case RID_HAS_NOTHROW_ASSIGN:
3727 case RID_HAS_NOTHROW_CONSTRUCTOR:
3728 case RID_HAS_NOTHROW_COPY:
3729 case RID_HAS_TRIVIAL_ASSIGN:
3730 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3731 case RID_HAS_TRIVIAL_COPY:
3732 case RID_HAS_TRIVIAL_DESTRUCTOR:
3733 case RID_HAS_VIRTUAL_DESTRUCTOR:
3734 case RID_IS_ABSTRACT:
3735 case RID_IS_BASE_OF:
3737 case RID_IS_CONVERTIBLE_TO:
3741 case RID_IS_POLYMORPHIC:
3742 case RID_IS_STD_LAYOUT:
3743 case RID_IS_TRIVIAL:
3745 return cp_parser_trait_expr (parser, token->keyword);
3747 /* Objective-C++ expressions. */
3749 case RID_AT_PROTOCOL:
3750 case RID_AT_SELECTOR:
3751 return cp_parser_objc_expression (parser);
3754 cp_parser_error (parser, "expected primary-expression");
3755 return error_mark_node;
3758 /* An id-expression can start with either an identifier, a
3759 `::' as the beginning of a qualified-id, or the "operator"
3763 case CPP_TEMPLATE_ID:
3764 case CPP_NESTED_NAME_SPECIFIER:
3768 const char *error_msg;
3771 cp_token *id_expr_token;
3774 /* Parse the id-expression. */
3776 = cp_parser_id_expression (parser,
3777 /*template_keyword_p=*/false,
3778 /*check_dependency_p=*/true,
3780 /*declarator_p=*/false,
3781 /*optional_p=*/false);
3782 if (id_expression == error_mark_node)
3783 return error_mark_node;
3784 id_expr_token = token;
3785 token = cp_lexer_peek_token (parser->lexer);
3786 done = (token->type != CPP_OPEN_SQUARE
3787 && token->type != CPP_OPEN_PAREN
3788 && token->type != CPP_DOT
3789 && token->type != CPP_DEREF
3790 && token->type != CPP_PLUS_PLUS
3791 && token->type != CPP_MINUS_MINUS);
3792 /* If we have a template-id, then no further lookup is
3793 required. If the template-id was for a template-class, we
3794 will sometimes have a TYPE_DECL at this point. */
3795 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3796 || TREE_CODE (id_expression) == TYPE_DECL)
3797 decl = id_expression;
3798 /* Look up the name. */
3801 tree ambiguous_decls;
3803 /* If we already know that this lookup is ambiguous, then
3804 we've already issued an error message; there's no reason
3806 if (id_expr_token->type == CPP_NAME
3807 && id_expr_token->ambiguous_p)
3809 cp_parser_simulate_error (parser);
3810 return error_mark_node;
3813 decl = cp_parser_lookup_name (parser, id_expression,
3816 /*is_namespace=*/false,
3817 /*check_dependency=*/true,
3819 id_expr_token->location);
3820 /* If the lookup was ambiguous, an error will already have
3822 if (ambiguous_decls)
3823 return error_mark_node;
3825 /* In Objective-C++, an instance variable (ivar) may be preferred
3826 to whatever cp_parser_lookup_name() found. */
3827 decl = objc_lookup_ivar (decl, id_expression);
3829 /* If name lookup gives us a SCOPE_REF, then the
3830 qualifying scope was dependent. */
3831 if (TREE_CODE (decl) == SCOPE_REF)
3833 /* At this point, we do not know if DECL is a valid
3834 integral constant expression. We assume that it is
3835 in fact such an expression, so that code like:
3837 template <int N> struct A {
3841 is accepted. At template-instantiation time, we
3842 will check that B<N>::i is actually a constant. */
3845 /* Check to see if DECL is a local variable in a context
3846 where that is forbidden. */
3847 if (parser->local_variables_forbidden_p
3848 && local_variable_p (decl))
3850 /* It might be that we only found DECL because we are
3851 trying to be generous with pre-ISO scoping rules.
3852 For example, consider:
3856 for (int i = 0; i < 10; ++i) {}
3857 extern void f(int j = i);
3860 Here, name look up will originally find the out
3861 of scope `i'. We need to issue a warning message,
3862 but then use the global `i'. */
3863 decl = check_for_out_of_scope_variable (decl);
3864 if (local_variable_p (decl))
3866 error_at (id_expr_token->location,
3867 "local variable %qD may not appear in this context",
3869 return error_mark_node;
3874 decl = (finish_id_expression
3875 (id_expression, decl, parser->scope,
3877 parser->integral_constant_expression_p,
3878 parser->allow_non_integral_constant_expression_p,
3879 &parser->non_integral_constant_expression_p,
3880 template_p, done, address_p,
3883 id_expr_token->location));
3885 cp_parser_error (parser, error_msg);
3889 /* Anything else is an error. */
3891 cp_parser_error (parser, "expected primary-expression");
3892 return error_mark_node;
3896 /* Parse an id-expression.
3903 :: [opt] nested-name-specifier template [opt] unqualified-id
3905 :: operator-function-id
3908 Return a representation of the unqualified portion of the
3909 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3910 a `::' or nested-name-specifier.
3912 Often, if the id-expression was a qualified-id, the caller will
3913 want to make a SCOPE_REF to represent the qualified-id. This
3914 function does not do this in order to avoid wastefully creating
3915 SCOPE_REFs when they are not required.
3917 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3920 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3921 uninstantiated templates.
3923 If *TEMPLATE_P is non-NULL, it is set to true iff the
3924 `template' keyword is used to explicitly indicate that the entity
3925 named is a template.
3927 If DECLARATOR_P is true, the id-expression is appearing as part of
3928 a declarator, rather than as part of an expression. */
3931 cp_parser_id_expression (cp_parser *parser,
3932 bool template_keyword_p,
3933 bool check_dependency_p,
3938 bool global_scope_p;
3939 bool nested_name_specifier_p;
3941 /* Assume the `template' keyword was not used. */
3943 *template_p = template_keyword_p;
3945 /* Look for the optional `::' operator. */
3947 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3949 /* Look for the optional nested-name-specifier. */
3950 nested_name_specifier_p
3951 = (cp_parser_nested_name_specifier_opt (parser,
3952 /*typename_keyword_p=*/false,
3957 /* If there is a nested-name-specifier, then we are looking at
3958 the first qualified-id production. */
3959 if (nested_name_specifier_p)
3962 tree saved_object_scope;
3963 tree saved_qualifying_scope;
3964 tree unqualified_id;
3967 /* See if the next token is the `template' keyword. */
3969 template_p = &is_template;
3970 *template_p = cp_parser_optional_template_keyword (parser);
3971 /* Name lookup we do during the processing of the
3972 unqualified-id might obliterate SCOPE. */
3973 saved_scope = parser->scope;
3974 saved_object_scope = parser->object_scope;
3975 saved_qualifying_scope = parser->qualifying_scope;
3976 /* Process the final unqualified-id. */
3977 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3980 /*optional_p=*/false);
3981 /* Restore the SAVED_SCOPE for our caller. */
3982 parser->scope = saved_scope;
3983 parser->object_scope = saved_object_scope;
3984 parser->qualifying_scope = saved_qualifying_scope;
3986 return unqualified_id;
3988 /* Otherwise, if we are in global scope, then we are looking at one
3989 of the other qualified-id productions. */
3990 else if (global_scope_p)
3995 /* Peek at the next token. */
3996 token = cp_lexer_peek_token (parser->lexer);
3998 /* If it's an identifier, and the next token is not a "<", then
3999 we can avoid the template-id case. This is an optimization
4000 for this common case. */
4001 if (token->type == CPP_NAME
4002 && !cp_parser_nth_token_starts_template_argument_list_p
4004 return cp_parser_identifier (parser);
4006 cp_parser_parse_tentatively (parser);
4007 /* Try a template-id. */
4008 id = cp_parser_template_id (parser,
4009 /*template_keyword_p=*/false,
4010 /*check_dependency_p=*/true,
4012 /* If that worked, we're done. */
4013 if (cp_parser_parse_definitely (parser))
4016 /* Peek at the next token. (Changes in the token buffer may
4017 have invalidated the pointer obtained above.) */
4018 token = cp_lexer_peek_token (parser->lexer);
4020 switch (token->type)
4023 return cp_parser_identifier (parser);
4026 if (token->keyword == RID_OPERATOR)
4027 return cp_parser_operator_function_id (parser);
4031 cp_parser_error (parser, "expected id-expression");
4032 return error_mark_node;
4036 return cp_parser_unqualified_id (parser, template_keyword_p,
4037 /*check_dependency_p=*/true,
4042 /* Parse an unqualified-id.
4046 operator-function-id
4047 conversion-function-id
4051 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4052 keyword, in a construct like `A::template ...'.
4054 Returns a representation of unqualified-id. For the `identifier'
4055 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4056 production a BIT_NOT_EXPR is returned; the operand of the
4057 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4058 other productions, see the documentation accompanying the
4059 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4060 names are looked up in uninstantiated templates. If DECLARATOR_P
4061 is true, the unqualified-id is appearing as part of a declarator,
4062 rather than as part of an expression. */
4065 cp_parser_unqualified_id (cp_parser* parser,
4066 bool template_keyword_p,
4067 bool check_dependency_p,
4073 /* Peek at the next token. */
4074 token = cp_lexer_peek_token (parser->lexer);
4076 switch (token->type)
4082 /* We don't know yet whether or not this will be a
4084 cp_parser_parse_tentatively (parser);
4085 /* Try a template-id. */
4086 id = cp_parser_template_id (parser, template_keyword_p,
4089 /* If it worked, we're done. */
4090 if (cp_parser_parse_definitely (parser))
4092 /* Otherwise, it's an ordinary identifier. */
4093 return cp_parser_identifier (parser);
4096 case CPP_TEMPLATE_ID:
4097 return cp_parser_template_id (parser, template_keyword_p,
4104 tree qualifying_scope;
4109 /* Consume the `~' token. */
4110 cp_lexer_consume_token (parser->lexer);
4111 /* Parse the class-name. The standard, as written, seems to
4114 template <typename T> struct S { ~S (); };
4115 template <typename T> S<T>::~S() {}
4117 is invalid, since `~' must be followed by a class-name, but
4118 `S<T>' is dependent, and so not known to be a class.
4119 That's not right; we need to look in uninstantiated
4120 templates. A further complication arises from:
4122 template <typename T> void f(T t) {
4126 Here, it is not possible to look up `T' in the scope of `T'
4127 itself. We must look in both the current scope, and the
4128 scope of the containing complete expression.
4130 Yet another issue is:
4139 The standard does not seem to say that the `S' in `~S'
4140 should refer to the type `S' and not the data member
4143 /* DR 244 says that we look up the name after the "~" in the
4144 same scope as we looked up the qualifying name. That idea
4145 isn't fully worked out; it's more complicated than that. */
4146 scope = parser->scope;
4147 object_scope = parser->object_scope;
4148 qualifying_scope = parser->qualifying_scope;
4150 /* Check for invalid scopes. */
4151 if (scope == error_mark_node)
4153 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4154 cp_lexer_consume_token (parser->lexer);
4155 return error_mark_node;
4157 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4159 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4160 error_at (token->location,
4161 "scope %qT before %<~%> is not a class-name",
4163 cp_parser_simulate_error (parser);
4164 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4165 cp_lexer_consume_token (parser->lexer);
4166 return error_mark_node;
4168 gcc_assert (!scope || TYPE_P (scope));
4170 /* If the name is of the form "X::~X" it's OK even if X is a
4172 token = cp_lexer_peek_token (parser->lexer);
4174 && token->type == CPP_NAME
4175 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4177 && (token->u.value == TYPE_IDENTIFIER (scope)
4178 || constructor_name_p (token->u.value, scope)))
4180 cp_lexer_consume_token (parser->lexer);
4181 return build_nt (BIT_NOT_EXPR, scope);
4184 /* If there was an explicit qualification (S::~T), first look
4185 in the scope given by the qualification (i.e., S).
4187 Note: in the calls to cp_parser_class_name below we pass
4188 typename_type so that lookup finds the injected-class-name
4189 rather than the constructor. */
4191 type_decl = NULL_TREE;
4194 cp_parser_parse_tentatively (parser);
4195 type_decl = cp_parser_class_name (parser,
4196 /*typename_keyword_p=*/false,
4197 /*template_keyword_p=*/false,
4199 /*check_dependency=*/false,
4200 /*class_head_p=*/false,
4202 if (cp_parser_parse_definitely (parser))
4205 /* In "N::S::~S", look in "N" as well. */
4206 if (!done && scope && qualifying_scope)
4208 cp_parser_parse_tentatively (parser);
4209 parser->scope = qualifying_scope;
4210 parser->object_scope = NULL_TREE;
4211 parser->qualifying_scope = NULL_TREE;
4213 = cp_parser_class_name (parser,
4214 /*typename_keyword_p=*/false,
4215 /*template_keyword_p=*/false,
4217 /*check_dependency=*/false,
4218 /*class_head_p=*/false,
4220 if (cp_parser_parse_definitely (parser))
4223 /* In "p->S::~T", look in the scope given by "*p" as well. */
4224 else if (!done && object_scope)
4226 cp_parser_parse_tentatively (parser);
4227 parser->scope = object_scope;
4228 parser->object_scope = NULL_TREE;
4229 parser->qualifying_scope = NULL_TREE;
4231 = cp_parser_class_name (parser,
4232 /*typename_keyword_p=*/false,
4233 /*template_keyword_p=*/false,
4235 /*check_dependency=*/false,
4236 /*class_head_p=*/false,
4238 if (cp_parser_parse_definitely (parser))
4241 /* Look in the surrounding context. */
4244 parser->scope = NULL_TREE;
4245 parser->object_scope = NULL_TREE;
4246 parser->qualifying_scope = NULL_TREE;
4247 if (processing_template_decl)
4248 cp_parser_parse_tentatively (parser);
4250 = cp_parser_class_name (parser,
4251 /*typename_keyword_p=*/false,
4252 /*template_keyword_p=*/false,
4254 /*check_dependency=*/false,
4255 /*class_head_p=*/false,
4257 if (processing_template_decl
4258 && ! cp_parser_parse_definitely (parser))
4260 /* We couldn't find a type with this name, so just accept
4261 it and check for a match at instantiation time. */
4262 type_decl = cp_parser_identifier (parser);
4263 if (type_decl != error_mark_node)
4264 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4268 /* If an error occurred, assume that the name of the
4269 destructor is the same as the name of the qualifying
4270 class. That allows us to keep parsing after running
4271 into ill-formed destructor names. */
4272 if (type_decl == error_mark_node && scope)
4273 return build_nt (BIT_NOT_EXPR, scope);
4274 else if (type_decl == error_mark_node)
4275 return error_mark_node;
4277 /* Check that destructor name and scope match. */
4278 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4280 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4281 error_at (token->location,
4282 "declaration of %<~%T%> as member of %qT",
4284 cp_parser_simulate_error (parser);
4285 return error_mark_node;
4290 A typedef-name that names a class shall not be used as the
4291 identifier in the declarator for a destructor declaration. */
4293 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4294 && !DECL_SELF_REFERENCE_P (type_decl)
4295 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4296 error_at (token->location,
4297 "typedef-name %qD used as destructor declarator",
4300 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4304 if (token->keyword == RID_OPERATOR)
4308 /* This could be a template-id, so we try that first. */
4309 cp_parser_parse_tentatively (parser);
4310 /* Try a template-id. */
4311 id = cp_parser_template_id (parser, template_keyword_p,
4312 /*check_dependency_p=*/true,
4314 /* If that worked, we're done. */
4315 if (cp_parser_parse_definitely (parser))
4317 /* We still don't know whether we're looking at an
4318 operator-function-id or a conversion-function-id. */
4319 cp_parser_parse_tentatively (parser);
4320 /* Try an operator-function-id. */
4321 id = cp_parser_operator_function_id (parser);
4322 /* If that didn't work, try a conversion-function-id. */
4323 if (!cp_parser_parse_definitely (parser))
4324 id = cp_parser_conversion_function_id (parser);
4333 cp_parser_error (parser, "expected unqualified-id");
4334 return error_mark_node;
4338 /* Parse an (optional) nested-name-specifier.
4340 nested-name-specifier: [C++98]
4341 class-or-namespace-name :: nested-name-specifier [opt]
4342 class-or-namespace-name :: template nested-name-specifier [opt]
4344 nested-name-specifier: [C++0x]
4347 nested-name-specifier identifier ::
4348 nested-name-specifier template [opt] simple-template-id ::
4350 PARSER->SCOPE should be set appropriately before this function is
4351 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4352 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4355 Sets PARSER->SCOPE to the class (TYPE) or namespace
4356 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4357 it unchanged if there is no nested-name-specifier. Returns the new
4358 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4360 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4361 part of a declaration and/or decl-specifier. */
4364 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4365 bool typename_keyword_p,
4366 bool check_dependency_p,
4368 bool is_declaration)
4370 bool success = false;
4371 cp_token_position start = 0;
4374 /* Remember where the nested-name-specifier starts. */
4375 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4377 start = cp_lexer_token_position (parser->lexer, false);
4378 push_deferring_access_checks (dk_deferred);
4385 tree saved_qualifying_scope;
4386 bool template_keyword_p;
4388 /* Spot cases that cannot be the beginning of a
4389 nested-name-specifier. */
4390 token = cp_lexer_peek_token (parser->lexer);
4392 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4393 the already parsed nested-name-specifier. */
4394 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4396 /* Grab the nested-name-specifier and continue the loop. */
4397 cp_parser_pre_parsed_nested_name_specifier (parser);
4398 /* If we originally encountered this nested-name-specifier
4399 with IS_DECLARATION set to false, we will not have
4400 resolved TYPENAME_TYPEs, so we must do so here. */
4402 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4404 new_scope = resolve_typename_type (parser->scope,
4405 /*only_current_p=*/false);
4406 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4407 parser->scope = new_scope;
4413 /* Spot cases that cannot be the beginning of a
4414 nested-name-specifier. On the second and subsequent times
4415 through the loop, we look for the `template' keyword. */
4416 if (success && token->keyword == RID_TEMPLATE)
4418 /* A template-id can start a nested-name-specifier. */
4419 else if (token->type == CPP_TEMPLATE_ID)
4423 /* If the next token is not an identifier, then it is
4424 definitely not a type-name or namespace-name. */
4425 if (token->type != CPP_NAME)
4427 /* If the following token is neither a `<' (to begin a
4428 template-id), nor a `::', then we are not looking at a
4429 nested-name-specifier. */
4430 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4431 if (token->type != CPP_SCOPE
4432 && !cp_parser_nth_token_starts_template_argument_list_p
4437 /* The nested-name-specifier is optional, so we parse
4439 cp_parser_parse_tentatively (parser);
4441 /* Look for the optional `template' keyword, if this isn't the
4442 first time through the loop. */
4444 template_keyword_p = cp_parser_optional_template_keyword (parser);
4446 template_keyword_p = false;
4448 /* Save the old scope since the name lookup we are about to do
4449 might destroy it. */
4450 old_scope = parser->scope;
4451 saved_qualifying_scope = parser->qualifying_scope;
4452 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4453 look up names in "X<T>::I" in order to determine that "Y" is
4454 a template. So, if we have a typename at this point, we make
4455 an effort to look through it. */
4457 && !typename_keyword_p
4459 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4460 parser->scope = resolve_typename_type (parser->scope,
4461 /*only_current_p=*/false);
4462 /* Parse the qualifying entity. */
4464 = cp_parser_qualifying_entity (parser,
4470 /* Look for the `::' token. */
4471 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4473 /* If we found what we wanted, we keep going; otherwise, we're
4475 if (!cp_parser_parse_definitely (parser))
4477 bool error_p = false;
4479 /* Restore the OLD_SCOPE since it was valid before the
4480 failed attempt at finding the last
4481 class-or-namespace-name. */
4482 parser->scope = old_scope;
4483 parser->qualifying_scope = saved_qualifying_scope;
4484 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4486 /* If the next token is an identifier, and the one after
4487 that is a `::', then any valid interpretation would have
4488 found a class-or-namespace-name. */
4489 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4490 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4492 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4495 token = cp_lexer_consume_token (parser->lexer);
4498 if (!token->ambiguous_p)
4501 tree ambiguous_decls;
4503 decl = cp_parser_lookup_name (parser, token->u.value,
4505 /*is_template=*/false,
4506 /*is_namespace=*/false,
4507 /*check_dependency=*/true,
4510 if (TREE_CODE (decl) == TEMPLATE_DECL)
4511 error_at (token->location,
4512 "%qD used without template parameters",
4514 else if (ambiguous_decls)
4516 error_at (token->location,
4517 "reference to %qD is ambiguous",
4519 print_candidates (ambiguous_decls);
4520 decl = error_mark_node;
4524 if (cxx_dialect != cxx98)
4525 cp_parser_name_lookup_error
4526 (parser, token->u.value, decl, NLE_NOT_CXX98,
4529 cp_parser_name_lookup_error
4530 (parser, token->u.value, decl, NLE_CXX98,
4534 parser->scope = error_mark_node;
4536 /* Treat this as a successful nested-name-specifier
4541 If the name found is not a class-name (clause
4542 _class_) or namespace-name (_namespace.def_), the
4543 program is ill-formed. */
4546 cp_lexer_consume_token (parser->lexer);
4550 /* We've found one valid nested-name-specifier. */
4552 /* Name lookup always gives us a DECL. */
4553 if (TREE_CODE (new_scope) == TYPE_DECL)
4554 new_scope = TREE_TYPE (new_scope);
4555 /* Uses of "template" must be followed by actual templates. */
4556 if (template_keyword_p
4557 && !(CLASS_TYPE_P (new_scope)
4558 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4559 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4560 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4561 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4562 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4563 == TEMPLATE_ID_EXPR)))
4564 permerror (input_location, TYPE_P (new_scope)
4565 ? "%qT is not a template"
4566 : "%qD is not a template",
4568 /* If it is a class scope, try to complete it; we are about to
4569 be looking up names inside the class. */
4570 if (TYPE_P (new_scope)
4571 /* Since checking types for dependency can be expensive,
4572 avoid doing it if the type is already complete. */
4573 && !COMPLETE_TYPE_P (new_scope)
4574 /* Do not try to complete dependent types. */
4575 && !dependent_type_p (new_scope))
4577 new_scope = complete_type (new_scope);
4578 /* If it is a typedef to current class, use the current
4579 class instead, as the typedef won't have any names inside
4581 if (!COMPLETE_TYPE_P (new_scope)
4582 && currently_open_class (new_scope))
4583 new_scope = TYPE_MAIN_VARIANT (new_scope);
4585 /* Make sure we look in the right scope the next time through
4587 parser->scope = new_scope;
4590 /* If parsing tentatively, replace the sequence of tokens that makes
4591 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4592 token. That way, should we re-parse the token stream, we will
4593 not have to repeat the effort required to do the parse, nor will
4594 we issue duplicate error messages. */
4595 if (success && start)
4599 token = cp_lexer_token_at (parser->lexer, start);
4600 /* Reset the contents of the START token. */
4601 token->type = CPP_NESTED_NAME_SPECIFIER;
4602 /* Retrieve any deferred checks. Do not pop this access checks yet
4603 so the memory will not be reclaimed during token replacing below. */
4604 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4605 token->u.tree_check_value->value = parser->scope;
4606 token->u.tree_check_value->checks = get_deferred_access_checks ();
4607 token->u.tree_check_value->qualifying_scope =
4608 parser->qualifying_scope;
4609 token->keyword = RID_MAX;
4611 /* Purge all subsequent tokens. */
4612 cp_lexer_purge_tokens_after (parser->lexer, start);
4616 pop_to_parent_deferring_access_checks ();
4618 return success ? parser->scope : NULL_TREE;
4621 /* Parse a nested-name-specifier. See
4622 cp_parser_nested_name_specifier_opt for details. This function
4623 behaves identically, except that it will an issue an error if no
4624 nested-name-specifier is present. */
4627 cp_parser_nested_name_specifier (cp_parser *parser,
4628 bool typename_keyword_p,
4629 bool check_dependency_p,
4631 bool is_declaration)
4635 /* Look for the nested-name-specifier. */
4636 scope = cp_parser_nested_name_specifier_opt (parser,
4641 /* If it was not present, issue an error message. */
4644 cp_parser_error (parser, "expected nested-name-specifier");
4645 parser->scope = NULL_TREE;
4651 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4652 this is either a class-name or a namespace-name (which corresponds
4653 to the class-or-namespace-name production in the grammar). For
4654 C++0x, it can also be a type-name that refers to an enumeration
4657 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4658 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4659 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4660 TYPE_P is TRUE iff the next name should be taken as a class-name,
4661 even the same name is declared to be another entity in the same
4664 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4665 specified by the class-or-namespace-name. If neither is found the
4666 ERROR_MARK_NODE is returned. */
4669 cp_parser_qualifying_entity (cp_parser *parser,
4670 bool typename_keyword_p,
4671 bool template_keyword_p,
4672 bool check_dependency_p,
4674 bool is_declaration)
4677 tree saved_qualifying_scope;
4678 tree saved_object_scope;
4681 bool successful_parse_p;
4683 /* Before we try to parse the class-name, we must save away the
4684 current PARSER->SCOPE since cp_parser_class_name will destroy
4686 saved_scope = parser->scope;
4687 saved_qualifying_scope = parser->qualifying_scope;
4688 saved_object_scope = parser->object_scope;
4689 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4690 there is no need to look for a namespace-name. */
4691 only_class_p = template_keyword_p
4692 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4694 cp_parser_parse_tentatively (parser);
4695 scope = cp_parser_class_name (parser,
4698 type_p ? class_type : none_type,
4700 /*class_head_p=*/false,
4702 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4703 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4705 && cxx_dialect != cxx98
4706 && !successful_parse_p)
4708 /* Restore the saved scope. */
4709 parser->scope = saved_scope;
4710 parser->qualifying_scope = saved_qualifying_scope;
4711 parser->object_scope = saved_object_scope;
4713 /* Parse tentatively. */
4714 cp_parser_parse_tentatively (parser);
4716 /* Parse a typedef-name or enum-name. */
4717 scope = cp_parser_nonclass_name (parser);
4719 /* "If the name found does not designate a namespace or a class,
4720 enumeration, or dependent type, the program is ill-formed."
4722 We cover classes and dependent types above and namespaces below,
4723 so this code is only looking for enums. */
4724 if (!scope || TREE_CODE (scope) != TYPE_DECL
4725 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4726 cp_parser_simulate_error (parser);
4728 successful_parse_p = cp_parser_parse_definitely (parser);
4730 /* If that didn't work, try for a namespace-name. */
4731 if (!only_class_p && !successful_parse_p)
4733 /* Restore the saved scope. */
4734 parser->scope = saved_scope;
4735 parser->qualifying_scope = saved_qualifying_scope;
4736 parser->object_scope = saved_object_scope;
4737 /* If we are not looking at an identifier followed by the scope
4738 resolution operator, then this is not part of a
4739 nested-name-specifier. (Note that this function is only used
4740 to parse the components of a nested-name-specifier.) */
4741 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4742 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4743 return error_mark_node;
4744 scope = cp_parser_namespace_name (parser);
4750 /* Parse a postfix-expression.
4754 postfix-expression [ expression ]
4755 postfix-expression ( expression-list [opt] )
4756 simple-type-specifier ( expression-list [opt] )
4757 typename :: [opt] nested-name-specifier identifier
4758 ( expression-list [opt] )
4759 typename :: [opt] nested-name-specifier template [opt] template-id
4760 ( expression-list [opt] )
4761 postfix-expression . template [opt] id-expression
4762 postfix-expression -> template [opt] id-expression
4763 postfix-expression . pseudo-destructor-name
4764 postfix-expression -> pseudo-destructor-name
4765 postfix-expression ++
4766 postfix-expression --
4767 dynamic_cast < type-id > ( expression )
4768 static_cast < type-id > ( expression )
4769 reinterpret_cast < type-id > ( expression )
4770 const_cast < type-id > ( expression )
4771 typeid ( expression )
4777 ( type-id ) { initializer-list , [opt] }
4779 This extension is a GNU version of the C99 compound-literal
4780 construct. (The C99 grammar uses `type-name' instead of `type-id',
4781 but they are essentially the same concept.)
4783 If ADDRESS_P is true, the postfix expression is the operand of the
4784 `&' operator. CAST_P is true if this expression is the target of a
4787 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4788 class member access expressions [expr.ref].
4790 Returns a representation of the expression. */
4793 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4794 bool member_access_only_p,
4795 cp_id_kind * pidk_return)
4799 cp_id_kind idk = CP_ID_KIND_NONE;
4800 tree postfix_expression = NULL_TREE;
4801 bool is_member_access = false;
4803 /* Peek at the next token. */
4804 token = cp_lexer_peek_token (parser->lexer);
4805 /* Some of the productions are determined by keywords. */
4806 keyword = token->keyword;
4816 const char *saved_message;
4818 /* All of these can be handled in the same way from the point
4819 of view of parsing. Begin by consuming the token
4820 identifying the cast. */
4821 cp_lexer_consume_token (parser->lexer);
4823 /* New types cannot be defined in the cast. */
4824 saved_message = parser->type_definition_forbidden_message;
4825 parser->type_definition_forbidden_message
4826 = G_("types may not be defined in casts");
4828 /* Look for the opening `<'. */
4829 cp_parser_require (parser, CPP_LESS, RT_LESS);
4830 /* Parse the type to which we are casting. */
4831 type = cp_parser_type_id (parser);
4832 /* Look for the closing `>'. */
4833 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4834 /* Restore the old message. */
4835 parser->type_definition_forbidden_message = saved_message;
4837 /* And the expression which is being cast. */
4838 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4839 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4840 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4842 /* Only type conversions to integral or enumeration types
4843 can be used in constant-expressions. */
4844 if (!cast_valid_in_integral_constant_expression_p (type)
4845 && (cp_parser_non_integral_constant_expression (parser, NIC_CAST)))
4846 return error_mark_node;
4852 = build_dynamic_cast (type, expression, tf_warning_or_error);
4856 = build_static_cast (type, expression, tf_warning_or_error);
4860 = build_reinterpret_cast (type, expression,
4861 tf_warning_or_error);
4865 = build_const_cast (type, expression, tf_warning_or_error);
4876 const char *saved_message;
4877 bool saved_in_type_id_in_expr_p;
4879 /* Consume the `typeid' token. */
4880 cp_lexer_consume_token (parser->lexer);
4881 /* Look for the `(' token. */
4882 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4883 /* Types cannot be defined in a `typeid' expression. */
4884 saved_message = parser->type_definition_forbidden_message;
4885 parser->type_definition_forbidden_message
4886 = G_("types may not be defined in a %<typeid%> expression");
4887 /* We can't be sure yet whether we're looking at a type-id or an
4889 cp_parser_parse_tentatively (parser);
4890 /* Try a type-id first. */
4891 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4892 parser->in_type_id_in_expr_p = true;
4893 type = cp_parser_type_id (parser);
4894 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4895 /* Look for the `)' token. Otherwise, we can't be sure that
4896 we're not looking at an expression: consider `typeid (int
4897 (3))', for example. */
4898 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4899 /* If all went well, simply lookup the type-id. */
4900 if (cp_parser_parse_definitely (parser))
4901 postfix_expression = get_typeid (type);
4902 /* Otherwise, fall back to the expression variant. */
4907 /* Look for an expression. */
4908 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4909 /* Compute its typeid. */
4910 postfix_expression = build_typeid (expression);
4911 /* Look for the `)' token. */
4912 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4914 /* Restore the saved message. */
4915 parser->type_definition_forbidden_message = saved_message;
4916 /* `typeid' may not appear in an integral constant expression. */
4917 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4918 return error_mark_node;
4925 /* The syntax permitted here is the same permitted for an
4926 elaborated-type-specifier. */
4927 type = cp_parser_elaborated_type_specifier (parser,
4928 /*is_friend=*/false,
4929 /*is_declaration=*/false);
4930 postfix_expression = cp_parser_functional_cast (parser, type);
4938 /* If the next thing is a simple-type-specifier, we may be
4939 looking at a functional cast. We could also be looking at
4940 an id-expression. So, we try the functional cast, and if
4941 that doesn't work we fall back to the primary-expression. */
4942 cp_parser_parse_tentatively (parser);
4943 /* Look for the simple-type-specifier. */
4944 type = cp_parser_simple_type_specifier (parser,
4945 /*decl_specs=*/NULL,
4946 CP_PARSER_FLAGS_NONE);
4947 /* Parse the cast itself. */
4948 if (!cp_parser_error_occurred (parser))
4950 = cp_parser_functional_cast (parser, type);
4951 /* If that worked, we're done. */
4952 if (cp_parser_parse_definitely (parser))
4955 /* If the functional-cast didn't work out, try a
4956 compound-literal. */
4957 if (cp_parser_allow_gnu_extensions_p (parser)
4958 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4960 VEC(constructor_elt,gc) *initializer_list = NULL;
4961 bool saved_in_type_id_in_expr_p;
4963 cp_parser_parse_tentatively (parser);
4964 /* Consume the `('. */
4965 cp_lexer_consume_token (parser->lexer);
4966 /* Parse the type. */
4967 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4968 parser->in_type_id_in_expr_p = true;
4969 type = cp_parser_type_id (parser);
4970 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4971 /* Look for the `)'. */
4972 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4973 /* Look for the `{'. */
4974 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4975 /* If things aren't going well, there's no need to
4977 if (!cp_parser_error_occurred (parser))
4979 bool non_constant_p;
4980 /* Parse the initializer-list. */
4982 = cp_parser_initializer_list (parser, &non_constant_p);
4983 /* Allow a trailing `,'. */
4984 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4985 cp_lexer_consume_token (parser->lexer);
4986 /* Look for the final `}'. */
4987 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4989 /* If that worked, we're definitely looking at a
4990 compound-literal expression. */
4991 if (cp_parser_parse_definitely (parser))
4993 /* Warn the user that a compound literal is not
4994 allowed in standard C++. */
4995 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4996 /* For simplicity, we disallow compound literals in
4997 constant-expressions. We could
4998 allow compound literals of integer type, whose
4999 initializer was a constant, in constant
5000 expressions. Permitting that usage, as a further
5001 extension, would not change the meaning of any
5002 currently accepted programs. (Of course, as
5003 compound literals are not part of ISO C++, the
5004 standard has nothing to say.) */
5005 if (cp_parser_non_integral_constant_expression (parser,
5008 postfix_expression = error_mark_node;
5011 /* Form the representation of the compound-literal. */
5013 = (finish_compound_literal
5014 (type, build_constructor (init_list_type_node,
5015 initializer_list)));
5020 /* It must be a primary-expression. */
5022 = cp_parser_primary_expression (parser, address_p, cast_p,
5023 /*template_arg_p=*/false,
5029 /* Keep looping until the postfix-expression is complete. */
5032 if (idk == CP_ID_KIND_UNQUALIFIED
5033 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5034 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5035 /* It is not a Koenig lookup function call. */
5037 = unqualified_name_lookup_error (postfix_expression);
5039 /* Peek at the next token. */
5040 token = cp_lexer_peek_token (parser->lexer);
5042 switch (token->type)
5044 case CPP_OPEN_SQUARE:
5046 = cp_parser_postfix_open_square_expression (parser,
5049 idk = CP_ID_KIND_NONE;
5050 is_member_access = false;
5053 case CPP_OPEN_PAREN:
5054 /* postfix-expression ( expression-list [opt] ) */
5057 bool is_builtin_constant_p;
5058 bool saved_integral_constant_expression_p = false;
5059 bool saved_non_integral_constant_expression_p = false;
5062 is_member_access = false;
5064 is_builtin_constant_p
5065 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5066 if (is_builtin_constant_p)
5068 /* The whole point of __builtin_constant_p is to allow
5069 non-constant expressions to appear as arguments. */
5070 saved_integral_constant_expression_p
5071 = parser->integral_constant_expression_p;
5072 saved_non_integral_constant_expression_p
5073 = parser->non_integral_constant_expression_p;
5074 parser->integral_constant_expression_p = false;
5076 args = (cp_parser_parenthesized_expression_list
5078 /*cast_p=*/false, /*allow_expansion_p=*/true,
5079 /*non_constant_p=*/NULL));
5080 if (is_builtin_constant_p)
5082 parser->integral_constant_expression_p
5083 = saved_integral_constant_expression_p;
5084 parser->non_integral_constant_expression_p
5085 = saved_non_integral_constant_expression_p;
5090 postfix_expression = error_mark_node;
5094 /* Function calls are not permitted in
5095 constant-expressions. */
5096 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5097 && cp_parser_non_integral_constant_expression (parser,
5100 postfix_expression = error_mark_node;
5101 release_tree_vector (args);
5106 if (idk == CP_ID_KIND_UNQUALIFIED
5107 || idk == CP_ID_KIND_TEMPLATE_ID)
5109 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5111 if (!VEC_empty (tree, args))
5114 if (!any_type_dependent_arguments_p (args))
5116 = perform_koenig_lookup (postfix_expression, args);
5120 = unqualified_fn_lookup_error (postfix_expression);
5122 /* We do not perform argument-dependent lookup if
5123 normal lookup finds a non-function, in accordance
5124 with the expected resolution of DR 218. */
5125 else if (!VEC_empty (tree, args)
5126 && is_overloaded_fn (postfix_expression))
5128 tree fn = get_first_fn (postfix_expression);
5129 fn = STRIP_TEMPLATE (fn);
5131 /* Do not do argument dependent lookup if regular
5132 lookup finds a member function or a block-scope
5133 function declaration. [basic.lookup.argdep]/3 */
5134 if (!DECL_FUNCTION_MEMBER_P (fn)
5135 && !DECL_LOCAL_FUNCTION_P (fn))
5138 if (!any_type_dependent_arguments_p (args))
5140 = perform_koenig_lookup (postfix_expression, args);
5145 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5147 tree instance = TREE_OPERAND (postfix_expression, 0);
5148 tree fn = TREE_OPERAND (postfix_expression, 1);
5150 if (processing_template_decl
5151 && (type_dependent_expression_p (instance)
5152 || (!BASELINK_P (fn)
5153 && TREE_CODE (fn) != FIELD_DECL)
5154 || type_dependent_expression_p (fn)
5155 || any_type_dependent_arguments_p (args)))
5158 = build_nt_call_vec (postfix_expression, args);
5159 release_tree_vector (args);
5163 if (BASELINK_P (fn))
5166 = (build_new_method_call
5167 (instance, fn, &args, NULL_TREE,
5168 (idk == CP_ID_KIND_QUALIFIED
5169 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5171 tf_warning_or_error));
5175 = finish_call_expr (postfix_expression, &args,
5176 /*disallow_virtual=*/false,
5178 tf_warning_or_error);
5180 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5181 || TREE_CODE (postfix_expression) == MEMBER_REF
5182 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5183 postfix_expression = (build_offset_ref_call_from_tree
5184 (postfix_expression, &args));
5185 else if (idk == CP_ID_KIND_QUALIFIED)
5186 /* A call to a static class member, or a namespace-scope
5189 = finish_call_expr (postfix_expression, &args,
5190 /*disallow_virtual=*/true,
5192 tf_warning_or_error);
5194 /* All other function calls. */
5196 = finish_call_expr (postfix_expression, &args,
5197 /*disallow_virtual=*/false,
5199 tf_warning_or_error);
5201 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5202 idk = CP_ID_KIND_NONE;
5204 release_tree_vector (args);
5210 /* postfix-expression . template [opt] id-expression
5211 postfix-expression . pseudo-destructor-name
5212 postfix-expression -> template [opt] id-expression
5213 postfix-expression -> pseudo-destructor-name */
5215 /* Consume the `.' or `->' operator. */
5216 cp_lexer_consume_token (parser->lexer);
5219 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5224 is_member_access = true;
5228 /* postfix-expression ++ */
5229 /* Consume the `++' token. */
5230 cp_lexer_consume_token (parser->lexer);
5231 /* Generate a representation for the complete expression. */
5233 = finish_increment_expr (postfix_expression,
5234 POSTINCREMENT_EXPR);
5235 /* Increments may not appear in constant-expressions. */
5236 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5237 postfix_expression = error_mark_node;
5238 idk = CP_ID_KIND_NONE;
5239 is_member_access = false;
5242 case CPP_MINUS_MINUS:
5243 /* postfix-expression -- */
5244 /* Consume the `--' token. */
5245 cp_lexer_consume_token (parser->lexer);
5246 /* Generate a representation for the complete expression. */
5248 = finish_increment_expr (postfix_expression,
5249 POSTDECREMENT_EXPR);
5250 /* Decrements may not appear in constant-expressions. */
5251 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5252 postfix_expression = error_mark_node;
5253 idk = CP_ID_KIND_NONE;
5254 is_member_access = false;
5258 if (pidk_return != NULL)
5259 * pidk_return = idk;
5260 if (member_access_only_p)
5261 return is_member_access? postfix_expression : error_mark_node;
5263 return postfix_expression;
5267 /* We should never get here. */
5269 return error_mark_node;
5272 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5273 by cp_parser_builtin_offsetof. We're looking for
5275 postfix-expression [ expression ]
5277 FOR_OFFSETOF is set if we're being called in that context, which
5278 changes how we deal with integer constant expressions. */
5281 cp_parser_postfix_open_square_expression (cp_parser *parser,
5282 tree postfix_expression,
5287 /* Consume the `[' token. */
5288 cp_lexer_consume_token (parser->lexer);
5290 /* Parse the index expression. */
5291 /* ??? For offsetof, there is a question of what to allow here. If
5292 offsetof is not being used in an integral constant expression context,
5293 then we *could* get the right answer by computing the value at runtime.
5294 If we are in an integral constant expression context, then we might
5295 could accept any constant expression; hard to say without analysis.
5296 Rather than open the barn door too wide right away, allow only integer
5297 constant expressions here. */
5299 index = cp_parser_constant_expression (parser, false, NULL);
5301 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5303 /* Look for the closing `]'. */
5304 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5306 /* Build the ARRAY_REF. */
5307 postfix_expression = grok_array_decl (postfix_expression, index);
5309 /* When not doing offsetof, array references are not permitted in
5310 constant-expressions. */
5312 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5313 postfix_expression = error_mark_node;
5315 return postfix_expression;
5318 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5319 by cp_parser_builtin_offsetof. We're looking for
5321 postfix-expression . template [opt] id-expression
5322 postfix-expression . pseudo-destructor-name
5323 postfix-expression -> template [opt] id-expression
5324 postfix-expression -> pseudo-destructor-name
5326 FOR_OFFSETOF is set if we're being called in that context. That sorta
5327 limits what of the above we'll actually accept, but nevermind.
5328 TOKEN_TYPE is the "." or "->" token, which will already have been
5329 removed from the stream. */
5332 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5333 enum cpp_ttype token_type,
5334 tree postfix_expression,
5335 bool for_offsetof, cp_id_kind *idk,
5336 location_t location)
5340 bool pseudo_destructor_p;
5341 tree scope = NULL_TREE;
5343 /* If this is a `->' operator, dereference the pointer. */
5344 if (token_type == CPP_DEREF)
5345 postfix_expression = build_x_arrow (postfix_expression);
5346 /* Check to see whether or not the expression is type-dependent. */
5347 dependent_p = type_dependent_expression_p (postfix_expression);
5348 /* The identifier following the `->' or `.' is not qualified. */
5349 parser->scope = NULL_TREE;
5350 parser->qualifying_scope = NULL_TREE;
5351 parser->object_scope = NULL_TREE;
5352 *idk = CP_ID_KIND_NONE;
5354 /* Enter the scope corresponding to the type of the object
5355 given by the POSTFIX_EXPRESSION. */
5356 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5358 scope = TREE_TYPE (postfix_expression);
5359 /* According to the standard, no expression should ever have
5360 reference type. Unfortunately, we do not currently match
5361 the standard in this respect in that our internal representation
5362 of an expression may have reference type even when the standard
5363 says it does not. Therefore, we have to manually obtain the
5364 underlying type here. */
5365 scope = non_reference (scope);
5366 /* The type of the POSTFIX_EXPRESSION must be complete. */
5367 if (scope == unknown_type_node)
5369 error_at (location, "%qE does not have class type",
5370 postfix_expression);
5374 scope = complete_type_or_else (scope, NULL_TREE);
5375 /* Let the name lookup machinery know that we are processing a
5376 class member access expression. */
5377 parser->context->object_type = scope;
5378 /* If something went wrong, we want to be able to discern that case,
5379 as opposed to the case where there was no SCOPE due to the type
5380 of expression being dependent. */
5382 scope = error_mark_node;
5383 /* If the SCOPE was erroneous, make the various semantic analysis
5384 functions exit quickly -- and without issuing additional error
5386 if (scope == error_mark_node)
5387 postfix_expression = error_mark_node;
5390 /* Assume this expression is not a pseudo-destructor access. */
5391 pseudo_destructor_p = false;
5393 /* If the SCOPE is a scalar type, then, if this is a valid program,
5394 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5395 is type dependent, it can be pseudo-destructor-name or something else.
5396 Try to parse it as pseudo-destructor-name first. */
5397 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5402 cp_parser_parse_tentatively (parser);
5403 /* Parse the pseudo-destructor-name. */
5405 cp_parser_pseudo_destructor_name (parser, &s, &type);
5407 && (cp_parser_error_occurred (parser)
5408 || TREE_CODE (type) != TYPE_DECL
5409 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5410 cp_parser_abort_tentative_parse (parser);
5411 else if (cp_parser_parse_definitely (parser))
5413 pseudo_destructor_p = true;
5415 = finish_pseudo_destructor_expr (postfix_expression,
5416 s, TREE_TYPE (type));
5420 if (!pseudo_destructor_p)
5422 /* If the SCOPE is not a scalar type, we are looking at an
5423 ordinary class member access expression, rather than a
5424 pseudo-destructor-name. */
5426 cp_token *token = cp_lexer_peek_token (parser->lexer);
5427 /* Parse the id-expression. */
5428 name = (cp_parser_id_expression
5430 cp_parser_optional_template_keyword (parser),
5431 /*check_dependency_p=*/true,
5433 /*declarator_p=*/false,
5434 /*optional_p=*/false));
5435 /* In general, build a SCOPE_REF if the member name is qualified.
5436 However, if the name was not dependent and has already been
5437 resolved; there is no need to build the SCOPE_REF. For example;
5439 struct X { void f(); };
5440 template <typename T> void f(T* t) { t->X::f(); }
5442 Even though "t" is dependent, "X::f" is not and has been resolved
5443 to a BASELINK; there is no need to include scope information. */
5445 /* But we do need to remember that there was an explicit scope for
5446 virtual function calls. */
5448 *idk = CP_ID_KIND_QUALIFIED;
5450 /* If the name is a template-id that names a type, we will get a
5451 TYPE_DECL here. That is invalid code. */
5452 if (TREE_CODE (name) == TYPE_DECL)
5454 error_at (token->location, "invalid use of %qD", name);
5455 postfix_expression = error_mark_node;
5459 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5461 name = build_qualified_name (/*type=*/NULL_TREE,
5465 parser->scope = NULL_TREE;
5466 parser->qualifying_scope = NULL_TREE;
5467 parser->object_scope = NULL_TREE;
5469 if (scope && name && BASELINK_P (name))
5470 adjust_result_of_qualified_name_lookup
5471 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5473 = finish_class_member_access_expr (postfix_expression, name,
5475 tf_warning_or_error);
5479 /* We no longer need to look up names in the scope of the object on
5480 the left-hand side of the `.' or `->' operator. */
5481 parser->context->object_type = NULL_TREE;
5483 /* Outside of offsetof, these operators may not appear in
5484 constant-expressions. */
5486 && (cp_parser_non_integral_constant_expression
5487 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5488 postfix_expression = error_mark_node;
5490 return postfix_expression;
5493 /* Parse a parenthesized expression-list.
5496 assignment-expression
5497 expression-list, assignment-expression
5502 identifier, expression-list
5504 CAST_P is true if this expression is the target of a cast.
5506 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5509 Returns a vector of trees. Each element is a representation of an
5510 assignment-expression. NULL is returned if the ( and or ) are
5511 missing. An empty, but allocated, vector is returned on no
5512 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5513 if we are parsing an attribute list for an attribute that wants a
5514 plain identifier argument, normal_attr for an attribute that wants
5515 an expression, or non_attr if we aren't parsing an attribute list. If
5516 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5517 not all of the expressions in the list were constant. */
5519 static VEC(tree,gc) *
5520 cp_parser_parenthesized_expression_list (cp_parser* parser,
5521 int is_attribute_list,
5523 bool allow_expansion_p,
5524 bool *non_constant_p)
5526 VEC(tree,gc) *expression_list;
5527 bool fold_expr_p = is_attribute_list != non_attr;
5528 tree identifier = NULL_TREE;
5529 bool saved_greater_than_is_operator_p;
5531 /* Assume all the expressions will be constant. */
5533 *non_constant_p = false;
5535 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5538 expression_list = make_tree_vector ();
5540 /* Within a parenthesized expression, a `>' token is always
5541 the greater-than operator. */
5542 saved_greater_than_is_operator_p
5543 = parser->greater_than_is_operator_p;
5544 parser->greater_than_is_operator_p = true;
5546 /* Consume expressions until there are no more. */
5547 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5552 /* At the beginning of attribute lists, check to see if the
5553 next token is an identifier. */
5554 if (is_attribute_list == id_attr
5555 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5559 /* Consume the identifier. */
5560 token = cp_lexer_consume_token (parser->lexer);
5561 /* Save the identifier. */
5562 identifier = token->u.value;
5566 bool expr_non_constant_p;
5568 /* Parse the next assignment-expression. */
5569 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5571 /* A braced-init-list. */
5572 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5573 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5574 if (non_constant_p && expr_non_constant_p)
5575 *non_constant_p = true;
5577 else if (non_constant_p)
5579 expr = (cp_parser_constant_expression
5580 (parser, /*allow_non_constant_p=*/true,
5581 &expr_non_constant_p));
5582 if (expr_non_constant_p)
5583 *non_constant_p = true;
5586 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5589 expr = fold_non_dependent_expr (expr);
5591 /* If we have an ellipsis, then this is an expression
5593 if (allow_expansion_p
5594 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5596 /* Consume the `...'. */
5597 cp_lexer_consume_token (parser->lexer);
5599 /* Build the argument pack. */
5600 expr = make_pack_expansion (expr);
5603 /* Add it to the list. We add error_mark_node
5604 expressions to the list, so that we can still tell if
5605 the correct form for a parenthesized expression-list
5606 is found. That gives better errors. */
5607 VEC_safe_push (tree, gc, expression_list, expr);
5609 if (expr == error_mark_node)
5613 /* After the first item, attribute lists look the same as
5614 expression lists. */
5615 is_attribute_list = non_attr;
5618 /* If the next token isn't a `,', then we are done. */
5619 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5622 /* Otherwise, consume the `,' and keep going. */
5623 cp_lexer_consume_token (parser->lexer);
5626 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5631 /* We try and resync to an unnested comma, as that will give the
5632 user better diagnostics. */
5633 ending = cp_parser_skip_to_closing_parenthesis (parser,
5634 /*recovering=*/true,
5636 /*consume_paren=*/true);
5641 parser->greater_than_is_operator_p
5642 = saved_greater_than_is_operator_p;
5647 parser->greater_than_is_operator_p
5648 = saved_greater_than_is_operator_p;
5651 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5653 return expression_list;
5656 /* Parse a pseudo-destructor-name.
5658 pseudo-destructor-name:
5659 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5660 :: [opt] nested-name-specifier template template-id :: ~ type-name
5661 :: [opt] nested-name-specifier [opt] ~ type-name
5663 If either of the first two productions is used, sets *SCOPE to the
5664 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5665 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5666 or ERROR_MARK_NODE if the parse fails. */
5669 cp_parser_pseudo_destructor_name (cp_parser* parser,
5673 bool nested_name_specifier_p;
5675 /* Assume that things will not work out. */
5676 *type = error_mark_node;
5678 /* Look for the optional `::' operator. */
5679 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5680 /* Look for the optional nested-name-specifier. */
5681 nested_name_specifier_p
5682 = (cp_parser_nested_name_specifier_opt (parser,
5683 /*typename_keyword_p=*/false,
5684 /*check_dependency_p=*/true,
5686 /*is_declaration=*/false)
5688 /* Now, if we saw a nested-name-specifier, we might be doing the
5689 second production. */
5690 if (nested_name_specifier_p
5691 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5693 /* Consume the `template' keyword. */
5694 cp_lexer_consume_token (parser->lexer);
5695 /* Parse the template-id. */
5696 cp_parser_template_id (parser,
5697 /*template_keyword_p=*/true,
5698 /*check_dependency_p=*/false,
5699 /*is_declaration=*/true);
5700 /* Look for the `::' token. */
5701 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5703 /* If the next token is not a `~', then there might be some
5704 additional qualification. */
5705 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5707 /* At this point, we're looking for "type-name :: ~". The type-name
5708 must not be a class-name, since this is a pseudo-destructor. So,
5709 it must be either an enum-name, or a typedef-name -- both of which
5710 are just identifiers. So, we peek ahead to check that the "::"
5711 and "~" tokens are present; if they are not, then we can avoid
5712 calling type_name. */
5713 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5714 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5715 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5717 cp_parser_error (parser, "non-scalar type");
5721 /* Look for the type-name. */
5722 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5723 if (*scope == error_mark_node)
5726 /* Look for the `::' token. */
5727 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5732 /* Look for the `~'. */
5733 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5734 /* Look for the type-name again. We are not responsible for
5735 checking that it matches the first type-name. */
5736 *type = cp_parser_nonclass_name (parser);
5739 /* Parse a unary-expression.
5745 unary-operator cast-expression
5746 sizeof unary-expression
5754 __extension__ cast-expression
5755 __alignof__ unary-expression
5756 __alignof__ ( type-id )
5757 __real__ cast-expression
5758 __imag__ cast-expression
5761 ADDRESS_P is true iff the unary-expression is appearing as the
5762 operand of the `&' operator. CAST_P is true if this expression is
5763 the target of a cast.
5765 Returns a representation of the expression. */
5768 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5772 enum tree_code unary_operator;
5774 /* Peek at the next token. */
5775 token = cp_lexer_peek_token (parser->lexer);
5776 /* Some keywords give away the kind of expression. */
5777 if (token->type == CPP_KEYWORD)
5779 enum rid keyword = token->keyword;
5789 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5790 /* Consume the token. */
5791 cp_lexer_consume_token (parser->lexer);
5792 /* Parse the operand. */
5793 operand = cp_parser_sizeof_operand (parser, keyword);
5795 if (TYPE_P (operand))
5796 return cxx_sizeof_or_alignof_type (operand, op, true);
5798 return cxx_sizeof_or_alignof_expr (operand, op, true);
5802 return cp_parser_new_expression (parser);
5805 return cp_parser_delete_expression (parser);
5809 /* The saved value of the PEDANTIC flag. */
5813 /* Save away the PEDANTIC flag. */
5814 cp_parser_extension_opt (parser, &saved_pedantic);
5815 /* Parse the cast-expression. */
5816 expr = cp_parser_simple_cast_expression (parser);
5817 /* Restore the PEDANTIC flag. */
5818 pedantic = saved_pedantic;
5828 /* Consume the `__real__' or `__imag__' token. */
5829 cp_lexer_consume_token (parser->lexer);
5830 /* Parse the cast-expression. */
5831 expression = cp_parser_simple_cast_expression (parser);
5832 /* Create the complete representation. */
5833 return build_x_unary_op ((keyword == RID_REALPART
5834 ? REALPART_EXPR : IMAGPART_EXPR),
5836 tf_warning_or_error);
5845 /* Look for the `:: new' and `:: delete', which also signal the
5846 beginning of a new-expression, or delete-expression,
5847 respectively. If the next token is `::', then it might be one of
5849 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5853 /* See if the token after the `::' is one of the keywords in
5854 which we're interested. */
5855 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5856 /* If it's `new', we have a new-expression. */
5857 if (keyword == RID_NEW)
5858 return cp_parser_new_expression (parser);
5859 /* Similarly, for `delete'. */
5860 else if (keyword == RID_DELETE)
5861 return cp_parser_delete_expression (parser);
5864 /* Look for a unary operator. */
5865 unary_operator = cp_parser_unary_operator (token);
5866 /* The `++' and `--' operators can be handled similarly, even though
5867 they are not technically unary-operators in the grammar. */
5868 if (unary_operator == ERROR_MARK)
5870 if (token->type == CPP_PLUS_PLUS)
5871 unary_operator = PREINCREMENT_EXPR;
5872 else if (token->type == CPP_MINUS_MINUS)
5873 unary_operator = PREDECREMENT_EXPR;
5874 /* Handle the GNU address-of-label extension. */
5875 else if (cp_parser_allow_gnu_extensions_p (parser)
5876 && token->type == CPP_AND_AND)
5880 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5882 /* Consume the '&&' token. */
5883 cp_lexer_consume_token (parser->lexer);
5884 /* Look for the identifier. */
5885 identifier = cp_parser_identifier (parser);
5886 /* Create an expression representing the address. */
5887 expression = finish_label_address_expr (identifier, loc);
5888 if (cp_parser_non_integral_constant_expression (parser,
5890 expression = error_mark_node;
5894 if (unary_operator != ERROR_MARK)
5896 tree cast_expression;
5897 tree expression = error_mark_node;
5898 non_integral_constant non_constant_p = 0;
5900 /* Consume the operator token. */
5901 token = cp_lexer_consume_token (parser->lexer);
5902 /* Parse the cast-expression. */
5904 = cp_parser_cast_expression (parser,
5905 unary_operator == ADDR_EXPR,
5906 /*cast_p=*/false, pidk);
5907 /* Now, build an appropriate representation. */
5908 switch (unary_operator)
5911 non_constant_p = NIC_STAR;
5912 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5913 tf_warning_or_error);
5917 non_constant_p = NIC_ADDR;
5920 expression = build_x_unary_op (unary_operator, cast_expression,
5921 tf_warning_or_error);
5924 case PREINCREMENT_EXPR:
5925 case PREDECREMENT_EXPR:
5926 non_constant_p = unary_operator == PREINCREMENT_EXPR
5927 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5929 case UNARY_PLUS_EXPR:
5931 case TRUTH_NOT_EXPR:
5932 expression = finish_unary_op_expr (unary_operator, cast_expression);
5940 && cp_parser_non_integral_constant_expression (parser,
5942 expression = error_mark_node;
5947 return cp_parser_postfix_expression (parser, address_p, cast_p,
5948 /*member_access_only_p=*/false,
5952 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5953 unary-operator, the corresponding tree code is returned. */
5955 static enum tree_code
5956 cp_parser_unary_operator (cp_token* token)
5958 switch (token->type)
5961 return INDIRECT_REF;
5967 return UNARY_PLUS_EXPR;
5973 return TRUTH_NOT_EXPR;
5976 return BIT_NOT_EXPR;
5983 /* Parse a new-expression.
5986 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5987 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5989 Returns a representation of the expression. */
5992 cp_parser_new_expression (cp_parser* parser)
5994 bool global_scope_p;
5995 VEC(tree,gc) *placement;
5997 VEC(tree,gc) *initializer;
6001 /* Look for the optional `::' operator. */
6003 = (cp_parser_global_scope_opt (parser,
6004 /*current_scope_valid_p=*/false)
6006 /* Look for the `new' operator. */
6007 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6008 /* There's no easy way to tell a new-placement from the
6009 `( type-id )' construct. */
6010 cp_parser_parse_tentatively (parser);
6011 /* Look for a new-placement. */
6012 placement = cp_parser_new_placement (parser);
6013 /* If that didn't work out, there's no new-placement. */
6014 if (!cp_parser_parse_definitely (parser))
6016 if (placement != NULL)
6017 release_tree_vector (placement);
6021 /* If the next token is a `(', then we have a parenthesized
6023 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6026 /* Consume the `('. */
6027 cp_lexer_consume_token (parser->lexer);
6028 /* Parse the type-id. */
6029 type = cp_parser_type_id (parser);
6030 /* Look for the closing `)'. */
6031 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6032 token = cp_lexer_peek_token (parser->lexer);
6033 /* There should not be a direct-new-declarator in this production,
6034 but GCC used to allowed this, so we check and emit a sensible error
6035 message for this case. */
6036 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6038 error_at (token->location,
6039 "array bound forbidden after parenthesized type-id");
6040 inform (token->location,
6041 "try removing the parentheses around the type-id");
6042 cp_parser_direct_new_declarator (parser);
6046 /* Otherwise, there must be a new-type-id. */
6048 type = cp_parser_new_type_id (parser, &nelts);
6050 /* If the next token is a `(' or '{', then we have a new-initializer. */
6051 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6052 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6053 initializer = cp_parser_new_initializer (parser);
6057 /* A new-expression may not appear in an integral constant
6059 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6060 ret = error_mark_node;
6063 /* Create a representation of the new-expression. */
6064 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6065 tf_warning_or_error);
6068 if (placement != NULL)
6069 release_tree_vector (placement);
6070 if (initializer != NULL)
6071 release_tree_vector (initializer);
6076 /* Parse a new-placement.
6081 Returns the same representation as for an expression-list. */
6083 static VEC(tree,gc) *
6084 cp_parser_new_placement (cp_parser* parser)
6086 VEC(tree,gc) *expression_list;
6088 /* Parse the expression-list. */
6089 expression_list = (cp_parser_parenthesized_expression_list
6090 (parser, non_attr, /*cast_p=*/false,
6091 /*allow_expansion_p=*/true,
6092 /*non_constant_p=*/NULL));
6094 return expression_list;
6097 /* Parse a new-type-id.
6100 type-specifier-seq new-declarator [opt]
6102 Returns the TYPE allocated. If the new-type-id indicates an array
6103 type, *NELTS is set to the number of elements in the last array
6104 bound; the TYPE will not include the last array bound. */
6107 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6109 cp_decl_specifier_seq type_specifier_seq;
6110 cp_declarator *new_declarator;
6111 cp_declarator *declarator;
6112 cp_declarator *outer_declarator;
6113 const char *saved_message;
6116 /* The type-specifier sequence must not contain type definitions.
6117 (It cannot contain declarations of new types either, but if they
6118 are not definitions we will catch that because they are not
6120 saved_message = parser->type_definition_forbidden_message;
6121 parser->type_definition_forbidden_message
6122 = G_("types may not be defined in a new-type-id");
6123 /* Parse the type-specifier-seq. */
6124 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6125 /*is_trailing_return=*/false,
6126 &type_specifier_seq);
6127 /* Restore the old message. */
6128 parser->type_definition_forbidden_message = saved_message;
6129 /* Parse the new-declarator. */
6130 new_declarator = cp_parser_new_declarator_opt (parser);
6132 /* Determine the number of elements in the last array dimension, if
6135 /* Skip down to the last array dimension. */
6136 declarator = new_declarator;
6137 outer_declarator = NULL;
6138 while (declarator && (declarator->kind == cdk_pointer
6139 || declarator->kind == cdk_ptrmem))
6141 outer_declarator = declarator;
6142 declarator = declarator->declarator;
6145 && declarator->kind == cdk_array
6146 && declarator->declarator
6147 && declarator->declarator->kind == cdk_array)
6149 outer_declarator = declarator;
6150 declarator = declarator->declarator;
6153 if (declarator && declarator->kind == cdk_array)
6155 *nelts = declarator->u.array.bounds;
6156 if (*nelts == error_mark_node)
6157 *nelts = integer_one_node;
6159 if (outer_declarator)
6160 outer_declarator->declarator = declarator->declarator;
6162 new_declarator = NULL;
6165 type = groktypename (&type_specifier_seq, new_declarator, false);
6169 /* Parse an (optional) new-declarator.
6172 ptr-operator new-declarator [opt]
6173 direct-new-declarator
6175 Returns the declarator. */
6177 static cp_declarator *
6178 cp_parser_new_declarator_opt (cp_parser* parser)
6180 enum tree_code code;
6182 cp_cv_quals cv_quals;
6184 /* We don't know if there's a ptr-operator next, or not. */
6185 cp_parser_parse_tentatively (parser);
6186 /* Look for a ptr-operator. */
6187 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6188 /* If that worked, look for more new-declarators. */
6189 if (cp_parser_parse_definitely (parser))
6191 cp_declarator *declarator;
6193 /* Parse another optional declarator. */
6194 declarator = cp_parser_new_declarator_opt (parser);
6196 return cp_parser_make_indirect_declarator
6197 (code, type, cv_quals, declarator);
6200 /* If the next token is a `[', there is a direct-new-declarator. */
6201 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6202 return cp_parser_direct_new_declarator (parser);
6207 /* Parse a direct-new-declarator.
6209 direct-new-declarator:
6211 direct-new-declarator [constant-expression]
6215 static cp_declarator *
6216 cp_parser_direct_new_declarator (cp_parser* parser)
6218 cp_declarator *declarator = NULL;
6224 /* Look for the opening `['. */
6225 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6226 /* The first expression is not required to be constant. */
6229 cp_token *token = cp_lexer_peek_token (parser->lexer);
6230 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6231 /* The standard requires that the expression have integral
6232 type. DR 74 adds enumeration types. We believe that the
6233 real intent is that these expressions be handled like the
6234 expression in a `switch' condition, which also allows
6235 classes with a single conversion to integral or
6236 enumeration type. */
6237 if (!processing_template_decl)
6240 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6245 error_at (token->location,
6246 "expression in new-declarator must have integral "
6247 "or enumeration type");
6248 expression = error_mark_node;
6252 /* But all the other expressions must be. */
6255 = cp_parser_constant_expression (parser,
6256 /*allow_non_constant=*/false,
6258 /* Look for the closing `]'. */
6259 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6261 /* Add this bound to the declarator. */
6262 declarator = make_array_declarator (declarator, expression);
6264 /* If the next token is not a `[', then there are no more
6266 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6273 /* Parse a new-initializer.
6276 ( expression-list [opt] )
6279 Returns a representation of the expression-list. */
6281 static VEC(tree,gc) *
6282 cp_parser_new_initializer (cp_parser* parser)
6284 VEC(tree,gc) *expression_list;
6286 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6289 bool expr_non_constant_p;
6290 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6291 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6292 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6293 expression_list = make_tree_vector_single (t);
6296 expression_list = (cp_parser_parenthesized_expression_list
6297 (parser, non_attr, /*cast_p=*/false,
6298 /*allow_expansion_p=*/true,
6299 /*non_constant_p=*/NULL));
6301 return expression_list;
6304 /* Parse a delete-expression.
6307 :: [opt] delete cast-expression
6308 :: [opt] delete [ ] cast-expression
6310 Returns a representation of the expression. */
6313 cp_parser_delete_expression (cp_parser* parser)
6315 bool global_scope_p;
6319 /* Look for the optional `::' operator. */
6321 = (cp_parser_global_scope_opt (parser,
6322 /*current_scope_valid_p=*/false)
6324 /* Look for the `delete' keyword. */
6325 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6326 /* See if the array syntax is in use. */
6327 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6329 /* Consume the `[' token. */
6330 cp_lexer_consume_token (parser->lexer);
6331 /* Look for the `]' token. */
6332 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6333 /* Remember that this is the `[]' construct. */
6339 /* Parse the cast-expression. */
6340 expression = cp_parser_simple_cast_expression (parser);
6342 /* A delete-expression may not appear in an integral constant
6344 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6345 return error_mark_node;
6347 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6350 /* Returns true if TOKEN may start a cast-expression and false
6354 cp_parser_token_starts_cast_expression (cp_token *token)
6356 switch (token->type)
6362 case CPP_CLOSE_SQUARE:
6363 case CPP_CLOSE_PAREN:
6364 case CPP_CLOSE_BRACE:
6368 case CPP_DEREF_STAR:
6376 case CPP_GREATER_EQ:
6396 /* '[' may start a primary-expression in obj-c++. */
6397 case CPP_OPEN_SQUARE:
6398 return c_dialect_objc ();
6405 /* Parse a cast-expression.
6409 ( type-id ) cast-expression
6411 ADDRESS_P is true iff the unary-expression is appearing as the
6412 operand of the `&' operator. CAST_P is true if this expression is
6413 the target of a cast.
6415 Returns a representation of the expression. */
6418 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6421 /* If it's a `(', then we might be looking at a cast. */
6422 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6424 tree type = NULL_TREE;
6425 tree expr = NULL_TREE;
6426 bool compound_literal_p;
6427 const char *saved_message;
6429 /* There's no way to know yet whether or not this is a cast.
6430 For example, `(int (3))' is a unary-expression, while `(int)
6431 3' is a cast. So, we resort to parsing tentatively. */
6432 cp_parser_parse_tentatively (parser);
6433 /* Types may not be defined in a cast. */
6434 saved_message = parser->type_definition_forbidden_message;
6435 parser->type_definition_forbidden_message
6436 = G_("types may not be defined in casts");
6437 /* Consume the `('. */
6438 cp_lexer_consume_token (parser->lexer);
6439 /* A very tricky bit is that `(struct S) { 3 }' is a
6440 compound-literal (which we permit in C++ as an extension).
6441 But, that construct is not a cast-expression -- it is a
6442 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6443 is legal; if the compound-literal were a cast-expression,
6444 you'd need an extra set of parentheses.) But, if we parse
6445 the type-id, and it happens to be a class-specifier, then we
6446 will commit to the parse at that point, because we cannot
6447 undo the action that is done when creating a new class. So,
6448 then we cannot back up and do a postfix-expression.
6450 Therefore, we scan ahead to the closing `)', and check to see
6451 if the token after the `)' is a `{'. If so, we are not
6452 looking at a cast-expression.
6454 Save tokens so that we can put them back. */
6455 cp_lexer_save_tokens (parser->lexer);
6456 /* Skip tokens until the next token is a closing parenthesis.
6457 If we find the closing `)', and the next token is a `{', then
6458 we are looking at a compound-literal. */
6460 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6461 /*consume_paren=*/true)
6462 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6463 /* Roll back the tokens we skipped. */
6464 cp_lexer_rollback_tokens (parser->lexer);
6465 /* If we were looking at a compound-literal, simulate an error
6466 so that the call to cp_parser_parse_definitely below will
6468 if (compound_literal_p)
6469 cp_parser_simulate_error (parser);
6472 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6473 parser->in_type_id_in_expr_p = true;
6474 /* Look for the type-id. */
6475 type = cp_parser_type_id (parser);
6476 /* Look for the closing `)'. */
6477 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6478 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6481 /* Restore the saved message. */
6482 parser->type_definition_forbidden_message = saved_message;
6484 /* At this point this can only be either a cast or a
6485 parenthesized ctor such as `(T ())' that looks like a cast to
6486 function returning T. */
6487 if (!cp_parser_error_occurred (parser)
6488 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6491 cp_parser_parse_definitely (parser);
6492 expr = cp_parser_cast_expression (parser,
6493 /*address_p=*/false,
6494 /*cast_p=*/true, pidk);
6496 /* Warn about old-style casts, if so requested. */
6497 if (warn_old_style_cast
6498 && !in_system_header
6499 && !VOID_TYPE_P (type)
6500 && current_lang_name != lang_name_c)
6501 warning (OPT_Wold_style_cast, "use of old-style cast");
6503 /* Only type conversions to integral or enumeration types
6504 can be used in constant-expressions. */
6505 if (!cast_valid_in_integral_constant_expression_p (type)
6506 && (cp_parser_non_integral_constant_expression (parser,
6508 return error_mark_node;
6510 /* Perform the cast. */
6511 expr = build_c_cast (input_location, type, expr);
6515 cp_parser_abort_tentative_parse (parser);
6518 /* If we get here, then it's not a cast, so it must be a
6519 unary-expression. */
6520 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6523 /* Parse a binary expression of the general form:
6527 pm-expression .* cast-expression
6528 pm-expression ->* cast-expression
6530 multiplicative-expression:
6532 multiplicative-expression * pm-expression
6533 multiplicative-expression / pm-expression
6534 multiplicative-expression % pm-expression
6536 additive-expression:
6537 multiplicative-expression
6538 additive-expression + multiplicative-expression
6539 additive-expression - multiplicative-expression
6543 shift-expression << additive-expression
6544 shift-expression >> additive-expression
6546 relational-expression:
6548 relational-expression < shift-expression
6549 relational-expression > shift-expression
6550 relational-expression <= shift-expression
6551 relational-expression >= shift-expression
6555 relational-expression:
6556 relational-expression <? shift-expression
6557 relational-expression >? shift-expression
6559 equality-expression:
6560 relational-expression
6561 equality-expression == relational-expression
6562 equality-expression != relational-expression
6566 and-expression & equality-expression
6568 exclusive-or-expression:
6570 exclusive-or-expression ^ and-expression
6572 inclusive-or-expression:
6573 exclusive-or-expression
6574 inclusive-or-expression | exclusive-or-expression
6576 logical-and-expression:
6577 inclusive-or-expression
6578 logical-and-expression && inclusive-or-expression
6580 logical-or-expression:
6581 logical-and-expression
6582 logical-or-expression || logical-and-expression
6584 All these are implemented with a single function like:
6587 simple-cast-expression
6588 binary-expression <token> binary-expression
6590 CAST_P is true if this expression is the target of a cast.
6592 The binops_by_token map is used to get the tree codes for each <token> type.
6593 binary-expressions are associated according to a precedence table. */
6595 #define TOKEN_PRECEDENCE(token) \
6596 (((token->type == CPP_GREATER \
6597 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6598 && !parser->greater_than_is_operator_p) \
6599 ? PREC_NOT_OPERATOR \
6600 : binops_by_token[token->type].prec)
6603 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6604 bool no_toplevel_fold_p,
6605 enum cp_parser_prec prec,
6608 cp_parser_expression_stack stack;
6609 cp_parser_expression_stack_entry *sp = &stack[0];
6612 enum tree_code tree_type, lhs_type, rhs_type;
6613 enum cp_parser_prec new_prec, lookahead_prec;
6616 /* Parse the first expression. */
6617 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6618 lhs_type = ERROR_MARK;
6622 /* Get an operator token. */
6623 token = cp_lexer_peek_token (parser->lexer);
6625 if (warn_cxx0x_compat
6626 && token->type == CPP_RSHIFT
6627 && !parser->greater_than_is_operator_p)
6629 if (warning_at (token->location, OPT_Wc__0x_compat,
6630 "%<>>%> operator will be treated as"
6631 " two right angle brackets in C++0x"))
6632 inform (token->location,
6633 "suggest parentheses around %<>>%> expression");
6636 new_prec = TOKEN_PRECEDENCE (token);
6638 /* Popping an entry off the stack means we completed a subexpression:
6639 - either we found a token which is not an operator (`>' where it is not
6640 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6641 will happen repeatedly;
6642 - or, we found an operator which has lower priority. This is the case
6643 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6645 if (new_prec <= prec)
6654 tree_type = binops_by_token[token->type].tree_type;
6656 /* We used the operator token. */
6657 cp_lexer_consume_token (parser->lexer);
6659 /* For "false && x" or "true || x", x will never be executed;
6660 disable warnings while evaluating it. */
6661 if (tree_type == TRUTH_ANDIF_EXPR)
6662 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6663 else if (tree_type == TRUTH_ORIF_EXPR)
6664 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6666 /* Extract another operand. It may be the RHS of this expression
6667 or the LHS of a new, higher priority expression. */
6668 rhs = cp_parser_simple_cast_expression (parser);
6669 rhs_type = ERROR_MARK;
6671 /* Get another operator token. Look up its precedence to avoid
6672 building a useless (immediately popped) stack entry for common
6673 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6674 token = cp_lexer_peek_token (parser->lexer);
6675 lookahead_prec = TOKEN_PRECEDENCE (token);
6676 if (lookahead_prec > new_prec)
6678 /* ... and prepare to parse the RHS of the new, higher priority
6679 expression. Since precedence levels on the stack are
6680 monotonically increasing, we do not have to care about
6683 sp->tree_type = tree_type;
6685 sp->lhs_type = lhs_type;
6688 lhs_type = rhs_type;
6690 new_prec = lookahead_prec;
6694 lookahead_prec = new_prec;
6695 /* If the stack is not empty, we have parsed into LHS the right side
6696 (`4' in the example above) of an expression we had suspended.
6697 We can use the information on the stack to recover the LHS (`3')
6698 from the stack together with the tree code (`MULT_EXPR'), and
6699 the precedence of the higher level subexpression
6700 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6701 which will be used to actually build the additive expression. */
6704 tree_type = sp->tree_type;
6706 rhs_type = lhs_type;
6708 lhs_type = sp->lhs_type;
6711 /* Undo the disabling of warnings done above. */
6712 if (tree_type == TRUTH_ANDIF_EXPR)
6713 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6714 else if (tree_type == TRUTH_ORIF_EXPR)
6715 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6717 overloaded_p = false;
6718 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6719 ERROR_MARK for everything that is not a binary expression.
6720 This makes warn_about_parentheses miss some warnings that
6721 involve unary operators. For unary expressions we should
6722 pass the correct tree_code unless the unary expression was
6723 surrounded by parentheses.
6725 if (no_toplevel_fold_p
6726 && lookahead_prec <= prec
6728 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6729 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6731 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6732 &overloaded_p, tf_warning_or_error);
6733 lhs_type = tree_type;
6735 /* If the binary operator required the use of an overloaded operator,
6736 then this expression cannot be an integral constant-expression.
6737 An overloaded operator can be used even if both operands are
6738 otherwise permissible in an integral constant-expression if at
6739 least one of the operands is of enumeration type. */
6742 && (cp_parser_non_integral_constant_expression (parser,
6744 return error_mark_node;
6751 /* Parse the `? expression : assignment-expression' part of a
6752 conditional-expression. The LOGICAL_OR_EXPR is the
6753 logical-or-expression that started the conditional-expression.
6754 Returns a representation of the entire conditional-expression.
6756 This routine is used by cp_parser_assignment_expression.
6758 ? expression : assignment-expression
6762 ? : assignment-expression */
6765 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6768 tree assignment_expr;
6770 /* Consume the `?' token. */
6771 cp_lexer_consume_token (parser->lexer);
6772 if (cp_parser_allow_gnu_extensions_p (parser)
6773 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6775 /* Implicit true clause. */
6777 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6781 /* Parse the expression. */
6782 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6783 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6784 c_inhibit_evaluation_warnings +=
6785 ((logical_or_expr == truthvalue_true_node)
6786 - (logical_or_expr == truthvalue_false_node));
6789 /* The next token should be a `:'. */
6790 cp_parser_require (parser, CPP_COLON, RT_COLON);
6791 /* Parse the assignment-expression. */
6792 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6793 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6795 /* Build the conditional-expression. */
6796 return build_x_conditional_expr (logical_or_expr,
6799 tf_warning_or_error);
6802 /* Parse an assignment-expression.
6804 assignment-expression:
6805 conditional-expression
6806 logical-or-expression assignment-operator assignment_expression
6809 CAST_P is true if this expression is the target of a cast.
6811 Returns a representation for the expression. */
6814 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6819 /* If the next token is the `throw' keyword, then we're looking at
6820 a throw-expression. */
6821 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6822 expr = cp_parser_throw_expression (parser);
6823 /* Otherwise, it must be that we are looking at a
6824 logical-or-expression. */
6827 /* Parse the binary expressions (logical-or-expression). */
6828 expr = cp_parser_binary_expression (parser, cast_p, false,
6829 PREC_NOT_OPERATOR, pidk);
6830 /* If the next token is a `?' then we're actually looking at a
6831 conditional-expression. */
6832 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6833 return cp_parser_question_colon_clause (parser, expr);
6836 enum tree_code assignment_operator;
6838 /* If it's an assignment-operator, we're using the second
6841 = cp_parser_assignment_operator_opt (parser);
6842 if (assignment_operator != ERROR_MARK)
6844 bool non_constant_p;
6846 /* Parse the right-hand side of the assignment. */
6847 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6849 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6850 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6852 /* An assignment may not appear in a
6853 constant-expression. */
6854 if (cp_parser_non_integral_constant_expression (parser,
6856 return error_mark_node;
6857 /* Build the assignment expression. */
6858 expr = build_x_modify_expr (expr,
6859 assignment_operator,
6861 tf_warning_or_error);
6869 /* Parse an (optional) assignment-operator.
6871 assignment-operator: one of
6872 = *= /= %= += -= >>= <<= &= ^= |=
6876 assignment-operator: one of
6879 If the next token is an assignment operator, the corresponding tree
6880 code is returned, and the token is consumed. For example, for
6881 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6882 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6883 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6884 operator, ERROR_MARK is returned. */
6886 static enum tree_code
6887 cp_parser_assignment_operator_opt (cp_parser* parser)
6892 /* Peek at the next token. */
6893 token = cp_lexer_peek_token (parser->lexer);
6895 switch (token->type)
6906 op = TRUNC_DIV_EXPR;
6910 op = TRUNC_MOD_EXPR;
6942 /* Nothing else is an assignment operator. */
6946 /* If it was an assignment operator, consume it. */
6947 if (op != ERROR_MARK)
6948 cp_lexer_consume_token (parser->lexer);
6953 /* Parse an expression.
6956 assignment-expression
6957 expression , assignment-expression
6959 CAST_P is true if this expression is the target of a cast.
6961 Returns a representation of the expression. */
6964 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6966 tree expression = NULL_TREE;
6970 tree assignment_expression;
6972 /* Parse the next assignment-expression. */
6973 assignment_expression
6974 = cp_parser_assignment_expression (parser, cast_p, pidk);
6975 /* If this is the first assignment-expression, we can just
6978 expression = assignment_expression;
6980 expression = build_x_compound_expr (expression,
6981 assignment_expression,
6982 tf_warning_or_error);
6983 /* If the next token is not a comma, then we are done with the
6985 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6987 /* Consume the `,'. */
6988 cp_lexer_consume_token (parser->lexer);
6989 /* A comma operator cannot appear in a constant-expression. */
6990 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
6991 expression = error_mark_node;
6997 /* Parse a constant-expression.
6999 constant-expression:
7000 conditional-expression
7002 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7003 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7004 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7005 is false, NON_CONSTANT_P should be NULL. */
7008 cp_parser_constant_expression (cp_parser* parser,
7009 bool allow_non_constant_p,
7010 bool *non_constant_p)
7012 bool saved_integral_constant_expression_p;
7013 bool saved_allow_non_integral_constant_expression_p;
7014 bool saved_non_integral_constant_expression_p;
7017 /* It might seem that we could simply parse the
7018 conditional-expression, and then check to see if it were
7019 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7020 one that the compiler can figure out is constant, possibly after
7021 doing some simplifications or optimizations. The standard has a
7022 precise definition of constant-expression, and we must honor
7023 that, even though it is somewhat more restrictive.
7029 is not a legal declaration, because `(2, 3)' is not a
7030 constant-expression. The `,' operator is forbidden in a
7031 constant-expression. However, GCC's constant-folding machinery
7032 will fold this operation to an INTEGER_CST for `3'. */
7034 /* Save the old settings. */
7035 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7036 saved_allow_non_integral_constant_expression_p
7037 = parser->allow_non_integral_constant_expression_p;
7038 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7039 /* We are now parsing a constant-expression. */
7040 parser->integral_constant_expression_p = true;
7041 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
7042 parser->non_integral_constant_expression_p = false;
7043 /* Although the grammar says "conditional-expression", we parse an
7044 "assignment-expression", which also permits "throw-expression"
7045 and the use of assignment operators. In the case that
7046 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7047 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7048 actually essential that we look for an assignment-expression.
7049 For example, cp_parser_initializer_clauses uses this function to
7050 determine whether a particular assignment-expression is in fact
7052 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7053 /* Restore the old settings. */
7054 parser->integral_constant_expression_p
7055 = saved_integral_constant_expression_p;
7056 parser->allow_non_integral_constant_expression_p
7057 = saved_allow_non_integral_constant_expression_p;
7058 if (allow_non_constant_p)
7059 *non_constant_p = parser->non_integral_constant_expression_p;
7060 else if (parser->non_integral_constant_expression_p)
7061 expression = error_mark_node;
7062 parser->non_integral_constant_expression_p
7063 = saved_non_integral_constant_expression_p;
7068 /* Parse __builtin_offsetof.
7070 offsetof-expression:
7071 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7073 offsetof-member-designator:
7075 | offsetof-member-designator "." id-expression
7076 | offsetof-member-designator "[" expression "]"
7077 | offsetof-member-designator "->" id-expression */
7080 cp_parser_builtin_offsetof (cp_parser *parser)
7082 int save_ice_p, save_non_ice_p;
7087 /* We're about to accept non-integral-constant things, but will
7088 definitely yield an integral constant expression. Save and
7089 restore these values around our local parsing. */
7090 save_ice_p = parser->integral_constant_expression_p;
7091 save_non_ice_p = parser->non_integral_constant_expression_p;
7093 /* Consume the "__builtin_offsetof" token. */
7094 cp_lexer_consume_token (parser->lexer);
7095 /* Consume the opening `('. */
7096 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7097 /* Parse the type-id. */
7098 type = cp_parser_type_id (parser);
7099 /* Look for the `,'. */
7100 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7101 token = cp_lexer_peek_token (parser->lexer);
7103 /* Build the (type *)null that begins the traditional offsetof macro. */
7104 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7105 tf_warning_or_error);
7107 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7108 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7109 true, &dummy, token->location);
7112 token = cp_lexer_peek_token (parser->lexer);
7113 switch (token->type)
7115 case CPP_OPEN_SQUARE:
7116 /* offsetof-member-designator "[" expression "]" */
7117 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7121 /* offsetof-member-designator "->" identifier */
7122 expr = grok_array_decl (expr, integer_zero_node);
7126 /* offsetof-member-designator "." identifier */
7127 cp_lexer_consume_token (parser->lexer);
7128 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7133 case CPP_CLOSE_PAREN:
7134 /* Consume the ")" token. */
7135 cp_lexer_consume_token (parser->lexer);
7139 /* Error. We know the following require will fail, but
7140 that gives the proper error message. */
7141 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7142 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7143 expr = error_mark_node;
7149 /* If we're processing a template, we can't finish the semantics yet.
7150 Otherwise we can fold the entire expression now. */
7151 if (processing_template_decl)
7152 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7154 expr = finish_offsetof (expr);
7157 parser->integral_constant_expression_p = save_ice_p;
7158 parser->non_integral_constant_expression_p = save_non_ice_p;
7163 /* Parse a trait expression. */
7166 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7169 tree type1, type2 = NULL_TREE;
7170 bool binary = false;
7171 cp_decl_specifier_seq decl_specs;
7175 case RID_HAS_NOTHROW_ASSIGN:
7176 kind = CPTK_HAS_NOTHROW_ASSIGN;
7178 case RID_HAS_NOTHROW_CONSTRUCTOR:
7179 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7181 case RID_HAS_NOTHROW_COPY:
7182 kind = CPTK_HAS_NOTHROW_COPY;
7184 case RID_HAS_TRIVIAL_ASSIGN:
7185 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7187 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7188 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7190 case RID_HAS_TRIVIAL_COPY:
7191 kind = CPTK_HAS_TRIVIAL_COPY;
7193 case RID_HAS_TRIVIAL_DESTRUCTOR:
7194 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7196 case RID_HAS_VIRTUAL_DESTRUCTOR:
7197 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7199 case RID_IS_ABSTRACT:
7200 kind = CPTK_IS_ABSTRACT;
7202 case RID_IS_BASE_OF:
7203 kind = CPTK_IS_BASE_OF;
7207 kind = CPTK_IS_CLASS;
7209 case RID_IS_CONVERTIBLE_TO:
7210 kind = CPTK_IS_CONVERTIBLE_TO;
7214 kind = CPTK_IS_EMPTY;
7217 kind = CPTK_IS_ENUM;
7222 case RID_IS_POLYMORPHIC:
7223 kind = CPTK_IS_POLYMORPHIC;
7225 case RID_IS_STD_LAYOUT:
7226 kind = CPTK_IS_STD_LAYOUT;
7228 case RID_IS_TRIVIAL:
7229 kind = CPTK_IS_TRIVIAL;
7232 kind = CPTK_IS_UNION;
7238 /* Consume the token. */
7239 cp_lexer_consume_token (parser->lexer);
7241 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7243 type1 = cp_parser_type_id (parser);
7245 if (type1 == error_mark_node)
7246 return error_mark_node;
7248 /* Build a trivial decl-specifier-seq. */
7249 clear_decl_specs (&decl_specs);
7250 decl_specs.type = type1;
7252 /* Call grokdeclarator to figure out what type this is. */
7253 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7254 /*initialized=*/0, /*attrlist=*/NULL);
7258 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7260 type2 = cp_parser_type_id (parser);
7262 if (type2 == error_mark_node)
7263 return error_mark_node;
7265 /* Build a trivial decl-specifier-seq. */
7266 clear_decl_specs (&decl_specs);
7267 decl_specs.type = type2;
7269 /* Call grokdeclarator to figure out what type this is. */
7270 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7271 /*initialized=*/0, /*attrlist=*/NULL);
7274 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7276 /* Complete the trait expression, which may mean either processing
7277 the trait expr now or saving it for template instantiation. */
7278 return finish_trait_expr (kind, type1, type2);
7281 /* Lambdas that appear in variable initializer or default argument scope
7282 get that in their mangling, so we need to record it. We might as well
7283 use the count for function and namespace scopes as well. */
7284 static GTY(()) tree lambda_scope;
7285 static GTY(()) int lambda_count;
7286 typedef struct GTY(()) tree_int
7291 DEF_VEC_O(tree_int);
7292 DEF_VEC_ALLOC_O(tree_int,gc);
7293 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7296 start_lambda_scope (tree decl)
7300 /* Once we're inside a function, we ignore other scopes and just push
7301 the function again so that popping works properly. */
7302 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7303 decl = current_function_decl;
7304 ti.t = lambda_scope;
7305 ti.i = lambda_count;
7306 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7307 if (lambda_scope != decl)
7309 /* Don't reset the count if we're still in the same function. */
7310 lambda_scope = decl;
7316 record_lambda_scope (tree lambda)
7318 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7319 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7323 finish_lambda_scope (void)
7325 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7326 if (lambda_scope != p->t)
7328 lambda_scope = p->t;
7329 lambda_count = p->i;
7331 VEC_pop (tree_int, lambda_scope_stack);
7334 /* Parse a lambda expression.
7337 lambda-introducer lambda-declarator [opt] compound-statement
7339 Returns a representation of the expression. */
7342 cp_parser_lambda_expression (cp_parser* parser)
7344 tree lambda_expr = build_lambda_expr ();
7347 LAMBDA_EXPR_LOCATION (lambda_expr)
7348 = cp_lexer_peek_token (parser->lexer)->location;
7350 if (cp_unevaluated_operand)
7351 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7352 "lambda-expression in unevaluated context");
7354 /* We may be in the middle of deferred access check. Disable
7356 push_deferring_access_checks (dk_no_deferred);
7358 cp_parser_lambda_introducer (parser, lambda_expr);
7360 type = begin_lambda_type (lambda_expr);
7362 record_lambda_scope (lambda_expr);
7364 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7365 determine_visibility (TYPE_NAME (type));
7367 /* Now that we've started the type, add the capture fields for any
7368 explicit captures. */
7369 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7372 /* Inside the class, surrounding template-parameter-lists do not apply. */
7373 unsigned int saved_num_template_parameter_lists
7374 = parser->num_template_parameter_lists;
7376 parser->num_template_parameter_lists = 0;
7378 /* By virtue of defining a local class, a lambda expression has access to
7379 the private variables of enclosing classes. */
7381 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7383 cp_parser_lambda_body (parser, lambda_expr);
7385 /* The capture list was built up in reverse order; fix that now. */
7387 tree newlist = NULL_TREE;
7390 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7393 tree field = TREE_PURPOSE (elt);
7396 next = TREE_CHAIN (elt);
7397 TREE_CHAIN (elt) = newlist;
7400 /* Also add __ to the beginning of the field name so that code
7401 outside the lambda body can't see the captured name. We could
7402 just remove the name entirely, but this is more useful for
7404 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7405 /* The 'this' capture already starts with __. */
7408 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7409 buf[1] = buf[0] = '_';
7410 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7411 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7412 DECL_NAME (field) = get_identifier (buf);
7414 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7417 maybe_add_lambda_conv_op (type);
7419 type = finish_struct (type, /*attributes=*/NULL_TREE);
7421 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7424 pop_deferring_access_checks ();
7426 return build_lambda_object (lambda_expr);
7429 /* Parse the beginning of a lambda expression.
7432 [ lambda-capture [opt] ]
7434 LAMBDA_EXPR is the current representation of the lambda expression. */
7437 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7439 /* Need commas after the first capture. */
7442 /* Eat the leading `['. */
7443 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7445 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7446 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7447 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7448 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7449 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7450 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7452 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7454 cp_lexer_consume_token (parser->lexer);
7458 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7460 cp_token* capture_token;
7462 tree capture_init_expr;
7463 cp_id_kind idk = CP_ID_KIND_NONE;
7464 bool explicit_init_p = false;
7466 enum capture_kind_type
7471 enum capture_kind_type capture_kind = BY_COPY;
7473 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7475 error ("expected end of capture-list");
7482 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7484 /* Possibly capture `this'. */
7485 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7487 cp_lexer_consume_token (parser->lexer);
7488 add_capture (lambda_expr,
7489 /*id=*/get_identifier ("__this"),
7490 /*initializer=*/finish_this_expr(),
7491 /*by_reference_p=*/false,
7496 /* Remember whether we want to capture as a reference or not. */
7497 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7499 capture_kind = BY_REFERENCE;
7500 cp_lexer_consume_token (parser->lexer);
7503 /* Get the identifier. */
7504 capture_token = cp_lexer_peek_token (parser->lexer);
7505 capture_id = cp_parser_identifier (parser);
7507 if (capture_id == error_mark_node)
7508 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7509 delimiters, but I modified this to stop on unnested ']' as well. It
7510 was already changed to stop on unnested '}', so the
7511 "closing_parenthesis" name is no more misleading with my change. */
7513 cp_parser_skip_to_closing_parenthesis (parser,
7514 /*recovering=*/true,
7516 /*consume_paren=*/true);
7520 /* Find the initializer for this capture. */
7521 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7523 /* An explicit expression exists. */
7524 cp_lexer_consume_token (parser->lexer);
7525 pedwarn (input_location, OPT_pedantic,
7526 "ISO C++ does not allow initializers "
7527 "in lambda expression capture lists");
7528 capture_init_expr = cp_parser_assignment_expression (parser,
7531 explicit_init_p = true;
7535 const char* error_msg;
7537 /* Turn the identifier into an id-expression. */
7539 = cp_parser_lookup_name
7543 /*is_template=*/false,
7544 /*is_namespace=*/false,
7545 /*check_dependency=*/true,
7546 /*ambiguous_decls=*/NULL,
7547 capture_token->location);
7550 = finish_id_expression
7555 /*integral_constant_expression_p=*/false,
7556 /*allow_non_integral_constant_expression_p=*/false,
7557 /*non_integral_constant_expression_p=*/NULL,
7558 /*template_p=*/false,
7560 /*address_p=*/false,
7561 /*template_arg_p=*/false,
7563 capture_token->location);
7566 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7568 = unqualified_name_lookup_error (capture_init_expr);
7570 add_capture (lambda_expr,
7573 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7577 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7580 /* Parse the (optional) middle of a lambda expression.
7583 ( parameter-declaration-clause [opt] )
7584 attribute-specifier [opt]
7586 exception-specification [opt]
7587 lambda-return-type-clause [opt]
7589 LAMBDA_EXPR is the current representation of the lambda expression. */
7592 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7594 /* 5.1.1.4 of the standard says:
7595 If a lambda-expression does not include a lambda-declarator, it is as if
7596 the lambda-declarator were ().
7597 This means an empty parameter list, no attributes, and no exception
7599 tree param_list = void_list_node;
7600 tree attributes = NULL_TREE;
7601 tree exception_spec = NULL_TREE;
7604 /* The lambda-declarator is optional, but must begin with an opening
7605 parenthesis if present. */
7606 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7608 cp_lexer_consume_token (parser->lexer);
7610 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7612 /* Parse parameters. */
7613 param_list = cp_parser_parameter_declaration_clause (parser);
7615 /* Default arguments shall not be specified in the
7616 parameter-declaration-clause of a lambda-declarator. */
7617 for (t = param_list; t; t = TREE_CHAIN (t))
7618 if (TREE_PURPOSE (t))
7619 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7620 "default argument specified for lambda parameter");
7622 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7624 attributes = cp_parser_attributes_opt (parser);
7626 /* Parse optional `mutable' keyword. */
7627 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7629 cp_lexer_consume_token (parser->lexer);
7630 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7633 /* Parse optional exception specification. */
7634 exception_spec = cp_parser_exception_specification_opt (parser);
7636 /* Parse optional trailing return type. */
7637 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7639 cp_lexer_consume_token (parser->lexer);
7640 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7643 /* The function parameters must be in scope all the way until after the
7644 trailing-return-type in case of decltype. */
7645 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7646 pop_binding (DECL_NAME (t), t);
7651 /* Create the function call operator.
7653 Messing with declarators like this is no uglier than building up the
7654 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7657 cp_decl_specifier_seq return_type_specs;
7658 cp_declarator* declarator;
7663 clear_decl_specs (&return_type_specs);
7664 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7665 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7667 /* Maybe we will deduce the return type later, but we can use void
7668 as a placeholder return type anyways. */
7669 return_type_specs.type = void_type_node;
7671 p = obstack_alloc (&declarator_obstack, 0);
7673 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7676 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7677 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7678 declarator = make_call_declarator (declarator, param_list, quals,
7680 /*late_return_type=*/NULL_TREE);
7681 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7683 fco = grokmethod (&return_type_specs,
7686 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7687 DECL_ARTIFICIAL (fco) = 1;
7689 finish_member_declaration (fco);
7691 obstack_free (&declarator_obstack, p);
7695 /* Parse the body of a lambda expression, which is simply
7699 but which requires special handling.
7700 LAMBDA_EXPR is the current representation of the lambda expression. */
7703 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7705 bool nested = (current_function_decl != NULL_TREE);
7707 push_function_context ();
7709 /* Finish the function call operator
7711 + late_parsing_for_member
7712 + function_definition_after_declarator
7713 + ctor_initializer_opt_and_function_body */
7715 tree fco = lambda_function (lambda_expr);
7719 /* Let the front end know that we are going to be defining this
7721 start_preparsed_function (fco,
7723 SF_PRE_PARSED | SF_INCLASS_INLINE);
7725 start_lambda_scope (fco);
7726 body = begin_function_body ();
7728 /* 5.1.1.4 of the standard says:
7729 If a lambda-expression does not include a trailing-return-type, it
7730 is as if the trailing-return-type denotes the following type:
7731 * if the compound-statement is of the form
7732 { return attribute-specifier [opt] expression ; }
7733 the type of the returned expression after lvalue-to-rvalue
7734 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7735 (_conv.array_ 4.2), and function-to-pointer conversion
7737 * otherwise, void. */
7739 /* In a lambda that has neither a lambda-return-type-clause
7740 nor a deducible form, errors should be reported for return statements
7741 in the body. Since we used void as the placeholder return type, parsing
7742 the body as usual will give such desired behavior. */
7743 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7744 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7745 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7746 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7749 tree expr = NULL_TREE;
7750 cp_id_kind idk = CP_ID_KIND_NONE;
7752 /* Parse tentatively in case there's more after the initial return
7754 cp_parser_parse_tentatively (parser);
7756 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7757 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7759 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7761 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7762 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7764 if (cp_parser_parse_definitely (parser))
7766 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7768 compound_stmt = begin_compound_stmt (0);
7769 /* Will get error here if type not deduced yet. */
7770 finish_return_stmt (expr);
7771 finish_compound_stmt (compound_stmt);
7779 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7780 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7781 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7782 cp_parser_compound_stmt does not pass it. */
7783 cp_parser_function_body (parser);
7784 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7787 finish_function_body (body);
7788 finish_lambda_scope ();
7790 /* Finish the function and generate code for it if necessary. */
7791 expand_or_defer_fn (finish_function (/*inline*/2));
7795 pop_function_context();
7798 /* Statements [gram.stmt.stmt] */
7800 /* Parse a statement.
7804 expression-statement
7809 declaration-statement
7812 IN_COMPOUND is true when the statement is nested inside a
7813 cp_parser_compound_statement; this matters for certain pragmas.
7815 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7816 is a (possibly labeled) if statement which is not enclosed in braces
7817 and has an else clause. This is used to implement -Wparentheses. */
7820 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7821 bool in_compound, bool *if_p)
7825 location_t statement_location;
7830 /* There is no statement yet. */
7831 statement = NULL_TREE;
7832 /* Peek at the next token. */
7833 token = cp_lexer_peek_token (parser->lexer);
7834 /* Remember the location of the first token in the statement. */
7835 statement_location = token->location;
7836 /* If this is a keyword, then that will often determine what kind of
7837 statement we have. */
7838 if (token->type == CPP_KEYWORD)
7840 enum rid keyword = token->keyword;
7846 /* Looks like a labeled-statement with a case label.
7847 Parse the label, and then use tail recursion to parse
7849 cp_parser_label_for_labeled_statement (parser);
7854 statement = cp_parser_selection_statement (parser, if_p);
7860 statement = cp_parser_iteration_statement (parser);
7867 statement = cp_parser_jump_statement (parser);
7870 /* Objective-C++ exception-handling constructs. */
7873 case RID_AT_FINALLY:
7874 case RID_AT_SYNCHRONIZED:
7876 statement = cp_parser_objc_statement (parser);
7880 statement = cp_parser_try_block (parser);
7884 /* This must be a namespace alias definition. */
7885 cp_parser_declaration_statement (parser);
7889 /* It might be a keyword like `int' that can start a
7890 declaration-statement. */
7894 else if (token->type == CPP_NAME)
7896 /* If the next token is a `:', then we are looking at a
7897 labeled-statement. */
7898 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7899 if (token->type == CPP_COLON)
7901 /* Looks like a labeled-statement with an ordinary label.
7902 Parse the label, and then use tail recursion to parse
7904 cp_parser_label_for_labeled_statement (parser);
7908 /* Anything that starts with a `{' must be a compound-statement. */
7909 else if (token->type == CPP_OPEN_BRACE)
7910 statement = cp_parser_compound_statement (parser, NULL, false);
7911 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7912 a statement all its own. */
7913 else if (token->type == CPP_PRAGMA)
7915 /* Only certain OpenMP pragmas are attached to statements, and thus
7916 are considered statements themselves. All others are not. In
7917 the context of a compound, accept the pragma as a "statement" and
7918 return so that we can check for a close brace. Otherwise we
7919 require a real statement and must go back and read one. */
7921 cp_parser_pragma (parser, pragma_compound);
7922 else if (!cp_parser_pragma (parser, pragma_stmt))
7926 else if (token->type == CPP_EOF)
7928 cp_parser_error (parser, "expected statement");
7932 /* Everything else must be a declaration-statement or an
7933 expression-statement. Try for the declaration-statement
7934 first, unless we are looking at a `;', in which case we know that
7935 we have an expression-statement. */
7938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7940 cp_parser_parse_tentatively (parser);
7941 /* Try to parse the declaration-statement. */
7942 cp_parser_declaration_statement (parser);
7943 /* If that worked, we're done. */
7944 if (cp_parser_parse_definitely (parser))
7947 /* Look for an expression-statement instead. */
7948 statement = cp_parser_expression_statement (parser, in_statement_expr);
7951 /* Set the line number for the statement. */
7952 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7953 SET_EXPR_LOCATION (statement, statement_location);
7956 /* Parse the label for a labeled-statement, i.e.
7959 case constant-expression :
7963 case constant-expression ... constant-expression : statement
7965 When a label is parsed without errors, the label is added to the
7966 parse tree by the finish_* functions, so this function doesn't
7967 have to return the label. */
7970 cp_parser_label_for_labeled_statement (cp_parser* parser)
7973 tree label = NULL_TREE;
7975 /* The next token should be an identifier. */
7976 token = cp_lexer_peek_token (parser->lexer);
7977 if (token->type != CPP_NAME
7978 && token->type != CPP_KEYWORD)
7980 cp_parser_error (parser, "expected labeled-statement");
7984 switch (token->keyword)
7991 /* Consume the `case' token. */
7992 cp_lexer_consume_token (parser->lexer);
7993 /* Parse the constant-expression. */
7994 expr = cp_parser_constant_expression (parser,
7995 /*allow_non_constant_p=*/false,
7998 ellipsis = cp_lexer_peek_token (parser->lexer);
7999 if (ellipsis->type == CPP_ELLIPSIS)
8001 /* Consume the `...' token. */
8002 cp_lexer_consume_token (parser->lexer);
8004 cp_parser_constant_expression (parser,
8005 /*allow_non_constant_p=*/false,
8007 /* We don't need to emit warnings here, as the common code
8008 will do this for us. */
8011 expr_hi = NULL_TREE;
8013 if (parser->in_switch_statement_p)
8014 finish_case_label (token->location, expr, expr_hi);
8016 error_at (token->location,
8017 "case label %qE not within a switch statement",
8023 /* Consume the `default' token. */
8024 cp_lexer_consume_token (parser->lexer);
8026 if (parser->in_switch_statement_p)
8027 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8029 error_at (token->location, "case label not within a switch statement");
8033 /* Anything else must be an ordinary label. */
8034 label = finish_label_stmt (cp_parser_identifier (parser));
8038 /* Require the `:' token. */
8039 cp_parser_require (parser, CPP_COLON, RT_COLON);
8041 /* An ordinary label may optionally be followed by attributes.
8042 However, this is only permitted if the attributes are then
8043 followed by a semicolon. This is because, for backward
8044 compatibility, when parsing
8045 lab: __attribute__ ((unused)) int i;
8046 we want the attribute to attach to "i", not "lab". */
8047 if (label != NULL_TREE
8048 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8052 cp_parser_parse_tentatively (parser);
8053 attrs = cp_parser_attributes_opt (parser);
8054 if (attrs == NULL_TREE
8055 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8056 cp_parser_abort_tentative_parse (parser);
8057 else if (!cp_parser_parse_definitely (parser))
8060 cplus_decl_attributes (&label, attrs, 0);
8064 /* Parse an expression-statement.
8066 expression-statement:
8069 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8070 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8071 indicates whether this expression-statement is part of an
8072 expression statement. */
8075 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8077 tree statement = NULL_TREE;
8078 cp_token *token = cp_lexer_peek_token (parser->lexer);
8080 /* If the next token is a ';', then there is no expression
8082 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8083 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8085 /* Give a helpful message for "A<T>::type t;" and the like. */
8086 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8087 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8089 if (TREE_CODE (statement) == SCOPE_REF)
8090 error_at (token->location, "need %<typename%> before %qE because "
8091 "%qT is a dependent scope",
8092 statement, TREE_OPERAND (statement, 0));
8093 else if (is_overloaded_fn (statement)
8094 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8097 tree fn = get_first_fn (statement);
8098 error_at (token->location,
8099 "%<%T::%D%> names the constructor, not the type",
8100 DECL_CONTEXT (fn), DECL_NAME (fn));
8104 /* Consume the final `;'. */
8105 cp_parser_consume_semicolon_at_end_of_statement (parser);
8107 if (in_statement_expr
8108 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8109 /* This is the final expression statement of a statement
8111 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8113 statement = finish_expr_stmt (statement);
8120 /* Parse a compound-statement.
8123 { statement-seq [opt] }
8128 { label-declaration-seq [opt] statement-seq [opt] }
8130 label-declaration-seq:
8132 label-declaration-seq label-declaration
8134 Returns a tree representing the statement. */
8137 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8142 /* Consume the `{'. */
8143 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8144 return error_mark_node;
8145 /* Begin the compound-statement. */
8146 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8147 /* If the next keyword is `__label__' we have a label declaration. */
8148 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8149 cp_parser_label_declaration (parser);
8150 /* Parse an (optional) statement-seq. */
8151 cp_parser_statement_seq_opt (parser, in_statement_expr);
8152 /* Finish the compound-statement. */
8153 finish_compound_stmt (compound_stmt);
8154 /* Consume the `}'. */
8155 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8157 return compound_stmt;
8160 /* Parse an (optional) statement-seq.
8164 statement-seq [opt] statement */
8167 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8169 /* Scan statements until there aren't any more. */
8172 cp_token *token = cp_lexer_peek_token (parser->lexer);
8174 /* If we're looking at a `}', then we've run out of statements. */
8175 if (token->type == CPP_CLOSE_BRACE
8176 || token->type == CPP_EOF
8177 || token->type == CPP_PRAGMA_EOL)
8180 /* If we are in a compound statement and find 'else' then
8181 something went wrong. */
8182 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8184 if (parser->in_statement & IN_IF_STMT)
8188 token = cp_lexer_consume_token (parser->lexer);
8189 error_at (token->location, "%<else%> without a previous %<if%>");
8193 /* Parse the statement. */
8194 cp_parser_statement (parser, in_statement_expr, true, NULL);
8198 /* Parse a selection-statement.
8200 selection-statement:
8201 if ( condition ) statement
8202 if ( condition ) statement else statement
8203 switch ( condition ) statement
8205 Returns the new IF_STMT or SWITCH_STMT.
8207 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8208 is a (possibly labeled) if statement which is not enclosed in
8209 braces and has an else clause. This is used to implement
8213 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8221 /* Peek at the next token. */
8222 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8224 /* See what kind of keyword it is. */
8225 keyword = token->keyword;
8234 /* Look for the `('. */
8235 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8237 cp_parser_skip_to_end_of_statement (parser);
8238 return error_mark_node;
8241 /* Begin the selection-statement. */
8242 if (keyword == RID_IF)
8243 statement = begin_if_stmt ();
8245 statement = begin_switch_stmt ();
8247 /* Parse the condition. */
8248 condition = cp_parser_condition (parser);
8249 /* Look for the `)'. */
8250 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8251 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8252 /*consume_paren=*/true);
8254 if (keyword == RID_IF)
8257 unsigned char in_statement;
8259 /* Add the condition. */
8260 finish_if_stmt_cond (condition, statement);
8262 /* Parse the then-clause. */
8263 in_statement = parser->in_statement;
8264 parser->in_statement |= IN_IF_STMT;
8265 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8267 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8268 add_stmt (build_empty_stmt (loc));
8269 cp_lexer_consume_token (parser->lexer);
8270 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8271 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8272 "empty body in an %<if%> statement");
8276 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8277 parser->in_statement = in_statement;
8279 finish_then_clause (statement);
8281 /* If the next token is `else', parse the else-clause. */
8282 if (cp_lexer_next_token_is_keyword (parser->lexer,
8285 /* Consume the `else' keyword. */
8286 cp_lexer_consume_token (parser->lexer);
8287 begin_else_clause (statement);
8288 /* Parse the else-clause. */
8289 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8292 loc = cp_lexer_peek_token (parser->lexer)->location;
8294 OPT_Wempty_body, "suggest braces around "
8295 "empty body in an %<else%> statement");
8296 add_stmt (build_empty_stmt (loc));
8297 cp_lexer_consume_token (parser->lexer);
8300 cp_parser_implicitly_scoped_statement (parser, NULL);
8302 finish_else_clause (statement);
8304 /* If we are currently parsing a then-clause, then
8305 IF_P will not be NULL. We set it to true to
8306 indicate that this if statement has an else clause.
8307 This may trigger the Wparentheses warning below
8308 when we get back up to the parent if statement. */
8314 /* This if statement does not have an else clause. If
8315 NESTED_IF is true, then the then-clause is an if
8316 statement which does have an else clause. We warn
8317 about the potential ambiguity. */
8319 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8320 "suggest explicit braces to avoid ambiguous"
8324 /* Now we're all done with the if-statement. */
8325 finish_if_stmt (statement);
8329 bool in_switch_statement_p;
8330 unsigned char in_statement;
8332 /* Add the condition. */
8333 finish_switch_cond (condition, statement);
8335 /* Parse the body of the switch-statement. */
8336 in_switch_statement_p = parser->in_switch_statement_p;
8337 in_statement = parser->in_statement;
8338 parser->in_switch_statement_p = true;
8339 parser->in_statement |= IN_SWITCH_STMT;
8340 cp_parser_implicitly_scoped_statement (parser, NULL);
8341 parser->in_switch_statement_p = in_switch_statement_p;
8342 parser->in_statement = in_statement;
8344 /* Now we're all done with the switch-statement. */
8345 finish_switch_stmt (statement);
8353 cp_parser_error (parser, "expected selection-statement");
8354 return error_mark_node;
8358 /* Parse a condition.
8362 type-specifier-seq declarator = initializer-clause
8363 type-specifier-seq declarator braced-init-list
8368 type-specifier-seq declarator asm-specification [opt]
8369 attributes [opt] = assignment-expression
8371 Returns the expression that should be tested. */
8374 cp_parser_condition (cp_parser* parser)
8376 cp_decl_specifier_seq type_specifiers;
8377 const char *saved_message;
8379 /* Try the declaration first. */
8380 cp_parser_parse_tentatively (parser);
8381 /* New types are not allowed in the type-specifier-seq for a
8383 saved_message = parser->type_definition_forbidden_message;
8384 parser->type_definition_forbidden_message
8385 = G_("types may not be defined in conditions");
8386 /* Parse the type-specifier-seq. */
8387 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8388 /*is_trailing_return=*/false,
8390 /* Restore the saved message. */
8391 parser->type_definition_forbidden_message = saved_message;
8392 /* If all is well, we might be looking at a declaration. */
8393 if (!cp_parser_error_occurred (parser))
8396 tree asm_specification;
8398 cp_declarator *declarator;
8399 tree initializer = NULL_TREE;
8401 /* Parse the declarator. */
8402 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8403 /*ctor_dtor_or_conv_p=*/NULL,
8404 /*parenthesized_p=*/NULL,
8405 /*member_p=*/false);
8406 /* Parse the attributes. */
8407 attributes = cp_parser_attributes_opt (parser);
8408 /* Parse the asm-specification. */
8409 asm_specification = cp_parser_asm_specification_opt (parser);
8410 /* If the next token is not an `=' or '{', then we might still be
8411 looking at an expression. For example:
8415 looks like a decl-specifier-seq and a declarator -- but then
8416 there is no `=', so this is an expression. */
8417 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8418 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8419 cp_parser_simulate_error (parser);
8421 /* If we did see an `=' or '{', then we are looking at a declaration
8423 if (cp_parser_parse_definitely (parser))
8426 bool non_constant_p;
8427 bool flags = LOOKUP_ONLYCONVERTING;
8429 /* Create the declaration. */
8430 decl = start_decl (declarator, &type_specifiers,
8431 /*initialized_p=*/true,
8432 attributes, /*prefix_attributes=*/NULL_TREE,
8435 /* Parse the initializer. */
8436 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8438 initializer = cp_parser_braced_list (parser, &non_constant_p);
8439 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8444 /* Consume the `='. */
8445 cp_parser_require (parser, CPP_EQ, RT_EQ);
8446 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8448 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8449 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8451 if (!non_constant_p)
8452 initializer = fold_non_dependent_expr (initializer);
8454 /* Process the initializer. */
8455 cp_finish_decl (decl,
8456 initializer, !non_constant_p,
8461 pop_scope (pushed_scope);
8463 return convert_from_reference (decl);
8466 /* If we didn't even get past the declarator successfully, we are
8467 definitely not looking at a declaration. */
8469 cp_parser_abort_tentative_parse (parser);
8471 /* Otherwise, we are looking at an expression. */
8472 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8475 /* Parse an iteration-statement.
8477 iteration-statement:
8478 while ( condition ) statement
8479 do statement while ( expression ) ;
8480 for ( for-init-statement condition [opt] ; expression [opt] )
8483 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8486 cp_parser_iteration_statement (cp_parser* parser)
8491 unsigned char in_statement;
8493 /* Peek at the next token. */
8494 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8496 return error_mark_node;
8498 /* Remember whether or not we are already within an iteration
8500 in_statement = parser->in_statement;
8502 /* See what kind of keyword it is. */
8503 keyword = token->keyword;
8510 /* Begin the while-statement. */
8511 statement = begin_while_stmt ();
8512 /* Look for the `('. */
8513 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8514 /* Parse the condition. */
8515 condition = cp_parser_condition (parser);
8516 finish_while_stmt_cond (condition, statement);
8517 /* Look for the `)'. */
8518 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8519 /* Parse the dependent statement. */
8520 parser->in_statement = IN_ITERATION_STMT;
8521 cp_parser_already_scoped_statement (parser);
8522 parser->in_statement = in_statement;
8523 /* We're done with the while-statement. */
8524 finish_while_stmt (statement);
8532 /* Begin the do-statement. */
8533 statement = begin_do_stmt ();
8534 /* Parse the body of the do-statement. */
8535 parser->in_statement = IN_ITERATION_STMT;
8536 cp_parser_implicitly_scoped_statement (parser, NULL);
8537 parser->in_statement = in_statement;
8538 finish_do_body (statement);
8539 /* Look for the `while' keyword. */
8540 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8541 /* Look for the `('. */
8542 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8543 /* Parse the expression. */
8544 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8545 /* We're done with the do-statement. */
8546 finish_do_stmt (expression, statement);
8547 /* Look for the `)'. */
8548 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8549 /* Look for the `;'. */
8550 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8556 tree condition = NULL_TREE;
8557 tree expression = NULL_TREE;
8559 /* Begin the for-statement. */
8560 statement = begin_for_stmt ();
8561 /* Look for the `('. */
8562 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8563 /* Parse the initialization. */
8564 cp_parser_for_init_statement (parser);
8565 finish_for_init_stmt (statement);
8567 /* If there's a condition, process it. */
8568 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8569 condition = cp_parser_condition (parser);
8570 finish_for_cond (condition, statement);
8571 /* Look for the `;'. */
8572 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8574 /* If there's an expression, process it. */
8575 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8576 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8577 finish_for_expr (expression, statement);
8578 /* Look for the `)'. */
8579 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8581 /* Parse the body of the for-statement. */
8582 parser->in_statement = IN_ITERATION_STMT;
8583 cp_parser_already_scoped_statement (parser);
8584 parser->in_statement = in_statement;
8586 /* We're done with the for-statement. */
8587 finish_for_stmt (statement);
8592 cp_parser_error (parser, "expected iteration-statement");
8593 statement = error_mark_node;
8600 /* Parse a for-init-statement.
8603 expression-statement
8604 simple-declaration */
8607 cp_parser_for_init_statement (cp_parser* parser)
8609 /* If the next token is a `;', then we have an empty
8610 expression-statement. Grammatically, this is also a
8611 simple-declaration, but an invalid one, because it does not
8612 declare anything. Therefore, if we did not handle this case
8613 specially, we would issue an error message about an invalid
8615 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8617 /* We're going to speculatively look for a declaration, falling back
8618 to an expression, if necessary. */
8619 cp_parser_parse_tentatively (parser);
8620 /* Parse the declaration. */
8621 cp_parser_simple_declaration (parser,
8622 /*function_definition_allowed_p=*/false);
8623 /* If the tentative parse failed, then we shall need to look for an
8624 expression-statement. */
8625 if (cp_parser_parse_definitely (parser))
8629 cp_parser_expression_statement (parser, NULL_TREE);
8632 /* Parse a jump-statement.
8637 return expression [opt] ;
8638 return braced-init-list ;
8646 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8649 cp_parser_jump_statement (cp_parser* parser)
8651 tree statement = error_mark_node;
8654 unsigned char in_statement;
8656 /* Peek at the next token. */
8657 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
8659 return error_mark_node;
8661 /* See what kind of keyword it is. */
8662 keyword = token->keyword;
8666 in_statement = parser->in_statement & ~IN_IF_STMT;
8667 switch (in_statement)
8670 error_at (token->location, "break statement not within loop or switch");
8673 gcc_assert ((in_statement & IN_SWITCH_STMT)
8674 || in_statement == IN_ITERATION_STMT);
8675 statement = finish_break_stmt ();
8678 error_at (token->location, "invalid exit from OpenMP structured block");
8681 error_at (token->location, "break statement used with OpenMP for loop");
8684 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8688 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8691 error_at (token->location, "continue statement not within a loop");
8693 case IN_ITERATION_STMT:
8695 statement = finish_continue_stmt ();
8698 error_at (token->location, "invalid exit from OpenMP structured block");
8703 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8709 bool expr_non_constant_p;
8711 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8713 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8714 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8716 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8717 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8719 /* If the next token is a `;', then there is no
8722 /* Build the return-statement. */
8723 statement = finish_return_stmt (expr);
8724 /* Look for the final `;'. */
8725 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8730 /* Create the goto-statement. */
8731 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8733 /* Issue a warning about this use of a GNU extension. */
8734 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8735 /* Consume the '*' token. */
8736 cp_lexer_consume_token (parser->lexer);
8737 /* Parse the dependent expression. */
8738 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8741 finish_goto_stmt (cp_parser_identifier (parser));
8742 /* Look for the final `;'. */
8743 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8747 cp_parser_error (parser, "expected jump-statement");
8754 /* Parse a declaration-statement.
8756 declaration-statement:
8757 block-declaration */
8760 cp_parser_declaration_statement (cp_parser* parser)
8764 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8765 p = obstack_alloc (&declarator_obstack, 0);
8767 /* Parse the block-declaration. */
8768 cp_parser_block_declaration (parser, /*statement_p=*/true);
8770 /* Free any declarators allocated. */
8771 obstack_free (&declarator_obstack, p);
8773 /* Finish off the statement. */
8777 /* Some dependent statements (like `if (cond) statement'), are
8778 implicitly in their own scope. In other words, if the statement is
8779 a single statement (as opposed to a compound-statement), it is
8780 none-the-less treated as if it were enclosed in braces. Any
8781 declarations appearing in the dependent statement are out of scope
8782 after control passes that point. This function parses a statement,
8783 but ensures that is in its own scope, even if it is not a
8786 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8787 is a (possibly labeled) if statement which is not enclosed in
8788 braces and has an else clause. This is used to implement
8791 Returns the new statement. */
8794 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8801 /* Mark if () ; with a special NOP_EXPR. */
8802 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8804 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8805 cp_lexer_consume_token (parser->lexer);
8806 statement = add_stmt (build_empty_stmt (loc));
8808 /* if a compound is opened, we simply parse the statement directly. */
8809 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8810 statement = cp_parser_compound_statement (parser, NULL, false);
8811 /* If the token is not a `{', then we must take special action. */
8814 /* Create a compound-statement. */
8815 statement = begin_compound_stmt (0);
8816 /* Parse the dependent-statement. */
8817 cp_parser_statement (parser, NULL_TREE, false, if_p);
8818 /* Finish the dummy compound-statement. */
8819 finish_compound_stmt (statement);
8822 /* Return the statement. */
8826 /* For some dependent statements (like `while (cond) statement'), we
8827 have already created a scope. Therefore, even if the dependent
8828 statement is a compound-statement, we do not want to create another
8832 cp_parser_already_scoped_statement (cp_parser* parser)
8834 /* If the token is a `{', then we must take special action. */
8835 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8836 cp_parser_statement (parser, NULL_TREE, false, NULL);
8839 /* Avoid calling cp_parser_compound_statement, so that we
8840 don't create a new scope. Do everything else by hand. */
8841 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
8842 /* If the next keyword is `__label__' we have a label declaration. */
8843 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8844 cp_parser_label_declaration (parser);
8845 /* Parse an (optional) statement-seq. */
8846 cp_parser_statement_seq_opt (parser, NULL_TREE);
8847 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8851 /* Declarations [gram.dcl.dcl] */
8853 /* Parse an optional declaration-sequence.
8857 declaration-seq declaration */
8860 cp_parser_declaration_seq_opt (cp_parser* parser)
8866 token = cp_lexer_peek_token (parser->lexer);
8868 if (token->type == CPP_CLOSE_BRACE
8869 || token->type == CPP_EOF
8870 || token->type == CPP_PRAGMA_EOL)
8873 if (token->type == CPP_SEMICOLON)
8875 /* A declaration consisting of a single semicolon is
8876 invalid. Allow it unless we're being pedantic. */
8877 cp_lexer_consume_token (parser->lexer);
8878 if (!in_system_header)
8879 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8883 /* If we're entering or exiting a region that's implicitly
8884 extern "C", modify the lang context appropriately. */
8885 if (!parser->implicit_extern_c && token->implicit_extern_c)
8887 push_lang_context (lang_name_c);
8888 parser->implicit_extern_c = true;
8890 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8892 pop_lang_context ();
8893 parser->implicit_extern_c = false;
8896 if (token->type == CPP_PRAGMA)
8898 /* A top-level declaration can consist solely of a #pragma.
8899 A nested declaration cannot, so this is done here and not
8900 in cp_parser_declaration. (A #pragma at block scope is
8901 handled in cp_parser_statement.) */
8902 cp_parser_pragma (parser, pragma_external);
8906 /* Parse the declaration itself. */
8907 cp_parser_declaration (parser);
8911 /* Parse a declaration.
8916 template-declaration
8917 explicit-instantiation
8918 explicit-specialization
8919 linkage-specification
8920 namespace-definition
8925 __extension__ declaration */
8928 cp_parser_declaration (cp_parser* parser)
8935 /* Check for the `__extension__' keyword. */
8936 if (cp_parser_extension_opt (parser, &saved_pedantic))
8938 /* Parse the qualified declaration. */
8939 cp_parser_declaration (parser);
8940 /* Restore the PEDANTIC flag. */
8941 pedantic = saved_pedantic;
8946 /* Try to figure out what kind of declaration is present. */
8947 token1 = *cp_lexer_peek_token (parser->lexer);
8949 if (token1.type != CPP_EOF)
8950 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8953 token2.type = CPP_EOF;
8954 token2.keyword = RID_MAX;
8957 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8958 p = obstack_alloc (&declarator_obstack, 0);
8960 /* If the next token is `extern' and the following token is a string
8961 literal, then we have a linkage specification. */
8962 if (token1.keyword == RID_EXTERN
8963 && cp_parser_is_string_literal (&token2))
8964 cp_parser_linkage_specification (parser);
8965 /* If the next token is `template', then we have either a template
8966 declaration, an explicit instantiation, or an explicit
8968 else if (token1.keyword == RID_TEMPLATE)
8970 /* `template <>' indicates a template specialization. */
8971 if (token2.type == CPP_LESS
8972 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8973 cp_parser_explicit_specialization (parser);
8974 /* `template <' indicates a template declaration. */
8975 else if (token2.type == CPP_LESS)
8976 cp_parser_template_declaration (parser, /*member_p=*/false);
8977 /* Anything else must be an explicit instantiation. */
8979 cp_parser_explicit_instantiation (parser);
8981 /* If the next token is `export', then we have a template
8983 else if (token1.keyword == RID_EXPORT)
8984 cp_parser_template_declaration (parser, /*member_p=*/false);
8985 /* If the next token is `extern', 'static' or 'inline' and the one
8986 after that is `template', we have a GNU extended explicit
8987 instantiation directive. */
8988 else if (cp_parser_allow_gnu_extensions_p (parser)
8989 && (token1.keyword == RID_EXTERN
8990 || token1.keyword == RID_STATIC
8991 || token1.keyword == RID_INLINE)
8992 && token2.keyword == RID_TEMPLATE)
8993 cp_parser_explicit_instantiation (parser);
8994 /* If the next token is `namespace', check for a named or unnamed
8995 namespace definition. */
8996 else if (token1.keyword == RID_NAMESPACE
8997 && (/* A named namespace definition. */
8998 (token2.type == CPP_NAME
8999 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9001 /* An unnamed namespace definition. */
9002 || token2.type == CPP_OPEN_BRACE
9003 || token2.keyword == RID_ATTRIBUTE))
9004 cp_parser_namespace_definition (parser);
9005 /* An inline (associated) namespace definition. */
9006 else if (token1.keyword == RID_INLINE
9007 && token2.keyword == RID_NAMESPACE)
9008 cp_parser_namespace_definition (parser);
9009 /* Objective-C++ declaration/definition. */
9010 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9011 cp_parser_objc_declaration (parser);
9012 /* We must have either a block declaration or a function
9015 /* Try to parse a block-declaration, or a function-definition. */
9016 cp_parser_block_declaration (parser, /*statement_p=*/false);
9018 /* Free any declarators allocated. */
9019 obstack_free (&declarator_obstack, p);
9022 /* Parse a block-declaration.
9027 namespace-alias-definition
9034 __extension__ block-declaration
9039 static_assert-declaration
9041 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9042 part of a declaration-statement. */
9045 cp_parser_block_declaration (cp_parser *parser,
9051 /* Check for the `__extension__' keyword. */
9052 if (cp_parser_extension_opt (parser, &saved_pedantic))
9054 /* Parse the qualified declaration. */
9055 cp_parser_block_declaration (parser, statement_p);
9056 /* Restore the PEDANTIC flag. */
9057 pedantic = saved_pedantic;
9062 /* Peek at the next token to figure out which kind of declaration is
9064 token1 = cp_lexer_peek_token (parser->lexer);
9066 /* If the next keyword is `asm', we have an asm-definition. */
9067 if (token1->keyword == RID_ASM)
9070 cp_parser_commit_to_tentative_parse (parser);
9071 cp_parser_asm_definition (parser);
9073 /* If the next keyword is `namespace', we have a
9074 namespace-alias-definition. */
9075 else if (token1->keyword == RID_NAMESPACE)
9076 cp_parser_namespace_alias_definition (parser);
9077 /* If the next keyword is `using', we have either a
9078 using-declaration or a using-directive. */
9079 else if (token1->keyword == RID_USING)
9084 cp_parser_commit_to_tentative_parse (parser);
9085 /* If the token after `using' is `namespace', then we have a
9087 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9088 if (token2->keyword == RID_NAMESPACE)
9089 cp_parser_using_directive (parser);
9090 /* Otherwise, it's a using-declaration. */
9092 cp_parser_using_declaration (parser,
9093 /*access_declaration_p=*/false);
9095 /* If the next keyword is `__label__' we have a misplaced label
9097 else if (token1->keyword == RID_LABEL)
9099 cp_lexer_consume_token (parser->lexer);
9100 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9101 cp_parser_skip_to_end_of_statement (parser);
9102 /* If the next token is now a `;', consume it. */
9103 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9104 cp_lexer_consume_token (parser->lexer);
9106 /* If the next token is `static_assert' we have a static assertion. */
9107 else if (token1->keyword == RID_STATIC_ASSERT)
9108 cp_parser_static_assert (parser, /*member_p=*/false);
9109 /* Anything else must be a simple-declaration. */
9111 cp_parser_simple_declaration (parser, !statement_p);
9114 /* Parse a simple-declaration.
9117 decl-specifier-seq [opt] init-declarator-list [opt] ;
9119 init-declarator-list:
9121 init-declarator-list , init-declarator
9123 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9124 function-definition as a simple-declaration. */
9127 cp_parser_simple_declaration (cp_parser* parser,
9128 bool function_definition_allowed_p)
9130 cp_decl_specifier_seq decl_specifiers;
9131 int declares_class_or_enum;
9132 bool saw_declarator;
9134 /* Defer access checks until we know what is being declared; the
9135 checks for names appearing in the decl-specifier-seq should be
9136 done as if we were in the scope of the thing being declared. */
9137 push_deferring_access_checks (dk_deferred);
9139 /* Parse the decl-specifier-seq. We have to keep track of whether
9140 or not the decl-specifier-seq declares a named class or
9141 enumeration type, since that is the only case in which the
9142 init-declarator-list is allowed to be empty.
9146 In a simple-declaration, the optional init-declarator-list can be
9147 omitted only when declaring a class or enumeration, that is when
9148 the decl-specifier-seq contains either a class-specifier, an
9149 elaborated-type-specifier, or an enum-specifier. */
9150 cp_parser_decl_specifier_seq (parser,
9151 CP_PARSER_FLAGS_OPTIONAL,
9153 &declares_class_or_enum);
9154 /* We no longer need to defer access checks. */
9155 stop_deferring_access_checks ();
9157 /* In a block scope, a valid declaration must always have a
9158 decl-specifier-seq. By not trying to parse declarators, we can
9159 resolve the declaration/expression ambiguity more quickly. */
9160 if (!function_definition_allowed_p
9161 && !decl_specifiers.any_specifiers_p)
9163 cp_parser_error (parser, "expected declaration");
9167 /* If the next two tokens are both identifiers, the code is
9168 erroneous. The usual cause of this situation is code like:
9172 where "T" should name a type -- but does not. */
9173 if (!decl_specifiers.any_type_specifiers_p
9174 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9176 /* If parsing tentatively, we should commit; we really are
9177 looking at a declaration. */
9178 cp_parser_commit_to_tentative_parse (parser);
9183 /* If we have seen at least one decl-specifier, and the next token
9184 is not a parenthesis, then we must be looking at a declaration.
9185 (After "int (" we might be looking at a functional cast.) */
9186 if (decl_specifiers.any_specifiers_p
9187 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9188 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9189 && !cp_parser_error_occurred (parser))
9190 cp_parser_commit_to_tentative_parse (parser);
9192 /* Keep going until we hit the `;' at the end of the simple
9194 saw_declarator = false;
9195 while (cp_lexer_next_token_is_not (parser->lexer,
9199 bool function_definition_p;
9204 /* If we are processing next declarator, coma is expected */
9205 token = cp_lexer_peek_token (parser->lexer);
9206 gcc_assert (token->type == CPP_COMMA);
9207 cp_lexer_consume_token (parser->lexer);
9210 saw_declarator = true;
9212 /* Parse the init-declarator. */
9213 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9215 function_definition_allowed_p,
9217 declares_class_or_enum,
9218 &function_definition_p);
9219 /* If an error occurred while parsing tentatively, exit quickly.
9220 (That usually happens when in the body of a function; each
9221 statement is treated as a declaration-statement until proven
9223 if (cp_parser_error_occurred (parser))
9225 /* Handle function definitions specially. */
9226 if (function_definition_p)
9228 /* If the next token is a `,', then we are probably
9229 processing something like:
9233 which is erroneous. */
9234 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9236 cp_token *token = cp_lexer_peek_token (parser->lexer);
9237 error_at (token->location,
9239 " declarations and function-definitions is forbidden");
9241 /* Otherwise, we're done with the list of declarators. */
9244 pop_deferring_access_checks ();
9248 /* The next token should be either a `,' or a `;'. */
9249 token = cp_lexer_peek_token (parser->lexer);
9250 /* If it's a `,', there are more declarators to come. */
9251 if (token->type == CPP_COMMA)
9252 /* will be consumed next time around */;
9253 /* If it's a `;', we are done. */
9254 else if (token->type == CPP_SEMICOLON)
9256 /* Anything else is an error. */
9259 /* If we have already issued an error message we don't need
9260 to issue another one. */
9261 if (decl != error_mark_node
9262 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9263 cp_parser_error (parser, "expected %<,%> or %<;%>");
9264 /* Skip tokens until we reach the end of the statement. */
9265 cp_parser_skip_to_end_of_statement (parser);
9266 /* If the next token is now a `;', consume it. */
9267 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9268 cp_lexer_consume_token (parser->lexer);
9271 /* After the first time around, a function-definition is not
9272 allowed -- even if it was OK at first. For example:
9277 function_definition_allowed_p = false;
9280 /* Issue an error message if no declarators are present, and the
9281 decl-specifier-seq does not itself declare a class or
9283 if (!saw_declarator)
9285 if (cp_parser_declares_only_class_p (parser))
9286 shadow_tag (&decl_specifiers);
9287 /* Perform any deferred access checks. */
9288 perform_deferred_access_checks ();
9291 /* Consume the `;'. */
9292 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9295 pop_deferring_access_checks ();
9298 /* Parse a decl-specifier-seq.
9301 decl-specifier-seq [opt] decl-specifier
9304 storage-class-specifier
9315 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9317 The parser flags FLAGS is used to control type-specifier parsing.
9319 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9322 1: one of the decl-specifiers is an elaborated-type-specifier
9323 (i.e., a type declaration)
9324 2: one of the decl-specifiers is an enum-specifier or a
9325 class-specifier (i.e., a type definition)
9330 cp_parser_decl_specifier_seq (cp_parser* parser,
9331 cp_parser_flags flags,
9332 cp_decl_specifier_seq *decl_specs,
9333 int* declares_class_or_enum)
9335 bool constructor_possible_p = !parser->in_declarator_p;
9336 cp_token *start_token = NULL;
9338 /* Clear DECL_SPECS. */
9339 clear_decl_specs (decl_specs);
9341 /* Assume no class or enumeration type is declared. */
9342 *declares_class_or_enum = 0;
9344 /* Keep reading specifiers until there are no more to read. */
9348 bool found_decl_spec;
9351 /* Peek at the next token. */
9352 token = cp_lexer_peek_token (parser->lexer);
9354 /* Save the first token of the decl spec list for error
9357 start_token = token;
9358 /* Handle attributes. */
9359 if (token->keyword == RID_ATTRIBUTE)
9361 /* Parse the attributes. */
9362 decl_specs->attributes
9363 = chainon (decl_specs->attributes,
9364 cp_parser_attributes_opt (parser));
9367 /* Assume we will find a decl-specifier keyword. */
9368 found_decl_spec = true;
9369 /* If the next token is an appropriate keyword, we can simply
9370 add it to the list. */
9371 switch (token->keyword)
9377 if (!at_class_scope_p ())
9379 error_at (token->location, "%<friend%> used outside of class");
9380 cp_lexer_purge_token (parser->lexer);
9384 ++decl_specs->specs[(int) ds_friend];
9385 /* Consume the token. */
9386 cp_lexer_consume_token (parser->lexer);
9391 ++decl_specs->specs[(int) ds_constexpr];
9392 cp_lexer_consume_token (parser->lexer);
9395 /* function-specifier:
9402 cp_parser_function_specifier_opt (parser, decl_specs);
9408 ++decl_specs->specs[(int) ds_typedef];
9409 /* Consume the token. */
9410 cp_lexer_consume_token (parser->lexer);
9411 /* A constructor declarator cannot appear in a typedef. */
9412 constructor_possible_p = false;
9413 /* The "typedef" keyword can only occur in a declaration; we
9414 may as well commit at this point. */
9415 cp_parser_commit_to_tentative_parse (parser);
9417 if (decl_specs->storage_class != sc_none)
9418 decl_specs->conflicting_specifiers_p = true;
9421 /* storage-class-specifier:
9431 if (cxx_dialect == cxx98)
9433 /* Consume the token. */
9434 cp_lexer_consume_token (parser->lexer);
9436 /* Complain about `auto' as a storage specifier, if
9437 we're complaining about C++0x compatibility. */
9438 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9439 " will change meaning in C++0x; please remove it");
9441 /* Set the storage class anyway. */
9442 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9446 /* C++0x auto type-specifier. */
9447 found_decl_spec = false;
9454 /* Consume the token. */
9455 cp_lexer_consume_token (parser->lexer);
9456 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9460 /* Consume the token. */
9461 cp_lexer_consume_token (parser->lexer);
9462 ++decl_specs->specs[(int) ds_thread];
9466 /* We did not yet find a decl-specifier yet. */
9467 found_decl_spec = false;
9471 /* Constructors are a special case. The `S' in `S()' is not a
9472 decl-specifier; it is the beginning of the declarator. */
9475 && constructor_possible_p
9476 && (cp_parser_constructor_declarator_p
9477 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9479 /* If we don't have a DECL_SPEC yet, then we must be looking at
9480 a type-specifier. */
9481 if (!found_decl_spec && !constructor_p)
9483 int decl_spec_declares_class_or_enum;
9484 bool is_cv_qualifier;
9488 = cp_parser_type_specifier (parser, flags,
9490 /*is_declaration=*/true,
9491 &decl_spec_declares_class_or_enum,
9493 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9495 /* If this type-specifier referenced a user-defined type
9496 (a typedef, class-name, etc.), then we can't allow any
9497 more such type-specifiers henceforth.
9501 The longest sequence of decl-specifiers that could
9502 possibly be a type name is taken as the
9503 decl-specifier-seq of a declaration. The sequence shall
9504 be self-consistent as described below.
9508 As a general rule, at most one type-specifier is allowed
9509 in the complete decl-specifier-seq of a declaration. The
9510 only exceptions are the following:
9512 -- const or volatile can be combined with any other
9515 -- signed or unsigned can be combined with char, long,
9523 void g (const int Pc);
9525 Here, Pc is *not* part of the decl-specifier seq; it's
9526 the declarator. Therefore, once we see a type-specifier
9527 (other than a cv-qualifier), we forbid any additional
9528 user-defined types. We *do* still allow things like `int
9529 int' to be considered a decl-specifier-seq, and issue the
9530 error message later. */
9531 if (type_spec && !is_cv_qualifier)
9532 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9533 /* A constructor declarator cannot follow a type-specifier. */
9536 constructor_possible_p = false;
9537 found_decl_spec = true;
9538 if (!is_cv_qualifier)
9539 decl_specs->any_type_specifiers_p = true;
9543 /* If we still do not have a DECL_SPEC, then there are no more
9545 if (!found_decl_spec)
9548 decl_specs->any_specifiers_p = true;
9549 /* After we see one decl-specifier, further decl-specifiers are
9551 flags |= CP_PARSER_FLAGS_OPTIONAL;
9554 cp_parser_check_decl_spec (decl_specs, start_token->location);
9556 /* Don't allow a friend specifier with a class definition. */
9557 if (decl_specs->specs[(int) ds_friend] != 0
9558 && (*declares_class_or_enum & 2))
9559 error_at (start_token->location,
9560 "class definition may not be declared a friend");
9563 /* Parse an (optional) storage-class-specifier.
9565 storage-class-specifier:
9574 storage-class-specifier:
9577 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9580 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9582 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9585 if (cxx_dialect != cxx98)
9587 /* Fall through for C++98. */
9594 /* Consume the token. */
9595 return cp_lexer_consume_token (parser->lexer)->u.value;
9602 /* Parse an (optional) function-specifier.
9609 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9610 Updates DECL_SPECS, if it is non-NULL. */
9613 cp_parser_function_specifier_opt (cp_parser* parser,
9614 cp_decl_specifier_seq *decl_specs)
9616 cp_token *token = cp_lexer_peek_token (parser->lexer);
9617 switch (token->keyword)
9621 ++decl_specs->specs[(int) ds_inline];
9625 /* 14.5.2.3 [temp.mem]
9627 A member function template shall not be virtual. */
9628 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9629 error_at (token->location, "templates may not be %<virtual%>");
9630 else if (decl_specs)
9631 ++decl_specs->specs[(int) ds_virtual];
9636 ++decl_specs->specs[(int) ds_explicit];
9643 /* Consume the token. */
9644 return cp_lexer_consume_token (parser->lexer)->u.value;
9647 /* Parse a linkage-specification.
9649 linkage-specification:
9650 extern string-literal { declaration-seq [opt] }
9651 extern string-literal declaration */
9654 cp_parser_linkage_specification (cp_parser* parser)
9658 /* Look for the `extern' keyword. */
9659 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
9661 /* Look for the string-literal. */
9662 linkage = cp_parser_string_literal (parser, false, false);
9664 /* Transform the literal into an identifier. If the literal is a
9665 wide-character string, or contains embedded NULs, then we can't
9666 handle it as the user wants. */
9667 if (strlen (TREE_STRING_POINTER (linkage))
9668 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9670 cp_parser_error (parser, "invalid linkage-specification");
9671 /* Assume C++ linkage. */
9672 linkage = lang_name_cplusplus;
9675 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9677 /* We're now using the new linkage. */
9678 push_lang_context (linkage);
9680 /* If the next token is a `{', then we're using the first
9682 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9684 /* Consume the `{' token. */
9685 cp_lexer_consume_token (parser->lexer);
9686 /* Parse the declarations. */
9687 cp_parser_declaration_seq_opt (parser);
9688 /* Look for the closing `}'. */
9689 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9691 /* Otherwise, there's just one declaration. */
9694 bool saved_in_unbraced_linkage_specification_p;
9696 saved_in_unbraced_linkage_specification_p
9697 = parser->in_unbraced_linkage_specification_p;
9698 parser->in_unbraced_linkage_specification_p = true;
9699 cp_parser_declaration (parser);
9700 parser->in_unbraced_linkage_specification_p
9701 = saved_in_unbraced_linkage_specification_p;
9704 /* We're done with the linkage-specification. */
9705 pop_lang_context ();
9708 /* Parse a static_assert-declaration.
9710 static_assert-declaration:
9711 static_assert ( constant-expression , string-literal ) ;
9713 If MEMBER_P, this static_assert is a class member. */
9716 cp_parser_static_assert(cp_parser *parser, bool member_p)
9721 location_t saved_loc;
9723 /* Peek at the `static_assert' token so we can keep track of exactly
9724 where the static assertion started. */
9725 token = cp_lexer_peek_token (parser->lexer);
9726 saved_loc = token->location;
9728 /* Look for the `static_assert' keyword. */
9729 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9733 /* We know we are in a static assertion; commit to any tentative
9735 if (cp_parser_parsing_tentatively (parser))
9736 cp_parser_commit_to_tentative_parse (parser);
9738 /* Parse the `(' starting the static assertion condition. */
9739 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9741 /* Parse the constant-expression. */
9743 cp_parser_constant_expression (parser,
9744 /*allow_non_constant_p=*/false,
9745 /*non_constant_p=*/NULL);
9747 /* Parse the separating `,'. */
9748 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9750 /* Parse the string-literal message. */
9751 message = cp_parser_string_literal (parser,
9752 /*translate=*/false,
9755 /* A `)' completes the static assertion. */
9756 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9757 cp_parser_skip_to_closing_parenthesis (parser,
9758 /*recovering=*/true,
9760 /*consume_paren=*/true);
9762 /* A semicolon terminates the declaration. */
9763 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9765 /* Complete the static assertion, which may mean either processing
9766 the static assert now or saving it for template instantiation. */
9767 finish_static_assert (condition, message, saved_loc, member_p);
9770 /* Parse a `decltype' type. Returns the type.
9772 simple-type-specifier:
9773 decltype ( expression ) */
9776 cp_parser_decltype (cp_parser *parser)
9779 bool id_expression_or_member_access_p = false;
9780 const char *saved_message;
9781 bool saved_integral_constant_expression_p;
9782 bool saved_non_integral_constant_expression_p;
9783 cp_token *id_expr_start_token;
9785 /* Look for the `decltype' token. */
9786 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
9787 return error_mark_node;
9789 /* Types cannot be defined in a `decltype' expression. Save away the
9791 saved_message = parser->type_definition_forbidden_message;
9793 /* And create the new one. */
9794 parser->type_definition_forbidden_message
9795 = G_("types may not be defined in %<decltype%> expressions");
9797 /* The restrictions on constant-expressions do not apply inside
9798 decltype expressions. */
9799 saved_integral_constant_expression_p
9800 = parser->integral_constant_expression_p;
9801 saved_non_integral_constant_expression_p
9802 = parser->non_integral_constant_expression_p;
9803 parser->integral_constant_expression_p = false;
9805 /* Do not actually evaluate the expression. */
9806 ++cp_unevaluated_operand;
9808 /* Do not warn about problems with the expression. */
9809 ++c_inhibit_evaluation_warnings;
9811 /* Parse the opening `('. */
9812 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9813 return error_mark_node;
9815 /* First, try parsing an id-expression. */
9816 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9817 cp_parser_parse_tentatively (parser);
9818 expr = cp_parser_id_expression (parser,
9819 /*template_keyword_p=*/false,
9820 /*check_dependency_p=*/true,
9821 /*template_p=*/NULL,
9822 /*declarator_p=*/false,
9823 /*optional_p=*/false);
9825 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9827 bool non_integral_constant_expression_p = false;
9828 tree id_expression = expr;
9830 const char *error_msg;
9832 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9833 /* Lookup the name we got back from the id-expression. */
9834 expr = cp_parser_lookup_name (parser, expr,
9836 /*is_template=*/false,
9837 /*is_namespace=*/false,
9838 /*check_dependency=*/true,
9839 /*ambiguous_decls=*/NULL,
9840 id_expr_start_token->location);
9843 && expr != error_mark_node
9844 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9845 && TREE_CODE (expr) != TYPE_DECL
9846 && (TREE_CODE (expr) != BIT_NOT_EXPR
9847 || !TYPE_P (TREE_OPERAND (expr, 0)))
9848 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9850 /* Complete lookup of the id-expression. */
9851 expr = (finish_id_expression
9852 (id_expression, expr, parser->scope, &idk,
9853 /*integral_constant_expression_p=*/false,
9854 /*allow_non_integral_constant_expression_p=*/true,
9855 &non_integral_constant_expression_p,
9856 /*template_p=*/false,
9858 /*address_p=*/false,
9859 /*template_arg_p=*/false,
9861 id_expr_start_token->location));
9863 if (expr == error_mark_node)
9864 /* We found an id-expression, but it was something that we
9865 should not have found. This is an error, not something
9866 we can recover from, so note that we found an
9867 id-expression and we'll recover as gracefully as
9869 id_expression_or_member_access_p = true;
9873 && expr != error_mark_node
9874 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9875 /* We have an id-expression. */
9876 id_expression_or_member_access_p = true;
9879 if (!id_expression_or_member_access_p)
9881 /* Abort the id-expression parse. */
9882 cp_parser_abort_tentative_parse (parser);
9884 /* Parsing tentatively, again. */
9885 cp_parser_parse_tentatively (parser);
9887 /* Parse a class member access. */
9888 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9890 /*member_access_only_p=*/true, NULL);
9893 && expr != error_mark_node
9894 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9895 /* We have an id-expression. */
9896 id_expression_or_member_access_p = true;
9899 if (id_expression_or_member_access_p)
9900 /* We have parsed the complete id-expression or member access. */
9901 cp_parser_parse_definitely (parser);
9904 bool saved_greater_than_is_operator_p;
9906 /* Abort our attempt to parse an id-expression or member access
9908 cp_parser_abort_tentative_parse (parser);
9910 /* Within a parenthesized expression, a `>' token is always
9911 the greater-than operator. */
9912 saved_greater_than_is_operator_p
9913 = parser->greater_than_is_operator_p;
9914 parser->greater_than_is_operator_p = true;
9916 /* Parse a full expression. */
9917 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9919 /* The `>' token might be the end of a template-id or
9920 template-parameter-list now. */
9921 parser->greater_than_is_operator_p
9922 = saved_greater_than_is_operator_p;
9925 /* Go back to evaluating expressions. */
9926 --cp_unevaluated_operand;
9927 --c_inhibit_evaluation_warnings;
9929 /* Restore the old message and the integral constant expression
9931 parser->type_definition_forbidden_message = saved_message;
9932 parser->integral_constant_expression_p
9933 = saved_integral_constant_expression_p;
9934 parser->non_integral_constant_expression_p
9935 = saved_non_integral_constant_expression_p;
9937 if (expr == error_mark_node)
9939 /* Skip everything up to the closing `)'. */
9940 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9941 /*consume_paren=*/true);
9942 return error_mark_node;
9945 /* Parse to the closing `)'. */
9946 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9948 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9949 /*consume_paren=*/true);
9950 return error_mark_node;
9953 return finish_decltype_type (expr, id_expression_or_member_access_p);
9956 /* Special member functions [gram.special] */
9958 /* Parse a conversion-function-id.
9960 conversion-function-id:
9961 operator conversion-type-id
9963 Returns an IDENTIFIER_NODE representing the operator. */
9966 cp_parser_conversion_function_id (cp_parser* parser)
9970 tree saved_qualifying_scope;
9971 tree saved_object_scope;
9972 tree pushed_scope = NULL_TREE;
9974 /* Look for the `operator' token. */
9975 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
9976 return error_mark_node;
9977 /* When we parse the conversion-type-id, the current scope will be
9978 reset. However, we need that information in able to look up the
9979 conversion function later, so we save it here. */
9980 saved_scope = parser->scope;
9981 saved_qualifying_scope = parser->qualifying_scope;
9982 saved_object_scope = parser->object_scope;
9983 /* We must enter the scope of the class so that the names of
9984 entities declared within the class are available in the
9985 conversion-type-id. For example, consider:
9992 S::operator I() { ... }
9994 In order to see that `I' is a type-name in the definition, we
9995 must be in the scope of `S'. */
9997 pushed_scope = push_scope (saved_scope);
9998 /* Parse the conversion-type-id. */
9999 type = cp_parser_conversion_type_id (parser);
10000 /* Leave the scope of the class, if any. */
10002 pop_scope (pushed_scope);
10003 /* Restore the saved scope. */
10004 parser->scope = saved_scope;
10005 parser->qualifying_scope = saved_qualifying_scope;
10006 parser->object_scope = saved_object_scope;
10007 /* If the TYPE is invalid, indicate failure. */
10008 if (type == error_mark_node)
10009 return error_mark_node;
10010 return mangle_conv_op_name_for_type (type);
10013 /* Parse a conversion-type-id:
10015 conversion-type-id:
10016 type-specifier-seq conversion-declarator [opt]
10018 Returns the TYPE specified. */
10021 cp_parser_conversion_type_id (cp_parser* parser)
10024 cp_decl_specifier_seq type_specifiers;
10025 cp_declarator *declarator;
10026 tree type_specified;
10028 /* Parse the attributes. */
10029 attributes = cp_parser_attributes_opt (parser);
10030 /* Parse the type-specifiers. */
10031 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10032 /*is_trailing_return=*/false,
10034 /* If that didn't work, stop. */
10035 if (type_specifiers.type == error_mark_node)
10036 return error_mark_node;
10037 /* Parse the conversion-declarator. */
10038 declarator = cp_parser_conversion_declarator_opt (parser);
10040 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10041 /*initialized=*/0, &attributes);
10043 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10045 /* Don't give this error when parsing tentatively. This happens to
10046 work because we always parse this definitively once. */
10047 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10048 && type_uses_auto (type_specified))
10050 error ("invalid use of %<auto%> in conversion operator");
10051 return error_mark_node;
10054 return type_specified;
10057 /* Parse an (optional) conversion-declarator.
10059 conversion-declarator:
10060 ptr-operator conversion-declarator [opt]
10064 static cp_declarator *
10065 cp_parser_conversion_declarator_opt (cp_parser* parser)
10067 enum tree_code code;
10069 cp_cv_quals cv_quals;
10071 /* We don't know if there's a ptr-operator next, or not. */
10072 cp_parser_parse_tentatively (parser);
10073 /* Try the ptr-operator. */
10074 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10075 /* If it worked, look for more conversion-declarators. */
10076 if (cp_parser_parse_definitely (parser))
10078 cp_declarator *declarator;
10080 /* Parse another optional declarator. */
10081 declarator = cp_parser_conversion_declarator_opt (parser);
10083 return cp_parser_make_indirect_declarator
10084 (code, class_type, cv_quals, declarator);
10090 /* Parse an (optional) ctor-initializer.
10093 : mem-initializer-list
10095 Returns TRUE iff the ctor-initializer was actually present. */
10098 cp_parser_ctor_initializer_opt (cp_parser* parser)
10100 /* If the next token is not a `:', then there is no
10101 ctor-initializer. */
10102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10104 /* Do default initialization of any bases and members. */
10105 if (DECL_CONSTRUCTOR_P (current_function_decl))
10106 finish_mem_initializers (NULL_TREE);
10111 /* Consume the `:' token. */
10112 cp_lexer_consume_token (parser->lexer);
10113 /* And the mem-initializer-list. */
10114 cp_parser_mem_initializer_list (parser);
10119 /* Parse a mem-initializer-list.
10121 mem-initializer-list:
10122 mem-initializer ... [opt]
10123 mem-initializer ... [opt] , mem-initializer-list */
10126 cp_parser_mem_initializer_list (cp_parser* parser)
10128 tree mem_initializer_list = NULL_TREE;
10129 cp_token *token = cp_lexer_peek_token (parser->lexer);
10131 /* Let the semantic analysis code know that we are starting the
10132 mem-initializer-list. */
10133 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10134 error_at (token->location,
10135 "only constructors take base initializers");
10137 /* Loop through the list. */
10140 tree mem_initializer;
10142 token = cp_lexer_peek_token (parser->lexer);
10143 /* Parse the mem-initializer. */
10144 mem_initializer = cp_parser_mem_initializer (parser);
10145 /* If the next token is a `...', we're expanding member initializers. */
10146 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10148 /* Consume the `...'. */
10149 cp_lexer_consume_token (parser->lexer);
10151 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10152 can be expanded but members cannot. */
10153 if (mem_initializer != error_mark_node
10154 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10156 error_at (token->location,
10157 "cannot expand initializer for member %<%D%>",
10158 TREE_PURPOSE (mem_initializer));
10159 mem_initializer = error_mark_node;
10162 /* Construct the pack expansion type. */
10163 if (mem_initializer != error_mark_node)
10164 mem_initializer = make_pack_expansion (mem_initializer);
10166 /* Add it to the list, unless it was erroneous. */
10167 if (mem_initializer != error_mark_node)
10169 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10170 mem_initializer_list = mem_initializer;
10172 /* If the next token is not a `,', we're done. */
10173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10175 /* Consume the `,' token. */
10176 cp_lexer_consume_token (parser->lexer);
10179 /* Perform semantic analysis. */
10180 if (DECL_CONSTRUCTOR_P (current_function_decl))
10181 finish_mem_initializers (mem_initializer_list);
10184 /* Parse a mem-initializer.
10187 mem-initializer-id ( expression-list [opt] )
10188 mem-initializer-id braced-init-list
10193 ( expression-list [opt] )
10195 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10196 class) or FIELD_DECL (for a non-static data member) to initialize;
10197 the TREE_VALUE is the expression-list. An empty initialization
10198 list is represented by void_list_node. */
10201 cp_parser_mem_initializer (cp_parser* parser)
10203 tree mem_initializer_id;
10204 tree expression_list;
10206 cp_token *token = cp_lexer_peek_token (parser->lexer);
10208 /* Find out what is being initialized. */
10209 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10211 permerror (token->location,
10212 "anachronistic old-style base class initializer");
10213 mem_initializer_id = NULL_TREE;
10217 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10218 if (mem_initializer_id == error_mark_node)
10219 return mem_initializer_id;
10221 member = expand_member_init (mem_initializer_id);
10222 if (member && !DECL_P (member))
10223 in_base_initializer = 1;
10225 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10227 bool expr_non_constant_p;
10228 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10229 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10230 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10231 expression_list = build_tree_list (NULL_TREE, expression_list);
10236 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10238 /*allow_expansion_p=*/true,
10239 /*non_constant_p=*/NULL);
10241 return error_mark_node;
10242 expression_list = build_tree_list_vec (vec);
10243 release_tree_vector (vec);
10246 if (expression_list == error_mark_node)
10247 return error_mark_node;
10248 if (!expression_list)
10249 expression_list = void_type_node;
10251 in_base_initializer = 0;
10253 return member ? build_tree_list (member, expression_list) : error_mark_node;
10256 /* Parse a mem-initializer-id.
10258 mem-initializer-id:
10259 :: [opt] nested-name-specifier [opt] class-name
10262 Returns a TYPE indicating the class to be initializer for the first
10263 production. Returns an IDENTIFIER_NODE indicating the data member
10264 to be initialized for the second production. */
10267 cp_parser_mem_initializer_id (cp_parser* parser)
10269 bool global_scope_p;
10270 bool nested_name_specifier_p;
10271 bool template_p = false;
10274 cp_token *token = cp_lexer_peek_token (parser->lexer);
10276 /* `typename' is not allowed in this context ([temp.res]). */
10277 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10279 error_at (token->location,
10280 "keyword %<typename%> not allowed in this context (a qualified "
10281 "member initializer is implicitly a type)");
10282 cp_lexer_consume_token (parser->lexer);
10284 /* Look for the optional `::' operator. */
10286 = (cp_parser_global_scope_opt (parser,
10287 /*current_scope_valid_p=*/false)
10289 /* Look for the optional nested-name-specifier. The simplest way to
10294 The keyword `typename' is not permitted in a base-specifier or
10295 mem-initializer; in these contexts a qualified name that
10296 depends on a template-parameter is implicitly assumed to be a
10299 is to assume that we have seen the `typename' keyword at this
10301 nested_name_specifier_p
10302 = (cp_parser_nested_name_specifier_opt (parser,
10303 /*typename_keyword_p=*/true,
10304 /*check_dependency_p=*/true,
10306 /*is_declaration=*/true)
10308 if (nested_name_specifier_p)
10309 template_p = cp_parser_optional_template_keyword (parser);
10310 /* If there is a `::' operator or a nested-name-specifier, then we
10311 are definitely looking for a class-name. */
10312 if (global_scope_p || nested_name_specifier_p)
10313 return cp_parser_class_name (parser,
10314 /*typename_keyword_p=*/true,
10315 /*template_keyword_p=*/template_p,
10317 /*check_dependency_p=*/true,
10318 /*class_head_p=*/false,
10319 /*is_declaration=*/true);
10320 /* Otherwise, we could also be looking for an ordinary identifier. */
10321 cp_parser_parse_tentatively (parser);
10322 /* Try a class-name. */
10323 id = cp_parser_class_name (parser,
10324 /*typename_keyword_p=*/true,
10325 /*template_keyword_p=*/false,
10327 /*check_dependency_p=*/true,
10328 /*class_head_p=*/false,
10329 /*is_declaration=*/true);
10330 /* If we found one, we're done. */
10331 if (cp_parser_parse_definitely (parser))
10333 /* Otherwise, look for an ordinary identifier. */
10334 return cp_parser_identifier (parser);
10337 /* Overloading [gram.over] */
10339 /* Parse an operator-function-id.
10341 operator-function-id:
10344 Returns an IDENTIFIER_NODE for the operator which is a
10345 human-readable spelling of the identifier, e.g., `operator +'. */
10348 cp_parser_operator_function_id (cp_parser* parser)
10350 /* Look for the `operator' keyword. */
10351 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10352 return error_mark_node;
10353 /* And then the name of the operator itself. */
10354 return cp_parser_operator (parser);
10357 /* Parse an operator.
10360 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10361 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10362 || ++ -- , ->* -> () []
10369 Returns an IDENTIFIER_NODE for the operator which is a
10370 human-readable spelling of the identifier, e.g., `operator +'. */
10373 cp_parser_operator (cp_parser* parser)
10375 tree id = NULL_TREE;
10378 /* Peek at the next token. */
10379 token = cp_lexer_peek_token (parser->lexer);
10380 /* Figure out which operator we have. */
10381 switch (token->type)
10387 /* The keyword should be either `new' or `delete'. */
10388 if (token->keyword == RID_NEW)
10390 else if (token->keyword == RID_DELETE)
10395 /* Consume the `new' or `delete' token. */
10396 cp_lexer_consume_token (parser->lexer);
10398 /* Peek at the next token. */
10399 token = cp_lexer_peek_token (parser->lexer);
10400 /* If it's a `[' token then this is the array variant of the
10402 if (token->type == CPP_OPEN_SQUARE)
10404 /* Consume the `[' token. */
10405 cp_lexer_consume_token (parser->lexer);
10406 /* Look for the `]' token. */
10407 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10408 id = ansi_opname (op == NEW_EXPR
10409 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10411 /* Otherwise, we have the non-array variant. */
10413 id = ansi_opname (op);
10419 id = ansi_opname (PLUS_EXPR);
10423 id = ansi_opname (MINUS_EXPR);
10427 id = ansi_opname (MULT_EXPR);
10431 id = ansi_opname (TRUNC_DIV_EXPR);
10435 id = ansi_opname (TRUNC_MOD_EXPR);
10439 id = ansi_opname (BIT_XOR_EXPR);
10443 id = ansi_opname (BIT_AND_EXPR);
10447 id = ansi_opname (BIT_IOR_EXPR);
10451 id = ansi_opname (BIT_NOT_EXPR);
10455 id = ansi_opname (TRUTH_NOT_EXPR);
10459 id = ansi_assopname (NOP_EXPR);
10463 id = ansi_opname (LT_EXPR);
10467 id = ansi_opname (GT_EXPR);
10471 id = ansi_assopname (PLUS_EXPR);
10475 id = ansi_assopname (MINUS_EXPR);
10479 id = ansi_assopname (MULT_EXPR);
10483 id = ansi_assopname (TRUNC_DIV_EXPR);
10487 id = ansi_assopname (TRUNC_MOD_EXPR);
10491 id = ansi_assopname (BIT_XOR_EXPR);
10495 id = ansi_assopname (BIT_AND_EXPR);
10499 id = ansi_assopname (BIT_IOR_EXPR);
10503 id = ansi_opname (LSHIFT_EXPR);
10507 id = ansi_opname (RSHIFT_EXPR);
10510 case CPP_LSHIFT_EQ:
10511 id = ansi_assopname (LSHIFT_EXPR);
10514 case CPP_RSHIFT_EQ:
10515 id = ansi_assopname (RSHIFT_EXPR);
10519 id = ansi_opname (EQ_EXPR);
10523 id = ansi_opname (NE_EXPR);
10527 id = ansi_opname (LE_EXPR);
10530 case CPP_GREATER_EQ:
10531 id = ansi_opname (GE_EXPR);
10535 id = ansi_opname (TRUTH_ANDIF_EXPR);
10539 id = ansi_opname (TRUTH_ORIF_EXPR);
10542 case CPP_PLUS_PLUS:
10543 id = ansi_opname (POSTINCREMENT_EXPR);
10546 case CPP_MINUS_MINUS:
10547 id = ansi_opname (PREDECREMENT_EXPR);
10551 id = ansi_opname (COMPOUND_EXPR);
10554 case CPP_DEREF_STAR:
10555 id = ansi_opname (MEMBER_REF);
10559 id = ansi_opname (COMPONENT_REF);
10562 case CPP_OPEN_PAREN:
10563 /* Consume the `('. */
10564 cp_lexer_consume_token (parser->lexer);
10565 /* Look for the matching `)'. */
10566 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10567 return ansi_opname (CALL_EXPR);
10569 case CPP_OPEN_SQUARE:
10570 /* Consume the `['. */
10571 cp_lexer_consume_token (parser->lexer);
10572 /* Look for the matching `]'. */
10573 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10574 return ansi_opname (ARRAY_REF);
10577 /* Anything else is an error. */
10581 /* If we have selected an identifier, we need to consume the
10584 cp_lexer_consume_token (parser->lexer);
10585 /* Otherwise, no valid operator name was present. */
10588 cp_parser_error (parser, "expected operator");
10589 id = error_mark_node;
10595 /* Parse a template-declaration.
10597 template-declaration:
10598 export [opt] template < template-parameter-list > declaration
10600 If MEMBER_P is TRUE, this template-declaration occurs within a
10603 The grammar rule given by the standard isn't correct. What
10604 is really meant is:
10606 template-declaration:
10607 export [opt] template-parameter-list-seq
10608 decl-specifier-seq [opt] init-declarator [opt] ;
10609 export [opt] template-parameter-list-seq
10610 function-definition
10612 template-parameter-list-seq:
10613 template-parameter-list-seq [opt]
10614 template < template-parameter-list > */
10617 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10619 /* Check for `export'. */
10620 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10622 /* Consume the `export' token. */
10623 cp_lexer_consume_token (parser->lexer);
10624 /* Warn that we do not support `export'. */
10625 warning (0, "keyword %<export%> not implemented, and will be ignored");
10628 cp_parser_template_declaration_after_export (parser, member_p);
10631 /* Parse a template-parameter-list.
10633 template-parameter-list:
10635 template-parameter-list , template-parameter
10637 Returns a TREE_LIST. Each node represents a template parameter.
10638 The nodes are connected via their TREE_CHAINs. */
10641 cp_parser_template_parameter_list (cp_parser* parser)
10643 tree parameter_list = NULL_TREE;
10645 begin_template_parm_list ();
10650 bool is_parameter_pack;
10651 location_t parm_loc;
10653 /* Parse the template-parameter. */
10654 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10655 parameter = cp_parser_template_parameter (parser,
10657 &is_parameter_pack);
10658 /* Add it to the list. */
10659 if (parameter != error_mark_node)
10660 parameter_list = process_template_parm (parameter_list,
10664 is_parameter_pack);
10667 tree err_parm = build_tree_list (parameter, parameter);
10668 TREE_VALUE (err_parm) = error_mark_node;
10669 parameter_list = chainon (parameter_list, err_parm);
10672 /* If the next token is not a `,', we're done. */
10673 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10675 /* Otherwise, consume the `,' token. */
10676 cp_lexer_consume_token (parser->lexer);
10679 return end_template_parm_list (parameter_list);
10682 /* Parse a template-parameter.
10684 template-parameter:
10686 parameter-declaration
10688 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10689 the parameter. The TREE_PURPOSE is the default value, if any.
10690 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10691 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10692 set to true iff this parameter is a parameter pack. */
10695 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10696 bool *is_parameter_pack)
10699 cp_parameter_declarator *parameter_declarator;
10700 cp_declarator *id_declarator;
10703 /* Assume it is a type parameter or a template parameter. */
10704 *is_non_type = false;
10705 /* Assume it not a parameter pack. */
10706 *is_parameter_pack = false;
10707 /* Peek at the next token. */
10708 token = cp_lexer_peek_token (parser->lexer);
10709 /* If it is `class' or `template', we have a type-parameter. */
10710 if (token->keyword == RID_TEMPLATE)
10711 return cp_parser_type_parameter (parser, is_parameter_pack);
10712 /* If it is `class' or `typename' we do not know yet whether it is a
10713 type parameter or a non-type parameter. Consider:
10715 template <typename T, typename T::X X> ...
10719 template <class C, class D*> ...
10721 Here, the first parameter is a type parameter, and the second is
10722 a non-type parameter. We can tell by looking at the token after
10723 the identifier -- if it is a `,', `=', or `>' then we have a type
10725 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10727 /* Peek at the token after `class' or `typename'. */
10728 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10729 /* If it's an ellipsis, we have a template type parameter
10731 if (token->type == CPP_ELLIPSIS)
10732 return cp_parser_type_parameter (parser, is_parameter_pack);
10733 /* If it's an identifier, skip it. */
10734 if (token->type == CPP_NAME)
10735 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10736 /* Now, see if the token looks like the end of a template
10738 if (token->type == CPP_COMMA
10739 || token->type == CPP_EQ
10740 || token->type == CPP_GREATER)
10741 return cp_parser_type_parameter (parser, is_parameter_pack);
10744 /* Otherwise, it is a non-type parameter.
10748 When parsing a default template-argument for a non-type
10749 template-parameter, the first non-nested `>' is taken as the end
10750 of the template parameter-list rather than a greater-than
10752 *is_non_type = true;
10753 parameter_declarator
10754 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10755 /*parenthesized_p=*/NULL);
10757 /* If the parameter declaration is marked as a parameter pack, set
10758 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10759 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10761 if (parameter_declarator
10762 && parameter_declarator->declarator
10763 && parameter_declarator->declarator->parameter_pack_p)
10765 *is_parameter_pack = true;
10766 parameter_declarator->declarator->parameter_pack_p = false;
10769 /* If the next token is an ellipsis, and we don't already have it
10770 marked as a parameter pack, then we have a parameter pack (that
10771 has no declarator). */
10772 if (!*is_parameter_pack
10773 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10774 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10776 /* Consume the `...'. */
10777 cp_lexer_consume_token (parser->lexer);
10778 maybe_warn_variadic_templates ();
10780 *is_parameter_pack = true;
10782 /* We might end up with a pack expansion as the type of the non-type
10783 template parameter, in which case this is a non-type template
10785 else if (parameter_declarator
10786 && parameter_declarator->decl_specifiers.type
10787 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10789 *is_parameter_pack = true;
10790 parameter_declarator->decl_specifiers.type =
10791 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10794 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10796 /* Parameter packs cannot have default arguments. However, a
10797 user may try to do so, so we'll parse them and give an
10798 appropriate diagnostic here. */
10800 /* Consume the `='. */
10801 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10802 cp_lexer_consume_token (parser->lexer);
10804 /* Find the name of the parameter pack. */
10805 id_declarator = parameter_declarator->declarator;
10806 while (id_declarator && id_declarator->kind != cdk_id)
10807 id_declarator = id_declarator->declarator;
10809 if (id_declarator && id_declarator->kind == cdk_id)
10810 error_at (start_token->location,
10811 "template parameter pack %qD cannot have a default argument",
10812 id_declarator->u.id.unqualified_name);
10814 error_at (start_token->location,
10815 "template parameter pack cannot have a default argument");
10817 /* Parse the default argument, but throw away the result. */
10818 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10821 parm = grokdeclarator (parameter_declarator->declarator,
10822 ¶meter_declarator->decl_specifiers,
10823 TPARM, /*initialized=*/0,
10824 /*attrlist=*/NULL);
10825 if (parm == error_mark_node)
10826 return error_mark_node;
10828 return build_tree_list (parameter_declarator->default_argument, parm);
10831 /* Parse a type-parameter.
10834 class identifier [opt]
10835 class identifier [opt] = type-id
10836 typename identifier [opt]
10837 typename identifier [opt] = type-id
10838 template < template-parameter-list > class identifier [opt]
10839 template < template-parameter-list > class identifier [opt]
10842 GNU Extension (variadic templates):
10845 class ... identifier [opt]
10846 typename ... identifier [opt]
10848 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10849 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10850 the declaration of the parameter.
10852 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10855 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10860 /* Look for a keyword to tell us what kind of parameter this is. */
10861 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
10863 return error_mark_node;
10865 switch (token->keyword)
10871 tree default_argument;
10873 /* If the next token is an ellipsis, we have a template
10875 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10877 /* Consume the `...' token. */
10878 cp_lexer_consume_token (parser->lexer);
10879 maybe_warn_variadic_templates ();
10881 *is_parameter_pack = true;
10884 /* If the next token is an identifier, then it names the
10886 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10887 identifier = cp_parser_identifier (parser);
10889 identifier = NULL_TREE;
10891 /* Create the parameter. */
10892 parameter = finish_template_type_parm (class_type_node, identifier);
10894 /* If the next token is an `=', we have a default argument. */
10895 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10897 /* Consume the `=' token. */
10898 cp_lexer_consume_token (parser->lexer);
10899 /* Parse the default-argument. */
10900 push_deferring_access_checks (dk_no_deferred);
10901 default_argument = cp_parser_type_id (parser);
10903 /* Template parameter packs cannot have default
10905 if (*is_parameter_pack)
10908 error_at (token->location,
10909 "template parameter pack %qD cannot have a "
10910 "default argument", identifier);
10912 error_at (token->location,
10913 "template parameter packs cannot have "
10914 "default arguments");
10915 default_argument = NULL_TREE;
10917 pop_deferring_access_checks ();
10920 default_argument = NULL_TREE;
10922 /* Create the combined representation of the parameter and the
10923 default argument. */
10924 parameter = build_tree_list (default_argument, parameter);
10931 tree default_argument;
10933 /* Look for the `<'. */
10934 cp_parser_require (parser, CPP_LESS, RT_LESS);
10935 /* Parse the template-parameter-list. */
10936 cp_parser_template_parameter_list (parser);
10937 /* Look for the `>'. */
10938 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
10939 /* Look for the `class' keyword. */
10940 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
10941 /* If the next token is an ellipsis, we have a template
10943 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10945 /* Consume the `...' token. */
10946 cp_lexer_consume_token (parser->lexer);
10947 maybe_warn_variadic_templates ();
10949 *is_parameter_pack = true;
10951 /* If the next token is an `=', then there is a
10952 default-argument. If the next token is a `>', we are at
10953 the end of the parameter-list. If the next token is a `,',
10954 then we are at the end of this parameter. */
10955 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10956 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10957 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10959 identifier = cp_parser_identifier (parser);
10960 /* Treat invalid names as if the parameter were nameless. */
10961 if (identifier == error_mark_node)
10962 identifier = NULL_TREE;
10965 identifier = NULL_TREE;
10967 /* Create the template parameter. */
10968 parameter = finish_template_template_parm (class_type_node,
10971 /* If the next token is an `=', then there is a
10972 default-argument. */
10973 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10977 /* Consume the `='. */
10978 cp_lexer_consume_token (parser->lexer);
10979 /* Parse the id-expression. */
10980 push_deferring_access_checks (dk_no_deferred);
10981 /* save token before parsing the id-expression, for error
10983 token = cp_lexer_peek_token (parser->lexer);
10985 = cp_parser_id_expression (parser,
10986 /*template_keyword_p=*/false,
10987 /*check_dependency_p=*/true,
10988 /*template_p=*/&is_template,
10989 /*declarator_p=*/false,
10990 /*optional_p=*/false);
10991 if (TREE_CODE (default_argument) == TYPE_DECL)
10992 /* If the id-expression was a template-id that refers to
10993 a template-class, we already have the declaration here,
10994 so no further lookup is needed. */
10997 /* Look up the name. */
10999 = cp_parser_lookup_name (parser, default_argument,
11001 /*is_template=*/is_template,
11002 /*is_namespace=*/false,
11003 /*check_dependency=*/true,
11004 /*ambiguous_decls=*/NULL,
11006 /* See if the default argument is valid. */
11008 = check_template_template_default_arg (default_argument);
11010 /* Template parameter packs cannot have default
11012 if (*is_parameter_pack)
11015 error_at (token->location,
11016 "template parameter pack %qD cannot "
11017 "have a default argument",
11020 error_at (token->location, "template parameter packs cannot "
11021 "have default arguments");
11022 default_argument = NULL_TREE;
11024 pop_deferring_access_checks ();
11027 default_argument = NULL_TREE;
11029 /* Create the combined representation of the parameter and the
11030 default argument. */
11031 parameter = build_tree_list (default_argument, parameter);
11036 gcc_unreachable ();
11043 /* Parse a template-id.
11046 template-name < template-argument-list [opt] >
11048 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11049 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11050 returned. Otherwise, if the template-name names a function, or set
11051 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11052 names a class, returns a TYPE_DECL for the specialization.
11054 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11055 uninstantiated templates. */
11058 cp_parser_template_id (cp_parser *parser,
11059 bool template_keyword_p,
11060 bool check_dependency_p,
11061 bool is_declaration)
11067 cp_token_position start_of_id = 0;
11068 deferred_access_check *chk;
11069 VEC (deferred_access_check,gc) *access_check;
11070 cp_token *next_token = NULL, *next_token_2 = NULL;
11071 bool is_identifier;
11073 /* If the next token corresponds to a template-id, there is no need
11075 next_token = cp_lexer_peek_token (parser->lexer);
11076 if (next_token->type == CPP_TEMPLATE_ID)
11078 struct tree_check *check_value;
11080 /* Get the stored value. */
11081 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11082 /* Perform any access checks that were deferred. */
11083 access_check = check_value->checks;
11087 VEC_iterate (deferred_access_check, access_check, i, chk) ;
11090 perform_or_defer_access_check (chk->binfo,
11095 /* Return the stored value. */
11096 return check_value->value;
11099 /* Avoid performing name lookup if there is no possibility of
11100 finding a template-id. */
11101 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11102 || (next_token->type == CPP_NAME
11103 && !cp_parser_nth_token_starts_template_argument_list_p
11106 cp_parser_error (parser, "expected template-id");
11107 return error_mark_node;
11110 /* Remember where the template-id starts. */
11111 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11112 start_of_id = cp_lexer_token_position (parser->lexer, false);
11114 push_deferring_access_checks (dk_deferred);
11116 /* Parse the template-name. */
11117 is_identifier = false;
11118 templ = cp_parser_template_name (parser, template_keyword_p,
11119 check_dependency_p,
11122 if (templ == error_mark_node || is_identifier)
11124 pop_deferring_access_checks ();
11128 /* If we find the sequence `[:' after a template-name, it's probably
11129 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11130 parse correctly the argument list. */
11131 next_token = cp_lexer_peek_token (parser->lexer);
11132 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11133 if (next_token->type == CPP_OPEN_SQUARE
11134 && next_token->flags & DIGRAPH
11135 && next_token_2->type == CPP_COLON
11136 && !(next_token_2->flags & PREV_WHITE))
11138 cp_parser_parse_tentatively (parser);
11139 /* Change `:' into `::'. */
11140 next_token_2->type = CPP_SCOPE;
11141 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11143 cp_lexer_consume_token (parser->lexer);
11145 /* Parse the arguments. */
11146 arguments = cp_parser_enclosed_template_argument_list (parser);
11147 if (!cp_parser_parse_definitely (parser))
11149 /* If we couldn't parse an argument list, then we revert our changes
11150 and return simply an error. Maybe this is not a template-id
11152 next_token_2->type = CPP_COLON;
11153 cp_parser_error (parser, "expected %<<%>");
11154 pop_deferring_access_checks ();
11155 return error_mark_node;
11157 /* Otherwise, emit an error about the invalid digraph, but continue
11158 parsing because we got our argument list. */
11159 if (permerror (next_token->location,
11160 "%<<::%> cannot begin a template-argument list"))
11162 static bool hint = false;
11163 inform (next_token->location,
11164 "%<<:%> is an alternate spelling for %<[%>."
11165 " Insert whitespace between %<<%> and %<::%>");
11166 if (!hint && !flag_permissive)
11168 inform (next_token->location, "(if you use %<-fpermissive%>"
11169 " G++ will accept your code)");
11176 /* Look for the `<' that starts the template-argument-list. */
11177 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11179 pop_deferring_access_checks ();
11180 return error_mark_node;
11182 /* Parse the arguments. */
11183 arguments = cp_parser_enclosed_template_argument_list (parser);
11186 /* Build a representation of the specialization. */
11187 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11188 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11189 else if (DECL_CLASS_TEMPLATE_P (templ)
11190 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11192 bool entering_scope;
11193 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11194 template (rather than some instantiation thereof) only if
11195 is not nested within some other construct. For example, in
11196 "template <typename T> void f(T) { A<T>::", A<T> is just an
11197 instantiation of A. */
11198 entering_scope = (template_parm_scope_p ()
11199 && cp_lexer_next_token_is (parser->lexer,
11202 = finish_template_type (templ, arguments, entering_scope);
11206 /* If it's not a class-template or a template-template, it should be
11207 a function-template. */
11208 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11209 || TREE_CODE (templ) == OVERLOAD
11210 || BASELINK_P (templ)));
11212 template_id = lookup_template_function (templ, arguments);
11215 /* If parsing tentatively, replace the sequence of tokens that makes
11216 up the template-id with a CPP_TEMPLATE_ID token. That way,
11217 should we re-parse the token stream, we will not have to repeat
11218 the effort required to do the parse, nor will we issue duplicate
11219 error messages about problems during instantiation of the
11223 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11225 /* Reset the contents of the START_OF_ID token. */
11226 token->type = CPP_TEMPLATE_ID;
11227 /* Retrieve any deferred checks. Do not pop this access checks yet
11228 so the memory will not be reclaimed during token replacing below. */
11229 token->u.tree_check_value = GGC_CNEW (struct tree_check);
11230 token->u.tree_check_value->value = template_id;
11231 token->u.tree_check_value->checks = get_deferred_access_checks ();
11232 token->keyword = RID_MAX;
11234 /* Purge all subsequent tokens. */
11235 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11237 /* ??? Can we actually assume that, if template_id ==
11238 error_mark_node, we will have issued a diagnostic to the
11239 user, as opposed to simply marking the tentative parse as
11241 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11242 error_at (token->location, "parse error in template argument list");
11245 pop_deferring_access_checks ();
11246 return template_id;
11249 /* Parse a template-name.
11254 The standard should actually say:
11258 operator-function-id
11260 A defect report has been filed about this issue.
11262 A conversion-function-id cannot be a template name because they cannot
11263 be part of a template-id. In fact, looking at this code:
11265 a.operator K<int>()
11267 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11268 It is impossible to call a templated conversion-function-id with an
11269 explicit argument list, since the only allowed template parameter is
11270 the type to which it is converting.
11272 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11273 `template' keyword, in a construction like:
11277 In that case `f' is taken to be a template-name, even though there
11278 is no way of knowing for sure.
11280 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11281 name refers to a set of overloaded functions, at least one of which
11282 is a template, or an IDENTIFIER_NODE with the name of the template,
11283 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11284 names are looked up inside uninstantiated templates. */
11287 cp_parser_template_name (cp_parser* parser,
11288 bool template_keyword_p,
11289 bool check_dependency_p,
11290 bool is_declaration,
11291 bool *is_identifier)
11296 cp_token *token = cp_lexer_peek_token (parser->lexer);
11298 /* If the next token is `operator', then we have either an
11299 operator-function-id or a conversion-function-id. */
11300 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11302 /* We don't know whether we're looking at an
11303 operator-function-id or a conversion-function-id. */
11304 cp_parser_parse_tentatively (parser);
11305 /* Try an operator-function-id. */
11306 identifier = cp_parser_operator_function_id (parser);
11307 /* If that didn't work, try a conversion-function-id. */
11308 if (!cp_parser_parse_definitely (parser))
11310 cp_parser_error (parser, "expected template-name");
11311 return error_mark_node;
11314 /* Look for the identifier. */
11316 identifier = cp_parser_identifier (parser);
11318 /* If we didn't find an identifier, we don't have a template-id. */
11319 if (identifier == error_mark_node)
11320 return error_mark_node;
11322 /* If the name immediately followed the `template' keyword, then it
11323 is a template-name. However, if the next token is not `<', then
11324 we do not treat it as a template-name, since it is not being used
11325 as part of a template-id. This enables us to handle constructs
11328 template <typename T> struct S { S(); };
11329 template <typename T> S<T>::S();
11331 correctly. We would treat `S' as a template -- if it were `S<T>'
11332 -- but we do not if there is no `<'. */
11334 if (processing_template_decl
11335 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11337 /* In a declaration, in a dependent context, we pretend that the
11338 "template" keyword was present in order to improve error
11339 recovery. For example, given:
11341 template <typename T> void f(T::X<int>);
11343 we want to treat "X<int>" as a template-id. */
11345 && !template_keyword_p
11346 && parser->scope && TYPE_P (parser->scope)
11347 && check_dependency_p
11348 && dependent_scope_p (parser->scope)
11349 /* Do not do this for dtors (or ctors), since they never
11350 need the template keyword before their name. */
11351 && !constructor_name_p (identifier, parser->scope))
11353 cp_token_position start = 0;
11355 /* Explain what went wrong. */
11356 error_at (token->location, "non-template %qD used as template",
11358 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11359 parser->scope, identifier);
11360 /* If parsing tentatively, find the location of the "<" token. */
11361 if (cp_parser_simulate_error (parser))
11362 start = cp_lexer_token_position (parser->lexer, true);
11363 /* Parse the template arguments so that we can issue error
11364 messages about them. */
11365 cp_lexer_consume_token (parser->lexer);
11366 cp_parser_enclosed_template_argument_list (parser);
11367 /* Skip tokens until we find a good place from which to
11368 continue parsing. */
11369 cp_parser_skip_to_closing_parenthesis (parser,
11370 /*recovering=*/true,
11372 /*consume_paren=*/false);
11373 /* If parsing tentatively, permanently remove the
11374 template argument list. That will prevent duplicate
11375 error messages from being issued about the missing
11376 "template" keyword. */
11378 cp_lexer_purge_tokens_after (parser->lexer, start);
11380 *is_identifier = true;
11384 /* If the "template" keyword is present, then there is generally
11385 no point in doing name-lookup, so we just return IDENTIFIER.
11386 But, if the qualifying scope is non-dependent then we can
11387 (and must) do name-lookup normally. */
11388 if (template_keyword_p
11390 || (TYPE_P (parser->scope)
11391 && dependent_type_p (parser->scope))))
11395 /* Look up the name. */
11396 decl = cp_parser_lookup_name (parser, identifier,
11398 /*is_template=*/true,
11399 /*is_namespace=*/false,
11400 check_dependency_p,
11401 /*ambiguous_decls=*/NULL,
11404 /* If DECL is a template, then the name was a template-name. */
11405 if (TREE_CODE (decl) == TEMPLATE_DECL)
11409 tree fn = NULL_TREE;
11411 /* The standard does not explicitly indicate whether a name that
11412 names a set of overloaded declarations, some of which are
11413 templates, is a template-name. However, such a name should
11414 be a template-name; otherwise, there is no way to form a
11415 template-id for the overloaded templates. */
11416 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11417 if (TREE_CODE (fns) == OVERLOAD)
11418 for (fn = fns; fn; fn = OVL_NEXT (fn))
11419 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11424 /* The name does not name a template. */
11425 cp_parser_error (parser, "expected template-name");
11426 return error_mark_node;
11430 /* If DECL is dependent, and refers to a function, then just return
11431 its name; we will look it up again during template instantiation. */
11432 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11434 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11435 if (TYPE_P (scope) && dependent_type_p (scope))
11442 /* Parse a template-argument-list.
11444 template-argument-list:
11445 template-argument ... [opt]
11446 template-argument-list , template-argument ... [opt]
11448 Returns a TREE_VEC containing the arguments. */
11451 cp_parser_template_argument_list (cp_parser* parser)
11453 tree fixed_args[10];
11454 unsigned n_args = 0;
11455 unsigned alloced = 10;
11456 tree *arg_ary = fixed_args;
11458 bool saved_in_template_argument_list_p;
11460 bool saved_non_ice_p;
11462 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11463 parser->in_template_argument_list_p = true;
11464 /* Even if the template-id appears in an integral
11465 constant-expression, the contents of the argument list do
11467 saved_ice_p = parser->integral_constant_expression_p;
11468 parser->integral_constant_expression_p = false;
11469 saved_non_ice_p = parser->non_integral_constant_expression_p;
11470 parser->non_integral_constant_expression_p = false;
11471 /* Parse the arguments. */
11477 /* Consume the comma. */
11478 cp_lexer_consume_token (parser->lexer);
11480 /* Parse the template-argument. */
11481 argument = cp_parser_template_argument (parser);
11483 /* If the next token is an ellipsis, we're expanding a template
11485 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11487 if (argument == error_mark_node)
11489 cp_token *token = cp_lexer_peek_token (parser->lexer);
11490 error_at (token->location,
11491 "expected parameter pack before %<...%>");
11493 /* Consume the `...' token. */
11494 cp_lexer_consume_token (parser->lexer);
11496 /* Make the argument into a TYPE_PACK_EXPANSION or
11497 EXPR_PACK_EXPANSION. */
11498 argument = make_pack_expansion (argument);
11501 if (n_args == alloced)
11505 if (arg_ary == fixed_args)
11507 arg_ary = XNEWVEC (tree, alloced);
11508 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11511 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11513 arg_ary[n_args++] = argument;
11515 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11517 vec = make_tree_vec (n_args);
11520 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11522 if (arg_ary != fixed_args)
11524 parser->non_integral_constant_expression_p = saved_non_ice_p;
11525 parser->integral_constant_expression_p = saved_ice_p;
11526 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11527 #ifdef ENABLE_CHECKING
11528 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11533 /* Parse a template-argument.
11536 assignment-expression
11540 The representation is that of an assignment-expression, type-id, or
11541 id-expression -- except that the qualified id-expression is
11542 evaluated, so that the value returned is either a DECL or an
11545 Although the standard says "assignment-expression", it forbids
11546 throw-expressions or assignments in the template argument.
11547 Therefore, we use "conditional-expression" instead. */
11550 cp_parser_template_argument (cp_parser* parser)
11555 bool maybe_type_id = false;
11556 cp_token *token = NULL, *argument_start_token = NULL;
11559 /* There's really no way to know what we're looking at, so we just
11560 try each alternative in order.
11564 In a template-argument, an ambiguity between a type-id and an
11565 expression is resolved to a type-id, regardless of the form of
11566 the corresponding template-parameter.
11568 Therefore, we try a type-id first. */
11569 cp_parser_parse_tentatively (parser);
11570 argument = cp_parser_template_type_arg (parser);
11571 /* If there was no error parsing the type-id but the next token is a
11572 '>>', our behavior depends on which dialect of C++ we're
11573 parsing. In C++98, we probably found a typo for '> >'. But there
11574 are type-id which are also valid expressions. For instance:
11576 struct X { int operator >> (int); };
11577 template <int V> struct Foo {};
11580 Here 'X()' is a valid type-id of a function type, but the user just
11581 wanted to write the expression "X() >> 5". Thus, we remember that we
11582 found a valid type-id, but we still try to parse the argument as an
11583 expression to see what happens.
11585 In C++0x, the '>>' will be considered two separate '>'
11587 if (!cp_parser_error_occurred (parser)
11588 && cxx_dialect == cxx98
11589 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11591 maybe_type_id = true;
11592 cp_parser_abort_tentative_parse (parser);
11596 /* If the next token isn't a `,' or a `>', then this argument wasn't
11597 really finished. This means that the argument is not a valid
11599 if (!cp_parser_next_token_ends_template_argument_p (parser))
11600 cp_parser_error (parser, "expected template-argument");
11601 /* If that worked, we're done. */
11602 if (cp_parser_parse_definitely (parser))
11605 /* We're still not sure what the argument will be. */
11606 cp_parser_parse_tentatively (parser);
11607 /* Try a template. */
11608 argument_start_token = cp_lexer_peek_token (parser->lexer);
11609 argument = cp_parser_id_expression (parser,
11610 /*template_keyword_p=*/false,
11611 /*check_dependency_p=*/true,
11613 /*declarator_p=*/false,
11614 /*optional_p=*/false);
11615 /* If the next token isn't a `,' or a `>', then this argument wasn't
11616 really finished. */
11617 if (!cp_parser_next_token_ends_template_argument_p (parser))
11618 cp_parser_error (parser, "expected template-argument");
11619 if (!cp_parser_error_occurred (parser))
11621 /* Figure out what is being referred to. If the id-expression
11622 was for a class template specialization, then we will have a
11623 TYPE_DECL at this point. There is no need to do name lookup
11624 at this point in that case. */
11625 if (TREE_CODE (argument) != TYPE_DECL)
11626 argument = cp_parser_lookup_name (parser, argument,
11628 /*is_template=*/template_p,
11629 /*is_namespace=*/false,
11630 /*check_dependency=*/true,
11631 /*ambiguous_decls=*/NULL,
11632 argument_start_token->location);
11633 if (TREE_CODE (argument) != TEMPLATE_DECL
11634 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11635 cp_parser_error (parser, "expected template-name");
11637 if (cp_parser_parse_definitely (parser))
11639 /* It must be a non-type argument. There permitted cases are given
11640 in [temp.arg.nontype]:
11642 -- an integral constant-expression of integral or enumeration
11645 -- the name of a non-type template-parameter; or
11647 -- the name of an object or function with external linkage...
11649 -- the address of an object or function with external linkage...
11651 -- a pointer to member... */
11652 /* Look for a non-type template parameter. */
11653 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11655 cp_parser_parse_tentatively (parser);
11656 argument = cp_parser_primary_expression (parser,
11657 /*address_p=*/false,
11659 /*template_arg_p=*/true,
11661 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11662 || !cp_parser_next_token_ends_template_argument_p (parser))
11663 cp_parser_simulate_error (parser);
11664 if (cp_parser_parse_definitely (parser))
11668 /* If the next token is "&", the argument must be the address of an
11669 object or function with external linkage. */
11670 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11672 cp_lexer_consume_token (parser->lexer);
11673 /* See if we might have an id-expression. */
11674 token = cp_lexer_peek_token (parser->lexer);
11675 if (token->type == CPP_NAME
11676 || token->keyword == RID_OPERATOR
11677 || token->type == CPP_SCOPE
11678 || token->type == CPP_TEMPLATE_ID
11679 || token->type == CPP_NESTED_NAME_SPECIFIER)
11681 cp_parser_parse_tentatively (parser);
11682 argument = cp_parser_primary_expression (parser,
11685 /*template_arg_p=*/true,
11687 if (cp_parser_error_occurred (parser)
11688 || !cp_parser_next_token_ends_template_argument_p (parser))
11689 cp_parser_abort_tentative_parse (parser);
11694 if (TREE_CODE (argument) == INDIRECT_REF)
11696 gcc_assert (REFERENCE_REF_P (argument));
11697 argument = TREE_OPERAND (argument, 0);
11700 /* If we're in a template, we represent a qualified-id referring
11701 to a static data member as a SCOPE_REF even if the scope isn't
11702 dependent so that we can check access control later. */
11704 if (TREE_CODE (probe) == SCOPE_REF)
11705 probe = TREE_OPERAND (probe, 1);
11706 if (TREE_CODE (probe) == VAR_DECL)
11708 /* A variable without external linkage might still be a
11709 valid constant-expression, so no error is issued here
11710 if the external-linkage check fails. */
11711 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11712 cp_parser_simulate_error (parser);
11714 else if (is_overloaded_fn (argument))
11715 /* All overloaded functions are allowed; if the external
11716 linkage test does not pass, an error will be issued
11720 && (TREE_CODE (argument) == OFFSET_REF
11721 || TREE_CODE (argument) == SCOPE_REF))
11722 /* A pointer-to-member. */
11724 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11727 cp_parser_simulate_error (parser);
11729 if (cp_parser_parse_definitely (parser))
11732 argument = build_x_unary_op (ADDR_EXPR, argument,
11733 tf_warning_or_error);
11738 /* If the argument started with "&", there are no other valid
11739 alternatives at this point. */
11742 cp_parser_error (parser, "invalid non-type template argument");
11743 return error_mark_node;
11746 /* If the argument wasn't successfully parsed as a type-id followed
11747 by '>>', the argument can only be a constant expression now.
11748 Otherwise, we try parsing the constant-expression tentatively,
11749 because the argument could really be a type-id. */
11751 cp_parser_parse_tentatively (parser);
11752 argument = cp_parser_constant_expression (parser,
11753 /*allow_non_constant_p=*/false,
11754 /*non_constant_p=*/NULL);
11755 argument = fold_non_dependent_expr (argument);
11756 if (!maybe_type_id)
11758 if (!cp_parser_next_token_ends_template_argument_p (parser))
11759 cp_parser_error (parser, "expected template-argument");
11760 if (cp_parser_parse_definitely (parser))
11762 /* We did our best to parse the argument as a non type-id, but that
11763 was the only alternative that matched (albeit with a '>' after
11764 it). We can assume it's just a typo from the user, and a
11765 diagnostic will then be issued. */
11766 return cp_parser_template_type_arg (parser);
11769 /* Parse an explicit-instantiation.
11771 explicit-instantiation:
11772 template declaration
11774 Although the standard says `declaration', what it really means is:
11776 explicit-instantiation:
11777 template decl-specifier-seq [opt] declarator [opt] ;
11779 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11780 supposed to be allowed. A defect report has been filed about this
11785 explicit-instantiation:
11786 storage-class-specifier template
11787 decl-specifier-seq [opt] declarator [opt] ;
11788 function-specifier template
11789 decl-specifier-seq [opt] declarator [opt] ; */
11792 cp_parser_explicit_instantiation (cp_parser* parser)
11794 int declares_class_or_enum;
11795 cp_decl_specifier_seq decl_specifiers;
11796 tree extension_specifier = NULL_TREE;
11798 /* Look for an (optional) storage-class-specifier or
11799 function-specifier. */
11800 if (cp_parser_allow_gnu_extensions_p (parser))
11802 extension_specifier
11803 = cp_parser_storage_class_specifier_opt (parser);
11804 if (!extension_specifier)
11805 extension_specifier
11806 = cp_parser_function_specifier_opt (parser,
11807 /*decl_specs=*/NULL);
11810 /* Look for the `template' keyword. */
11811 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
11812 /* Let the front end know that we are processing an explicit
11814 begin_explicit_instantiation ();
11815 /* [temp.explicit] says that we are supposed to ignore access
11816 control while processing explicit instantiation directives. */
11817 push_deferring_access_checks (dk_no_check);
11818 /* Parse a decl-specifier-seq. */
11819 cp_parser_decl_specifier_seq (parser,
11820 CP_PARSER_FLAGS_OPTIONAL,
11822 &declares_class_or_enum);
11823 /* If there was exactly one decl-specifier, and it declared a class,
11824 and there's no declarator, then we have an explicit type
11826 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11830 type = check_tag_decl (&decl_specifiers);
11831 /* Turn access control back on for names used during
11832 template instantiation. */
11833 pop_deferring_access_checks ();
11835 do_type_instantiation (type, extension_specifier,
11836 /*complain=*/tf_error);
11840 cp_declarator *declarator;
11843 /* Parse the declarator. */
11845 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11846 /*ctor_dtor_or_conv_p=*/NULL,
11847 /*parenthesized_p=*/NULL,
11848 /*member_p=*/false);
11849 if (declares_class_or_enum & 2)
11850 cp_parser_check_for_definition_in_return_type (declarator,
11851 decl_specifiers.type,
11852 decl_specifiers.type_location);
11853 if (declarator != cp_error_declarator)
11855 decl = grokdeclarator (declarator, &decl_specifiers,
11856 NORMAL, 0, &decl_specifiers.attributes);
11857 /* Turn access control back on for names used during
11858 template instantiation. */
11859 pop_deferring_access_checks ();
11860 /* Do the explicit instantiation. */
11861 do_decl_instantiation (decl, extension_specifier);
11865 pop_deferring_access_checks ();
11866 /* Skip the body of the explicit instantiation. */
11867 cp_parser_skip_to_end_of_statement (parser);
11870 /* We're done with the instantiation. */
11871 end_explicit_instantiation ();
11873 cp_parser_consume_semicolon_at_end_of_statement (parser);
11876 /* Parse an explicit-specialization.
11878 explicit-specialization:
11879 template < > declaration
11881 Although the standard says `declaration', what it really means is:
11883 explicit-specialization:
11884 template <> decl-specifier [opt] init-declarator [opt] ;
11885 template <> function-definition
11886 template <> explicit-specialization
11887 template <> template-declaration */
11890 cp_parser_explicit_specialization (cp_parser* parser)
11892 bool need_lang_pop;
11893 cp_token *token = cp_lexer_peek_token (parser->lexer);
11895 /* Look for the `template' keyword. */
11896 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
11897 /* Look for the `<'. */
11898 cp_parser_require (parser, CPP_LESS, RT_LESS);
11899 /* Look for the `>'. */
11900 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11901 /* We have processed another parameter list. */
11902 ++parser->num_template_parameter_lists;
11905 A template ... explicit specialization ... shall not have C
11907 if (current_lang_name == lang_name_c)
11909 error_at (token->location, "template specialization with C linkage");
11910 /* Give it C++ linkage to avoid confusing other parts of the
11912 push_lang_context (lang_name_cplusplus);
11913 need_lang_pop = true;
11916 need_lang_pop = false;
11917 /* Let the front end know that we are beginning a specialization. */
11918 if (!begin_specialization ())
11920 end_specialization ();
11924 /* If the next keyword is `template', we need to figure out whether
11925 or not we're looking a template-declaration. */
11926 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11928 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11929 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11930 cp_parser_template_declaration_after_export (parser,
11931 /*member_p=*/false);
11933 cp_parser_explicit_specialization (parser);
11936 /* Parse the dependent declaration. */
11937 cp_parser_single_declaration (parser,
11939 /*member_p=*/false,
11940 /*explicit_specialization_p=*/true,
11941 /*friend_p=*/NULL);
11942 /* We're done with the specialization. */
11943 end_specialization ();
11944 /* For the erroneous case of a template with C linkage, we pushed an
11945 implicit C++ linkage scope; exit that scope now. */
11947 pop_lang_context ();
11948 /* We're done with this parameter list. */
11949 --parser->num_template_parameter_lists;
11952 /* Parse a type-specifier.
11955 simple-type-specifier
11958 elaborated-type-specifier
11966 Returns a representation of the type-specifier. For a
11967 class-specifier, enum-specifier, or elaborated-type-specifier, a
11968 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11970 The parser flags FLAGS is used to control type-specifier parsing.
11972 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11973 in a decl-specifier-seq.
11975 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11976 class-specifier, enum-specifier, or elaborated-type-specifier, then
11977 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11978 if a type is declared; 2 if it is defined. Otherwise, it is set to
11981 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11982 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11983 is set to FALSE. */
11986 cp_parser_type_specifier (cp_parser* parser,
11987 cp_parser_flags flags,
11988 cp_decl_specifier_seq *decl_specs,
11989 bool is_declaration,
11990 int* declares_class_or_enum,
11991 bool* is_cv_qualifier)
11993 tree type_spec = NULL_TREE;
11996 cp_decl_spec ds = ds_last;
11998 /* Assume this type-specifier does not declare a new type. */
11999 if (declares_class_or_enum)
12000 *declares_class_or_enum = 0;
12001 /* And that it does not specify a cv-qualifier. */
12002 if (is_cv_qualifier)
12003 *is_cv_qualifier = false;
12004 /* Peek at the next token. */
12005 token = cp_lexer_peek_token (parser->lexer);
12007 /* If we're looking at a keyword, we can use that to guide the
12008 production we choose. */
12009 keyword = token->keyword;
12013 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12014 goto elaborated_type_specifier;
12016 /* Look for the enum-specifier. */
12017 type_spec = cp_parser_enum_specifier (parser);
12018 /* If that worked, we're done. */
12021 if (declares_class_or_enum)
12022 *declares_class_or_enum = 2;
12024 cp_parser_set_decl_spec_type (decl_specs,
12027 /*user_defined_p=*/true);
12031 goto elaborated_type_specifier;
12033 /* Any of these indicate either a class-specifier, or an
12034 elaborated-type-specifier. */
12038 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12039 goto elaborated_type_specifier;
12041 /* Parse tentatively so that we can back up if we don't find a
12042 class-specifier. */
12043 cp_parser_parse_tentatively (parser);
12044 /* Look for the class-specifier. */
12045 type_spec = cp_parser_class_specifier (parser);
12046 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12047 /* If that worked, we're done. */
12048 if (cp_parser_parse_definitely (parser))
12050 if (declares_class_or_enum)
12051 *declares_class_or_enum = 2;
12053 cp_parser_set_decl_spec_type (decl_specs,
12056 /*user_defined_p=*/true);
12060 /* Fall through. */
12061 elaborated_type_specifier:
12062 /* We're declaring (not defining) a class or enum. */
12063 if (declares_class_or_enum)
12064 *declares_class_or_enum = 1;
12066 /* Fall through. */
12068 /* Look for an elaborated-type-specifier. */
12070 = (cp_parser_elaborated_type_specifier
12072 decl_specs && decl_specs->specs[(int) ds_friend],
12075 cp_parser_set_decl_spec_type (decl_specs,
12078 /*user_defined_p=*/true);
12083 if (is_cv_qualifier)
12084 *is_cv_qualifier = true;
12089 if (is_cv_qualifier)
12090 *is_cv_qualifier = true;
12095 if (is_cv_qualifier)
12096 *is_cv_qualifier = true;
12100 /* The `__complex__' keyword is a GNU extension. */
12108 /* Handle simple keywords. */
12113 ++decl_specs->specs[(int)ds];
12114 decl_specs->any_specifiers_p = true;
12116 return cp_lexer_consume_token (parser->lexer)->u.value;
12119 /* If we do not already have a type-specifier, assume we are looking
12120 at a simple-type-specifier. */
12121 type_spec = cp_parser_simple_type_specifier (parser,
12125 /* If we didn't find a type-specifier, and a type-specifier was not
12126 optional in this context, issue an error message. */
12127 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12129 cp_parser_error (parser, "expected type specifier");
12130 return error_mark_node;
12136 /* Parse a simple-type-specifier.
12138 simple-type-specifier:
12139 :: [opt] nested-name-specifier [opt] type-name
12140 :: [opt] nested-name-specifier template template-id
12155 simple-type-specifier:
12157 decltype ( expression )
12163 simple-type-specifier:
12164 __typeof__ unary-expression
12165 __typeof__ ( type-id )
12167 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12168 appropriately updated. */
12171 cp_parser_simple_type_specifier (cp_parser* parser,
12172 cp_decl_specifier_seq *decl_specs,
12173 cp_parser_flags flags)
12175 tree type = NULL_TREE;
12178 /* Peek at the next token. */
12179 token = cp_lexer_peek_token (parser->lexer);
12181 /* If we're looking at a keyword, things are easy. */
12182 switch (token->keyword)
12186 decl_specs->explicit_char_p = true;
12187 type = char_type_node;
12190 type = char16_type_node;
12193 type = char32_type_node;
12196 type = wchar_type_node;
12199 type = boolean_type_node;
12203 ++decl_specs->specs[(int) ds_short];
12204 type = short_integer_type_node;
12208 decl_specs->explicit_int_p = true;
12209 type = integer_type_node;
12213 ++decl_specs->specs[(int) ds_long];
12214 type = long_integer_type_node;
12218 ++decl_specs->specs[(int) ds_signed];
12219 type = integer_type_node;
12223 ++decl_specs->specs[(int) ds_unsigned];
12224 type = unsigned_type_node;
12227 type = float_type_node;
12230 type = double_type_node;
12233 type = void_type_node;
12237 maybe_warn_cpp0x (CPP0X_AUTO);
12238 type = make_auto ();
12242 /* Parse the `decltype' type. */
12243 type = cp_parser_decltype (parser);
12246 cp_parser_set_decl_spec_type (decl_specs, type,
12248 /*user_defined_p=*/true);
12253 /* Consume the `typeof' token. */
12254 cp_lexer_consume_token (parser->lexer);
12255 /* Parse the operand to `typeof'. */
12256 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12257 /* If it is not already a TYPE, take its type. */
12258 if (!TYPE_P (type))
12259 type = finish_typeof (type);
12262 cp_parser_set_decl_spec_type (decl_specs, type,
12264 /*user_defined_p=*/true);
12272 /* If the type-specifier was for a built-in type, we're done. */
12275 /* Record the type. */
12277 && (token->keyword != RID_SIGNED
12278 && token->keyword != RID_UNSIGNED
12279 && token->keyword != RID_SHORT
12280 && token->keyword != RID_LONG))
12281 cp_parser_set_decl_spec_type (decl_specs,
12284 /*user_defined=*/false);
12286 decl_specs->any_specifiers_p = true;
12288 /* Consume the token. */
12289 cp_lexer_consume_token (parser->lexer);
12291 /* There is no valid C++ program where a non-template type is
12292 followed by a "<". That usually indicates that the user thought
12293 that the type was a template. */
12294 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12296 return TYPE_NAME (type);
12299 /* The type-specifier must be a user-defined type. */
12300 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12305 /* Don't gobble tokens or issue error messages if this is an
12306 optional type-specifier. */
12307 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12308 cp_parser_parse_tentatively (parser);
12310 /* Look for the optional `::' operator. */
12312 = (cp_parser_global_scope_opt (parser,
12313 /*current_scope_valid_p=*/false)
12315 /* Look for the nested-name specifier. */
12317 = (cp_parser_nested_name_specifier_opt (parser,
12318 /*typename_keyword_p=*/false,
12319 /*check_dependency_p=*/true,
12321 /*is_declaration=*/false)
12323 token = cp_lexer_peek_token (parser->lexer);
12324 /* If we have seen a nested-name-specifier, and the next token
12325 is `template', then we are using the template-id production. */
12327 && cp_parser_optional_template_keyword (parser))
12329 /* Look for the template-id. */
12330 type = cp_parser_template_id (parser,
12331 /*template_keyword_p=*/true,
12332 /*check_dependency_p=*/true,
12333 /*is_declaration=*/false);
12334 /* If the template-id did not name a type, we are out of
12336 if (TREE_CODE (type) != TYPE_DECL)
12338 cp_parser_error (parser, "expected template-id for type");
12342 /* Otherwise, look for a type-name. */
12344 type = cp_parser_type_name (parser);
12345 /* Keep track of all name-lookups performed in class scopes. */
12349 && TREE_CODE (type) == TYPE_DECL
12350 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12351 maybe_note_name_used_in_class (DECL_NAME (type), type);
12352 /* If it didn't work out, we don't have a TYPE. */
12353 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12354 && !cp_parser_parse_definitely (parser))
12356 if (type && decl_specs)
12357 cp_parser_set_decl_spec_type (decl_specs, type,
12359 /*user_defined=*/true);
12362 /* If we didn't get a type-name, issue an error message. */
12363 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12365 cp_parser_error (parser, "expected type-name");
12366 return error_mark_node;
12369 /* There is no valid C++ program where a non-template type is
12370 followed by a "<". That usually indicates that the user thought
12371 that the type was a template. */
12372 if (type && type != error_mark_node)
12374 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12375 If it is, then the '<'...'>' enclose protocol names rather than
12376 template arguments, and so everything is fine. */
12377 if (c_dialect_objc ()
12378 && (objc_is_id (type) || objc_is_class_name (type)))
12380 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12381 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12383 /* Clobber the "unqualified" type previously entered into
12384 DECL_SPECS with the new, improved protocol-qualified version. */
12386 decl_specs->type = qual_type;
12391 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12398 /* Parse a type-name.
12411 Returns a TYPE_DECL for the type. */
12414 cp_parser_type_name (cp_parser* parser)
12418 /* We can't know yet whether it is a class-name or not. */
12419 cp_parser_parse_tentatively (parser);
12420 /* Try a class-name. */
12421 type_decl = cp_parser_class_name (parser,
12422 /*typename_keyword_p=*/false,
12423 /*template_keyword_p=*/false,
12425 /*check_dependency_p=*/true,
12426 /*class_head_p=*/false,
12427 /*is_declaration=*/false);
12428 /* If it's not a class-name, keep looking. */
12429 if (!cp_parser_parse_definitely (parser))
12431 /* It must be a typedef-name or an enum-name. */
12432 return cp_parser_nonclass_name (parser);
12438 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12446 Returns a TYPE_DECL for the type. */
12449 cp_parser_nonclass_name (cp_parser* parser)
12454 cp_token *token = cp_lexer_peek_token (parser->lexer);
12455 identifier = cp_parser_identifier (parser);
12456 if (identifier == error_mark_node)
12457 return error_mark_node;
12459 /* Look up the type-name. */
12460 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12462 if (TREE_CODE (type_decl) != TYPE_DECL
12463 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12465 /* See if this is an Objective-C type. */
12466 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12467 tree type = objc_get_protocol_qualified_type (identifier, protos);
12469 type_decl = TYPE_NAME (type);
12472 /* Issue an error if we did not find a type-name. */
12473 if (TREE_CODE (type_decl) != TYPE_DECL)
12475 if (!cp_parser_simulate_error (parser))
12476 cp_parser_name_lookup_error (parser, identifier, type_decl,
12477 NLE_TYPE, token->location);
12478 return error_mark_node;
12480 /* Remember that the name was used in the definition of the
12481 current class so that we can check later to see if the
12482 meaning would have been different after the class was
12483 entirely defined. */
12484 else if (type_decl != error_mark_node
12486 maybe_note_name_used_in_class (identifier, type_decl);
12491 /* Parse an elaborated-type-specifier. Note that the grammar given
12492 here incorporates the resolution to DR68.
12494 elaborated-type-specifier:
12495 class-key :: [opt] nested-name-specifier [opt] identifier
12496 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12497 enum-key :: [opt] nested-name-specifier [opt] identifier
12498 typename :: [opt] nested-name-specifier identifier
12499 typename :: [opt] nested-name-specifier template [opt]
12504 elaborated-type-specifier:
12505 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12506 class-key attributes :: [opt] nested-name-specifier [opt]
12507 template [opt] template-id
12508 enum attributes :: [opt] nested-name-specifier [opt] identifier
12510 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12511 declared `friend'. If IS_DECLARATION is TRUE, then this
12512 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12513 something is being declared.
12515 Returns the TYPE specified. */
12518 cp_parser_elaborated_type_specifier (cp_parser* parser,
12520 bool is_declaration)
12522 enum tag_types tag_type;
12524 tree type = NULL_TREE;
12525 tree attributes = NULL_TREE;
12527 cp_token *token = NULL;
12529 /* See if we're looking at the `enum' keyword. */
12530 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12532 /* Consume the `enum' token. */
12533 cp_lexer_consume_token (parser->lexer);
12534 /* Remember that it's an enumeration type. */
12535 tag_type = enum_type;
12536 /* Parse the optional `struct' or `class' key (for C++0x scoped
12538 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12539 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12541 if (cxx_dialect == cxx98)
12542 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12544 /* Consume the `struct' or `class'. */
12545 cp_lexer_consume_token (parser->lexer);
12547 /* Parse the attributes. */
12548 attributes = cp_parser_attributes_opt (parser);
12550 /* Or, it might be `typename'. */
12551 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12554 /* Consume the `typename' token. */
12555 cp_lexer_consume_token (parser->lexer);
12556 /* Remember that it's a `typename' type. */
12557 tag_type = typename_type;
12559 /* Otherwise it must be a class-key. */
12562 tag_type = cp_parser_class_key (parser);
12563 if (tag_type == none_type)
12564 return error_mark_node;
12565 /* Parse the attributes. */
12566 attributes = cp_parser_attributes_opt (parser);
12569 /* Look for the `::' operator. */
12570 globalscope = cp_parser_global_scope_opt (parser,
12571 /*current_scope_valid_p=*/false);
12572 /* Look for the nested-name-specifier. */
12573 if (tag_type == typename_type && !globalscope)
12575 if (!cp_parser_nested_name_specifier (parser,
12576 /*typename_keyword_p=*/true,
12577 /*check_dependency_p=*/true,
12580 return error_mark_node;
12583 /* Even though `typename' is not present, the proposed resolution
12584 to Core Issue 180 says that in `class A<T>::B', `B' should be
12585 considered a type-name, even if `A<T>' is dependent. */
12586 cp_parser_nested_name_specifier_opt (parser,
12587 /*typename_keyword_p=*/true,
12588 /*check_dependency_p=*/true,
12591 /* For everything but enumeration types, consider a template-id.
12592 For an enumeration type, consider only a plain identifier. */
12593 if (tag_type != enum_type)
12595 bool template_p = false;
12598 /* Allow the `template' keyword. */
12599 template_p = cp_parser_optional_template_keyword (parser);
12600 /* If we didn't see `template', we don't know if there's a
12601 template-id or not. */
12603 cp_parser_parse_tentatively (parser);
12604 /* Parse the template-id. */
12605 token = cp_lexer_peek_token (parser->lexer);
12606 decl = cp_parser_template_id (parser, template_p,
12607 /*check_dependency_p=*/true,
12609 /* If we didn't find a template-id, look for an ordinary
12611 if (!template_p && !cp_parser_parse_definitely (parser))
12613 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12614 in effect, then we must assume that, upon instantiation, the
12615 template will correspond to a class. */
12616 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12617 && tag_type == typename_type)
12618 type = make_typename_type (parser->scope, decl,
12620 /*complain=*/tf_error);
12621 /* If the `typename' keyword is in effect and DECL is not a type
12622 decl. Then type is non existant. */
12623 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12626 type = TREE_TYPE (decl);
12631 token = cp_lexer_peek_token (parser->lexer);
12632 identifier = cp_parser_identifier (parser);
12634 if (identifier == error_mark_node)
12636 parser->scope = NULL_TREE;
12637 return error_mark_node;
12640 /* For a `typename', we needn't call xref_tag. */
12641 if (tag_type == typename_type
12642 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12643 return cp_parser_make_typename_type (parser, parser->scope,
12646 /* Look up a qualified name in the usual way. */
12650 tree ambiguous_decls;
12652 decl = cp_parser_lookup_name (parser, identifier,
12654 /*is_template=*/false,
12655 /*is_namespace=*/false,
12656 /*check_dependency=*/true,
12660 /* If the lookup was ambiguous, an error will already have been
12662 if (ambiguous_decls)
12663 return error_mark_node;
12665 /* If we are parsing friend declaration, DECL may be a
12666 TEMPLATE_DECL tree node here. However, we need to check
12667 whether this TEMPLATE_DECL results in valid code. Consider
12668 the following example:
12671 template <class T> class C {};
12674 template <class T> friend class N::C; // #1, valid code
12676 template <class T> class Y {
12677 friend class N::C; // #2, invalid code
12680 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12681 name lookup of `N::C'. We see that friend declaration must
12682 be template for the code to be valid. Note that
12683 processing_template_decl does not work here since it is
12684 always 1 for the above two cases. */
12686 decl = (cp_parser_maybe_treat_template_as_class
12687 (decl, /*tag_name_p=*/is_friend
12688 && parser->num_template_parameter_lists));
12690 if (TREE_CODE (decl) != TYPE_DECL)
12692 cp_parser_diagnose_invalid_type_name (parser,
12696 return error_mark_node;
12699 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12701 bool allow_template = (parser->num_template_parameter_lists
12702 || DECL_SELF_REFERENCE_P (decl));
12703 type = check_elaborated_type_specifier (tag_type, decl,
12706 if (type == error_mark_node)
12707 return error_mark_node;
12710 /* Forward declarations of nested types, such as
12715 are invalid unless all components preceding the final '::'
12716 are complete. If all enclosing types are complete, these
12717 declarations become merely pointless.
12719 Invalid forward declarations of nested types are errors
12720 caught elsewhere in parsing. Those that are pointless arrive
12723 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12724 && !is_friend && !processing_explicit_instantiation)
12725 warning (0, "declaration %qD does not declare anything", decl);
12727 type = TREE_TYPE (decl);
12731 /* An elaborated-type-specifier sometimes introduces a new type and
12732 sometimes names an existing type. Normally, the rule is that it
12733 introduces a new type only if there is not an existing type of
12734 the same name already in scope. For example, given:
12737 void f() { struct S s; }
12739 the `struct S' in the body of `f' is the same `struct S' as in
12740 the global scope; the existing definition is used. However, if
12741 there were no global declaration, this would introduce a new
12742 local class named `S'.
12744 An exception to this rule applies to the following code:
12746 namespace N { struct S; }
12748 Here, the elaborated-type-specifier names a new type
12749 unconditionally; even if there is already an `S' in the
12750 containing scope this declaration names a new type.
12751 This exception only applies if the elaborated-type-specifier
12752 forms the complete declaration:
12756 A declaration consisting solely of `class-key identifier ;' is
12757 either a redeclaration of the name in the current scope or a
12758 forward declaration of the identifier as a class name. It
12759 introduces the name into the current scope.
12761 We are in this situation precisely when the next token is a `;'.
12763 An exception to the exception is that a `friend' declaration does
12764 *not* name a new type; i.e., given:
12766 struct S { friend struct T; };
12768 `T' is not a new type in the scope of `S'.
12770 Also, `new struct S' or `sizeof (struct S)' never results in the
12771 definition of a new type; a new type can only be declared in a
12772 declaration context. */
12778 /* Friends have special name lookup rules. */
12779 ts = ts_within_enclosing_non_class;
12780 else if (is_declaration
12781 && cp_lexer_next_token_is (parser->lexer,
12783 /* This is a `class-key identifier ;' */
12789 (parser->num_template_parameter_lists
12790 && (cp_parser_next_token_starts_class_definition_p (parser)
12791 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12792 /* An unqualified name was used to reference this type, so
12793 there were no qualifying templates. */
12794 if (!cp_parser_check_template_parameters (parser,
12795 /*num_templates=*/0,
12797 /*declarator=*/NULL))
12798 return error_mark_node;
12799 type = xref_tag (tag_type, identifier, ts, template_p);
12803 if (type == error_mark_node)
12804 return error_mark_node;
12806 /* Allow attributes on forward declarations of classes. */
12809 if (TREE_CODE (type) == TYPENAME_TYPE)
12810 warning (OPT_Wattributes,
12811 "attributes ignored on uninstantiated type");
12812 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12813 && ! processing_explicit_instantiation)
12814 warning (OPT_Wattributes,
12815 "attributes ignored on template instantiation");
12816 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12817 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12819 warning (OPT_Wattributes,
12820 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12823 if (tag_type != enum_type)
12824 cp_parser_check_class_key (tag_type, type);
12826 /* A "<" cannot follow an elaborated type specifier. If that
12827 happens, the user was probably trying to form a template-id. */
12828 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12833 /* Parse an enum-specifier.
12836 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12841 enum struct [C++0x]
12844 : type-specifier-seq
12847 enum-key attributes[opt] identifier [opt] enum-base [opt]
12848 { enumerator-list [opt] }attributes[opt]
12850 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12851 if the token stream isn't an enum-specifier after all. */
12854 cp_parser_enum_specifier (cp_parser* parser)
12859 bool scoped_enum_p = false;
12860 bool has_underlying_type = false;
12861 tree underlying_type = NULL_TREE;
12863 /* Parse tentatively so that we can back up if we don't find a
12865 cp_parser_parse_tentatively (parser);
12867 /* Caller guarantees that the current token is 'enum', an identifier
12868 possibly follows, and the token after that is an opening brace.
12869 If we don't have an identifier, fabricate an anonymous name for
12870 the enumeration being defined. */
12871 cp_lexer_consume_token (parser->lexer);
12873 /* Parse the "class" or "struct", which indicates a scoped
12874 enumeration type in C++0x. */
12875 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12876 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12878 if (cxx_dialect == cxx98)
12879 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12881 /* Consume the `struct' or `class' token. */
12882 cp_lexer_consume_token (parser->lexer);
12884 scoped_enum_p = true;
12887 attributes = cp_parser_attributes_opt (parser);
12889 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12890 identifier = cp_parser_identifier (parser);
12892 identifier = make_anon_name ();
12894 /* Check for the `:' that denotes a specified underlying type in C++0x.
12895 Note that a ':' could also indicate a bitfield width, however. */
12896 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12898 cp_decl_specifier_seq type_specifiers;
12900 /* Consume the `:'. */
12901 cp_lexer_consume_token (parser->lexer);
12903 /* Parse the type-specifier-seq. */
12904 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12905 /*is_trailing_return=*/false,
12908 /* At this point this is surely not elaborated type specifier. */
12909 if (!cp_parser_parse_definitely (parser))
12912 if (cxx_dialect == cxx98)
12913 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12915 has_underlying_type = true;
12917 /* If that didn't work, stop. */
12918 if (type_specifiers.type != error_mark_node)
12920 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12921 /*initialized=*/0, NULL);
12922 if (underlying_type == error_mark_node)
12923 underlying_type = NULL_TREE;
12927 /* Look for the `{' but don't consume it yet. */
12928 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12930 cp_parser_error (parser, "expected %<{%>");
12931 if (has_underlying_type)
12935 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12938 /* Issue an error message if type-definitions are forbidden here. */
12939 if (!cp_parser_check_type_definition (parser))
12940 type = error_mark_node;
12942 /* Create the new type. We do this before consuming the opening
12943 brace so the enum will be recorded as being on the line of its
12944 tag (or the 'enum' keyword, if there is no tag). */
12945 type = start_enum (identifier, underlying_type, scoped_enum_p);
12947 /* Consume the opening brace. */
12948 cp_lexer_consume_token (parser->lexer);
12950 if (type == error_mark_node)
12952 cp_parser_skip_to_end_of_block_or_statement (parser);
12953 return error_mark_node;
12956 /* If the next token is not '}', then there are some enumerators. */
12957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12958 cp_parser_enumerator_list (parser, type);
12960 /* Consume the final '}'. */
12961 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12963 /* Look for trailing attributes to apply to this enumeration, and
12964 apply them if appropriate. */
12965 if (cp_parser_allow_gnu_extensions_p (parser))
12967 tree trailing_attr = cp_parser_attributes_opt (parser);
12968 trailing_attr = chainon (trailing_attr, attributes);
12969 cplus_decl_attributes (&type,
12971 (int) ATTR_FLAG_TYPE_IN_PLACE);
12974 /* Finish up the enumeration. */
12975 finish_enum (type);
12980 /* Parse an enumerator-list. The enumerators all have the indicated
12984 enumerator-definition
12985 enumerator-list , enumerator-definition */
12988 cp_parser_enumerator_list (cp_parser* parser, tree type)
12992 /* Parse an enumerator-definition. */
12993 cp_parser_enumerator_definition (parser, type);
12995 /* If the next token is not a ',', we've reached the end of
12997 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12999 /* Otherwise, consume the `,' and keep going. */
13000 cp_lexer_consume_token (parser->lexer);
13001 /* If the next token is a `}', there is a trailing comma. */
13002 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13004 if (!in_system_header)
13005 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13011 /* Parse an enumerator-definition. The enumerator has the indicated
13014 enumerator-definition:
13016 enumerator = constant-expression
13022 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13027 /* Look for the identifier. */
13028 identifier = cp_parser_identifier (parser);
13029 if (identifier == error_mark_node)
13032 /* If the next token is an '=', then there is an explicit value. */
13033 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13035 /* Consume the `=' token. */
13036 cp_lexer_consume_token (parser->lexer);
13037 /* Parse the value. */
13038 value = cp_parser_constant_expression (parser,
13039 /*allow_non_constant_p=*/false,
13045 /* If we are processing a template, make sure the initializer of the
13046 enumerator doesn't contain any bare template parameter pack. */
13047 if (check_for_bare_parameter_packs (value))
13048 value = error_mark_node;
13050 /* Create the enumerator. */
13051 build_enumerator (identifier, value, type);
13054 /* Parse a namespace-name.
13057 original-namespace-name
13060 Returns the NAMESPACE_DECL for the namespace. */
13063 cp_parser_namespace_name (cp_parser* parser)
13066 tree namespace_decl;
13068 cp_token *token = cp_lexer_peek_token (parser->lexer);
13070 /* Get the name of the namespace. */
13071 identifier = cp_parser_identifier (parser);
13072 if (identifier == error_mark_node)
13073 return error_mark_node;
13075 /* Look up the identifier in the currently active scope. Look only
13076 for namespaces, due to:
13078 [basic.lookup.udir]
13080 When looking up a namespace-name in a using-directive or alias
13081 definition, only namespace names are considered.
13085 [basic.lookup.qual]
13087 During the lookup of a name preceding the :: scope resolution
13088 operator, object, function, and enumerator names are ignored.
13090 (Note that cp_parser_qualifying_entity only calls this
13091 function if the token after the name is the scope resolution
13093 namespace_decl = cp_parser_lookup_name (parser, identifier,
13095 /*is_template=*/false,
13096 /*is_namespace=*/true,
13097 /*check_dependency=*/true,
13098 /*ambiguous_decls=*/NULL,
13100 /* If it's not a namespace, issue an error. */
13101 if (namespace_decl == error_mark_node
13102 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13104 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13105 error_at (token->location, "%qD is not a namespace-name", identifier);
13106 cp_parser_error (parser, "expected namespace-name");
13107 namespace_decl = error_mark_node;
13110 return namespace_decl;
13113 /* Parse a namespace-definition.
13115 namespace-definition:
13116 named-namespace-definition
13117 unnamed-namespace-definition
13119 named-namespace-definition:
13120 original-namespace-definition
13121 extension-namespace-definition
13123 original-namespace-definition:
13124 namespace identifier { namespace-body }
13126 extension-namespace-definition:
13127 namespace original-namespace-name { namespace-body }
13129 unnamed-namespace-definition:
13130 namespace { namespace-body } */
13133 cp_parser_namespace_definition (cp_parser* parser)
13135 tree identifier, attribs;
13136 bool has_visibility;
13139 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13142 cp_lexer_consume_token (parser->lexer);
13147 /* Look for the `namespace' keyword. */
13148 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13150 /* Get the name of the namespace. We do not attempt to distinguish
13151 between an original-namespace-definition and an
13152 extension-namespace-definition at this point. The semantic
13153 analysis routines are responsible for that. */
13154 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13155 identifier = cp_parser_identifier (parser);
13157 identifier = NULL_TREE;
13159 /* Parse any specified attributes. */
13160 attribs = cp_parser_attributes_opt (parser);
13162 /* Look for the `{' to start the namespace. */
13163 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13164 /* Start the namespace. */
13165 push_namespace (identifier);
13167 /* "inline namespace" is equivalent to a stub namespace definition
13168 followed by a strong using directive. */
13171 tree name_space = current_namespace;
13172 /* Set up namespace association. */
13173 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13174 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13175 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13176 /* Import the contents of the inline namespace. */
13178 do_using_directive (name_space);
13179 push_namespace (identifier);
13182 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13184 /* Parse the body of the namespace. */
13185 cp_parser_namespace_body (parser);
13187 #ifdef HANDLE_PRAGMA_VISIBILITY
13188 if (has_visibility)
13189 pop_visibility (1);
13192 /* Finish the namespace. */
13194 /* Look for the final `}'. */
13195 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13198 /* Parse a namespace-body.
13201 declaration-seq [opt] */
13204 cp_parser_namespace_body (cp_parser* parser)
13206 cp_parser_declaration_seq_opt (parser);
13209 /* Parse a namespace-alias-definition.
13211 namespace-alias-definition:
13212 namespace identifier = qualified-namespace-specifier ; */
13215 cp_parser_namespace_alias_definition (cp_parser* parser)
13218 tree namespace_specifier;
13220 cp_token *token = cp_lexer_peek_token (parser->lexer);
13222 /* Look for the `namespace' keyword. */
13223 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13224 /* Look for the identifier. */
13225 identifier = cp_parser_identifier (parser);
13226 if (identifier == error_mark_node)
13228 /* Look for the `=' token. */
13229 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13230 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13232 error_at (token->location, "%<namespace%> definition is not allowed here");
13233 /* Skip the definition. */
13234 cp_lexer_consume_token (parser->lexer);
13235 if (cp_parser_skip_to_closing_brace (parser))
13236 cp_lexer_consume_token (parser->lexer);
13239 cp_parser_require (parser, CPP_EQ, RT_EQ);
13240 /* Look for the qualified-namespace-specifier. */
13241 namespace_specifier
13242 = cp_parser_qualified_namespace_specifier (parser);
13243 /* Look for the `;' token. */
13244 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13246 /* Register the alias in the symbol table. */
13247 do_namespace_alias (identifier, namespace_specifier);
13250 /* Parse a qualified-namespace-specifier.
13252 qualified-namespace-specifier:
13253 :: [opt] nested-name-specifier [opt] namespace-name
13255 Returns a NAMESPACE_DECL corresponding to the specified
13259 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13261 /* Look for the optional `::'. */
13262 cp_parser_global_scope_opt (parser,
13263 /*current_scope_valid_p=*/false);
13265 /* Look for the optional nested-name-specifier. */
13266 cp_parser_nested_name_specifier_opt (parser,
13267 /*typename_keyword_p=*/false,
13268 /*check_dependency_p=*/true,
13270 /*is_declaration=*/true);
13272 return cp_parser_namespace_name (parser);
13275 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13276 access declaration.
13279 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13280 using :: unqualified-id ;
13282 access-declaration:
13288 cp_parser_using_declaration (cp_parser* parser,
13289 bool access_declaration_p)
13292 bool typename_p = false;
13293 bool global_scope_p;
13298 if (access_declaration_p)
13299 cp_parser_parse_tentatively (parser);
13302 /* Look for the `using' keyword. */
13303 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13305 /* Peek at the next token. */
13306 token = cp_lexer_peek_token (parser->lexer);
13307 /* See if it's `typename'. */
13308 if (token->keyword == RID_TYPENAME)
13310 /* Remember that we've seen it. */
13312 /* Consume the `typename' token. */
13313 cp_lexer_consume_token (parser->lexer);
13317 /* Look for the optional global scope qualification. */
13319 = (cp_parser_global_scope_opt (parser,
13320 /*current_scope_valid_p=*/false)
13323 /* If we saw `typename', or didn't see `::', then there must be a
13324 nested-name-specifier present. */
13325 if (typename_p || !global_scope_p)
13326 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13327 /*check_dependency_p=*/true,
13329 /*is_declaration=*/true);
13330 /* Otherwise, we could be in either of the two productions. In that
13331 case, treat the nested-name-specifier as optional. */
13333 qscope = cp_parser_nested_name_specifier_opt (parser,
13334 /*typename_keyword_p=*/false,
13335 /*check_dependency_p=*/true,
13337 /*is_declaration=*/true);
13339 qscope = global_namespace;
13341 if (access_declaration_p && cp_parser_error_occurred (parser))
13342 /* Something has already gone wrong; there's no need to parse
13343 further. Since an error has occurred, the return value of
13344 cp_parser_parse_definitely will be false, as required. */
13345 return cp_parser_parse_definitely (parser);
13347 token = cp_lexer_peek_token (parser->lexer);
13348 /* Parse the unqualified-id. */
13349 identifier = cp_parser_unqualified_id (parser,
13350 /*template_keyword_p=*/false,
13351 /*check_dependency_p=*/true,
13352 /*declarator_p=*/true,
13353 /*optional_p=*/false);
13355 if (access_declaration_p)
13357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13358 cp_parser_simulate_error (parser);
13359 if (!cp_parser_parse_definitely (parser))
13363 /* The function we call to handle a using-declaration is different
13364 depending on what scope we are in. */
13365 if (qscope == error_mark_node || identifier == error_mark_node)
13367 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13368 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13369 /* [namespace.udecl]
13371 A using declaration shall not name a template-id. */
13372 error_at (token->location,
13373 "a template-id may not appear in a using-declaration");
13376 if (at_class_scope_p ())
13378 /* Create the USING_DECL. */
13379 decl = do_class_using_decl (parser->scope, identifier);
13381 if (check_for_bare_parameter_packs (decl))
13384 /* Add it to the list of members in this class. */
13385 finish_member_declaration (decl);
13389 decl = cp_parser_lookup_name_simple (parser,
13392 if (decl == error_mark_node)
13393 cp_parser_name_lookup_error (parser, identifier,
13396 else if (check_for_bare_parameter_packs (decl))
13398 else if (!at_namespace_scope_p ())
13399 do_local_using_decl (decl, qscope, identifier);
13401 do_toplevel_using_decl (decl, qscope, identifier);
13405 /* Look for the final `;'. */
13406 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13411 /* Parse a using-directive.
13414 using namespace :: [opt] nested-name-specifier [opt]
13415 namespace-name ; */
13418 cp_parser_using_directive (cp_parser* parser)
13420 tree namespace_decl;
13423 /* Look for the `using' keyword. */
13424 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13425 /* And the `namespace' keyword. */
13426 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13427 /* Look for the optional `::' operator. */
13428 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13429 /* And the optional nested-name-specifier. */
13430 cp_parser_nested_name_specifier_opt (parser,
13431 /*typename_keyword_p=*/false,
13432 /*check_dependency_p=*/true,
13434 /*is_declaration=*/true);
13435 /* Get the namespace being used. */
13436 namespace_decl = cp_parser_namespace_name (parser);
13437 /* And any specified attributes. */
13438 attribs = cp_parser_attributes_opt (parser);
13439 /* Update the symbol table. */
13440 parse_using_directive (namespace_decl, attribs);
13441 /* Look for the final `;'. */
13442 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13445 /* Parse an asm-definition.
13448 asm ( string-literal ) ;
13453 asm volatile [opt] ( string-literal ) ;
13454 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13455 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13456 : asm-operand-list [opt] ) ;
13457 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13458 : asm-operand-list [opt]
13459 : asm-clobber-list [opt] ) ;
13460 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13461 : asm-clobber-list [opt]
13462 : asm-goto-list ) ; */
13465 cp_parser_asm_definition (cp_parser* parser)
13468 tree outputs = NULL_TREE;
13469 tree inputs = NULL_TREE;
13470 tree clobbers = NULL_TREE;
13471 tree labels = NULL_TREE;
13473 bool volatile_p = false;
13474 bool extended_p = false;
13475 bool invalid_inputs_p = false;
13476 bool invalid_outputs_p = false;
13477 bool goto_p = false;
13478 required_token missing = 0;
13480 /* Look for the `asm' keyword. */
13481 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
13482 /* See if the next token is `volatile'. */
13483 if (cp_parser_allow_gnu_extensions_p (parser)
13484 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13486 /* Remember that we saw the `volatile' keyword. */
13488 /* Consume the token. */
13489 cp_lexer_consume_token (parser->lexer);
13491 if (cp_parser_allow_gnu_extensions_p (parser)
13492 && parser->in_function_body
13493 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13495 /* Remember that we saw the `goto' keyword. */
13497 /* Consume the token. */
13498 cp_lexer_consume_token (parser->lexer);
13500 /* Look for the opening `('. */
13501 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13503 /* Look for the string. */
13504 string = cp_parser_string_literal (parser, false, false);
13505 if (string == error_mark_node)
13507 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13508 /*consume_paren=*/true);
13512 /* If we're allowing GNU extensions, check for the extended assembly
13513 syntax. Unfortunately, the `:' tokens need not be separated by
13514 a space in C, and so, for compatibility, we tolerate that here
13515 too. Doing that means that we have to treat the `::' operator as
13517 if (cp_parser_allow_gnu_extensions_p (parser)
13518 && parser->in_function_body
13519 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13520 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13522 bool inputs_p = false;
13523 bool clobbers_p = false;
13524 bool labels_p = false;
13526 /* The extended syntax was used. */
13529 /* Look for outputs. */
13530 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13532 /* Consume the `:'. */
13533 cp_lexer_consume_token (parser->lexer);
13534 /* Parse the output-operands. */
13535 if (cp_lexer_next_token_is_not (parser->lexer,
13537 && cp_lexer_next_token_is_not (parser->lexer,
13539 && cp_lexer_next_token_is_not (parser->lexer,
13542 outputs = cp_parser_asm_operand_list (parser);
13544 if (outputs == error_mark_node)
13545 invalid_outputs_p = true;
13547 /* If the next token is `::', there are no outputs, and the
13548 next token is the beginning of the inputs. */
13549 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13550 /* The inputs are coming next. */
13553 /* Look for inputs. */
13555 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13557 /* Consume the `:' or `::'. */
13558 cp_lexer_consume_token (parser->lexer);
13559 /* Parse the output-operands. */
13560 if (cp_lexer_next_token_is_not (parser->lexer,
13562 && cp_lexer_next_token_is_not (parser->lexer,
13564 && cp_lexer_next_token_is_not (parser->lexer,
13566 inputs = cp_parser_asm_operand_list (parser);
13568 if (inputs == error_mark_node)
13569 invalid_inputs_p = true;
13571 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13572 /* The clobbers are coming next. */
13575 /* Look for clobbers. */
13577 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13580 /* Consume the `:' or `::'. */
13581 cp_lexer_consume_token (parser->lexer);
13582 /* Parse the clobbers. */
13583 if (cp_lexer_next_token_is_not (parser->lexer,
13585 && cp_lexer_next_token_is_not (parser->lexer,
13587 clobbers = cp_parser_asm_clobber_list (parser);
13590 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13591 /* The labels are coming next. */
13594 /* Look for labels. */
13596 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13599 /* Consume the `:' or `::'. */
13600 cp_lexer_consume_token (parser->lexer);
13601 /* Parse the labels. */
13602 labels = cp_parser_asm_label_list (parser);
13605 if (goto_p && !labels_p)
13606 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
13609 missing = RT_COLON_SCOPE;
13611 /* Look for the closing `)'. */
13612 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13613 missing ? missing : RT_CLOSE_PAREN))
13614 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13615 /*consume_paren=*/true);
13616 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13618 if (!invalid_inputs_p && !invalid_outputs_p)
13620 /* Create the ASM_EXPR. */
13621 if (parser->in_function_body)
13623 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13624 inputs, clobbers, labels);
13625 /* If the extended syntax was not used, mark the ASM_EXPR. */
13628 tree temp = asm_stmt;
13629 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13630 temp = TREE_OPERAND (temp, 0);
13632 ASM_INPUT_P (temp) = 1;
13636 cgraph_add_asm_node (string);
13640 /* Declarators [gram.dcl.decl] */
13642 /* Parse an init-declarator.
13645 declarator initializer [opt]
13650 declarator asm-specification [opt] attributes [opt] initializer [opt]
13652 function-definition:
13653 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13655 decl-specifier-seq [opt] declarator function-try-block
13659 function-definition:
13660 __extension__ function-definition
13662 The DECL_SPECIFIERS apply to this declarator. Returns a
13663 representation of the entity declared. If MEMBER_P is TRUE, then
13664 this declarator appears in a class scope. The new DECL created by
13665 this declarator is returned.
13667 The CHECKS are access checks that should be performed once we know
13668 what entity is being declared (and, therefore, what classes have
13671 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13672 for a function-definition here as well. If the declarator is a
13673 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13674 be TRUE upon return. By that point, the function-definition will
13675 have been completely parsed.
13677 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13681 cp_parser_init_declarator (cp_parser* parser,
13682 cp_decl_specifier_seq *decl_specifiers,
13683 VEC (deferred_access_check,gc)* checks,
13684 bool function_definition_allowed_p,
13686 int declares_class_or_enum,
13687 bool* function_definition_p)
13689 cp_token *token = NULL, *asm_spec_start_token = NULL,
13690 *attributes_start_token = NULL;
13691 cp_declarator *declarator;
13692 tree prefix_attributes;
13694 tree asm_specification;
13696 tree decl = NULL_TREE;
13698 int is_initialized;
13699 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13700 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13702 enum cpp_ttype initialization_kind;
13703 bool is_direct_init = false;
13704 bool is_non_constant_init;
13705 int ctor_dtor_or_conv_p;
13707 tree pushed_scope = NULL;
13709 /* Gather the attributes that were provided with the
13710 decl-specifiers. */
13711 prefix_attributes = decl_specifiers->attributes;
13713 /* Assume that this is not the declarator for a function
13715 if (function_definition_p)
13716 *function_definition_p = false;
13718 /* Defer access checks while parsing the declarator; we cannot know
13719 what names are accessible until we know what is being
13721 resume_deferring_access_checks ();
13723 /* Parse the declarator. */
13724 token = cp_lexer_peek_token (parser->lexer);
13726 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13727 &ctor_dtor_or_conv_p,
13728 /*parenthesized_p=*/NULL,
13729 /*member_p=*/false);
13730 /* Gather up the deferred checks. */
13731 stop_deferring_access_checks ();
13733 /* If the DECLARATOR was erroneous, there's no need to go
13735 if (declarator == cp_error_declarator)
13736 return error_mark_node;
13738 /* Check that the number of template-parameter-lists is OK. */
13739 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13741 return error_mark_node;
13743 if (declares_class_or_enum & 2)
13744 cp_parser_check_for_definition_in_return_type (declarator,
13745 decl_specifiers->type,
13746 decl_specifiers->type_location);
13748 /* Figure out what scope the entity declared by the DECLARATOR is
13749 located in. `grokdeclarator' sometimes changes the scope, so
13750 we compute it now. */
13751 scope = get_scope_of_declarator (declarator);
13753 /* Perform any lookups in the declared type which were thought to be
13754 dependent, but are not in the scope of the declarator. */
13755 decl_specifiers->type
13756 = maybe_update_decl_type (decl_specifiers->type, scope);
13758 /* If we're allowing GNU extensions, look for an asm-specification
13760 if (cp_parser_allow_gnu_extensions_p (parser))
13762 /* Look for an asm-specification. */
13763 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13764 asm_specification = cp_parser_asm_specification_opt (parser);
13765 /* And attributes. */
13766 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13767 attributes = cp_parser_attributes_opt (parser);
13771 asm_specification = NULL_TREE;
13772 attributes = NULL_TREE;
13775 /* Peek at the next token. */
13776 token = cp_lexer_peek_token (parser->lexer);
13777 /* Check to see if the token indicates the start of a
13778 function-definition. */
13779 if (function_declarator_p (declarator)
13780 && cp_parser_token_starts_function_definition_p (token))
13782 if (!function_definition_allowed_p)
13784 /* If a function-definition should not appear here, issue an
13786 cp_parser_error (parser,
13787 "a function-definition is not allowed here");
13788 return error_mark_node;
13792 location_t func_brace_location
13793 = cp_lexer_peek_token (parser->lexer)->location;
13795 /* Neither attributes nor an asm-specification are allowed
13796 on a function-definition. */
13797 if (asm_specification)
13798 error_at (asm_spec_start_token->location,
13799 "an asm-specification is not allowed "
13800 "on a function-definition");
13802 error_at (attributes_start_token->location,
13803 "attributes are not allowed on a function-definition");
13804 /* This is a function-definition. */
13805 *function_definition_p = true;
13807 /* Parse the function definition. */
13809 decl = cp_parser_save_member_function_body (parser,
13812 prefix_attributes);
13815 = (cp_parser_function_definition_from_specifiers_and_declarator
13816 (parser, decl_specifiers, prefix_attributes, declarator));
13818 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13820 /* This is where the prologue starts... */
13821 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13822 = func_brace_location;
13831 Only in function declarations for constructors, destructors, and
13832 type conversions can the decl-specifier-seq be omitted.
13834 We explicitly postpone this check past the point where we handle
13835 function-definitions because we tolerate function-definitions
13836 that are missing their return types in some modes. */
13837 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13839 cp_parser_error (parser,
13840 "expected constructor, destructor, or type conversion");
13841 return error_mark_node;
13844 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13845 if (token->type == CPP_EQ
13846 || token->type == CPP_OPEN_PAREN
13847 || token->type == CPP_OPEN_BRACE)
13849 is_initialized = SD_INITIALIZED;
13850 initialization_kind = token->type;
13852 if (token->type == CPP_EQ
13853 && function_declarator_p (declarator))
13855 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13856 if (t2->keyword == RID_DEFAULT)
13857 is_initialized = SD_DEFAULTED;
13858 else if (t2->keyword == RID_DELETE)
13859 is_initialized = SD_DELETED;
13864 /* If the init-declarator isn't initialized and isn't followed by a
13865 `,' or `;', it's not a valid init-declarator. */
13866 if (token->type != CPP_COMMA
13867 && token->type != CPP_SEMICOLON)
13869 cp_parser_error (parser, "expected initializer");
13870 return error_mark_node;
13872 is_initialized = SD_UNINITIALIZED;
13873 initialization_kind = CPP_EOF;
13876 /* Because start_decl has side-effects, we should only call it if we
13877 know we're going ahead. By this point, we know that we cannot
13878 possibly be looking at any other construct. */
13879 cp_parser_commit_to_tentative_parse (parser);
13881 /* If the decl specifiers were bad, issue an error now that we're
13882 sure this was intended to be a declarator. Then continue
13883 declaring the variable(s), as int, to try to cut down on further
13885 if (decl_specifiers->any_specifiers_p
13886 && decl_specifiers->type == error_mark_node)
13888 cp_parser_error (parser, "invalid type in declaration");
13889 decl_specifiers->type = integer_type_node;
13892 /* Check to see whether or not this declaration is a friend. */
13893 friend_p = cp_parser_friend_p (decl_specifiers);
13895 /* Enter the newly declared entry in the symbol table. If we're
13896 processing a declaration in a class-specifier, we wait until
13897 after processing the initializer. */
13900 if (parser->in_unbraced_linkage_specification_p)
13901 decl_specifiers->storage_class = sc_extern;
13902 decl = start_decl (declarator, decl_specifiers,
13903 is_initialized, attributes, prefix_attributes,
13907 /* Enter the SCOPE. That way unqualified names appearing in the
13908 initializer will be looked up in SCOPE. */
13909 pushed_scope = push_scope (scope);
13911 /* Perform deferred access control checks, now that we know in which
13912 SCOPE the declared entity resides. */
13913 if (!member_p && decl)
13915 tree saved_current_function_decl = NULL_TREE;
13917 /* If the entity being declared is a function, pretend that we
13918 are in its scope. If it is a `friend', it may have access to
13919 things that would not otherwise be accessible. */
13920 if (TREE_CODE (decl) == FUNCTION_DECL)
13922 saved_current_function_decl = current_function_decl;
13923 current_function_decl = decl;
13926 /* Perform access checks for template parameters. */
13927 cp_parser_perform_template_parameter_access_checks (checks);
13929 /* Perform the access control checks for the declarator and the
13930 decl-specifiers. */
13931 perform_deferred_access_checks ();
13933 /* Restore the saved value. */
13934 if (TREE_CODE (decl) == FUNCTION_DECL)
13935 current_function_decl = saved_current_function_decl;
13938 /* Parse the initializer. */
13939 initializer = NULL_TREE;
13940 is_direct_init = false;
13941 is_non_constant_init = true;
13942 if (is_initialized)
13944 if (function_declarator_p (declarator))
13946 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13947 if (initialization_kind == CPP_EQ)
13948 initializer = cp_parser_pure_specifier (parser);
13951 /* If the declaration was erroneous, we don't really
13952 know what the user intended, so just silently
13953 consume the initializer. */
13954 if (decl != error_mark_node)
13955 error_at (initializer_start_token->location,
13956 "initializer provided for function");
13957 cp_parser_skip_to_closing_parenthesis (parser,
13958 /*recovering=*/true,
13959 /*or_comma=*/false,
13960 /*consume_paren=*/true);
13965 /* We want to record the extra mangling scope for in-class
13966 initializers of class members and initializers of static data
13967 member templates. The former is a C++0x feature which isn't
13968 implemented yet, and I expect it will involve deferring
13969 parsing of the initializer until end of class as with default
13970 arguments. So right here we only handle the latter. */
13971 if (!member_p && processing_template_decl)
13972 start_lambda_scope (decl);
13973 initializer = cp_parser_initializer (parser,
13975 &is_non_constant_init);
13976 if (!member_p && processing_template_decl)
13977 finish_lambda_scope ();
13981 /* The old parser allows attributes to appear after a parenthesized
13982 initializer. Mark Mitchell proposed removing this functionality
13983 on the GCC mailing lists on 2002-08-13. This parser accepts the
13984 attributes -- but ignores them. */
13985 if (cp_parser_allow_gnu_extensions_p (parser)
13986 && initialization_kind == CPP_OPEN_PAREN)
13987 if (cp_parser_attributes_opt (parser))
13988 warning (OPT_Wattributes,
13989 "attributes after parenthesized initializer ignored");
13991 /* For an in-class declaration, use `grokfield' to create the
13997 pop_scope (pushed_scope);
13998 pushed_scope = false;
14000 decl = grokfield (declarator, decl_specifiers,
14001 initializer, !is_non_constant_init,
14002 /*asmspec=*/NULL_TREE,
14003 prefix_attributes);
14004 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14005 cp_parser_save_default_args (parser, decl);
14008 /* Finish processing the declaration. But, skip friend
14010 if (!friend_p && decl && decl != error_mark_node)
14012 cp_finish_decl (decl,
14013 initializer, !is_non_constant_init,
14015 /* If the initializer is in parentheses, then this is
14016 a direct-initialization, which means that an
14017 `explicit' constructor is OK. Otherwise, an
14018 `explicit' constructor cannot be used. */
14019 ((is_direct_init || !is_initialized)
14020 ? 0 : LOOKUP_ONLYCONVERTING));
14022 else if ((cxx_dialect != cxx98) && friend_p
14023 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14024 /* Core issue #226 (C++0x only): A default template-argument
14025 shall not be specified in a friend class template
14027 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14028 /*is_partial=*/0, /*is_friend_decl=*/1);
14030 if (!friend_p && pushed_scope)
14031 pop_scope (pushed_scope);
14036 /* Parse a declarator.
14040 ptr-operator declarator
14042 abstract-declarator:
14043 ptr-operator abstract-declarator [opt]
14044 direct-abstract-declarator
14049 attributes [opt] direct-declarator
14050 attributes [opt] ptr-operator declarator
14052 abstract-declarator:
14053 attributes [opt] ptr-operator abstract-declarator [opt]
14054 attributes [opt] direct-abstract-declarator
14056 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14057 detect constructor, destructor or conversion operators. It is set
14058 to -1 if the declarator is a name, and +1 if it is a
14059 function. Otherwise it is set to zero. Usually you just want to
14060 test for >0, but internally the negative value is used.
14062 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14063 a decl-specifier-seq unless it declares a constructor, destructor,
14064 or conversion. It might seem that we could check this condition in
14065 semantic analysis, rather than parsing, but that makes it difficult
14066 to handle something like `f()'. We want to notice that there are
14067 no decl-specifiers, and therefore realize that this is an
14068 expression, not a declaration.)
14070 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14071 the declarator is a direct-declarator of the form "(...)".
14073 MEMBER_P is true iff this declarator is a member-declarator. */
14075 static cp_declarator *
14076 cp_parser_declarator (cp_parser* parser,
14077 cp_parser_declarator_kind dcl_kind,
14078 int* ctor_dtor_or_conv_p,
14079 bool* parenthesized_p,
14082 cp_declarator *declarator;
14083 enum tree_code code;
14084 cp_cv_quals cv_quals;
14086 tree attributes = NULL_TREE;
14088 /* Assume this is not a constructor, destructor, or type-conversion
14090 if (ctor_dtor_or_conv_p)
14091 *ctor_dtor_or_conv_p = 0;
14093 if (cp_parser_allow_gnu_extensions_p (parser))
14094 attributes = cp_parser_attributes_opt (parser);
14096 /* Check for the ptr-operator production. */
14097 cp_parser_parse_tentatively (parser);
14098 /* Parse the ptr-operator. */
14099 code = cp_parser_ptr_operator (parser,
14102 /* If that worked, then we have a ptr-operator. */
14103 if (cp_parser_parse_definitely (parser))
14105 /* If a ptr-operator was found, then this declarator was not
14107 if (parenthesized_p)
14108 *parenthesized_p = true;
14109 /* The dependent declarator is optional if we are parsing an
14110 abstract-declarator. */
14111 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14112 cp_parser_parse_tentatively (parser);
14114 /* Parse the dependent declarator. */
14115 declarator = cp_parser_declarator (parser, dcl_kind,
14116 /*ctor_dtor_or_conv_p=*/NULL,
14117 /*parenthesized_p=*/NULL,
14118 /*member_p=*/false);
14120 /* If we are parsing an abstract-declarator, we must handle the
14121 case where the dependent declarator is absent. */
14122 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14123 && !cp_parser_parse_definitely (parser))
14126 declarator = cp_parser_make_indirect_declarator
14127 (code, class_type, cv_quals, declarator);
14129 /* Everything else is a direct-declarator. */
14132 if (parenthesized_p)
14133 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14135 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14136 ctor_dtor_or_conv_p,
14140 if (attributes && declarator && declarator != cp_error_declarator)
14141 declarator->attributes = attributes;
14146 /* Parse a direct-declarator or direct-abstract-declarator.
14150 direct-declarator ( parameter-declaration-clause )
14151 cv-qualifier-seq [opt]
14152 exception-specification [opt]
14153 direct-declarator [ constant-expression [opt] ]
14156 direct-abstract-declarator:
14157 direct-abstract-declarator [opt]
14158 ( parameter-declaration-clause )
14159 cv-qualifier-seq [opt]
14160 exception-specification [opt]
14161 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14162 ( abstract-declarator )
14164 Returns a representation of the declarator. DCL_KIND is
14165 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14166 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14167 we are parsing a direct-declarator. It is
14168 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14169 of ambiguity we prefer an abstract declarator, as per
14170 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14171 cp_parser_declarator. */
14173 static cp_declarator *
14174 cp_parser_direct_declarator (cp_parser* parser,
14175 cp_parser_declarator_kind dcl_kind,
14176 int* ctor_dtor_or_conv_p,
14180 cp_declarator *declarator = NULL;
14181 tree scope = NULL_TREE;
14182 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14183 bool saved_in_declarator_p = parser->in_declarator_p;
14185 tree pushed_scope = NULL_TREE;
14189 /* Peek at the next token. */
14190 token = cp_lexer_peek_token (parser->lexer);
14191 if (token->type == CPP_OPEN_PAREN)
14193 /* This is either a parameter-declaration-clause, or a
14194 parenthesized declarator. When we know we are parsing a
14195 named declarator, it must be a parenthesized declarator
14196 if FIRST is true. For instance, `(int)' is a
14197 parameter-declaration-clause, with an omitted
14198 direct-abstract-declarator. But `((*))', is a
14199 parenthesized abstract declarator. Finally, when T is a
14200 template parameter `(T)' is a
14201 parameter-declaration-clause, and not a parenthesized
14204 We first try and parse a parameter-declaration-clause,
14205 and then try a nested declarator (if FIRST is true).
14207 It is not an error for it not to be a
14208 parameter-declaration-clause, even when FIRST is
14214 The first is the declaration of a function while the
14215 second is the definition of a variable, including its
14218 Having seen only the parenthesis, we cannot know which of
14219 these two alternatives should be selected. Even more
14220 complex are examples like:
14225 The former is a function-declaration; the latter is a
14226 variable initialization.
14228 Thus again, we try a parameter-declaration-clause, and if
14229 that fails, we back out and return. */
14231 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14234 unsigned saved_num_template_parameter_lists;
14235 bool is_declarator = false;
14238 /* In a member-declarator, the only valid interpretation
14239 of a parenthesis is the start of a
14240 parameter-declaration-clause. (It is invalid to
14241 initialize a static data member with a parenthesized
14242 initializer; only the "=" form of initialization is
14245 cp_parser_parse_tentatively (parser);
14247 /* Consume the `('. */
14248 cp_lexer_consume_token (parser->lexer);
14251 /* If this is going to be an abstract declarator, we're
14252 in a declarator and we can't have default args. */
14253 parser->default_arg_ok_p = false;
14254 parser->in_declarator_p = true;
14257 /* Inside the function parameter list, surrounding
14258 template-parameter-lists do not apply. */
14259 saved_num_template_parameter_lists
14260 = parser->num_template_parameter_lists;
14261 parser->num_template_parameter_lists = 0;
14263 begin_scope (sk_function_parms, NULL_TREE);
14265 /* Parse the parameter-declaration-clause. */
14266 params = cp_parser_parameter_declaration_clause (parser);
14268 parser->num_template_parameter_lists
14269 = saved_num_template_parameter_lists;
14271 /* If all went well, parse the cv-qualifier-seq and the
14272 exception-specification. */
14273 if (member_p || cp_parser_parse_definitely (parser))
14275 cp_cv_quals cv_quals;
14276 tree exception_specification;
14279 is_declarator = true;
14281 if (ctor_dtor_or_conv_p)
14282 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14284 /* Consume the `)'. */
14285 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14287 /* Parse the cv-qualifier-seq. */
14288 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14289 /* And the exception-specification. */
14290 exception_specification
14291 = cp_parser_exception_specification_opt (parser);
14294 = cp_parser_late_return_type_opt (parser);
14296 /* Create the function-declarator. */
14297 declarator = make_call_declarator (declarator,
14300 exception_specification,
14302 /* Any subsequent parameter lists are to do with
14303 return type, so are not those of the declared
14305 parser->default_arg_ok_p = false;
14308 /* Remove the function parms from scope. */
14309 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14310 pop_binding (DECL_NAME (t), t);
14314 /* Repeat the main loop. */
14318 /* If this is the first, we can try a parenthesized
14322 bool saved_in_type_id_in_expr_p;
14324 parser->default_arg_ok_p = saved_default_arg_ok_p;
14325 parser->in_declarator_p = saved_in_declarator_p;
14327 /* Consume the `('. */
14328 cp_lexer_consume_token (parser->lexer);
14329 /* Parse the nested declarator. */
14330 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14331 parser->in_type_id_in_expr_p = true;
14333 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14334 /*parenthesized_p=*/NULL,
14336 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14338 /* Expect a `)'. */
14339 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14340 declarator = cp_error_declarator;
14341 if (declarator == cp_error_declarator)
14344 goto handle_declarator;
14346 /* Otherwise, we must be done. */
14350 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14351 && token->type == CPP_OPEN_SQUARE)
14353 /* Parse an array-declarator. */
14356 if (ctor_dtor_or_conv_p)
14357 *ctor_dtor_or_conv_p = 0;
14360 parser->default_arg_ok_p = false;
14361 parser->in_declarator_p = true;
14362 /* Consume the `['. */
14363 cp_lexer_consume_token (parser->lexer);
14364 /* Peek at the next token. */
14365 token = cp_lexer_peek_token (parser->lexer);
14366 /* If the next token is `]', then there is no
14367 constant-expression. */
14368 if (token->type != CPP_CLOSE_SQUARE)
14370 bool non_constant_p;
14373 = cp_parser_constant_expression (parser,
14374 /*allow_non_constant=*/true,
14376 if (!non_constant_p)
14377 bounds = fold_non_dependent_expr (bounds);
14378 /* Normally, the array bound must be an integral constant
14379 expression. However, as an extension, we allow VLAs
14380 in function scopes as long as they aren't part of a
14381 parameter declaration. */
14382 else if (!parser->in_function_body
14383 || current_binding_level->kind == sk_function_parms)
14385 cp_parser_error (parser,
14386 "array bound is not an integer constant");
14387 bounds = error_mark_node;
14389 else if (processing_template_decl && !error_operand_p (bounds))
14391 /* Remember this wasn't a constant-expression. */
14392 bounds = build_nop (TREE_TYPE (bounds), bounds);
14393 TREE_SIDE_EFFECTS (bounds) = 1;
14397 bounds = NULL_TREE;
14398 /* Look for the closing `]'. */
14399 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14401 declarator = cp_error_declarator;
14405 declarator = make_array_declarator (declarator, bounds);
14407 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14410 tree qualifying_scope;
14411 tree unqualified_name;
14412 special_function_kind sfk;
14414 bool pack_expansion_p = false;
14415 cp_token *declarator_id_start_token;
14417 /* Parse a declarator-id */
14418 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14421 cp_parser_parse_tentatively (parser);
14423 /* If we see an ellipsis, we should be looking at a
14425 if (token->type == CPP_ELLIPSIS)
14427 /* Consume the `...' */
14428 cp_lexer_consume_token (parser->lexer);
14430 pack_expansion_p = true;
14434 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14436 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14437 qualifying_scope = parser->scope;
14442 if (!unqualified_name && pack_expansion_p)
14444 /* Check whether an error occurred. */
14445 okay = !cp_parser_error_occurred (parser);
14447 /* We already consumed the ellipsis to mark a
14448 parameter pack, but we have no way to report it,
14449 so abort the tentative parse. We will be exiting
14450 immediately anyway. */
14451 cp_parser_abort_tentative_parse (parser);
14454 okay = cp_parser_parse_definitely (parser);
14457 unqualified_name = error_mark_node;
14458 else if (unqualified_name
14459 && (qualifying_scope
14460 || (TREE_CODE (unqualified_name)
14461 != IDENTIFIER_NODE)))
14463 cp_parser_error (parser, "expected unqualified-id");
14464 unqualified_name = error_mark_node;
14468 if (!unqualified_name)
14470 if (unqualified_name == error_mark_node)
14472 declarator = cp_error_declarator;
14473 pack_expansion_p = false;
14474 declarator->parameter_pack_p = false;
14478 if (qualifying_scope && at_namespace_scope_p ()
14479 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14481 /* In the declaration of a member of a template class
14482 outside of the class itself, the SCOPE will sometimes
14483 be a TYPENAME_TYPE. For example, given:
14485 template <typename T>
14486 int S<T>::R::i = 3;
14488 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14489 this context, we must resolve S<T>::R to an ordinary
14490 type, rather than a typename type.
14492 The reason we normally avoid resolving TYPENAME_TYPEs
14493 is that a specialization of `S' might render
14494 `S<T>::R' not a type. However, if `S' is
14495 specialized, then this `i' will not be used, so there
14496 is no harm in resolving the types here. */
14499 /* Resolve the TYPENAME_TYPE. */
14500 type = resolve_typename_type (qualifying_scope,
14501 /*only_current_p=*/false);
14502 /* If that failed, the declarator is invalid. */
14503 if (TREE_CODE (type) == TYPENAME_TYPE)
14505 if (typedef_variant_p (type))
14506 error_at (declarator_id_start_token->location,
14507 "cannot define member of dependent typedef "
14510 error_at (declarator_id_start_token->location,
14511 "%<%T::%E%> is not a type",
14512 TYPE_CONTEXT (qualifying_scope),
14513 TYPE_IDENTIFIER (qualifying_scope));
14515 qualifying_scope = type;
14520 if (unqualified_name)
14524 if (qualifying_scope
14525 && CLASS_TYPE_P (qualifying_scope))
14526 class_type = qualifying_scope;
14528 class_type = current_class_type;
14530 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14532 tree name_type = TREE_TYPE (unqualified_name);
14533 if (class_type && same_type_p (name_type, class_type))
14535 if (qualifying_scope
14536 && CLASSTYPE_USE_TEMPLATE (name_type))
14538 error_at (declarator_id_start_token->location,
14539 "invalid use of constructor as a template");
14540 inform (declarator_id_start_token->location,
14541 "use %<%T::%D%> instead of %<%T::%D%> to "
14542 "name the constructor in a qualified name",
14544 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14545 class_type, name_type);
14546 declarator = cp_error_declarator;
14550 unqualified_name = constructor_name (class_type);
14554 /* We do not attempt to print the declarator
14555 here because we do not have enough
14556 information about its original syntactic
14558 cp_parser_error (parser, "invalid declarator");
14559 declarator = cp_error_declarator;
14566 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14567 sfk = sfk_destructor;
14568 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14569 sfk = sfk_conversion;
14570 else if (/* There's no way to declare a constructor
14571 for an anonymous type, even if the type
14572 got a name for linkage purposes. */
14573 !TYPE_WAS_ANONYMOUS (class_type)
14574 && constructor_name_p (unqualified_name,
14577 unqualified_name = constructor_name (class_type);
14578 sfk = sfk_constructor;
14580 else if (is_overloaded_fn (unqualified_name)
14581 && DECL_CONSTRUCTOR_P (get_first_fn
14582 (unqualified_name)))
14583 sfk = sfk_constructor;
14585 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14586 *ctor_dtor_or_conv_p = -1;
14589 declarator = make_id_declarator (qualifying_scope,
14592 declarator->id_loc = token->location;
14593 declarator->parameter_pack_p = pack_expansion_p;
14595 if (pack_expansion_p)
14596 maybe_warn_variadic_templates ();
14599 handle_declarator:;
14600 scope = get_scope_of_declarator (declarator);
14602 /* Any names that appear after the declarator-id for a
14603 member are looked up in the containing scope. */
14604 pushed_scope = push_scope (scope);
14605 parser->in_declarator_p = true;
14606 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14607 || (declarator && declarator->kind == cdk_id))
14608 /* Default args are only allowed on function
14610 parser->default_arg_ok_p = saved_default_arg_ok_p;
14612 parser->default_arg_ok_p = false;
14621 /* For an abstract declarator, we might wind up with nothing at this
14622 point. That's an error; the declarator is not optional. */
14624 cp_parser_error (parser, "expected declarator");
14626 /* If we entered a scope, we must exit it now. */
14628 pop_scope (pushed_scope);
14630 parser->default_arg_ok_p = saved_default_arg_ok_p;
14631 parser->in_declarator_p = saved_in_declarator_p;
14636 /* Parse a ptr-operator.
14639 * cv-qualifier-seq [opt]
14641 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14646 & cv-qualifier-seq [opt]
14648 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14649 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14650 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14651 filled in with the TYPE containing the member. *CV_QUALS is
14652 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14653 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14654 Note that the tree codes returned by this function have nothing
14655 to do with the types of trees that will be eventually be created
14656 to represent the pointer or reference type being parsed. They are
14657 just constants with suggestive names. */
14658 static enum tree_code
14659 cp_parser_ptr_operator (cp_parser* parser,
14661 cp_cv_quals *cv_quals)
14663 enum tree_code code = ERROR_MARK;
14666 /* Assume that it's not a pointer-to-member. */
14668 /* And that there are no cv-qualifiers. */
14669 *cv_quals = TYPE_UNQUALIFIED;
14671 /* Peek at the next token. */
14672 token = cp_lexer_peek_token (parser->lexer);
14674 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14675 if (token->type == CPP_MULT)
14676 code = INDIRECT_REF;
14677 else if (token->type == CPP_AND)
14679 else if ((cxx_dialect != cxx98) &&
14680 token->type == CPP_AND_AND) /* C++0x only */
14681 code = NON_LVALUE_EXPR;
14683 if (code != ERROR_MARK)
14685 /* Consume the `*', `&' or `&&'. */
14686 cp_lexer_consume_token (parser->lexer);
14688 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14689 `&', if we are allowing GNU extensions. (The only qualifier
14690 that can legally appear after `&' is `restrict', but that is
14691 enforced during semantic analysis. */
14692 if (code == INDIRECT_REF
14693 || cp_parser_allow_gnu_extensions_p (parser))
14694 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14698 /* Try the pointer-to-member case. */
14699 cp_parser_parse_tentatively (parser);
14700 /* Look for the optional `::' operator. */
14701 cp_parser_global_scope_opt (parser,
14702 /*current_scope_valid_p=*/false);
14703 /* Look for the nested-name specifier. */
14704 token = cp_lexer_peek_token (parser->lexer);
14705 cp_parser_nested_name_specifier (parser,
14706 /*typename_keyword_p=*/false,
14707 /*check_dependency_p=*/true,
14709 /*is_declaration=*/false);
14710 /* If we found it, and the next token is a `*', then we are
14711 indeed looking at a pointer-to-member operator. */
14712 if (!cp_parser_error_occurred (parser)
14713 && cp_parser_require (parser, CPP_MULT, RT_MULT))
14715 /* Indicate that the `*' operator was used. */
14716 code = INDIRECT_REF;
14718 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14719 error_at (token->location, "%qD is a namespace", parser->scope);
14722 /* The type of which the member is a member is given by the
14724 *type = parser->scope;
14725 /* The next name will not be qualified. */
14726 parser->scope = NULL_TREE;
14727 parser->qualifying_scope = NULL_TREE;
14728 parser->object_scope = NULL_TREE;
14729 /* Look for the optional cv-qualifier-seq. */
14730 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14733 /* If that didn't work we don't have a ptr-operator. */
14734 if (!cp_parser_parse_definitely (parser))
14735 cp_parser_error (parser, "expected ptr-operator");
14741 /* Parse an (optional) cv-qualifier-seq.
14744 cv-qualifier cv-qualifier-seq [opt]
14755 Returns a bitmask representing the cv-qualifiers. */
14758 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14760 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14765 cp_cv_quals cv_qualifier;
14767 /* Peek at the next token. */
14768 token = cp_lexer_peek_token (parser->lexer);
14769 /* See if it's a cv-qualifier. */
14770 switch (token->keyword)
14773 cv_qualifier = TYPE_QUAL_CONST;
14777 cv_qualifier = TYPE_QUAL_VOLATILE;
14781 cv_qualifier = TYPE_QUAL_RESTRICT;
14785 cv_qualifier = TYPE_UNQUALIFIED;
14792 if (cv_quals & cv_qualifier)
14794 error_at (token->location, "duplicate cv-qualifier");
14795 cp_lexer_purge_token (parser->lexer);
14799 cp_lexer_consume_token (parser->lexer);
14800 cv_quals |= cv_qualifier;
14807 /* Parse a late-specified return type, if any. This is not a separate
14808 non-terminal, but part of a function declarator, which looks like
14810 -> trailing-type-specifier-seq abstract-declarator(opt)
14812 Returns the type indicated by the type-id. */
14815 cp_parser_late_return_type_opt (cp_parser* parser)
14819 /* Peek at the next token. */
14820 token = cp_lexer_peek_token (parser->lexer);
14821 /* A late-specified return type is indicated by an initial '->'. */
14822 if (token->type != CPP_DEREF)
14825 /* Consume the ->. */
14826 cp_lexer_consume_token (parser->lexer);
14828 return cp_parser_trailing_type_id (parser);
14831 /* Parse a declarator-id.
14835 :: [opt] nested-name-specifier [opt] type-name
14837 In the `id-expression' case, the value returned is as for
14838 cp_parser_id_expression if the id-expression was an unqualified-id.
14839 If the id-expression was a qualified-id, then a SCOPE_REF is
14840 returned. The first operand is the scope (either a NAMESPACE_DECL
14841 or TREE_TYPE), but the second is still just a representation of an
14845 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14848 /* The expression must be an id-expression. Assume that qualified
14849 names are the names of types so that:
14852 int S<T>::R::i = 3;
14854 will work; we must treat `S<T>::R' as the name of a type.
14855 Similarly, assume that qualified names are templates, where
14859 int S<T>::R<T>::i = 3;
14862 id = cp_parser_id_expression (parser,
14863 /*template_keyword_p=*/false,
14864 /*check_dependency_p=*/false,
14865 /*template_p=*/NULL,
14866 /*declarator_p=*/true,
14868 if (id && BASELINK_P (id))
14869 id = BASELINK_FUNCTIONS (id);
14873 /* Parse a type-id.
14876 type-specifier-seq abstract-declarator [opt]
14878 Returns the TYPE specified. */
14881 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14882 bool is_trailing_return)
14884 cp_decl_specifier_seq type_specifier_seq;
14885 cp_declarator *abstract_declarator;
14887 /* Parse the type-specifier-seq. */
14888 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14889 is_trailing_return,
14890 &type_specifier_seq);
14891 if (type_specifier_seq.type == error_mark_node)
14892 return error_mark_node;
14894 /* There might or might not be an abstract declarator. */
14895 cp_parser_parse_tentatively (parser);
14896 /* Look for the declarator. */
14897 abstract_declarator
14898 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14899 /*parenthesized_p=*/NULL,
14900 /*member_p=*/false);
14901 /* Check to see if there really was a declarator. */
14902 if (!cp_parser_parse_definitely (parser))
14903 abstract_declarator = NULL;
14905 if (type_specifier_seq.type
14906 && type_uses_auto (type_specifier_seq.type))
14908 /* A type-id with type 'auto' is only ok if the abstract declarator
14909 is a function declarator with a late-specified return type. */
14910 if (abstract_declarator
14911 && abstract_declarator->kind == cdk_function
14912 && abstract_declarator->u.function.late_return_type)
14916 error ("invalid use of %<auto%>");
14917 return error_mark_node;
14921 return groktypename (&type_specifier_seq, abstract_declarator,
14925 static tree cp_parser_type_id (cp_parser *parser)
14927 return cp_parser_type_id_1 (parser, false, false);
14930 static tree cp_parser_template_type_arg (cp_parser *parser)
14932 return cp_parser_type_id_1 (parser, true, false);
14935 static tree cp_parser_trailing_type_id (cp_parser *parser)
14937 return cp_parser_type_id_1 (parser, false, true);
14940 /* Parse a type-specifier-seq.
14942 type-specifier-seq:
14943 type-specifier type-specifier-seq [opt]
14947 type-specifier-seq:
14948 attributes type-specifier-seq [opt]
14950 If IS_DECLARATION is true, we are at the start of a "condition" or
14951 exception-declaration, so we might be followed by a declarator-id.
14953 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14954 i.e. we've just seen "->".
14956 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14959 cp_parser_type_specifier_seq (cp_parser* parser,
14960 bool is_declaration,
14961 bool is_trailing_return,
14962 cp_decl_specifier_seq *type_specifier_seq)
14964 bool seen_type_specifier = false;
14965 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14966 cp_token *start_token = NULL;
14968 /* Clear the TYPE_SPECIFIER_SEQ. */
14969 clear_decl_specs (type_specifier_seq);
14971 /* In the context of a trailing return type, enum E { } is an
14972 elaborated-type-specifier followed by a function-body, not an
14974 if (is_trailing_return)
14975 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14977 /* Parse the type-specifiers and attributes. */
14980 tree type_specifier;
14981 bool is_cv_qualifier;
14983 /* Check for attributes first. */
14984 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14986 type_specifier_seq->attributes =
14987 chainon (type_specifier_seq->attributes,
14988 cp_parser_attributes_opt (parser));
14992 /* record the token of the beginning of the type specifier seq,
14993 for error reporting purposes*/
14995 start_token = cp_lexer_peek_token (parser->lexer);
14997 /* Look for the type-specifier. */
14998 type_specifier = cp_parser_type_specifier (parser,
15000 type_specifier_seq,
15001 /*is_declaration=*/false,
15004 if (!type_specifier)
15006 /* If the first type-specifier could not be found, this is not a
15007 type-specifier-seq at all. */
15008 if (!seen_type_specifier)
15010 cp_parser_error (parser, "expected type-specifier");
15011 type_specifier_seq->type = error_mark_node;
15014 /* If subsequent type-specifiers could not be found, the
15015 type-specifier-seq is complete. */
15019 seen_type_specifier = true;
15020 /* The standard says that a condition can be:
15022 type-specifier-seq declarator = assignment-expression
15029 we should treat the "S" as a declarator, not as a
15030 type-specifier. The standard doesn't say that explicitly for
15031 type-specifier-seq, but it does say that for
15032 decl-specifier-seq in an ordinary declaration. Perhaps it
15033 would be clearer just to allow a decl-specifier-seq here, and
15034 then add a semantic restriction that if any decl-specifiers
15035 that are not type-specifiers appear, the program is invalid. */
15036 if (is_declaration && !is_cv_qualifier)
15037 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15040 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15043 /* Parse a parameter-declaration-clause.
15045 parameter-declaration-clause:
15046 parameter-declaration-list [opt] ... [opt]
15047 parameter-declaration-list , ...
15049 Returns a representation for the parameter declarations. A return
15050 value of NULL indicates a parameter-declaration-clause consisting
15051 only of an ellipsis. */
15054 cp_parser_parameter_declaration_clause (cp_parser* parser)
15061 /* Peek at the next token. */
15062 token = cp_lexer_peek_token (parser->lexer);
15063 /* Check for trivial parameter-declaration-clauses. */
15064 if (token->type == CPP_ELLIPSIS)
15066 /* Consume the `...' token. */
15067 cp_lexer_consume_token (parser->lexer);
15070 else if (token->type == CPP_CLOSE_PAREN)
15071 /* There are no parameters. */
15073 #ifndef NO_IMPLICIT_EXTERN_C
15074 if (in_system_header && current_class_type == NULL
15075 && current_lang_name == lang_name_c)
15079 return void_list_node;
15081 /* Check for `(void)', too, which is a special case. */
15082 else if (token->keyword == RID_VOID
15083 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15084 == CPP_CLOSE_PAREN))
15086 /* Consume the `void' token. */
15087 cp_lexer_consume_token (parser->lexer);
15088 /* There are no parameters. */
15089 return void_list_node;
15092 /* Parse the parameter-declaration-list. */
15093 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15094 /* If a parse error occurred while parsing the
15095 parameter-declaration-list, then the entire
15096 parameter-declaration-clause is erroneous. */
15100 /* Peek at the next token. */
15101 token = cp_lexer_peek_token (parser->lexer);
15102 /* If it's a `,', the clause should terminate with an ellipsis. */
15103 if (token->type == CPP_COMMA)
15105 /* Consume the `,'. */
15106 cp_lexer_consume_token (parser->lexer);
15107 /* Expect an ellipsis. */
15109 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15111 /* It might also be `...' if the optional trailing `,' was
15113 else if (token->type == CPP_ELLIPSIS)
15115 /* Consume the `...' token. */
15116 cp_lexer_consume_token (parser->lexer);
15117 /* And remember that we saw it. */
15121 ellipsis_p = false;
15123 /* Finish the parameter list. */
15125 parameters = chainon (parameters, void_list_node);
15130 /* Parse a parameter-declaration-list.
15132 parameter-declaration-list:
15133 parameter-declaration
15134 parameter-declaration-list , parameter-declaration
15136 Returns a representation of the parameter-declaration-list, as for
15137 cp_parser_parameter_declaration_clause. However, the
15138 `void_list_node' is never appended to the list. Upon return,
15139 *IS_ERROR will be true iff an error occurred. */
15142 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15144 tree parameters = NULL_TREE;
15145 tree *tail = ¶meters;
15146 bool saved_in_unbraced_linkage_specification_p;
15149 /* Assume all will go well. */
15151 /* The special considerations that apply to a function within an
15152 unbraced linkage specifications do not apply to the parameters
15153 to the function. */
15154 saved_in_unbraced_linkage_specification_p
15155 = parser->in_unbraced_linkage_specification_p;
15156 parser->in_unbraced_linkage_specification_p = false;
15158 /* Look for more parameters. */
15161 cp_parameter_declarator *parameter;
15162 tree decl = error_mark_node;
15163 bool parenthesized_p;
15164 /* Parse the parameter. */
15166 = cp_parser_parameter_declaration (parser,
15167 /*template_parm_p=*/false,
15170 /* We don't know yet if the enclosing context is deprecated, so wait
15171 and warn in grokparms if appropriate. */
15172 deprecated_state = DEPRECATED_SUPPRESS;
15175 decl = grokdeclarator (parameter->declarator,
15176 ¶meter->decl_specifiers,
15178 parameter->default_argument != NULL_TREE,
15179 ¶meter->decl_specifiers.attributes);
15181 deprecated_state = DEPRECATED_NORMAL;
15183 /* If a parse error occurred parsing the parameter declaration,
15184 then the entire parameter-declaration-list is erroneous. */
15185 if (decl == error_mark_node)
15188 parameters = error_mark_node;
15192 if (parameter->decl_specifiers.attributes)
15193 cplus_decl_attributes (&decl,
15194 parameter->decl_specifiers.attributes,
15196 if (DECL_NAME (decl))
15197 decl = pushdecl (decl);
15199 if (decl != error_mark_node)
15201 retrofit_lang_decl (decl);
15202 DECL_PARM_INDEX (decl) = ++index;
15205 /* Add the new parameter to the list. */
15206 *tail = build_tree_list (parameter->default_argument, decl);
15207 tail = &TREE_CHAIN (*tail);
15209 /* Peek at the next token. */
15210 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15211 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15212 /* These are for Objective-C++ */
15213 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15214 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15215 /* The parameter-declaration-list is complete. */
15217 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15221 /* Peek at the next token. */
15222 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15223 /* If it's an ellipsis, then the list is complete. */
15224 if (token->type == CPP_ELLIPSIS)
15226 /* Otherwise, there must be more parameters. Consume the
15228 cp_lexer_consume_token (parser->lexer);
15229 /* When parsing something like:
15231 int i(float f, double d)
15233 we can tell after seeing the declaration for "f" that we
15234 are not looking at an initialization of a variable "i",
15235 but rather at the declaration of a function "i".
15237 Due to the fact that the parsing of template arguments
15238 (as specified to a template-id) requires backtracking we
15239 cannot use this technique when inside a template argument
15241 if (!parser->in_template_argument_list_p
15242 && !parser->in_type_id_in_expr_p
15243 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15244 /* However, a parameter-declaration of the form
15245 "foat(f)" (which is a valid declaration of a
15246 parameter "f") can also be interpreted as an
15247 expression (the conversion of "f" to "float"). */
15248 && !parenthesized_p)
15249 cp_parser_commit_to_tentative_parse (parser);
15253 cp_parser_error (parser, "expected %<,%> or %<...%>");
15254 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15255 cp_parser_skip_to_closing_parenthesis (parser,
15256 /*recovering=*/true,
15257 /*or_comma=*/false,
15258 /*consume_paren=*/false);
15263 parser->in_unbraced_linkage_specification_p
15264 = saved_in_unbraced_linkage_specification_p;
15269 /* Parse a parameter declaration.
15271 parameter-declaration:
15272 decl-specifier-seq ... [opt] declarator
15273 decl-specifier-seq declarator = assignment-expression
15274 decl-specifier-seq ... [opt] abstract-declarator [opt]
15275 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15277 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15278 declares a template parameter. (In that case, a non-nested `>'
15279 token encountered during the parsing of the assignment-expression
15280 is not interpreted as a greater-than operator.)
15282 Returns a representation of the parameter, or NULL if an error
15283 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15284 true iff the declarator is of the form "(p)". */
15286 static cp_parameter_declarator *
15287 cp_parser_parameter_declaration (cp_parser *parser,
15288 bool template_parm_p,
15289 bool *parenthesized_p)
15291 int declares_class_or_enum;
15292 cp_decl_specifier_seq decl_specifiers;
15293 cp_declarator *declarator;
15294 tree default_argument;
15295 cp_token *token = NULL, *declarator_token_start = NULL;
15296 const char *saved_message;
15298 /* In a template parameter, `>' is not an operator.
15302 When parsing a default template-argument for a non-type
15303 template-parameter, the first non-nested `>' is taken as the end
15304 of the template parameter-list rather than a greater-than
15307 /* Type definitions may not appear in parameter types. */
15308 saved_message = parser->type_definition_forbidden_message;
15309 parser->type_definition_forbidden_message
15310 = G_("types may not be defined in parameter types");
15312 /* Parse the declaration-specifiers. */
15313 cp_parser_decl_specifier_seq (parser,
15314 CP_PARSER_FLAGS_NONE,
15316 &declares_class_or_enum);
15318 /* Complain about missing 'typename' or other invalid type names. */
15319 if (!decl_specifiers.any_type_specifiers_p)
15320 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15322 /* If an error occurred, there's no reason to attempt to parse the
15323 rest of the declaration. */
15324 if (cp_parser_error_occurred (parser))
15326 parser->type_definition_forbidden_message = saved_message;
15330 /* Peek at the next token. */
15331 token = cp_lexer_peek_token (parser->lexer);
15333 /* If the next token is a `)', `,', `=', `>', or `...', then there
15334 is no declarator. However, when variadic templates are enabled,
15335 there may be a declarator following `...'. */
15336 if (token->type == CPP_CLOSE_PAREN
15337 || token->type == CPP_COMMA
15338 || token->type == CPP_EQ
15339 || token->type == CPP_GREATER)
15342 if (parenthesized_p)
15343 *parenthesized_p = false;
15345 /* Otherwise, there should be a declarator. */
15348 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15349 parser->default_arg_ok_p = false;
15351 /* After seeing a decl-specifier-seq, if the next token is not a
15352 "(", there is no possibility that the code is a valid
15353 expression. Therefore, if parsing tentatively, we commit at
15355 if (!parser->in_template_argument_list_p
15356 /* In an expression context, having seen:
15360 we cannot be sure whether we are looking at a
15361 function-type (taking a "char" as a parameter) or a cast
15362 of some object of type "char" to "int". */
15363 && !parser->in_type_id_in_expr_p
15364 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15365 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15366 cp_parser_commit_to_tentative_parse (parser);
15367 /* Parse the declarator. */
15368 declarator_token_start = token;
15369 declarator = cp_parser_declarator (parser,
15370 CP_PARSER_DECLARATOR_EITHER,
15371 /*ctor_dtor_or_conv_p=*/NULL,
15373 /*member_p=*/false);
15374 parser->default_arg_ok_p = saved_default_arg_ok_p;
15375 /* After the declarator, allow more attributes. */
15376 decl_specifiers.attributes
15377 = chainon (decl_specifiers.attributes,
15378 cp_parser_attributes_opt (parser));
15381 /* If the next token is an ellipsis, and we have not seen a
15382 declarator name, and the type of the declarator contains parameter
15383 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15384 a parameter pack expansion expression. Otherwise, leave the
15385 ellipsis for a C-style variadic function. */
15386 token = cp_lexer_peek_token (parser->lexer);
15387 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15389 tree type = decl_specifiers.type;
15391 if (type && DECL_P (type))
15392 type = TREE_TYPE (type);
15395 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15396 && declarator_can_be_parameter_pack (declarator)
15397 && (!declarator || !declarator->parameter_pack_p)
15398 && uses_parameter_packs (type))
15400 /* Consume the `...'. */
15401 cp_lexer_consume_token (parser->lexer);
15402 maybe_warn_variadic_templates ();
15404 /* Build a pack expansion type */
15406 declarator->parameter_pack_p = true;
15408 decl_specifiers.type = make_pack_expansion (type);
15412 /* The restriction on defining new types applies only to the type
15413 of the parameter, not to the default argument. */
15414 parser->type_definition_forbidden_message = saved_message;
15416 /* If the next token is `=', then process a default argument. */
15417 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15419 /* Consume the `='. */
15420 cp_lexer_consume_token (parser->lexer);
15422 /* If we are defining a class, then the tokens that make up the
15423 default argument must be saved and processed later. */
15424 if (!template_parm_p && at_class_scope_p ()
15425 && TYPE_BEING_DEFINED (current_class_type)
15426 && !LAMBDA_TYPE_P (current_class_type))
15428 unsigned depth = 0;
15429 int maybe_template_id = 0;
15430 cp_token *first_token;
15433 /* Add tokens until we have processed the entire default
15434 argument. We add the range [first_token, token). */
15435 first_token = cp_lexer_peek_token (parser->lexer);
15440 /* Peek at the next token. */
15441 token = cp_lexer_peek_token (parser->lexer);
15442 /* What we do depends on what token we have. */
15443 switch (token->type)
15445 /* In valid code, a default argument must be
15446 immediately followed by a `,' `)', or `...'. */
15448 if (depth == 0 && maybe_template_id)
15450 /* If we've seen a '<', we might be in a
15451 template-argument-list. Until Core issue 325 is
15452 resolved, we don't know how this situation ought
15453 to be handled, so try to DTRT. We check whether
15454 what comes after the comma is a valid parameter
15455 declaration list. If it is, then the comma ends
15456 the default argument; otherwise the default
15457 argument continues. */
15458 bool error = false;
15460 /* Set ITALP so cp_parser_parameter_declaration_list
15461 doesn't decide to commit to this parse. */
15462 bool saved_italp = parser->in_template_argument_list_p;
15463 parser->in_template_argument_list_p = true;
15465 cp_parser_parse_tentatively (parser);
15466 cp_lexer_consume_token (parser->lexer);
15467 cp_parser_parameter_declaration_list (parser, &error);
15468 if (!cp_parser_error_occurred (parser) && !error)
15470 cp_parser_abort_tentative_parse (parser);
15472 parser->in_template_argument_list_p = saved_italp;
15475 case CPP_CLOSE_PAREN:
15477 /* If we run into a non-nested `;', `}', or `]',
15478 then the code is invalid -- but the default
15479 argument is certainly over. */
15480 case CPP_SEMICOLON:
15481 case CPP_CLOSE_BRACE:
15482 case CPP_CLOSE_SQUARE:
15485 /* Update DEPTH, if necessary. */
15486 else if (token->type == CPP_CLOSE_PAREN
15487 || token->type == CPP_CLOSE_BRACE
15488 || token->type == CPP_CLOSE_SQUARE)
15492 case CPP_OPEN_PAREN:
15493 case CPP_OPEN_SQUARE:
15494 case CPP_OPEN_BRACE:
15500 /* This might be the comparison operator, or it might
15501 start a template argument list. */
15502 ++maybe_template_id;
15506 if (cxx_dialect == cxx98)
15508 /* Fall through for C++0x, which treats the `>>'
15509 operator like two `>' tokens in certain
15515 /* This might be an operator, or it might close a
15516 template argument list. But if a previous '<'
15517 started a template argument list, this will have
15518 closed it, so we can't be in one anymore. */
15519 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15520 if (maybe_template_id < 0)
15521 maybe_template_id = 0;
15525 /* If we run out of tokens, issue an error message. */
15527 case CPP_PRAGMA_EOL:
15528 error_at (token->location, "file ends in default argument");
15534 /* In these cases, we should look for template-ids.
15535 For example, if the default argument is
15536 `X<int, double>()', we need to do name lookup to
15537 figure out whether or not `X' is a template; if
15538 so, the `,' does not end the default argument.
15540 That is not yet done. */
15547 /* If we've reached the end, stop. */
15551 /* Add the token to the token block. */
15552 token = cp_lexer_consume_token (parser->lexer);
15555 /* Create a DEFAULT_ARG to represent the unparsed default
15557 default_argument = make_node (DEFAULT_ARG);
15558 DEFARG_TOKENS (default_argument)
15559 = cp_token_cache_new (first_token, token);
15560 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15562 /* Outside of a class definition, we can just parse the
15563 assignment-expression. */
15566 token = cp_lexer_peek_token (parser->lexer);
15568 = cp_parser_default_argument (parser, template_parm_p);
15571 if (!parser->default_arg_ok_p)
15573 if (flag_permissive)
15574 warning (0, "deprecated use of default argument for parameter of non-function");
15577 error_at (token->location,
15578 "default arguments are only "
15579 "permitted for function parameters");
15580 default_argument = NULL_TREE;
15583 else if ((declarator && declarator->parameter_pack_p)
15584 || (decl_specifiers.type
15585 && PACK_EXPANSION_P (decl_specifiers.type)))
15587 /* Find the name of the parameter pack. */
15588 cp_declarator *id_declarator = declarator;
15589 while (id_declarator && id_declarator->kind != cdk_id)
15590 id_declarator = id_declarator->declarator;
15592 if (id_declarator && id_declarator->kind == cdk_id)
15593 error_at (declarator_token_start->location,
15595 ? "template parameter pack %qD"
15596 " cannot have a default argument"
15597 : "parameter pack %qD cannot have a default argument",
15598 id_declarator->u.id.unqualified_name);
15600 error_at (declarator_token_start->location,
15602 ? "template parameter pack cannot have a default argument"
15603 : "parameter pack cannot have a default argument");
15605 default_argument = NULL_TREE;
15609 default_argument = NULL_TREE;
15611 return make_parameter_declarator (&decl_specifiers,
15616 /* Parse a default argument and return it.
15618 TEMPLATE_PARM_P is true if this is a default argument for a
15619 non-type template parameter. */
15621 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15623 tree default_argument = NULL_TREE;
15624 bool saved_greater_than_is_operator_p;
15625 bool saved_local_variables_forbidden_p;
15627 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15629 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15630 parser->greater_than_is_operator_p = !template_parm_p;
15631 /* Local variable names (and the `this' keyword) may not
15632 appear in a default argument. */
15633 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15634 parser->local_variables_forbidden_p = true;
15635 /* Parse the assignment-expression. */
15636 if (template_parm_p)
15637 push_deferring_access_checks (dk_no_deferred);
15639 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15640 if (template_parm_p)
15641 pop_deferring_access_checks ();
15642 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15643 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15645 return default_argument;
15648 /* Parse a function-body.
15651 compound_statement */
15654 cp_parser_function_body (cp_parser *parser)
15656 cp_parser_compound_statement (parser, NULL, false);
15659 /* Parse a ctor-initializer-opt followed by a function-body. Return
15660 true if a ctor-initializer was present. */
15663 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15666 bool ctor_initializer_p;
15668 /* Begin the function body. */
15669 body = begin_function_body ();
15670 /* Parse the optional ctor-initializer. */
15671 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15672 /* Parse the function-body. */
15673 cp_parser_function_body (parser);
15674 /* Finish the function body. */
15675 finish_function_body (body);
15677 return ctor_initializer_p;
15680 /* Parse an initializer.
15683 = initializer-clause
15684 ( expression-list )
15686 Returns an expression representing the initializer. If no
15687 initializer is present, NULL_TREE is returned.
15689 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15690 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15691 set to TRUE if there is no initializer present. If there is an
15692 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15693 is set to true; otherwise it is set to false. */
15696 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15697 bool* non_constant_p)
15702 /* Peek at the next token. */
15703 token = cp_lexer_peek_token (parser->lexer);
15705 /* Let our caller know whether or not this initializer was
15707 *is_direct_init = (token->type != CPP_EQ);
15708 /* Assume that the initializer is constant. */
15709 *non_constant_p = false;
15711 if (token->type == CPP_EQ)
15713 /* Consume the `='. */
15714 cp_lexer_consume_token (parser->lexer);
15715 /* Parse the initializer-clause. */
15716 init = cp_parser_initializer_clause (parser, non_constant_p);
15718 else if (token->type == CPP_OPEN_PAREN)
15721 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15723 /*allow_expansion_p=*/true,
15726 return error_mark_node;
15727 init = build_tree_list_vec (vec);
15728 release_tree_vector (vec);
15730 else if (token->type == CPP_OPEN_BRACE)
15732 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15733 init = cp_parser_braced_list (parser, non_constant_p);
15734 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15738 /* Anything else is an error. */
15739 cp_parser_error (parser, "expected initializer");
15740 init = error_mark_node;
15746 /* Parse an initializer-clause.
15748 initializer-clause:
15749 assignment-expression
15752 Returns an expression representing the initializer.
15754 If the `assignment-expression' production is used the value
15755 returned is simply a representation for the expression.
15757 Otherwise, calls cp_parser_braced_list. */
15760 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15764 /* Assume the expression is constant. */
15765 *non_constant_p = false;
15767 /* If it is not a `{', then we are looking at an
15768 assignment-expression. */
15769 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15772 = cp_parser_constant_expression (parser,
15773 /*allow_non_constant_p=*/true,
15775 if (!*non_constant_p)
15776 initializer = fold_non_dependent_expr (initializer);
15779 initializer = cp_parser_braced_list (parser, non_constant_p);
15781 return initializer;
15784 /* Parse a brace-enclosed initializer list.
15787 { initializer-list , [opt] }
15790 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15791 the elements of the initializer-list (or NULL, if the last
15792 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15793 NULL_TREE. There is no way to detect whether or not the optional
15794 trailing `,' was provided. NON_CONSTANT_P is as for
15795 cp_parser_initializer. */
15798 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15802 /* Consume the `{' token. */
15803 cp_lexer_consume_token (parser->lexer);
15804 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15805 initializer = make_node (CONSTRUCTOR);
15806 /* If it's not a `}', then there is a non-trivial initializer. */
15807 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15809 /* Parse the initializer list. */
15810 CONSTRUCTOR_ELTS (initializer)
15811 = cp_parser_initializer_list (parser, non_constant_p);
15812 /* A trailing `,' token is allowed. */
15813 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15814 cp_lexer_consume_token (parser->lexer);
15816 /* Now, there should be a trailing `}'. */
15817 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15818 TREE_TYPE (initializer) = init_list_type_node;
15819 return initializer;
15822 /* Parse an initializer-list.
15825 initializer-clause ... [opt]
15826 initializer-list , initializer-clause ... [opt]
15831 identifier : initializer-clause
15832 initializer-list, identifier : initializer-clause
15834 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15835 for the initializer. If the INDEX of the elt is non-NULL, it is the
15836 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15837 as for cp_parser_initializer. */
15839 static VEC(constructor_elt,gc) *
15840 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15842 VEC(constructor_elt,gc) *v = NULL;
15844 /* Assume all of the expressions are constant. */
15845 *non_constant_p = false;
15847 /* Parse the rest of the list. */
15853 bool clause_non_constant_p;
15855 /* If the next token is an identifier and the following one is a
15856 colon, we are looking at the GNU designated-initializer
15858 if (cp_parser_allow_gnu_extensions_p (parser)
15859 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15860 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15862 /* Warn the user that they are using an extension. */
15863 pedwarn (input_location, OPT_pedantic,
15864 "ISO C++ does not allow designated initializers");
15865 /* Consume the identifier. */
15866 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15867 /* Consume the `:'. */
15868 cp_lexer_consume_token (parser->lexer);
15871 identifier = NULL_TREE;
15873 /* Parse the initializer. */
15874 initializer = cp_parser_initializer_clause (parser,
15875 &clause_non_constant_p);
15876 /* If any clause is non-constant, so is the entire initializer. */
15877 if (clause_non_constant_p)
15878 *non_constant_p = true;
15880 /* If we have an ellipsis, this is an initializer pack
15882 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15884 /* Consume the `...'. */
15885 cp_lexer_consume_token (parser->lexer);
15887 /* Turn the initializer into an initializer expansion. */
15888 initializer = make_pack_expansion (initializer);
15891 /* Add it to the vector. */
15892 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15894 /* If the next token is not a comma, we have reached the end of
15896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15899 /* Peek at the next token. */
15900 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15901 /* If the next token is a `}', then we're still done. An
15902 initializer-clause can have a trailing `,' after the
15903 initializer-list and before the closing `}'. */
15904 if (token->type == CPP_CLOSE_BRACE)
15907 /* Consume the `,' token. */
15908 cp_lexer_consume_token (parser->lexer);
15914 /* Classes [gram.class] */
15916 /* Parse a class-name.
15922 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15923 to indicate that names looked up in dependent types should be
15924 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15925 keyword has been used to indicate that the name that appears next
15926 is a template. TAG_TYPE indicates the explicit tag given before
15927 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15928 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15929 is the class being defined in a class-head.
15931 Returns the TYPE_DECL representing the class. */
15934 cp_parser_class_name (cp_parser *parser,
15935 bool typename_keyword_p,
15936 bool template_keyword_p,
15937 enum tag_types tag_type,
15938 bool check_dependency_p,
15940 bool is_declaration)
15946 tree identifier = NULL_TREE;
15948 /* All class-names start with an identifier. */
15949 token = cp_lexer_peek_token (parser->lexer);
15950 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15952 cp_parser_error (parser, "expected class-name");
15953 return error_mark_node;
15956 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15957 to a template-id, so we save it here. */
15958 scope = parser->scope;
15959 if (scope == error_mark_node)
15960 return error_mark_node;
15962 /* Any name names a type if we're following the `typename' keyword
15963 in a qualified name where the enclosing scope is type-dependent. */
15964 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15965 && dependent_type_p (scope));
15966 /* Handle the common case (an identifier, but not a template-id)
15968 if (token->type == CPP_NAME
15969 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15971 cp_token *identifier_token;
15974 /* Look for the identifier. */
15975 identifier_token = cp_lexer_peek_token (parser->lexer);
15976 ambiguous_p = identifier_token->ambiguous_p;
15977 identifier = cp_parser_identifier (parser);
15978 /* If the next token isn't an identifier, we are certainly not
15979 looking at a class-name. */
15980 if (identifier == error_mark_node)
15981 decl = error_mark_node;
15982 /* If we know this is a type-name, there's no need to look it
15984 else if (typename_p)
15988 tree ambiguous_decls;
15989 /* If we already know that this lookup is ambiguous, then
15990 we've already issued an error message; there's no reason
15994 cp_parser_simulate_error (parser);
15995 return error_mark_node;
15997 /* If the next token is a `::', then the name must be a type
16000 [basic.lookup.qual]
16002 During the lookup for a name preceding the :: scope
16003 resolution operator, object, function, and enumerator
16004 names are ignored. */
16005 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16006 tag_type = typename_type;
16007 /* Look up the name. */
16008 decl = cp_parser_lookup_name (parser, identifier,
16010 /*is_template=*/false,
16011 /*is_namespace=*/false,
16012 check_dependency_p,
16014 identifier_token->location);
16015 if (ambiguous_decls)
16017 if (cp_parser_parsing_tentatively (parser))
16018 cp_parser_simulate_error (parser);
16019 return error_mark_node;
16025 /* Try a template-id. */
16026 decl = cp_parser_template_id (parser, template_keyword_p,
16027 check_dependency_p,
16029 if (decl == error_mark_node)
16030 return error_mark_node;
16033 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16035 /* If this is a typename, create a TYPENAME_TYPE. */
16036 if (typename_p && decl != error_mark_node)
16038 decl = make_typename_type (scope, decl, typename_type,
16039 /*complain=*/tf_error);
16040 if (decl != error_mark_node)
16041 decl = TYPE_NAME (decl);
16044 /* Check to see that it is really the name of a class. */
16045 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16046 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16047 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16048 /* Situations like this:
16050 template <typename T> struct A {
16051 typename T::template X<int>::I i;
16054 are problematic. Is `T::template X<int>' a class-name? The
16055 standard does not seem to be definitive, but there is no other
16056 valid interpretation of the following `::'. Therefore, those
16057 names are considered class-names. */
16059 decl = make_typename_type (scope, decl, tag_type, tf_error);
16060 if (decl != error_mark_node)
16061 decl = TYPE_NAME (decl);
16063 else if (TREE_CODE (decl) != TYPE_DECL
16064 || TREE_TYPE (decl) == error_mark_node
16065 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
16066 decl = error_mark_node;
16068 if (decl == error_mark_node)
16069 cp_parser_error (parser, "expected class-name");
16070 else if (identifier && !parser->scope)
16071 maybe_note_name_used_in_class (identifier, decl);
16076 /* Parse a class-specifier.
16079 class-head { member-specification [opt] }
16081 Returns the TREE_TYPE representing the class. */
16084 cp_parser_class_specifier (cp_parser* parser)
16087 tree attributes = NULL_TREE;
16088 bool nested_name_specifier_p;
16089 unsigned saved_num_template_parameter_lists;
16090 bool saved_in_function_body;
16091 bool saved_in_unbraced_linkage_specification_p;
16092 tree old_scope = NULL_TREE;
16093 tree scope = NULL_TREE;
16096 push_deferring_access_checks (dk_no_deferred);
16098 /* Parse the class-head. */
16099 type = cp_parser_class_head (parser,
16100 &nested_name_specifier_p,
16103 /* If the class-head was a semantic disaster, skip the entire body
16107 cp_parser_skip_to_end_of_block_or_statement (parser);
16108 pop_deferring_access_checks ();
16109 return error_mark_node;
16112 /* Look for the `{'. */
16113 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16115 pop_deferring_access_checks ();
16116 return error_mark_node;
16119 /* Process the base classes. If they're invalid, skip the
16120 entire class body. */
16121 if (!xref_basetypes (type, bases))
16123 /* Consuming the closing brace yields better error messages
16125 if (cp_parser_skip_to_closing_brace (parser))
16126 cp_lexer_consume_token (parser->lexer);
16127 pop_deferring_access_checks ();
16128 return error_mark_node;
16131 /* Issue an error message if type-definitions are forbidden here. */
16132 cp_parser_check_type_definition (parser);
16133 /* Remember that we are defining one more class. */
16134 ++parser->num_classes_being_defined;
16135 /* Inside the class, surrounding template-parameter-lists do not
16137 saved_num_template_parameter_lists
16138 = parser->num_template_parameter_lists;
16139 parser->num_template_parameter_lists = 0;
16140 /* We are not in a function body. */
16141 saved_in_function_body = parser->in_function_body;
16142 parser->in_function_body = false;
16143 /* We are not immediately inside an extern "lang" block. */
16144 saved_in_unbraced_linkage_specification_p
16145 = parser->in_unbraced_linkage_specification_p;
16146 parser->in_unbraced_linkage_specification_p = false;
16148 /* Start the class. */
16149 if (nested_name_specifier_p)
16151 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16152 old_scope = push_inner_scope (scope);
16154 type = begin_class_definition (type, attributes);
16156 if (type == error_mark_node)
16157 /* If the type is erroneous, skip the entire body of the class. */
16158 cp_parser_skip_to_closing_brace (parser);
16160 /* Parse the member-specification. */
16161 cp_parser_member_specification_opt (parser);
16163 /* Look for the trailing `}'. */
16164 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16165 /* Look for trailing attributes to apply to this class. */
16166 if (cp_parser_allow_gnu_extensions_p (parser))
16167 attributes = cp_parser_attributes_opt (parser);
16168 if (type != error_mark_node)
16169 type = finish_struct (type, attributes);
16170 if (nested_name_specifier_p)
16171 pop_inner_scope (old_scope, scope);
16172 /* If this class is not itself within the scope of another class,
16173 then we need to parse the bodies of all of the queued function
16174 definitions. Note that the queued functions defined in a class
16175 are not always processed immediately following the
16176 class-specifier for that class. Consider:
16179 struct B { void f() { sizeof (A); } };
16182 If `f' were processed before the processing of `A' were
16183 completed, there would be no way to compute the size of `A'.
16184 Note that the nesting we are interested in here is lexical --
16185 not the semantic nesting given by TYPE_CONTEXT. In particular,
16188 struct A { struct B; };
16189 struct A::B { void f() { } };
16191 there is no need to delay the parsing of `A::B::f'. */
16192 if (--parser->num_classes_being_defined == 0)
16196 tree class_type = NULL_TREE;
16197 tree pushed_scope = NULL_TREE;
16199 /* In a first pass, parse default arguments to the functions.
16200 Then, in a second pass, parse the bodies of the functions.
16201 This two-phased approach handles cases like:
16209 for (TREE_PURPOSE (parser->unparsed_functions_queues)
16210 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
16211 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
16212 TREE_PURPOSE (parser->unparsed_functions_queues)
16213 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
16215 fn = TREE_VALUE (queue_entry);
16216 /* If there are default arguments that have not yet been processed,
16217 take care of them now. */
16218 if (class_type != TREE_PURPOSE (queue_entry))
16221 pop_scope (pushed_scope);
16222 class_type = TREE_PURPOSE (queue_entry);
16223 pushed_scope = push_scope (class_type);
16225 /* Make sure that any template parameters are in scope. */
16226 maybe_begin_member_template_processing (fn);
16227 /* Parse the default argument expressions. */
16228 cp_parser_late_parsing_default_args (parser, fn);
16229 /* Remove any template parameters from the symbol table. */
16230 maybe_end_member_template_processing ();
16233 pop_scope (pushed_scope);
16234 /* Now parse the body of the functions. */
16235 for (TREE_VALUE (parser->unparsed_functions_queues)
16236 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
16237 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
16238 TREE_VALUE (parser->unparsed_functions_queues)
16239 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
16241 /* Figure out which function we need to process. */
16242 fn = TREE_VALUE (queue_entry);
16243 /* Parse the function. */
16244 cp_parser_late_parsing_for_member (parser, fn);
16248 /* Put back any saved access checks. */
16249 pop_deferring_access_checks ();
16251 /* Restore saved state. */
16252 parser->in_function_body = saved_in_function_body;
16253 parser->num_template_parameter_lists
16254 = saved_num_template_parameter_lists;
16255 parser->in_unbraced_linkage_specification_p
16256 = saved_in_unbraced_linkage_specification_p;
16261 /* Parse a class-head.
16264 class-key identifier [opt] base-clause [opt]
16265 class-key nested-name-specifier identifier base-clause [opt]
16266 class-key nested-name-specifier [opt] template-id
16270 class-key attributes identifier [opt] base-clause [opt]
16271 class-key attributes nested-name-specifier identifier base-clause [opt]
16272 class-key attributes nested-name-specifier [opt] template-id
16275 Upon return BASES is initialized to the list of base classes (or
16276 NULL, if there are none) in the same form returned by
16277 cp_parser_base_clause.
16279 Returns the TYPE of the indicated class. Sets
16280 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16281 involving a nested-name-specifier was used, and FALSE otherwise.
16283 Returns error_mark_node if this is not a class-head.
16285 Returns NULL_TREE if the class-head is syntactically valid, but
16286 semantically invalid in a way that means we should skip the entire
16287 body of the class. */
16290 cp_parser_class_head (cp_parser* parser,
16291 bool* nested_name_specifier_p,
16292 tree *attributes_p,
16295 tree nested_name_specifier;
16296 enum tag_types class_key;
16297 tree id = NULL_TREE;
16298 tree type = NULL_TREE;
16300 bool template_id_p = false;
16301 bool qualified_p = false;
16302 bool invalid_nested_name_p = false;
16303 bool invalid_explicit_specialization_p = false;
16304 tree pushed_scope = NULL_TREE;
16305 unsigned num_templates;
16306 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16307 /* Assume no nested-name-specifier will be present. */
16308 *nested_name_specifier_p = false;
16309 /* Assume no template parameter lists will be used in defining the
16313 *bases = NULL_TREE;
16315 /* Look for the class-key. */
16316 class_key = cp_parser_class_key (parser);
16317 if (class_key == none_type)
16318 return error_mark_node;
16320 /* Parse the attributes. */
16321 attributes = cp_parser_attributes_opt (parser);
16323 /* If the next token is `::', that is invalid -- but sometimes
16324 people do try to write:
16328 Handle this gracefully by accepting the extra qualifier, and then
16329 issuing an error about it later if this really is a
16330 class-head. If it turns out just to be an elaborated type
16331 specifier, remain silent. */
16332 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16333 qualified_p = true;
16335 push_deferring_access_checks (dk_no_check);
16337 /* Determine the name of the class. Begin by looking for an
16338 optional nested-name-specifier. */
16339 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16340 nested_name_specifier
16341 = cp_parser_nested_name_specifier_opt (parser,
16342 /*typename_keyword_p=*/false,
16343 /*check_dependency_p=*/false,
16345 /*is_declaration=*/false);
16346 /* If there was a nested-name-specifier, then there *must* be an
16348 if (nested_name_specifier)
16350 type_start_token = cp_lexer_peek_token (parser->lexer);
16351 /* Although the grammar says `identifier', it really means
16352 `class-name' or `template-name'. You are only allowed to
16353 define a class that has already been declared with this
16356 The proposed resolution for Core Issue 180 says that wherever
16357 you see `class T::X' you should treat `X' as a type-name.
16359 It is OK to define an inaccessible class; for example:
16361 class A { class B; };
16364 We do not know if we will see a class-name, or a
16365 template-name. We look for a class-name first, in case the
16366 class-name is a template-id; if we looked for the
16367 template-name first we would stop after the template-name. */
16368 cp_parser_parse_tentatively (parser);
16369 type = cp_parser_class_name (parser,
16370 /*typename_keyword_p=*/false,
16371 /*template_keyword_p=*/false,
16373 /*check_dependency_p=*/false,
16374 /*class_head_p=*/true,
16375 /*is_declaration=*/false);
16376 /* If that didn't work, ignore the nested-name-specifier. */
16377 if (!cp_parser_parse_definitely (parser))
16379 invalid_nested_name_p = true;
16380 type_start_token = cp_lexer_peek_token (parser->lexer);
16381 id = cp_parser_identifier (parser);
16382 if (id == error_mark_node)
16385 /* If we could not find a corresponding TYPE, treat this
16386 declaration like an unqualified declaration. */
16387 if (type == error_mark_node)
16388 nested_name_specifier = NULL_TREE;
16389 /* Otherwise, count the number of templates used in TYPE and its
16390 containing scopes. */
16395 for (scope = TREE_TYPE (type);
16396 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16397 scope = (TYPE_P (scope)
16398 ? TYPE_CONTEXT (scope)
16399 : DECL_CONTEXT (scope)))
16401 && CLASS_TYPE_P (scope)
16402 && CLASSTYPE_TEMPLATE_INFO (scope)
16403 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16404 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16408 /* Otherwise, the identifier is optional. */
16411 /* We don't know whether what comes next is a template-id,
16412 an identifier, or nothing at all. */
16413 cp_parser_parse_tentatively (parser);
16414 /* Check for a template-id. */
16415 type_start_token = cp_lexer_peek_token (parser->lexer);
16416 id = cp_parser_template_id (parser,
16417 /*template_keyword_p=*/false,
16418 /*check_dependency_p=*/true,
16419 /*is_declaration=*/true);
16420 /* If that didn't work, it could still be an identifier. */
16421 if (!cp_parser_parse_definitely (parser))
16423 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16425 type_start_token = cp_lexer_peek_token (parser->lexer);
16426 id = cp_parser_identifier (parser);
16433 template_id_p = true;
16438 pop_deferring_access_checks ();
16441 cp_parser_check_for_invalid_template_id (parser, id,
16442 type_start_token->location);
16444 /* If it's not a `:' or a `{' then we can't really be looking at a
16445 class-head, since a class-head only appears as part of a
16446 class-specifier. We have to detect this situation before calling
16447 xref_tag, since that has irreversible side-effects. */
16448 if (!cp_parser_next_token_starts_class_definition_p (parser))
16450 cp_parser_error (parser, "expected %<{%> or %<:%>");
16451 return error_mark_node;
16454 /* At this point, we're going ahead with the class-specifier, even
16455 if some other problem occurs. */
16456 cp_parser_commit_to_tentative_parse (parser);
16457 /* Issue the error about the overly-qualified name now. */
16460 cp_parser_error (parser,
16461 "global qualification of class name is invalid");
16462 return error_mark_node;
16464 else if (invalid_nested_name_p)
16466 cp_parser_error (parser,
16467 "qualified name does not name a class");
16468 return error_mark_node;
16470 else if (nested_name_specifier)
16474 /* Reject typedef-names in class heads. */
16475 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16477 error_at (type_start_token->location,
16478 "invalid class name in declaration of %qD",
16484 /* Figure out in what scope the declaration is being placed. */
16485 scope = current_scope ();
16486 /* If that scope does not contain the scope in which the
16487 class was originally declared, the program is invalid. */
16488 if (scope && !is_ancestor (scope, nested_name_specifier))
16490 if (at_namespace_scope_p ())
16491 error_at (type_start_token->location,
16492 "declaration of %qD in namespace %qD which does not "
16494 type, scope, nested_name_specifier);
16496 error_at (type_start_token->location,
16497 "declaration of %qD in %qD which does not enclose %qD",
16498 type, scope, nested_name_specifier);
16504 A declarator-id shall not be qualified except for the
16505 definition of a ... nested class outside of its class
16506 ... [or] the definition or explicit instantiation of a
16507 class member of a namespace outside of its namespace. */
16508 if (scope == nested_name_specifier)
16510 permerror (nested_name_specifier_token_start->location,
16511 "extra qualification not allowed");
16512 nested_name_specifier = NULL_TREE;
16516 /* An explicit-specialization must be preceded by "template <>". If
16517 it is not, try to recover gracefully. */
16518 if (at_namespace_scope_p ()
16519 && parser->num_template_parameter_lists == 0
16522 error_at (type_start_token->location,
16523 "an explicit specialization must be preceded by %<template <>%>");
16524 invalid_explicit_specialization_p = true;
16525 /* Take the same action that would have been taken by
16526 cp_parser_explicit_specialization. */
16527 ++parser->num_template_parameter_lists;
16528 begin_specialization ();
16530 /* There must be no "return" statements between this point and the
16531 end of this function; set "type "to the correct return value and
16532 use "goto done;" to return. */
16533 /* Make sure that the right number of template parameters were
16535 if (!cp_parser_check_template_parameters (parser, num_templates,
16536 type_start_token->location,
16537 /*declarator=*/NULL))
16539 /* If something went wrong, there is no point in even trying to
16540 process the class-definition. */
16545 /* Look up the type. */
16548 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16549 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16550 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16552 error_at (type_start_token->location,
16553 "function template %qD redeclared as a class template", id);
16554 type = error_mark_node;
16558 type = TREE_TYPE (id);
16559 type = maybe_process_partial_specialization (type);
16561 if (nested_name_specifier)
16562 pushed_scope = push_scope (nested_name_specifier);
16564 else if (nested_name_specifier)
16570 template <typename T> struct S { struct T };
16571 template <typename T> struct S<T>::T { };
16573 we will get a TYPENAME_TYPE when processing the definition of
16574 `S::T'. We need to resolve it to the actual type before we
16575 try to define it. */
16576 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16578 class_type = resolve_typename_type (TREE_TYPE (type),
16579 /*only_current_p=*/false);
16580 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16581 type = TYPE_NAME (class_type);
16584 cp_parser_error (parser, "could not resolve typename type");
16585 type = error_mark_node;
16589 if (maybe_process_partial_specialization (TREE_TYPE (type))
16590 == error_mark_node)
16596 class_type = current_class_type;
16597 /* Enter the scope indicated by the nested-name-specifier. */
16598 pushed_scope = push_scope (nested_name_specifier);
16599 /* Get the canonical version of this type. */
16600 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16601 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16602 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16604 type = push_template_decl (type);
16605 if (type == error_mark_node)
16612 type = TREE_TYPE (type);
16613 *nested_name_specifier_p = true;
16615 else /* The name is not a nested name. */
16617 /* If the class was unnamed, create a dummy name. */
16619 id = make_anon_name ();
16620 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16621 parser->num_template_parameter_lists);
16624 /* Indicate whether this class was declared as a `class' or as a
16626 if (TREE_CODE (type) == RECORD_TYPE)
16627 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16628 cp_parser_check_class_key (class_key, type);
16630 /* If this type was already complete, and we see another definition,
16631 that's an error. */
16632 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16634 error_at (type_start_token->location, "redefinition of %q#T",
16636 error_at (type_start_token->location, "previous definition of %q+#T",
16641 else if (type == error_mark_node)
16644 /* We will have entered the scope containing the class; the names of
16645 base classes should be looked up in that context. For example:
16647 struct A { struct B {}; struct C; };
16648 struct A::C : B {};
16652 /* Get the list of base-classes, if there is one. */
16653 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16654 *bases = cp_parser_base_clause (parser);
16657 /* Leave the scope given by the nested-name-specifier. We will
16658 enter the class scope itself while processing the members. */
16660 pop_scope (pushed_scope);
16662 if (invalid_explicit_specialization_p)
16664 end_specialization ();
16665 --parser->num_template_parameter_lists;
16669 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16670 *attributes_p = attributes;
16674 /* Parse a class-key.
16681 Returns the kind of class-key specified, or none_type to indicate
16684 static enum tag_types
16685 cp_parser_class_key (cp_parser* parser)
16688 enum tag_types tag_type;
16690 /* Look for the class-key. */
16691 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
16695 /* Check to see if the TOKEN is a class-key. */
16696 tag_type = cp_parser_token_is_class_key (token);
16698 cp_parser_error (parser, "expected class-key");
16702 /* Parse an (optional) member-specification.
16704 member-specification:
16705 member-declaration member-specification [opt]
16706 access-specifier : member-specification [opt] */
16709 cp_parser_member_specification_opt (cp_parser* parser)
16716 /* Peek at the next token. */
16717 token = cp_lexer_peek_token (parser->lexer);
16718 /* If it's a `}', or EOF then we've seen all the members. */
16719 if (token->type == CPP_CLOSE_BRACE
16720 || token->type == CPP_EOF
16721 || token->type == CPP_PRAGMA_EOL)
16724 /* See if this token is a keyword. */
16725 keyword = token->keyword;
16729 case RID_PROTECTED:
16731 /* Consume the access-specifier. */
16732 cp_lexer_consume_token (parser->lexer);
16733 /* Remember which access-specifier is active. */
16734 current_access_specifier = token->u.value;
16735 /* Look for the `:'. */
16736 cp_parser_require (parser, CPP_COLON, RT_COLON);
16740 /* Accept #pragmas at class scope. */
16741 if (token->type == CPP_PRAGMA)
16743 cp_parser_pragma (parser, pragma_external);
16747 /* Otherwise, the next construction must be a
16748 member-declaration. */
16749 cp_parser_member_declaration (parser);
16754 /* Parse a member-declaration.
16756 member-declaration:
16757 decl-specifier-seq [opt] member-declarator-list [opt] ;
16758 function-definition ; [opt]
16759 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16761 template-declaration
16763 member-declarator-list:
16765 member-declarator-list , member-declarator
16768 declarator pure-specifier [opt]
16769 declarator constant-initializer [opt]
16770 identifier [opt] : constant-expression
16774 member-declaration:
16775 __extension__ member-declaration
16778 declarator attributes [opt] pure-specifier [opt]
16779 declarator attributes [opt] constant-initializer [opt]
16780 identifier [opt] attributes [opt] : constant-expression
16784 member-declaration:
16785 static_assert-declaration */
16788 cp_parser_member_declaration (cp_parser* parser)
16790 cp_decl_specifier_seq decl_specifiers;
16791 tree prefix_attributes;
16793 int declares_class_or_enum;
16795 cp_token *token = NULL;
16796 cp_token *decl_spec_token_start = NULL;
16797 cp_token *initializer_token_start = NULL;
16798 int saved_pedantic;
16800 /* Check for the `__extension__' keyword. */
16801 if (cp_parser_extension_opt (parser, &saved_pedantic))
16804 cp_parser_member_declaration (parser);
16805 /* Restore the old value of the PEDANTIC flag. */
16806 pedantic = saved_pedantic;
16811 /* Check for a template-declaration. */
16812 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16814 /* An explicit specialization here is an error condition, and we
16815 expect the specialization handler to detect and report this. */
16816 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16817 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16818 cp_parser_explicit_specialization (parser);
16820 cp_parser_template_declaration (parser, /*member_p=*/true);
16825 /* Check for a using-declaration. */
16826 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16828 /* Parse the using-declaration. */
16829 cp_parser_using_declaration (parser,
16830 /*access_declaration_p=*/false);
16834 /* Check for @defs. */
16835 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16838 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16839 ivar = ivar_chains;
16843 ivar = TREE_CHAIN (member);
16844 TREE_CHAIN (member) = NULL_TREE;
16845 finish_member_declaration (member);
16850 /* If the next token is `static_assert' we have a static assertion. */
16851 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16853 cp_parser_static_assert (parser, /*member_p=*/true);
16857 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16860 /* Parse the decl-specifier-seq. */
16861 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16862 cp_parser_decl_specifier_seq (parser,
16863 CP_PARSER_FLAGS_OPTIONAL,
16865 &declares_class_or_enum);
16866 prefix_attributes = decl_specifiers.attributes;
16867 decl_specifiers.attributes = NULL_TREE;
16868 /* Check for an invalid type-name. */
16869 if (!decl_specifiers.any_type_specifiers_p
16870 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16872 /* If there is no declarator, then the decl-specifier-seq should
16874 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16876 /* If there was no decl-specifier-seq, and the next token is a
16877 `;', then we have something like:
16883 Each member-declaration shall declare at least one member
16884 name of the class. */
16885 if (!decl_specifiers.any_specifiers_p)
16887 cp_token *token = cp_lexer_peek_token (parser->lexer);
16888 if (!in_system_header_at (token->location))
16889 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16895 /* See if this declaration is a friend. */
16896 friend_p = cp_parser_friend_p (&decl_specifiers);
16897 /* If there were decl-specifiers, check to see if there was
16898 a class-declaration. */
16899 type = check_tag_decl (&decl_specifiers);
16900 /* Nested classes have already been added to the class, but
16901 a `friend' needs to be explicitly registered. */
16904 /* If the `friend' keyword was present, the friend must
16905 be introduced with a class-key. */
16906 if (!declares_class_or_enum)
16907 error_at (decl_spec_token_start->location,
16908 "a class-key must be used when declaring a friend");
16911 template <typename T> struct A {
16912 friend struct A<T>::B;
16915 A<T>::B will be represented by a TYPENAME_TYPE, and
16916 therefore not recognized by check_tag_decl. */
16918 && decl_specifiers.type
16919 && TYPE_P (decl_specifiers.type))
16920 type = decl_specifiers.type;
16921 if (!type || !TYPE_P (type))
16922 error_at (decl_spec_token_start->location,
16923 "friend declaration does not name a class or "
16926 make_friend_class (current_class_type, type,
16927 /*complain=*/true);
16929 /* If there is no TYPE, an error message will already have
16931 else if (!type || type == error_mark_node)
16933 /* An anonymous aggregate has to be handled specially; such
16934 a declaration really declares a data member (with a
16935 particular type), as opposed to a nested class. */
16936 else if (ANON_AGGR_TYPE_P (type))
16938 /* Remove constructors and such from TYPE, now that we
16939 know it is an anonymous aggregate. */
16940 fixup_anonymous_aggr (type);
16941 /* And make the corresponding data member. */
16942 decl = build_decl (decl_spec_token_start->location,
16943 FIELD_DECL, NULL_TREE, type);
16944 /* Add it to the class. */
16945 finish_member_declaration (decl);
16948 cp_parser_check_access_in_redeclaration
16950 decl_spec_token_start->location);
16955 /* See if these declarations will be friends. */
16956 friend_p = cp_parser_friend_p (&decl_specifiers);
16958 /* Keep going until we hit the `;' at the end of the
16960 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16962 tree attributes = NULL_TREE;
16963 tree first_attribute;
16965 /* Peek at the next token. */
16966 token = cp_lexer_peek_token (parser->lexer);
16968 /* Check for a bitfield declaration. */
16969 if (token->type == CPP_COLON
16970 || (token->type == CPP_NAME
16971 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16977 /* Get the name of the bitfield. Note that we cannot just
16978 check TOKEN here because it may have been invalidated by
16979 the call to cp_lexer_peek_nth_token above. */
16980 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16981 identifier = cp_parser_identifier (parser);
16983 identifier = NULL_TREE;
16985 /* Consume the `:' token. */
16986 cp_lexer_consume_token (parser->lexer);
16987 /* Get the width of the bitfield. */
16989 = cp_parser_constant_expression (parser,
16990 /*allow_non_constant=*/false,
16993 /* Look for attributes that apply to the bitfield. */
16994 attributes = cp_parser_attributes_opt (parser);
16995 /* Remember which attributes are prefix attributes and
16997 first_attribute = attributes;
16998 /* Combine the attributes. */
16999 attributes = chainon (prefix_attributes, attributes);
17001 /* Create the bitfield declaration. */
17002 decl = grokbitfield (identifier
17003 ? make_id_declarator (NULL_TREE,
17013 cp_declarator *declarator;
17015 tree asm_specification;
17016 int ctor_dtor_or_conv_p;
17018 /* Parse the declarator. */
17020 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17021 &ctor_dtor_or_conv_p,
17022 /*parenthesized_p=*/NULL,
17023 /*member_p=*/true);
17025 /* If something went wrong parsing the declarator, make sure
17026 that we at least consume some tokens. */
17027 if (declarator == cp_error_declarator)
17029 /* Skip to the end of the statement. */
17030 cp_parser_skip_to_end_of_statement (parser);
17031 /* If the next token is not a semicolon, that is
17032 probably because we just skipped over the body of
17033 a function. So, we consume a semicolon if
17034 present, but do not issue an error message if it
17036 if (cp_lexer_next_token_is (parser->lexer,
17038 cp_lexer_consume_token (parser->lexer);
17042 if (declares_class_or_enum & 2)
17043 cp_parser_check_for_definition_in_return_type
17044 (declarator, decl_specifiers.type,
17045 decl_specifiers.type_location);
17047 /* Look for an asm-specification. */
17048 asm_specification = cp_parser_asm_specification_opt (parser);
17049 /* Look for attributes that apply to the declaration. */
17050 attributes = cp_parser_attributes_opt (parser);
17051 /* Remember which attributes are prefix attributes and
17053 first_attribute = attributes;
17054 /* Combine the attributes. */
17055 attributes = chainon (prefix_attributes, attributes);
17057 /* If it's an `=', then we have a constant-initializer or a
17058 pure-specifier. It is not correct to parse the
17059 initializer before registering the member declaration
17060 since the member declaration should be in scope while
17061 its initializer is processed. However, the rest of the
17062 front end does not yet provide an interface that allows
17063 us to handle this correctly. */
17064 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17068 A pure-specifier shall be used only in the declaration of
17069 a virtual function.
17071 A member-declarator can contain a constant-initializer
17072 only if it declares a static member of integral or
17075 Therefore, if the DECLARATOR is for a function, we look
17076 for a pure-specifier; otherwise, we look for a
17077 constant-initializer. When we call `grokfield', it will
17078 perform more stringent semantics checks. */
17079 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17080 if (function_declarator_p (declarator))
17081 initializer = cp_parser_pure_specifier (parser);
17083 /* Parse the initializer. */
17084 initializer = cp_parser_constant_initializer (parser);
17086 /* Otherwise, there is no initializer. */
17088 initializer = NULL_TREE;
17090 /* See if we are probably looking at a function
17091 definition. We are certainly not looking at a
17092 member-declarator. Calling `grokfield' has
17093 side-effects, so we must not do it unless we are sure
17094 that we are looking at a member-declarator. */
17095 if (cp_parser_token_starts_function_definition_p
17096 (cp_lexer_peek_token (parser->lexer)))
17098 /* The grammar does not allow a pure-specifier to be
17099 used when a member function is defined. (It is
17100 possible that this fact is an oversight in the
17101 standard, since a pure function may be defined
17102 outside of the class-specifier. */
17104 error_at (initializer_token_start->location,
17105 "pure-specifier on function-definition");
17106 decl = cp_parser_save_member_function_body (parser,
17110 /* If the member was not a friend, declare it here. */
17112 finish_member_declaration (decl);
17113 /* Peek at the next token. */
17114 token = cp_lexer_peek_token (parser->lexer);
17115 /* If the next token is a semicolon, consume it. */
17116 if (token->type == CPP_SEMICOLON)
17117 cp_lexer_consume_token (parser->lexer);
17121 if (declarator->kind == cdk_function)
17122 declarator->id_loc = token->location;
17123 /* Create the declaration. */
17124 decl = grokfield (declarator, &decl_specifiers,
17125 initializer, /*init_const_expr_p=*/true,
17130 /* Reset PREFIX_ATTRIBUTES. */
17131 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17132 attributes = TREE_CHAIN (attributes);
17134 TREE_CHAIN (attributes) = NULL_TREE;
17136 /* If there is any qualification still in effect, clear it
17137 now; we will be starting fresh with the next declarator. */
17138 parser->scope = NULL_TREE;
17139 parser->qualifying_scope = NULL_TREE;
17140 parser->object_scope = NULL_TREE;
17141 /* If it's a `,', then there are more declarators. */
17142 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17143 cp_lexer_consume_token (parser->lexer);
17144 /* If the next token isn't a `;', then we have a parse error. */
17145 else if (cp_lexer_next_token_is_not (parser->lexer,
17148 cp_parser_error (parser, "expected %<;%>");
17149 /* Skip tokens until we find a `;'. */
17150 cp_parser_skip_to_end_of_statement (parser);
17157 /* Add DECL to the list of members. */
17159 finish_member_declaration (decl);
17161 if (TREE_CODE (decl) == FUNCTION_DECL)
17162 cp_parser_save_default_args (parser, decl);
17167 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17170 /* Parse a pure-specifier.
17175 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17176 Otherwise, ERROR_MARK_NODE is returned. */
17179 cp_parser_pure_specifier (cp_parser* parser)
17183 /* Look for the `=' token. */
17184 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17185 return error_mark_node;
17186 /* Look for the `0' token. */
17187 token = cp_lexer_peek_token (parser->lexer);
17189 if (token->type == CPP_EOF
17190 || token->type == CPP_PRAGMA_EOL)
17191 return error_mark_node;
17193 cp_lexer_consume_token (parser->lexer);
17195 /* Accept = default or = delete in c++0x mode. */
17196 if (token->keyword == RID_DEFAULT
17197 || token->keyword == RID_DELETE)
17199 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17200 return token->u.value;
17203 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17204 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17206 cp_parser_error (parser,
17207 "invalid pure specifier (only %<= 0%> is allowed)");
17208 cp_parser_skip_to_end_of_statement (parser);
17209 return error_mark_node;
17211 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17213 error_at (token->location, "templates may not be %<virtual%>");
17214 return error_mark_node;
17217 return integer_zero_node;
17220 /* Parse a constant-initializer.
17222 constant-initializer:
17223 = constant-expression
17225 Returns a representation of the constant-expression. */
17228 cp_parser_constant_initializer (cp_parser* parser)
17230 /* Look for the `=' token. */
17231 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17232 return error_mark_node;
17234 /* It is invalid to write:
17236 struct S { static const int i = { 7 }; };
17239 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17241 cp_parser_error (parser,
17242 "a brace-enclosed initializer is not allowed here");
17243 /* Consume the opening brace. */
17244 cp_lexer_consume_token (parser->lexer);
17245 /* Skip the initializer. */
17246 cp_parser_skip_to_closing_brace (parser);
17247 /* Look for the trailing `}'. */
17248 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17250 return error_mark_node;
17253 return cp_parser_constant_expression (parser,
17254 /*allow_non_constant=*/false,
17258 /* Derived classes [gram.class.derived] */
17260 /* Parse a base-clause.
17263 : base-specifier-list
17265 base-specifier-list:
17266 base-specifier ... [opt]
17267 base-specifier-list , base-specifier ... [opt]
17269 Returns a TREE_LIST representing the base-classes, in the order in
17270 which they were declared. The representation of each node is as
17271 described by cp_parser_base_specifier.
17273 In the case that no bases are specified, this function will return
17274 NULL_TREE, not ERROR_MARK_NODE. */
17277 cp_parser_base_clause (cp_parser* parser)
17279 tree bases = NULL_TREE;
17281 /* Look for the `:' that begins the list. */
17282 cp_parser_require (parser, CPP_COLON, RT_COLON);
17284 /* Scan the base-specifier-list. */
17289 bool pack_expansion_p = false;
17291 /* Look for the base-specifier. */
17292 base = cp_parser_base_specifier (parser);
17293 /* Look for the (optional) ellipsis. */
17294 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17296 /* Consume the `...'. */
17297 cp_lexer_consume_token (parser->lexer);
17299 pack_expansion_p = true;
17302 /* Add BASE to the front of the list. */
17303 if (base != error_mark_node)
17305 if (pack_expansion_p)
17306 /* Make this a pack expansion type. */
17307 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17310 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17312 TREE_CHAIN (base) = bases;
17316 /* Peek at the next token. */
17317 token = cp_lexer_peek_token (parser->lexer);
17318 /* If it's not a comma, then the list is complete. */
17319 if (token->type != CPP_COMMA)
17321 /* Consume the `,'. */
17322 cp_lexer_consume_token (parser->lexer);
17325 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17326 base class had a qualified name. However, the next name that
17327 appears is certainly not qualified. */
17328 parser->scope = NULL_TREE;
17329 parser->qualifying_scope = NULL_TREE;
17330 parser->object_scope = NULL_TREE;
17332 return nreverse (bases);
17335 /* Parse a base-specifier.
17338 :: [opt] nested-name-specifier [opt] class-name
17339 virtual access-specifier [opt] :: [opt] nested-name-specifier
17341 access-specifier virtual [opt] :: [opt] nested-name-specifier
17344 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17345 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17346 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17347 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17350 cp_parser_base_specifier (cp_parser* parser)
17354 bool virtual_p = false;
17355 bool duplicate_virtual_error_issued_p = false;
17356 bool duplicate_access_error_issued_p = false;
17357 bool class_scope_p, template_p;
17358 tree access = access_default_node;
17361 /* Process the optional `virtual' and `access-specifier'. */
17364 /* Peek at the next token. */
17365 token = cp_lexer_peek_token (parser->lexer);
17366 /* Process `virtual'. */
17367 switch (token->keyword)
17370 /* If `virtual' appears more than once, issue an error. */
17371 if (virtual_p && !duplicate_virtual_error_issued_p)
17373 cp_parser_error (parser,
17374 "%<virtual%> specified more than once in base-specified");
17375 duplicate_virtual_error_issued_p = true;
17380 /* Consume the `virtual' token. */
17381 cp_lexer_consume_token (parser->lexer);
17386 case RID_PROTECTED:
17388 /* If more than one access specifier appears, issue an
17390 if (access != access_default_node
17391 && !duplicate_access_error_issued_p)
17393 cp_parser_error (parser,
17394 "more than one access specifier in base-specified");
17395 duplicate_access_error_issued_p = true;
17398 access = ridpointers[(int) token->keyword];
17400 /* Consume the access-specifier. */
17401 cp_lexer_consume_token (parser->lexer);
17410 /* It is not uncommon to see programs mechanically, erroneously, use
17411 the 'typename' keyword to denote (dependent) qualified types
17412 as base classes. */
17413 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17415 token = cp_lexer_peek_token (parser->lexer);
17416 if (!processing_template_decl)
17417 error_at (token->location,
17418 "keyword %<typename%> not allowed outside of templates");
17420 error_at (token->location,
17421 "keyword %<typename%> not allowed in this context "
17422 "(the base class is implicitly a type)");
17423 cp_lexer_consume_token (parser->lexer);
17426 /* Look for the optional `::' operator. */
17427 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17428 /* Look for the nested-name-specifier. The simplest way to
17433 The keyword `typename' is not permitted in a base-specifier or
17434 mem-initializer; in these contexts a qualified name that
17435 depends on a template-parameter is implicitly assumed to be a
17438 is to pretend that we have seen the `typename' keyword at this
17440 cp_parser_nested_name_specifier_opt (parser,
17441 /*typename_keyword_p=*/true,
17442 /*check_dependency_p=*/true,
17444 /*is_declaration=*/true);
17445 /* If the base class is given by a qualified name, assume that names
17446 we see are type names or templates, as appropriate. */
17447 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17448 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17450 /* Finally, look for the class-name. */
17451 type = cp_parser_class_name (parser,
17455 /*check_dependency_p=*/true,
17456 /*class_head_p=*/false,
17457 /*is_declaration=*/true);
17459 if (type == error_mark_node)
17460 return error_mark_node;
17462 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17465 /* Exception handling [gram.exception] */
17467 /* Parse an (optional) exception-specification.
17469 exception-specification:
17470 throw ( type-id-list [opt] )
17472 Returns a TREE_LIST representing the exception-specification. The
17473 TREE_VALUE of each node is a type. */
17476 cp_parser_exception_specification_opt (cp_parser* parser)
17481 /* Peek at the next token. */
17482 token = cp_lexer_peek_token (parser->lexer);
17483 /* If it's not `throw', then there's no exception-specification. */
17484 if (!cp_parser_is_keyword (token, RID_THROW))
17487 /* Consume the `throw'. */
17488 cp_lexer_consume_token (parser->lexer);
17490 /* Look for the `('. */
17491 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17493 /* Peek at the next token. */
17494 token = cp_lexer_peek_token (parser->lexer);
17495 /* If it's not a `)', then there is a type-id-list. */
17496 if (token->type != CPP_CLOSE_PAREN)
17498 const char *saved_message;
17500 /* Types may not be defined in an exception-specification. */
17501 saved_message = parser->type_definition_forbidden_message;
17502 parser->type_definition_forbidden_message
17503 = G_("types may not be defined in an exception-specification");
17504 /* Parse the type-id-list. */
17505 type_id_list = cp_parser_type_id_list (parser);
17506 /* Restore the saved message. */
17507 parser->type_definition_forbidden_message = saved_message;
17510 type_id_list = empty_except_spec;
17512 /* Look for the `)'. */
17513 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17515 return type_id_list;
17518 /* Parse an (optional) type-id-list.
17522 type-id-list , type-id ... [opt]
17524 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17525 in the order that the types were presented. */
17528 cp_parser_type_id_list (cp_parser* parser)
17530 tree types = NULL_TREE;
17537 /* Get the next type-id. */
17538 type = cp_parser_type_id (parser);
17539 /* Parse the optional ellipsis. */
17540 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17542 /* Consume the `...'. */
17543 cp_lexer_consume_token (parser->lexer);
17545 /* Turn the type into a pack expansion expression. */
17546 type = make_pack_expansion (type);
17548 /* Add it to the list. */
17549 types = add_exception_specifier (types, type, /*complain=*/1);
17550 /* Peek at the next token. */
17551 token = cp_lexer_peek_token (parser->lexer);
17552 /* If it is not a `,', we are done. */
17553 if (token->type != CPP_COMMA)
17555 /* Consume the `,'. */
17556 cp_lexer_consume_token (parser->lexer);
17559 return nreverse (types);
17562 /* Parse a try-block.
17565 try compound-statement handler-seq */
17568 cp_parser_try_block (cp_parser* parser)
17572 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
17573 try_block = begin_try_block ();
17574 cp_parser_compound_statement (parser, NULL, true);
17575 finish_try_block (try_block);
17576 cp_parser_handler_seq (parser);
17577 finish_handler_sequence (try_block);
17582 /* Parse a function-try-block.
17584 function-try-block:
17585 try ctor-initializer [opt] function-body handler-seq */
17588 cp_parser_function_try_block (cp_parser* parser)
17590 tree compound_stmt;
17592 bool ctor_initializer_p;
17594 /* Look for the `try' keyword. */
17595 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
17597 /* Let the rest of the front end know where we are. */
17598 try_block = begin_function_try_block (&compound_stmt);
17599 /* Parse the function-body. */
17601 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17602 /* We're done with the `try' part. */
17603 finish_function_try_block (try_block);
17604 /* Parse the handlers. */
17605 cp_parser_handler_seq (parser);
17606 /* We're done with the handlers. */
17607 finish_function_handler_sequence (try_block, compound_stmt);
17609 return ctor_initializer_p;
17612 /* Parse a handler-seq.
17615 handler handler-seq [opt] */
17618 cp_parser_handler_seq (cp_parser* parser)
17624 /* Parse the handler. */
17625 cp_parser_handler (parser);
17626 /* Peek at the next token. */
17627 token = cp_lexer_peek_token (parser->lexer);
17628 /* If it's not `catch' then there are no more handlers. */
17629 if (!cp_parser_is_keyword (token, RID_CATCH))
17634 /* Parse a handler.
17637 catch ( exception-declaration ) compound-statement */
17640 cp_parser_handler (cp_parser* parser)
17645 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
17646 handler = begin_handler ();
17647 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17648 declaration = cp_parser_exception_declaration (parser);
17649 finish_handler_parms (declaration, handler);
17650 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17651 cp_parser_compound_statement (parser, NULL, false);
17652 finish_handler (handler);
17655 /* Parse an exception-declaration.
17657 exception-declaration:
17658 type-specifier-seq declarator
17659 type-specifier-seq abstract-declarator
17663 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17664 ellipsis variant is used. */
17667 cp_parser_exception_declaration (cp_parser* parser)
17669 cp_decl_specifier_seq type_specifiers;
17670 cp_declarator *declarator;
17671 const char *saved_message;
17673 /* If it's an ellipsis, it's easy to handle. */
17674 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17676 /* Consume the `...' token. */
17677 cp_lexer_consume_token (parser->lexer);
17681 /* Types may not be defined in exception-declarations. */
17682 saved_message = parser->type_definition_forbidden_message;
17683 parser->type_definition_forbidden_message
17684 = G_("types may not be defined in exception-declarations");
17686 /* Parse the type-specifier-seq. */
17687 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17688 /*is_trailing_return=*/false,
17690 /* If it's a `)', then there is no declarator. */
17691 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17694 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17695 /*ctor_dtor_or_conv_p=*/NULL,
17696 /*parenthesized_p=*/NULL,
17697 /*member_p=*/false);
17699 /* Restore the saved message. */
17700 parser->type_definition_forbidden_message = saved_message;
17702 if (!type_specifiers.any_specifiers_p)
17703 return error_mark_node;
17705 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17708 /* Parse a throw-expression.
17711 throw assignment-expression [opt]
17713 Returns a THROW_EXPR representing the throw-expression. */
17716 cp_parser_throw_expression (cp_parser* parser)
17721 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
17722 token = cp_lexer_peek_token (parser->lexer);
17723 /* Figure out whether or not there is an assignment-expression
17724 following the "throw" keyword. */
17725 if (token->type == CPP_COMMA
17726 || token->type == CPP_SEMICOLON
17727 || token->type == CPP_CLOSE_PAREN
17728 || token->type == CPP_CLOSE_SQUARE
17729 || token->type == CPP_CLOSE_BRACE
17730 || token->type == CPP_COLON)
17731 expression = NULL_TREE;
17733 expression = cp_parser_assignment_expression (parser,
17734 /*cast_p=*/false, NULL);
17736 return build_throw (expression);
17739 /* GNU Extensions */
17741 /* Parse an (optional) asm-specification.
17744 asm ( string-literal )
17746 If the asm-specification is present, returns a STRING_CST
17747 corresponding to the string-literal. Otherwise, returns
17751 cp_parser_asm_specification_opt (cp_parser* parser)
17754 tree asm_specification;
17756 /* Peek at the next token. */
17757 token = cp_lexer_peek_token (parser->lexer);
17758 /* If the next token isn't the `asm' keyword, then there's no
17759 asm-specification. */
17760 if (!cp_parser_is_keyword (token, RID_ASM))
17763 /* Consume the `asm' token. */
17764 cp_lexer_consume_token (parser->lexer);
17765 /* Look for the `('. */
17766 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17768 /* Look for the string-literal. */
17769 asm_specification = cp_parser_string_literal (parser, false, false);
17771 /* Look for the `)'. */
17772 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17774 return asm_specification;
17777 /* Parse an asm-operand-list.
17781 asm-operand-list , asm-operand
17784 string-literal ( expression )
17785 [ string-literal ] string-literal ( expression )
17787 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17788 each node is the expression. The TREE_PURPOSE is itself a
17789 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17790 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17791 is a STRING_CST for the string literal before the parenthesis. Returns
17792 ERROR_MARK_NODE if any of the operands are invalid. */
17795 cp_parser_asm_operand_list (cp_parser* parser)
17797 tree asm_operands = NULL_TREE;
17798 bool invalid_operands = false;
17802 tree string_literal;
17806 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17808 /* Consume the `[' token. */
17809 cp_lexer_consume_token (parser->lexer);
17810 /* Read the operand name. */
17811 name = cp_parser_identifier (parser);
17812 if (name != error_mark_node)
17813 name = build_string (IDENTIFIER_LENGTH (name),
17814 IDENTIFIER_POINTER (name));
17815 /* Look for the closing `]'. */
17816 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17820 /* Look for the string-literal. */
17821 string_literal = cp_parser_string_literal (parser, false, false);
17823 /* Look for the `('. */
17824 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17825 /* Parse the expression. */
17826 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17827 /* Look for the `)'. */
17828 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17830 if (name == error_mark_node
17831 || string_literal == error_mark_node
17832 || expression == error_mark_node)
17833 invalid_operands = true;
17835 /* Add this operand to the list. */
17836 asm_operands = tree_cons (build_tree_list (name, string_literal),
17839 /* If the next token is not a `,', there are no more
17841 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17843 /* Consume the `,'. */
17844 cp_lexer_consume_token (parser->lexer);
17847 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17850 /* Parse an asm-clobber-list.
17854 asm-clobber-list , string-literal
17856 Returns a TREE_LIST, indicating the clobbers in the order that they
17857 appeared. The TREE_VALUE of each node is a STRING_CST. */
17860 cp_parser_asm_clobber_list (cp_parser* parser)
17862 tree clobbers = NULL_TREE;
17866 tree string_literal;
17868 /* Look for the string literal. */
17869 string_literal = cp_parser_string_literal (parser, false, false);
17870 /* Add it to the list. */
17871 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17872 /* If the next token is not a `,', then the list is
17874 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17876 /* Consume the `,' token. */
17877 cp_lexer_consume_token (parser->lexer);
17883 /* Parse an asm-label-list.
17887 asm-label-list , identifier
17889 Returns a TREE_LIST, indicating the labels in the order that they
17890 appeared. The TREE_VALUE of each node is a label. */
17893 cp_parser_asm_label_list (cp_parser* parser)
17895 tree labels = NULL_TREE;
17899 tree identifier, label, name;
17901 /* Look for the identifier. */
17902 identifier = cp_parser_identifier (parser);
17903 if (!error_operand_p (identifier))
17905 label = lookup_label (identifier);
17906 if (TREE_CODE (label) == LABEL_DECL)
17908 TREE_USED (label) = 1;
17909 check_goto (label);
17910 name = build_string (IDENTIFIER_LENGTH (identifier),
17911 IDENTIFIER_POINTER (identifier));
17912 labels = tree_cons (name, label, labels);
17915 /* If the next token is not a `,', then the list is
17917 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17919 /* Consume the `,' token. */
17920 cp_lexer_consume_token (parser->lexer);
17923 return nreverse (labels);
17926 /* Parse an (optional) series of attributes.
17929 attributes attribute
17932 __attribute__ (( attribute-list [opt] ))
17934 The return value is as for cp_parser_attribute_list. */
17937 cp_parser_attributes_opt (cp_parser* parser)
17939 tree attributes = NULL_TREE;
17944 tree attribute_list;
17946 /* Peek at the next token. */
17947 token = cp_lexer_peek_token (parser->lexer);
17948 /* If it's not `__attribute__', then we're done. */
17949 if (token->keyword != RID_ATTRIBUTE)
17952 /* Consume the `__attribute__' keyword. */
17953 cp_lexer_consume_token (parser->lexer);
17954 /* Look for the two `(' tokens. */
17955 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17956 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17958 /* Peek at the next token. */
17959 token = cp_lexer_peek_token (parser->lexer);
17960 if (token->type != CPP_CLOSE_PAREN)
17961 /* Parse the attribute-list. */
17962 attribute_list = cp_parser_attribute_list (parser);
17964 /* If the next token is a `)', then there is no attribute
17966 attribute_list = NULL;
17968 /* Look for the two `)' tokens. */
17969 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17970 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17972 /* Add these new attributes to the list. */
17973 attributes = chainon (attributes, attribute_list);
17979 /* Parse an attribute-list.
17983 attribute-list , attribute
17987 identifier ( identifier )
17988 identifier ( identifier , expression-list )
17989 identifier ( expression-list )
17991 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17992 to an attribute. The TREE_PURPOSE of each node is the identifier
17993 indicating which attribute is in use. The TREE_VALUE represents
17994 the arguments, if any. */
17997 cp_parser_attribute_list (cp_parser* parser)
17999 tree attribute_list = NULL_TREE;
18000 bool save_translate_strings_p = parser->translate_strings_p;
18002 parser->translate_strings_p = false;
18009 /* Look for the identifier. We also allow keywords here; for
18010 example `__attribute__ ((const))' is legal. */
18011 token = cp_lexer_peek_token (parser->lexer);
18012 if (token->type == CPP_NAME
18013 || token->type == CPP_KEYWORD)
18015 tree arguments = NULL_TREE;
18017 /* Consume the token. */
18018 token = cp_lexer_consume_token (parser->lexer);
18020 /* Save away the identifier that indicates which attribute
18022 identifier = (token->type == CPP_KEYWORD)
18023 /* For keywords, use the canonical spelling, not the
18024 parsed identifier. */
18025 ? ridpointers[(int) token->keyword]
18028 attribute = build_tree_list (identifier, NULL_TREE);
18030 /* Peek at the next token. */
18031 token = cp_lexer_peek_token (parser->lexer);
18032 /* If it's an `(', then parse the attribute arguments. */
18033 if (token->type == CPP_OPEN_PAREN)
18036 int attr_flag = (attribute_takes_identifier_p (identifier)
18037 ? id_attr : normal_attr);
18038 vec = cp_parser_parenthesized_expression_list
18039 (parser, attr_flag, /*cast_p=*/false,
18040 /*allow_expansion_p=*/false,
18041 /*non_constant_p=*/NULL);
18043 arguments = error_mark_node;
18046 arguments = build_tree_list_vec (vec);
18047 release_tree_vector (vec);
18049 /* Save the arguments away. */
18050 TREE_VALUE (attribute) = arguments;
18053 if (arguments != error_mark_node)
18055 /* Add this attribute to the list. */
18056 TREE_CHAIN (attribute) = attribute_list;
18057 attribute_list = attribute;
18060 token = cp_lexer_peek_token (parser->lexer);
18062 /* Now, look for more attributes. If the next token isn't a
18063 `,', we're done. */
18064 if (token->type != CPP_COMMA)
18067 /* Consume the comma and keep going. */
18068 cp_lexer_consume_token (parser->lexer);
18070 parser->translate_strings_p = save_translate_strings_p;
18072 /* We built up the list in reverse order. */
18073 return nreverse (attribute_list);
18076 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18077 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18078 current value of the PEDANTIC flag, regardless of whether or not
18079 the `__extension__' keyword is present. The caller is responsible
18080 for restoring the value of the PEDANTIC flag. */
18083 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18085 /* Save the old value of the PEDANTIC flag. */
18086 *saved_pedantic = pedantic;
18088 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18090 /* Consume the `__extension__' token. */
18091 cp_lexer_consume_token (parser->lexer);
18092 /* We're not being pedantic while the `__extension__' keyword is
18102 /* Parse a label declaration.
18105 __label__ label-declarator-seq ;
18107 label-declarator-seq:
18108 identifier , label-declarator-seq
18112 cp_parser_label_declaration (cp_parser* parser)
18114 /* Look for the `__label__' keyword. */
18115 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18121 /* Look for an identifier. */
18122 identifier = cp_parser_identifier (parser);
18123 /* If we failed, stop. */
18124 if (identifier == error_mark_node)
18126 /* Declare it as a label. */
18127 finish_label_decl (identifier);
18128 /* If the next token is a `;', stop. */
18129 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18131 /* Look for the `,' separating the label declarations. */
18132 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18135 /* Look for the final `;'. */
18136 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18139 /* Support Functions */
18141 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18142 NAME should have one of the representations used for an
18143 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18144 is returned. If PARSER->SCOPE is a dependent type, then a
18145 SCOPE_REF is returned.
18147 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18148 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18149 was formed. Abstractly, such entities should not be passed to this
18150 function, because they do not need to be looked up, but it is
18151 simpler to check for this special case here, rather than at the
18154 In cases not explicitly covered above, this function returns a
18155 DECL, OVERLOAD, or baselink representing the result of the lookup.
18156 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18159 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18160 (e.g., "struct") that was used. In that case bindings that do not
18161 refer to types are ignored.
18163 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18166 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18169 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18172 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18173 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18174 NULL_TREE otherwise. */
18177 cp_parser_lookup_name (cp_parser *parser, tree name,
18178 enum tag_types tag_type,
18181 bool check_dependency,
18182 tree *ambiguous_decls,
18183 location_t name_location)
18187 tree object_type = parser->context->object_type;
18189 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18190 flags |= LOOKUP_COMPLAIN;
18192 /* Assume that the lookup will be unambiguous. */
18193 if (ambiguous_decls)
18194 *ambiguous_decls = NULL_TREE;
18196 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18197 no longer valid. Note that if we are parsing tentatively, and
18198 the parse fails, OBJECT_TYPE will be automatically restored. */
18199 parser->context->object_type = NULL_TREE;
18201 if (name == error_mark_node)
18202 return error_mark_node;
18204 /* A template-id has already been resolved; there is no lookup to
18206 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18208 if (BASELINK_P (name))
18210 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
18211 == TEMPLATE_ID_EXPR);
18215 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
18216 it should already have been checked to make sure that the name
18217 used matches the type being destroyed. */
18218 if (TREE_CODE (name) == BIT_NOT_EXPR)
18222 /* Figure out to which type this destructor applies. */
18224 type = parser->scope;
18225 else if (object_type)
18226 type = object_type;
18228 type = current_class_type;
18229 /* If that's not a class type, there is no destructor. */
18230 if (!type || !CLASS_TYPE_P (type))
18231 return error_mark_node;
18232 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
18233 lazily_declare_fn (sfk_destructor, type);
18234 if (!CLASSTYPE_DESTRUCTORS (type))
18235 return error_mark_node;
18236 /* If it was a class type, return the destructor. */
18237 return CLASSTYPE_DESTRUCTORS (type);
18240 /* By this point, the NAME should be an ordinary identifier. If
18241 the id-expression was a qualified name, the qualifying scope is
18242 stored in PARSER->SCOPE at this point. */
18243 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
18245 /* Perform the lookup. */
18250 if (parser->scope == error_mark_node)
18251 return error_mark_node;
18253 /* If the SCOPE is dependent, the lookup must be deferred until
18254 the template is instantiated -- unless we are explicitly
18255 looking up names in uninstantiated templates. Even then, we
18256 cannot look up the name if the scope is not a class type; it
18257 might, for example, be a template type parameter. */
18258 dependent_p = (TYPE_P (parser->scope)
18259 && dependent_scope_p (parser->scope));
18260 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18262 /* Defer lookup. */
18263 decl = error_mark_node;
18266 tree pushed_scope = NULL_TREE;
18268 /* If PARSER->SCOPE is a dependent type, then it must be a
18269 class type, and we must not be checking dependencies;
18270 otherwise, we would have processed this lookup above. So
18271 that PARSER->SCOPE is not considered a dependent base by
18272 lookup_member, we must enter the scope here. */
18274 pushed_scope = push_scope (parser->scope);
18276 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18277 lookup result and the nested-name-specifier nominates a class C:
18278 * if the name specified after the nested-name-specifier, when
18279 looked up in C, is the injected-class-name of C (Clause 9), or
18280 * if the name specified after the nested-name-specifier is the
18281 same as the identifier or the simple-template-id's template-
18282 name in the last component of the nested-name-specifier,
18283 the name is instead considered to name the constructor of
18284 class C. [ Note: for example, the constructor is not an
18285 acceptable lookup result in an elaborated-type-specifier so
18286 the constructor would not be used in place of the
18287 injected-class-name. --end note ] Such a constructor name
18288 shall be used only in the declarator-id of a declaration that
18289 names a constructor or in a using-declaration. */
18290 if (tag_type == none_type
18291 && CLASS_TYPE_P (parser->scope)
18292 && constructor_name_p (name, parser->scope))
18293 name = ctor_identifier;
18295 /* If the PARSER->SCOPE is a template specialization, it
18296 may be instantiated during name lookup. In that case,
18297 errors may be issued. Even if we rollback the current
18298 tentative parse, those errors are valid. */
18299 decl = lookup_qualified_name (parser->scope, name,
18300 tag_type != none_type,
18301 /*complain=*/true);
18303 /* If we have a single function from a using decl, pull it out. */
18304 if (TREE_CODE (decl) == OVERLOAD
18305 && !really_overloaded_fn (decl))
18306 decl = OVL_FUNCTION (decl);
18309 pop_scope (pushed_scope);
18312 /* If the scope is a dependent type and either we deferred lookup or
18313 we did lookup but didn't find the name, rememeber the name. */
18314 if (decl == error_mark_node && TYPE_P (parser->scope)
18315 && dependent_type_p (parser->scope))
18321 /* The resolution to Core Issue 180 says that `struct
18322 A::B' should be considered a type-name, even if `A'
18324 type = make_typename_type (parser->scope, name, tag_type,
18325 /*complain=*/tf_error);
18326 decl = TYPE_NAME (type);
18328 else if (is_template
18329 && (cp_parser_next_token_ends_template_argument_p (parser)
18330 || cp_lexer_next_token_is (parser->lexer,
18332 decl = make_unbound_class_template (parser->scope,
18334 /*complain=*/tf_error);
18336 decl = build_qualified_name (/*type=*/NULL_TREE,
18337 parser->scope, name,
18340 parser->qualifying_scope = parser->scope;
18341 parser->object_scope = NULL_TREE;
18343 else if (object_type)
18345 tree object_decl = NULL_TREE;
18346 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18347 OBJECT_TYPE is not a class. */
18348 if (CLASS_TYPE_P (object_type))
18349 /* If the OBJECT_TYPE is a template specialization, it may
18350 be instantiated during name lookup. In that case, errors
18351 may be issued. Even if we rollback the current tentative
18352 parse, those errors are valid. */
18353 object_decl = lookup_member (object_type,
18356 tag_type != none_type);
18357 /* Look it up in the enclosing context, too. */
18358 decl = lookup_name_real (name, tag_type != none_type,
18360 /*block_p=*/true, is_namespace, flags);
18361 parser->object_scope = object_type;
18362 parser->qualifying_scope = NULL_TREE;
18364 decl = object_decl;
18368 decl = lookup_name_real (name, tag_type != none_type,
18370 /*block_p=*/true, is_namespace, flags);
18371 parser->qualifying_scope = NULL_TREE;
18372 parser->object_scope = NULL_TREE;
18375 /* If the lookup failed, let our caller know. */
18376 if (!decl || decl == error_mark_node)
18377 return error_mark_node;
18379 /* Pull out the template from an injected-class-name (or multiple). */
18381 decl = maybe_get_template_decl_from_type_decl (decl);
18383 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18384 if (TREE_CODE (decl) == TREE_LIST)
18386 if (ambiguous_decls)
18387 *ambiguous_decls = decl;
18388 /* The error message we have to print is too complicated for
18389 cp_parser_error, so we incorporate its actions directly. */
18390 if (!cp_parser_simulate_error (parser))
18392 error_at (name_location, "reference to %qD is ambiguous",
18394 print_candidates (decl);
18396 return error_mark_node;
18399 gcc_assert (DECL_P (decl)
18400 || TREE_CODE (decl) == OVERLOAD
18401 || TREE_CODE (decl) == SCOPE_REF
18402 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18403 || BASELINK_P (decl));
18405 /* If we have resolved the name of a member declaration, check to
18406 see if the declaration is accessible. When the name resolves to
18407 set of overloaded functions, accessibility is checked when
18408 overload resolution is done.
18410 During an explicit instantiation, access is not checked at all,
18411 as per [temp.explicit]. */
18413 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18418 /* Like cp_parser_lookup_name, but for use in the typical case where
18419 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18420 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18423 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18425 return cp_parser_lookup_name (parser, name,
18427 /*is_template=*/false,
18428 /*is_namespace=*/false,
18429 /*check_dependency=*/true,
18430 /*ambiguous_decls=*/NULL,
18434 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18435 the current context, return the TYPE_DECL. If TAG_NAME_P is
18436 true, the DECL indicates the class being defined in a class-head,
18437 or declared in an elaborated-type-specifier.
18439 Otherwise, return DECL. */
18442 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18444 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18445 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18448 template <typename T> struct B;
18451 template <typename T> struct A::B {};
18453 Similarly, in an elaborated-type-specifier:
18455 namespace N { struct X{}; }
18458 template <typename T> friend struct N::X;
18461 However, if the DECL refers to a class type, and we are in
18462 the scope of the class, then the name lookup automatically
18463 finds the TYPE_DECL created by build_self_reference rather
18464 than a TEMPLATE_DECL. For example, in:
18466 template <class T> struct S {
18470 there is no need to handle such case. */
18472 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18473 return DECL_TEMPLATE_RESULT (decl);
18478 /* If too many, or too few, template-parameter lists apply to the
18479 declarator, issue an error message. Returns TRUE if all went well,
18480 and FALSE otherwise. */
18483 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18484 cp_declarator *declarator,
18485 location_t declarator_location)
18487 unsigned num_templates;
18489 /* We haven't seen any classes that involve template parameters yet. */
18492 switch (declarator->kind)
18495 if (declarator->u.id.qualifying_scope)
18499 scope = declarator->u.id.qualifying_scope;
18501 while (scope && CLASS_TYPE_P (scope))
18503 /* You're supposed to have one `template <...>'
18504 for every template class, but you don't need one
18505 for a full specialization. For example:
18507 template <class T> struct S{};
18508 template <> struct S<int> { void f(); };
18509 void S<int>::f () {}
18511 is correct; there shouldn't be a `template <>' for
18512 the definition of `S<int>::f'. */
18513 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18514 /* If SCOPE does not have template information of any
18515 kind, then it is not a template, nor is it nested
18516 within a template. */
18518 if (explicit_class_specialization_p (scope))
18520 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18523 scope = TYPE_CONTEXT (scope);
18526 else if (TREE_CODE (declarator->u.id.unqualified_name)
18527 == TEMPLATE_ID_EXPR)
18528 /* If the DECLARATOR has the form `X<y>' then it uses one
18529 additional level of template parameters. */
18532 return cp_parser_check_template_parameters
18533 (parser, num_templates, declarator_location, declarator);
18539 case cdk_reference:
18541 return (cp_parser_check_declarator_template_parameters
18542 (parser, declarator->declarator, declarator_location));
18548 gcc_unreachable ();
18553 /* NUM_TEMPLATES were used in the current declaration. If that is
18554 invalid, return FALSE and issue an error messages. Otherwise,
18555 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18556 declarator and we can print more accurate diagnostics. */
18559 cp_parser_check_template_parameters (cp_parser* parser,
18560 unsigned num_templates,
18561 location_t location,
18562 cp_declarator *declarator)
18564 /* If there are the same number of template classes and parameter
18565 lists, that's OK. */
18566 if (parser->num_template_parameter_lists == num_templates)
18568 /* If there are more, but only one more, then we are referring to a
18569 member template. That's OK too. */
18570 if (parser->num_template_parameter_lists == num_templates + 1)
18572 /* If there are more template classes than parameter lists, we have
18575 template <class T> void S<T>::R<T>::f (); */
18576 if (parser->num_template_parameter_lists < num_templates)
18578 if (declarator && !current_function_decl)
18579 error_at (location, "specializing member %<%T::%E%> "
18580 "requires %<template<>%> syntax",
18581 declarator->u.id.qualifying_scope,
18582 declarator->u.id.unqualified_name);
18583 else if (declarator)
18584 error_at (location, "invalid declaration of %<%T::%E%>",
18585 declarator->u.id.qualifying_scope,
18586 declarator->u.id.unqualified_name);
18588 error_at (location, "too few template-parameter-lists");
18591 /* Otherwise, there are too many template parameter lists. We have
18594 template <class T> template <class U> void S::f(); */
18595 error_at (location, "too many template-parameter-lists");
18599 /* Parse an optional `::' token indicating that the following name is
18600 from the global namespace. If so, PARSER->SCOPE is set to the
18601 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18602 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18603 Returns the new value of PARSER->SCOPE, if the `::' token is
18604 present, and NULL_TREE otherwise. */
18607 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18611 /* Peek at the next token. */
18612 token = cp_lexer_peek_token (parser->lexer);
18613 /* If we're looking at a `::' token then we're starting from the
18614 global namespace, not our current location. */
18615 if (token->type == CPP_SCOPE)
18617 /* Consume the `::' token. */
18618 cp_lexer_consume_token (parser->lexer);
18619 /* Set the SCOPE so that we know where to start the lookup. */
18620 parser->scope = global_namespace;
18621 parser->qualifying_scope = global_namespace;
18622 parser->object_scope = NULL_TREE;
18624 return parser->scope;
18626 else if (!current_scope_valid_p)
18628 parser->scope = NULL_TREE;
18629 parser->qualifying_scope = NULL_TREE;
18630 parser->object_scope = NULL_TREE;
18636 /* Returns TRUE if the upcoming token sequence is the start of a
18637 constructor declarator. If FRIEND_P is true, the declarator is
18638 preceded by the `friend' specifier. */
18641 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18643 bool constructor_p;
18644 tree nested_name_specifier;
18645 cp_token *next_token;
18647 /* The common case is that this is not a constructor declarator, so
18648 try to avoid doing lots of work if at all possible. It's not
18649 valid declare a constructor at function scope. */
18650 if (parser->in_function_body)
18652 /* And only certain tokens can begin a constructor declarator. */
18653 next_token = cp_lexer_peek_token (parser->lexer);
18654 if (next_token->type != CPP_NAME
18655 && next_token->type != CPP_SCOPE
18656 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18657 && next_token->type != CPP_TEMPLATE_ID)
18660 /* Parse tentatively; we are going to roll back all of the tokens
18662 cp_parser_parse_tentatively (parser);
18663 /* Assume that we are looking at a constructor declarator. */
18664 constructor_p = true;
18666 /* Look for the optional `::' operator. */
18667 cp_parser_global_scope_opt (parser,
18668 /*current_scope_valid_p=*/false);
18669 /* Look for the nested-name-specifier. */
18670 nested_name_specifier
18671 = (cp_parser_nested_name_specifier_opt (parser,
18672 /*typename_keyword_p=*/false,
18673 /*check_dependency_p=*/false,
18675 /*is_declaration=*/false));
18676 /* Outside of a class-specifier, there must be a
18677 nested-name-specifier. */
18678 if (!nested_name_specifier &&
18679 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18681 constructor_p = false;
18682 else if (nested_name_specifier == error_mark_node)
18683 constructor_p = false;
18685 /* If we have a class scope, this is easy; DR 147 says that S::S always
18686 names the constructor, and no other qualified name could. */
18687 if (constructor_p && nested_name_specifier
18688 && TYPE_P (nested_name_specifier))
18690 tree id = cp_parser_unqualified_id (parser,
18691 /*template_keyword_p=*/false,
18692 /*check_dependency_p=*/false,
18693 /*declarator_p=*/true,
18694 /*optional_p=*/false);
18695 if (is_overloaded_fn (id))
18696 id = DECL_NAME (get_first_fn (id));
18697 if (!constructor_name_p (id, nested_name_specifier))
18698 constructor_p = false;
18700 /* If we still think that this might be a constructor-declarator,
18701 look for a class-name. */
18702 else if (constructor_p)
18706 template <typename T> struct S {
18710 we must recognize that the nested `S' names a class. */
18712 type_decl = cp_parser_class_name (parser,
18713 /*typename_keyword_p=*/false,
18714 /*template_keyword_p=*/false,
18716 /*check_dependency_p=*/false,
18717 /*class_head_p=*/false,
18718 /*is_declaration=*/false);
18719 /* If there was no class-name, then this is not a constructor. */
18720 constructor_p = !cp_parser_error_occurred (parser);
18722 /* If we're still considering a constructor, we have to see a `(',
18723 to begin the parameter-declaration-clause, followed by either a
18724 `)', an `...', or a decl-specifier. We need to check for a
18725 type-specifier to avoid being fooled into thinking that:
18729 is a constructor. (It is actually a function named `f' that
18730 takes one parameter (of type `int') and returns a value of type
18733 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18734 constructor_p = false;
18737 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18738 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18739 /* A parameter declaration begins with a decl-specifier,
18740 which is either the "attribute" keyword, a storage class
18741 specifier, or (usually) a type-specifier. */
18742 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18745 tree pushed_scope = NULL_TREE;
18746 unsigned saved_num_template_parameter_lists;
18748 /* Names appearing in the type-specifier should be looked up
18749 in the scope of the class. */
18750 if (current_class_type)
18754 type = TREE_TYPE (type_decl);
18755 if (TREE_CODE (type) == TYPENAME_TYPE)
18757 type = resolve_typename_type (type,
18758 /*only_current_p=*/false);
18759 if (TREE_CODE (type) == TYPENAME_TYPE)
18761 cp_parser_abort_tentative_parse (parser);
18765 pushed_scope = push_scope (type);
18768 /* Inside the constructor parameter list, surrounding
18769 template-parameter-lists do not apply. */
18770 saved_num_template_parameter_lists
18771 = parser->num_template_parameter_lists;
18772 parser->num_template_parameter_lists = 0;
18774 /* Look for the type-specifier. */
18775 cp_parser_type_specifier (parser,
18776 CP_PARSER_FLAGS_NONE,
18777 /*decl_specs=*/NULL,
18778 /*is_declarator=*/true,
18779 /*declares_class_or_enum=*/NULL,
18780 /*is_cv_qualifier=*/NULL);
18782 parser->num_template_parameter_lists
18783 = saved_num_template_parameter_lists;
18785 /* Leave the scope of the class. */
18787 pop_scope (pushed_scope);
18789 constructor_p = !cp_parser_error_occurred (parser);
18793 /* We did not really want to consume any tokens. */
18794 cp_parser_abort_tentative_parse (parser);
18796 return constructor_p;
18799 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18800 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18801 they must be performed once we are in the scope of the function.
18803 Returns the function defined. */
18806 cp_parser_function_definition_from_specifiers_and_declarator
18807 (cp_parser* parser,
18808 cp_decl_specifier_seq *decl_specifiers,
18810 const cp_declarator *declarator)
18815 /* Begin the function-definition. */
18816 success_p = start_function (decl_specifiers, declarator, attributes);
18818 /* The things we're about to see are not directly qualified by any
18819 template headers we've seen thus far. */
18820 reset_specialization ();
18822 /* If there were names looked up in the decl-specifier-seq that we
18823 did not check, check them now. We must wait until we are in the
18824 scope of the function to perform the checks, since the function
18825 might be a friend. */
18826 perform_deferred_access_checks ();
18830 /* Skip the entire function. */
18831 cp_parser_skip_to_end_of_block_or_statement (parser);
18832 fn = error_mark_node;
18834 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18836 /* Seen already, skip it. An error message has already been output. */
18837 cp_parser_skip_to_end_of_block_or_statement (parser);
18838 fn = current_function_decl;
18839 current_function_decl = NULL_TREE;
18840 /* If this is a function from a class, pop the nested class. */
18841 if (current_class_name)
18842 pop_nested_class ();
18845 fn = cp_parser_function_definition_after_declarator (parser,
18846 /*inline_p=*/false);
18851 /* Parse the part of a function-definition that follows the
18852 declarator. INLINE_P is TRUE iff this function is an inline
18853 function defined within a class-specifier.
18855 Returns the function defined. */
18858 cp_parser_function_definition_after_declarator (cp_parser* parser,
18862 bool ctor_initializer_p = false;
18863 bool saved_in_unbraced_linkage_specification_p;
18864 bool saved_in_function_body;
18865 unsigned saved_num_template_parameter_lists;
18868 saved_in_function_body = parser->in_function_body;
18869 parser->in_function_body = true;
18870 /* If the next token is `return', then the code may be trying to
18871 make use of the "named return value" extension that G++ used to
18873 token = cp_lexer_peek_token (parser->lexer);
18874 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18876 /* Consume the `return' keyword. */
18877 cp_lexer_consume_token (parser->lexer);
18878 /* Look for the identifier that indicates what value is to be
18880 cp_parser_identifier (parser);
18881 /* Issue an error message. */
18882 error_at (token->location,
18883 "named return values are no longer supported");
18884 /* Skip tokens until we reach the start of the function body. */
18887 cp_token *token = cp_lexer_peek_token (parser->lexer);
18888 if (token->type == CPP_OPEN_BRACE
18889 || token->type == CPP_EOF
18890 || token->type == CPP_PRAGMA_EOL)
18892 cp_lexer_consume_token (parser->lexer);
18895 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18896 anything declared inside `f'. */
18897 saved_in_unbraced_linkage_specification_p
18898 = parser->in_unbraced_linkage_specification_p;
18899 parser->in_unbraced_linkage_specification_p = false;
18900 /* Inside the function, surrounding template-parameter-lists do not
18902 saved_num_template_parameter_lists
18903 = parser->num_template_parameter_lists;
18904 parser->num_template_parameter_lists = 0;
18906 start_lambda_scope (current_function_decl);
18908 /* If the next token is `try', then we are looking at a
18909 function-try-block. */
18910 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18911 ctor_initializer_p = cp_parser_function_try_block (parser);
18912 /* A function-try-block includes the function-body, so we only do
18913 this next part if we're not processing a function-try-block. */
18916 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18918 finish_lambda_scope ();
18920 /* Finish the function. */
18921 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18922 (inline_p ? 2 : 0));
18923 /* Generate code for it, if necessary. */
18924 expand_or_defer_fn (fn);
18925 /* Restore the saved values. */
18926 parser->in_unbraced_linkage_specification_p
18927 = saved_in_unbraced_linkage_specification_p;
18928 parser->num_template_parameter_lists
18929 = saved_num_template_parameter_lists;
18930 parser->in_function_body = saved_in_function_body;
18935 /* Parse a template-declaration, assuming that the `export' (and
18936 `extern') keywords, if present, has already been scanned. MEMBER_P
18937 is as for cp_parser_template_declaration. */
18940 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18942 tree decl = NULL_TREE;
18943 VEC (deferred_access_check,gc) *checks;
18944 tree parameter_list;
18945 bool friend_p = false;
18946 bool need_lang_pop;
18949 /* Look for the `template' keyword. */
18950 token = cp_lexer_peek_token (parser->lexer);
18951 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
18955 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
18957 if (at_class_scope_p () && current_function_decl)
18959 /* 14.5.2.2 [temp.mem]
18961 A local class shall not have member templates. */
18962 error_at (token->location,
18963 "invalid declaration of member template in local class");
18964 cp_parser_skip_to_end_of_block_or_statement (parser);
18969 A template ... shall not have C linkage. */
18970 if (current_lang_name == lang_name_c)
18972 error_at (token->location, "template with C linkage");
18973 /* Give it C++ linkage to avoid confusing other parts of the
18975 push_lang_context (lang_name_cplusplus);
18976 need_lang_pop = true;
18979 need_lang_pop = false;
18981 /* We cannot perform access checks on the template parameter
18982 declarations until we know what is being declared, just as we
18983 cannot check the decl-specifier list. */
18984 push_deferring_access_checks (dk_deferred);
18986 /* If the next token is `>', then we have an invalid
18987 specialization. Rather than complain about an invalid template
18988 parameter, issue an error message here. */
18989 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18991 cp_parser_error (parser, "invalid explicit specialization");
18992 begin_specialization ();
18993 parameter_list = NULL_TREE;
18996 /* Parse the template parameters. */
18997 parameter_list = cp_parser_template_parameter_list (parser);
18999 /* Get the deferred access checks from the parameter list. These
19000 will be checked once we know what is being declared, as for a
19001 member template the checks must be performed in the scope of the
19002 class containing the member. */
19003 checks = get_deferred_access_checks ();
19005 /* Look for the `>'. */
19006 cp_parser_skip_to_end_of_template_parameter_list (parser);
19007 /* We just processed one more parameter list. */
19008 ++parser->num_template_parameter_lists;
19009 /* If the next token is `template', there are more template
19011 if (cp_lexer_next_token_is_keyword (parser->lexer,
19013 cp_parser_template_declaration_after_export (parser, member_p);
19016 /* There are no access checks when parsing a template, as we do not
19017 know if a specialization will be a friend. */
19018 push_deferring_access_checks (dk_no_check);
19019 token = cp_lexer_peek_token (parser->lexer);
19020 decl = cp_parser_single_declaration (parser,
19023 /*explicit_specialization_p=*/false,
19025 pop_deferring_access_checks ();
19027 /* If this is a member template declaration, let the front
19029 if (member_p && !friend_p && decl)
19031 if (TREE_CODE (decl) == TYPE_DECL)
19032 cp_parser_check_access_in_redeclaration (decl, token->location);
19034 decl = finish_member_template_decl (decl);
19036 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19037 make_friend_class (current_class_type, TREE_TYPE (decl),
19038 /*complain=*/true);
19040 /* We are done with the current parameter list. */
19041 --parser->num_template_parameter_lists;
19043 pop_deferring_access_checks ();
19046 finish_template_decl (parameter_list);
19048 /* Register member declarations. */
19049 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19050 finish_member_declaration (decl);
19051 /* For the erroneous case of a template with C linkage, we pushed an
19052 implicit C++ linkage scope; exit that scope now. */
19054 pop_lang_context ();
19055 /* If DECL is a function template, we must return to parse it later.
19056 (Even though there is no definition, there might be default
19057 arguments that need handling.) */
19058 if (member_p && decl
19059 && (TREE_CODE (decl) == FUNCTION_DECL
19060 || DECL_FUNCTION_TEMPLATE_P (decl)))
19061 TREE_VALUE (parser->unparsed_functions_queues)
19062 = tree_cons (NULL_TREE, decl,
19063 TREE_VALUE (parser->unparsed_functions_queues));
19066 /* Perform the deferred access checks from a template-parameter-list.
19067 CHECKS is a TREE_LIST of access checks, as returned by
19068 get_deferred_access_checks. */
19071 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19073 ++processing_template_parmlist;
19074 perform_access_checks (checks);
19075 --processing_template_parmlist;
19078 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19079 `function-definition' sequence. MEMBER_P is true, this declaration
19080 appears in a class scope.
19082 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19083 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19086 cp_parser_single_declaration (cp_parser* parser,
19087 VEC (deferred_access_check,gc)* checks,
19089 bool explicit_specialization_p,
19092 int declares_class_or_enum;
19093 tree decl = NULL_TREE;
19094 cp_decl_specifier_seq decl_specifiers;
19095 bool function_definition_p = false;
19096 cp_token *decl_spec_token_start;
19098 /* This function is only used when processing a template
19100 gcc_assert (innermost_scope_kind () == sk_template_parms
19101 || innermost_scope_kind () == sk_template_spec);
19103 /* Defer access checks until we know what is being declared. */
19104 push_deferring_access_checks (dk_deferred);
19106 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19108 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19109 cp_parser_decl_specifier_seq (parser,
19110 CP_PARSER_FLAGS_OPTIONAL,
19112 &declares_class_or_enum);
19114 *friend_p = cp_parser_friend_p (&decl_specifiers);
19116 /* There are no template typedefs. */
19117 if (decl_specifiers.specs[(int) ds_typedef])
19119 error_at (decl_spec_token_start->location,
19120 "template declaration of %<typedef%>");
19121 decl = error_mark_node;
19124 /* Gather up the access checks that occurred the
19125 decl-specifier-seq. */
19126 stop_deferring_access_checks ();
19128 /* Check for the declaration of a template class. */
19129 if (declares_class_or_enum)
19131 if (cp_parser_declares_only_class_p (parser))
19133 decl = shadow_tag (&decl_specifiers);
19138 friend template <typename T> struct A<T>::B;
19141 A<T>::B will be represented by a TYPENAME_TYPE, and
19142 therefore not recognized by shadow_tag. */
19143 if (friend_p && *friend_p
19145 && decl_specifiers.type
19146 && TYPE_P (decl_specifiers.type))
19147 decl = decl_specifiers.type;
19149 if (decl && decl != error_mark_node)
19150 decl = TYPE_NAME (decl);
19152 decl = error_mark_node;
19154 /* Perform access checks for template parameters. */
19155 cp_parser_perform_template_parameter_access_checks (checks);
19159 /* Complain about missing 'typename' or other invalid type names. */
19160 if (!decl_specifiers.any_type_specifiers_p)
19161 cp_parser_parse_and_diagnose_invalid_type_name (parser);
19163 /* If it's not a template class, try for a template function. If
19164 the next token is a `;', then this declaration does not declare
19165 anything. But, if there were errors in the decl-specifiers, then
19166 the error might well have come from an attempted class-specifier.
19167 In that case, there's no need to warn about a missing declarator. */
19169 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19170 || decl_specifiers.type != error_mark_node))
19172 decl = cp_parser_init_declarator (parser,
19175 /*function_definition_allowed_p=*/true,
19177 declares_class_or_enum,
19178 &function_definition_p);
19180 /* 7.1.1-1 [dcl.stc]
19182 A storage-class-specifier shall not be specified in an explicit
19183 specialization... */
19185 && explicit_specialization_p
19186 && decl_specifiers.storage_class != sc_none)
19188 error_at (decl_spec_token_start->location,
19189 "explicit template specialization cannot have a storage class");
19190 decl = error_mark_node;
19194 pop_deferring_access_checks ();
19196 /* Clear any current qualification; whatever comes next is the start
19197 of something new. */
19198 parser->scope = NULL_TREE;
19199 parser->qualifying_scope = NULL_TREE;
19200 parser->object_scope = NULL_TREE;
19201 /* Look for a trailing `;' after the declaration. */
19202 if (!function_definition_p
19203 && (decl == error_mark_node
19204 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19205 cp_parser_skip_to_end_of_block_or_statement (parser);
19210 /* Parse a cast-expression that is not the operand of a unary "&". */
19213 cp_parser_simple_cast_expression (cp_parser *parser)
19215 return cp_parser_cast_expression (parser, /*address_p=*/false,
19216 /*cast_p=*/false, NULL);
19219 /* Parse a functional cast to TYPE. Returns an expression
19220 representing the cast. */
19223 cp_parser_functional_cast (cp_parser* parser, tree type)
19226 tree expression_list;
19230 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19232 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19233 expression_list = cp_parser_braced_list (parser, &nonconst_p);
19234 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
19235 if (TREE_CODE (type) == TYPE_DECL)
19236 type = TREE_TYPE (type);
19237 return finish_compound_literal (type, expression_list);
19241 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19243 /*allow_expansion_p=*/true,
19244 /*non_constant_p=*/NULL);
19246 expression_list = error_mark_node;
19249 expression_list = build_tree_list_vec (vec);
19250 release_tree_vector (vec);
19253 cast = build_functional_cast (type, expression_list,
19254 tf_warning_or_error);
19255 /* [expr.const]/1: In an integral constant expression "only type
19256 conversions to integral or enumeration type can be used". */
19257 if (TREE_CODE (type) == TYPE_DECL)
19258 type = TREE_TYPE (type);
19259 if (cast != error_mark_node
19260 && !cast_valid_in_integral_constant_expression_p (type)
19261 && (cp_parser_non_integral_constant_expression (parser,
19263 return error_mark_node;
19267 /* Save the tokens that make up the body of a member function defined
19268 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19269 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19270 specifiers applied to the declaration. Returns the FUNCTION_DECL
19271 for the member function. */
19274 cp_parser_save_member_function_body (cp_parser* parser,
19275 cp_decl_specifier_seq *decl_specifiers,
19276 cp_declarator *declarator,
19283 /* Create the FUNCTION_DECL. */
19284 fn = grokmethod (decl_specifiers, declarator, attributes);
19285 /* If something went badly wrong, bail out now. */
19286 if (fn == error_mark_node)
19288 /* If there's a function-body, skip it. */
19289 if (cp_parser_token_starts_function_definition_p
19290 (cp_lexer_peek_token (parser->lexer)))
19291 cp_parser_skip_to_end_of_block_or_statement (parser);
19292 return error_mark_node;
19295 /* Remember it, if there default args to post process. */
19296 cp_parser_save_default_args (parser, fn);
19298 /* Save away the tokens that make up the body of the
19300 first = parser->lexer->next_token;
19301 /* We can have braced-init-list mem-initializers before the fn body. */
19302 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19304 cp_lexer_consume_token (parser->lexer);
19305 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19306 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19308 /* cache_group will stop after an un-nested { } pair, too. */
19309 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19312 /* variadic mem-inits have ... after the ')'. */
19313 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19314 cp_lexer_consume_token (parser->lexer);
19317 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19318 /* Handle function try blocks. */
19319 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19320 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19321 last = parser->lexer->next_token;
19323 /* Save away the inline definition; we will process it when the
19324 class is complete. */
19325 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19326 DECL_PENDING_INLINE_P (fn) = 1;
19328 /* We need to know that this was defined in the class, so that
19329 friend templates are handled correctly. */
19330 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19332 /* Add FN to the queue of functions to be parsed later. */
19333 TREE_VALUE (parser->unparsed_functions_queues)
19334 = tree_cons (NULL_TREE, fn,
19335 TREE_VALUE (parser->unparsed_functions_queues));
19340 /* Parse a template-argument-list, as well as the trailing ">" (but
19341 not the opening ">"). See cp_parser_template_argument_list for the
19345 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19349 tree saved_qualifying_scope;
19350 tree saved_object_scope;
19351 bool saved_greater_than_is_operator_p;
19352 int saved_unevaluated_operand;
19353 int saved_inhibit_evaluation_warnings;
19357 When parsing a template-id, the first non-nested `>' is taken as
19358 the end of the template-argument-list rather than a greater-than
19360 saved_greater_than_is_operator_p
19361 = parser->greater_than_is_operator_p;
19362 parser->greater_than_is_operator_p = false;
19363 /* Parsing the argument list may modify SCOPE, so we save it
19365 saved_scope = parser->scope;
19366 saved_qualifying_scope = parser->qualifying_scope;
19367 saved_object_scope = parser->object_scope;
19368 /* We need to evaluate the template arguments, even though this
19369 template-id may be nested within a "sizeof". */
19370 saved_unevaluated_operand = cp_unevaluated_operand;
19371 cp_unevaluated_operand = 0;
19372 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19373 c_inhibit_evaluation_warnings = 0;
19374 /* Parse the template-argument-list itself. */
19375 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19376 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19377 arguments = NULL_TREE;
19379 arguments = cp_parser_template_argument_list (parser);
19380 /* Look for the `>' that ends the template-argument-list. If we find
19381 a '>>' instead, it's probably just a typo. */
19382 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19384 if (cxx_dialect != cxx98)
19386 /* In C++0x, a `>>' in a template argument list or cast
19387 expression is considered to be two separate `>'
19388 tokens. So, change the current token to a `>', but don't
19389 consume it: it will be consumed later when the outer
19390 template argument list (or cast expression) is parsed.
19391 Note that this replacement of `>' for `>>' is necessary
19392 even if we are parsing tentatively: in the tentative
19393 case, after calling
19394 cp_parser_enclosed_template_argument_list we will always
19395 throw away all of the template arguments and the first
19396 closing `>', either because the template argument list
19397 was erroneous or because we are replacing those tokens
19398 with a CPP_TEMPLATE_ID token. The second `>' (which will
19399 not have been thrown away) is needed either to close an
19400 outer template argument list or to complete a new-style
19402 cp_token *token = cp_lexer_peek_token (parser->lexer);
19403 token->type = CPP_GREATER;
19405 else if (!saved_greater_than_is_operator_p)
19407 /* If we're in a nested template argument list, the '>>' has
19408 to be a typo for '> >'. We emit the error message, but we
19409 continue parsing and we push a '>' as next token, so that
19410 the argument list will be parsed correctly. Note that the
19411 global source location is still on the token before the
19412 '>>', so we need to say explicitly where we want it. */
19413 cp_token *token = cp_lexer_peek_token (parser->lexer);
19414 error_at (token->location, "%<>>%> should be %<> >%> "
19415 "within a nested template argument list");
19417 token->type = CPP_GREATER;
19421 /* If this is not a nested template argument list, the '>>'
19422 is a typo for '>'. Emit an error message and continue.
19423 Same deal about the token location, but here we can get it
19424 right by consuming the '>>' before issuing the diagnostic. */
19425 cp_token *token = cp_lexer_consume_token (parser->lexer);
19426 error_at (token->location,
19427 "spurious %<>>%>, use %<>%> to terminate "
19428 "a template argument list");
19432 cp_parser_skip_to_end_of_template_parameter_list (parser);
19433 /* The `>' token might be a greater-than operator again now. */
19434 parser->greater_than_is_operator_p
19435 = saved_greater_than_is_operator_p;
19436 /* Restore the SAVED_SCOPE. */
19437 parser->scope = saved_scope;
19438 parser->qualifying_scope = saved_qualifying_scope;
19439 parser->object_scope = saved_object_scope;
19440 cp_unevaluated_operand = saved_unevaluated_operand;
19441 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19446 /* MEMBER_FUNCTION is a member function, or a friend. If default
19447 arguments, or the body of the function have not yet been parsed,
19451 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19453 /* If this member is a template, get the underlying
19455 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19456 member_function = DECL_TEMPLATE_RESULT (member_function);
19458 /* There should not be any class definitions in progress at this
19459 point; the bodies of members are only parsed outside of all class
19461 gcc_assert (parser->num_classes_being_defined == 0);
19462 /* While we're parsing the member functions we might encounter more
19463 classes. We want to handle them right away, but we don't want
19464 them getting mixed up with functions that are currently in the
19466 parser->unparsed_functions_queues
19467 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19469 /* Make sure that any template parameters are in scope. */
19470 maybe_begin_member_template_processing (member_function);
19472 /* If the body of the function has not yet been parsed, parse it
19474 if (DECL_PENDING_INLINE_P (member_function))
19476 tree function_scope;
19477 cp_token_cache *tokens;
19479 /* The function is no longer pending; we are processing it. */
19480 tokens = DECL_PENDING_INLINE_INFO (member_function);
19481 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19482 DECL_PENDING_INLINE_P (member_function) = 0;
19484 /* If this is a local class, enter the scope of the containing
19486 function_scope = current_function_decl;
19487 if (function_scope)
19488 push_function_context ();
19490 /* Push the body of the function onto the lexer stack. */
19491 cp_parser_push_lexer_for_tokens (parser, tokens);
19493 /* Let the front end know that we going to be defining this
19495 start_preparsed_function (member_function, NULL_TREE,
19496 SF_PRE_PARSED | SF_INCLASS_INLINE);
19498 /* Don't do access checking if it is a templated function. */
19499 if (processing_template_decl)
19500 push_deferring_access_checks (dk_no_check);
19502 /* Now, parse the body of the function. */
19503 cp_parser_function_definition_after_declarator (parser,
19504 /*inline_p=*/true);
19506 if (processing_template_decl)
19507 pop_deferring_access_checks ();
19509 /* Leave the scope of the containing function. */
19510 if (function_scope)
19511 pop_function_context ();
19512 cp_parser_pop_lexer (parser);
19515 /* Remove any template parameters from the symbol table. */
19516 maybe_end_member_template_processing ();
19518 /* Restore the queue. */
19519 parser->unparsed_functions_queues
19520 = TREE_CHAIN (parser->unparsed_functions_queues);
19523 /* If DECL contains any default args, remember it on the unparsed
19524 functions queue. */
19527 cp_parser_save_default_args (cp_parser* parser, tree decl)
19531 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19533 probe = TREE_CHAIN (probe))
19534 if (TREE_PURPOSE (probe))
19536 TREE_PURPOSE (parser->unparsed_functions_queues)
19537 = tree_cons (current_class_type, decl,
19538 TREE_PURPOSE (parser->unparsed_functions_queues));
19543 /* FN is a FUNCTION_DECL which may contains a parameter with an
19544 unparsed DEFAULT_ARG. Parse the default args now. This function
19545 assumes that the current scope is the scope in which the default
19546 argument should be processed. */
19549 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19551 bool saved_local_variables_forbidden_p;
19552 tree parm, parmdecl;
19554 /* While we're parsing the default args, we might (due to the
19555 statement expression extension) encounter more classes. We want
19556 to handle them right away, but we don't want them getting mixed
19557 up with default args that are currently in the queue. */
19558 parser->unparsed_functions_queues
19559 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19561 /* Local variable names (and the `this' keyword) may not appear
19562 in a default argument. */
19563 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19564 parser->local_variables_forbidden_p = true;
19566 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19567 parmdecl = DECL_ARGUMENTS (fn);
19568 parm && parm != void_list_node;
19569 parm = TREE_CHAIN (parm),
19570 parmdecl = TREE_CHAIN (parmdecl))
19572 cp_token_cache *tokens;
19573 tree default_arg = TREE_PURPOSE (parm);
19575 VEC(tree,gc) *insts;
19582 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19583 /* This can happen for a friend declaration for a function
19584 already declared with default arguments. */
19587 /* Push the saved tokens for the default argument onto the parser's
19589 tokens = DEFARG_TOKENS (default_arg);
19590 cp_parser_push_lexer_for_tokens (parser, tokens);
19592 start_lambda_scope (parmdecl);
19594 /* Parse the assignment-expression. */
19595 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19596 if (parsed_arg == error_mark_node)
19598 cp_parser_pop_lexer (parser);
19602 if (!processing_template_decl)
19603 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19605 TREE_PURPOSE (parm) = parsed_arg;
19607 /* Update any instantiations we've already created. */
19608 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19609 VEC_iterate (tree, insts, ix, copy); ix++)
19610 TREE_PURPOSE (copy) = parsed_arg;
19612 finish_lambda_scope ();
19614 /* If the token stream has not been completely used up, then
19615 there was extra junk after the end of the default
19617 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19618 cp_parser_error (parser, "expected %<,%>");
19620 /* Revert to the main lexer. */
19621 cp_parser_pop_lexer (parser);
19624 /* Make sure no default arg is missing. */
19625 check_default_args (fn);
19627 /* Restore the state of local_variables_forbidden_p. */
19628 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19630 /* Restore the queue. */
19631 parser->unparsed_functions_queues
19632 = TREE_CHAIN (parser->unparsed_functions_queues);
19635 /* Parse the operand of `sizeof' (or a similar operator). Returns
19636 either a TYPE or an expression, depending on the form of the
19637 input. The KEYWORD indicates which kind of expression we have
19641 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19643 tree expr = NULL_TREE;
19644 const char *saved_message;
19646 bool saved_integral_constant_expression_p;
19647 bool saved_non_integral_constant_expression_p;
19648 bool pack_expansion_p = false;
19650 /* Types cannot be defined in a `sizeof' expression. Save away the
19652 saved_message = parser->type_definition_forbidden_message;
19653 /* And create the new one. */
19654 tmp = concat ("types may not be defined in %<",
19655 IDENTIFIER_POINTER (ridpointers[keyword]),
19656 "%> expressions", NULL);
19657 parser->type_definition_forbidden_message = tmp;
19659 /* The restrictions on constant-expressions do not apply inside
19660 sizeof expressions. */
19661 saved_integral_constant_expression_p
19662 = parser->integral_constant_expression_p;
19663 saved_non_integral_constant_expression_p
19664 = parser->non_integral_constant_expression_p;
19665 parser->integral_constant_expression_p = false;
19667 /* If it's a `...', then we are computing the length of a parameter
19669 if (keyword == RID_SIZEOF
19670 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19672 /* Consume the `...'. */
19673 cp_lexer_consume_token (parser->lexer);
19674 maybe_warn_variadic_templates ();
19676 /* Note that this is an expansion. */
19677 pack_expansion_p = true;
19680 /* Do not actually evaluate the expression. */
19681 ++cp_unevaluated_operand;
19682 ++c_inhibit_evaluation_warnings;
19683 /* If it's a `(', then we might be looking at the type-id
19685 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19688 bool saved_in_type_id_in_expr_p;
19690 /* We can't be sure yet whether we're looking at a type-id or an
19692 cp_parser_parse_tentatively (parser);
19693 /* Consume the `('. */
19694 cp_lexer_consume_token (parser->lexer);
19695 /* Parse the type-id. */
19696 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19697 parser->in_type_id_in_expr_p = true;
19698 type = cp_parser_type_id (parser);
19699 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19700 /* Now, look for the trailing `)'. */
19701 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19702 /* If all went well, then we're done. */
19703 if (cp_parser_parse_definitely (parser))
19705 cp_decl_specifier_seq decl_specs;
19707 /* Build a trivial decl-specifier-seq. */
19708 clear_decl_specs (&decl_specs);
19709 decl_specs.type = type;
19711 /* Call grokdeclarator to figure out what type this is. */
19712 expr = grokdeclarator (NULL,
19716 /*attrlist=*/NULL);
19720 /* If the type-id production did not work out, then we must be
19721 looking at the unary-expression production. */
19723 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19724 /*cast_p=*/false, NULL);
19726 if (pack_expansion_p)
19727 /* Build a pack expansion. */
19728 expr = make_pack_expansion (expr);
19730 /* Go back to evaluating expressions. */
19731 --cp_unevaluated_operand;
19732 --c_inhibit_evaluation_warnings;
19734 /* Free the message we created. */
19736 /* And restore the old one. */
19737 parser->type_definition_forbidden_message = saved_message;
19738 parser->integral_constant_expression_p
19739 = saved_integral_constant_expression_p;
19740 parser->non_integral_constant_expression_p
19741 = saved_non_integral_constant_expression_p;
19746 /* If the current declaration has no declarator, return true. */
19749 cp_parser_declares_only_class_p (cp_parser *parser)
19751 /* If the next token is a `;' or a `,' then there is no
19753 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19754 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19757 /* Update the DECL_SPECS to reflect the storage class indicated by
19761 cp_parser_set_storage_class (cp_parser *parser,
19762 cp_decl_specifier_seq *decl_specs,
19764 location_t location)
19766 cp_storage_class storage_class;
19768 if (parser->in_unbraced_linkage_specification_p)
19770 error_at (location, "invalid use of %qD in linkage specification",
19771 ridpointers[keyword]);
19774 else if (decl_specs->storage_class != sc_none)
19776 decl_specs->conflicting_specifiers_p = true;
19780 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19781 && decl_specs->specs[(int) ds_thread])
19783 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19784 decl_specs->specs[(int) ds_thread] = 0;
19790 storage_class = sc_auto;
19793 storage_class = sc_register;
19796 storage_class = sc_static;
19799 storage_class = sc_extern;
19802 storage_class = sc_mutable;
19805 gcc_unreachable ();
19807 decl_specs->storage_class = storage_class;
19809 /* A storage class specifier cannot be applied alongside a typedef
19810 specifier. If there is a typedef specifier present then set
19811 conflicting_specifiers_p which will trigger an error later
19812 on in grokdeclarator. */
19813 if (decl_specs->specs[(int)ds_typedef])
19814 decl_specs->conflicting_specifiers_p = true;
19817 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19818 is true, the type is a user-defined type; otherwise it is a
19819 built-in type specified by a keyword. */
19822 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19824 location_t location,
19825 bool user_defined_p)
19827 decl_specs->any_specifiers_p = true;
19829 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19830 (with, for example, in "typedef int wchar_t;") we remember that
19831 this is what happened. In system headers, we ignore these
19832 declarations so that G++ can work with system headers that are not
19834 if (decl_specs->specs[(int) ds_typedef]
19836 && (type_spec == boolean_type_node
19837 || type_spec == char16_type_node
19838 || type_spec == char32_type_node
19839 || type_spec == wchar_type_node)
19840 && (decl_specs->type
19841 || decl_specs->specs[(int) ds_long]
19842 || decl_specs->specs[(int) ds_short]
19843 || decl_specs->specs[(int) ds_unsigned]
19844 || decl_specs->specs[(int) ds_signed]))
19846 decl_specs->redefined_builtin_type = type_spec;
19847 if (!decl_specs->type)
19849 decl_specs->type = type_spec;
19850 decl_specs->user_defined_type_p = false;
19851 decl_specs->type_location = location;
19854 else if (decl_specs->type)
19855 decl_specs->multiple_types_p = true;
19858 decl_specs->type = type_spec;
19859 decl_specs->user_defined_type_p = user_defined_p;
19860 decl_specs->redefined_builtin_type = NULL_TREE;
19861 decl_specs->type_location = location;
19865 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19866 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19869 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19871 return decl_specifiers->specs[(int) ds_friend] != 0;
19874 /* Issue an error message indicating that TOKEN_DESC was expected.
19875 If KEYWORD is true, it indicated this function is called by
19876 cp_parser_require_keword and the required token can only be
19877 a indicated keyword. */
19880 cp_parser_required_error (cp_parser *parser,
19881 required_token token_desc,
19884 switch (token_desc)
19887 cp_parser_error (parser, "expected %<new%>");
19890 cp_parser_error (parser, "expected %<delete%>");
19893 cp_parser_error (parser, "expected %<return%>");
19896 cp_parser_error (parser, "expected %<while%>");
19899 cp_parser_error (parser, "expected %<extern%>");
19901 case RT_STATIC_ASSERT:
19902 cp_parser_error (parser, "expected %<static_assert%>");
19905 cp_parser_error (parser, "expected %<decltype%>");
19908 cp_parser_error (parser, "expected %<operator%>");
19911 cp_parser_error (parser, "expected %<class%>");
19914 cp_parser_error (parser, "expected %<template%>");
19917 cp_parser_error (parser, "expected %<namespace%>");
19920 cp_parser_error (parser, "expected %<using%>");
19923 cp_parser_error (parser, "expected %<asm%>");
19926 cp_parser_error (parser, "expected %<try%>");
19929 cp_parser_error (parser, "expected %<catch%>");
19932 cp_parser_error (parser, "expected %<throw%>");
19935 cp_parser_error (parser, "expected %<__label__%>");
19938 cp_parser_error (parser, "expected %<@try%>");
19940 case RT_AT_SYNCHRONIZED:
19941 cp_parser_error (parser, "expected %<@synchronized%>");
19944 cp_parser_error (parser, "expected %<@throw%>");
19951 switch (token_desc)
19954 cp_parser_error (parser, "expected %<;%>");
19956 case RT_OPEN_PAREN:
19957 cp_parser_error (parser, "expected %<(%>");
19959 case RT_CLOSE_BRACE:
19960 cp_parser_error (parser, "expected %<}%>");
19962 case RT_OPEN_BRACE:
19963 cp_parser_error (parser, "expected %<{%>");
19965 case RT_CLOSE_SQUARE:
19966 cp_parser_error (parser, "expected %<]%>");
19968 case RT_OPEN_SQUARE:
19969 cp_parser_error (parser, "expected %<[%>");
19972 cp_parser_error (parser, "expected %<,%>");
19975 cp_parser_error (parser, "expected %<::%>");
19978 cp_parser_error (parser, "expected %<<%>");
19981 cp_parser_error (parser, "expected %<>%>");
19984 cp_parser_error (parser, "expected %<=%>");
19987 cp_parser_error (parser, "expected %<...%>");
19990 cp_parser_error (parser, "expected %<*%>");
19993 cp_parser_error (parser, "expected %<~%>");
19996 cp_parser_error (parser, "expected %<:%>");
19998 case RT_COLON_SCOPE:
19999 cp_parser_error (parser, "expected %<:%> or %<::%>");
20001 case RT_CLOSE_PAREN:
20002 cp_parser_error (parser, "expected %<)%>");
20004 case RT_COMMA_CLOSE_PAREN:
20005 cp_parser_error (parser, "expected %<,%> or %<)%>");
20007 case RT_PRAGMA_EOL:
20008 cp_parser_error (parser, "expected end of line");
20011 cp_parser_error (parser, "expected identifier");
20014 cp_parser_error (parser, "expected selection-statement");
20016 case RT_INTERATION:
20017 cp_parser_error (parser, "expected iteration-statement");
20020 cp_parser_error (parser, "expected jump-statement");
20023 cp_parser_error (parser, "expected class-key");
20025 case RT_CLASS_TYPENAME_TEMPLATE:
20026 cp_parser_error (parser,
20027 "expected %<class%>, %<typename%>, or %<template%>");
20030 gcc_unreachable ();
20034 gcc_unreachable ();
20039 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20040 issue an error message indicating that TOKEN_DESC was expected.
20042 Returns the token consumed, if the token had the appropriate type.
20043 Otherwise, returns NULL. */
20046 cp_parser_require (cp_parser* parser,
20047 enum cpp_ttype type,
20048 required_token token_desc)
20050 if (cp_lexer_next_token_is (parser->lexer, type))
20051 return cp_lexer_consume_token (parser->lexer);
20054 /* Output the MESSAGE -- unless we're parsing tentatively. */
20055 if (!cp_parser_simulate_error (parser))
20056 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20061 /* An error message is produced if the next token is not '>'.
20062 All further tokens are skipped until the desired token is
20063 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20066 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20068 /* Current level of '< ... >'. */
20069 unsigned level = 0;
20070 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20071 unsigned nesting_depth = 0;
20073 /* Are we ready, yet? If not, issue error message. */
20074 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20077 /* Skip tokens until the desired token is found. */
20080 /* Peek at the next token. */
20081 switch (cp_lexer_peek_token (parser->lexer)->type)
20084 if (!nesting_depth)
20089 if (cxx_dialect == cxx98)
20090 /* C++0x views the `>>' operator as two `>' tokens, but
20093 else if (!nesting_depth && level-- == 0)
20095 /* We've hit a `>>' where the first `>' closes the
20096 template argument list, and the second `>' is
20097 spurious. Just consume the `>>' and stop; we've
20098 already produced at least one error. */
20099 cp_lexer_consume_token (parser->lexer);
20102 /* Fall through for C++0x, so we handle the second `>' in
20106 if (!nesting_depth && level-- == 0)
20108 /* We've reached the token we want, consume it and stop. */
20109 cp_lexer_consume_token (parser->lexer);
20114 case CPP_OPEN_PAREN:
20115 case CPP_OPEN_SQUARE:
20119 case CPP_CLOSE_PAREN:
20120 case CPP_CLOSE_SQUARE:
20121 if (nesting_depth-- == 0)
20126 case CPP_PRAGMA_EOL:
20127 case CPP_SEMICOLON:
20128 case CPP_OPEN_BRACE:
20129 case CPP_CLOSE_BRACE:
20130 /* The '>' was probably forgotten, don't look further. */
20137 /* Consume this token. */
20138 cp_lexer_consume_token (parser->lexer);
20142 /* If the next token is the indicated keyword, consume it. Otherwise,
20143 issue an error message indicating that TOKEN_DESC was expected.
20145 Returns the token consumed, if the token had the appropriate type.
20146 Otherwise, returns NULL. */
20149 cp_parser_require_keyword (cp_parser* parser,
20151 required_token token_desc)
20153 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20155 if (token && token->keyword != keyword)
20157 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
20164 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20165 function-definition. */
20168 cp_parser_token_starts_function_definition_p (cp_token* token)
20170 return (/* An ordinary function-body begins with an `{'. */
20171 token->type == CPP_OPEN_BRACE
20172 /* A ctor-initializer begins with a `:'. */
20173 || token->type == CPP_COLON
20174 /* A function-try-block begins with `try'. */
20175 || token->keyword == RID_TRY
20176 /* The named return value extension begins with `return'. */
20177 || token->keyword == RID_RETURN);
20180 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20184 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20188 token = cp_lexer_peek_token (parser->lexer);
20189 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20192 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20193 C++0x) ending a template-argument. */
20196 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20200 token = cp_lexer_peek_token (parser->lexer);
20201 return (token->type == CPP_COMMA
20202 || token->type == CPP_GREATER
20203 || token->type == CPP_ELLIPSIS
20204 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20207 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20208 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
20211 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
20216 token = cp_lexer_peek_nth_token (parser->lexer, n);
20217 if (token->type == CPP_LESS)
20219 /* Check for the sequence `<::' in the original code. It would be lexed as
20220 `[:', where `[' is a digraph, and there is no whitespace before
20222 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
20225 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
20226 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
20232 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20233 or none_type otherwise. */
20235 static enum tag_types
20236 cp_parser_token_is_class_key (cp_token* token)
20238 switch (token->keyword)
20243 return record_type;
20252 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
20255 cp_parser_check_class_key (enum tag_types class_key, tree type)
20257 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
20258 permerror (input_location, "%qs tag used in naming %q#T",
20259 class_key == union_type ? "union"
20260 : class_key == record_type ? "struct" : "class",
20264 /* Issue an error message if DECL is redeclared with different
20265 access than its original declaration [class.access.spec/3].
20266 This applies to nested classes and nested class templates.
20270 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
20272 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
20275 if ((TREE_PRIVATE (decl)
20276 != (current_access_specifier == access_private_node))
20277 || (TREE_PROTECTED (decl)
20278 != (current_access_specifier == access_protected_node)))
20279 error_at (location, "%qD redeclared with different access", decl);
20282 /* Look for the `template' keyword, as a syntactic disambiguator.
20283 Return TRUE iff it is present, in which case it will be
20287 cp_parser_optional_template_keyword (cp_parser *parser)
20289 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20291 /* The `template' keyword can only be used within templates;
20292 outside templates the parser can always figure out what is a
20293 template and what is not. */
20294 if (!processing_template_decl)
20296 cp_token *token = cp_lexer_peek_token (parser->lexer);
20297 error_at (token->location,
20298 "%<template%> (as a disambiguator) is only allowed "
20299 "within templates");
20300 /* If this part of the token stream is rescanned, the same
20301 error message would be generated. So, we purge the token
20302 from the stream. */
20303 cp_lexer_purge_token (parser->lexer);
20308 /* Consume the `template' keyword. */
20309 cp_lexer_consume_token (parser->lexer);
20317 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
20318 set PARSER->SCOPE, and perform other related actions. */
20321 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
20324 struct tree_check *check_value;
20325 deferred_access_check *chk;
20326 VEC (deferred_access_check,gc) *checks;
20328 /* Get the stored value. */
20329 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
20330 /* Perform any access checks that were deferred. */
20331 checks = check_value->checks;
20335 VEC_iterate (deferred_access_check, checks, i, chk) ;
20338 perform_or_defer_access_check (chk->binfo,
20343 /* Set the scope from the stored value. */
20344 parser->scope = check_value->value;
20345 parser->qualifying_scope = check_value->qualifying_scope;
20346 parser->object_scope = NULL_TREE;
20349 /* Consume tokens up through a non-nested END token. Returns TRUE if we
20350 encounter the end of a block before what we were looking for. */
20353 cp_parser_cache_group (cp_parser *parser,
20354 enum cpp_ttype end,
20359 cp_token *token = cp_lexer_peek_token (parser->lexer);
20361 /* Abort a parenthesized expression if we encounter a semicolon. */
20362 if ((end == CPP_CLOSE_PAREN || depth == 0)
20363 && token->type == CPP_SEMICOLON)
20365 /* If we've reached the end of the file, stop. */
20366 if (token->type == CPP_EOF
20367 || (end != CPP_PRAGMA_EOL
20368 && token->type == CPP_PRAGMA_EOL))
20370 if (token->type == CPP_CLOSE_BRACE && depth == 0)
20371 /* We've hit the end of an enclosing block, so there's been some
20372 kind of syntax error. */
20375 /* Consume the token. */
20376 cp_lexer_consume_token (parser->lexer);
20377 /* See if it starts a new group. */
20378 if (token->type == CPP_OPEN_BRACE)
20380 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
20381 /* In theory this should probably check end == '}', but
20382 cp_parser_save_member_function_body needs it to exit
20383 after either '}' or ')' when called with ')'. */
20387 else if (token->type == CPP_OPEN_PAREN)
20389 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
20390 if (depth == 0 && end == CPP_CLOSE_PAREN)
20393 else if (token->type == CPP_PRAGMA)
20394 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
20395 else if (token->type == end)
20400 /* Begin parsing tentatively. We always save tokens while parsing
20401 tentatively so that if the tentative parsing fails we can restore the
20405 cp_parser_parse_tentatively (cp_parser* parser)
20407 /* Enter a new parsing context. */
20408 parser->context = cp_parser_context_new (parser->context);
20409 /* Begin saving tokens. */
20410 cp_lexer_save_tokens (parser->lexer);
20411 /* In order to avoid repetitive access control error messages,
20412 access checks are queued up until we are no longer parsing
20414 push_deferring_access_checks (dk_deferred);
20417 /* Commit to the currently active tentative parse. */
20420 cp_parser_commit_to_tentative_parse (cp_parser* parser)
20422 cp_parser_context *context;
20425 /* Mark all of the levels as committed. */
20426 lexer = parser->lexer;
20427 for (context = parser->context; context->next; context = context->next)
20429 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
20431 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
20432 while (!cp_lexer_saving_tokens (lexer))
20433 lexer = lexer->next;
20434 cp_lexer_commit_tokens (lexer);
20438 /* Abort the currently active tentative parse. All consumed tokens
20439 will be rolled back, and no diagnostics will be issued. */
20442 cp_parser_abort_tentative_parse (cp_parser* parser)
20444 cp_parser_simulate_error (parser);
20445 /* Now, pretend that we want to see if the construct was
20446 successfully parsed. */
20447 cp_parser_parse_definitely (parser);
20450 /* Stop parsing tentatively. If a parse error has occurred, restore the
20451 token stream. Otherwise, commit to the tokens we have consumed.
20452 Returns true if no error occurred; false otherwise. */
20455 cp_parser_parse_definitely (cp_parser* parser)
20457 bool error_occurred;
20458 cp_parser_context *context;
20460 /* Remember whether or not an error occurred, since we are about to
20461 destroy that information. */
20462 error_occurred = cp_parser_error_occurred (parser);
20463 /* Remove the topmost context from the stack. */
20464 context = parser->context;
20465 parser->context = context->next;
20466 /* If no parse errors occurred, commit to the tentative parse. */
20467 if (!error_occurred)
20469 /* Commit to the tokens read tentatively, unless that was
20471 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20472 cp_lexer_commit_tokens (parser->lexer);
20474 pop_to_parent_deferring_access_checks ();
20476 /* Otherwise, if errors occurred, roll back our state so that things
20477 are just as they were before we began the tentative parse. */
20480 cp_lexer_rollback_tokens (parser->lexer);
20481 pop_deferring_access_checks ();
20483 /* Add the context to the front of the free list. */
20484 context->next = cp_parser_context_free_list;
20485 cp_parser_context_free_list = context;
20487 return !error_occurred;
20490 /* Returns true if we are parsing tentatively and are not committed to
20491 this tentative parse. */
20494 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20496 return (cp_parser_parsing_tentatively (parser)
20497 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20500 /* Returns nonzero iff an error has occurred during the most recent
20501 tentative parse. */
20504 cp_parser_error_occurred (cp_parser* parser)
20506 return (cp_parser_parsing_tentatively (parser)
20507 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20510 /* Returns nonzero if GNU extensions are allowed. */
20513 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20515 return parser->allow_gnu_extensions_p;
20518 /* Objective-C++ Productions */
20521 /* Parse an Objective-C expression, which feeds into a primary-expression
20525 objc-message-expression
20526 objc-string-literal
20527 objc-encode-expression
20528 objc-protocol-expression
20529 objc-selector-expression
20531 Returns a tree representation of the expression. */
20534 cp_parser_objc_expression (cp_parser* parser)
20536 /* Try to figure out what kind of declaration is present. */
20537 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20541 case CPP_OPEN_SQUARE:
20542 return cp_parser_objc_message_expression (parser);
20544 case CPP_OBJC_STRING:
20545 kwd = cp_lexer_consume_token (parser->lexer);
20546 return objc_build_string_object (kwd->u.value);
20549 switch (kwd->keyword)
20551 case RID_AT_ENCODE:
20552 return cp_parser_objc_encode_expression (parser);
20554 case RID_AT_PROTOCOL:
20555 return cp_parser_objc_protocol_expression (parser);
20557 case RID_AT_SELECTOR:
20558 return cp_parser_objc_selector_expression (parser);
20564 error_at (kwd->location,
20565 "misplaced %<@%D%> Objective-C++ construct",
20567 cp_parser_skip_to_end_of_block_or_statement (parser);
20570 return error_mark_node;
20573 /* Parse an Objective-C message expression.
20575 objc-message-expression:
20576 [ objc-message-receiver objc-message-args ]
20578 Returns a representation of an Objective-C message. */
20581 cp_parser_objc_message_expression (cp_parser* parser)
20583 tree receiver, messageargs;
20585 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20586 receiver = cp_parser_objc_message_receiver (parser);
20587 messageargs = cp_parser_objc_message_args (parser);
20588 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20590 return objc_build_message_expr (build_tree_list (receiver, messageargs));
20593 /* Parse an objc-message-receiver.
20595 objc-message-receiver:
20597 simple-type-specifier
20599 Returns a representation of the type or expression. */
20602 cp_parser_objc_message_receiver (cp_parser* parser)
20606 /* An Objective-C message receiver may be either (1) a type
20607 or (2) an expression. */
20608 cp_parser_parse_tentatively (parser);
20609 rcv = cp_parser_expression (parser, false, NULL);
20611 if (cp_parser_parse_definitely (parser))
20614 rcv = cp_parser_simple_type_specifier (parser,
20615 /*decl_specs=*/NULL,
20616 CP_PARSER_FLAGS_NONE);
20618 return objc_get_class_reference (rcv);
20621 /* Parse the arguments and selectors comprising an Objective-C message.
20626 objc-selector-args , objc-comma-args
20628 objc-selector-args:
20629 objc-selector [opt] : assignment-expression
20630 objc-selector-args objc-selector [opt] : assignment-expression
20633 assignment-expression
20634 objc-comma-args , assignment-expression
20636 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20637 selector arguments and TREE_VALUE containing a list of comma
20641 cp_parser_objc_message_args (cp_parser* parser)
20643 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20644 bool maybe_unary_selector_p = true;
20645 cp_token *token = cp_lexer_peek_token (parser->lexer);
20647 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20649 tree selector = NULL_TREE, arg;
20651 if (token->type != CPP_COLON)
20652 selector = cp_parser_objc_selector (parser);
20654 /* Detect if we have a unary selector. */
20655 if (maybe_unary_selector_p
20656 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20657 return build_tree_list (selector, NULL_TREE);
20659 maybe_unary_selector_p = false;
20660 cp_parser_require (parser, CPP_COLON, RT_COLON);
20661 arg = cp_parser_assignment_expression (parser, false, NULL);
20664 = chainon (sel_args,
20665 build_tree_list (selector, arg));
20667 token = cp_lexer_peek_token (parser->lexer);
20670 /* Handle non-selector arguments, if any. */
20671 while (token->type == CPP_COMMA)
20675 cp_lexer_consume_token (parser->lexer);
20676 arg = cp_parser_assignment_expression (parser, false, NULL);
20679 = chainon (addl_args,
20680 build_tree_list (NULL_TREE, arg));
20682 token = cp_lexer_peek_token (parser->lexer);
20685 return build_tree_list (sel_args, addl_args);
20688 /* Parse an Objective-C encode expression.
20690 objc-encode-expression:
20691 @encode objc-typename
20693 Returns an encoded representation of the type argument. */
20696 cp_parser_objc_encode_expression (cp_parser* parser)
20701 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20702 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20703 token = cp_lexer_peek_token (parser->lexer);
20704 type = complete_type (cp_parser_type_id (parser));
20705 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20709 error_at (token->location,
20710 "%<@encode%> must specify a type as an argument");
20711 return error_mark_node;
20714 return objc_build_encode_expr (type);
20717 /* Parse an Objective-C @defs expression. */
20720 cp_parser_objc_defs_expression (cp_parser *parser)
20724 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20725 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20726 name = cp_parser_identifier (parser);
20727 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20729 return objc_get_class_ivars (name);
20732 /* Parse an Objective-C protocol expression.
20734 objc-protocol-expression:
20735 @protocol ( identifier )
20737 Returns a representation of the protocol expression. */
20740 cp_parser_objc_protocol_expression (cp_parser* parser)
20744 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20745 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20746 proto = cp_parser_identifier (parser);
20747 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20749 return objc_build_protocol_expr (proto);
20752 /* Parse an Objective-C selector expression.
20754 objc-selector-expression:
20755 @selector ( objc-method-signature )
20757 objc-method-signature:
20763 objc-selector-seq objc-selector :
20765 Returns a representation of the method selector. */
20768 cp_parser_objc_selector_expression (cp_parser* parser)
20770 tree sel_seq = NULL_TREE;
20771 bool maybe_unary_selector_p = true;
20773 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20775 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20776 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20777 token = cp_lexer_peek_token (parser->lexer);
20779 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20780 || token->type == CPP_SCOPE)
20782 tree selector = NULL_TREE;
20784 if (token->type != CPP_COLON
20785 || token->type == CPP_SCOPE)
20786 selector = cp_parser_objc_selector (parser);
20788 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20789 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20791 /* Detect if we have a unary selector. */
20792 if (maybe_unary_selector_p)
20794 sel_seq = selector;
20795 goto finish_selector;
20799 cp_parser_error (parser, "expected %<:%>");
20802 maybe_unary_selector_p = false;
20803 token = cp_lexer_consume_token (parser->lexer);
20805 if (token->type == CPP_SCOPE)
20808 = chainon (sel_seq,
20809 build_tree_list (selector, NULL_TREE));
20811 = chainon (sel_seq,
20812 build_tree_list (NULL_TREE, NULL_TREE));
20816 = chainon (sel_seq,
20817 build_tree_list (selector, NULL_TREE));
20819 token = cp_lexer_peek_token (parser->lexer);
20823 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20825 return objc_build_selector_expr (loc, sel_seq);
20828 /* Parse a list of identifiers.
20830 objc-identifier-list:
20832 objc-identifier-list , identifier
20834 Returns a TREE_LIST of identifier nodes. */
20837 cp_parser_objc_identifier_list (cp_parser* parser)
20839 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20840 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20842 while (sep->type == CPP_COMMA)
20844 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20845 list = chainon (list,
20846 build_tree_list (NULL_TREE,
20847 cp_parser_identifier (parser)));
20848 sep = cp_lexer_peek_token (parser->lexer);
20854 /* Parse an Objective-C alias declaration.
20856 objc-alias-declaration:
20857 @compatibility_alias identifier identifier ;
20859 This function registers the alias mapping with the Objective-C front end.
20860 It returns nothing. */
20863 cp_parser_objc_alias_declaration (cp_parser* parser)
20867 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20868 alias = cp_parser_identifier (parser);
20869 orig = cp_parser_identifier (parser);
20870 objc_declare_alias (alias, orig);
20871 cp_parser_consume_semicolon_at_end_of_statement (parser);
20874 /* Parse an Objective-C class forward-declaration.
20876 objc-class-declaration:
20877 @class objc-identifier-list ;
20879 The function registers the forward declarations with the Objective-C
20880 front end. It returns nothing. */
20883 cp_parser_objc_class_declaration (cp_parser* parser)
20885 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20886 objc_declare_class (cp_parser_objc_identifier_list (parser));
20887 cp_parser_consume_semicolon_at_end_of_statement (parser);
20890 /* Parse a list of Objective-C protocol references.
20892 objc-protocol-refs-opt:
20893 objc-protocol-refs [opt]
20895 objc-protocol-refs:
20896 < objc-identifier-list >
20898 Returns a TREE_LIST of identifiers, if any. */
20901 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20903 tree protorefs = NULL_TREE;
20905 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20907 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20908 protorefs = cp_parser_objc_identifier_list (parser);
20909 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
20915 /* Parse a Objective-C visibility specification. */
20918 cp_parser_objc_visibility_spec (cp_parser* parser)
20920 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20922 switch (vis->keyword)
20924 case RID_AT_PRIVATE:
20925 objc_set_visibility (2);
20927 case RID_AT_PROTECTED:
20928 objc_set_visibility (0);
20930 case RID_AT_PUBLIC:
20931 objc_set_visibility (1);
20937 /* Eat '@private'/'@protected'/'@public'. */
20938 cp_lexer_consume_token (parser->lexer);
20941 /* Parse an Objective-C method type. */
20944 cp_parser_objc_method_type (cp_parser* parser)
20946 objc_set_method_type
20947 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20952 /* Parse an Objective-C protocol qualifier. */
20955 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20957 tree quals = NULL_TREE, node;
20958 cp_token *token = cp_lexer_peek_token (parser->lexer);
20960 node = token->u.value;
20962 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20963 && (node == ridpointers [(int) RID_IN]
20964 || node == ridpointers [(int) RID_OUT]
20965 || node == ridpointers [(int) RID_INOUT]
20966 || node == ridpointers [(int) RID_BYCOPY]
20967 || node == ridpointers [(int) RID_BYREF]
20968 || node == ridpointers [(int) RID_ONEWAY]))
20970 quals = tree_cons (NULL_TREE, node, quals);
20971 cp_lexer_consume_token (parser->lexer);
20972 token = cp_lexer_peek_token (parser->lexer);
20973 node = token->u.value;
20979 /* Parse an Objective-C typename. */
20982 cp_parser_objc_typename (cp_parser* parser)
20984 tree type_name = NULL_TREE;
20986 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20988 tree proto_quals, cp_type = NULL_TREE;
20990 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20991 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20993 /* An ObjC type name may consist of just protocol qualifiers, in which
20994 case the type shall default to 'id'. */
20995 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20996 cp_type = cp_parser_type_id (parser);
20998 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20999 type_name = build_tree_list (proto_quals, cp_type);
21005 /* Check to see if TYPE refers to an Objective-C selector name. */
21008 cp_parser_objc_selector_p (enum cpp_ttype type)
21010 return (type == CPP_NAME || type == CPP_KEYWORD
21011 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21012 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21013 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21014 || type == CPP_XOR || type == CPP_XOR_EQ);
21017 /* Parse an Objective-C selector. */
21020 cp_parser_objc_selector (cp_parser* parser)
21022 cp_token *token = cp_lexer_consume_token (parser->lexer);
21024 if (!cp_parser_objc_selector_p (token->type))
21026 error_at (token->location, "invalid Objective-C++ selector name");
21027 return error_mark_node;
21030 /* C++ operator names are allowed to appear in ObjC selectors. */
21031 switch (token->type)
21033 case CPP_AND_AND: return get_identifier ("and");
21034 case CPP_AND_EQ: return get_identifier ("and_eq");
21035 case CPP_AND: return get_identifier ("bitand");
21036 case CPP_OR: return get_identifier ("bitor");
21037 case CPP_COMPL: return get_identifier ("compl");
21038 case CPP_NOT: return get_identifier ("not");
21039 case CPP_NOT_EQ: return get_identifier ("not_eq");
21040 case CPP_OR_OR: return get_identifier ("or");
21041 case CPP_OR_EQ: return get_identifier ("or_eq");
21042 case CPP_XOR: return get_identifier ("xor");
21043 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21044 default: return token->u.value;
21048 /* Parse an Objective-C params list. */
21051 cp_parser_objc_method_keyword_params (cp_parser* parser)
21053 tree params = NULL_TREE;
21054 bool maybe_unary_selector_p = true;
21055 cp_token *token = cp_lexer_peek_token (parser->lexer);
21057 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21059 tree selector = NULL_TREE, type_name, identifier;
21061 if (token->type != CPP_COLON)
21062 selector = cp_parser_objc_selector (parser);
21064 /* Detect if we have a unary selector. */
21065 if (maybe_unary_selector_p
21066 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21069 maybe_unary_selector_p = false;
21070 cp_parser_require (parser, CPP_COLON, RT_COLON);
21071 type_name = cp_parser_objc_typename (parser);
21072 identifier = cp_parser_identifier (parser);
21076 objc_build_keyword_decl (selector,
21080 token = cp_lexer_peek_token (parser->lexer);
21086 /* Parse the non-keyword Objective-C params. */
21089 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
21091 tree params = make_node (TREE_LIST);
21092 cp_token *token = cp_lexer_peek_token (parser->lexer);
21093 *ellipsisp = false; /* Initially, assume no ellipsis. */
21095 while (token->type == CPP_COMMA)
21097 cp_parameter_declarator *parmdecl;
21100 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21101 token = cp_lexer_peek_token (parser->lexer);
21103 if (token->type == CPP_ELLIPSIS)
21105 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
21110 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21111 parm = grokdeclarator (parmdecl->declarator,
21112 &parmdecl->decl_specifiers,
21113 PARM, /*initialized=*/0,
21114 /*attrlist=*/NULL);
21116 chainon (params, build_tree_list (NULL_TREE, parm));
21117 token = cp_lexer_peek_token (parser->lexer);
21123 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
21126 cp_parser_objc_interstitial_code (cp_parser* parser)
21128 cp_token *token = cp_lexer_peek_token (parser->lexer);
21130 /* If the next token is `extern' and the following token is a string
21131 literal, then we have a linkage specification. */
21132 if (token->keyword == RID_EXTERN
21133 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
21134 cp_parser_linkage_specification (parser);
21135 /* Handle #pragma, if any. */
21136 else if (token->type == CPP_PRAGMA)
21137 cp_parser_pragma (parser, pragma_external);
21138 /* Allow stray semicolons. */
21139 else if (token->type == CPP_SEMICOLON)
21140 cp_lexer_consume_token (parser->lexer);
21141 /* Finally, try to parse a block-declaration, or a function-definition. */
21143 cp_parser_block_declaration (parser, /*statement_p=*/false);
21146 /* Parse a method signature. */
21149 cp_parser_objc_method_signature (cp_parser* parser)
21151 tree rettype, kwdparms, optparms;
21152 bool ellipsis = false;
21154 cp_parser_objc_method_type (parser);
21155 rettype = cp_parser_objc_typename (parser);
21156 kwdparms = cp_parser_objc_method_keyword_params (parser);
21157 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
21159 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
21162 /* Pars an Objective-C method prototype list. */
21165 cp_parser_objc_method_prototype_list (cp_parser* parser)
21167 cp_token *token = cp_lexer_peek_token (parser->lexer);
21169 while (token->keyword != RID_AT_END)
21171 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21173 objc_add_method_declaration
21174 (cp_parser_objc_method_signature (parser));
21175 cp_parser_consume_semicolon_at_end_of_statement (parser);
21178 /* Allow for interspersed non-ObjC++ code. */
21179 cp_parser_objc_interstitial_code (parser);
21181 token = cp_lexer_peek_token (parser->lexer);
21184 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21185 objc_finish_interface ();
21188 /* Parse an Objective-C method definition list. */
21191 cp_parser_objc_method_definition_list (cp_parser* parser)
21193 cp_token *token = cp_lexer_peek_token (parser->lexer);
21195 while (token->keyword != RID_AT_END)
21199 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21201 push_deferring_access_checks (dk_deferred);
21202 objc_start_method_definition
21203 (cp_parser_objc_method_signature (parser));
21205 /* For historical reasons, we accept an optional semicolon. */
21206 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21207 cp_lexer_consume_token (parser->lexer);
21209 perform_deferred_access_checks ();
21210 stop_deferring_access_checks ();
21211 meth = cp_parser_function_definition_after_declarator (parser,
21213 pop_deferring_access_checks ();
21214 objc_finish_method_definition (meth);
21217 /* Allow for interspersed non-ObjC++ code. */
21218 cp_parser_objc_interstitial_code (parser);
21220 token = cp_lexer_peek_token (parser->lexer);
21223 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21224 objc_finish_implementation ();
21227 /* Parse Objective-C ivars. */
21230 cp_parser_objc_class_ivars (cp_parser* parser)
21232 cp_token *token = cp_lexer_peek_token (parser->lexer);
21234 if (token->type != CPP_OPEN_BRACE)
21235 return; /* No ivars specified. */
21237 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
21238 token = cp_lexer_peek_token (parser->lexer);
21240 while (token->type != CPP_CLOSE_BRACE)
21242 cp_decl_specifier_seq declspecs;
21243 int decl_class_or_enum_p;
21244 tree prefix_attributes;
21246 cp_parser_objc_visibility_spec (parser);
21248 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21251 cp_parser_decl_specifier_seq (parser,
21252 CP_PARSER_FLAGS_OPTIONAL,
21254 &decl_class_or_enum_p);
21255 prefix_attributes = declspecs.attributes;
21256 declspecs.attributes = NULL_TREE;
21258 /* Keep going until we hit the `;' at the end of the
21260 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21262 tree width = NULL_TREE, attributes, first_attribute, decl;
21263 cp_declarator *declarator = NULL;
21264 int ctor_dtor_or_conv_p;
21266 /* Check for a (possibly unnamed) bitfield declaration. */
21267 token = cp_lexer_peek_token (parser->lexer);
21268 if (token->type == CPP_COLON)
21271 if (token->type == CPP_NAME
21272 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21275 /* Get the name of the bitfield. */
21276 declarator = make_id_declarator (NULL_TREE,
21277 cp_parser_identifier (parser),
21281 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
21282 /* Get the width of the bitfield. */
21284 = cp_parser_constant_expression (parser,
21285 /*allow_non_constant=*/false,
21290 /* Parse the declarator. */
21292 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21293 &ctor_dtor_or_conv_p,
21294 /*parenthesized_p=*/NULL,
21295 /*member_p=*/false);
21298 /* Look for attributes that apply to the ivar. */
21299 attributes = cp_parser_attributes_opt (parser);
21300 /* Remember which attributes are prefix attributes and
21302 first_attribute = attributes;
21303 /* Combine the attributes. */
21304 attributes = chainon (prefix_attributes, attributes);
21307 /* Create the bitfield declaration. */
21308 decl = grokbitfield (declarator, &declspecs,
21312 decl = grokfield (declarator, &declspecs,
21313 NULL_TREE, /*init_const_expr_p=*/false,
21314 NULL_TREE, attributes);
21316 /* Add the instance variable. */
21317 objc_add_instance_variable (decl);
21319 /* Reset PREFIX_ATTRIBUTES. */
21320 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21321 attributes = TREE_CHAIN (attributes);
21323 TREE_CHAIN (attributes) = NULL_TREE;
21325 token = cp_lexer_peek_token (parser->lexer);
21327 if (token->type == CPP_COMMA)
21329 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21335 cp_parser_consume_semicolon_at_end_of_statement (parser);
21336 token = cp_lexer_peek_token (parser->lexer);
21339 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
21340 /* For historical reasons, we accept an optional semicolon. */
21341 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21342 cp_lexer_consume_token (parser->lexer);
21345 /* Parse an Objective-C protocol declaration. */
21348 cp_parser_objc_protocol_declaration (cp_parser* parser)
21350 tree proto, protorefs;
21353 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
21356 tok = cp_lexer_peek_token (parser->lexer);
21357 error_at (tok->location, "identifier expected after %<@protocol%>");
21361 /* See if we have a forward declaration or a definition. */
21362 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
21364 /* Try a forward declaration first. */
21365 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
21367 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
21369 cp_parser_consume_semicolon_at_end_of_statement (parser);
21372 /* Ok, we got a full-fledged definition (or at least should). */
21375 proto = cp_parser_identifier (parser);
21376 protorefs = cp_parser_objc_protocol_refs_opt (parser);
21377 objc_start_protocol (proto, protorefs);
21378 cp_parser_objc_method_prototype_list (parser);
21382 /* Parse an Objective-C superclass or category. */
21385 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
21388 cp_token *next = cp_lexer_peek_token (parser->lexer);
21390 *super = *categ = NULL_TREE;
21391 if (next->type == CPP_COLON)
21393 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
21394 *super = cp_parser_identifier (parser);
21396 else if (next->type == CPP_OPEN_PAREN)
21398 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21399 *categ = cp_parser_identifier (parser);
21400 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21404 /* Parse an Objective-C class interface. */
21407 cp_parser_objc_class_interface (cp_parser* parser)
21409 tree name, super, categ, protos;
21411 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
21412 name = cp_parser_identifier (parser);
21413 cp_parser_objc_superclass_or_category (parser, &super, &categ);
21414 protos = cp_parser_objc_protocol_refs_opt (parser);
21416 /* We have either a class or a category on our hands. */
21418 objc_start_category_interface (name, categ, protos);
21421 objc_start_class_interface (name, super, protos);
21422 /* Handle instance variable declarations, if any. */
21423 cp_parser_objc_class_ivars (parser);
21424 objc_continue_interface ();
21427 cp_parser_objc_method_prototype_list (parser);
21430 /* Parse an Objective-C class implementation. */
21433 cp_parser_objc_class_implementation (cp_parser* parser)
21435 tree name, super, categ;
21437 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
21438 name = cp_parser_identifier (parser);
21439 cp_parser_objc_superclass_or_category (parser, &super, &categ);
21441 /* We have either a class or a category on our hands. */
21443 objc_start_category_implementation (name, categ);
21446 objc_start_class_implementation (name, super);
21447 /* Handle instance variable declarations, if any. */
21448 cp_parser_objc_class_ivars (parser);
21449 objc_continue_implementation ();
21452 cp_parser_objc_method_definition_list (parser);
21455 /* Consume the @end token and finish off the implementation. */
21458 cp_parser_objc_end_implementation (cp_parser* parser)
21460 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21461 objc_finish_implementation ();
21464 /* Parse an Objective-C declaration. */
21467 cp_parser_objc_declaration (cp_parser* parser)
21469 /* Try to figure out what kind of declaration is present. */
21470 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21472 switch (kwd->keyword)
21475 cp_parser_objc_alias_declaration (parser);
21478 cp_parser_objc_class_declaration (parser);
21480 case RID_AT_PROTOCOL:
21481 cp_parser_objc_protocol_declaration (parser);
21483 case RID_AT_INTERFACE:
21484 cp_parser_objc_class_interface (parser);
21486 case RID_AT_IMPLEMENTATION:
21487 cp_parser_objc_class_implementation (parser);
21490 cp_parser_objc_end_implementation (parser);
21493 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21495 cp_parser_skip_to_end_of_block_or_statement (parser);
21499 /* Parse an Objective-C try-catch-finally statement.
21501 objc-try-catch-finally-stmt:
21502 @try compound-statement objc-catch-clause-seq [opt]
21503 objc-finally-clause [opt]
21505 objc-catch-clause-seq:
21506 objc-catch-clause objc-catch-clause-seq [opt]
21509 @catch ( exception-declaration ) compound-statement
21511 objc-finally-clause
21512 @finally compound-statement
21514 Returns NULL_TREE. */
21517 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21518 location_t location;
21521 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
21522 location = cp_lexer_peek_token (parser->lexer)->location;
21523 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21524 node, lest it get absorbed into the surrounding block. */
21525 stmt = push_stmt_list ();
21526 cp_parser_compound_statement (parser, NULL, false);
21527 objc_begin_try_stmt (location, pop_stmt_list (stmt));
21529 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21531 cp_parameter_declarator *parmdecl;
21534 cp_lexer_consume_token (parser->lexer);
21535 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21536 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21537 parm = grokdeclarator (parmdecl->declarator,
21538 &parmdecl->decl_specifiers,
21539 PARM, /*initialized=*/0,
21540 /*attrlist=*/NULL);
21541 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21542 objc_begin_catch_clause (parm);
21543 cp_parser_compound_statement (parser, NULL, false);
21544 objc_finish_catch_clause ();
21547 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21549 cp_lexer_consume_token (parser->lexer);
21550 location = cp_lexer_peek_token (parser->lexer)->location;
21551 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21552 node, lest it get absorbed into the surrounding block. */
21553 stmt = push_stmt_list ();
21554 cp_parser_compound_statement (parser, NULL, false);
21555 objc_build_finally_clause (location, pop_stmt_list (stmt));
21558 return objc_finish_try_stmt ();
21561 /* Parse an Objective-C synchronized statement.
21563 objc-synchronized-stmt:
21564 @synchronized ( expression ) compound-statement
21566 Returns NULL_TREE. */
21569 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21570 location_t location;
21573 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
21575 location = cp_lexer_peek_token (parser->lexer)->location;
21576 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21577 lock = cp_parser_expression (parser, false, NULL);
21578 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21580 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21581 node, lest it get absorbed into the surrounding block. */
21582 stmt = push_stmt_list ();
21583 cp_parser_compound_statement (parser, NULL, false);
21585 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21588 /* Parse an Objective-C throw statement.
21591 @throw assignment-expression [opt] ;
21593 Returns a constructed '@throw' statement. */
21596 cp_parser_objc_throw_statement (cp_parser *parser) {
21597 tree expr = NULL_TREE;
21598 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21600 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
21602 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21603 expr = cp_parser_assignment_expression (parser, false, NULL);
21605 cp_parser_consume_semicolon_at_end_of_statement (parser);
21607 return objc_build_throw_stmt (loc, expr);
21610 /* Parse an Objective-C statement. */
21613 cp_parser_objc_statement (cp_parser * parser) {
21614 /* Try to figure out what kind of declaration is present. */
21615 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21617 switch (kwd->keyword)
21620 return cp_parser_objc_try_catch_finally_statement (parser);
21621 case RID_AT_SYNCHRONIZED:
21622 return cp_parser_objc_synchronized_statement (parser);
21624 return cp_parser_objc_throw_statement (parser);
21626 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21628 cp_parser_skip_to_end_of_block_or_statement (parser);
21631 return error_mark_node;
21634 /* OpenMP 2.5 parsing routines. */
21636 /* Returns name of the next clause.
21637 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21638 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21639 returned and the token is consumed. */
21641 static pragma_omp_clause
21642 cp_parser_omp_clause_name (cp_parser *parser)
21644 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21646 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21647 result = PRAGMA_OMP_CLAUSE_IF;
21648 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21649 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21650 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21651 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21652 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21654 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21655 const char *p = IDENTIFIER_POINTER (id);
21660 if (!strcmp ("collapse", p))
21661 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21662 else if (!strcmp ("copyin", p))
21663 result = PRAGMA_OMP_CLAUSE_COPYIN;
21664 else if (!strcmp ("copyprivate", p))
21665 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21668 if (!strcmp ("firstprivate", p))
21669 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21672 if (!strcmp ("lastprivate", p))
21673 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21676 if (!strcmp ("nowait", p))
21677 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21678 else if (!strcmp ("num_threads", p))
21679 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21682 if (!strcmp ("ordered", p))
21683 result = PRAGMA_OMP_CLAUSE_ORDERED;
21686 if (!strcmp ("reduction", p))
21687 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21690 if (!strcmp ("schedule", p))
21691 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21692 else if (!strcmp ("shared", p))
21693 result = PRAGMA_OMP_CLAUSE_SHARED;
21696 if (!strcmp ("untied", p))
21697 result = PRAGMA_OMP_CLAUSE_UNTIED;
21702 if (result != PRAGMA_OMP_CLAUSE_NONE)
21703 cp_lexer_consume_token (parser->lexer);
21708 /* Validate that a clause of the given type does not already exist. */
21711 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21712 const char *name, location_t location)
21716 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21717 if (OMP_CLAUSE_CODE (c) == code)
21719 error_at (location, "too many %qs clauses", name);
21727 variable-list , identifier
21729 In addition, we match a closing parenthesis. An opening parenthesis
21730 will have been consumed by the caller.
21732 If KIND is nonzero, create the appropriate node and install the decl
21733 in OMP_CLAUSE_DECL and add the node to the head of the list.
21735 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21736 return the list created. */
21739 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21747 token = cp_lexer_peek_token (parser->lexer);
21748 name = cp_parser_id_expression (parser, /*template_p=*/false,
21749 /*check_dependency_p=*/true,
21750 /*template_p=*/NULL,
21751 /*declarator_p=*/false,
21752 /*optional_p=*/false);
21753 if (name == error_mark_node)
21756 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21757 if (decl == error_mark_node)
21758 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
21760 else if (kind != 0)
21762 tree u = build_omp_clause (token->location, kind);
21763 OMP_CLAUSE_DECL (u) = decl;
21764 OMP_CLAUSE_CHAIN (u) = list;
21768 list = tree_cons (decl, NULL_TREE, list);
21771 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21773 cp_lexer_consume_token (parser->lexer);
21776 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21780 /* Try to resync to an unnested comma. Copied from
21781 cp_parser_parenthesized_expression_list. */
21783 ending = cp_parser_skip_to_closing_parenthesis (parser,
21784 /*recovering=*/true,
21786 /*consume_paren=*/true);
21794 /* Similarly, but expect leading and trailing parenthesis. This is a very
21795 common case for omp clauses. */
21798 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21800 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21801 return cp_parser_omp_var_list_no_open (parser, kind, list);
21806 collapse ( constant-expression ) */
21809 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21815 loc = cp_lexer_peek_token (parser->lexer)->location;
21816 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21819 num = cp_parser_constant_expression (parser, false, NULL);
21821 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21822 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21823 /*or_comma=*/false,
21824 /*consume_paren=*/true);
21826 if (num == error_mark_node)
21828 num = fold_non_dependent_expr (num);
21829 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21830 || !host_integerp (num, 0)
21831 || (n = tree_low_cst (num, 0)) <= 0
21834 error_at (loc, "collapse argument needs positive constant integer expression");
21838 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21839 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21840 OMP_CLAUSE_CHAIN (c) = list;
21841 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21847 default ( shared | none ) */
21850 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21852 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21855 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21857 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21859 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21860 const char *p = IDENTIFIER_POINTER (id);
21865 if (strcmp ("none", p) != 0)
21867 kind = OMP_CLAUSE_DEFAULT_NONE;
21871 if (strcmp ("shared", p) != 0)
21873 kind = OMP_CLAUSE_DEFAULT_SHARED;
21880 cp_lexer_consume_token (parser->lexer);
21885 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21888 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21889 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21890 /*or_comma=*/false,
21891 /*consume_paren=*/true);
21893 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21896 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21897 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21898 OMP_CLAUSE_CHAIN (c) = list;
21899 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21905 if ( expression ) */
21908 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21912 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21915 t = cp_parser_condition (parser);
21917 if (t == error_mark_node
21918 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21919 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21920 /*or_comma=*/false,
21921 /*consume_paren=*/true);
21923 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21925 c = build_omp_clause (location, OMP_CLAUSE_IF);
21926 OMP_CLAUSE_IF_EXPR (c) = t;
21927 OMP_CLAUSE_CHAIN (c) = list;
21936 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21937 tree list, location_t location)
21941 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21943 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21944 OMP_CLAUSE_CHAIN (c) = list;
21949 num_threads ( expression ) */
21952 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21953 location_t location)
21957 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21960 t = cp_parser_expression (parser, false, NULL);
21962 if (t == error_mark_node
21963 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21964 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21965 /*or_comma=*/false,
21966 /*consume_paren=*/true);
21968 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21969 "num_threads", location);
21971 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21972 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21973 OMP_CLAUSE_CHAIN (c) = list;
21982 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21983 tree list, location_t location)
21987 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21988 "ordered", location);
21990 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21991 OMP_CLAUSE_CHAIN (c) = list;
21996 reduction ( reduction-operator : variable-list )
21998 reduction-operator:
21999 One of: + * - & ^ | && || */
22002 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
22004 enum tree_code code;
22007 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22010 switch (cp_lexer_peek_token (parser->lexer)->type)
22022 code = BIT_AND_EXPR;
22025 code = BIT_XOR_EXPR;
22028 code = BIT_IOR_EXPR;
22031 code = TRUTH_ANDIF_EXPR;
22034 code = TRUTH_ORIF_EXPR;
22037 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
22038 "%<|%>, %<&&%>, or %<||%>");
22040 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22041 /*or_comma=*/false,
22042 /*consume_paren=*/true);
22045 cp_lexer_consume_token (parser->lexer);
22047 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22050 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
22051 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
22052 OMP_CLAUSE_REDUCTION_CODE (c) = code;
22058 schedule ( schedule-kind )
22059 schedule ( schedule-kind , expression )
22062 static | dynamic | guided | runtime | auto */
22065 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
22069 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22072 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
22074 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22076 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22077 const char *p = IDENTIFIER_POINTER (id);
22082 if (strcmp ("dynamic", p) != 0)
22084 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
22088 if (strcmp ("guided", p) != 0)
22090 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
22094 if (strcmp ("runtime", p) != 0)
22096 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
22103 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
22104 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
22105 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
22106 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
22109 cp_lexer_consume_token (parser->lexer);
22111 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22114 cp_lexer_consume_token (parser->lexer);
22116 token = cp_lexer_peek_token (parser->lexer);
22117 t = cp_parser_assignment_expression (parser, false, NULL);
22119 if (t == error_mark_node)
22121 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
22122 error_at (token->location, "schedule %<runtime%> does not take "
22123 "a %<chunk_size%> parameter");
22124 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
22125 error_at (token->location, "schedule %<auto%> does not take "
22126 "a %<chunk_size%> parameter");
22128 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
22130 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22133 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
22136 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
22137 OMP_CLAUSE_CHAIN (c) = list;
22141 cp_parser_error (parser, "invalid schedule kind");
22143 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22144 /*or_comma=*/false,
22145 /*consume_paren=*/true);
22153 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
22154 tree list, location_t location)
22158 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
22160 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
22161 OMP_CLAUSE_CHAIN (c) = list;
22165 /* Parse all OpenMP clauses. The set clauses allowed by the directive
22166 is a bitmask in MASK. Return the list of clauses found; the result
22167 of clause default goes in *pdefault. */
22170 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
22171 const char *where, cp_token *pragma_tok)
22173 tree clauses = NULL;
22175 cp_token *token = NULL;
22177 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
22179 pragma_omp_clause c_kind;
22180 const char *c_name;
22181 tree prev = clauses;
22183 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22184 cp_lexer_consume_token (parser->lexer);
22186 token = cp_lexer_peek_token (parser->lexer);
22187 c_kind = cp_parser_omp_clause_name (parser);
22192 case PRAGMA_OMP_CLAUSE_COLLAPSE:
22193 clauses = cp_parser_omp_clause_collapse (parser, clauses,
22195 c_name = "collapse";
22197 case PRAGMA_OMP_CLAUSE_COPYIN:
22198 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
22201 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
22202 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
22204 c_name = "copyprivate";
22206 case PRAGMA_OMP_CLAUSE_DEFAULT:
22207 clauses = cp_parser_omp_clause_default (parser, clauses,
22209 c_name = "default";
22211 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
22212 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
22214 c_name = "firstprivate";
22216 case PRAGMA_OMP_CLAUSE_IF:
22217 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
22220 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
22221 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
22223 c_name = "lastprivate";
22225 case PRAGMA_OMP_CLAUSE_NOWAIT:
22226 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
22229 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
22230 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
22232 c_name = "num_threads";
22234 case PRAGMA_OMP_CLAUSE_ORDERED:
22235 clauses = cp_parser_omp_clause_ordered (parser, clauses,
22237 c_name = "ordered";
22239 case PRAGMA_OMP_CLAUSE_PRIVATE:
22240 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
22242 c_name = "private";
22244 case PRAGMA_OMP_CLAUSE_REDUCTION:
22245 clauses = cp_parser_omp_clause_reduction (parser, clauses);
22246 c_name = "reduction";
22248 case PRAGMA_OMP_CLAUSE_SCHEDULE:
22249 clauses = cp_parser_omp_clause_schedule (parser, clauses,
22251 c_name = "schedule";
22253 case PRAGMA_OMP_CLAUSE_SHARED:
22254 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
22258 case PRAGMA_OMP_CLAUSE_UNTIED:
22259 clauses = cp_parser_omp_clause_untied (parser, clauses,
22264 cp_parser_error (parser, "expected %<#pragma omp%> clause");
22268 if (((mask >> c_kind) & 1) == 0)
22270 /* Remove the invalid clause(s) from the list to avoid
22271 confusing the rest of the compiler. */
22273 error_at (token->location, "%qs is not valid for %qs", c_name, where);
22277 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22278 return finish_omp_clauses (clauses);
22285 In practice, we're also interested in adding the statement to an
22286 outer node. So it is convenient if we work around the fact that
22287 cp_parser_statement calls add_stmt. */
22290 cp_parser_begin_omp_structured_block (cp_parser *parser)
22292 unsigned save = parser->in_statement;
22294 /* Only move the values to IN_OMP_BLOCK if they weren't false.
22295 This preserves the "not within loop or switch" style error messages
22296 for nonsense cases like
22302 if (parser->in_statement)
22303 parser->in_statement = IN_OMP_BLOCK;
22309 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
22311 parser->in_statement = save;
22315 cp_parser_omp_structured_block (cp_parser *parser)
22317 tree stmt = begin_omp_structured_block ();
22318 unsigned int save = cp_parser_begin_omp_structured_block (parser);
22320 cp_parser_statement (parser, NULL_TREE, false, NULL);
22322 cp_parser_end_omp_structured_block (parser, save);
22323 return finish_omp_structured_block (stmt);
22327 # pragma omp atomic new-line
22331 x binop= expr | x++ | ++x | x-- | --x
22333 +, *, -, /, &, ^, |, <<, >>
22335 where x is an lvalue expression with scalar type. */
22338 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
22341 enum tree_code code;
22343 cp_parser_require_pragma_eol (parser, pragma_tok);
22345 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
22346 /*cast_p=*/false, NULL);
22347 switch (TREE_CODE (lhs))
22352 case PREINCREMENT_EXPR:
22353 case POSTINCREMENT_EXPR:
22354 lhs = TREE_OPERAND (lhs, 0);
22356 rhs = integer_one_node;
22359 case PREDECREMENT_EXPR:
22360 case POSTDECREMENT_EXPR:
22361 lhs = TREE_OPERAND (lhs, 0);
22363 rhs = integer_one_node;
22367 switch (cp_lexer_peek_token (parser->lexer)->type)
22373 code = TRUNC_DIV_EXPR;
22381 case CPP_LSHIFT_EQ:
22382 code = LSHIFT_EXPR;
22384 case CPP_RSHIFT_EQ:
22385 code = RSHIFT_EXPR;
22388 code = BIT_AND_EXPR;
22391 code = BIT_IOR_EXPR;
22394 code = BIT_XOR_EXPR;
22397 cp_parser_error (parser,
22398 "invalid operator for %<#pragma omp atomic%>");
22401 cp_lexer_consume_token (parser->lexer);
22403 rhs = cp_parser_expression (parser, false, NULL);
22404 if (rhs == error_mark_node)
22408 finish_omp_atomic (code, lhs, rhs);
22409 cp_parser_consume_semicolon_at_end_of_statement (parser);
22413 cp_parser_skip_to_end_of_block_or_statement (parser);
22418 # pragma omp barrier new-line */
22421 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
22423 cp_parser_require_pragma_eol (parser, pragma_tok);
22424 finish_omp_barrier ();
22428 # pragma omp critical [(name)] new-line
22429 structured-block */
22432 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
22434 tree stmt, name = NULL;
22436 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22438 cp_lexer_consume_token (parser->lexer);
22440 name = cp_parser_identifier (parser);
22442 if (name == error_mark_node
22443 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22444 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22445 /*or_comma=*/false,
22446 /*consume_paren=*/true);
22447 if (name == error_mark_node)
22450 cp_parser_require_pragma_eol (parser, pragma_tok);
22452 stmt = cp_parser_omp_structured_block (parser);
22453 return c_finish_omp_critical (input_location, stmt, name);
22457 # pragma omp flush flush-vars[opt] new-line
22460 ( variable-list ) */
22463 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22465 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22466 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22467 cp_parser_require_pragma_eol (parser, pragma_tok);
22469 finish_omp_flush ();
22472 /* Helper function, to parse omp for increment expression. */
22475 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22477 tree cond = cp_parser_binary_expression (parser, false, true,
22478 PREC_NOT_OPERATOR, NULL);
22481 if (cond == error_mark_node
22482 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22484 cp_parser_skip_to_end_of_statement (parser);
22485 return error_mark_node;
22488 switch (TREE_CODE (cond))
22496 return error_mark_node;
22499 /* If decl is an iterator, preserve LHS and RHS of the relational
22500 expr until finish_omp_for. */
22502 && (type_dependent_expression_p (decl)
22503 || CLASS_TYPE_P (TREE_TYPE (decl))))
22506 return build_x_binary_op (TREE_CODE (cond),
22507 TREE_OPERAND (cond, 0), ERROR_MARK,
22508 TREE_OPERAND (cond, 1), ERROR_MARK,
22509 &overloaded_p, tf_warning_or_error);
22512 /* Helper function, to parse omp for increment expression. */
22515 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22517 cp_token *token = cp_lexer_peek_token (parser->lexer);
22523 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22525 op = (token->type == CPP_PLUS_PLUS
22526 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22527 cp_lexer_consume_token (parser->lexer);
22528 lhs = cp_parser_cast_expression (parser, false, false, NULL);
22530 return error_mark_node;
22531 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22534 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22536 return error_mark_node;
22538 token = cp_lexer_peek_token (parser->lexer);
22539 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22541 op = (token->type == CPP_PLUS_PLUS
22542 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22543 cp_lexer_consume_token (parser->lexer);
22544 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22547 op = cp_parser_assignment_operator_opt (parser);
22548 if (op == ERROR_MARK)
22549 return error_mark_node;
22551 if (op != NOP_EXPR)
22553 rhs = cp_parser_assignment_expression (parser, false, NULL);
22554 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22555 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22558 lhs = cp_parser_binary_expression (parser, false, false,
22559 PREC_ADDITIVE_EXPRESSION, NULL);
22560 token = cp_lexer_peek_token (parser->lexer);
22561 decl_first = lhs == decl;
22564 if (token->type != CPP_PLUS
22565 && token->type != CPP_MINUS)
22566 return error_mark_node;
22570 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22571 cp_lexer_consume_token (parser->lexer);
22572 rhs = cp_parser_binary_expression (parser, false, false,
22573 PREC_ADDITIVE_EXPRESSION, NULL);
22574 token = cp_lexer_peek_token (parser->lexer);
22575 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22577 if (lhs == NULL_TREE)
22579 if (op == PLUS_EXPR)
22582 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22585 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22586 NULL, tf_warning_or_error);
22589 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22593 if (rhs != decl || op == MINUS_EXPR)
22594 return error_mark_node;
22595 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22598 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22600 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22603 /* Parse the restricted form of the for statement allowed by OpenMP. */
22606 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22608 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22609 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22610 tree this_pre_body, cl;
22611 location_t loc_first;
22612 bool collapse_err = false;
22613 int i, collapse = 1, nbraces = 0;
22615 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22616 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22617 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22619 gcc_assert (collapse >= 1);
22621 declv = make_tree_vec (collapse);
22622 initv = make_tree_vec (collapse);
22623 condv = make_tree_vec (collapse);
22624 incrv = make_tree_vec (collapse);
22626 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22628 for (i = 0; i < collapse; i++)
22630 int bracecount = 0;
22631 bool add_private_clause = false;
22634 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22636 cp_parser_error (parser, "for statement expected");
22639 loc = cp_lexer_consume_token (parser->lexer)->location;
22641 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22644 init = decl = real_decl = NULL;
22645 this_pre_body = push_stmt_list ();
22646 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22648 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22652 integer-type var = lb
22653 random-access-iterator-type var = lb
22654 pointer-type var = lb
22656 cp_decl_specifier_seq type_specifiers;
22658 /* First, try to parse as an initialized declaration. See
22659 cp_parser_condition, from whence the bulk of this is copied. */
22661 cp_parser_parse_tentatively (parser);
22662 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22663 /*is_trailing_return=*/false,
22665 if (cp_parser_parse_definitely (parser))
22667 /* If parsing a type specifier seq succeeded, then this
22668 MUST be a initialized declaration. */
22669 tree asm_specification, attributes;
22670 cp_declarator *declarator;
22672 declarator = cp_parser_declarator (parser,
22673 CP_PARSER_DECLARATOR_NAMED,
22674 /*ctor_dtor_or_conv_p=*/NULL,
22675 /*parenthesized_p=*/NULL,
22676 /*member_p=*/false);
22677 attributes = cp_parser_attributes_opt (parser);
22678 asm_specification = cp_parser_asm_specification_opt (parser);
22680 if (declarator == cp_error_declarator)
22681 cp_parser_skip_to_end_of_statement (parser);
22685 tree pushed_scope, auto_node;
22687 decl = start_decl (declarator, &type_specifiers,
22688 SD_INITIALIZED, attributes,
22689 /*prefix_attributes=*/NULL_TREE,
22692 auto_node = type_uses_auto (TREE_TYPE (decl));
22693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22695 if (cp_lexer_next_token_is (parser->lexer,
22697 error ("parenthesized initialization is not allowed in "
22698 "OpenMP %<for%> loop");
22700 /* Trigger an error. */
22701 cp_parser_require (parser, CPP_EQ, RT_EQ);
22703 init = error_mark_node;
22704 cp_parser_skip_to_end_of_statement (parser);
22706 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22707 || type_dependent_expression_p (decl)
22710 bool is_direct_init, is_non_constant_init;
22712 init = cp_parser_initializer (parser,
22714 &is_non_constant_init);
22716 if (auto_node && describable_type (init))
22719 = do_auto_deduction (TREE_TYPE (decl), init,
22722 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22723 && !type_dependent_expression_p (decl))
22727 cp_finish_decl (decl, init, !is_non_constant_init,
22729 LOOKUP_ONLYCONVERTING);
22730 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22733 = tree_cons (NULL, this_pre_body, for_block);
22737 init = pop_stmt_list (this_pre_body);
22738 this_pre_body = NULL_TREE;
22743 cp_lexer_consume_token (parser->lexer);
22744 init = cp_parser_assignment_expression (parser, false, NULL);
22747 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22748 init = error_mark_node;
22750 cp_finish_decl (decl, NULL_TREE,
22751 /*init_const_expr_p=*/false,
22753 LOOKUP_ONLYCONVERTING);
22757 pop_scope (pushed_scope);
22763 /* If parsing a type specifier sequence failed, then
22764 this MUST be a simple expression. */
22765 cp_parser_parse_tentatively (parser);
22766 decl = cp_parser_primary_expression (parser, false, false,
22768 if (!cp_parser_error_occurred (parser)
22771 && CLASS_TYPE_P (TREE_TYPE (decl)))
22775 cp_parser_parse_definitely (parser);
22776 cp_parser_require (parser, CPP_EQ, RT_EQ);
22777 rhs = cp_parser_assignment_expression (parser, false, NULL);
22778 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22780 tf_warning_or_error));
22781 add_private_clause = true;
22786 cp_parser_abort_tentative_parse (parser);
22787 init = cp_parser_expression (parser, false, NULL);
22790 if (TREE_CODE (init) == MODIFY_EXPR
22791 || TREE_CODE (init) == MODOP_EXPR)
22792 real_decl = TREE_OPERAND (init, 0);
22797 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22800 this_pre_body = pop_stmt_list (this_pre_body);
22804 pre_body = push_stmt_list ();
22806 add_stmt (this_pre_body);
22807 pre_body = pop_stmt_list (pre_body);
22810 pre_body = this_pre_body;
22815 if (par_clauses != NULL && real_decl != NULL_TREE)
22818 for (c = par_clauses; *c ; )
22819 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22820 && OMP_CLAUSE_DECL (*c) == real_decl)
22822 error_at (loc, "iteration variable %qD"
22823 " should not be firstprivate", real_decl);
22824 *c = OMP_CLAUSE_CHAIN (*c);
22826 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22827 && OMP_CLAUSE_DECL (*c) == real_decl)
22829 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22830 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22831 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22832 OMP_CLAUSE_DECL (l) = real_decl;
22833 OMP_CLAUSE_CHAIN (l) = clauses;
22834 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22836 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22837 CP_OMP_CLAUSE_INFO (*c) = NULL;
22838 add_private_clause = false;
22842 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22843 && OMP_CLAUSE_DECL (*c) == real_decl)
22844 add_private_clause = false;
22845 c = &OMP_CLAUSE_CHAIN (*c);
22849 if (add_private_clause)
22852 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22854 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22855 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22856 && OMP_CLAUSE_DECL (c) == decl)
22858 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22859 && OMP_CLAUSE_DECL (c) == decl)
22860 error_at (loc, "iteration variable %qD "
22861 "should not be firstprivate",
22863 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22864 && OMP_CLAUSE_DECL (c) == decl)
22865 error_at (loc, "iteration variable %qD should not be reduction",
22870 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22871 OMP_CLAUSE_DECL (c) = decl;
22872 c = finish_omp_clauses (c);
22875 OMP_CLAUSE_CHAIN (c) = clauses;
22882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22883 cond = cp_parser_omp_for_cond (parser, decl);
22884 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22887 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22889 /* If decl is an iterator, preserve the operator on decl
22890 until finish_omp_for. */
22892 && (type_dependent_expression_p (decl)
22893 || CLASS_TYPE_P (TREE_TYPE (decl))))
22894 incr = cp_parser_omp_for_incr (parser, decl);
22896 incr = cp_parser_expression (parser, false, NULL);
22899 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22900 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22901 /*or_comma=*/false,
22902 /*consume_paren=*/true);
22904 TREE_VEC_ELT (declv, i) = decl;
22905 TREE_VEC_ELT (initv, i) = init;
22906 TREE_VEC_ELT (condv, i) = cond;
22907 TREE_VEC_ELT (incrv, i) = incr;
22909 if (i == collapse - 1)
22912 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22913 in between the collapsed for loops to be still considered perfectly
22914 nested. Hopefully the final version clarifies this.
22915 For now handle (multiple) {'s and empty statements. */
22916 cp_parser_parse_tentatively (parser);
22919 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22921 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22923 cp_lexer_consume_token (parser->lexer);
22926 else if (bracecount
22927 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22928 cp_lexer_consume_token (parser->lexer);
22931 loc = cp_lexer_peek_token (parser->lexer)->location;
22932 error_at (loc, "not enough collapsed for loops");
22933 collapse_err = true;
22934 cp_parser_abort_tentative_parse (parser);
22943 cp_parser_parse_definitely (parser);
22944 nbraces += bracecount;
22948 /* Note that we saved the original contents of this flag when we entered
22949 the structured block, and so we don't need to re-save it here. */
22950 parser->in_statement = IN_OMP_FOR;
22952 /* Note that the grammar doesn't call for a structured block here,
22953 though the loop as a whole is a structured block. */
22954 body = push_stmt_list ();
22955 cp_parser_statement (parser, NULL_TREE, false, NULL);
22956 body = pop_stmt_list (body);
22958 if (declv == NULL_TREE)
22961 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22962 pre_body, clauses);
22966 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22968 cp_lexer_consume_token (parser->lexer);
22971 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22972 cp_lexer_consume_token (parser->lexer);
22977 error_at (cp_lexer_peek_token (parser->lexer)->location,
22978 "collapsed loops not perfectly nested");
22980 collapse_err = true;
22981 cp_parser_statement_seq_opt (parser, NULL);
22982 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22989 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22990 for_block = TREE_CHAIN (for_block);
22997 #pragma omp for for-clause[optseq] new-line
23000 #define OMP_FOR_CLAUSE_MASK \
23001 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23002 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23003 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
23004 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23005 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
23006 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
23007 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
23008 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
23011 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
23013 tree clauses, sb, ret;
23016 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
23017 "#pragma omp for", pragma_tok);
23019 sb = begin_omp_structured_block ();
23020 save = cp_parser_begin_omp_structured_block (parser);
23022 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
23024 cp_parser_end_omp_structured_block (parser, save);
23025 add_stmt (finish_omp_structured_block (sb));
23031 # pragma omp master new-line
23032 structured-block */
23035 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
23037 cp_parser_require_pragma_eol (parser, pragma_tok);
23038 return c_finish_omp_master (input_location,
23039 cp_parser_omp_structured_block (parser));
23043 # pragma omp ordered new-line
23044 structured-block */
23047 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
23049 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23050 cp_parser_require_pragma_eol (parser, pragma_tok);
23051 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
23057 { section-sequence }
23060 section-directive[opt] structured-block
23061 section-sequence section-directive structured-block */
23064 cp_parser_omp_sections_scope (cp_parser *parser)
23066 tree stmt, substmt;
23067 bool error_suppress = false;
23070 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23073 stmt = push_stmt_list ();
23075 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
23079 substmt = begin_omp_structured_block ();
23080 save = cp_parser_begin_omp_structured_block (parser);
23084 cp_parser_statement (parser, NULL_TREE, false, NULL);
23086 tok = cp_lexer_peek_token (parser->lexer);
23087 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23089 if (tok->type == CPP_CLOSE_BRACE)
23091 if (tok->type == CPP_EOF)
23095 cp_parser_end_omp_structured_block (parser, save);
23096 substmt = finish_omp_structured_block (substmt);
23097 substmt = build1 (OMP_SECTION, void_type_node, substmt);
23098 add_stmt (substmt);
23103 tok = cp_lexer_peek_token (parser->lexer);
23104 if (tok->type == CPP_CLOSE_BRACE)
23106 if (tok->type == CPP_EOF)
23109 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23111 cp_lexer_consume_token (parser->lexer);
23112 cp_parser_require_pragma_eol (parser, tok);
23113 error_suppress = false;
23115 else if (!error_suppress)
23117 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
23118 error_suppress = true;
23121 substmt = cp_parser_omp_structured_block (parser);
23122 substmt = build1 (OMP_SECTION, void_type_node, substmt);
23123 add_stmt (substmt);
23125 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23127 substmt = pop_stmt_list (stmt);
23129 stmt = make_node (OMP_SECTIONS);
23130 TREE_TYPE (stmt) = void_type_node;
23131 OMP_SECTIONS_BODY (stmt) = substmt;
23138 # pragma omp sections sections-clause[optseq] newline
23141 #define OMP_SECTIONS_CLAUSE_MASK \
23142 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23143 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23144 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
23145 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23146 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23149 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
23153 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
23154 "#pragma omp sections", pragma_tok);
23156 ret = cp_parser_omp_sections_scope (parser);
23158 OMP_SECTIONS_CLAUSES (ret) = clauses;
23164 # pragma parallel parallel-clause new-line
23165 # pragma parallel for parallel-for-clause new-line
23166 # pragma parallel sections parallel-sections-clause new-line */
23168 #define OMP_PARALLEL_CLAUSE_MASK \
23169 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
23170 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23171 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23172 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
23173 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
23174 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
23175 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23176 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
23179 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
23181 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
23182 const char *p_name = "#pragma omp parallel";
23183 tree stmt, clauses, par_clause, ws_clause, block;
23184 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
23186 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23188 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23190 cp_lexer_consume_token (parser->lexer);
23191 p_kind = PRAGMA_OMP_PARALLEL_FOR;
23192 p_name = "#pragma omp parallel for";
23193 mask |= OMP_FOR_CLAUSE_MASK;
23194 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
23196 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23198 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23199 const char *p = IDENTIFIER_POINTER (id);
23200 if (strcmp (p, "sections") == 0)
23202 cp_lexer_consume_token (parser->lexer);
23203 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
23204 p_name = "#pragma omp parallel sections";
23205 mask |= OMP_SECTIONS_CLAUSE_MASK;
23206 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
23210 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
23211 block = begin_omp_parallel ();
23212 save = cp_parser_begin_omp_structured_block (parser);
23216 case PRAGMA_OMP_PARALLEL:
23217 cp_parser_statement (parser, NULL_TREE, false, NULL);
23218 par_clause = clauses;
23221 case PRAGMA_OMP_PARALLEL_FOR:
23222 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
23223 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
23226 case PRAGMA_OMP_PARALLEL_SECTIONS:
23227 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
23228 stmt = cp_parser_omp_sections_scope (parser);
23230 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
23234 gcc_unreachable ();
23237 cp_parser_end_omp_structured_block (parser, save);
23238 stmt = finish_omp_parallel (par_clause, block);
23239 if (p_kind != PRAGMA_OMP_PARALLEL)
23240 OMP_PARALLEL_COMBINED (stmt) = 1;
23245 # pragma omp single single-clause[optseq] new-line
23246 structured-block */
23248 #define OMP_SINGLE_CLAUSE_MASK \
23249 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23250 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23251 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
23252 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23255 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
23257 tree stmt = make_node (OMP_SINGLE);
23258 TREE_TYPE (stmt) = void_type_node;
23260 OMP_SINGLE_CLAUSES (stmt)
23261 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
23262 "#pragma omp single", pragma_tok);
23263 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
23265 return add_stmt (stmt);
23269 # pragma omp task task-clause[optseq] new-line
23270 structured-block */
23272 #define OMP_TASK_CLAUSE_MASK \
23273 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
23274 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
23275 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
23276 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23277 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23278 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
23281 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
23283 tree clauses, block;
23286 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
23287 "#pragma omp task", pragma_tok);
23288 block = begin_omp_task ();
23289 save = cp_parser_begin_omp_structured_block (parser);
23290 cp_parser_statement (parser, NULL_TREE, false, NULL);
23291 cp_parser_end_omp_structured_block (parser, save);
23292 return finish_omp_task (clauses, block);
23296 # pragma omp taskwait new-line */
23299 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
23301 cp_parser_require_pragma_eol (parser, pragma_tok);
23302 finish_omp_taskwait ();
23306 # pragma omp threadprivate (variable-list) */
23309 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
23313 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23314 cp_parser_require_pragma_eol (parser, pragma_tok);
23316 finish_omp_threadprivate (vars);
23319 /* Main entry point to OpenMP statement pragmas. */
23322 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
23326 switch (pragma_tok->pragma_kind)
23328 case PRAGMA_OMP_ATOMIC:
23329 cp_parser_omp_atomic (parser, pragma_tok);
23331 case PRAGMA_OMP_CRITICAL:
23332 stmt = cp_parser_omp_critical (parser, pragma_tok);
23334 case PRAGMA_OMP_FOR:
23335 stmt = cp_parser_omp_for (parser, pragma_tok);
23337 case PRAGMA_OMP_MASTER:
23338 stmt = cp_parser_omp_master (parser, pragma_tok);
23340 case PRAGMA_OMP_ORDERED:
23341 stmt = cp_parser_omp_ordered (parser, pragma_tok);
23343 case PRAGMA_OMP_PARALLEL:
23344 stmt = cp_parser_omp_parallel (parser, pragma_tok);
23346 case PRAGMA_OMP_SECTIONS:
23347 stmt = cp_parser_omp_sections (parser, pragma_tok);
23349 case PRAGMA_OMP_SINGLE:
23350 stmt = cp_parser_omp_single (parser, pragma_tok);
23352 case PRAGMA_OMP_TASK:
23353 stmt = cp_parser_omp_task (parser, pragma_tok);
23356 gcc_unreachable ();
23360 SET_EXPR_LOCATION (stmt, pragma_tok->location);
23365 static GTY (()) cp_parser *the_parser;
23368 /* Special handling for the first token or line in the file. The first
23369 thing in the file might be #pragma GCC pch_preprocess, which loads a
23370 PCH file, which is a GC collection point. So we need to handle this
23371 first pragma without benefit of an existing lexer structure.
23373 Always returns one token to the caller in *FIRST_TOKEN. This is
23374 either the true first token of the file, or the first token after
23375 the initial pragma. */
23378 cp_parser_initial_pragma (cp_token *first_token)
23382 cp_lexer_get_preprocessor_token (NULL, first_token);
23383 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
23386 cp_lexer_get_preprocessor_token (NULL, first_token);
23387 if (first_token->type == CPP_STRING)
23389 name = first_token->u.value;
23391 cp_lexer_get_preprocessor_token (NULL, first_token);
23392 if (first_token->type != CPP_PRAGMA_EOL)
23393 error_at (first_token->location,
23394 "junk at end of %<#pragma GCC pch_preprocess%>");
23397 error_at (first_token->location, "expected string literal");
23399 /* Skip to the end of the pragma. */
23400 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
23401 cp_lexer_get_preprocessor_token (NULL, first_token);
23403 /* Now actually load the PCH file. */
23405 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
23407 /* Read one more token to return to our caller. We have to do this
23408 after reading the PCH file in, since its pointers have to be
23410 cp_lexer_get_preprocessor_token (NULL, first_token);
23413 /* Normal parsing of a pragma token. Here we can (and must) use the
23417 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
23419 cp_token *pragma_tok;
23422 pragma_tok = cp_lexer_consume_token (parser->lexer);
23423 gcc_assert (pragma_tok->type == CPP_PRAGMA);
23424 parser->lexer->in_pragma = true;
23426 id = pragma_tok->pragma_kind;
23429 case PRAGMA_GCC_PCH_PREPROCESS:
23430 error_at (pragma_tok->location,
23431 "%<#pragma GCC pch_preprocess%> must be first");
23434 case PRAGMA_OMP_BARRIER:
23437 case pragma_compound:
23438 cp_parser_omp_barrier (parser, pragma_tok);
23441 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23442 "used in compound statements");
23449 case PRAGMA_OMP_FLUSH:
23452 case pragma_compound:
23453 cp_parser_omp_flush (parser, pragma_tok);
23456 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23457 "used in compound statements");
23464 case PRAGMA_OMP_TASKWAIT:
23467 case pragma_compound:
23468 cp_parser_omp_taskwait (parser, pragma_tok);
23471 error_at (pragma_tok->location,
23472 "%<#pragma omp taskwait%> may only be "
23473 "used in compound statements");
23480 case PRAGMA_OMP_THREADPRIVATE:
23481 cp_parser_omp_threadprivate (parser, pragma_tok);
23484 case PRAGMA_OMP_ATOMIC:
23485 case PRAGMA_OMP_CRITICAL:
23486 case PRAGMA_OMP_FOR:
23487 case PRAGMA_OMP_MASTER:
23488 case PRAGMA_OMP_ORDERED:
23489 case PRAGMA_OMP_PARALLEL:
23490 case PRAGMA_OMP_SECTIONS:
23491 case PRAGMA_OMP_SINGLE:
23492 case PRAGMA_OMP_TASK:
23493 if (context == pragma_external)
23495 cp_parser_omp_construct (parser, pragma_tok);
23498 case PRAGMA_OMP_SECTION:
23499 error_at (pragma_tok->location,
23500 "%<#pragma omp section%> may only be used in "
23501 "%<#pragma omp sections%> construct");
23505 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23506 c_invoke_pragma_handler (id);
23510 cp_parser_error (parser, "expected declaration specifiers");
23514 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23518 /* The interface the pragma parsers have to the lexer. */
23521 pragma_lex (tree *value)
23524 enum cpp_ttype ret;
23526 tok = cp_lexer_peek_token (the_parser->lexer);
23529 *value = tok->u.value;
23531 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23533 else if (ret == CPP_STRING)
23534 *value = cp_parser_string_literal (the_parser, false, false);
23537 cp_lexer_consume_token (the_parser->lexer);
23538 if (ret == CPP_KEYWORD)
23546 /* External interface. */
23548 /* Parse one entire translation unit. */
23551 c_parse_file (void)
23553 static bool already_called = false;
23555 if (already_called)
23557 sorry ("inter-module optimizations not implemented for C++");
23560 already_called = true;
23562 the_parser = cp_parser_new ();
23563 push_deferring_access_checks (flag_access_control
23564 ? dk_no_deferred : dk_no_check);
23565 cp_parser_translation_unit (the_parser);
23569 #include "gt-cp-parser.h"