2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
30 #include "c-family/c-pragma.h"
33 #include "diagnostic-core.h"
37 #include "c-family/c-common.h"
38 #include "c-family/c-objc.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct GTY(()) tree_check {
51 /* The value associated with the token. */
53 /* The checks that have been associated with value. */
54 VEC (deferred_access_check, gc)* checks;
55 /* The token's qualifying scope (used when it is a
56 CPP_NESTED_NAME_SPECIFIER). */
57 tree qualifying_scope;
62 typedef struct GTY (()) cp_token {
63 /* The kind of token. */
64 ENUM_BITFIELD (cpp_ttype) type : 8;
65 /* If this token is a keyword, this value indicates which keyword.
66 Otherwise, this value is RID_MAX. */
67 ENUM_BITFIELD (rid) keyword : 8;
70 /* Identifier for the pragma. */
71 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
72 /* True if this token is from a context where it is implicitly extern "C" */
73 BOOL_BITFIELD implicit_extern_c : 1;
74 /* True for a CPP_NAME token that is not a keyword (i.e., for which
75 KEYWORD is RID_MAX) iff this name was looked up and found to be
76 ambiguous. An error has already been reported. */
77 BOOL_BITFIELD ambiguous_p : 1;
78 /* The location at which this token was found. */
80 /* The value associated with this token, if any. */
81 union cp_token_value {
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check* GTY((tag ("1"))) tree_check_value;
84 /* Use for all other tokens. */
85 tree GTY((tag ("0"))) value;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 /* We use a stack of token pointer for saving token sets. */
90 typedef struct cp_token *cp_token_position;
91 DEF_VEC_P (cp_token_position);
92 DEF_VEC_ALLOC_P (cp_token_position,heap);
94 static cp_token eof_token =
96 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
99 /* The cp_lexer structure represents the C++ lexer. It is responsible
100 for managing the token stream from the preprocessor and supplying
101 it to the parser. Tokens are never added to the cp_lexer after
104 typedef struct GTY (()) cp_lexer {
105 /* The memory allocated for the buffer. NULL if this lexer does not
106 own the token buffer. */
107 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
108 /* If the lexer owns the buffer, this is the number of tokens in the
110 size_t buffer_length;
112 /* A pointer just past the last available token. The tokens
113 in this lexer are [buffer, last_token). */
114 cp_token_position GTY ((skip)) last_token;
116 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
117 no more available tokens. */
118 cp_token_position GTY ((skip)) next_token;
120 /* A stack indicating positions at which cp_lexer_save_tokens was
121 called. The top entry is the most recent position at which we
122 began saving tokens. If the stack is non-empty, we are saving
124 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
126 /* The next lexer in a linked list of lexers. */
127 struct cp_lexer *next;
129 /* True if we should output debugging information. */
132 /* True if we're in the context of parsing a pragma, and should not
133 increment past the end-of-line marker. */
137 /* cp_token_cache is a range of tokens. There is no need to represent
138 allocate heap memory for it, since tokens are never removed from the
139 lexer's array. There is also no need for the GC to walk through
140 a cp_token_cache, since everything in here is referenced through
143 typedef struct GTY(()) cp_token_cache {
144 /* The beginning of the token range. */
145 cp_token * GTY((skip)) first;
147 /* Points immediately after the last token in the range. */
148 cp_token * GTY ((skip)) last;
151 /* The various kinds of non integral constant we encounter. */
152 typedef enum non_integral_constant {
154 /* floating-point literal */
158 /* %<__FUNCTION__%> */
160 /* %<__PRETTY_FUNCTION__%> */
168 /* %<typeid%> operator */
170 /* non-constant compound literals */
172 /* a function call */
178 /* an array reference */
184 /* the address of a label */
198 /* calls to overloaded operators */
202 /* a comma operator */
204 /* a call to a constructor */
206 } non_integral_constant;
208 /* The various kinds of errors about name-lookup failing. */
209 typedef enum name_lookup_error {
214 /* is not a class or namespace */
216 /* is not a class, namespace, or enumeration */
220 /* The various kinds of required token */
221 typedef enum required_token {
223 RT_SEMICOLON, /* ';' */
224 RT_OPEN_PAREN, /* '(' */
225 RT_CLOSE_BRACE, /* '}' */
226 RT_OPEN_BRACE, /* '{' */
227 RT_CLOSE_SQUARE, /* ']' */
228 RT_OPEN_SQUARE, /* '[' */
232 RT_GREATER, /* '>' */
234 RT_ELLIPSIS, /* '...' */
238 RT_COLON_SCOPE, /* ':' or '::' */
239 RT_CLOSE_PAREN, /* ')' */
240 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
241 RT_PRAGMA_EOL, /* end of line */
242 RT_NAME, /* identifier */
244 /* The type is CPP_KEYWORD */
246 RT_DELETE, /* delete */
247 RT_RETURN, /* return */
248 RT_WHILE, /* while */
249 RT_EXTERN, /* extern */
250 RT_STATIC_ASSERT, /* static_assert */
251 RT_DECLTYPE, /* decltype */
252 RT_OPERATOR, /* operator */
253 RT_CLASS, /* class */
254 RT_TEMPLATE, /* template */
255 RT_NAMESPACE, /* namespace */
256 RT_USING, /* using */
259 RT_CATCH, /* catch */
260 RT_THROW, /* throw */
261 RT_LABEL, /* __label__ */
262 RT_AT_TRY, /* @try */
263 RT_AT_SYNCHRONIZED, /* @synchronized */
264 RT_AT_THROW, /* @throw */
266 RT_SELECT, /* selection-statement */
267 RT_INTERATION, /* iteration-statement */
268 RT_JUMP, /* jump-statement */
269 RT_CLASS_KEY, /* class-key */
270 RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
275 static cp_lexer *cp_lexer_new_main
277 static cp_lexer *cp_lexer_new_from_tokens
278 (cp_token_cache *tokens);
279 static void cp_lexer_destroy
281 static int cp_lexer_saving_tokens
283 static cp_token_position cp_lexer_token_position
285 static cp_token *cp_lexer_token_at
286 (cp_lexer *, cp_token_position);
287 static void cp_lexer_get_preprocessor_token
288 (cp_lexer *, cp_token *);
289 static inline cp_token *cp_lexer_peek_token
291 static cp_token *cp_lexer_peek_nth_token
292 (cp_lexer *, size_t);
293 static inline bool cp_lexer_next_token_is
294 (cp_lexer *, enum cpp_ttype);
295 static bool cp_lexer_next_token_is_not
296 (cp_lexer *, enum cpp_ttype);
297 static bool cp_lexer_next_token_is_keyword
298 (cp_lexer *, enum rid);
299 static cp_token *cp_lexer_consume_token
301 static void cp_lexer_purge_token
303 static void cp_lexer_purge_tokens_after
304 (cp_lexer *, cp_token_position);
305 static void cp_lexer_save_tokens
307 static void cp_lexer_commit_tokens
309 static void cp_lexer_rollback_tokens
311 #ifdef ENABLE_CHECKING
312 static void cp_lexer_print_token
313 (FILE *, cp_token *);
314 static inline bool cp_lexer_debugging_p
316 static void cp_lexer_start_debugging
317 (cp_lexer *) ATTRIBUTE_UNUSED;
318 static void cp_lexer_stop_debugging
319 (cp_lexer *) ATTRIBUTE_UNUSED;
321 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
322 about passing NULL to functions that require non-NULL arguments
323 (fputs, fprintf). It will never be used, so all we need is a value
324 of the right type that's guaranteed not to be NULL. */
325 #define cp_lexer_debug_stream stdout
326 #define cp_lexer_print_token(str, tok) (void) 0
327 #define cp_lexer_debugging_p(lexer) 0
328 #endif /* ENABLE_CHECKING */
330 static cp_token_cache *cp_token_cache_new
331 (cp_token *, cp_token *);
333 static void cp_parser_initial_pragma
336 /* Manifest constants. */
337 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
338 #define CP_SAVED_TOKEN_STACK 5
340 /* A token type for keywords, as opposed to ordinary identifiers. */
341 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
343 /* A token type for template-ids. If a template-id is processed while
344 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
345 the value of the CPP_TEMPLATE_ID is whatever was returned by
346 cp_parser_template_id. */
347 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
349 /* A token type for nested-name-specifiers. If a
350 nested-name-specifier is processed while parsing tentatively, it is
351 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
352 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
353 cp_parser_nested_name_specifier_opt. */
354 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
356 /* A token type for tokens that are not tokens at all; these are used
357 to represent slots in the array where there used to be a token
358 that has now been deleted. */
359 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
361 /* The number of token types, including C++-specific ones. */
362 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
366 #ifdef ENABLE_CHECKING
367 /* The stream to which debugging output should be written. */
368 static FILE *cp_lexer_debug_stream;
369 #endif /* ENABLE_CHECKING */
371 /* Nonzero if we are parsing an unevaluated operand: an operand to
372 sizeof, typeof, or alignof. */
373 int cp_unevaluated_operand;
375 /* Create a new main C++ lexer, the lexer that gets tokens from the
379 cp_lexer_new_main (void)
381 cp_token first_token;
388 /* It's possible that parsing the first pragma will load a PCH file,
389 which is a GC collection point. So we have to do that before
390 allocating any memory. */
391 cp_parser_initial_pragma (&first_token);
393 c_common_no_more_pch ();
395 /* Allocate the memory. */
396 lexer = ggc_alloc_cleared_cp_lexer ();
398 #ifdef ENABLE_CHECKING
399 /* Initially we are not debugging. */
400 lexer->debugging_p = false;
401 #endif /* ENABLE_CHECKING */
402 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
403 CP_SAVED_TOKEN_STACK);
405 /* Create the buffer. */
406 alloc = CP_LEXER_BUFFER_SIZE;
407 buffer = ggc_alloc_vec_cp_token (alloc);
409 /* Put the first token in the buffer. */
414 /* Get the remaining tokens from the preprocessor. */
415 while (pos->type != CPP_EOF)
422 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
423 pos = buffer + space;
425 cp_lexer_get_preprocessor_token (lexer, pos);
427 lexer->buffer = buffer;
428 lexer->buffer_length = alloc - space;
429 lexer->last_token = pos;
430 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
432 /* Subsequent preprocessor diagnostics should use compiler
433 diagnostic functions to get the compiler source location. */
436 gcc_assert (lexer->next_token->type != CPP_PURGED);
440 /* Create a new lexer whose token stream is primed with the tokens in
441 CACHE. When these tokens are exhausted, no new tokens will be read. */
444 cp_lexer_new_from_tokens (cp_token_cache *cache)
446 cp_token *first = cache->first;
447 cp_token *last = cache->last;
448 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
450 /* We do not own the buffer. */
451 lexer->buffer = NULL;
452 lexer->buffer_length = 0;
453 lexer->next_token = first == last ? &eof_token : first;
454 lexer->last_token = last;
456 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
457 CP_SAVED_TOKEN_STACK);
459 #ifdef ENABLE_CHECKING
460 /* Initially we are not debugging. */
461 lexer->debugging_p = false;
464 gcc_assert (lexer->next_token->type != CPP_PURGED);
468 /* Frees all resources associated with LEXER. */
471 cp_lexer_destroy (cp_lexer *lexer)
474 ggc_free (lexer->buffer);
475 VEC_free (cp_token_position, heap, lexer->saved_tokens);
479 /* Returns nonzero if debugging information should be output. */
481 #ifdef ENABLE_CHECKING
484 cp_lexer_debugging_p (cp_lexer *lexer)
486 return lexer->debugging_p;
489 #endif /* ENABLE_CHECKING */
491 static inline cp_token_position
492 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
494 gcc_assert (!previous_p || lexer->next_token != &eof_token);
496 return lexer->next_token - previous_p;
499 static inline cp_token *
500 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
506 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
508 lexer->next_token = cp_lexer_token_at (lexer, pos);
511 static inline cp_token_position
512 cp_lexer_previous_token_position (cp_lexer *lexer)
514 if (lexer->next_token == &eof_token)
515 return lexer->last_token - 1;
517 return cp_lexer_token_position (lexer, true);
520 static inline cp_token *
521 cp_lexer_previous_token (cp_lexer *lexer)
523 cp_token_position tp = cp_lexer_previous_token_position (lexer);
525 return cp_lexer_token_at (lexer, tp);
528 /* nonzero if we are presently saving tokens. */
531 cp_lexer_saving_tokens (const cp_lexer* lexer)
533 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
536 /* Store the next token from the preprocessor in *TOKEN. Return true
537 if we reach EOF. If LEXER is NULL, assume we are handling an
538 initial #pragma pch_preprocess, and thus want the lexer to return
539 processed strings. */
542 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
544 static int is_extern_c = 0;
546 /* Get a new token from the preprocessor. */
548 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
549 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
550 token->keyword = RID_MAX;
551 token->pragma_kind = PRAGMA_NONE;
553 /* On some systems, some header files are surrounded by an
554 implicit extern "C" block. Set a flag in the token if it
555 comes from such a header. */
556 is_extern_c += pending_lang_change;
557 pending_lang_change = 0;
558 token->implicit_extern_c = is_extern_c > 0;
560 /* Check to see if this token is a keyword. */
561 if (token->type == CPP_NAME)
563 if (C_IS_RESERVED_WORD (token->u.value))
565 /* Mark this token as a keyword. */
566 token->type = CPP_KEYWORD;
567 /* Record which keyword. */
568 token->keyword = C_RID_CODE (token->u.value);
572 if (warn_cxx0x_compat
573 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
574 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
576 /* Warn about the C++0x keyword (but still treat it as
578 warning (OPT_Wc__0x_compat,
579 "identifier %qE will become a keyword in C++0x",
582 /* Clear out the C_RID_CODE so we don't warn about this
583 particular identifier-turned-keyword again. */
584 C_SET_RID_CODE (token->u.value, RID_MAX);
587 token->ambiguous_p = false;
588 token->keyword = RID_MAX;
591 else if (token->type == CPP_AT_NAME)
593 /* This only happens in Objective-C++; it must be a keyword. */
594 token->type = CPP_KEYWORD;
595 switch (C_RID_CODE (token->u.value))
597 /* Replace 'class' with '@class', 'private' with '@private',
598 etc. This prevents confusion with the C++ keyword
599 'class', and makes the tokens consistent with other
600 Objective-C 'AT' keywords. For example '@class' is
601 reported as RID_AT_CLASS which is consistent with
602 '@synchronized', which is reported as
605 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
606 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
607 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
608 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
609 case RID_THROW: token->keyword = RID_AT_THROW; break;
610 case RID_TRY: token->keyword = RID_AT_TRY; break;
611 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
612 default: token->keyword = C_RID_CODE (token->u.value);
615 else if (token->type == CPP_PRAGMA)
617 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
618 token->pragma_kind = ((enum pragma_kind)
619 TREE_INT_CST_LOW (token->u.value));
620 token->u.value = NULL_TREE;
624 /* Update the globals input_location and the input file stack from TOKEN. */
626 cp_lexer_set_source_position_from_token (cp_token *token)
628 if (token->type != CPP_EOF)
630 input_location = token->location;
634 /* Return a pointer to the next token in the token stream, but do not
637 static inline cp_token *
638 cp_lexer_peek_token (cp_lexer *lexer)
640 if (cp_lexer_debugging_p (lexer))
642 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
643 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
644 putc ('\n', cp_lexer_debug_stream);
646 return lexer->next_token;
649 /* Return true if the next token has the indicated TYPE. */
652 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
654 return cp_lexer_peek_token (lexer)->type == type;
657 /* Return true if the next token does not have the indicated TYPE. */
660 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
662 return !cp_lexer_next_token_is (lexer, type);
665 /* Return true if the next token is the indicated KEYWORD. */
668 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
670 return cp_lexer_peek_token (lexer)->keyword == keyword;
673 /* Return true if the next token is not the indicated KEYWORD. */
676 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
678 return cp_lexer_peek_token (lexer)->keyword != keyword;
681 /* Return true if the next token is a keyword for a decl-specifier. */
684 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
688 token = cp_lexer_peek_token (lexer);
689 switch (token->keyword)
691 /* auto specifier: storage-class-specifier in C++,
692 simple-type-specifier in C++0x. */
694 /* Storage classes. */
700 /* Elaborated type specifiers. */
706 /* Simple type specifiers. */
721 /* GNU extensions. */
724 /* C++0x extensions. */
733 /* Return a pointer to the Nth token in the token stream. If N is 1,
734 then this is precisely equivalent to cp_lexer_peek_token (except
735 that it is not inline). One would like to disallow that case, but
736 there is one case (cp_parser_nth_token_starts_template_id) where
737 the caller passes a variable for N and it might be 1. */
740 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
744 /* N is 1-based, not zero-based. */
747 if (cp_lexer_debugging_p (lexer))
748 fprintf (cp_lexer_debug_stream,
749 "cp_lexer: peeking ahead %ld at token: ", (long)n);
752 token = lexer->next_token;
753 gcc_assert (!n || token != &eof_token);
757 if (token == lexer->last_token)
763 if (token->type != CPP_PURGED)
767 if (cp_lexer_debugging_p (lexer))
769 cp_lexer_print_token (cp_lexer_debug_stream, token);
770 putc ('\n', cp_lexer_debug_stream);
776 /* Return the next token, and advance the lexer's next_token pointer
777 to point to the next non-purged token. */
780 cp_lexer_consume_token (cp_lexer* lexer)
782 cp_token *token = lexer->next_token;
784 gcc_assert (token != &eof_token);
785 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
790 if (lexer->next_token == lexer->last_token)
792 lexer->next_token = &eof_token;
797 while (lexer->next_token->type == CPP_PURGED);
799 cp_lexer_set_source_position_from_token (token);
801 /* Provide debugging output. */
802 if (cp_lexer_debugging_p (lexer))
804 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
805 cp_lexer_print_token (cp_lexer_debug_stream, token);
806 putc ('\n', cp_lexer_debug_stream);
812 /* Permanently remove the next token from the token stream, and
813 advance the next_token pointer to refer to the next non-purged
817 cp_lexer_purge_token (cp_lexer *lexer)
819 cp_token *tok = lexer->next_token;
821 gcc_assert (tok != &eof_token);
822 tok->type = CPP_PURGED;
823 tok->location = UNKNOWN_LOCATION;
824 tok->u.value = NULL_TREE;
825 tok->keyword = RID_MAX;
830 if (tok == lexer->last_token)
836 while (tok->type == CPP_PURGED);
837 lexer->next_token = tok;
840 /* Permanently remove all tokens after TOK, up to, but not
841 including, the token that will be returned next by
842 cp_lexer_peek_token. */
845 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
847 cp_token *peek = lexer->next_token;
849 if (peek == &eof_token)
850 peek = lexer->last_token;
852 gcc_assert (tok < peek);
854 for ( tok += 1; tok != peek; tok += 1)
856 tok->type = CPP_PURGED;
857 tok->location = UNKNOWN_LOCATION;
858 tok->u.value = NULL_TREE;
859 tok->keyword = RID_MAX;
863 /* Begin saving tokens. All tokens consumed after this point will be
867 cp_lexer_save_tokens (cp_lexer* lexer)
869 /* Provide debugging output. */
870 if (cp_lexer_debugging_p (lexer))
871 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
873 VEC_safe_push (cp_token_position, heap,
874 lexer->saved_tokens, lexer->next_token);
877 /* Commit to the portion of the token stream most recently saved. */
880 cp_lexer_commit_tokens (cp_lexer* lexer)
882 /* Provide debugging output. */
883 if (cp_lexer_debugging_p (lexer))
884 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
886 VEC_pop (cp_token_position, lexer->saved_tokens);
889 /* Return all tokens saved since the last call to cp_lexer_save_tokens
890 to the token stream. Stop saving tokens. */
893 cp_lexer_rollback_tokens (cp_lexer* lexer)
895 /* Provide debugging output. */
896 if (cp_lexer_debugging_p (lexer))
897 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
899 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
902 /* Print a representation of the TOKEN on the STREAM. */
904 #ifdef ENABLE_CHECKING
907 cp_lexer_print_token (FILE * stream, cp_token *token)
909 /* We don't use cpp_type2name here because the parser defines
910 a few tokens of its own. */
911 static const char *const token_names[] = {
912 /* cpplib-defined token types */
918 /* C++ parser token types - see "Manifest constants", above. */
921 "NESTED_NAME_SPECIFIER",
925 /* If we have a name for the token, print it out. Otherwise, we
926 simply give the numeric code. */
927 gcc_assert (token->type < ARRAY_SIZE(token_names));
928 fputs (token_names[token->type], stream);
930 /* For some tokens, print the associated data. */
934 /* Some keywords have a value that is not an IDENTIFIER_NODE.
935 For example, `struct' is mapped to an INTEGER_CST. */
936 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
938 /* else fall through */
940 fputs (IDENTIFIER_POINTER (token->u.value), stream);
948 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
956 /* Start emitting debugging information. */
959 cp_lexer_start_debugging (cp_lexer* lexer)
961 lexer->debugging_p = true;
964 /* Stop emitting debugging information. */
967 cp_lexer_stop_debugging (cp_lexer* lexer)
969 lexer->debugging_p = false;
972 #endif /* ENABLE_CHECKING */
974 /* Create a new cp_token_cache, representing a range of tokens. */
976 static cp_token_cache *
977 cp_token_cache_new (cp_token *first, cp_token *last)
979 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
980 cache->first = first;
986 /* Decl-specifiers. */
988 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
991 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
993 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
998 /* Nothing other than the parser should be creating declarators;
999 declarators are a semi-syntactic representation of C++ entities.
1000 Other parts of the front end that need to create entities (like
1001 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1003 static cp_declarator *make_call_declarator
1004 (cp_declarator *, tree, cp_cv_quals, tree, tree);
1005 static cp_declarator *make_array_declarator
1006 (cp_declarator *, tree);
1007 static cp_declarator *make_pointer_declarator
1008 (cp_cv_quals, cp_declarator *);
1009 static cp_declarator *make_reference_declarator
1010 (cp_cv_quals, cp_declarator *, bool);
1011 static cp_parameter_declarator *make_parameter_declarator
1012 (cp_decl_specifier_seq *, cp_declarator *, tree);
1013 static cp_declarator *make_ptrmem_declarator
1014 (cp_cv_quals, tree, cp_declarator *);
1016 /* An erroneous declarator. */
1017 static cp_declarator *cp_error_declarator;
1019 /* The obstack on which declarators and related data structures are
1021 static struct obstack declarator_obstack;
1023 /* Alloc BYTES from the declarator memory pool. */
1025 static inline void *
1026 alloc_declarator (size_t bytes)
1028 return obstack_alloc (&declarator_obstack, bytes);
1031 /* Allocate a declarator of the indicated KIND. Clear fields that are
1032 common to all declarators. */
1034 static cp_declarator *
1035 make_declarator (cp_declarator_kind kind)
1037 cp_declarator *declarator;
1039 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1040 declarator->kind = kind;
1041 declarator->attributes = NULL_TREE;
1042 declarator->declarator = NULL;
1043 declarator->parameter_pack_p = false;
1044 declarator->id_loc = UNKNOWN_LOCATION;
1049 /* Make a declarator for a generalized identifier. If
1050 QUALIFYING_SCOPE is non-NULL, the identifier is
1051 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1052 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1055 static cp_declarator *
1056 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1057 special_function_kind sfk)
1059 cp_declarator *declarator;
1061 /* It is valid to write:
1063 class C { void f(); };
1067 The standard is not clear about whether `typedef const C D' is
1068 legal; as of 2002-09-15 the committee is considering that
1069 question. EDG 3.0 allows that syntax. Therefore, we do as
1071 if (qualifying_scope && TYPE_P (qualifying_scope))
1072 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1074 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1075 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1076 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1078 declarator = make_declarator (cdk_id);
1079 declarator->u.id.qualifying_scope = qualifying_scope;
1080 declarator->u.id.unqualified_name = unqualified_name;
1081 declarator->u.id.sfk = sfk;
1086 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1087 of modifiers such as const or volatile to apply to the pointer
1088 type, represented as identifiers. */
1091 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1093 cp_declarator *declarator;
1095 declarator = make_declarator (cdk_pointer);
1096 declarator->declarator = target;
1097 declarator->u.pointer.qualifiers = cv_qualifiers;
1098 declarator->u.pointer.class_type = NULL_TREE;
1101 declarator->id_loc = target->id_loc;
1102 declarator->parameter_pack_p = target->parameter_pack_p;
1103 target->parameter_pack_p = false;
1106 declarator->parameter_pack_p = false;
1111 /* Like make_pointer_declarator -- but for references. */
1114 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1117 cp_declarator *declarator;
1119 declarator = make_declarator (cdk_reference);
1120 declarator->declarator = target;
1121 declarator->u.reference.qualifiers = cv_qualifiers;
1122 declarator->u.reference.rvalue_ref = rvalue_ref;
1125 declarator->id_loc = target->id_loc;
1126 declarator->parameter_pack_p = target->parameter_pack_p;
1127 target->parameter_pack_p = false;
1130 declarator->parameter_pack_p = false;
1135 /* Like make_pointer_declarator -- but for a pointer to a non-static
1136 member of CLASS_TYPE. */
1139 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1140 cp_declarator *pointee)
1142 cp_declarator *declarator;
1144 declarator = make_declarator (cdk_ptrmem);
1145 declarator->declarator = pointee;
1146 declarator->u.pointer.qualifiers = cv_qualifiers;
1147 declarator->u.pointer.class_type = class_type;
1151 declarator->parameter_pack_p = pointee->parameter_pack_p;
1152 pointee->parameter_pack_p = false;
1155 declarator->parameter_pack_p = false;
1160 /* Make a declarator for the function given by TARGET, with the
1161 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1162 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1163 indicates what exceptions can be thrown. */
1166 make_call_declarator (cp_declarator *target,
1168 cp_cv_quals cv_qualifiers,
1169 tree exception_specification,
1170 tree late_return_type)
1172 cp_declarator *declarator;
1174 declarator = make_declarator (cdk_function);
1175 declarator->declarator = target;
1176 declarator->u.function.parameters = parms;
1177 declarator->u.function.qualifiers = cv_qualifiers;
1178 declarator->u.function.exception_specification = exception_specification;
1179 declarator->u.function.late_return_type = late_return_type;
1182 declarator->id_loc = target->id_loc;
1183 declarator->parameter_pack_p = target->parameter_pack_p;
1184 target->parameter_pack_p = false;
1187 declarator->parameter_pack_p = false;
1192 /* Make a declarator for an array of BOUNDS elements, each of which is
1193 defined by ELEMENT. */
1196 make_array_declarator (cp_declarator *element, tree bounds)
1198 cp_declarator *declarator;
1200 declarator = make_declarator (cdk_array);
1201 declarator->declarator = element;
1202 declarator->u.array.bounds = bounds;
1205 declarator->id_loc = element->id_loc;
1206 declarator->parameter_pack_p = element->parameter_pack_p;
1207 element->parameter_pack_p = false;
1210 declarator->parameter_pack_p = false;
1215 /* Determine whether the declarator we've seen so far can be a
1216 parameter pack, when followed by an ellipsis. */
1218 declarator_can_be_parameter_pack (cp_declarator *declarator)
1220 /* Search for a declarator name, or any other declarator that goes
1221 after the point where the ellipsis could appear in a parameter
1222 pack. If we find any of these, then this declarator can not be
1223 made into a parameter pack. */
1225 while (declarator && !found)
1227 switch ((int)declarator->kind)
1238 declarator = declarator->declarator;
1246 cp_parameter_declarator *no_parameters;
1248 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1249 DECLARATOR and DEFAULT_ARGUMENT. */
1251 cp_parameter_declarator *
1252 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1253 cp_declarator *declarator,
1254 tree default_argument)
1256 cp_parameter_declarator *parameter;
1258 parameter = ((cp_parameter_declarator *)
1259 alloc_declarator (sizeof (cp_parameter_declarator)));
1260 parameter->next = NULL;
1261 if (decl_specifiers)
1262 parameter->decl_specifiers = *decl_specifiers;
1264 clear_decl_specs (¶meter->decl_specifiers);
1265 parameter->declarator = declarator;
1266 parameter->default_argument = default_argument;
1267 parameter->ellipsis_p = false;
1272 /* Returns true iff DECLARATOR is a declaration for a function. */
1275 function_declarator_p (const cp_declarator *declarator)
1279 if (declarator->kind == cdk_function
1280 && declarator->declarator->kind == cdk_id)
1282 if (declarator->kind == cdk_id
1283 || declarator->kind == cdk_error)
1285 declarator = declarator->declarator;
1295 A cp_parser parses the token stream as specified by the C++
1296 grammar. Its job is purely parsing, not semantic analysis. For
1297 example, the parser breaks the token stream into declarators,
1298 expressions, statements, and other similar syntactic constructs.
1299 It does not check that the types of the expressions on either side
1300 of an assignment-statement are compatible, or that a function is
1301 not declared with a parameter of type `void'.
1303 The parser invokes routines elsewhere in the compiler to perform
1304 semantic analysis and to build up the abstract syntax tree for the
1307 The parser (and the template instantiation code, which is, in a
1308 way, a close relative of parsing) are the only parts of the
1309 compiler that should be calling push_scope and pop_scope, or
1310 related functions. The parser (and template instantiation code)
1311 keeps track of what scope is presently active; everything else
1312 should simply honor that. (The code that generates static
1313 initializers may also need to set the scope, in order to check
1314 access control correctly when emitting the initializers.)
1319 The parser is of the standard recursive-descent variety. Upcoming
1320 tokens in the token stream are examined in order to determine which
1321 production to use when parsing a non-terminal. Some C++ constructs
1322 require arbitrary look ahead to disambiguate. For example, it is
1323 impossible, in the general case, to tell whether a statement is an
1324 expression or declaration without scanning the entire statement.
1325 Therefore, the parser is capable of "parsing tentatively." When the
1326 parser is not sure what construct comes next, it enters this mode.
1327 Then, while we attempt to parse the construct, the parser queues up
1328 error messages, rather than issuing them immediately, and saves the
1329 tokens it consumes. If the construct is parsed successfully, the
1330 parser "commits", i.e., it issues any queued error messages and
1331 the tokens that were being preserved are permanently discarded.
1332 If, however, the construct is not parsed successfully, the parser
1333 rolls back its state completely so that it can resume parsing using
1334 a different alternative.
1339 The performance of the parser could probably be improved substantially.
1340 We could often eliminate the need to parse tentatively by looking ahead
1341 a little bit. In some places, this approach might not entirely eliminate
1342 the need to parse tentatively, but it might still speed up the average
1345 /* Flags that are passed to some parsing functions. These values can
1346 be bitwise-ored together. */
1351 CP_PARSER_FLAGS_NONE = 0x0,
1352 /* The construct is optional. If it is not present, then no error
1353 should be issued. */
1354 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1355 /* When parsing a type-specifier, treat user-defined type-names
1356 as non-type identifiers. */
1357 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1358 /* When parsing a type-specifier, do not try to parse a class-specifier
1359 or enum-specifier. */
1360 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1361 /* When parsing a decl-specifier-seq, only allow type-specifier or
1363 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1366 /* This type is used for parameters and variables which hold
1367 combinations of the above flags. */
1368 typedef int cp_parser_flags;
1370 /* The different kinds of declarators we want to parse. */
1372 typedef enum cp_parser_declarator_kind
1374 /* We want an abstract declarator. */
1375 CP_PARSER_DECLARATOR_ABSTRACT,
1376 /* We want a named declarator. */
1377 CP_PARSER_DECLARATOR_NAMED,
1378 /* We don't mind, but the name must be an unqualified-id. */
1379 CP_PARSER_DECLARATOR_EITHER
1380 } cp_parser_declarator_kind;
1382 /* The precedence values used to parse binary expressions. The minimum value
1383 of PREC must be 1, because zero is reserved to quickly discriminate
1384 binary operators from other tokens. */
1389 PREC_LOGICAL_OR_EXPRESSION,
1390 PREC_LOGICAL_AND_EXPRESSION,
1391 PREC_INCLUSIVE_OR_EXPRESSION,
1392 PREC_EXCLUSIVE_OR_EXPRESSION,
1393 PREC_AND_EXPRESSION,
1394 PREC_EQUALITY_EXPRESSION,
1395 PREC_RELATIONAL_EXPRESSION,
1396 PREC_SHIFT_EXPRESSION,
1397 PREC_ADDITIVE_EXPRESSION,
1398 PREC_MULTIPLICATIVE_EXPRESSION,
1400 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1403 /* A mapping from a token type to a corresponding tree node type, with a
1404 precedence value. */
1406 typedef struct cp_parser_binary_operations_map_node
1408 /* The token type. */
1409 enum cpp_ttype token_type;
1410 /* The corresponding tree code. */
1411 enum tree_code tree_type;
1412 /* The precedence of this operator. */
1413 enum cp_parser_prec prec;
1414 } cp_parser_binary_operations_map_node;
1416 /* The status of a tentative parse. */
1418 typedef enum cp_parser_status_kind
1420 /* No errors have occurred. */
1421 CP_PARSER_STATUS_KIND_NO_ERROR,
1422 /* An error has occurred. */
1423 CP_PARSER_STATUS_KIND_ERROR,
1424 /* We are committed to this tentative parse, whether or not an error
1426 CP_PARSER_STATUS_KIND_COMMITTED
1427 } cp_parser_status_kind;
1429 typedef struct cp_parser_expression_stack_entry
1431 /* Left hand side of the binary operation we are currently
1434 /* Original tree code for left hand side, if it was a binary
1435 expression itself (used for -Wparentheses). */
1436 enum tree_code lhs_type;
1437 /* Tree code for the binary operation we are parsing. */
1438 enum tree_code tree_type;
1439 /* Precedence of the binary operation we are parsing. */
1440 enum cp_parser_prec prec;
1441 } cp_parser_expression_stack_entry;
1443 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1444 entries because precedence levels on the stack are monotonically
1446 typedef struct cp_parser_expression_stack_entry
1447 cp_parser_expression_stack[NUM_PREC_VALUES];
1449 /* Context that is saved and restored when parsing tentatively. */
1450 typedef struct GTY (()) cp_parser_context {
1451 /* If this is a tentative parsing context, the status of the
1453 enum cp_parser_status_kind status;
1454 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1455 that are looked up in this context must be looked up both in the
1456 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1457 the context of the containing expression. */
1460 /* The next parsing context in the stack. */
1461 struct cp_parser_context *next;
1462 } cp_parser_context;
1466 /* Constructors and destructors. */
1468 static cp_parser_context *cp_parser_context_new
1469 (cp_parser_context *);
1471 /* Class variables. */
1473 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1475 /* The operator-precedence table used by cp_parser_binary_expression.
1476 Transformed into an associative array (binops_by_token) by
1479 static const cp_parser_binary_operations_map_node binops[] = {
1480 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1481 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1483 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1484 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1485 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1487 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1488 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1490 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1491 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1493 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1494 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1495 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1496 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1498 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1499 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1501 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1503 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1505 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1507 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1509 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1512 /* The same as binops, but initialized by cp_parser_new so that
1513 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1515 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1517 /* Constructors and destructors. */
1519 /* Construct a new context. The context below this one on the stack
1520 is given by NEXT. */
1522 static cp_parser_context *
1523 cp_parser_context_new (cp_parser_context* next)
1525 cp_parser_context *context;
1527 /* Allocate the storage. */
1528 if (cp_parser_context_free_list != NULL)
1530 /* Pull the first entry from the free list. */
1531 context = cp_parser_context_free_list;
1532 cp_parser_context_free_list = context->next;
1533 memset (context, 0, sizeof (*context));
1536 context = ggc_alloc_cleared_cp_parser_context ();
1538 /* No errors have occurred yet in this context. */
1539 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1540 /* If this is not the bottommost context, copy information that we
1541 need from the previous context. */
1544 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1545 expression, then we are parsing one in this context, too. */
1546 context->object_type = next->object_type;
1547 /* Thread the stack. */
1548 context->next = next;
1554 /* An entry in a queue of function arguments that require post-processing. */
1556 typedef struct GTY(()) cp_default_arg_entry_d {
1557 /* The current_class_type when we parsed this arg. */
1560 /* The function decl itself. */
1562 } cp_default_arg_entry;
1564 DEF_VEC_O(cp_default_arg_entry);
1565 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1567 /* An entry in a stack for member functions of local classes. */
1569 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1570 /* Functions with default arguments that require post-processing.
1571 Functions appear in this list in declaration order. */
1572 VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1574 /* Functions with defintions that require post-processing. Functions
1575 appear in this list in declaration order. */
1576 VEC(tree,gc) *funs_with_definitions;
1577 } cp_unparsed_functions_entry;
1579 DEF_VEC_O(cp_unparsed_functions_entry);
1580 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1582 /* The cp_parser structure represents the C++ parser. */
1584 typedef struct GTY(()) cp_parser {
1585 /* The lexer from which we are obtaining tokens. */
1588 /* The scope in which names should be looked up. If NULL_TREE, then
1589 we look up names in the scope that is currently open in the
1590 source program. If non-NULL, this is either a TYPE or
1591 NAMESPACE_DECL for the scope in which we should look. It can
1592 also be ERROR_MARK, when we've parsed a bogus scope.
1594 This value is not cleared automatically after a name is looked
1595 up, so we must be careful to clear it before starting a new look
1596 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1597 will look up `Z' in the scope of `X', rather than the current
1598 scope.) Unfortunately, it is difficult to tell when name lookup
1599 is complete, because we sometimes peek at a token, look it up,
1600 and then decide not to consume it. */
1603 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1604 last lookup took place. OBJECT_SCOPE is used if an expression
1605 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1606 respectively. QUALIFYING_SCOPE is used for an expression of the
1607 form "X::Y"; it refers to X. */
1609 tree qualifying_scope;
1611 /* A stack of parsing contexts. All but the bottom entry on the
1612 stack will be tentative contexts.
1614 We parse tentatively in order to determine which construct is in
1615 use in some situations. For example, in order to determine
1616 whether a statement is an expression-statement or a
1617 declaration-statement we parse it tentatively as a
1618 declaration-statement. If that fails, we then reparse the same
1619 token stream as an expression-statement. */
1620 cp_parser_context *context;
1622 /* True if we are parsing GNU C++. If this flag is not set, then
1623 GNU extensions are not recognized. */
1624 bool allow_gnu_extensions_p;
1626 /* TRUE if the `>' token should be interpreted as the greater-than
1627 operator. FALSE if it is the end of a template-id or
1628 template-parameter-list. In C++0x mode, this flag also applies to
1629 `>>' tokens, which are viewed as two consecutive `>' tokens when
1630 this flag is FALSE. */
1631 bool greater_than_is_operator_p;
1633 /* TRUE if default arguments are allowed within a parameter list
1634 that starts at this point. FALSE if only a gnu extension makes
1635 them permissible. */
1636 bool default_arg_ok_p;
1638 /* TRUE if we are parsing an integral constant-expression. See
1639 [expr.const] for a precise definition. */
1640 bool integral_constant_expression_p;
1642 /* TRUE if we are parsing an integral constant-expression -- but a
1643 non-constant expression should be permitted as well. This flag
1644 is used when parsing an array bound so that GNU variable-length
1645 arrays are tolerated. */
1646 bool allow_non_integral_constant_expression_p;
1648 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1649 been seen that makes the expression non-constant. */
1650 bool non_integral_constant_expression_p;
1652 /* TRUE if local variable names and `this' are forbidden in the
1654 bool local_variables_forbidden_p;
1656 /* TRUE if the declaration we are parsing is part of a
1657 linkage-specification of the form `extern string-literal
1659 bool in_unbraced_linkage_specification_p;
1661 /* TRUE if we are presently parsing a declarator, after the
1662 direct-declarator. */
1663 bool in_declarator_p;
1665 /* TRUE if we are presently parsing a template-argument-list. */
1666 bool in_template_argument_list_p;
1668 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1669 to IN_OMP_BLOCK if parsing OpenMP structured block and
1670 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1671 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1672 iteration-statement, OpenMP block or loop within that switch. */
1673 #define IN_SWITCH_STMT 1
1674 #define IN_ITERATION_STMT 2
1675 #define IN_OMP_BLOCK 4
1676 #define IN_OMP_FOR 8
1677 #define IN_IF_STMT 16
1678 unsigned char in_statement;
1680 /* TRUE if we are presently parsing the body of a switch statement.
1681 Note that this doesn't quite overlap with in_statement above.
1682 The difference relates to giving the right sets of error messages:
1683 "case not in switch" vs "break statement used with OpenMP...". */
1684 bool in_switch_statement_p;
1686 /* TRUE if we are parsing a type-id in an expression context. In
1687 such a situation, both "type (expr)" and "type (type)" are valid
1689 bool in_type_id_in_expr_p;
1691 /* TRUE if we are currently in a header file where declarations are
1692 implicitly extern "C". */
1693 bool implicit_extern_c;
1695 /* TRUE if strings in expressions should be translated to the execution
1697 bool translate_strings_p;
1699 /* TRUE if we are presently parsing the body of a function, but not
1701 bool in_function_body;
1703 /* TRUE if we can auto-correct a colon to a scope operator. */
1704 bool colon_corrects_to_scope_p;
1706 /* If non-NULL, then we are parsing a construct where new type
1707 definitions are not permitted. The string stored here will be
1708 issued as an error message if a type is defined. */
1709 const char *type_definition_forbidden_message;
1711 /* A stack used for member functions of local classes. The lists
1712 contained in an individual entry can only be processed once the
1713 outermost class being defined is complete. */
1714 VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1716 /* The number of classes whose definitions are currently in
1718 unsigned num_classes_being_defined;
1720 /* The number of template parameter lists that apply directly to the
1721 current declaration. */
1722 unsigned num_template_parameter_lists;
1725 /* Managing the unparsed function queues. */
1727 #define unparsed_funs_with_default_args \
1728 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1729 #define unparsed_funs_with_definitions \
1730 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1733 push_unparsed_function_queues (cp_parser *parser)
1735 VEC_safe_push (cp_unparsed_functions_entry, gc,
1736 parser->unparsed_queues, NULL);
1737 unparsed_funs_with_default_args = NULL;
1738 unparsed_funs_with_definitions = make_tree_vector ();
1742 pop_unparsed_function_queues (cp_parser *parser)
1744 release_tree_vector (unparsed_funs_with_definitions);
1745 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1750 /* Constructors and destructors. */
1752 static cp_parser *cp_parser_new
1755 /* Routines to parse various constructs.
1757 Those that return `tree' will return the error_mark_node (rather
1758 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1759 Sometimes, they will return an ordinary node if error-recovery was
1760 attempted, even though a parse error occurred. So, to check
1761 whether or not a parse error occurred, you should always use
1762 cp_parser_error_occurred. If the construct is optional (indicated
1763 either by an `_opt' in the name of the function that does the
1764 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1765 the construct is not present. */
1767 /* Lexical conventions [gram.lex] */
1769 static tree cp_parser_identifier
1771 static tree cp_parser_string_literal
1772 (cp_parser *, bool, bool);
1774 /* Basic concepts [gram.basic] */
1776 static bool cp_parser_translation_unit
1779 /* Expressions [gram.expr] */
1781 static tree cp_parser_primary_expression
1782 (cp_parser *, bool, bool, bool, cp_id_kind *);
1783 static tree cp_parser_id_expression
1784 (cp_parser *, bool, bool, bool *, bool, bool);
1785 static tree cp_parser_unqualified_id
1786 (cp_parser *, bool, bool, bool, bool);
1787 static tree cp_parser_nested_name_specifier_opt
1788 (cp_parser *, bool, bool, bool, bool);
1789 static tree cp_parser_nested_name_specifier
1790 (cp_parser *, bool, bool, bool, bool);
1791 static tree cp_parser_qualifying_entity
1792 (cp_parser *, bool, bool, bool, bool, bool);
1793 static tree cp_parser_postfix_expression
1794 (cp_parser *, bool, bool, bool, cp_id_kind *);
1795 static tree cp_parser_postfix_open_square_expression
1796 (cp_parser *, tree, bool);
1797 static tree cp_parser_postfix_dot_deref_expression
1798 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1799 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1800 (cp_parser *, int, bool, bool, bool *);
1801 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1802 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1803 static void cp_parser_pseudo_destructor_name
1804 (cp_parser *, tree *, tree *);
1805 static tree cp_parser_unary_expression
1806 (cp_parser *, bool, bool, cp_id_kind *);
1807 static enum tree_code cp_parser_unary_operator
1809 static tree cp_parser_new_expression
1811 static VEC(tree,gc) *cp_parser_new_placement
1813 static tree cp_parser_new_type_id
1814 (cp_parser *, tree *);
1815 static cp_declarator *cp_parser_new_declarator_opt
1817 static cp_declarator *cp_parser_direct_new_declarator
1819 static VEC(tree,gc) *cp_parser_new_initializer
1821 static tree cp_parser_delete_expression
1823 static tree cp_parser_cast_expression
1824 (cp_parser *, bool, bool, cp_id_kind *);
1825 static tree cp_parser_binary_expression
1826 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1827 static tree cp_parser_question_colon_clause
1828 (cp_parser *, tree);
1829 static tree cp_parser_assignment_expression
1830 (cp_parser *, bool, cp_id_kind *);
1831 static enum tree_code cp_parser_assignment_operator_opt
1833 static tree cp_parser_expression
1834 (cp_parser *, bool, cp_id_kind *);
1835 static tree cp_parser_constant_expression
1836 (cp_parser *, bool, bool *);
1837 static tree cp_parser_builtin_offsetof
1839 static tree cp_parser_lambda_expression
1841 static void cp_parser_lambda_introducer
1842 (cp_parser *, tree);
1843 static void cp_parser_lambda_declarator_opt
1844 (cp_parser *, tree);
1845 static void cp_parser_lambda_body
1846 (cp_parser *, tree);
1848 /* Statements [gram.stmt.stmt] */
1850 static void cp_parser_statement
1851 (cp_parser *, tree, bool, bool *);
1852 static void cp_parser_label_for_labeled_statement
1854 static tree cp_parser_expression_statement
1855 (cp_parser *, tree);
1856 static tree cp_parser_compound_statement
1857 (cp_parser *, tree, bool);
1858 static void cp_parser_statement_seq_opt
1859 (cp_parser *, tree);
1860 static tree cp_parser_selection_statement
1861 (cp_parser *, bool *);
1862 static tree cp_parser_condition
1864 static tree cp_parser_iteration_statement
1866 static void cp_parser_for_init_statement
1868 static tree cp_parser_c_for
1870 static tree cp_parser_range_for
1872 static tree cp_parser_jump_statement
1874 static void cp_parser_declaration_statement
1877 static tree cp_parser_implicitly_scoped_statement
1878 (cp_parser *, bool *);
1879 static void cp_parser_already_scoped_statement
1882 /* Declarations [gram.dcl.dcl] */
1884 static void cp_parser_declaration_seq_opt
1886 static void cp_parser_declaration
1888 static void cp_parser_block_declaration
1889 (cp_parser *, bool);
1890 static void cp_parser_simple_declaration
1891 (cp_parser *, bool);
1892 static void cp_parser_decl_specifier_seq
1893 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1894 static tree cp_parser_storage_class_specifier_opt
1896 static tree cp_parser_function_specifier_opt
1897 (cp_parser *, cp_decl_specifier_seq *);
1898 static tree cp_parser_type_specifier
1899 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1901 static tree cp_parser_simple_type_specifier
1902 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1903 static tree cp_parser_type_name
1905 static tree cp_parser_nonclass_name
1906 (cp_parser* parser);
1907 static tree cp_parser_elaborated_type_specifier
1908 (cp_parser *, bool, bool);
1909 static tree cp_parser_enum_specifier
1911 static void cp_parser_enumerator_list
1912 (cp_parser *, tree);
1913 static void cp_parser_enumerator_definition
1914 (cp_parser *, tree);
1915 static tree cp_parser_namespace_name
1917 static void cp_parser_namespace_definition
1919 static void cp_parser_namespace_body
1921 static tree cp_parser_qualified_namespace_specifier
1923 static void cp_parser_namespace_alias_definition
1925 static bool cp_parser_using_declaration
1926 (cp_parser *, bool);
1927 static void cp_parser_using_directive
1929 static void cp_parser_asm_definition
1931 static void cp_parser_linkage_specification
1933 static void cp_parser_static_assert
1934 (cp_parser *, bool);
1935 static tree cp_parser_decltype
1938 /* Declarators [gram.dcl.decl] */
1940 static tree cp_parser_init_declarator
1941 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1942 static cp_declarator *cp_parser_declarator
1943 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1944 static cp_declarator *cp_parser_direct_declarator
1945 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1946 static enum tree_code cp_parser_ptr_operator
1947 (cp_parser *, tree *, cp_cv_quals *);
1948 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1950 static tree cp_parser_late_return_type_opt
1952 static tree cp_parser_declarator_id
1953 (cp_parser *, bool);
1954 static tree cp_parser_type_id
1956 static tree cp_parser_template_type_arg
1958 static tree cp_parser_trailing_type_id (cp_parser *);
1959 static tree cp_parser_type_id_1
1960 (cp_parser *, bool, bool);
1961 static void cp_parser_type_specifier_seq
1962 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1963 static tree cp_parser_parameter_declaration_clause
1965 static tree cp_parser_parameter_declaration_list
1966 (cp_parser *, bool *);
1967 static cp_parameter_declarator *cp_parser_parameter_declaration
1968 (cp_parser *, bool, bool *);
1969 static tree cp_parser_default_argument
1970 (cp_parser *, bool);
1971 static void cp_parser_function_body
1973 static tree cp_parser_initializer
1974 (cp_parser *, bool *, bool *);
1975 static tree cp_parser_initializer_clause
1976 (cp_parser *, bool *);
1977 static tree cp_parser_braced_list
1978 (cp_parser*, bool*);
1979 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1980 (cp_parser *, bool *);
1982 static bool cp_parser_ctor_initializer_opt_and_function_body
1985 /* Classes [gram.class] */
1987 static tree cp_parser_class_name
1988 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1989 static tree cp_parser_class_specifier
1991 static tree cp_parser_class_head
1992 (cp_parser *, bool *, tree *, tree *);
1993 static enum tag_types cp_parser_class_key
1995 static void cp_parser_member_specification_opt
1997 static void cp_parser_member_declaration
1999 static tree cp_parser_pure_specifier
2001 static tree cp_parser_constant_initializer
2004 /* Derived classes [gram.class.derived] */
2006 static tree cp_parser_base_clause
2008 static tree cp_parser_base_specifier
2011 /* Special member functions [gram.special] */
2013 static tree cp_parser_conversion_function_id
2015 static tree cp_parser_conversion_type_id
2017 static cp_declarator *cp_parser_conversion_declarator_opt
2019 static bool cp_parser_ctor_initializer_opt
2021 static void cp_parser_mem_initializer_list
2023 static tree cp_parser_mem_initializer
2025 static tree cp_parser_mem_initializer_id
2028 /* Overloading [gram.over] */
2030 static tree cp_parser_operator_function_id
2032 static tree cp_parser_operator
2035 /* Templates [gram.temp] */
2037 static void cp_parser_template_declaration
2038 (cp_parser *, bool);
2039 static tree cp_parser_template_parameter_list
2041 static tree cp_parser_template_parameter
2042 (cp_parser *, bool *, bool *);
2043 static tree cp_parser_type_parameter
2044 (cp_parser *, bool *);
2045 static tree cp_parser_template_id
2046 (cp_parser *, bool, bool, bool);
2047 static tree cp_parser_template_name
2048 (cp_parser *, bool, bool, bool, bool *);
2049 static tree cp_parser_template_argument_list
2051 static tree cp_parser_template_argument
2053 static void cp_parser_explicit_instantiation
2055 static void cp_parser_explicit_specialization
2058 /* Exception handling [gram.exception] */
2060 static tree cp_parser_try_block
2062 static bool cp_parser_function_try_block
2064 static void cp_parser_handler_seq
2066 static void cp_parser_handler
2068 static tree cp_parser_exception_declaration
2070 static tree cp_parser_throw_expression
2072 static tree cp_parser_exception_specification_opt
2074 static tree cp_parser_type_id_list
2077 /* GNU Extensions */
2079 static tree cp_parser_asm_specification_opt
2081 static tree cp_parser_asm_operand_list
2083 static tree cp_parser_asm_clobber_list
2085 static tree cp_parser_asm_label_list
2087 static tree cp_parser_attributes_opt
2089 static tree cp_parser_attribute_list
2091 static bool cp_parser_extension_opt
2092 (cp_parser *, int *);
2093 static void cp_parser_label_declaration
2096 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2097 static bool cp_parser_pragma
2098 (cp_parser *, enum pragma_context);
2100 /* Objective-C++ Productions */
2102 static tree cp_parser_objc_message_receiver
2104 static tree cp_parser_objc_message_args
2106 static tree cp_parser_objc_message_expression
2108 static tree cp_parser_objc_encode_expression
2110 static tree cp_parser_objc_defs_expression
2112 static tree cp_parser_objc_protocol_expression
2114 static tree cp_parser_objc_selector_expression
2116 static tree cp_parser_objc_expression
2118 static bool cp_parser_objc_selector_p
2120 static tree cp_parser_objc_selector
2122 static tree cp_parser_objc_protocol_refs_opt
2124 static void cp_parser_objc_declaration
2125 (cp_parser *, tree);
2126 static tree cp_parser_objc_statement
2128 static bool cp_parser_objc_valid_prefix_attributes
2129 (cp_parser *, tree *);
2130 static void cp_parser_objc_at_property_declaration
2132 static void cp_parser_objc_at_synthesize_declaration
2134 static void cp_parser_objc_at_dynamic_declaration
2136 static tree cp_parser_objc_struct_declaration
2139 /* Utility Routines */
2141 static tree cp_parser_lookup_name
2142 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2143 static tree cp_parser_lookup_name_simple
2144 (cp_parser *, tree, location_t);
2145 static tree cp_parser_maybe_treat_template_as_class
2147 static bool cp_parser_check_declarator_template_parameters
2148 (cp_parser *, cp_declarator *, location_t);
2149 static bool cp_parser_check_template_parameters
2150 (cp_parser *, unsigned, location_t, cp_declarator *);
2151 static tree cp_parser_simple_cast_expression
2153 static tree cp_parser_global_scope_opt
2154 (cp_parser *, bool);
2155 static bool cp_parser_constructor_declarator_p
2156 (cp_parser *, bool);
2157 static tree cp_parser_function_definition_from_specifiers_and_declarator
2158 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2159 static tree cp_parser_function_definition_after_declarator
2160 (cp_parser *, bool);
2161 static void cp_parser_template_declaration_after_export
2162 (cp_parser *, bool);
2163 static void cp_parser_perform_template_parameter_access_checks
2164 (VEC (deferred_access_check,gc)*);
2165 static tree cp_parser_single_declaration
2166 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2167 static tree cp_parser_functional_cast
2168 (cp_parser *, tree);
2169 static tree cp_parser_save_member_function_body
2170 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2171 static tree cp_parser_enclosed_template_argument_list
2173 static void cp_parser_save_default_args
2174 (cp_parser *, tree);
2175 static void cp_parser_late_parsing_for_member
2176 (cp_parser *, tree);
2177 static void cp_parser_late_parsing_default_args
2178 (cp_parser *, tree);
2179 static tree cp_parser_sizeof_operand
2180 (cp_parser *, enum rid);
2181 static tree cp_parser_trait_expr
2182 (cp_parser *, enum rid);
2183 static bool cp_parser_declares_only_class_p
2185 static void cp_parser_set_storage_class
2186 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2187 static void cp_parser_set_decl_spec_type
2188 (cp_decl_specifier_seq *, tree, location_t, bool);
2189 static bool cp_parser_friend_p
2190 (const cp_decl_specifier_seq *);
2191 static void cp_parser_required_error
2192 (cp_parser *, required_token, bool);
2193 static cp_token *cp_parser_require
2194 (cp_parser *, enum cpp_ttype, required_token);
2195 static cp_token *cp_parser_require_keyword
2196 (cp_parser *, enum rid, required_token);
2197 static bool cp_parser_token_starts_function_definition_p
2199 static bool cp_parser_next_token_starts_class_definition_p
2201 static bool cp_parser_next_token_ends_template_argument_p
2203 static bool cp_parser_nth_token_starts_template_argument_list_p
2204 (cp_parser *, size_t);
2205 static enum tag_types cp_parser_token_is_class_key
2207 static void cp_parser_check_class_key
2208 (enum tag_types, tree type);
2209 static void cp_parser_check_access_in_redeclaration
2210 (tree type, location_t location);
2211 static bool cp_parser_optional_template_keyword
2213 static void cp_parser_pre_parsed_nested_name_specifier
2215 static bool cp_parser_cache_group
2216 (cp_parser *, enum cpp_ttype, unsigned);
2217 static void cp_parser_parse_tentatively
2219 static void cp_parser_commit_to_tentative_parse
2221 static void cp_parser_abort_tentative_parse
2223 static bool cp_parser_parse_definitely
2225 static inline bool cp_parser_parsing_tentatively
2227 static bool cp_parser_uncommitted_to_tentative_parse_p
2229 static void cp_parser_error
2230 (cp_parser *, const char *);
2231 static void cp_parser_name_lookup_error
2232 (cp_parser *, tree, tree, name_lookup_error, location_t);
2233 static bool cp_parser_simulate_error
2235 static bool cp_parser_check_type_definition
2237 static void cp_parser_check_for_definition_in_return_type
2238 (cp_declarator *, tree, location_t type_location);
2239 static void cp_parser_check_for_invalid_template_id
2240 (cp_parser *, tree, location_t location);
2241 static bool cp_parser_non_integral_constant_expression
2242 (cp_parser *, non_integral_constant);
2243 static void cp_parser_diagnose_invalid_type_name
2244 (cp_parser *, tree, tree, location_t);
2245 static bool cp_parser_parse_and_diagnose_invalid_type_name
2247 static int cp_parser_skip_to_closing_parenthesis
2248 (cp_parser *, bool, bool, bool);
2249 static void cp_parser_skip_to_end_of_statement
2251 static void cp_parser_consume_semicolon_at_end_of_statement
2253 static void cp_parser_skip_to_end_of_block_or_statement
2255 static bool cp_parser_skip_to_closing_brace
2257 static void cp_parser_skip_to_end_of_template_parameter_list
2259 static void cp_parser_skip_to_pragma_eol
2260 (cp_parser*, cp_token *);
2261 static bool cp_parser_error_occurred
2263 static bool cp_parser_allow_gnu_extensions_p
2265 static bool cp_parser_is_string_literal
2267 static bool cp_parser_is_keyword
2268 (cp_token *, enum rid);
2269 static tree cp_parser_make_typename_type
2270 (cp_parser *, tree, tree, location_t location);
2271 static cp_declarator * cp_parser_make_indirect_declarator
2272 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2274 /* Returns nonzero if we are parsing tentatively. */
2277 cp_parser_parsing_tentatively (cp_parser* parser)
2279 return parser->context->next != NULL;
2282 /* Returns nonzero if TOKEN is a string literal. */
2285 cp_parser_is_string_literal (cp_token* token)
2287 return (token->type == CPP_STRING ||
2288 token->type == CPP_STRING16 ||
2289 token->type == CPP_STRING32 ||
2290 token->type == CPP_WSTRING ||
2291 token->type == CPP_UTF8STRING);
2294 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2297 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2299 return token->keyword == keyword;
2302 /* If not parsing tentatively, issue a diagnostic of the form
2303 FILE:LINE: MESSAGE before TOKEN
2304 where TOKEN is the next token in the input stream. MESSAGE
2305 (specified by the caller) is usually of the form "expected
2309 cp_parser_error (cp_parser* parser, const char* gmsgid)
2311 if (!cp_parser_simulate_error (parser))
2313 cp_token *token = cp_lexer_peek_token (parser->lexer);
2314 /* This diagnostic makes more sense if it is tagged to the line
2315 of the token we just peeked at. */
2316 cp_lexer_set_source_position_from_token (token);
2318 if (token->type == CPP_PRAGMA)
2320 error_at (token->location,
2321 "%<#pragma%> is not allowed here");
2322 cp_parser_skip_to_pragma_eol (parser, token);
2326 c_parse_error (gmsgid,
2327 /* Because c_parser_error does not understand
2328 CPP_KEYWORD, keywords are treated like
2330 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2331 token->u.value, token->flags);
2335 /* Issue an error about name-lookup failing. NAME is the
2336 IDENTIFIER_NODE DECL is the result of
2337 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2338 the thing that we hoped to find. */
2341 cp_parser_name_lookup_error (cp_parser* parser,
2344 name_lookup_error desired,
2345 location_t location)
2347 /* If name lookup completely failed, tell the user that NAME was not
2349 if (decl == error_mark_node)
2351 if (parser->scope && parser->scope != global_namespace)
2352 error_at (location, "%<%E::%E%> has not been declared",
2353 parser->scope, name);
2354 else if (parser->scope == global_namespace)
2355 error_at (location, "%<::%E%> has not been declared", name);
2356 else if (parser->object_scope
2357 && !CLASS_TYPE_P (parser->object_scope))
2358 error_at (location, "request for member %qE in non-class type %qT",
2359 name, parser->object_scope);
2360 else if (parser->object_scope)
2361 error_at (location, "%<%T::%E%> has not been declared",
2362 parser->object_scope, name);
2364 error_at (location, "%qE has not been declared", name);
2366 else if (parser->scope && parser->scope != global_namespace)
2371 error_at (location, "%<%E::%E%> is not a type",
2372 parser->scope, name);
2375 error_at (location, "%<%E::%E%> is not a class or namespace",
2376 parser->scope, name);
2380 "%<%E::%E%> is not a class, namespace, or enumeration",
2381 parser->scope, name);
2388 else if (parser->scope == global_namespace)
2393 error_at (location, "%<::%E%> is not a type", name);
2396 error_at (location, "%<::%E%> is not a class or namespace", name);
2400 "%<::%E%> is not a class, namespace, or enumeration",
2412 error_at (location, "%qE is not a type", name);
2415 error_at (location, "%qE is not a class or namespace", name);
2419 "%qE is not a class, namespace, or enumeration", name);
2427 /* If we are parsing tentatively, remember that an error has occurred
2428 during this tentative parse. Returns true if the error was
2429 simulated; false if a message should be issued by the caller. */
2432 cp_parser_simulate_error (cp_parser* parser)
2434 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2436 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2442 /* Check for repeated decl-specifiers. */
2445 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2446 location_t location)
2450 for (ds = ds_first; ds != ds_last; ++ds)
2452 unsigned count = decl_specs->specs[ds];
2455 /* The "long" specifier is a special case because of "long long". */
2459 error_at (location, "%<long long long%> is too long for GCC");
2461 pedwarn_cxx98 (location, OPT_Wlong_long,
2462 "ISO C++ 1998 does not support %<long long%>");
2466 static const char *const decl_spec_names[] = {
2483 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2488 /* This function is called when a type is defined. If type
2489 definitions are forbidden at this point, an error message is
2493 cp_parser_check_type_definition (cp_parser* parser)
2495 /* If types are forbidden here, issue a message. */
2496 if (parser->type_definition_forbidden_message)
2498 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2499 in the message need to be interpreted. */
2500 error (parser->type_definition_forbidden_message);
2506 /* This function is called when the DECLARATOR is processed. The TYPE
2507 was a type defined in the decl-specifiers. If it is invalid to
2508 define a type in the decl-specifiers for DECLARATOR, an error is
2509 issued. TYPE_LOCATION is the location of TYPE and is used
2510 for error reporting. */
2513 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2514 tree type, location_t type_location)
2516 /* [dcl.fct] forbids type definitions in return types.
2517 Unfortunately, it's not easy to know whether or not we are
2518 processing a return type until after the fact. */
2520 && (declarator->kind == cdk_pointer
2521 || declarator->kind == cdk_reference
2522 || declarator->kind == cdk_ptrmem))
2523 declarator = declarator->declarator;
2525 && declarator->kind == cdk_function)
2527 error_at (type_location,
2528 "new types may not be defined in a return type");
2529 inform (type_location,
2530 "(perhaps a semicolon is missing after the definition of %qT)",
2535 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2536 "<" in any valid C++ program. If the next token is indeed "<",
2537 issue a message warning the user about what appears to be an
2538 invalid attempt to form a template-id. LOCATION is the location
2539 of the type-specifier (TYPE) */
2542 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2543 tree type, location_t location)
2545 cp_token_position start = 0;
2547 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2550 error_at (location, "%qT is not a template", type);
2551 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2552 error_at (location, "%qE is not a template", type);
2554 error_at (location, "invalid template-id");
2555 /* Remember the location of the invalid "<". */
2556 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2557 start = cp_lexer_token_position (parser->lexer, true);
2558 /* Consume the "<". */
2559 cp_lexer_consume_token (parser->lexer);
2560 /* Parse the template arguments. */
2561 cp_parser_enclosed_template_argument_list (parser);
2562 /* Permanently remove the invalid template arguments so that
2563 this error message is not issued again. */
2565 cp_lexer_purge_tokens_after (parser->lexer, start);
2569 /* If parsing an integral constant-expression, issue an error message
2570 about the fact that THING appeared and return true. Otherwise,
2571 return false. In either case, set
2572 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2575 cp_parser_non_integral_constant_expression (cp_parser *parser,
2576 non_integral_constant thing)
2578 parser->non_integral_constant_expression_p = true;
2579 if (parser->integral_constant_expression_p)
2581 if (!parser->allow_non_integral_constant_expression_p)
2583 const char *msg = NULL;
2587 error ("floating-point literal "
2588 "cannot appear in a constant-expression");
2591 error ("a cast to a type other than an integral or "
2592 "enumeration type cannot appear in a "
2593 "constant-expression");
2596 error ("%<typeid%> operator "
2597 "cannot appear in a constant-expression");
2600 error ("non-constant compound literals "
2601 "cannot appear in a constant-expression");
2604 error ("a function call "
2605 "cannot appear in a constant-expression");
2608 error ("an increment "
2609 "cannot appear in a constant-expression");
2612 error ("an decrement "
2613 "cannot appear in a constant-expression");
2616 error ("an array reference "
2617 "cannot appear in a constant-expression");
2619 case NIC_ADDR_LABEL:
2620 error ("the address of a label "
2621 "cannot appear in a constant-expression");
2623 case NIC_OVERLOADED:
2624 error ("calls to overloaded operators "
2625 "cannot appear in a constant-expression");
2627 case NIC_ASSIGNMENT:
2628 error ("an assignment cannot appear in a constant-expression");
2631 error ("a comma operator "
2632 "cannot appear in a constant-expression");
2634 case NIC_CONSTRUCTOR:
2635 error ("a call to a constructor "
2636 "cannot appear in a constant-expression");
2642 msg = "__FUNCTION__";
2644 case NIC_PRETTY_FUNC:
2645 msg = "__PRETTY_FUNCTION__";
2665 case NIC_PREINCREMENT:
2668 case NIC_PREDECREMENT:
2681 error ("%qs cannot appear in a constant-expression", msg);
2688 /* Emit a diagnostic for an invalid type name. SCOPE is the
2689 qualifying scope (or NULL, if none) for ID. This function commits
2690 to the current active tentative parse, if any. (Otherwise, the
2691 problematic construct might be encountered again later, resulting
2692 in duplicate error messages.) LOCATION is the location of ID. */
2695 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2696 tree scope, tree id,
2697 location_t location)
2699 tree decl, old_scope;
2700 /* Try to lookup the identifier. */
2701 old_scope = parser->scope;
2702 parser->scope = scope;
2703 decl = cp_parser_lookup_name_simple (parser, id, location);
2704 parser->scope = old_scope;
2705 /* If the lookup found a template-name, it means that the user forgot
2706 to specify an argument list. Emit a useful error message. */
2707 if (TREE_CODE (decl) == TEMPLATE_DECL)
2709 "invalid use of template-name %qE without an argument list",
2711 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2712 error_at (location, "invalid use of destructor %qD as a type", id);
2713 else if (TREE_CODE (decl) == TYPE_DECL)
2714 /* Something like 'unsigned A a;' */
2715 error_at (location, "invalid combination of multiple type-specifiers");
2716 else if (!parser->scope)
2718 /* Issue an error message. */
2719 error_at (location, "%qE does not name a type", id);
2720 /* If we're in a template class, it's possible that the user was
2721 referring to a type from a base class. For example:
2723 template <typename T> struct A { typedef T X; };
2724 template <typename T> struct B : public A<T> { X x; };
2726 The user should have said "typename A<T>::X". */
2727 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2728 inform (location, "C++0x %<constexpr%> only available with "
2729 "-std=c++0x or -std=gnu++0x");
2730 else if (processing_template_decl && current_class_type
2731 && TYPE_BINFO (current_class_type))
2735 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2739 tree base_type = BINFO_TYPE (b);
2740 if (CLASS_TYPE_P (base_type)
2741 && dependent_type_p (base_type))
2744 /* Go from a particular instantiation of the
2745 template (which will have an empty TYPE_FIELDs),
2746 to the main version. */
2747 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2748 for (field = TYPE_FIELDS (base_type);
2750 field = DECL_CHAIN (field))
2751 if (TREE_CODE (field) == TYPE_DECL
2752 && DECL_NAME (field) == id)
2755 "(perhaps %<typename %T::%E%> was intended)",
2756 BINFO_TYPE (b), id);
2765 /* Here we diagnose qualified-ids where the scope is actually correct,
2766 but the identifier does not resolve to a valid type name. */
2767 else if (parser->scope != error_mark_node)
2769 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2770 error_at (location, "%qE in namespace %qE does not name a type",
2772 else if (CLASS_TYPE_P (parser->scope)
2773 && constructor_name_p (id, parser->scope))
2776 error_at (location, "%<%T::%E%> names the constructor, not"
2777 " the type", parser->scope, id);
2778 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2779 error_at (location, "and %qT has no template constructors",
2782 else if (TYPE_P (parser->scope)
2783 && dependent_scope_p (parser->scope))
2784 error_at (location, "need %<typename%> before %<%T::%E%> because "
2785 "%qT is a dependent scope",
2786 parser->scope, id, parser->scope);
2787 else if (TYPE_P (parser->scope))
2788 error_at (location, "%qE in class %qT does not name a type",
2793 cp_parser_commit_to_tentative_parse (parser);
2796 /* Check for a common situation where a type-name should be present,
2797 but is not, and issue a sensible error message. Returns true if an
2798 invalid type-name was detected.
2800 The situation handled by this function are variable declarations of the
2801 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2802 Usually, `ID' should name a type, but if we got here it means that it
2803 does not. We try to emit the best possible error message depending on
2804 how exactly the id-expression looks like. */
2807 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2810 cp_token *token = cp_lexer_peek_token (parser->lexer);
2812 /* Avoid duplicate error about ambiguous lookup. */
2813 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2815 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2816 if (next->type == CPP_NAME && next->ambiguous_p)
2820 cp_parser_parse_tentatively (parser);
2821 id = cp_parser_id_expression (parser,
2822 /*template_keyword_p=*/false,
2823 /*check_dependency_p=*/true,
2824 /*template_p=*/NULL,
2825 /*declarator_p=*/true,
2826 /*optional_p=*/false);
2827 /* If the next token is a (, this is a function with no explicit return
2828 type, i.e. constructor, destructor or conversion op. */
2829 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2830 || TREE_CODE (id) == TYPE_DECL)
2832 cp_parser_abort_tentative_parse (parser);
2835 if (!cp_parser_parse_definitely (parser))
2838 /* Emit a diagnostic for the invalid type. */
2839 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2840 id, token->location);
2842 /* If we aren't in the middle of a declarator (i.e. in a
2843 parameter-declaration-clause), skip to the end of the declaration;
2844 there's no point in trying to process it. */
2845 if (!parser->in_declarator_p)
2846 cp_parser_skip_to_end_of_block_or_statement (parser);
2850 /* Consume tokens up to, and including, the next non-nested closing `)'.
2851 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2852 are doing error recovery. Returns -1 if OR_COMMA is true and we
2853 found an unnested comma. */
2856 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2861 unsigned paren_depth = 0;
2862 unsigned brace_depth = 0;
2863 unsigned square_depth = 0;
2865 if (recovering && !or_comma
2866 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2871 cp_token * token = cp_lexer_peek_token (parser->lexer);
2873 switch (token->type)
2876 case CPP_PRAGMA_EOL:
2877 /* If we've run out of tokens, then there is no closing `)'. */
2880 /* This is good for lambda expression capture-lists. */
2881 case CPP_OPEN_SQUARE:
2884 case CPP_CLOSE_SQUARE:
2885 if (!square_depth--)
2890 /* This matches the processing in skip_to_end_of_statement. */
2895 case CPP_OPEN_BRACE:
2898 case CPP_CLOSE_BRACE:
2904 if (recovering && or_comma && !brace_depth && !paren_depth
2909 case CPP_OPEN_PAREN:
2914 case CPP_CLOSE_PAREN:
2915 if (!brace_depth && !paren_depth--)
2918 cp_lexer_consume_token (parser->lexer);
2927 /* Consume the token. */
2928 cp_lexer_consume_token (parser->lexer);
2932 /* Consume tokens until we reach the end of the current statement.
2933 Normally, that will be just before consuming a `;'. However, if a
2934 non-nested `}' comes first, then we stop before consuming that. */
2937 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2939 unsigned nesting_depth = 0;
2943 cp_token *token = cp_lexer_peek_token (parser->lexer);
2945 switch (token->type)
2948 case CPP_PRAGMA_EOL:
2949 /* If we've run out of tokens, stop. */
2953 /* If the next token is a `;', we have reached the end of the
2959 case CPP_CLOSE_BRACE:
2960 /* If this is a non-nested '}', stop before consuming it.
2961 That way, when confronted with something like:
2965 we stop before consuming the closing '}', even though we
2966 have not yet reached a `;'. */
2967 if (nesting_depth == 0)
2970 /* If it is the closing '}' for a block that we have
2971 scanned, stop -- but only after consuming the token.
2977 we will stop after the body of the erroneously declared
2978 function, but before consuming the following `typedef'
2980 if (--nesting_depth == 0)
2982 cp_lexer_consume_token (parser->lexer);
2986 case CPP_OPEN_BRACE:
2994 /* Consume the token. */
2995 cp_lexer_consume_token (parser->lexer);
2999 /* This function is called at the end of a statement or declaration.
3000 If the next token is a semicolon, it is consumed; otherwise, error
3001 recovery is attempted. */
3004 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3006 /* Look for the trailing `;'. */
3007 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3009 /* If there is additional (erroneous) input, skip to the end of
3011 cp_parser_skip_to_end_of_statement (parser);
3012 /* If the next token is now a `;', consume it. */
3013 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3014 cp_lexer_consume_token (parser->lexer);
3018 /* Skip tokens until we have consumed an entire block, or until we
3019 have consumed a non-nested `;'. */
3022 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3024 int nesting_depth = 0;
3026 while (nesting_depth >= 0)
3028 cp_token *token = cp_lexer_peek_token (parser->lexer);
3030 switch (token->type)
3033 case CPP_PRAGMA_EOL:
3034 /* If we've run out of tokens, stop. */
3038 /* Stop if this is an unnested ';'. */
3043 case CPP_CLOSE_BRACE:
3044 /* Stop if this is an unnested '}', or closes the outermost
3047 if (nesting_depth < 0)
3053 case CPP_OPEN_BRACE:
3062 /* Consume the token. */
3063 cp_lexer_consume_token (parser->lexer);
3067 /* Skip tokens until a non-nested closing curly brace is the next
3068 token, or there are no more tokens. Return true in the first case,
3072 cp_parser_skip_to_closing_brace (cp_parser *parser)
3074 unsigned nesting_depth = 0;
3078 cp_token *token = cp_lexer_peek_token (parser->lexer);
3080 switch (token->type)
3083 case CPP_PRAGMA_EOL:
3084 /* If we've run out of tokens, stop. */
3087 case CPP_CLOSE_BRACE:
3088 /* If the next token is a non-nested `}', then we have reached
3089 the end of the current block. */
3090 if (nesting_depth-- == 0)
3094 case CPP_OPEN_BRACE:
3095 /* If it the next token is a `{', then we are entering a new
3096 block. Consume the entire block. */
3104 /* Consume the token. */
3105 cp_lexer_consume_token (parser->lexer);
3109 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3110 parameter is the PRAGMA token, allowing us to purge the entire pragma
3114 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3118 parser->lexer->in_pragma = false;
3121 token = cp_lexer_consume_token (parser->lexer);
3122 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3124 /* Ensure that the pragma is not parsed again. */
3125 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3128 /* Require pragma end of line, resyncing with it as necessary. The
3129 arguments are as for cp_parser_skip_to_pragma_eol. */
3132 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3134 parser->lexer->in_pragma = false;
3135 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3136 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3139 /* This is a simple wrapper around make_typename_type. When the id is
3140 an unresolved identifier node, we can provide a superior diagnostic
3141 using cp_parser_diagnose_invalid_type_name. */
3144 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3145 tree id, location_t id_location)
3148 if (TREE_CODE (id) == IDENTIFIER_NODE)
3150 result = make_typename_type (scope, id, typename_type,
3151 /*complain=*/tf_none);
3152 if (result == error_mark_node)
3153 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3156 return make_typename_type (scope, id, typename_type, tf_error);
3159 /* This is a wrapper around the
3160 make_{pointer,ptrmem,reference}_declarator functions that decides
3161 which one to call based on the CODE and CLASS_TYPE arguments. The
3162 CODE argument should be one of the values returned by
3163 cp_parser_ptr_operator. */
3164 static cp_declarator *
3165 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3166 cp_cv_quals cv_qualifiers,
3167 cp_declarator *target)
3169 if (code == ERROR_MARK)
3170 return cp_error_declarator;
3172 if (code == INDIRECT_REF)
3173 if (class_type == NULL_TREE)
3174 return make_pointer_declarator (cv_qualifiers, target);
3176 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3177 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3178 return make_reference_declarator (cv_qualifiers, target, false);
3179 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3180 return make_reference_declarator (cv_qualifiers, target, true);
3184 /* Create a new C++ parser. */
3187 cp_parser_new (void)
3193 /* cp_lexer_new_main is called before doing GC allocation because
3194 cp_lexer_new_main might load a PCH file. */
3195 lexer = cp_lexer_new_main ();
3197 /* Initialize the binops_by_token so that we can get the tree
3198 directly from the token. */
3199 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3200 binops_by_token[binops[i].token_type] = binops[i];
3202 parser = ggc_alloc_cleared_cp_parser ();
3203 parser->lexer = lexer;
3204 parser->context = cp_parser_context_new (NULL);
3206 /* For now, we always accept GNU extensions. */
3207 parser->allow_gnu_extensions_p = 1;
3209 /* The `>' token is a greater-than operator, not the end of a
3211 parser->greater_than_is_operator_p = true;
3213 parser->default_arg_ok_p = true;
3215 /* We are not parsing a constant-expression. */
3216 parser->integral_constant_expression_p = false;
3217 parser->allow_non_integral_constant_expression_p = false;
3218 parser->non_integral_constant_expression_p = false;
3220 /* Local variable names are not forbidden. */
3221 parser->local_variables_forbidden_p = false;
3223 /* We are not processing an `extern "C"' declaration. */
3224 parser->in_unbraced_linkage_specification_p = false;
3226 /* We are not processing a declarator. */
3227 parser->in_declarator_p = false;
3229 /* We are not processing a template-argument-list. */
3230 parser->in_template_argument_list_p = false;
3232 /* We are not in an iteration statement. */
3233 parser->in_statement = 0;
3235 /* We are not in a switch statement. */
3236 parser->in_switch_statement_p = false;
3238 /* We are not parsing a type-id inside an expression. */
3239 parser->in_type_id_in_expr_p = false;
3241 /* Declarations aren't implicitly extern "C". */
3242 parser->implicit_extern_c = false;
3244 /* String literals should be translated to the execution character set. */
3245 parser->translate_strings_p = true;
3247 /* We are not parsing a function body. */
3248 parser->in_function_body = false;
3250 /* We can correct until told otherwise. */
3251 parser->colon_corrects_to_scope_p = true;
3253 /* The unparsed function queue is empty. */
3254 push_unparsed_function_queues (parser);
3256 /* There are no classes being defined. */
3257 parser->num_classes_being_defined = 0;
3259 /* No template parameters apply. */
3260 parser->num_template_parameter_lists = 0;
3265 /* Create a cp_lexer structure which will emit the tokens in CACHE
3266 and push it onto the parser's lexer stack. This is used for delayed
3267 parsing of in-class method bodies and default arguments, and should
3268 not be confused with tentative parsing. */
3270 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3272 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3273 lexer->next = parser->lexer;
3274 parser->lexer = lexer;
3276 /* Move the current source position to that of the first token in the
3278 cp_lexer_set_source_position_from_token (lexer->next_token);
3281 /* Pop the top lexer off the parser stack. This is never used for the
3282 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3284 cp_parser_pop_lexer (cp_parser *parser)
3286 cp_lexer *lexer = parser->lexer;
3287 parser->lexer = lexer->next;
3288 cp_lexer_destroy (lexer);
3290 /* Put the current source position back where it was before this
3291 lexer was pushed. */
3292 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3295 /* Lexical conventions [gram.lex] */
3297 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3301 cp_parser_identifier (cp_parser* parser)
3305 /* Look for the identifier. */
3306 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3307 /* Return the value. */
3308 return token ? token->u.value : error_mark_node;
3311 /* Parse a sequence of adjacent string constants. Returns a
3312 TREE_STRING representing the combined, nul-terminated string
3313 constant. If TRANSLATE is true, translate the string to the
3314 execution character set. If WIDE_OK is true, a wide string is
3317 C++98 [lex.string] says that if a narrow string literal token is
3318 adjacent to a wide string literal token, the behavior is undefined.
3319 However, C99 6.4.5p4 says that this results in a wide string literal.
3320 We follow C99 here, for consistency with the C front end.
3322 This code is largely lifted from lex_string() in c-lex.c.
3324 FUTURE: ObjC++ will need to handle @-strings here. */
3326 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3330 struct obstack str_ob;
3331 cpp_string str, istr, *strs;
3333 enum cpp_ttype type;
3335 tok = cp_lexer_peek_token (parser->lexer);
3336 if (!cp_parser_is_string_literal (tok))
3338 cp_parser_error (parser, "expected string-literal");
3339 return error_mark_node;
3344 /* Try to avoid the overhead of creating and destroying an obstack
3345 for the common case of just one string. */
3346 if (!cp_parser_is_string_literal
3347 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3349 cp_lexer_consume_token (parser->lexer);
3351 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3352 str.len = TREE_STRING_LENGTH (tok->u.value);
3359 gcc_obstack_init (&str_ob);
3364 cp_lexer_consume_token (parser->lexer);
3366 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3367 str.len = TREE_STRING_LENGTH (tok->u.value);
3369 if (type != tok->type)
3371 if (type == CPP_STRING)
3373 else if (tok->type != CPP_STRING)
3374 error_at (tok->location,
3375 "unsupported non-standard concatenation "
3376 "of string literals");
3379 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3381 tok = cp_lexer_peek_token (parser->lexer);
3383 while (cp_parser_is_string_literal (tok));
3385 strs = (cpp_string *) obstack_finish (&str_ob);
3388 if (type != CPP_STRING && !wide_ok)
3390 cp_parser_error (parser, "a wide string is invalid in this context");
3394 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3395 (parse_in, strs, count, &istr, type))
3397 value = build_string (istr.len, (const char *)istr.text);
3398 free (CONST_CAST (unsigned char *, istr.text));
3404 case CPP_UTF8STRING:
3405 TREE_TYPE (value) = char_array_type_node;
3408 TREE_TYPE (value) = char16_array_type_node;
3411 TREE_TYPE (value) = char32_array_type_node;
3414 TREE_TYPE (value) = wchar_array_type_node;
3418 value = fix_string_type (value);
3421 /* cpp_interpret_string has issued an error. */
3422 value = error_mark_node;
3425 obstack_free (&str_ob, 0);
3431 /* Basic concepts [gram.basic] */
3433 /* Parse a translation-unit.
3436 declaration-seq [opt]
3438 Returns TRUE if all went well. */
3441 cp_parser_translation_unit (cp_parser* parser)
3443 /* The address of the first non-permanent object on the declarator
3445 static void *declarator_obstack_base;
3449 /* Create the declarator obstack, if necessary. */
3450 if (!cp_error_declarator)
3452 gcc_obstack_init (&declarator_obstack);
3453 /* Create the error declarator. */
3454 cp_error_declarator = make_declarator (cdk_error);
3455 /* Create the empty parameter list. */
3456 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3457 /* Remember where the base of the declarator obstack lies. */
3458 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3461 cp_parser_declaration_seq_opt (parser);
3463 /* If there are no tokens left then all went well. */
3464 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3466 /* Get rid of the token array; we don't need it any more. */
3467 cp_lexer_destroy (parser->lexer);
3468 parser->lexer = NULL;
3470 /* This file might have been a context that's implicitly extern
3471 "C". If so, pop the lang context. (Only relevant for PCH.) */
3472 if (parser->implicit_extern_c)
3474 pop_lang_context ();
3475 parser->implicit_extern_c = false;
3479 finish_translation_unit ();
3485 cp_parser_error (parser, "expected declaration");
3489 /* Make sure the declarator obstack was fully cleaned up. */
3490 gcc_assert (obstack_next_free (&declarator_obstack)
3491 == declarator_obstack_base);
3493 /* All went well. */
3497 /* Expressions [gram.expr] */
3499 /* Parse a primary-expression.
3510 ( compound-statement )
3511 __builtin_va_arg ( assignment-expression , type-id )
3512 __builtin_offsetof ( type-id , offsetof-expression )
3515 __has_nothrow_assign ( type-id )
3516 __has_nothrow_constructor ( type-id )
3517 __has_nothrow_copy ( type-id )
3518 __has_trivial_assign ( type-id )
3519 __has_trivial_constructor ( type-id )
3520 __has_trivial_copy ( type-id )
3521 __has_trivial_destructor ( type-id )
3522 __has_virtual_destructor ( type-id )
3523 __is_abstract ( type-id )
3524 __is_base_of ( type-id , type-id )
3525 __is_class ( type-id )
3526 __is_convertible_to ( type-id , type-id )
3527 __is_empty ( type-id )
3528 __is_enum ( type-id )
3529 __is_pod ( type-id )
3530 __is_polymorphic ( type-id )
3531 __is_union ( type-id )
3533 Objective-C++ Extension:
3541 ADDRESS_P is true iff this expression was immediately preceded by
3542 "&" and therefore might denote a pointer-to-member. CAST_P is true
3543 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3544 true iff this expression is a template argument.
3546 Returns a representation of the expression. Upon return, *IDK
3547 indicates what kind of id-expression (if any) was present. */
3550 cp_parser_primary_expression (cp_parser *parser,
3553 bool template_arg_p,
3556 cp_token *token = NULL;
3558 /* Assume the primary expression is not an id-expression. */
3559 *idk = CP_ID_KIND_NONE;
3561 /* Peek at the next token. */
3562 token = cp_lexer_peek_token (parser->lexer);
3563 switch (token->type)
3576 token = cp_lexer_consume_token (parser->lexer);
3577 if (TREE_CODE (token->u.value) == FIXED_CST)
3579 error_at (token->location,
3580 "fixed-point types not supported in C++");
3581 return error_mark_node;
3583 /* Floating-point literals are only allowed in an integral
3584 constant expression if they are cast to an integral or
3585 enumeration type. */
3586 if (TREE_CODE (token->u.value) == REAL_CST
3587 && parser->integral_constant_expression_p
3590 /* CAST_P will be set even in invalid code like "int(2.7 +
3591 ...)". Therefore, we have to check that the next token
3592 is sure to end the cast. */
3595 cp_token *next_token;
3597 next_token = cp_lexer_peek_token (parser->lexer);
3598 if (/* The comma at the end of an
3599 enumerator-definition. */
3600 next_token->type != CPP_COMMA
3601 /* The curly brace at the end of an enum-specifier. */
3602 && next_token->type != CPP_CLOSE_BRACE
3603 /* The end of a statement. */
3604 && next_token->type != CPP_SEMICOLON
3605 /* The end of the cast-expression. */
3606 && next_token->type != CPP_CLOSE_PAREN
3607 /* The end of an array bound. */
3608 && next_token->type != CPP_CLOSE_SQUARE
3609 /* The closing ">" in a template-argument-list. */
3610 && (next_token->type != CPP_GREATER
3611 || parser->greater_than_is_operator_p)
3612 /* C++0x only: A ">>" treated like two ">" tokens,
3613 in a template-argument-list. */
3614 && (next_token->type != CPP_RSHIFT
3615 || (cxx_dialect == cxx98)
3616 || parser->greater_than_is_operator_p))
3620 /* If we are within a cast, then the constraint that the
3621 cast is to an integral or enumeration type will be
3622 checked at that point. If we are not within a cast, then
3623 this code is invalid. */
3625 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3627 return token->u.value;
3633 case CPP_UTF8STRING:
3634 /* ??? Should wide strings be allowed when parser->translate_strings_p
3635 is false (i.e. in attributes)? If not, we can kill the third
3636 argument to cp_parser_string_literal. */
3637 return cp_parser_string_literal (parser,
3638 parser->translate_strings_p,
3641 case CPP_OPEN_PAREN:
3644 bool saved_greater_than_is_operator_p;
3646 /* Consume the `('. */
3647 cp_lexer_consume_token (parser->lexer);
3648 /* Within a parenthesized expression, a `>' token is always
3649 the greater-than operator. */
3650 saved_greater_than_is_operator_p
3651 = parser->greater_than_is_operator_p;
3652 parser->greater_than_is_operator_p = true;
3653 /* If we see `( { ' then we are looking at the beginning of
3654 a GNU statement-expression. */
3655 if (cp_parser_allow_gnu_extensions_p (parser)
3656 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3658 /* Statement-expressions are not allowed by the standard. */
3659 pedwarn (token->location, OPT_pedantic,
3660 "ISO C++ forbids braced-groups within expressions");
3662 /* And they're not allowed outside of a function-body; you
3663 cannot, for example, write:
3665 int i = ({ int j = 3; j + 1; });
3667 at class or namespace scope. */
3668 if (!parser->in_function_body
3669 || parser->in_template_argument_list_p)
3671 error_at (token->location,
3672 "statement-expressions are not allowed outside "
3673 "functions nor in template-argument lists");
3674 cp_parser_skip_to_end_of_block_or_statement (parser);
3675 expr = error_mark_node;
3679 /* Start the statement-expression. */
3680 expr = begin_stmt_expr ();
3681 /* Parse the compound-statement. */
3682 cp_parser_compound_statement (parser, expr, false);
3684 expr = finish_stmt_expr (expr, false);
3689 /* Parse the parenthesized expression. */
3690 expr = cp_parser_expression (parser, cast_p, idk);
3691 /* Let the front end know that this expression was
3692 enclosed in parentheses. This matters in case, for
3693 example, the expression is of the form `A::B', since
3694 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3696 finish_parenthesized_expr (expr);
3698 /* The `>' token might be the end of a template-id or
3699 template-parameter-list now. */
3700 parser->greater_than_is_operator_p
3701 = saved_greater_than_is_operator_p;
3702 /* Consume the `)'. */
3703 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3704 cp_parser_skip_to_end_of_statement (parser);
3709 case CPP_OPEN_SQUARE:
3710 if (c_dialect_objc ())
3711 /* We have an Objective-C++ message. */
3712 return cp_parser_objc_expression (parser);
3713 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3714 return cp_parser_lambda_expression (parser);
3716 case CPP_OBJC_STRING:
3717 if (c_dialect_objc ())
3718 /* We have an Objective-C++ string literal. */
3719 return cp_parser_objc_expression (parser);
3720 cp_parser_error (parser, "expected primary-expression");
3721 return error_mark_node;
3724 switch (token->keyword)
3726 /* These two are the boolean literals. */
3728 cp_lexer_consume_token (parser->lexer);
3729 return boolean_true_node;
3731 cp_lexer_consume_token (parser->lexer);
3732 return boolean_false_node;
3734 /* The `__null' literal. */
3736 cp_lexer_consume_token (parser->lexer);
3739 /* The `nullptr' literal. */
3741 cp_lexer_consume_token (parser->lexer);
3742 return nullptr_node;
3744 /* Recognize the `this' keyword. */
3746 cp_lexer_consume_token (parser->lexer);
3747 if (parser->local_variables_forbidden_p)
3749 error_at (token->location,
3750 "%<this%> may not be used in this context");
3751 return error_mark_node;
3753 /* Pointers cannot appear in constant-expressions. */
3754 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3755 return error_mark_node;
3756 return finish_this_expr ();
3758 /* The `operator' keyword can be the beginning of an
3763 case RID_FUNCTION_NAME:
3764 case RID_PRETTY_FUNCTION_NAME:
3765 case RID_C99_FUNCTION_NAME:
3767 non_integral_constant name;
3769 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3770 __func__ are the names of variables -- but they are
3771 treated specially. Therefore, they are handled here,
3772 rather than relying on the generic id-expression logic
3773 below. Grammatically, these names are id-expressions.
3775 Consume the token. */
3776 token = cp_lexer_consume_token (parser->lexer);
3778 switch (token->keyword)
3780 case RID_FUNCTION_NAME:
3781 name = NIC_FUNC_NAME;
3783 case RID_PRETTY_FUNCTION_NAME:
3784 name = NIC_PRETTY_FUNC;
3786 case RID_C99_FUNCTION_NAME:
3787 name = NIC_C99_FUNC;
3793 if (cp_parser_non_integral_constant_expression (parser, name))
3794 return error_mark_node;
3796 /* Look up the name. */
3797 return finish_fname (token->u.value);
3805 /* The `__builtin_va_arg' construct is used to handle
3806 `va_arg'. Consume the `__builtin_va_arg' token. */
3807 cp_lexer_consume_token (parser->lexer);
3808 /* Look for the opening `('. */
3809 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3810 /* Now, parse the assignment-expression. */
3811 expression = cp_parser_assignment_expression (parser,
3812 /*cast_p=*/false, NULL);
3813 /* Look for the `,'. */
3814 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3815 /* Parse the type-id. */
3816 type = cp_parser_type_id (parser);
3817 /* Look for the closing `)'. */
3818 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3819 /* Using `va_arg' in a constant-expression is not
3821 if (cp_parser_non_integral_constant_expression (parser,
3823 return error_mark_node;
3824 return build_x_va_arg (expression, type);
3828 return cp_parser_builtin_offsetof (parser);
3830 case RID_HAS_NOTHROW_ASSIGN:
3831 case RID_HAS_NOTHROW_CONSTRUCTOR:
3832 case RID_HAS_NOTHROW_COPY:
3833 case RID_HAS_TRIVIAL_ASSIGN:
3834 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3835 case RID_HAS_TRIVIAL_COPY:
3836 case RID_HAS_TRIVIAL_DESTRUCTOR:
3837 case RID_HAS_VIRTUAL_DESTRUCTOR:
3838 case RID_IS_ABSTRACT:
3839 case RID_IS_BASE_OF:
3841 case RID_IS_CONVERTIBLE_TO:
3845 case RID_IS_POLYMORPHIC:
3846 case RID_IS_STD_LAYOUT:
3847 case RID_IS_TRIVIAL:
3849 case RID_IS_LITERAL_TYPE:
3850 return cp_parser_trait_expr (parser, token->keyword);
3852 /* Objective-C++ expressions. */
3854 case RID_AT_PROTOCOL:
3855 case RID_AT_SELECTOR:
3856 return cp_parser_objc_expression (parser);
3859 if (parser->in_function_body
3860 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3863 error_at (token->location,
3864 "a template declaration cannot appear at block scope");
3865 cp_parser_skip_to_end_of_block_or_statement (parser);
3866 return error_mark_node;
3869 cp_parser_error (parser, "expected primary-expression");
3870 return error_mark_node;
3873 /* An id-expression can start with either an identifier, a
3874 `::' as the beginning of a qualified-id, or the "operator"
3878 case CPP_TEMPLATE_ID:
3879 case CPP_NESTED_NAME_SPECIFIER:
3883 const char *error_msg;
3886 cp_token *id_expr_token;
3889 /* Parse the id-expression. */
3891 = cp_parser_id_expression (parser,
3892 /*template_keyword_p=*/false,
3893 /*check_dependency_p=*/true,
3895 /*declarator_p=*/false,
3896 /*optional_p=*/false);
3897 if (id_expression == error_mark_node)
3898 return error_mark_node;
3899 id_expr_token = token;
3900 token = cp_lexer_peek_token (parser->lexer);
3901 done = (token->type != CPP_OPEN_SQUARE
3902 && token->type != CPP_OPEN_PAREN
3903 && token->type != CPP_DOT
3904 && token->type != CPP_DEREF
3905 && token->type != CPP_PLUS_PLUS
3906 && token->type != CPP_MINUS_MINUS);
3907 /* If we have a template-id, then no further lookup is
3908 required. If the template-id was for a template-class, we
3909 will sometimes have a TYPE_DECL at this point. */
3910 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3911 || TREE_CODE (id_expression) == TYPE_DECL)
3912 decl = id_expression;
3913 /* Look up the name. */
3916 tree ambiguous_decls;
3918 /* If we already know that this lookup is ambiguous, then
3919 we've already issued an error message; there's no reason
3921 if (id_expr_token->type == CPP_NAME
3922 && id_expr_token->ambiguous_p)
3924 cp_parser_simulate_error (parser);
3925 return error_mark_node;
3928 decl = cp_parser_lookup_name (parser, id_expression,
3931 /*is_namespace=*/false,
3932 /*check_dependency=*/true,
3934 id_expr_token->location);
3935 /* If the lookup was ambiguous, an error will already have
3937 if (ambiguous_decls)
3938 return error_mark_node;
3940 /* In Objective-C++, we may have an Objective-C 2.0
3941 dot-syntax for classes here. */
3942 if (c_dialect_objc ()
3943 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3944 && TREE_CODE (decl) == TYPE_DECL
3945 && objc_is_class_name (decl))
3948 cp_lexer_consume_token (parser->lexer);
3949 component = cp_parser_identifier (parser);
3950 if (component == error_mark_node)
3951 return error_mark_node;
3953 return objc_build_class_component_ref (id_expression, component);
3956 /* In Objective-C++, an instance variable (ivar) may be preferred
3957 to whatever cp_parser_lookup_name() found. */
3958 decl = objc_lookup_ivar (decl, id_expression);
3960 /* If name lookup gives us a SCOPE_REF, then the
3961 qualifying scope was dependent. */
3962 if (TREE_CODE (decl) == SCOPE_REF)
3964 /* At this point, we do not know if DECL is a valid
3965 integral constant expression. We assume that it is
3966 in fact such an expression, so that code like:
3968 template <int N> struct A {
3972 is accepted. At template-instantiation time, we
3973 will check that B<N>::i is actually a constant. */
3976 /* Check to see if DECL is a local variable in a context
3977 where that is forbidden. */
3978 if (parser->local_variables_forbidden_p
3979 && local_variable_p (decl))
3981 /* It might be that we only found DECL because we are
3982 trying to be generous with pre-ISO scoping rules.
3983 For example, consider:
3987 for (int i = 0; i < 10; ++i) {}
3988 extern void f(int j = i);
3991 Here, name look up will originally find the out
3992 of scope `i'. We need to issue a warning message,
3993 but then use the global `i'. */
3994 decl = check_for_out_of_scope_variable (decl);
3995 if (local_variable_p (decl))
3997 error_at (id_expr_token->location,
3998 "local variable %qD may not appear in this context",
4000 return error_mark_node;
4005 decl = (finish_id_expression
4006 (id_expression, decl, parser->scope,
4008 parser->integral_constant_expression_p,
4009 parser->allow_non_integral_constant_expression_p,
4010 &parser->non_integral_constant_expression_p,
4011 template_p, done, address_p,
4014 id_expr_token->location));
4016 cp_parser_error (parser, error_msg);
4020 /* Anything else is an error. */
4022 cp_parser_error (parser, "expected primary-expression");
4023 return error_mark_node;
4027 /* Parse an id-expression.
4034 :: [opt] nested-name-specifier template [opt] unqualified-id
4036 :: operator-function-id
4039 Return a representation of the unqualified portion of the
4040 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4041 a `::' or nested-name-specifier.
4043 Often, if the id-expression was a qualified-id, the caller will
4044 want to make a SCOPE_REF to represent the qualified-id. This
4045 function does not do this in order to avoid wastefully creating
4046 SCOPE_REFs when they are not required.
4048 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4051 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4052 uninstantiated templates.
4054 If *TEMPLATE_P is non-NULL, it is set to true iff the
4055 `template' keyword is used to explicitly indicate that the entity
4056 named is a template.
4058 If DECLARATOR_P is true, the id-expression is appearing as part of
4059 a declarator, rather than as part of an expression. */
4062 cp_parser_id_expression (cp_parser *parser,
4063 bool template_keyword_p,
4064 bool check_dependency_p,
4069 bool global_scope_p;
4070 bool nested_name_specifier_p;
4072 /* Assume the `template' keyword was not used. */
4074 *template_p = template_keyword_p;
4076 /* Look for the optional `::' operator. */
4078 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4080 /* Look for the optional nested-name-specifier. */
4081 nested_name_specifier_p
4082 = (cp_parser_nested_name_specifier_opt (parser,
4083 /*typename_keyword_p=*/false,
4088 /* If there is a nested-name-specifier, then we are looking at
4089 the first qualified-id production. */
4090 if (nested_name_specifier_p)
4093 tree saved_object_scope;
4094 tree saved_qualifying_scope;
4095 tree unqualified_id;
4098 /* See if the next token is the `template' keyword. */
4100 template_p = &is_template;
4101 *template_p = cp_parser_optional_template_keyword (parser);
4102 /* Name lookup we do during the processing of the
4103 unqualified-id might obliterate SCOPE. */
4104 saved_scope = parser->scope;
4105 saved_object_scope = parser->object_scope;
4106 saved_qualifying_scope = parser->qualifying_scope;
4107 /* Process the final unqualified-id. */
4108 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4111 /*optional_p=*/false);
4112 /* Restore the SAVED_SCOPE for our caller. */
4113 parser->scope = saved_scope;
4114 parser->object_scope = saved_object_scope;
4115 parser->qualifying_scope = saved_qualifying_scope;
4117 return unqualified_id;
4119 /* Otherwise, if we are in global scope, then we are looking at one
4120 of the other qualified-id productions. */
4121 else if (global_scope_p)
4126 /* Peek at the next token. */
4127 token = cp_lexer_peek_token (parser->lexer);
4129 /* If it's an identifier, and the next token is not a "<", then
4130 we can avoid the template-id case. This is an optimization
4131 for this common case. */
4132 if (token->type == CPP_NAME
4133 && !cp_parser_nth_token_starts_template_argument_list_p
4135 return cp_parser_identifier (parser);
4137 cp_parser_parse_tentatively (parser);
4138 /* Try a template-id. */
4139 id = cp_parser_template_id (parser,
4140 /*template_keyword_p=*/false,
4141 /*check_dependency_p=*/true,
4143 /* If that worked, we're done. */
4144 if (cp_parser_parse_definitely (parser))
4147 /* Peek at the next token. (Changes in the token buffer may
4148 have invalidated the pointer obtained above.) */
4149 token = cp_lexer_peek_token (parser->lexer);
4151 switch (token->type)
4154 return cp_parser_identifier (parser);
4157 if (token->keyword == RID_OPERATOR)
4158 return cp_parser_operator_function_id (parser);
4162 cp_parser_error (parser, "expected id-expression");
4163 return error_mark_node;
4167 return cp_parser_unqualified_id (parser, template_keyword_p,
4168 /*check_dependency_p=*/true,
4173 /* Parse an unqualified-id.
4177 operator-function-id
4178 conversion-function-id
4182 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4183 keyword, in a construct like `A::template ...'.
4185 Returns a representation of unqualified-id. For the `identifier'
4186 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4187 production a BIT_NOT_EXPR is returned; the operand of the
4188 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4189 other productions, see the documentation accompanying the
4190 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4191 names are looked up in uninstantiated templates. If DECLARATOR_P
4192 is true, the unqualified-id is appearing as part of a declarator,
4193 rather than as part of an expression. */
4196 cp_parser_unqualified_id (cp_parser* parser,
4197 bool template_keyword_p,
4198 bool check_dependency_p,
4204 /* Peek at the next token. */
4205 token = cp_lexer_peek_token (parser->lexer);
4207 switch (token->type)
4213 /* We don't know yet whether or not this will be a
4215 cp_parser_parse_tentatively (parser);
4216 /* Try a template-id. */
4217 id = cp_parser_template_id (parser, template_keyword_p,
4220 /* If it worked, we're done. */
4221 if (cp_parser_parse_definitely (parser))
4223 /* Otherwise, it's an ordinary identifier. */
4224 return cp_parser_identifier (parser);
4227 case CPP_TEMPLATE_ID:
4228 return cp_parser_template_id (parser, template_keyword_p,
4235 tree qualifying_scope;
4240 /* Consume the `~' token. */
4241 cp_lexer_consume_token (parser->lexer);
4242 /* Parse the class-name. The standard, as written, seems to
4245 template <typename T> struct S { ~S (); };
4246 template <typename T> S<T>::~S() {}
4248 is invalid, since `~' must be followed by a class-name, but
4249 `S<T>' is dependent, and so not known to be a class.
4250 That's not right; we need to look in uninstantiated
4251 templates. A further complication arises from:
4253 template <typename T> void f(T t) {
4257 Here, it is not possible to look up `T' in the scope of `T'
4258 itself. We must look in both the current scope, and the
4259 scope of the containing complete expression.
4261 Yet another issue is:
4270 The standard does not seem to say that the `S' in `~S'
4271 should refer to the type `S' and not the data member
4274 /* DR 244 says that we look up the name after the "~" in the
4275 same scope as we looked up the qualifying name. That idea
4276 isn't fully worked out; it's more complicated than that. */
4277 scope = parser->scope;
4278 object_scope = parser->object_scope;
4279 qualifying_scope = parser->qualifying_scope;
4281 /* Check for invalid scopes. */
4282 if (scope == error_mark_node)
4284 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4285 cp_lexer_consume_token (parser->lexer);
4286 return error_mark_node;
4288 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4290 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4291 error_at (token->location,
4292 "scope %qT before %<~%> is not a class-name",
4294 cp_parser_simulate_error (parser);
4295 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4296 cp_lexer_consume_token (parser->lexer);
4297 return error_mark_node;
4299 gcc_assert (!scope || TYPE_P (scope));
4301 /* If the name is of the form "X::~X" it's OK even if X is a
4303 token = cp_lexer_peek_token (parser->lexer);
4305 && token->type == CPP_NAME
4306 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4308 && (token->u.value == TYPE_IDENTIFIER (scope)
4309 || constructor_name_p (token->u.value, scope)))
4311 cp_lexer_consume_token (parser->lexer);
4312 return build_nt (BIT_NOT_EXPR, scope);
4315 /* If there was an explicit qualification (S::~T), first look
4316 in the scope given by the qualification (i.e., S).
4318 Note: in the calls to cp_parser_class_name below we pass
4319 typename_type so that lookup finds the injected-class-name
4320 rather than the constructor. */
4322 type_decl = NULL_TREE;
4325 cp_parser_parse_tentatively (parser);
4326 type_decl = cp_parser_class_name (parser,
4327 /*typename_keyword_p=*/false,
4328 /*template_keyword_p=*/false,
4330 /*check_dependency=*/false,
4331 /*class_head_p=*/false,
4333 if (cp_parser_parse_definitely (parser))
4336 /* In "N::S::~S", look in "N" as well. */
4337 if (!done && scope && qualifying_scope)
4339 cp_parser_parse_tentatively (parser);
4340 parser->scope = qualifying_scope;
4341 parser->object_scope = NULL_TREE;
4342 parser->qualifying_scope = NULL_TREE;
4344 = cp_parser_class_name (parser,
4345 /*typename_keyword_p=*/false,
4346 /*template_keyword_p=*/false,
4348 /*check_dependency=*/false,
4349 /*class_head_p=*/false,
4351 if (cp_parser_parse_definitely (parser))
4354 /* In "p->S::~T", look in the scope given by "*p" as well. */
4355 else if (!done && object_scope)
4357 cp_parser_parse_tentatively (parser);
4358 parser->scope = object_scope;
4359 parser->object_scope = NULL_TREE;
4360 parser->qualifying_scope = NULL_TREE;
4362 = cp_parser_class_name (parser,
4363 /*typename_keyword_p=*/false,
4364 /*template_keyword_p=*/false,
4366 /*check_dependency=*/false,
4367 /*class_head_p=*/false,
4369 if (cp_parser_parse_definitely (parser))
4372 /* Look in the surrounding context. */
4375 parser->scope = NULL_TREE;
4376 parser->object_scope = NULL_TREE;
4377 parser->qualifying_scope = NULL_TREE;
4378 if (processing_template_decl)
4379 cp_parser_parse_tentatively (parser);
4381 = cp_parser_class_name (parser,
4382 /*typename_keyword_p=*/false,
4383 /*template_keyword_p=*/false,
4385 /*check_dependency=*/false,
4386 /*class_head_p=*/false,
4388 if (processing_template_decl
4389 && ! cp_parser_parse_definitely (parser))
4391 /* We couldn't find a type with this name, so just accept
4392 it and check for a match at instantiation time. */
4393 type_decl = cp_parser_identifier (parser);
4394 if (type_decl != error_mark_node)
4395 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4399 /* If an error occurred, assume that the name of the
4400 destructor is the same as the name of the qualifying
4401 class. That allows us to keep parsing after running
4402 into ill-formed destructor names. */
4403 if (type_decl == error_mark_node && scope)
4404 return build_nt (BIT_NOT_EXPR, scope);
4405 else if (type_decl == error_mark_node)
4406 return error_mark_node;
4408 /* Check that destructor name and scope match. */
4409 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4411 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4412 error_at (token->location,
4413 "declaration of %<~%T%> as member of %qT",
4415 cp_parser_simulate_error (parser);
4416 return error_mark_node;
4421 A typedef-name that names a class shall not be used as the
4422 identifier in the declarator for a destructor declaration. */
4424 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4425 && !DECL_SELF_REFERENCE_P (type_decl)
4426 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4427 error_at (token->location,
4428 "typedef-name %qD used as destructor declarator",
4431 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4435 if (token->keyword == RID_OPERATOR)
4439 /* This could be a template-id, so we try that first. */
4440 cp_parser_parse_tentatively (parser);
4441 /* Try a template-id. */
4442 id = cp_parser_template_id (parser, template_keyword_p,
4443 /*check_dependency_p=*/true,
4445 /* If that worked, we're done. */
4446 if (cp_parser_parse_definitely (parser))
4448 /* We still don't know whether we're looking at an
4449 operator-function-id or a conversion-function-id. */
4450 cp_parser_parse_tentatively (parser);
4451 /* Try an operator-function-id. */
4452 id = cp_parser_operator_function_id (parser);
4453 /* If that didn't work, try a conversion-function-id. */
4454 if (!cp_parser_parse_definitely (parser))
4455 id = cp_parser_conversion_function_id (parser);
4464 cp_parser_error (parser, "expected unqualified-id");
4465 return error_mark_node;
4469 /* Parse an (optional) nested-name-specifier.
4471 nested-name-specifier: [C++98]
4472 class-or-namespace-name :: nested-name-specifier [opt]
4473 class-or-namespace-name :: template nested-name-specifier [opt]
4475 nested-name-specifier: [C++0x]
4478 nested-name-specifier identifier ::
4479 nested-name-specifier template [opt] simple-template-id ::
4481 PARSER->SCOPE should be set appropriately before this function is
4482 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4483 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4486 Sets PARSER->SCOPE to the class (TYPE) or namespace
4487 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4488 it unchanged if there is no nested-name-specifier. Returns the new
4489 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4491 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4492 part of a declaration and/or decl-specifier. */
4495 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4496 bool typename_keyword_p,
4497 bool check_dependency_p,
4499 bool is_declaration)
4501 bool success = false;
4502 cp_token_position start = 0;
4505 /* Remember where the nested-name-specifier starts. */
4506 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4508 start = cp_lexer_token_position (parser->lexer, false);
4509 push_deferring_access_checks (dk_deferred);
4516 tree saved_qualifying_scope;
4517 bool template_keyword_p;
4519 /* Spot cases that cannot be the beginning of a
4520 nested-name-specifier. */
4521 token = cp_lexer_peek_token (parser->lexer);
4523 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4524 the already parsed nested-name-specifier. */
4525 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4527 /* Grab the nested-name-specifier and continue the loop. */
4528 cp_parser_pre_parsed_nested_name_specifier (parser);
4529 /* If we originally encountered this nested-name-specifier
4530 with IS_DECLARATION set to false, we will not have
4531 resolved TYPENAME_TYPEs, so we must do so here. */
4533 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4535 new_scope = resolve_typename_type (parser->scope,
4536 /*only_current_p=*/false);
4537 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4538 parser->scope = new_scope;
4544 /* Spot cases that cannot be the beginning of a
4545 nested-name-specifier. On the second and subsequent times
4546 through the loop, we look for the `template' keyword. */
4547 if (success && token->keyword == RID_TEMPLATE)
4549 /* A template-id can start a nested-name-specifier. */
4550 else if (token->type == CPP_TEMPLATE_ID)
4554 /* If the next token is not an identifier, then it is
4555 definitely not a type-name or namespace-name. */
4556 if (token->type != CPP_NAME)
4558 /* If the following token is neither a `<' (to begin a
4559 template-id), nor a `::', then we are not looking at a
4560 nested-name-specifier. */
4561 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4563 if (token->type == CPP_COLON
4564 && parser->colon_corrects_to_scope_p
4565 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4567 error_at (token->location,
4568 "found %<:%> in nested-name-specifier, expected %<::%>");
4569 token->type = CPP_SCOPE;
4572 if (token->type != CPP_SCOPE
4573 && !cp_parser_nth_token_starts_template_argument_list_p
4578 /* The nested-name-specifier is optional, so we parse
4580 cp_parser_parse_tentatively (parser);
4582 /* Look for the optional `template' keyword, if this isn't the
4583 first time through the loop. */
4585 template_keyword_p = cp_parser_optional_template_keyword (parser);
4587 template_keyword_p = false;
4589 /* Save the old scope since the name lookup we are about to do
4590 might destroy it. */
4591 old_scope = parser->scope;
4592 saved_qualifying_scope = parser->qualifying_scope;
4593 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4594 look up names in "X<T>::I" in order to determine that "Y" is
4595 a template. So, if we have a typename at this point, we make
4596 an effort to look through it. */
4598 && !typename_keyword_p
4600 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4601 parser->scope = resolve_typename_type (parser->scope,
4602 /*only_current_p=*/false);
4603 /* Parse the qualifying entity. */
4605 = cp_parser_qualifying_entity (parser,
4611 /* Look for the `::' token. */
4612 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4614 /* If we found what we wanted, we keep going; otherwise, we're
4616 if (!cp_parser_parse_definitely (parser))
4618 bool error_p = false;
4620 /* Restore the OLD_SCOPE since it was valid before the
4621 failed attempt at finding the last
4622 class-or-namespace-name. */
4623 parser->scope = old_scope;
4624 parser->qualifying_scope = saved_qualifying_scope;
4625 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4627 /* If the next token is an identifier, and the one after
4628 that is a `::', then any valid interpretation would have
4629 found a class-or-namespace-name. */
4630 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4631 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4633 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4636 token = cp_lexer_consume_token (parser->lexer);
4639 if (!token->ambiguous_p)
4642 tree ambiguous_decls;
4644 decl = cp_parser_lookup_name (parser, token->u.value,
4646 /*is_template=*/false,
4647 /*is_namespace=*/false,
4648 /*check_dependency=*/true,
4651 if (TREE_CODE (decl) == TEMPLATE_DECL)
4652 error_at (token->location,
4653 "%qD used without template parameters",
4655 else if (ambiguous_decls)
4657 error_at (token->location,
4658 "reference to %qD is ambiguous",
4660 print_candidates (ambiguous_decls);
4661 decl = error_mark_node;
4665 if (cxx_dialect != cxx98)
4666 cp_parser_name_lookup_error
4667 (parser, token->u.value, decl, NLE_NOT_CXX98,
4670 cp_parser_name_lookup_error
4671 (parser, token->u.value, decl, NLE_CXX98,
4675 parser->scope = error_mark_node;
4677 /* Treat this as a successful nested-name-specifier
4682 If the name found is not a class-name (clause
4683 _class_) or namespace-name (_namespace.def_), the
4684 program is ill-formed. */
4687 cp_lexer_consume_token (parser->lexer);
4691 /* We've found one valid nested-name-specifier. */
4693 /* Name lookup always gives us a DECL. */
4694 if (TREE_CODE (new_scope) == TYPE_DECL)
4695 new_scope = TREE_TYPE (new_scope);
4696 /* Uses of "template" must be followed by actual templates. */
4697 if (template_keyword_p
4698 && !(CLASS_TYPE_P (new_scope)
4699 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4700 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4701 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4702 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4703 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4704 == TEMPLATE_ID_EXPR)))
4705 permerror (input_location, TYPE_P (new_scope)
4706 ? "%qT is not a template"
4707 : "%qD is not a template",
4709 /* If it is a class scope, try to complete it; we are about to
4710 be looking up names inside the class. */
4711 if (TYPE_P (new_scope)
4712 /* Since checking types for dependency can be expensive,
4713 avoid doing it if the type is already complete. */
4714 && !COMPLETE_TYPE_P (new_scope)
4715 /* Do not try to complete dependent types. */
4716 && !dependent_type_p (new_scope))
4718 new_scope = complete_type (new_scope);
4719 /* If it is a typedef to current class, use the current
4720 class instead, as the typedef won't have any names inside
4722 if (!COMPLETE_TYPE_P (new_scope)
4723 && currently_open_class (new_scope))
4724 new_scope = TYPE_MAIN_VARIANT (new_scope);
4726 /* Make sure we look in the right scope the next time through
4728 parser->scope = new_scope;
4731 /* If parsing tentatively, replace the sequence of tokens that makes
4732 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4733 token. That way, should we re-parse the token stream, we will
4734 not have to repeat the effort required to do the parse, nor will
4735 we issue duplicate error messages. */
4736 if (success && start)
4740 token = cp_lexer_token_at (parser->lexer, start);
4741 /* Reset the contents of the START token. */
4742 token->type = CPP_NESTED_NAME_SPECIFIER;
4743 /* Retrieve any deferred checks. Do not pop this access checks yet
4744 so the memory will not be reclaimed during token replacing below. */
4745 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4746 token->u.tree_check_value->value = parser->scope;
4747 token->u.tree_check_value->checks = get_deferred_access_checks ();
4748 token->u.tree_check_value->qualifying_scope =
4749 parser->qualifying_scope;
4750 token->keyword = RID_MAX;
4752 /* Purge all subsequent tokens. */
4753 cp_lexer_purge_tokens_after (parser->lexer, start);
4757 pop_to_parent_deferring_access_checks ();
4759 return success ? parser->scope : NULL_TREE;
4762 /* Parse a nested-name-specifier. See
4763 cp_parser_nested_name_specifier_opt for details. This function
4764 behaves identically, except that it will an issue an error if no
4765 nested-name-specifier is present. */
4768 cp_parser_nested_name_specifier (cp_parser *parser,
4769 bool typename_keyword_p,
4770 bool check_dependency_p,
4772 bool is_declaration)
4776 /* Look for the nested-name-specifier. */
4777 scope = cp_parser_nested_name_specifier_opt (parser,
4782 /* If it was not present, issue an error message. */
4785 cp_parser_error (parser, "expected nested-name-specifier");
4786 parser->scope = NULL_TREE;
4792 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4793 this is either a class-name or a namespace-name (which corresponds
4794 to the class-or-namespace-name production in the grammar). For
4795 C++0x, it can also be a type-name that refers to an enumeration
4798 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4799 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4800 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4801 TYPE_P is TRUE iff the next name should be taken as a class-name,
4802 even the same name is declared to be another entity in the same
4805 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4806 specified by the class-or-namespace-name. If neither is found the
4807 ERROR_MARK_NODE is returned. */
4810 cp_parser_qualifying_entity (cp_parser *parser,
4811 bool typename_keyword_p,
4812 bool template_keyword_p,
4813 bool check_dependency_p,
4815 bool is_declaration)
4818 tree saved_qualifying_scope;
4819 tree saved_object_scope;
4822 bool successful_parse_p;
4824 /* Before we try to parse the class-name, we must save away the
4825 current PARSER->SCOPE since cp_parser_class_name will destroy
4827 saved_scope = parser->scope;
4828 saved_qualifying_scope = parser->qualifying_scope;
4829 saved_object_scope = parser->object_scope;
4830 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4831 there is no need to look for a namespace-name. */
4832 only_class_p = template_keyword_p
4833 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4835 cp_parser_parse_tentatively (parser);
4836 scope = cp_parser_class_name (parser,
4839 type_p ? class_type : none_type,
4841 /*class_head_p=*/false,
4843 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4844 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4846 && cxx_dialect != cxx98
4847 && !successful_parse_p)
4849 /* Restore the saved scope. */
4850 parser->scope = saved_scope;
4851 parser->qualifying_scope = saved_qualifying_scope;
4852 parser->object_scope = saved_object_scope;
4854 /* Parse tentatively. */
4855 cp_parser_parse_tentatively (parser);
4857 /* Parse a typedef-name or enum-name. */
4858 scope = cp_parser_nonclass_name (parser);
4860 /* "If the name found does not designate a namespace or a class,
4861 enumeration, or dependent type, the program is ill-formed."
4863 We cover classes and dependent types above and namespaces below,
4864 so this code is only looking for enums. */
4865 if (!scope || TREE_CODE (scope) != TYPE_DECL
4866 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4867 cp_parser_simulate_error (parser);
4869 successful_parse_p = cp_parser_parse_definitely (parser);
4871 /* If that didn't work, try for a namespace-name. */
4872 if (!only_class_p && !successful_parse_p)
4874 /* Restore the saved scope. */
4875 parser->scope = saved_scope;
4876 parser->qualifying_scope = saved_qualifying_scope;
4877 parser->object_scope = saved_object_scope;
4878 /* If we are not looking at an identifier followed by the scope
4879 resolution operator, then this is not part of a
4880 nested-name-specifier. (Note that this function is only used
4881 to parse the components of a nested-name-specifier.) */
4882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4883 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4884 return error_mark_node;
4885 scope = cp_parser_namespace_name (parser);
4891 /* Parse a postfix-expression.
4895 postfix-expression [ expression ]
4896 postfix-expression ( expression-list [opt] )
4897 simple-type-specifier ( expression-list [opt] )
4898 typename :: [opt] nested-name-specifier identifier
4899 ( expression-list [opt] )
4900 typename :: [opt] nested-name-specifier template [opt] template-id
4901 ( expression-list [opt] )
4902 postfix-expression . template [opt] id-expression
4903 postfix-expression -> template [opt] id-expression
4904 postfix-expression . pseudo-destructor-name
4905 postfix-expression -> pseudo-destructor-name
4906 postfix-expression ++
4907 postfix-expression --
4908 dynamic_cast < type-id > ( expression )
4909 static_cast < type-id > ( expression )
4910 reinterpret_cast < type-id > ( expression )
4911 const_cast < type-id > ( expression )
4912 typeid ( expression )
4918 ( type-id ) { initializer-list , [opt] }
4920 This extension is a GNU version of the C99 compound-literal
4921 construct. (The C99 grammar uses `type-name' instead of `type-id',
4922 but they are essentially the same concept.)
4924 If ADDRESS_P is true, the postfix expression is the operand of the
4925 `&' operator. CAST_P is true if this expression is the target of a
4928 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4929 class member access expressions [expr.ref].
4931 Returns a representation of the expression. */
4934 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4935 bool member_access_only_p,
4936 cp_id_kind * pidk_return)
4940 cp_id_kind idk = CP_ID_KIND_NONE;
4941 tree postfix_expression = NULL_TREE;
4942 bool is_member_access = false;
4944 /* Peek at the next token. */
4945 token = cp_lexer_peek_token (parser->lexer);
4946 /* Some of the productions are determined by keywords. */
4947 keyword = token->keyword;
4957 const char *saved_message;
4959 /* All of these can be handled in the same way from the point
4960 of view of parsing. Begin by consuming the token
4961 identifying the cast. */
4962 cp_lexer_consume_token (parser->lexer);
4964 /* New types cannot be defined in the cast. */
4965 saved_message = parser->type_definition_forbidden_message;
4966 parser->type_definition_forbidden_message
4967 = G_("types may not be defined in casts");
4969 /* Look for the opening `<'. */
4970 cp_parser_require (parser, CPP_LESS, RT_LESS);
4971 /* Parse the type to which we are casting. */
4972 type = cp_parser_type_id (parser);
4973 /* Look for the closing `>'. */
4974 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4975 /* Restore the old message. */
4976 parser->type_definition_forbidden_message = saved_message;
4978 /* And the expression which is being cast. */
4979 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4980 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4981 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4983 /* Only type conversions to integral or enumeration types
4984 can be used in constant-expressions. */
4985 if (!cast_valid_in_integral_constant_expression_p (type)
4986 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4987 return error_mark_node;
4993 = build_dynamic_cast (type, expression, tf_warning_or_error);
4997 = build_static_cast (type, expression, tf_warning_or_error);
5001 = build_reinterpret_cast (type, expression,
5002 tf_warning_or_error);
5006 = build_const_cast (type, expression, tf_warning_or_error);
5017 const char *saved_message;
5018 bool saved_in_type_id_in_expr_p;
5020 /* Consume the `typeid' token. */
5021 cp_lexer_consume_token (parser->lexer);
5022 /* Look for the `(' token. */
5023 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5024 /* Types cannot be defined in a `typeid' expression. */
5025 saved_message = parser->type_definition_forbidden_message;
5026 parser->type_definition_forbidden_message
5027 = G_("types may not be defined in a %<typeid%> expression");
5028 /* We can't be sure yet whether we're looking at a type-id or an
5030 cp_parser_parse_tentatively (parser);
5031 /* Try a type-id first. */
5032 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5033 parser->in_type_id_in_expr_p = true;
5034 type = cp_parser_type_id (parser);
5035 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5036 /* Look for the `)' token. Otherwise, we can't be sure that
5037 we're not looking at an expression: consider `typeid (int
5038 (3))', for example. */
5039 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5040 /* If all went well, simply lookup the type-id. */
5041 if (cp_parser_parse_definitely (parser))
5042 postfix_expression = get_typeid (type);
5043 /* Otherwise, fall back to the expression variant. */
5048 /* Look for an expression. */
5049 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5050 /* Compute its typeid. */
5051 postfix_expression = build_typeid (expression);
5052 /* Look for the `)' token. */
5053 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5055 /* Restore the saved message. */
5056 parser->type_definition_forbidden_message = saved_message;
5057 /* `typeid' may not appear in an integral constant expression. */
5058 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
5059 return error_mark_node;
5066 /* The syntax permitted here is the same permitted for an
5067 elaborated-type-specifier. */
5068 type = cp_parser_elaborated_type_specifier (parser,
5069 /*is_friend=*/false,
5070 /*is_declaration=*/false);
5071 postfix_expression = cp_parser_functional_cast (parser, type);
5079 /* If the next thing is a simple-type-specifier, we may be
5080 looking at a functional cast. We could also be looking at
5081 an id-expression. So, we try the functional cast, and if
5082 that doesn't work we fall back to the primary-expression. */
5083 cp_parser_parse_tentatively (parser);
5084 /* Look for the simple-type-specifier. */
5085 type = cp_parser_simple_type_specifier (parser,
5086 /*decl_specs=*/NULL,
5087 CP_PARSER_FLAGS_NONE);
5088 /* Parse the cast itself. */
5089 if (!cp_parser_error_occurred (parser))
5091 = cp_parser_functional_cast (parser, type);
5092 /* If that worked, we're done. */
5093 if (cp_parser_parse_definitely (parser))
5096 /* If the functional-cast didn't work out, try a
5097 compound-literal. */
5098 if (cp_parser_allow_gnu_extensions_p (parser)
5099 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5101 VEC(constructor_elt,gc) *initializer_list = NULL;
5102 bool saved_in_type_id_in_expr_p;
5104 cp_parser_parse_tentatively (parser);
5105 /* Consume the `('. */
5106 cp_lexer_consume_token (parser->lexer);
5107 /* Parse the type. */
5108 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5109 parser->in_type_id_in_expr_p = true;
5110 type = cp_parser_type_id (parser);
5111 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5112 /* Look for the `)'. */
5113 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5114 /* Look for the `{'. */
5115 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5116 /* If things aren't going well, there's no need to
5118 if (!cp_parser_error_occurred (parser))
5120 bool non_constant_p;
5121 /* Parse the initializer-list. */
5123 = cp_parser_initializer_list (parser, &non_constant_p);
5124 /* Allow a trailing `,'. */
5125 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5126 cp_lexer_consume_token (parser->lexer);
5127 /* Look for the final `}'. */
5128 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5130 /* If that worked, we're definitely looking at a
5131 compound-literal expression. */
5132 if (cp_parser_parse_definitely (parser))
5134 /* Warn the user that a compound literal is not
5135 allowed in standard C++. */
5136 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5137 /* For simplicity, we disallow compound literals in
5138 constant-expressions. We could
5139 allow compound literals of integer type, whose
5140 initializer was a constant, in constant
5141 expressions. Permitting that usage, as a further
5142 extension, would not change the meaning of any
5143 currently accepted programs. (Of course, as
5144 compound literals are not part of ISO C++, the
5145 standard has nothing to say.) */
5146 if (cp_parser_non_integral_constant_expression (parser,
5149 postfix_expression = error_mark_node;
5152 /* Form the representation of the compound-literal. */
5154 = (finish_compound_literal
5155 (type, build_constructor (init_list_type_node,
5156 initializer_list)));
5161 /* It must be a primary-expression. */
5163 = cp_parser_primary_expression (parser, address_p, cast_p,
5164 /*template_arg_p=*/false,
5170 /* Keep looping until the postfix-expression is complete. */
5173 if (idk == CP_ID_KIND_UNQUALIFIED
5174 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5175 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5176 /* It is not a Koenig lookup function call. */
5178 = unqualified_name_lookup_error (postfix_expression);
5180 /* Peek at the next token. */
5181 token = cp_lexer_peek_token (parser->lexer);
5183 switch (token->type)
5185 case CPP_OPEN_SQUARE:
5187 = cp_parser_postfix_open_square_expression (parser,
5190 idk = CP_ID_KIND_NONE;
5191 is_member_access = false;
5194 case CPP_OPEN_PAREN:
5195 /* postfix-expression ( expression-list [opt] ) */
5198 bool is_builtin_constant_p;
5199 bool saved_integral_constant_expression_p = false;
5200 bool saved_non_integral_constant_expression_p = false;
5203 is_member_access = false;
5205 is_builtin_constant_p
5206 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5207 if (is_builtin_constant_p)
5209 /* The whole point of __builtin_constant_p is to allow
5210 non-constant expressions to appear as arguments. */
5211 saved_integral_constant_expression_p
5212 = parser->integral_constant_expression_p;
5213 saved_non_integral_constant_expression_p
5214 = parser->non_integral_constant_expression_p;
5215 parser->integral_constant_expression_p = false;
5217 args = (cp_parser_parenthesized_expression_list
5219 /*cast_p=*/false, /*allow_expansion_p=*/true,
5220 /*non_constant_p=*/NULL));
5221 if (is_builtin_constant_p)
5223 parser->integral_constant_expression_p
5224 = saved_integral_constant_expression_p;
5225 parser->non_integral_constant_expression_p
5226 = saved_non_integral_constant_expression_p;
5231 postfix_expression = error_mark_node;
5235 /* Function calls are not permitted in
5236 constant-expressions. */
5237 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5238 && cp_parser_non_integral_constant_expression (parser,
5241 postfix_expression = error_mark_node;
5242 release_tree_vector (args);
5247 if (idk == CP_ID_KIND_UNQUALIFIED
5248 || idk == CP_ID_KIND_TEMPLATE_ID)
5250 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5252 if (!VEC_empty (tree, args))
5255 if (!any_type_dependent_arguments_p (args))
5257 = perform_koenig_lookup (postfix_expression, args,
5258 /*include_std=*/false);
5262 = unqualified_fn_lookup_error (postfix_expression);
5264 /* We do not perform argument-dependent lookup if
5265 normal lookup finds a non-function, in accordance
5266 with the expected resolution of DR 218. */
5267 else if (!VEC_empty (tree, args)
5268 && is_overloaded_fn (postfix_expression))
5270 tree fn = get_first_fn (postfix_expression);
5271 fn = STRIP_TEMPLATE (fn);
5273 /* Do not do argument dependent lookup if regular
5274 lookup finds a member function or a block-scope
5275 function declaration. [basic.lookup.argdep]/3 */
5276 if (!DECL_FUNCTION_MEMBER_P (fn)
5277 && !DECL_LOCAL_FUNCTION_P (fn))
5280 if (!any_type_dependent_arguments_p (args))
5282 = perform_koenig_lookup (postfix_expression, args,
5283 /*include_std=*/false);
5288 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5290 tree instance = TREE_OPERAND (postfix_expression, 0);
5291 tree fn = TREE_OPERAND (postfix_expression, 1);
5293 if (processing_template_decl
5294 && (type_dependent_expression_p (instance)
5295 || (!BASELINK_P (fn)
5296 && TREE_CODE (fn) != FIELD_DECL)
5297 || type_dependent_expression_p (fn)
5298 || any_type_dependent_arguments_p (args)))
5301 = build_nt_call_vec (postfix_expression, args);
5302 release_tree_vector (args);
5306 if (BASELINK_P (fn))
5309 = (build_new_method_call
5310 (instance, fn, &args, NULL_TREE,
5311 (idk == CP_ID_KIND_QUALIFIED
5312 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5314 tf_warning_or_error));
5318 = finish_call_expr (postfix_expression, &args,
5319 /*disallow_virtual=*/false,
5321 tf_warning_or_error);
5323 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5324 || TREE_CODE (postfix_expression) == MEMBER_REF
5325 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5326 postfix_expression = (build_offset_ref_call_from_tree
5327 (postfix_expression, &args));
5328 else if (idk == CP_ID_KIND_QUALIFIED)
5329 /* A call to a static class member, or a namespace-scope
5332 = finish_call_expr (postfix_expression, &args,
5333 /*disallow_virtual=*/true,
5335 tf_warning_or_error);
5337 /* All other function calls. */
5339 = finish_call_expr (postfix_expression, &args,
5340 /*disallow_virtual=*/false,
5342 tf_warning_or_error);
5344 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5345 idk = CP_ID_KIND_NONE;
5347 release_tree_vector (args);
5353 /* postfix-expression . template [opt] id-expression
5354 postfix-expression . pseudo-destructor-name
5355 postfix-expression -> template [opt] id-expression
5356 postfix-expression -> pseudo-destructor-name */
5358 /* Consume the `.' or `->' operator. */
5359 cp_lexer_consume_token (parser->lexer);
5362 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5367 is_member_access = true;
5371 /* postfix-expression ++ */
5372 /* Consume the `++' token. */
5373 cp_lexer_consume_token (parser->lexer);
5374 /* Generate a representation for the complete expression. */
5376 = finish_increment_expr (postfix_expression,
5377 POSTINCREMENT_EXPR);
5378 /* Increments may not appear in constant-expressions. */
5379 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5380 postfix_expression = error_mark_node;
5381 idk = CP_ID_KIND_NONE;
5382 is_member_access = false;
5385 case CPP_MINUS_MINUS:
5386 /* postfix-expression -- */
5387 /* Consume the `--' token. */
5388 cp_lexer_consume_token (parser->lexer);
5389 /* Generate a representation for the complete expression. */
5391 = finish_increment_expr (postfix_expression,
5392 POSTDECREMENT_EXPR);
5393 /* Decrements may not appear in constant-expressions. */
5394 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5395 postfix_expression = error_mark_node;
5396 idk = CP_ID_KIND_NONE;
5397 is_member_access = false;
5401 if (pidk_return != NULL)
5402 * pidk_return = idk;
5403 if (member_access_only_p)
5404 return is_member_access? postfix_expression : error_mark_node;
5406 return postfix_expression;
5410 /* We should never get here. */
5412 return error_mark_node;
5415 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5416 by cp_parser_builtin_offsetof. We're looking for
5418 postfix-expression [ expression ]
5420 FOR_OFFSETOF is set if we're being called in that context, which
5421 changes how we deal with integer constant expressions. */
5424 cp_parser_postfix_open_square_expression (cp_parser *parser,
5425 tree postfix_expression,
5430 /* Consume the `[' token. */
5431 cp_lexer_consume_token (parser->lexer);
5433 /* Parse the index expression. */
5434 /* ??? For offsetof, there is a question of what to allow here. If
5435 offsetof is not being used in an integral constant expression context,
5436 then we *could* get the right answer by computing the value at runtime.
5437 If we are in an integral constant expression context, then we might
5438 could accept any constant expression; hard to say without analysis.
5439 Rather than open the barn door too wide right away, allow only integer
5440 constant expressions here. */
5442 index = cp_parser_constant_expression (parser, false, NULL);
5444 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5446 /* Look for the closing `]'. */
5447 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5449 /* Build the ARRAY_REF. */
5450 postfix_expression = grok_array_decl (postfix_expression, index);
5452 /* When not doing offsetof, array references are not permitted in
5453 constant-expressions. */
5455 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5456 postfix_expression = error_mark_node;
5458 return postfix_expression;
5461 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5462 by cp_parser_builtin_offsetof. We're looking for
5464 postfix-expression . template [opt] id-expression
5465 postfix-expression . pseudo-destructor-name
5466 postfix-expression -> template [opt] id-expression
5467 postfix-expression -> pseudo-destructor-name
5469 FOR_OFFSETOF is set if we're being called in that context. That sorta
5470 limits what of the above we'll actually accept, but nevermind.
5471 TOKEN_TYPE is the "." or "->" token, which will already have been
5472 removed from the stream. */
5475 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5476 enum cpp_ttype token_type,
5477 tree postfix_expression,
5478 bool for_offsetof, cp_id_kind *idk,
5479 location_t location)
5483 bool pseudo_destructor_p;
5484 tree scope = NULL_TREE;
5486 /* If this is a `->' operator, dereference the pointer. */
5487 if (token_type == CPP_DEREF)
5488 postfix_expression = build_x_arrow (postfix_expression);
5489 /* Check to see whether or not the expression is type-dependent. */
5490 dependent_p = type_dependent_expression_p (postfix_expression);
5491 /* The identifier following the `->' or `.' is not qualified. */
5492 parser->scope = NULL_TREE;
5493 parser->qualifying_scope = NULL_TREE;
5494 parser->object_scope = NULL_TREE;
5495 *idk = CP_ID_KIND_NONE;
5497 /* Enter the scope corresponding to the type of the object
5498 given by the POSTFIX_EXPRESSION. */
5499 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5501 scope = TREE_TYPE (postfix_expression);
5502 /* According to the standard, no expression should ever have
5503 reference type. Unfortunately, we do not currently match
5504 the standard in this respect in that our internal representation
5505 of an expression may have reference type even when the standard
5506 says it does not. Therefore, we have to manually obtain the
5507 underlying type here. */
5508 scope = non_reference (scope);
5509 /* The type of the POSTFIX_EXPRESSION must be complete. */
5510 if (scope == unknown_type_node)
5512 error_at (location, "%qE does not have class type",
5513 postfix_expression);
5517 scope = complete_type_or_else (scope, NULL_TREE);
5518 /* Let the name lookup machinery know that we are processing a
5519 class member access expression. */
5520 parser->context->object_type = scope;
5521 /* If something went wrong, we want to be able to discern that case,
5522 as opposed to the case where there was no SCOPE due to the type
5523 of expression being dependent. */
5525 scope = error_mark_node;
5526 /* If the SCOPE was erroneous, make the various semantic analysis
5527 functions exit quickly -- and without issuing additional error
5529 if (scope == error_mark_node)
5530 postfix_expression = error_mark_node;
5533 /* Assume this expression is not a pseudo-destructor access. */
5534 pseudo_destructor_p = false;
5536 /* If the SCOPE is a scalar type, then, if this is a valid program,
5537 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5538 is type dependent, it can be pseudo-destructor-name or something else.
5539 Try to parse it as pseudo-destructor-name first. */
5540 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5545 cp_parser_parse_tentatively (parser);
5546 /* Parse the pseudo-destructor-name. */
5548 cp_parser_pseudo_destructor_name (parser, &s, &type);
5550 && (cp_parser_error_occurred (parser)
5551 || TREE_CODE (type) != TYPE_DECL
5552 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5553 cp_parser_abort_tentative_parse (parser);
5554 else if (cp_parser_parse_definitely (parser))
5556 pseudo_destructor_p = true;
5558 = finish_pseudo_destructor_expr (postfix_expression,
5559 s, TREE_TYPE (type));
5563 if (!pseudo_destructor_p)
5565 /* If the SCOPE is not a scalar type, we are looking at an
5566 ordinary class member access expression, rather than a
5567 pseudo-destructor-name. */
5569 cp_token *token = cp_lexer_peek_token (parser->lexer);
5570 /* Parse the id-expression. */
5571 name = (cp_parser_id_expression
5573 cp_parser_optional_template_keyword (parser),
5574 /*check_dependency_p=*/true,
5576 /*declarator_p=*/false,
5577 /*optional_p=*/false));
5578 /* In general, build a SCOPE_REF if the member name is qualified.
5579 However, if the name was not dependent and has already been
5580 resolved; there is no need to build the SCOPE_REF. For example;
5582 struct X { void f(); };
5583 template <typename T> void f(T* t) { t->X::f(); }
5585 Even though "t" is dependent, "X::f" is not and has been resolved
5586 to a BASELINK; there is no need to include scope information. */
5588 /* But we do need to remember that there was an explicit scope for
5589 virtual function calls. */
5591 *idk = CP_ID_KIND_QUALIFIED;
5593 /* If the name is a template-id that names a type, we will get a
5594 TYPE_DECL here. That is invalid code. */
5595 if (TREE_CODE (name) == TYPE_DECL)
5597 error_at (token->location, "invalid use of %qD", name);
5598 postfix_expression = error_mark_node;
5602 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5604 name = build_qualified_name (/*type=*/NULL_TREE,
5608 parser->scope = NULL_TREE;
5609 parser->qualifying_scope = NULL_TREE;
5610 parser->object_scope = NULL_TREE;
5612 if (scope && name && BASELINK_P (name))
5613 adjust_result_of_qualified_name_lookup
5614 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5616 = finish_class_member_access_expr (postfix_expression, name,
5618 tf_warning_or_error);
5622 /* We no longer need to look up names in the scope of the object on
5623 the left-hand side of the `.' or `->' operator. */
5624 parser->context->object_type = NULL_TREE;
5626 /* Outside of offsetof, these operators may not appear in
5627 constant-expressions. */
5629 && (cp_parser_non_integral_constant_expression
5630 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5631 postfix_expression = error_mark_node;
5633 return postfix_expression;
5636 /* Parse a parenthesized expression-list.
5639 assignment-expression
5640 expression-list, assignment-expression
5645 identifier, expression-list
5647 CAST_P is true if this expression is the target of a cast.
5649 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5652 Returns a vector of trees. Each element is a representation of an
5653 assignment-expression. NULL is returned if the ( and or ) are
5654 missing. An empty, but allocated, vector is returned on no
5655 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5656 if we are parsing an attribute list for an attribute that wants a
5657 plain identifier argument, normal_attr for an attribute that wants
5658 an expression, or non_attr if we aren't parsing an attribute list. If
5659 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5660 not all of the expressions in the list were constant. */
5662 static VEC(tree,gc) *
5663 cp_parser_parenthesized_expression_list (cp_parser* parser,
5664 int is_attribute_list,
5666 bool allow_expansion_p,
5667 bool *non_constant_p)
5669 VEC(tree,gc) *expression_list;
5670 bool fold_expr_p = is_attribute_list != non_attr;
5671 tree identifier = NULL_TREE;
5672 bool saved_greater_than_is_operator_p;
5674 /* Assume all the expressions will be constant. */
5676 *non_constant_p = false;
5678 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5681 expression_list = make_tree_vector ();
5683 /* Within a parenthesized expression, a `>' token is always
5684 the greater-than operator. */
5685 saved_greater_than_is_operator_p
5686 = parser->greater_than_is_operator_p;
5687 parser->greater_than_is_operator_p = true;
5689 /* Consume expressions until there are no more. */
5690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5695 /* At the beginning of attribute lists, check to see if the
5696 next token is an identifier. */
5697 if (is_attribute_list == id_attr
5698 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5702 /* Consume the identifier. */
5703 token = cp_lexer_consume_token (parser->lexer);
5704 /* Save the identifier. */
5705 identifier = token->u.value;
5709 bool expr_non_constant_p;
5711 /* Parse the next assignment-expression. */
5712 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5714 /* A braced-init-list. */
5715 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5716 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5717 if (non_constant_p && expr_non_constant_p)
5718 *non_constant_p = true;
5720 else if (non_constant_p)
5722 expr = (cp_parser_constant_expression
5723 (parser, /*allow_non_constant_p=*/true,
5724 &expr_non_constant_p));
5725 if (expr_non_constant_p)
5726 *non_constant_p = true;
5729 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5732 expr = fold_non_dependent_expr (expr);
5734 /* If we have an ellipsis, then this is an expression
5736 if (allow_expansion_p
5737 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5739 /* Consume the `...'. */
5740 cp_lexer_consume_token (parser->lexer);
5742 /* Build the argument pack. */
5743 expr = make_pack_expansion (expr);
5746 /* Add it to the list. We add error_mark_node
5747 expressions to the list, so that we can still tell if
5748 the correct form for a parenthesized expression-list
5749 is found. That gives better errors. */
5750 VEC_safe_push (tree, gc, expression_list, expr);
5752 if (expr == error_mark_node)
5756 /* After the first item, attribute lists look the same as
5757 expression lists. */
5758 is_attribute_list = non_attr;
5761 /* If the next token isn't a `,', then we are done. */
5762 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5765 /* Otherwise, consume the `,' and keep going. */
5766 cp_lexer_consume_token (parser->lexer);
5769 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5774 /* We try and resync to an unnested comma, as that will give the
5775 user better diagnostics. */
5776 ending = cp_parser_skip_to_closing_parenthesis (parser,
5777 /*recovering=*/true,
5779 /*consume_paren=*/true);
5784 parser->greater_than_is_operator_p
5785 = saved_greater_than_is_operator_p;
5790 parser->greater_than_is_operator_p
5791 = saved_greater_than_is_operator_p;
5794 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5796 return expression_list;
5799 /* Parse a pseudo-destructor-name.
5801 pseudo-destructor-name:
5802 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5803 :: [opt] nested-name-specifier template template-id :: ~ type-name
5804 :: [opt] nested-name-specifier [opt] ~ type-name
5806 If either of the first two productions is used, sets *SCOPE to the
5807 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5808 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5809 or ERROR_MARK_NODE if the parse fails. */
5812 cp_parser_pseudo_destructor_name (cp_parser* parser,
5816 bool nested_name_specifier_p;
5818 /* Assume that things will not work out. */
5819 *type = error_mark_node;
5821 /* Look for the optional `::' operator. */
5822 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5823 /* Look for the optional nested-name-specifier. */
5824 nested_name_specifier_p
5825 = (cp_parser_nested_name_specifier_opt (parser,
5826 /*typename_keyword_p=*/false,
5827 /*check_dependency_p=*/true,
5829 /*is_declaration=*/false)
5831 /* Now, if we saw a nested-name-specifier, we might be doing the
5832 second production. */
5833 if (nested_name_specifier_p
5834 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5836 /* Consume the `template' keyword. */
5837 cp_lexer_consume_token (parser->lexer);
5838 /* Parse the template-id. */
5839 cp_parser_template_id (parser,
5840 /*template_keyword_p=*/true,
5841 /*check_dependency_p=*/false,
5842 /*is_declaration=*/true);
5843 /* Look for the `::' token. */
5844 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5846 /* If the next token is not a `~', then there might be some
5847 additional qualification. */
5848 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5850 /* At this point, we're looking for "type-name :: ~". The type-name
5851 must not be a class-name, since this is a pseudo-destructor. So,
5852 it must be either an enum-name, or a typedef-name -- both of which
5853 are just identifiers. So, we peek ahead to check that the "::"
5854 and "~" tokens are present; if they are not, then we can avoid
5855 calling type_name. */
5856 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5857 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5858 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5860 cp_parser_error (parser, "non-scalar type");
5864 /* Look for the type-name. */
5865 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5866 if (*scope == error_mark_node)
5869 /* Look for the `::' token. */
5870 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5875 /* Look for the `~'. */
5876 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5877 /* Look for the type-name again. We are not responsible for
5878 checking that it matches the first type-name. */
5879 *type = cp_parser_nonclass_name (parser);
5882 /* Parse a unary-expression.
5888 unary-operator cast-expression
5889 sizeof unary-expression
5897 __extension__ cast-expression
5898 __alignof__ unary-expression
5899 __alignof__ ( type-id )
5900 __real__ cast-expression
5901 __imag__ cast-expression
5904 ADDRESS_P is true iff the unary-expression is appearing as the
5905 operand of the `&' operator. CAST_P is true if this expression is
5906 the target of a cast.
5908 Returns a representation of the expression. */
5911 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5915 enum tree_code unary_operator;
5917 /* Peek at the next token. */
5918 token = cp_lexer_peek_token (parser->lexer);
5919 /* Some keywords give away the kind of expression. */
5920 if (token->type == CPP_KEYWORD)
5922 enum rid keyword = token->keyword;
5932 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5933 /* Consume the token. */
5934 cp_lexer_consume_token (parser->lexer);
5935 /* Parse the operand. */
5936 operand = cp_parser_sizeof_operand (parser, keyword);
5938 if (TYPE_P (operand))
5939 return cxx_sizeof_or_alignof_type (operand, op, true);
5941 return cxx_sizeof_or_alignof_expr (operand, op, true);
5945 return cp_parser_new_expression (parser);
5948 return cp_parser_delete_expression (parser);
5952 /* The saved value of the PEDANTIC flag. */
5956 /* Save away the PEDANTIC flag. */
5957 cp_parser_extension_opt (parser, &saved_pedantic);
5958 /* Parse the cast-expression. */
5959 expr = cp_parser_simple_cast_expression (parser);
5960 /* Restore the PEDANTIC flag. */
5961 pedantic = saved_pedantic;
5971 /* Consume the `__real__' or `__imag__' token. */
5972 cp_lexer_consume_token (parser->lexer);
5973 /* Parse the cast-expression. */
5974 expression = cp_parser_simple_cast_expression (parser);
5975 /* Create the complete representation. */
5976 return build_x_unary_op ((keyword == RID_REALPART
5977 ? REALPART_EXPR : IMAGPART_EXPR),
5979 tf_warning_or_error);
5986 const char *saved_message;
5987 bool saved_integral_constant_expression_p;
5988 bool saved_non_integral_constant_expression_p;
5989 bool saved_greater_than_is_operator_p;
5991 cp_lexer_consume_token (parser->lexer);
5992 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5994 saved_message = parser->type_definition_forbidden_message;
5995 parser->type_definition_forbidden_message
5996 = G_("types may not be defined in %<noexcept%> expressions");
5998 saved_integral_constant_expression_p
5999 = parser->integral_constant_expression_p;
6000 saved_non_integral_constant_expression_p
6001 = parser->non_integral_constant_expression_p;
6002 parser->integral_constant_expression_p = false;
6004 saved_greater_than_is_operator_p
6005 = parser->greater_than_is_operator_p;
6006 parser->greater_than_is_operator_p = true;
6008 ++cp_unevaluated_operand;
6009 ++c_inhibit_evaluation_warnings;
6010 expr = cp_parser_expression (parser, false, NULL);
6011 --c_inhibit_evaluation_warnings;
6012 --cp_unevaluated_operand;
6014 parser->greater_than_is_operator_p
6015 = saved_greater_than_is_operator_p;
6017 parser->integral_constant_expression_p
6018 = saved_integral_constant_expression_p;
6019 parser->non_integral_constant_expression_p
6020 = saved_non_integral_constant_expression_p;
6022 parser->type_definition_forbidden_message = saved_message;
6024 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6025 return finish_noexcept_expr (expr, tf_warning_or_error);
6033 /* Look for the `:: new' and `:: delete', which also signal the
6034 beginning of a new-expression, or delete-expression,
6035 respectively. If the next token is `::', then it might be one of
6037 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6041 /* See if the token after the `::' is one of the keywords in
6042 which we're interested. */
6043 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6044 /* If it's `new', we have a new-expression. */
6045 if (keyword == RID_NEW)
6046 return cp_parser_new_expression (parser);
6047 /* Similarly, for `delete'. */
6048 else if (keyword == RID_DELETE)
6049 return cp_parser_delete_expression (parser);
6052 /* Look for a unary operator. */
6053 unary_operator = cp_parser_unary_operator (token);
6054 /* The `++' and `--' operators can be handled similarly, even though
6055 they are not technically unary-operators in the grammar. */
6056 if (unary_operator == ERROR_MARK)
6058 if (token->type == CPP_PLUS_PLUS)
6059 unary_operator = PREINCREMENT_EXPR;
6060 else if (token->type == CPP_MINUS_MINUS)
6061 unary_operator = PREDECREMENT_EXPR;
6062 /* Handle the GNU address-of-label extension. */
6063 else if (cp_parser_allow_gnu_extensions_p (parser)
6064 && token->type == CPP_AND_AND)
6068 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6070 /* Consume the '&&' token. */
6071 cp_lexer_consume_token (parser->lexer);
6072 /* Look for the identifier. */
6073 identifier = cp_parser_identifier (parser);
6074 /* Create an expression representing the address. */
6075 expression = finish_label_address_expr (identifier, loc);
6076 if (cp_parser_non_integral_constant_expression (parser,
6078 expression = error_mark_node;
6082 if (unary_operator != ERROR_MARK)
6084 tree cast_expression;
6085 tree expression = error_mark_node;
6086 non_integral_constant non_constant_p = NIC_NONE;
6088 /* Consume the operator token. */
6089 token = cp_lexer_consume_token (parser->lexer);
6090 /* Parse the cast-expression. */
6092 = cp_parser_cast_expression (parser,
6093 unary_operator == ADDR_EXPR,
6094 /*cast_p=*/false, pidk);
6095 /* Now, build an appropriate representation. */
6096 switch (unary_operator)
6099 non_constant_p = NIC_STAR;
6100 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6101 tf_warning_or_error);
6105 non_constant_p = NIC_ADDR;
6108 expression = build_x_unary_op (unary_operator, cast_expression,
6109 tf_warning_or_error);
6112 case PREINCREMENT_EXPR:
6113 case PREDECREMENT_EXPR:
6114 non_constant_p = unary_operator == PREINCREMENT_EXPR
6115 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6117 case UNARY_PLUS_EXPR:
6119 case TRUTH_NOT_EXPR:
6120 expression = finish_unary_op_expr (unary_operator, cast_expression);
6127 if (non_constant_p != NIC_NONE
6128 && cp_parser_non_integral_constant_expression (parser,
6130 expression = error_mark_node;
6135 return cp_parser_postfix_expression (parser, address_p, cast_p,
6136 /*member_access_only_p=*/false,
6140 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6141 unary-operator, the corresponding tree code is returned. */
6143 static enum tree_code
6144 cp_parser_unary_operator (cp_token* token)
6146 switch (token->type)
6149 return INDIRECT_REF;
6155 return UNARY_PLUS_EXPR;
6161 return TRUTH_NOT_EXPR;
6164 return BIT_NOT_EXPR;
6171 /* Parse a new-expression.
6174 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6175 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6177 Returns a representation of the expression. */
6180 cp_parser_new_expression (cp_parser* parser)
6182 bool global_scope_p;
6183 VEC(tree,gc) *placement;
6185 VEC(tree,gc) *initializer;
6189 /* Look for the optional `::' operator. */
6191 = (cp_parser_global_scope_opt (parser,
6192 /*current_scope_valid_p=*/false)
6194 /* Look for the `new' operator. */
6195 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6196 /* There's no easy way to tell a new-placement from the
6197 `( type-id )' construct. */
6198 cp_parser_parse_tentatively (parser);
6199 /* Look for a new-placement. */
6200 placement = cp_parser_new_placement (parser);
6201 /* If that didn't work out, there's no new-placement. */
6202 if (!cp_parser_parse_definitely (parser))
6204 if (placement != NULL)
6205 release_tree_vector (placement);
6209 /* If the next token is a `(', then we have a parenthesized
6211 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6214 /* Consume the `('. */
6215 cp_lexer_consume_token (parser->lexer);
6216 /* Parse the type-id. */
6217 type = cp_parser_type_id (parser);
6218 /* Look for the closing `)'. */
6219 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6220 token = cp_lexer_peek_token (parser->lexer);
6221 /* There should not be a direct-new-declarator in this production,
6222 but GCC used to allowed this, so we check and emit a sensible error
6223 message for this case. */
6224 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6226 error_at (token->location,
6227 "array bound forbidden after parenthesized type-id");
6228 inform (token->location,
6229 "try removing the parentheses around the type-id");
6230 cp_parser_direct_new_declarator (parser);
6234 /* Otherwise, there must be a new-type-id. */
6236 type = cp_parser_new_type_id (parser, &nelts);
6238 /* If the next token is a `(' or '{', then we have a new-initializer. */
6239 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6240 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6241 initializer = cp_parser_new_initializer (parser);
6245 /* A new-expression may not appear in an integral constant
6247 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6248 ret = error_mark_node;
6251 /* Create a representation of the new-expression. */
6252 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6253 tf_warning_or_error);
6256 if (placement != NULL)
6257 release_tree_vector (placement);
6258 if (initializer != NULL)
6259 release_tree_vector (initializer);
6264 /* Parse a new-placement.
6269 Returns the same representation as for an expression-list. */
6271 static VEC(tree,gc) *
6272 cp_parser_new_placement (cp_parser* parser)
6274 VEC(tree,gc) *expression_list;
6276 /* Parse the expression-list. */
6277 expression_list = (cp_parser_parenthesized_expression_list
6278 (parser, non_attr, /*cast_p=*/false,
6279 /*allow_expansion_p=*/true,
6280 /*non_constant_p=*/NULL));
6282 return expression_list;
6285 /* Parse a new-type-id.
6288 type-specifier-seq new-declarator [opt]
6290 Returns the TYPE allocated. If the new-type-id indicates an array
6291 type, *NELTS is set to the number of elements in the last array
6292 bound; the TYPE will not include the last array bound. */
6295 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6297 cp_decl_specifier_seq type_specifier_seq;
6298 cp_declarator *new_declarator;
6299 cp_declarator *declarator;
6300 cp_declarator *outer_declarator;
6301 const char *saved_message;
6304 /* The type-specifier sequence must not contain type definitions.
6305 (It cannot contain declarations of new types either, but if they
6306 are not definitions we will catch that because they are not
6308 saved_message = parser->type_definition_forbidden_message;
6309 parser->type_definition_forbidden_message
6310 = G_("types may not be defined in a new-type-id");
6311 /* Parse the type-specifier-seq. */
6312 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6313 /*is_trailing_return=*/false,
6314 &type_specifier_seq);
6315 /* Restore the old message. */
6316 parser->type_definition_forbidden_message = saved_message;
6317 /* Parse the new-declarator. */
6318 new_declarator = cp_parser_new_declarator_opt (parser);
6320 /* Determine the number of elements in the last array dimension, if
6323 /* Skip down to the last array dimension. */
6324 declarator = new_declarator;
6325 outer_declarator = NULL;
6326 while (declarator && (declarator->kind == cdk_pointer
6327 || declarator->kind == cdk_ptrmem))
6329 outer_declarator = declarator;
6330 declarator = declarator->declarator;
6333 && declarator->kind == cdk_array
6334 && declarator->declarator
6335 && declarator->declarator->kind == cdk_array)
6337 outer_declarator = declarator;
6338 declarator = declarator->declarator;
6341 if (declarator && declarator->kind == cdk_array)
6343 *nelts = declarator->u.array.bounds;
6344 if (*nelts == error_mark_node)
6345 *nelts = integer_one_node;
6347 if (outer_declarator)
6348 outer_declarator->declarator = declarator->declarator;
6350 new_declarator = NULL;
6353 type = groktypename (&type_specifier_seq, new_declarator, false);
6357 /* Parse an (optional) new-declarator.
6360 ptr-operator new-declarator [opt]
6361 direct-new-declarator
6363 Returns the declarator. */
6365 static cp_declarator *
6366 cp_parser_new_declarator_opt (cp_parser* parser)
6368 enum tree_code code;
6370 cp_cv_quals cv_quals;
6372 /* We don't know if there's a ptr-operator next, or not. */
6373 cp_parser_parse_tentatively (parser);
6374 /* Look for a ptr-operator. */
6375 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6376 /* If that worked, look for more new-declarators. */
6377 if (cp_parser_parse_definitely (parser))
6379 cp_declarator *declarator;
6381 /* Parse another optional declarator. */
6382 declarator = cp_parser_new_declarator_opt (parser);
6384 return cp_parser_make_indirect_declarator
6385 (code, type, cv_quals, declarator);
6388 /* If the next token is a `[', there is a direct-new-declarator. */
6389 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6390 return cp_parser_direct_new_declarator (parser);
6395 /* Parse a direct-new-declarator.
6397 direct-new-declarator:
6399 direct-new-declarator [constant-expression]
6403 static cp_declarator *
6404 cp_parser_direct_new_declarator (cp_parser* parser)
6406 cp_declarator *declarator = NULL;
6412 /* Look for the opening `['. */
6413 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6414 /* The first expression is not required to be constant. */
6417 cp_token *token = cp_lexer_peek_token (parser->lexer);
6418 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6419 /* The standard requires that the expression have integral
6420 type. DR 74 adds enumeration types. We believe that the
6421 real intent is that these expressions be handled like the
6422 expression in a `switch' condition, which also allows
6423 classes with a single conversion to integral or
6424 enumeration type. */
6425 if (!processing_template_decl)
6428 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6433 error_at (token->location,
6434 "expression in new-declarator must have integral "
6435 "or enumeration type");
6436 expression = error_mark_node;
6440 /* But all the other expressions must be. */
6443 = cp_parser_constant_expression (parser,
6444 /*allow_non_constant=*/false,
6446 /* Look for the closing `]'. */
6447 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6449 /* Add this bound to the declarator. */
6450 declarator = make_array_declarator (declarator, expression);
6452 /* If the next token is not a `[', then there are no more
6454 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6461 /* Parse a new-initializer.
6464 ( expression-list [opt] )
6467 Returns a representation of the expression-list. */
6469 static VEC(tree,gc) *
6470 cp_parser_new_initializer (cp_parser* parser)
6472 VEC(tree,gc) *expression_list;
6474 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6477 bool expr_non_constant_p;
6478 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6479 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6480 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6481 expression_list = make_tree_vector_single (t);
6484 expression_list = (cp_parser_parenthesized_expression_list
6485 (parser, non_attr, /*cast_p=*/false,
6486 /*allow_expansion_p=*/true,
6487 /*non_constant_p=*/NULL));
6489 return expression_list;
6492 /* Parse a delete-expression.
6495 :: [opt] delete cast-expression
6496 :: [opt] delete [ ] cast-expression
6498 Returns a representation of the expression. */
6501 cp_parser_delete_expression (cp_parser* parser)
6503 bool global_scope_p;
6507 /* Look for the optional `::' operator. */
6509 = (cp_parser_global_scope_opt (parser,
6510 /*current_scope_valid_p=*/false)
6512 /* Look for the `delete' keyword. */
6513 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6514 /* See if the array syntax is in use. */
6515 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6517 /* Consume the `[' token. */
6518 cp_lexer_consume_token (parser->lexer);
6519 /* Look for the `]' token. */
6520 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6521 /* Remember that this is the `[]' construct. */
6527 /* Parse the cast-expression. */
6528 expression = cp_parser_simple_cast_expression (parser);
6530 /* A delete-expression may not appear in an integral constant
6532 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6533 return error_mark_node;
6535 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6538 /* Returns true if TOKEN may start a cast-expression and false
6542 cp_parser_token_starts_cast_expression (cp_token *token)
6544 switch (token->type)
6550 case CPP_CLOSE_SQUARE:
6551 case CPP_CLOSE_PAREN:
6552 case CPP_CLOSE_BRACE:
6556 case CPP_DEREF_STAR:
6564 case CPP_GREATER_EQ:
6584 /* '[' may start a primary-expression in obj-c++. */
6585 case CPP_OPEN_SQUARE:
6586 return c_dialect_objc ();
6593 /* Parse a cast-expression.
6597 ( type-id ) cast-expression
6599 ADDRESS_P is true iff the unary-expression is appearing as the
6600 operand of the `&' operator. CAST_P is true if this expression is
6601 the target of a cast.
6603 Returns a representation of the expression. */
6606 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6609 /* If it's a `(', then we might be looking at a cast. */
6610 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6612 tree type = NULL_TREE;
6613 tree expr = NULL_TREE;
6614 bool compound_literal_p;
6615 const char *saved_message;
6617 /* There's no way to know yet whether or not this is a cast.
6618 For example, `(int (3))' is a unary-expression, while `(int)
6619 3' is a cast. So, we resort to parsing tentatively. */
6620 cp_parser_parse_tentatively (parser);
6621 /* Types may not be defined in a cast. */
6622 saved_message = parser->type_definition_forbidden_message;
6623 parser->type_definition_forbidden_message
6624 = G_("types may not be defined in casts");
6625 /* Consume the `('. */
6626 cp_lexer_consume_token (parser->lexer);
6627 /* A very tricky bit is that `(struct S) { 3 }' is a
6628 compound-literal (which we permit in C++ as an extension).
6629 But, that construct is not a cast-expression -- it is a
6630 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6631 is legal; if the compound-literal were a cast-expression,
6632 you'd need an extra set of parentheses.) But, if we parse
6633 the type-id, and it happens to be a class-specifier, then we
6634 will commit to the parse at that point, because we cannot
6635 undo the action that is done when creating a new class. So,
6636 then we cannot back up and do a postfix-expression.
6638 Therefore, we scan ahead to the closing `)', and check to see
6639 if the token after the `)' is a `{'. If so, we are not
6640 looking at a cast-expression.
6642 Save tokens so that we can put them back. */
6643 cp_lexer_save_tokens (parser->lexer);
6644 /* Skip tokens until the next token is a closing parenthesis.
6645 If we find the closing `)', and the next token is a `{', then
6646 we are looking at a compound-literal. */
6648 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6649 /*consume_paren=*/true)
6650 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6651 /* Roll back the tokens we skipped. */
6652 cp_lexer_rollback_tokens (parser->lexer);
6653 /* If we were looking at a compound-literal, simulate an error
6654 so that the call to cp_parser_parse_definitely below will
6656 if (compound_literal_p)
6657 cp_parser_simulate_error (parser);
6660 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6661 parser->in_type_id_in_expr_p = true;
6662 /* Look for the type-id. */
6663 type = cp_parser_type_id (parser);
6664 /* Look for the closing `)'. */
6665 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6666 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6669 /* Restore the saved message. */
6670 parser->type_definition_forbidden_message = saved_message;
6672 /* At this point this can only be either a cast or a
6673 parenthesized ctor such as `(T ())' that looks like a cast to
6674 function returning T. */
6675 if (!cp_parser_error_occurred (parser)
6676 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6679 cp_parser_parse_definitely (parser);
6680 expr = cp_parser_cast_expression (parser,
6681 /*address_p=*/false,
6682 /*cast_p=*/true, pidk);
6684 /* Warn about old-style casts, if so requested. */
6685 if (warn_old_style_cast
6686 && !in_system_header
6687 && !VOID_TYPE_P (type)
6688 && current_lang_name != lang_name_c)
6689 warning (OPT_Wold_style_cast, "use of old-style cast");
6691 /* Only type conversions to integral or enumeration types
6692 can be used in constant-expressions. */
6693 if (!cast_valid_in_integral_constant_expression_p (type)
6694 && cp_parser_non_integral_constant_expression (parser,
6696 return error_mark_node;
6698 /* Perform the cast. */
6699 expr = build_c_cast (input_location, type, expr);
6703 cp_parser_abort_tentative_parse (parser);
6706 /* If we get here, then it's not a cast, so it must be a
6707 unary-expression. */
6708 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6711 /* Parse a binary expression of the general form:
6715 pm-expression .* cast-expression
6716 pm-expression ->* cast-expression
6718 multiplicative-expression:
6720 multiplicative-expression * pm-expression
6721 multiplicative-expression / pm-expression
6722 multiplicative-expression % pm-expression
6724 additive-expression:
6725 multiplicative-expression
6726 additive-expression + multiplicative-expression
6727 additive-expression - multiplicative-expression
6731 shift-expression << additive-expression
6732 shift-expression >> additive-expression
6734 relational-expression:
6736 relational-expression < shift-expression
6737 relational-expression > shift-expression
6738 relational-expression <= shift-expression
6739 relational-expression >= shift-expression
6743 relational-expression:
6744 relational-expression <? shift-expression
6745 relational-expression >? shift-expression
6747 equality-expression:
6748 relational-expression
6749 equality-expression == relational-expression
6750 equality-expression != relational-expression
6754 and-expression & equality-expression
6756 exclusive-or-expression:
6758 exclusive-or-expression ^ and-expression
6760 inclusive-or-expression:
6761 exclusive-or-expression
6762 inclusive-or-expression | exclusive-or-expression
6764 logical-and-expression:
6765 inclusive-or-expression
6766 logical-and-expression && inclusive-or-expression
6768 logical-or-expression:
6769 logical-and-expression
6770 logical-or-expression || logical-and-expression
6772 All these are implemented with a single function like:
6775 simple-cast-expression
6776 binary-expression <token> binary-expression
6778 CAST_P is true if this expression is the target of a cast.
6780 The binops_by_token map is used to get the tree codes for each <token> type.
6781 binary-expressions are associated according to a precedence table. */
6783 #define TOKEN_PRECEDENCE(token) \
6784 (((token->type == CPP_GREATER \
6785 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6786 && !parser->greater_than_is_operator_p) \
6787 ? PREC_NOT_OPERATOR \
6788 : binops_by_token[token->type].prec)
6791 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6792 bool no_toplevel_fold_p,
6793 enum cp_parser_prec prec,
6796 cp_parser_expression_stack stack;
6797 cp_parser_expression_stack_entry *sp = &stack[0];
6800 enum tree_code tree_type, lhs_type, rhs_type;
6801 enum cp_parser_prec new_prec, lookahead_prec;
6804 /* Parse the first expression. */
6805 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6806 lhs_type = ERROR_MARK;
6810 /* Get an operator token. */
6811 token = cp_lexer_peek_token (parser->lexer);
6813 if (warn_cxx0x_compat
6814 && token->type == CPP_RSHIFT
6815 && !parser->greater_than_is_operator_p)
6817 if (warning_at (token->location, OPT_Wc__0x_compat,
6818 "%<>>%> operator will be treated as"
6819 " two right angle brackets in C++0x"))
6820 inform (token->location,
6821 "suggest parentheses around %<>>%> expression");
6824 new_prec = TOKEN_PRECEDENCE (token);
6826 /* Popping an entry off the stack means we completed a subexpression:
6827 - either we found a token which is not an operator (`>' where it is not
6828 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6829 will happen repeatedly;
6830 - or, we found an operator which has lower priority. This is the case
6831 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6833 if (new_prec <= prec)
6842 tree_type = binops_by_token[token->type].tree_type;
6844 /* We used the operator token. */
6845 cp_lexer_consume_token (parser->lexer);
6847 /* For "false && x" or "true || x", x will never be executed;
6848 disable warnings while evaluating it. */
6849 if (tree_type == TRUTH_ANDIF_EXPR)
6850 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6851 else if (tree_type == TRUTH_ORIF_EXPR)
6852 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6854 /* Extract another operand. It may be the RHS of this expression
6855 or the LHS of a new, higher priority expression. */
6856 rhs = cp_parser_simple_cast_expression (parser);
6857 rhs_type = ERROR_MARK;
6859 /* Get another operator token. Look up its precedence to avoid
6860 building a useless (immediately popped) stack entry for common
6861 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6862 token = cp_lexer_peek_token (parser->lexer);
6863 lookahead_prec = TOKEN_PRECEDENCE (token);
6864 if (lookahead_prec > new_prec)
6866 /* ... and prepare to parse the RHS of the new, higher priority
6867 expression. Since precedence levels on the stack are
6868 monotonically increasing, we do not have to care about
6871 sp->tree_type = tree_type;
6873 sp->lhs_type = lhs_type;
6876 lhs_type = rhs_type;
6878 new_prec = lookahead_prec;
6882 lookahead_prec = new_prec;
6883 /* If the stack is not empty, we have parsed into LHS the right side
6884 (`4' in the example above) of an expression we had suspended.
6885 We can use the information on the stack to recover the LHS (`3')
6886 from the stack together with the tree code (`MULT_EXPR'), and
6887 the precedence of the higher level subexpression
6888 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6889 which will be used to actually build the additive expression. */
6892 tree_type = sp->tree_type;
6894 rhs_type = lhs_type;
6896 lhs_type = sp->lhs_type;
6899 /* Undo the disabling of warnings done above. */
6900 if (tree_type == TRUTH_ANDIF_EXPR)
6901 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6902 else if (tree_type == TRUTH_ORIF_EXPR)
6903 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6905 overloaded_p = false;
6906 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6907 ERROR_MARK for everything that is not a binary expression.
6908 This makes warn_about_parentheses miss some warnings that
6909 involve unary operators. For unary expressions we should
6910 pass the correct tree_code unless the unary expression was
6911 surrounded by parentheses.
6913 if (no_toplevel_fold_p
6914 && lookahead_prec <= prec
6916 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6917 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6919 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6920 &overloaded_p, tf_warning_or_error);
6921 lhs_type = tree_type;
6923 /* If the binary operator required the use of an overloaded operator,
6924 then this expression cannot be an integral constant-expression.
6925 An overloaded operator can be used even if both operands are
6926 otherwise permissible in an integral constant-expression if at
6927 least one of the operands is of enumeration type. */
6930 && cp_parser_non_integral_constant_expression (parser,
6932 return error_mark_node;
6939 /* Parse the `? expression : assignment-expression' part of a
6940 conditional-expression. The LOGICAL_OR_EXPR is the
6941 logical-or-expression that started the conditional-expression.
6942 Returns a representation of the entire conditional-expression.
6944 This routine is used by cp_parser_assignment_expression.
6946 ? expression : assignment-expression
6950 ? : assignment-expression */
6953 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6956 tree assignment_expr;
6957 struct cp_token *token;
6959 /* Consume the `?' token. */
6960 cp_lexer_consume_token (parser->lexer);
6961 token = cp_lexer_peek_token (parser->lexer);
6962 if (cp_parser_allow_gnu_extensions_p (parser)
6963 && token->type == CPP_COLON)
6965 pedwarn (token->location, OPT_pedantic,
6966 "ISO C++ does not allow ?: with omitted middle operand");
6967 /* Implicit true clause. */
6969 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6970 warn_for_omitted_condop (token->location, logical_or_expr);
6974 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6975 parser->colon_corrects_to_scope_p = false;
6976 /* Parse the expression. */
6977 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6978 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6979 c_inhibit_evaluation_warnings +=
6980 ((logical_or_expr == truthvalue_true_node)
6981 - (logical_or_expr == truthvalue_false_node));
6982 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6985 /* The next token should be a `:'. */
6986 cp_parser_require (parser, CPP_COLON, RT_COLON);
6987 /* Parse the assignment-expression. */
6988 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6989 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6991 /* Build the conditional-expression. */
6992 return build_x_conditional_expr (logical_or_expr,
6995 tf_warning_or_error);
6998 /* Parse an assignment-expression.
7000 assignment-expression:
7001 conditional-expression
7002 logical-or-expression assignment-operator assignment_expression
7005 CAST_P is true if this expression is the target of a cast.
7007 Returns a representation for the expression. */
7010 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7015 /* If the next token is the `throw' keyword, then we're looking at
7016 a throw-expression. */
7017 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7018 expr = cp_parser_throw_expression (parser);
7019 /* Otherwise, it must be that we are looking at a
7020 logical-or-expression. */
7023 /* Parse the binary expressions (logical-or-expression). */
7024 expr = cp_parser_binary_expression (parser, cast_p, false,
7025 PREC_NOT_OPERATOR, pidk);
7026 /* If the next token is a `?' then we're actually looking at a
7027 conditional-expression. */
7028 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7029 return cp_parser_question_colon_clause (parser, expr);
7032 enum tree_code assignment_operator;
7034 /* If it's an assignment-operator, we're using the second
7037 = cp_parser_assignment_operator_opt (parser);
7038 if (assignment_operator != ERROR_MARK)
7040 bool non_constant_p;
7042 /* Parse the right-hand side of the assignment. */
7043 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7045 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7046 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7048 /* An assignment may not appear in a
7049 constant-expression. */
7050 if (cp_parser_non_integral_constant_expression (parser,
7052 return error_mark_node;
7053 /* Build the assignment expression. */
7054 expr = build_x_modify_expr (expr,
7055 assignment_operator,
7057 tf_warning_or_error);
7065 /* Parse an (optional) assignment-operator.
7067 assignment-operator: one of
7068 = *= /= %= += -= >>= <<= &= ^= |=
7072 assignment-operator: one of
7075 If the next token is an assignment operator, the corresponding tree
7076 code is returned, and the token is consumed. For example, for
7077 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7078 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7079 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7080 operator, ERROR_MARK is returned. */
7082 static enum tree_code
7083 cp_parser_assignment_operator_opt (cp_parser* parser)
7088 /* Peek at the next token. */
7089 token = cp_lexer_peek_token (parser->lexer);
7091 switch (token->type)
7102 op = TRUNC_DIV_EXPR;
7106 op = TRUNC_MOD_EXPR;
7138 /* Nothing else is an assignment operator. */
7142 /* If it was an assignment operator, consume it. */
7143 if (op != ERROR_MARK)
7144 cp_lexer_consume_token (parser->lexer);
7149 /* Parse an expression.
7152 assignment-expression
7153 expression , assignment-expression
7155 CAST_P is true if this expression is the target of a cast.
7157 Returns a representation of the expression. */
7160 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7162 tree expression = NULL_TREE;
7166 tree assignment_expression;
7168 /* Parse the next assignment-expression. */
7169 assignment_expression
7170 = cp_parser_assignment_expression (parser, cast_p, pidk);
7171 /* If this is the first assignment-expression, we can just
7174 expression = assignment_expression;
7176 expression = build_x_compound_expr (expression,
7177 assignment_expression,
7178 tf_warning_or_error);
7179 /* If the next token is not a comma, then we are done with the
7181 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7183 /* Consume the `,'. */
7184 cp_lexer_consume_token (parser->lexer);
7185 /* A comma operator cannot appear in a constant-expression. */
7186 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7187 expression = error_mark_node;
7193 /* Parse a constant-expression.
7195 constant-expression:
7196 conditional-expression
7198 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7199 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7200 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7201 is false, NON_CONSTANT_P should be NULL. */
7204 cp_parser_constant_expression (cp_parser* parser,
7205 bool allow_non_constant_p,
7206 bool *non_constant_p)
7208 bool saved_integral_constant_expression_p;
7209 bool saved_allow_non_integral_constant_expression_p;
7210 bool saved_non_integral_constant_expression_p;
7213 /* It might seem that we could simply parse the
7214 conditional-expression, and then check to see if it were
7215 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7216 one that the compiler can figure out is constant, possibly after
7217 doing some simplifications or optimizations. The standard has a
7218 precise definition of constant-expression, and we must honor
7219 that, even though it is somewhat more restrictive.
7225 is not a legal declaration, because `(2, 3)' is not a
7226 constant-expression. The `,' operator is forbidden in a
7227 constant-expression. However, GCC's constant-folding machinery
7228 will fold this operation to an INTEGER_CST for `3'. */
7230 /* Save the old settings. */
7231 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7232 saved_allow_non_integral_constant_expression_p
7233 = parser->allow_non_integral_constant_expression_p;
7234 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7235 /* We are now parsing a constant-expression. */
7236 parser->integral_constant_expression_p = true;
7237 parser->allow_non_integral_constant_expression_p
7238 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7239 parser->non_integral_constant_expression_p = false;
7240 /* Although the grammar says "conditional-expression", we parse an
7241 "assignment-expression", which also permits "throw-expression"
7242 and the use of assignment operators. In the case that
7243 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7244 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7245 actually essential that we look for an assignment-expression.
7246 For example, cp_parser_initializer_clauses uses this function to
7247 determine whether a particular assignment-expression is in fact
7249 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7250 /* Restore the old settings. */
7251 parser->integral_constant_expression_p
7252 = saved_integral_constant_expression_p;
7253 parser->allow_non_integral_constant_expression_p
7254 = saved_allow_non_integral_constant_expression_p;
7255 if (allow_non_constant_p)
7256 *non_constant_p = parser->non_integral_constant_expression_p;
7257 else if (parser->non_integral_constant_expression_p
7258 && cxx_dialect < cxx0x)
7259 expression = error_mark_node;
7260 parser->non_integral_constant_expression_p
7261 = saved_non_integral_constant_expression_p;
7266 /* Parse __builtin_offsetof.
7268 offsetof-expression:
7269 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7271 offsetof-member-designator:
7273 | offsetof-member-designator "." id-expression
7274 | offsetof-member-designator "[" expression "]"
7275 | offsetof-member-designator "->" id-expression */
7278 cp_parser_builtin_offsetof (cp_parser *parser)
7280 int save_ice_p, save_non_ice_p;
7285 /* We're about to accept non-integral-constant things, but will
7286 definitely yield an integral constant expression. Save and
7287 restore these values around our local parsing. */
7288 save_ice_p = parser->integral_constant_expression_p;
7289 save_non_ice_p = parser->non_integral_constant_expression_p;
7291 /* Consume the "__builtin_offsetof" token. */
7292 cp_lexer_consume_token (parser->lexer);
7293 /* Consume the opening `('. */
7294 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7295 /* Parse the type-id. */
7296 type = cp_parser_type_id (parser);
7297 /* Look for the `,'. */
7298 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7299 token = cp_lexer_peek_token (parser->lexer);
7301 /* Build the (type *)null that begins the traditional offsetof macro. */
7302 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7303 tf_warning_or_error);
7305 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7306 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7307 true, &dummy, token->location);
7310 token = cp_lexer_peek_token (parser->lexer);
7311 switch (token->type)
7313 case CPP_OPEN_SQUARE:
7314 /* offsetof-member-designator "[" expression "]" */
7315 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7319 /* offsetof-member-designator "->" identifier */
7320 expr = grok_array_decl (expr, integer_zero_node);
7324 /* offsetof-member-designator "." identifier */
7325 cp_lexer_consume_token (parser->lexer);
7326 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7331 case CPP_CLOSE_PAREN:
7332 /* Consume the ")" token. */
7333 cp_lexer_consume_token (parser->lexer);
7337 /* Error. We know the following require will fail, but
7338 that gives the proper error message. */
7339 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7340 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7341 expr = error_mark_node;
7347 /* If we're processing a template, we can't finish the semantics yet.
7348 Otherwise we can fold the entire expression now. */
7349 if (processing_template_decl)
7350 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7352 expr = finish_offsetof (expr);
7355 parser->integral_constant_expression_p = save_ice_p;
7356 parser->non_integral_constant_expression_p = save_non_ice_p;
7361 /* Parse a trait expression. */
7364 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7367 tree type1, type2 = NULL_TREE;
7368 bool binary = false;
7369 cp_decl_specifier_seq decl_specs;
7373 case RID_HAS_NOTHROW_ASSIGN:
7374 kind = CPTK_HAS_NOTHROW_ASSIGN;
7376 case RID_HAS_NOTHROW_CONSTRUCTOR:
7377 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7379 case RID_HAS_NOTHROW_COPY:
7380 kind = CPTK_HAS_NOTHROW_COPY;
7382 case RID_HAS_TRIVIAL_ASSIGN:
7383 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7385 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7386 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7388 case RID_HAS_TRIVIAL_COPY:
7389 kind = CPTK_HAS_TRIVIAL_COPY;
7391 case RID_HAS_TRIVIAL_DESTRUCTOR:
7392 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7394 case RID_HAS_VIRTUAL_DESTRUCTOR:
7395 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7397 case RID_IS_ABSTRACT:
7398 kind = CPTK_IS_ABSTRACT;
7400 case RID_IS_BASE_OF:
7401 kind = CPTK_IS_BASE_OF;
7405 kind = CPTK_IS_CLASS;
7407 case RID_IS_CONVERTIBLE_TO:
7408 kind = CPTK_IS_CONVERTIBLE_TO;
7412 kind = CPTK_IS_EMPTY;
7415 kind = CPTK_IS_ENUM;
7420 case RID_IS_POLYMORPHIC:
7421 kind = CPTK_IS_POLYMORPHIC;
7423 case RID_IS_STD_LAYOUT:
7424 kind = CPTK_IS_STD_LAYOUT;
7426 case RID_IS_TRIVIAL:
7427 kind = CPTK_IS_TRIVIAL;
7430 kind = CPTK_IS_UNION;
7432 case RID_IS_LITERAL_TYPE:
7433 kind = CPTK_IS_LITERAL_TYPE;
7439 /* Consume the token. */
7440 cp_lexer_consume_token (parser->lexer);
7442 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7444 type1 = cp_parser_type_id (parser);
7446 if (type1 == error_mark_node)
7447 return error_mark_node;
7449 /* Build a trivial decl-specifier-seq. */
7450 clear_decl_specs (&decl_specs);
7451 decl_specs.type = type1;
7453 /* Call grokdeclarator to figure out what type this is. */
7454 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7455 /*initialized=*/0, /*attrlist=*/NULL);
7459 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7461 type2 = cp_parser_type_id (parser);
7463 if (type2 == error_mark_node)
7464 return error_mark_node;
7466 /* Build a trivial decl-specifier-seq. */
7467 clear_decl_specs (&decl_specs);
7468 decl_specs.type = type2;
7470 /* Call grokdeclarator to figure out what type this is. */
7471 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7472 /*initialized=*/0, /*attrlist=*/NULL);
7475 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7477 /* Complete the trait expression, which may mean either processing
7478 the trait expr now or saving it for template instantiation. */
7479 return finish_trait_expr (kind, type1, type2);
7482 /* Lambdas that appear in variable initializer or default argument scope
7483 get that in their mangling, so we need to record it. We might as well
7484 use the count for function and namespace scopes as well. */
7485 static GTY(()) tree lambda_scope;
7486 static GTY(()) int lambda_count;
7487 typedef struct GTY(()) tree_int
7492 DEF_VEC_O(tree_int);
7493 DEF_VEC_ALLOC_O(tree_int,gc);
7494 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7497 start_lambda_scope (tree decl)
7501 /* Once we're inside a function, we ignore other scopes and just push
7502 the function again so that popping works properly. */
7503 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7504 decl = current_function_decl;
7505 ti.t = lambda_scope;
7506 ti.i = lambda_count;
7507 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7508 if (lambda_scope != decl)
7510 /* Don't reset the count if we're still in the same function. */
7511 lambda_scope = decl;
7517 record_lambda_scope (tree lambda)
7519 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7520 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7524 finish_lambda_scope (void)
7526 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7527 if (lambda_scope != p->t)
7529 lambda_scope = p->t;
7530 lambda_count = p->i;
7532 VEC_pop (tree_int, lambda_scope_stack);
7535 /* Parse a lambda expression.
7538 lambda-introducer lambda-declarator [opt] compound-statement
7540 Returns a representation of the expression. */
7543 cp_parser_lambda_expression (cp_parser* parser)
7545 tree lambda_expr = build_lambda_expr ();
7548 LAMBDA_EXPR_LOCATION (lambda_expr)
7549 = cp_lexer_peek_token (parser->lexer)->location;
7551 if (cp_unevaluated_operand)
7552 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7553 "lambda-expression in unevaluated context");
7555 /* We may be in the middle of deferred access check. Disable
7557 push_deferring_access_checks (dk_no_deferred);
7559 cp_parser_lambda_introducer (parser, lambda_expr);
7561 type = begin_lambda_type (lambda_expr);
7563 record_lambda_scope (lambda_expr);
7565 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7566 determine_visibility (TYPE_NAME (type));
7568 /* Now that we've started the type, add the capture fields for any
7569 explicit captures. */
7570 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7573 /* Inside the class, surrounding template-parameter-lists do not apply. */
7574 unsigned int saved_num_template_parameter_lists
7575 = parser->num_template_parameter_lists;
7577 parser->num_template_parameter_lists = 0;
7579 /* By virtue of defining a local class, a lambda expression has access to
7580 the private variables of enclosing classes. */
7582 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7584 cp_parser_lambda_body (parser, lambda_expr);
7586 /* The capture list was built up in reverse order; fix that now. */
7588 tree newlist = NULL_TREE;
7591 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7594 tree field = TREE_PURPOSE (elt);
7597 next = TREE_CHAIN (elt);
7598 TREE_CHAIN (elt) = newlist;
7601 /* Also add __ to the beginning of the field name so that code
7602 outside the lambda body can't see the captured name. We could
7603 just remove the name entirely, but this is more useful for
7605 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7606 /* The 'this' capture already starts with __. */
7609 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7610 buf[1] = buf[0] = '_';
7611 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7612 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7613 DECL_NAME (field) = get_identifier (buf);
7615 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7618 maybe_add_lambda_conv_op (type);
7620 type = finish_struct (type, /*attributes=*/NULL_TREE);
7622 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7625 pop_deferring_access_checks ();
7627 return build_lambda_object (lambda_expr);
7630 /* Parse the beginning of a lambda expression.
7633 [ lambda-capture [opt] ]
7635 LAMBDA_EXPR is the current representation of the lambda expression. */
7638 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7640 /* Need commas after the first capture. */
7643 /* Eat the leading `['. */
7644 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7646 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7647 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7648 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7649 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7650 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7651 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7653 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7655 cp_lexer_consume_token (parser->lexer);
7659 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7661 cp_token* capture_token;
7663 tree capture_init_expr;
7664 cp_id_kind idk = CP_ID_KIND_NONE;
7665 bool explicit_init_p = false;
7667 enum capture_kind_type
7672 enum capture_kind_type capture_kind = BY_COPY;
7674 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7676 error ("expected end of capture-list");
7683 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7685 /* Possibly capture `this'. */
7686 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7688 cp_lexer_consume_token (parser->lexer);
7689 add_capture (lambda_expr,
7690 /*id=*/get_identifier ("__this"),
7691 /*initializer=*/finish_this_expr(),
7692 /*by_reference_p=*/false,
7697 /* Remember whether we want to capture as a reference or not. */
7698 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7700 capture_kind = BY_REFERENCE;
7701 cp_lexer_consume_token (parser->lexer);
7704 /* Get the identifier. */
7705 capture_token = cp_lexer_peek_token (parser->lexer);
7706 capture_id = cp_parser_identifier (parser);
7708 if (capture_id == error_mark_node)
7709 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7710 delimiters, but I modified this to stop on unnested ']' as well. It
7711 was already changed to stop on unnested '}', so the
7712 "closing_parenthesis" name is no more misleading with my change. */
7714 cp_parser_skip_to_closing_parenthesis (parser,
7715 /*recovering=*/true,
7717 /*consume_paren=*/true);
7721 /* Find the initializer for this capture. */
7722 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7724 /* An explicit expression exists. */
7725 cp_lexer_consume_token (parser->lexer);
7726 pedwarn (input_location, OPT_pedantic,
7727 "ISO C++ does not allow initializers "
7728 "in lambda expression capture lists");
7729 capture_init_expr = cp_parser_assignment_expression (parser,
7732 explicit_init_p = true;
7736 const char* error_msg;
7738 /* Turn the identifier into an id-expression. */
7740 = cp_parser_lookup_name
7744 /*is_template=*/false,
7745 /*is_namespace=*/false,
7746 /*check_dependency=*/true,
7747 /*ambiguous_decls=*/NULL,
7748 capture_token->location);
7751 = finish_id_expression
7756 /*integral_constant_expression_p=*/false,
7757 /*allow_non_integral_constant_expression_p=*/false,
7758 /*non_integral_constant_expression_p=*/NULL,
7759 /*template_p=*/false,
7761 /*address_p=*/false,
7762 /*template_arg_p=*/false,
7764 capture_token->location);
7767 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7769 = unqualified_name_lookup_error (capture_init_expr);
7771 add_capture (lambda_expr,
7774 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7778 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7781 /* Parse the (optional) middle of a lambda expression.
7784 ( parameter-declaration-clause [opt] )
7785 attribute-specifier [opt]
7787 exception-specification [opt]
7788 lambda-return-type-clause [opt]
7790 LAMBDA_EXPR is the current representation of the lambda expression. */
7793 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7795 /* 5.1.1.4 of the standard says:
7796 If a lambda-expression does not include a lambda-declarator, it is as if
7797 the lambda-declarator were ().
7798 This means an empty parameter list, no attributes, and no exception
7800 tree param_list = void_list_node;
7801 tree attributes = NULL_TREE;
7802 tree exception_spec = NULL_TREE;
7805 /* The lambda-declarator is optional, but must begin with an opening
7806 parenthesis if present. */
7807 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7809 cp_lexer_consume_token (parser->lexer);
7811 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7813 /* Parse parameters. */
7814 param_list = cp_parser_parameter_declaration_clause (parser);
7816 /* Default arguments shall not be specified in the
7817 parameter-declaration-clause of a lambda-declarator. */
7818 for (t = param_list; t; t = TREE_CHAIN (t))
7819 if (TREE_PURPOSE (t))
7820 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7821 "default argument specified for lambda parameter");
7823 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7825 attributes = cp_parser_attributes_opt (parser);
7827 /* Parse optional `mutable' keyword. */
7828 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7830 cp_lexer_consume_token (parser->lexer);
7831 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7834 /* Parse optional exception specification. */
7835 exception_spec = cp_parser_exception_specification_opt (parser);
7837 /* Parse optional trailing return type. */
7838 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7840 cp_lexer_consume_token (parser->lexer);
7841 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7844 /* The function parameters must be in scope all the way until after the
7845 trailing-return-type in case of decltype. */
7846 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7847 pop_binding (DECL_NAME (t), t);
7852 /* Create the function call operator.
7854 Messing with declarators like this is no uglier than building up the
7855 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7858 cp_decl_specifier_seq return_type_specs;
7859 cp_declarator* declarator;
7864 clear_decl_specs (&return_type_specs);
7865 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7866 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7868 /* Maybe we will deduce the return type later, but we can use void
7869 as a placeholder return type anyways. */
7870 return_type_specs.type = void_type_node;
7872 p = obstack_alloc (&declarator_obstack, 0);
7874 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7877 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7878 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7879 declarator = make_call_declarator (declarator, param_list, quals,
7881 /*late_return_type=*/NULL_TREE);
7882 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7884 fco = grokmethod (&return_type_specs,
7887 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7888 DECL_ARTIFICIAL (fco) = 1;
7890 finish_member_declaration (fco);
7892 obstack_free (&declarator_obstack, p);
7896 /* Parse the body of a lambda expression, which is simply
7900 but which requires special handling.
7901 LAMBDA_EXPR is the current representation of the lambda expression. */
7904 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7906 bool nested = (current_function_decl != NULL_TREE);
7908 push_function_context ();
7910 /* Finish the function call operator
7912 + late_parsing_for_member
7913 + function_definition_after_declarator
7914 + ctor_initializer_opt_and_function_body */
7916 tree fco = lambda_function (lambda_expr);
7920 /* Let the front end know that we are going to be defining this
7922 start_preparsed_function (fco,
7924 SF_PRE_PARSED | SF_INCLASS_INLINE);
7926 start_lambda_scope (fco);
7927 body = begin_function_body ();
7929 /* 5.1.1.4 of the standard says:
7930 If a lambda-expression does not include a trailing-return-type, it
7931 is as if the trailing-return-type denotes the following type:
7932 * if the compound-statement is of the form
7933 { return attribute-specifier [opt] expression ; }
7934 the type of the returned expression after lvalue-to-rvalue
7935 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7936 (_conv.array_ 4.2), and function-to-pointer conversion
7938 * otherwise, void. */
7940 /* In a lambda that has neither a lambda-return-type-clause
7941 nor a deducible form, errors should be reported for return statements
7942 in the body. Since we used void as the placeholder return type, parsing
7943 the body as usual will give such desired behavior. */
7944 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7945 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7946 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7947 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7950 tree expr = NULL_TREE;
7951 cp_id_kind idk = CP_ID_KIND_NONE;
7953 /* Parse tentatively in case there's more after the initial return
7955 cp_parser_parse_tentatively (parser);
7957 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7958 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7960 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7962 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7963 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7965 if (cp_parser_parse_definitely (parser))
7967 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7969 compound_stmt = begin_compound_stmt (0);
7970 /* Will get error here if type not deduced yet. */
7971 finish_return_stmt (expr);
7972 finish_compound_stmt (compound_stmt);
7980 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7981 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7982 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7983 cp_parser_compound_stmt does not pass it. */
7984 cp_parser_function_body (parser);
7985 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7988 finish_function_body (body);
7989 finish_lambda_scope ();
7991 /* Finish the function and generate code for it if necessary. */
7992 expand_or_defer_fn (finish_function (/*inline*/2));
7996 pop_function_context();
7999 /* Statements [gram.stmt.stmt] */
8001 /* Parse a statement.
8005 expression-statement
8010 declaration-statement
8013 IN_COMPOUND is true when the statement is nested inside a
8014 cp_parser_compound_statement; this matters for certain pragmas.
8016 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8017 is a (possibly labeled) if statement which is not enclosed in braces
8018 and has an else clause. This is used to implement -Wparentheses. */
8021 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8022 bool in_compound, bool *if_p)
8026 location_t statement_location;
8031 /* There is no statement yet. */
8032 statement = NULL_TREE;
8033 /* Peek at the next token. */
8034 token = cp_lexer_peek_token (parser->lexer);
8035 /* Remember the location of the first token in the statement. */
8036 statement_location = token->location;
8037 /* If this is a keyword, then that will often determine what kind of
8038 statement we have. */
8039 if (token->type == CPP_KEYWORD)
8041 enum rid keyword = token->keyword;
8047 /* Looks like a labeled-statement with a case label.
8048 Parse the label, and then use tail recursion to parse
8050 cp_parser_label_for_labeled_statement (parser);
8055 statement = cp_parser_selection_statement (parser, if_p);
8061 statement = cp_parser_iteration_statement (parser);
8068 statement = cp_parser_jump_statement (parser);
8071 /* Objective-C++ exception-handling constructs. */
8074 case RID_AT_FINALLY:
8075 case RID_AT_SYNCHRONIZED:
8077 statement = cp_parser_objc_statement (parser);
8081 statement = cp_parser_try_block (parser);
8085 /* This must be a namespace alias definition. */
8086 cp_parser_declaration_statement (parser);
8090 /* It might be a keyword like `int' that can start a
8091 declaration-statement. */
8095 else if (token->type == CPP_NAME)
8097 /* If the next token is a `:', then we are looking at a
8098 labeled-statement. */
8099 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8100 if (token->type == CPP_COLON)
8102 /* Looks like a labeled-statement with an ordinary label.
8103 Parse the label, and then use tail recursion to parse
8105 cp_parser_label_for_labeled_statement (parser);
8109 /* Anything that starts with a `{' must be a compound-statement. */
8110 else if (token->type == CPP_OPEN_BRACE)
8111 statement = cp_parser_compound_statement (parser, NULL, false);
8112 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8113 a statement all its own. */
8114 else if (token->type == CPP_PRAGMA)
8116 /* Only certain OpenMP pragmas are attached to statements, and thus
8117 are considered statements themselves. All others are not. In
8118 the context of a compound, accept the pragma as a "statement" and
8119 return so that we can check for a close brace. Otherwise we
8120 require a real statement and must go back and read one. */
8122 cp_parser_pragma (parser, pragma_compound);
8123 else if (!cp_parser_pragma (parser, pragma_stmt))
8127 else if (token->type == CPP_EOF)
8129 cp_parser_error (parser, "expected statement");
8133 /* Everything else must be a declaration-statement or an
8134 expression-statement. Try for the declaration-statement
8135 first, unless we are looking at a `;', in which case we know that
8136 we have an expression-statement. */
8139 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8141 cp_parser_parse_tentatively (parser);
8142 /* Try to parse the declaration-statement. */
8143 cp_parser_declaration_statement (parser);
8144 /* If that worked, we're done. */
8145 if (cp_parser_parse_definitely (parser))
8148 /* Look for an expression-statement instead. */
8149 statement = cp_parser_expression_statement (parser, in_statement_expr);
8152 /* Set the line number for the statement. */
8153 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8154 SET_EXPR_LOCATION (statement, statement_location);
8157 /* Parse the label for a labeled-statement, i.e.
8160 case constant-expression :
8164 case constant-expression ... constant-expression : statement
8166 When a label is parsed without errors, the label is added to the
8167 parse tree by the finish_* functions, so this function doesn't
8168 have to return the label. */
8171 cp_parser_label_for_labeled_statement (cp_parser* parser)
8174 tree label = NULL_TREE;
8175 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8177 /* The next token should be an identifier. */
8178 token = cp_lexer_peek_token (parser->lexer);
8179 if (token->type != CPP_NAME
8180 && token->type != CPP_KEYWORD)
8182 cp_parser_error (parser, "expected labeled-statement");
8186 parser->colon_corrects_to_scope_p = false;
8187 switch (token->keyword)
8194 /* Consume the `case' token. */
8195 cp_lexer_consume_token (parser->lexer);
8196 /* Parse the constant-expression. */
8197 expr = cp_parser_constant_expression (parser,
8198 /*allow_non_constant_p=*/false,
8201 ellipsis = cp_lexer_peek_token (parser->lexer);
8202 if (ellipsis->type == CPP_ELLIPSIS)
8204 /* Consume the `...' token. */
8205 cp_lexer_consume_token (parser->lexer);
8207 cp_parser_constant_expression (parser,
8208 /*allow_non_constant_p=*/false,
8210 /* We don't need to emit warnings here, as the common code
8211 will do this for us. */
8214 expr_hi = NULL_TREE;
8216 if (parser->in_switch_statement_p)
8217 finish_case_label (token->location, expr, expr_hi);
8219 error_at (token->location,
8220 "case label %qE not within a switch statement",
8226 /* Consume the `default' token. */
8227 cp_lexer_consume_token (parser->lexer);
8229 if (parser->in_switch_statement_p)
8230 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8232 error_at (token->location, "case label not within a switch statement");
8236 /* Anything else must be an ordinary label. */
8237 label = finish_label_stmt (cp_parser_identifier (parser));
8241 /* Require the `:' token. */
8242 cp_parser_require (parser, CPP_COLON, RT_COLON);
8244 /* An ordinary label may optionally be followed by attributes.
8245 However, this is only permitted if the attributes are then
8246 followed by a semicolon. This is because, for backward
8247 compatibility, when parsing
8248 lab: __attribute__ ((unused)) int i;
8249 we want the attribute to attach to "i", not "lab". */
8250 if (label != NULL_TREE
8251 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8255 cp_parser_parse_tentatively (parser);
8256 attrs = cp_parser_attributes_opt (parser);
8257 if (attrs == NULL_TREE
8258 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8259 cp_parser_abort_tentative_parse (parser);
8260 else if (!cp_parser_parse_definitely (parser))
8263 cplus_decl_attributes (&label, attrs, 0);
8266 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8269 /* Parse an expression-statement.
8271 expression-statement:
8274 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8275 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8276 indicates whether this expression-statement is part of an
8277 expression statement. */
8280 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8282 tree statement = NULL_TREE;
8283 cp_token *token = cp_lexer_peek_token (parser->lexer);
8285 /* If the next token is a ';', then there is no expression
8287 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8288 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8290 /* Give a helpful message for "A<T>::type t;" and the like. */
8291 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8292 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8294 if (TREE_CODE (statement) == SCOPE_REF)
8295 error_at (token->location, "need %<typename%> before %qE because "
8296 "%qT is a dependent scope",
8297 statement, TREE_OPERAND (statement, 0));
8298 else if (is_overloaded_fn (statement)
8299 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8302 tree fn = get_first_fn (statement);
8303 error_at (token->location,
8304 "%<%T::%D%> names the constructor, not the type",
8305 DECL_CONTEXT (fn), DECL_NAME (fn));
8309 /* Consume the final `;'. */
8310 cp_parser_consume_semicolon_at_end_of_statement (parser);
8312 if (in_statement_expr
8313 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8314 /* This is the final expression statement of a statement
8316 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8318 statement = finish_expr_stmt (statement);
8325 /* Parse a compound-statement.
8328 { statement-seq [opt] }
8333 { label-declaration-seq [opt] statement-seq [opt] }
8335 label-declaration-seq:
8337 label-declaration-seq label-declaration
8339 Returns a tree representing the statement. */
8342 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8347 /* Consume the `{'. */
8348 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8349 return error_mark_node;
8350 /* Begin the compound-statement. */
8351 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8352 /* If the next keyword is `__label__' we have a label declaration. */
8353 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8354 cp_parser_label_declaration (parser);
8355 /* Parse an (optional) statement-seq. */
8356 cp_parser_statement_seq_opt (parser, in_statement_expr);
8357 /* Finish the compound-statement. */
8358 finish_compound_stmt (compound_stmt);
8359 /* Consume the `}'. */
8360 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8362 return compound_stmt;
8365 /* Parse an (optional) statement-seq.
8369 statement-seq [opt] statement */
8372 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8374 /* Scan statements until there aren't any more. */
8377 cp_token *token = cp_lexer_peek_token (parser->lexer);
8379 /* If we are looking at a `}', then we have run out of
8380 statements; the same is true if we have reached the end
8381 of file, or have stumbled upon a stray '@end'. */
8382 if (token->type == CPP_CLOSE_BRACE
8383 || token->type == CPP_EOF
8384 || token->type == CPP_PRAGMA_EOL
8385 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8388 /* If we are in a compound statement and find 'else' then
8389 something went wrong. */
8390 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8392 if (parser->in_statement & IN_IF_STMT)
8396 token = cp_lexer_consume_token (parser->lexer);
8397 error_at (token->location, "%<else%> without a previous %<if%>");
8401 /* Parse the statement. */
8402 cp_parser_statement (parser, in_statement_expr, true, NULL);
8406 /* Parse a selection-statement.
8408 selection-statement:
8409 if ( condition ) statement
8410 if ( condition ) statement else statement
8411 switch ( condition ) statement
8413 Returns the new IF_STMT or SWITCH_STMT.
8415 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8416 is a (possibly labeled) if statement which is not enclosed in
8417 braces and has an else clause. This is used to implement
8421 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8429 /* Peek at the next token. */
8430 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8432 /* See what kind of keyword it is. */
8433 keyword = token->keyword;
8442 /* Look for the `('. */
8443 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8445 cp_parser_skip_to_end_of_statement (parser);
8446 return error_mark_node;
8449 /* Begin the selection-statement. */
8450 if (keyword == RID_IF)
8451 statement = begin_if_stmt ();
8453 statement = begin_switch_stmt ();
8455 /* Parse the condition. */
8456 condition = cp_parser_condition (parser);
8457 /* Look for the `)'. */
8458 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8459 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8460 /*consume_paren=*/true);
8462 if (keyword == RID_IF)
8465 unsigned char in_statement;
8467 /* Add the condition. */
8468 finish_if_stmt_cond (condition, statement);
8470 /* Parse the then-clause. */
8471 in_statement = parser->in_statement;
8472 parser->in_statement |= IN_IF_STMT;
8473 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8475 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8476 add_stmt (build_empty_stmt (loc));
8477 cp_lexer_consume_token (parser->lexer);
8478 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8479 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8480 "empty body in an %<if%> statement");
8484 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8485 parser->in_statement = in_statement;
8487 finish_then_clause (statement);
8489 /* If the next token is `else', parse the else-clause. */
8490 if (cp_lexer_next_token_is_keyword (parser->lexer,
8493 /* Consume the `else' keyword. */
8494 cp_lexer_consume_token (parser->lexer);
8495 begin_else_clause (statement);
8496 /* Parse the else-clause. */
8497 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8500 loc = cp_lexer_peek_token (parser->lexer)->location;
8502 OPT_Wempty_body, "suggest braces around "
8503 "empty body in an %<else%> statement");
8504 add_stmt (build_empty_stmt (loc));
8505 cp_lexer_consume_token (parser->lexer);
8508 cp_parser_implicitly_scoped_statement (parser, NULL);
8510 finish_else_clause (statement);
8512 /* If we are currently parsing a then-clause, then
8513 IF_P will not be NULL. We set it to true to
8514 indicate that this if statement has an else clause.
8515 This may trigger the Wparentheses warning below
8516 when we get back up to the parent if statement. */
8522 /* This if statement does not have an else clause. If
8523 NESTED_IF is true, then the then-clause is an if
8524 statement which does have an else clause. We warn
8525 about the potential ambiguity. */
8527 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8528 "suggest explicit braces to avoid ambiguous"
8532 /* Now we're all done with the if-statement. */
8533 finish_if_stmt (statement);
8537 bool in_switch_statement_p;
8538 unsigned char in_statement;
8540 /* Add the condition. */
8541 finish_switch_cond (condition, statement);
8543 /* Parse the body of the switch-statement. */
8544 in_switch_statement_p = parser->in_switch_statement_p;
8545 in_statement = parser->in_statement;
8546 parser->in_switch_statement_p = true;
8547 parser->in_statement |= IN_SWITCH_STMT;
8548 cp_parser_implicitly_scoped_statement (parser, NULL);
8549 parser->in_switch_statement_p = in_switch_statement_p;
8550 parser->in_statement = in_statement;
8552 /* Now we're all done with the switch-statement. */
8553 finish_switch_stmt (statement);
8561 cp_parser_error (parser, "expected selection-statement");
8562 return error_mark_node;
8566 /* Parse a condition.
8570 type-specifier-seq declarator = initializer-clause
8571 type-specifier-seq declarator braced-init-list
8576 type-specifier-seq declarator asm-specification [opt]
8577 attributes [opt] = assignment-expression
8579 Returns the expression that should be tested. */
8582 cp_parser_condition (cp_parser* parser)
8584 cp_decl_specifier_seq type_specifiers;
8585 const char *saved_message;
8586 int declares_class_or_enum;
8588 /* Try the declaration first. */
8589 cp_parser_parse_tentatively (parser);
8590 /* New types are not allowed in the type-specifier-seq for a
8592 saved_message = parser->type_definition_forbidden_message;
8593 parser->type_definition_forbidden_message
8594 = G_("types may not be defined in conditions");
8595 /* Parse the type-specifier-seq. */
8596 cp_parser_decl_specifier_seq (parser,
8597 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8599 &declares_class_or_enum);
8600 /* Restore the saved message. */
8601 parser->type_definition_forbidden_message = saved_message;
8602 /* If all is well, we might be looking at a declaration. */
8603 if (!cp_parser_error_occurred (parser))
8606 tree asm_specification;
8608 cp_declarator *declarator;
8609 tree initializer = NULL_TREE;
8611 /* Parse the declarator. */
8612 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8613 /*ctor_dtor_or_conv_p=*/NULL,
8614 /*parenthesized_p=*/NULL,
8615 /*member_p=*/false);
8616 /* Parse the attributes. */
8617 attributes = cp_parser_attributes_opt (parser);
8618 /* Parse the asm-specification. */
8619 asm_specification = cp_parser_asm_specification_opt (parser);
8620 /* If the next token is not an `=' or '{', then we might still be
8621 looking at an expression. For example:
8625 looks like a decl-specifier-seq and a declarator -- but then
8626 there is no `=', so this is an expression. */
8627 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8628 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8629 cp_parser_simulate_error (parser);
8631 /* If we did see an `=' or '{', then we are looking at a declaration
8633 if (cp_parser_parse_definitely (parser))
8636 bool non_constant_p;
8637 bool flags = LOOKUP_ONLYCONVERTING;
8639 /* Create the declaration. */
8640 decl = start_decl (declarator, &type_specifiers,
8641 /*initialized_p=*/true,
8642 attributes, /*prefix_attributes=*/NULL_TREE,
8645 /* Parse the initializer. */
8646 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8648 initializer = cp_parser_braced_list (parser, &non_constant_p);
8649 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8654 /* Consume the `='. */
8655 cp_parser_require (parser, CPP_EQ, RT_EQ);
8656 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8658 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8659 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8661 if (!non_constant_p)
8662 initializer = fold_non_dependent_expr (initializer);
8664 /* Process the initializer. */
8665 cp_finish_decl (decl,
8666 initializer, !non_constant_p,
8671 pop_scope (pushed_scope);
8673 return convert_from_reference (decl);
8676 /* If we didn't even get past the declarator successfully, we are
8677 definitely not looking at a declaration. */
8679 cp_parser_abort_tentative_parse (parser);
8681 /* Otherwise, we are looking at an expression. */
8682 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8685 /* Parses a traditional for-statement until the closing ')', not included. */
8688 cp_parser_c_for (cp_parser *parser)
8690 /* Normal for loop */
8692 tree condition = NULL_TREE;
8693 tree expression = NULL_TREE;
8695 /* Begin the for-statement. */
8696 stmt = begin_for_stmt ();
8698 /* Parse the initialization. */
8699 cp_parser_for_init_statement (parser);
8700 finish_for_init_stmt (stmt);
8702 /* If there's a condition, process it. */
8703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8704 condition = cp_parser_condition (parser);
8705 finish_for_cond (condition, stmt);
8706 /* Look for the `;'. */
8707 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8709 /* If there's an expression, process it. */
8710 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8711 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8712 finish_for_expr (expression, stmt);
8717 /* Tries to parse a range-based for-statement:
8720 type-specifier-seq declarator : expression
8722 If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8723 expression. Note that the *DECL is returned unfinished, so
8724 later you should call cp_finish_decl().
8726 Returns TRUE iff a range-based for is parsed. */
8729 cp_parser_range_for (cp_parser *parser)
8731 tree stmt, range_decl, range_expr;
8732 cp_decl_specifier_seq type_specifiers;
8733 cp_declarator *declarator;
8734 const char *saved_message;
8735 tree attributes, pushed_scope;
8736 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8738 parser->colon_corrects_to_scope_p = false;
8739 cp_parser_parse_tentatively (parser);
8740 /* New types are not allowed in the type-specifier-seq for a
8741 range-based for loop. */
8742 saved_message = parser->type_definition_forbidden_message;
8743 parser->type_definition_forbidden_message
8744 = G_("types may not be defined in range-based for loops");
8745 /* Parse the type-specifier-seq. */
8746 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8747 /*is_trailing_return=*/false,
8749 /* Restore the saved message. */
8750 parser->type_definition_forbidden_message = saved_message;
8751 /* If all is well, we might be looking at a declaration. */
8752 if (cp_parser_error_occurred (parser))
8754 cp_parser_abort_tentative_parse (parser);
8758 /* Parse the declarator. */
8759 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8760 /*ctor_dtor_or_conv_p=*/NULL,
8761 /*parenthesized_p=*/NULL,
8762 /*member_p=*/false);
8763 /* Parse the attributes. */
8764 attributes = cp_parser_attributes_opt (parser);
8765 /* The next token should be `:'. */
8766 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8767 cp_parser_simulate_error (parser);
8769 /* Check if it is a range-based for */
8770 if (!cp_parser_parse_definitely (parser))
8773 cp_parser_require (parser, CPP_COLON, RT_COLON);
8774 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8776 bool expr_non_constant_p;
8777 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8780 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8782 /* If in template, STMT is converted to a normal for-statements
8783 at instantiation. If not, it is done just ahead. */
8784 if (processing_template_decl)
8785 stmt = begin_range_for_stmt ();
8787 stmt = begin_for_stmt ();
8789 /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8790 range_decl = start_decl (declarator, &type_specifiers,
8791 /*initialized_p=*/SD_INITIALIZED,
8792 attributes, /*prefix_attributes=*/NULL_TREE,
8794 /* No scope allowed here */
8795 pop_scope (pushed_scope);
8797 if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8798 finish_range_for_decl (stmt, range_decl, range_expr);
8800 /* Convert the range-based for loop into a normal for-statement. */
8801 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8804 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8808 /* Converts a range-based for-statement into a normal
8809 for-statement, as per the definition.
8811 for (RANGE_DECL : RANGE_EXPR)
8814 should be equivalent to:
8817 auto &&__range = RANGE_EXPR;
8818 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8822 RANGE_DECL = *__begin;
8827 If RANGE_EXPR is an array:
8828 BEGIN_EXPR = __range
8829 END_EXPR = __range + ARRAY_SIZE(__range)
8831 BEGIN_EXPR = begin(__range)
8832 END_EXPR = end(__range);
8834 When calling begin()/end() we must use argument dependent
8835 lookup, but always considering 'std' as an associated namespace. */
8838 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8840 tree range_type, range_temp;
8842 tree iter_type, begin_expr, end_expr;
8843 tree condition, expression;
8845 /* Find out the type deduced by the declaration
8846 * `auto &&__range = range_expr' */
8847 range_type = cp_build_reference_type (make_auto (), true);
8848 range_type = do_auto_deduction (range_type, range_expr,
8849 type_uses_auto (range_type));
8851 /* Create the __range variable */
8852 range_temp = build_decl (input_location, VAR_DECL,
8853 get_identifier ("__for_range"), range_type);
8854 TREE_USED (range_temp) = 1;
8855 DECL_ARTIFICIAL (range_temp) = 1;
8856 pushdecl (range_temp);
8857 cp_finish_decl (range_temp, range_expr,
8858 /*is_constant_init*/false, NULL_TREE,
8859 LOOKUP_ONLYCONVERTING);
8861 range_temp = convert_from_reference (range_temp);
8863 if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8865 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8866 iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8867 begin_expr = range_temp;
8869 = build_binary_op (input_location, PLUS_EXPR,
8871 array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8875 /* If it is not an array, we must call begin(__range)/end__range() */
8878 begin_expr = get_identifier ("begin");
8879 vec = make_tree_vector ();
8880 VEC_safe_push (tree, gc, vec, range_temp);
8881 begin_expr = perform_koenig_lookup (begin_expr, vec,
8882 /*include_std=*/true);
8883 begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8884 tf_warning_or_error);
8885 release_tree_vector (vec);
8887 end_expr = get_identifier ("end");
8888 vec = make_tree_vector ();
8889 VEC_safe_push (tree, gc, vec, range_temp);
8890 end_expr = perform_koenig_lookup (end_expr, vec,
8891 /*include_std=*/true);
8892 end_expr = finish_call_expr (end_expr, &vec, false, true,
8893 tf_warning_or_error);
8894 release_tree_vector (vec);
8896 /* The unqualified type of the __begin and __end temporaries should
8897 * be the same as required by the multiple auto declaration */
8898 iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8899 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8900 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8901 TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8904 /* The new for initialization statement */
8905 begin = build_decl (input_location, VAR_DECL,
8906 get_identifier ("__for_begin"), iter_type);
8907 TREE_USED (begin) = 1;
8908 DECL_ARTIFICIAL (begin) = 1;
8910 cp_finish_decl (begin, begin_expr,
8911 /*is_constant_init*/false, NULL_TREE,
8912 LOOKUP_ONLYCONVERTING);
8914 end = build_decl (input_location, VAR_DECL,
8915 get_identifier ("__for_end"), iter_type);
8916 TREE_USED (end) = 1;
8917 DECL_ARTIFICIAL (end) = 1;
8919 cp_finish_decl (end, end_expr,
8920 /*is_constant_init*/false, NULL_TREE,
8921 LOOKUP_ONLYCONVERTING);
8923 finish_for_init_stmt (statement);
8925 /* The new for condition */
8926 condition = build_x_binary_op (NE_EXPR,
8929 NULL, tf_warning_or_error);
8930 finish_for_cond (condition, statement);
8932 /* The new increment expression */
8933 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8934 finish_for_expr (expression, statement);
8936 /* The declaration is initialized with *__begin inside the loop body */
8937 cp_finish_decl (range_decl,
8938 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8939 /*is_constant_init*/false, NULL_TREE,
8940 LOOKUP_ONLYCONVERTING);
8946 /* Parse an iteration-statement.
8948 iteration-statement:
8949 while ( condition ) statement
8950 do statement while ( expression ) ;
8951 for ( for-init-statement condition [opt] ; expression [opt] )
8954 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8957 cp_parser_iteration_statement (cp_parser* parser)
8962 unsigned char in_statement;
8964 /* Peek at the next token. */
8965 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8967 return error_mark_node;
8969 /* Remember whether or not we are already within an iteration
8971 in_statement = parser->in_statement;
8973 /* See what kind of keyword it is. */
8974 keyword = token->keyword;
8981 /* Begin the while-statement. */
8982 statement = begin_while_stmt ();
8983 /* Look for the `('. */
8984 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8985 /* Parse the condition. */
8986 condition = cp_parser_condition (parser);
8987 finish_while_stmt_cond (condition, statement);
8988 /* Look for the `)'. */
8989 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8990 /* Parse the dependent statement. */
8991 parser->in_statement = IN_ITERATION_STMT;
8992 cp_parser_already_scoped_statement (parser);
8993 parser->in_statement = in_statement;
8994 /* We're done with the while-statement. */
8995 finish_while_stmt (statement);
9003 /* Begin the do-statement. */
9004 statement = begin_do_stmt ();
9005 /* Parse the body of the do-statement. */
9006 parser->in_statement = IN_ITERATION_STMT;
9007 cp_parser_implicitly_scoped_statement (parser, NULL);
9008 parser->in_statement = in_statement;
9009 finish_do_body (statement);
9010 /* Look for the `while' keyword. */
9011 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9012 /* Look for the `('. */
9013 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9014 /* Parse the expression. */
9015 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9016 /* We're done with the do-statement. */
9017 finish_do_stmt (expression, statement);
9018 /* Look for the `)'. */
9019 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9020 /* Look for the `;'. */
9021 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9027 /* Look for the `('. */
9028 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9030 if (cxx_dialect == cxx0x)
9031 statement = cp_parser_range_for (parser);
9033 statement = NULL_TREE;
9034 if (statement == NULL_TREE)
9035 statement = cp_parser_c_for (parser);
9037 /* Look for the `)'. */
9038 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9040 /* Parse the body of the for-statement. */
9041 parser->in_statement = IN_ITERATION_STMT;
9042 cp_parser_already_scoped_statement (parser);
9043 parser->in_statement = in_statement;
9045 /* We're done with the for-statement. */
9046 finish_for_stmt (statement);
9051 cp_parser_error (parser, "expected iteration-statement");
9052 statement = error_mark_node;
9059 /* Parse a for-init-statement.
9062 expression-statement
9063 simple-declaration */
9066 cp_parser_for_init_statement (cp_parser* parser)
9068 /* If the next token is a `;', then we have an empty
9069 expression-statement. Grammatically, this is also a
9070 simple-declaration, but an invalid one, because it does not
9071 declare anything. Therefore, if we did not handle this case
9072 specially, we would issue an error message about an invalid
9074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9076 /* We're going to speculatively look for a declaration, falling back
9077 to an expression, if necessary. */
9078 cp_parser_parse_tentatively (parser);
9079 /* Parse the declaration. */
9080 cp_parser_simple_declaration (parser,
9081 /*function_definition_allowed_p=*/false);
9082 /* If the tentative parse failed, then we shall need to look for an
9083 expression-statement. */
9084 if (cp_parser_parse_definitely (parser))
9088 cp_parser_expression_statement (parser, NULL_TREE);
9091 /* Parse a jump-statement.
9096 return expression [opt] ;
9097 return braced-init-list ;
9105 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9108 cp_parser_jump_statement (cp_parser* parser)
9110 tree statement = error_mark_node;
9113 unsigned char in_statement;
9115 /* Peek at the next token. */
9116 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9118 return error_mark_node;
9120 /* See what kind of keyword it is. */
9121 keyword = token->keyword;
9125 in_statement = parser->in_statement & ~IN_IF_STMT;
9126 switch (in_statement)
9129 error_at (token->location, "break statement not within loop or switch");
9132 gcc_assert ((in_statement & IN_SWITCH_STMT)
9133 || in_statement == IN_ITERATION_STMT);
9134 statement = finish_break_stmt ();
9137 error_at (token->location, "invalid exit from OpenMP structured block");
9140 error_at (token->location, "break statement used with OpenMP for loop");
9143 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9147 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9150 error_at (token->location, "continue statement not within a loop");
9152 case IN_ITERATION_STMT:
9154 statement = finish_continue_stmt ();
9157 error_at (token->location, "invalid exit from OpenMP structured block");
9162 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9168 bool expr_non_constant_p;
9170 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9172 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9173 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9175 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9176 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9178 /* If the next token is a `;', then there is no
9181 /* Build the return-statement. */
9182 statement = finish_return_stmt (expr);
9183 /* Look for the final `;'. */
9184 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9189 /* Create the goto-statement. */
9190 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9192 /* Issue a warning about this use of a GNU extension. */
9193 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9194 /* Consume the '*' token. */
9195 cp_lexer_consume_token (parser->lexer);
9196 /* Parse the dependent expression. */
9197 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9200 finish_goto_stmt (cp_parser_identifier (parser));
9201 /* Look for the final `;'. */
9202 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9206 cp_parser_error (parser, "expected jump-statement");
9213 /* Parse a declaration-statement.
9215 declaration-statement:
9216 block-declaration */
9219 cp_parser_declaration_statement (cp_parser* parser)
9223 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9224 p = obstack_alloc (&declarator_obstack, 0);
9226 /* Parse the block-declaration. */
9227 cp_parser_block_declaration (parser, /*statement_p=*/true);
9229 /* Free any declarators allocated. */
9230 obstack_free (&declarator_obstack, p);
9232 /* Finish off the statement. */
9236 /* Some dependent statements (like `if (cond) statement'), are
9237 implicitly in their own scope. In other words, if the statement is
9238 a single statement (as opposed to a compound-statement), it is
9239 none-the-less treated as if it were enclosed in braces. Any
9240 declarations appearing in the dependent statement are out of scope
9241 after control passes that point. This function parses a statement,
9242 but ensures that is in its own scope, even if it is not a
9245 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9246 is a (possibly labeled) if statement which is not enclosed in
9247 braces and has an else clause. This is used to implement
9250 Returns the new statement. */
9253 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9260 /* Mark if () ; with a special NOP_EXPR. */
9261 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9263 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9264 cp_lexer_consume_token (parser->lexer);
9265 statement = add_stmt (build_empty_stmt (loc));
9267 /* if a compound is opened, we simply parse the statement directly. */
9268 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9269 statement = cp_parser_compound_statement (parser, NULL, false);
9270 /* If the token is not a `{', then we must take special action. */
9273 /* Create a compound-statement. */
9274 statement = begin_compound_stmt (0);
9275 /* Parse the dependent-statement. */
9276 cp_parser_statement (parser, NULL_TREE, false, if_p);
9277 /* Finish the dummy compound-statement. */
9278 finish_compound_stmt (statement);
9281 /* Return the statement. */
9285 /* For some dependent statements (like `while (cond) statement'), we
9286 have already created a scope. Therefore, even if the dependent
9287 statement is a compound-statement, we do not want to create another
9291 cp_parser_already_scoped_statement (cp_parser* parser)
9293 /* If the token is a `{', then we must take special action. */
9294 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9295 cp_parser_statement (parser, NULL_TREE, false, NULL);
9298 /* Avoid calling cp_parser_compound_statement, so that we
9299 don't create a new scope. Do everything else by hand. */
9300 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9301 /* If the next keyword is `__label__' we have a label declaration. */
9302 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9303 cp_parser_label_declaration (parser);
9304 /* Parse an (optional) statement-seq. */
9305 cp_parser_statement_seq_opt (parser, NULL_TREE);
9306 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9310 /* Declarations [gram.dcl.dcl] */
9312 /* Parse an optional declaration-sequence.
9316 declaration-seq declaration */
9319 cp_parser_declaration_seq_opt (cp_parser* parser)
9325 token = cp_lexer_peek_token (parser->lexer);
9327 if (token->type == CPP_CLOSE_BRACE
9328 || token->type == CPP_EOF
9329 || token->type == CPP_PRAGMA_EOL)
9332 if (token->type == CPP_SEMICOLON)
9334 /* A declaration consisting of a single semicolon is
9335 invalid. Allow it unless we're being pedantic. */
9336 cp_lexer_consume_token (parser->lexer);
9337 if (!in_system_header)
9338 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9342 /* If we're entering or exiting a region that's implicitly
9343 extern "C", modify the lang context appropriately. */
9344 if (!parser->implicit_extern_c && token->implicit_extern_c)
9346 push_lang_context (lang_name_c);
9347 parser->implicit_extern_c = true;
9349 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9351 pop_lang_context ();
9352 parser->implicit_extern_c = false;
9355 if (token->type == CPP_PRAGMA)
9357 /* A top-level declaration can consist solely of a #pragma.
9358 A nested declaration cannot, so this is done here and not
9359 in cp_parser_declaration. (A #pragma at block scope is
9360 handled in cp_parser_statement.) */
9361 cp_parser_pragma (parser, pragma_external);
9365 /* Parse the declaration itself. */
9366 cp_parser_declaration (parser);
9370 /* Parse a declaration.
9375 template-declaration
9376 explicit-instantiation
9377 explicit-specialization
9378 linkage-specification
9379 namespace-definition
9384 __extension__ declaration */
9387 cp_parser_declaration (cp_parser* parser)
9393 tree attributes = NULL_TREE;
9395 /* Check for the `__extension__' keyword. */
9396 if (cp_parser_extension_opt (parser, &saved_pedantic))
9398 /* Parse the qualified declaration. */
9399 cp_parser_declaration (parser);
9400 /* Restore the PEDANTIC flag. */
9401 pedantic = saved_pedantic;
9406 /* Try to figure out what kind of declaration is present. */
9407 token1 = *cp_lexer_peek_token (parser->lexer);
9409 if (token1.type != CPP_EOF)
9410 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9413 token2.type = CPP_EOF;
9414 token2.keyword = RID_MAX;
9417 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9418 p = obstack_alloc (&declarator_obstack, 0);
9420 /* If the next token is `extern' and the following token is a string
9421 literal, then we have a linkage specification. */
9422 if (token1.keyword == RID_EXTERN
9423 && cp_parser_is_string_literal (&token2))
9424 cp_parser_linkage_specification (parser);
9425 /* If the next token is `template', then we have either a template
9426 declaration, an explicit instantiation, or an explicit
9428 else if (token1.keyword == RID_TEMPLATE)
9430 /* `template <>' indicates a template specialization. */
9431 if (token2.type == CPP_LESS
9432 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9433 cp_parser_explicit_specialization (parser);
9434 /* `template <' indicates a template declaration. */
9435 else if (token2.type == CPP_LESS)
9436 cp_parser_template_declaration (parser, /*member_p=*/false);
9437 /* Anything else must be an explicit instantiation. */
9439 cp_parser_explicit_instantiation (parser);
9441 /* If the next token is `export', then we have a template
9443 else if (token1.keyword == RID_EXPORT)
9444 cp_parser_template_declaration (parser, /*member_p=*/false);
9445 /* If the next token is `extern', 'static' or 'inline' and the one
9446 after that is `template', we have a GNU extended explicit
9447 instantiation directive. */
9448 else if (cp_parser_allow_gnu_extensions_p (parser)
9449 && (token1.keyword == RID_EXTERN
9450 || token1.keyword == RID_STATIC
9451 || token1.keyword == RID_INLINE)
9452 && token2.keyword == RID_TEMPLATE)
9453 cp_parser_explicit_instantiation (parser);
9454 /* If the next token is `namespace', check for a named or unnamed
9455 namespace definition. */
9456 else if (token1.keyword == RID_NAMESPACE
9457 && (/* A named namespace definition. */
9458 (token2.type == CPP_NAME
9459 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9461 /* An unnamed namespace definition. */
9462 || token2.type == CPP_OPEN_BRACE
9463 || token2.keyword == RID_ATTRIBUTE))
9464 cp_parser_namespace_definition (parser);
9465 /* An inline (associated) namespace definition. */
9466 else if (token1.keyword == RID_INLINE
9467 && token2.keyword == RID_NAMESPACE)
9468 cp_parser_namespace_definition (parser);
9469 /* Objective-C++ declaration/definition. */
9470 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9471 cp_parser_objc_declaration (parser, NULL_TREE);
9472 else if (c_dialect_objc ()
9473 && token1.keyword == RID_ATTRIBUTE
9474 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9475 cp_parser_objc_declaration (parser, attributes);
9476 /* We must have either a block declaration or a function
9479 /* Try to parse a block-declaration, or a function-definition. */
9480 cp_parser_block_declaration (parser, /*statement_p=*/false);
9482 /* Free any declarators allocated. */
9483 obstack_free (&declarator_obstack, p);
9486 /* Parse a block-declaration.
9491 namespace-alias-definition
9498 __extension__ block-declaration
9503 static_assert-declaration
9505 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9506 part of a declaration-statement. */
9509 cp_parser_block_declaration (cp_parser *parser,
9515 /* Check for the `__extension__' keyword. */
9516 if (cp_parser_extension_opt (parser, &saved_pedantic))
9518 /* Parse the qualified declaration. */
9519 cp_parser_block_declaration (parser, statement_p);
9520 /* Restore the PEDANTIC flag. */
9521 pedantic = saved_pedantic;
9526 /* Peek at the next token to figure out which kind of declaration is
9528 token1 = cp_lexer_peek_token (parser->lexer);
9530 /* If the next keyword is `asm', we have an asm-definition. */
9531 if (token1->keyword == RID_ASM)
9534 cp_parser_commit_to_tentative_parse (parser);
9535 cp_parser_asm_definition (parser);
9537 /* If the next keyword is `namespace', we have a
9538 namespace-alias-definition. */
9539 else if (token1->keyword == RID_NAMESPACE)
9540 cp_parser_namespace_alias_definition (parser);
9541 /* If the next keyword is `using', we have either a
9542 using-declaration or a using-directive. */
9543 else if (token1->keyword == RID_USING)
9548 cp_parser_commit_to_tentative_parse (parser);
9549 /* If the token after `using' is `namespace', then we have a
9551 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9552 if (token2->keyword == RID_NAMESPACE)
9553 cp_parser_using_directive (parser);
9554 /* Otherwise, it's a using-declaration. */
9556 cp_parser_using_declaration (parser,
9557 /*access_declaration_p=*/false);
9559 /* If the next keyword is `__label__' we have a misplaced label
9561 else if (token1->keyword == RID_LABEL)
9563 cp_lexer_consume_token (parser->lexer);
9564 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9565 cp_parser_skip_to_end_of_statement (parser);
9566 /* If the next token is now a `;', consume it. */
9567 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9568 cp_lexer_consume_token (parser->lexer);
9570 /* If the next token is `static_assert' we have a static assertion. */
9571 else if (token1->keyword == RID_STATIC_ASSERT)
9572 cp_parser_static_assert (parser, /*member_p=*/false);
9573 /* Anything else must be a simple-declaration. */
9575 cp_parser_simple_declaration (parser, !statement_p);
9578 /* Parse a simple-declaration.
9581 decl-specifier-seq [opt] init-declarator-list [opt] ;
9583 init-declarator-list:
9585 init-declarator-list , init-declarator
9587 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9588 function-definition as a simple-declaration. */
9591 cp_parser_simple_declaration (cp_parser* parser,
9592 bool function_definition_allowed_p)
9594 cp_decl_specifier_seq decl_specifiers;
9595 int declares_class_or_enum;
9596 bool saw_declarator;
9598 /* Defer access checks until we know what is being declared; the
9599 checks for names appearing in the decl-specifier-seq should be
9600 done as if we were in the scope of the thing being declared. */
9601 push_deferring_access_checks (dk_deferred);
9603 /* Parse the decl-specifier-seq. We have to keep track of whether
9604 or not the decl-specifier-seq declares a named class or
9605 enumeration type, since that is the only case in which the
9606 init-declarator-list is allowed to be empty.
9610 In a simple-declaration, the optional init-declarator-list can be
9611 omitted only when declaring a class or enumeration, that is when
9612 the decl-specifier-seq contains either a class-specifier, an
9613 elaborated-type-specifier, or an enum-specifier. */
9614 cp_parser_decl_specifier_seq (parser,
9615 CP_PARSER_FLAGS_OPTIONAL,
9617 &declares_class_or_enum);
9618 /* We no longer need to defer access checks. */
9619 stop_deferring_access_checks ();
9621 /* In a block scope, a valid declaration must always have a
9622 decl-specifier-seq. By not trying to parse declarators, we can
9623 resolve the declaration/expression ambiguity more quickly. */
9624 if (!function_definition_allowed_p
9625 && !decl_specifiers.any_specifiers_p)
9627 cp_parser_error (parser, "expected declaration");
9631 /* If the next two tokens are both identifiers, the code is
9632 erroneous. The usual cause of this situation is code like:
9636 where "T" should name a type -- but does not. */
9637 if (!decl_specifiers.any_type_specifiers_p
9638 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9640 /* If parsing tentatively, we should commit; we really are
9641 looking at a declaration. */
9642 cp_parser_commit_to_tentative_parse (parser);
9647 /* If we have seen at least one decl-specifier, and the next token
9648 is not a parenthesis, then we must be looking at a declaration.
9649 (After "int (" we might be looking at a functional cast.) */
9650 if (decl_specifiers.any_specifiers_p
9651 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9652 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9653 && !cp_parser_error_occurred (parser))
9654 cp_parser_commit_to_tentative_parse (parser);
9656 /* Keep going until we hit the `;' at the end of the simple
9658 saw_declarator = false;
9659 while (cp_lexer_next_token_is_not (parser->lexer,
9663 bool function_definition_p;
9668 /* If we are processing next declarator, coma is expected */
9669 token = cp_lexer_peek_token (parser->lexer);
9670 gcc_assert (token->type == CPP_COMMA);
9671 cp_lexer_consume_token (parser->lexer);
9674 saw_declarator = true;
9676 /* Parse the init-declarator. */
9677 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9679 function_definition_allowed_p,
9681 declares_class_or_enum,
9682 &function_definition_p);
9683 /* If an error occurred while parsing tentatively, exit quickly.
9684 (That usually happens when in the body of a function; each
9685 statement is treated as a declaration-statement until proven
9687 if (cp_parser_error_occurred (parser))
9689 /* Handle function definitions specially. */
9690 if (function_definition_p)
9692 /* If the next token is a `,', then we are probably
9693 processing something like:
9697 which is erroneous. */
9698 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9700 cp_token *token = cp_lexer_peek_token (parser->lexer);
9701 error_at (token->location,
9703 " declarations and function-definitions is forbidden");
9705 /* Otherwise, we're done with the list of declarators. */
9708 pop_deferring_access_checks ();
9712 /* The next token should be either a `,' or a `;'. */
9713 token = cp_lexer_peek_token (parser->lexer);
9714 /* If it's a `,', there are more declarators to come. */
9715 if (token->type == CPP_COMMA)
9716 /* will be consumed next time around */;
9717 /* If it's a `;', we are done. */
9718 else if (token->type == CPP_SEMICOLON)
9720 /* Anything else is an error. */
9723 /* If we have already issued an error message we don't need
9724 to issue another one. */
9725 if (decl != error_mark_node
9726 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9727 cp_parser_error (parser, "expected %<,%> or %<;%>");
9728 /* Skip tokens until we reach the end of the statement. */
9729 cp_parser_skip_to_end_of_statement (parser);
9730 /* If the next token is now a `;', consume it. */
9731 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9732 cp_lexer_consume_token (parser->lexer);
9735 /* After the first time around, a function-definition is not
9736 allowed -- even if it was OK at first. For example:
9741 function_definition_allowed_p = false;
9744 /* Issue an error message if no declarators are present, and the
9745 decl-specifier-seq does not itself declare a class or
9747 if (!saw_declarator)
9749 if (cp_parser_declares_only_class_p (parser))
9750 shadow_tag (&decl_specifiers);
9751 /* Perform any deferred access checks. */
9752 perform_deferred_access_checks ();
9755 /* Consume the `;'. */
9756 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9759 pop_deferring_access_checks ();
9762 /* Parse a decl-specifier-seq.
9765 decl-specifier-seq [opt] decl-specifier
9768 storage-class-specifier
9779 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9781 The parser flags FLAGS is used to control type-specifier parsing.
9783 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9786 1: one of the decl-specifiers is an elaborated-type-specifier
9787 (i.e., a type declaration)
9788 2: one of the decl-specifiers is an enum-specifier or a
9789 class-specifier (i.e., a type definition)
9794 cp_parser_decl_specifier_seq (cp_parser* parser,
9795 cp_parser_flags flags,
9796 cp_decl_specifier_seq *decl_specs,
9797 int* declares_class_or_enum)
9799 bool constructor_possible_p = !parser->in_declarator_p;
9800 cp_token *start_token = NULL;
9802 /* Clear DECL_SPECS. */
9803 clear_decl_specs (decl_specs);
9805 /* Assume no class or enumeration type is declared. */
9806 *declares_class_or_enum = 0;
9808 /* Keep reading specifiers until there are no more to read. */
9812 bool found_decl_spec;
9815 /* Peek at the next token. */
9816 token = cp_lexer_peek_token (parser->lexer);
9818 /* Save the first token of the decl spec list for error
9821 start_token = token;
9822 /* Handle attributes. */
9823 if (token->keyword == RID_ATTRIBUTE)
9825 /* Parse the attributes. */
9826 decl_specs->attributes
9827 = chainon (decl_specs->attributes,
9828 cp_parser_attributes_opt (parser));
9831 /* Assume we will find a decl-specifier keyword. */
9832 found_decl_spec = true;
9833 /* If the next token is an appropriate keyword, we can simply
9834 add it to the list. */
9835 switch (token->keyword)
9841 if (!at_class_scope_p ())
9843 error_at (token->location, "%<friend%> used outside of class");
9844 cp_lexer_purge_token (parser->lexer);
9848 ++decl_specs->specs[(int) ds_friend];
9849 /* Consume the token. */
9850 cp_lexer_consume_token (parser->lexer);
9855 ++decl_specs->specs[(int) ds_constexpr];
9856 cp_lexer_consume_token (parser->lexer);
9859 /* function-specifier:
9866 cp_parser_function_specifier_opt (parser, decl_specs);
9872 ++decl_specs->specs[(int) ds_typedef];
9873 /* Consume the token. */
9874 cp_lexer_consume_token (parser->lexer);
9875 /* A constructor declarator cannot appear in a typedef. */
9876 constructor_possible_p = false;
9877 /* The "typedef" keyword can only occur in a declaration; we
9878 may as well commit at this point. */
9879 cp_parser_commit_to_tentative_parse (parser);
9881 if (decl_specs->storage_class != sc_none)
9882 decl_specs->conflicting_specifiers_p = true;
9885 /* storage-class-specifier:
9895 if (cxx_dialect == cxx98)
9897 /* Consume the token. */
9898 cp_lexer_consume_token (parser->lexer);
9900 /* Complain about `auto' as a storage specifier, if
9901 we're complaining about C++0x compatibility. */
9902 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9903 " will change meaning in C++0x; please remove it");
9905 /* Set the storage class anyway. */
9906 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9910 /* C++0x auto type-specifier. */
9911 found_decl_spec = false;
9918 /* Consume the token. */
9919 cp_lexer_consume_token (parser->lexer);
9920 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9924 /* Consume the token. */
9925 cp_lexer_consume_token (parser->lexer);
9926 ++decl_specs->specs[(int) ds_thread];
9930 /* We did not yet find a decl-specifier yet. */
9931 found_decl_spec = false;
9936 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9937 && token->keyword != RID_CONSTEXPR)
9938 error ("decl-specifier invalid in condition");
9940 /* Constructors are a special case. The `S' in `S()' is not a
9941 decl-specifier; it is the beginning of the declarator. */
9944 && constructor_possible_p
9945 && (cp_parser_constructor_declarator_p
9946 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9948 /* If we don't have a DECL_SPEC yet, then we must be looking at
9949 a type-specifier. */
9950 if (!found_decl_spec && !constructor_p)
9952 int decl_spec_declares_class_or_enum;
9953 bool is_cv_qualifier;
9957 = cp_parser_type_specifier (parser, flags,
9959 /*is_declaration=*/true,
9960 &decl_spec_declares_class_or_enum,
9962 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9964 /* If this type-specifier referenced a user-defined type
9965 (a typedef, class-name, etc.), then we can't allow any
9966 more such type-specifiers henceforth.
9970 The longest sequence of decl-specifiers that could
9971 possibly be a type name is taken as the
9972 decl-specifier-seq of a declaration. The sequence shall
9973 be self-consistent as described below.
9977 As a general rule, at most one type-specifier is allowed
9978 in the complete decl-specifier-seq of a declaration. The
9979 only exceptions are the following:
9981 -- const or volatile can be combined with any other
9984 -- signed or unsigned can be combined with char, long,
9992 void g (const int Pc);
9994 Here, Pc is *not* part of the decl-specifier seq; it's
9995 the declarator. Therefore, once we see a type-specifier
9996 (other than a cv-qualifier), we forbid any additional
9997 user-defined types. We *do* still allow things like `int
9998 int' to be considered a decl-specifier-seq, and issue the
9999 error message later. */
10000 if (type_spec && !is_cv_qualifier)
10001 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10002 /* A constructor declarator cannot follow a type-specifier. */
10005 constructor_possible_p = false;
10006 found_decl_spec = true;
10007 if (!is_cv_qualifier)
10008 decl_specs->any_type_specifiers_p = true;
10012 /* If we still do not have a DECL_SPEC, then there are no more
10013 decl-specifiers. */
10014 if (!found_decl_spec)
10017 decl_specs->any_specifiers_p = true;
10018 /* After we see one decl-specifier, further decl-specifiers are
10019 always optional. */
10020 flags |= CP_PARSER_FLAGS_OPTIONAL;
10023 cp_parser_check_decl_spec (decl_specs, start_token->location);
10025 /* Don't allow a friend specifier with a class definition. */
10026 if (decl_specs->specs[(int) ds_friend] != 0
10027 && (*declares_class_or_enum & 2))
10028 error_at (start_token->location,
10029 "class definition may not be declared a friend");
10032 /* Parse an (optional) storage-class-specifier.
10034 storage-class-specifier:
10043 storage-class-specifier:
10046 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10049 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10051 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10054 if (cxx_dialect != cxx98)
10056 /* Fall through for C++98. */
10063 /* Consume the token. */
10064 return cp_lexer_consume_token (parser->lexer)->u.value;
10071 /* Parse an (optional) function-specifier.
10073 function-specifier:
10078 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10079 Updates DECL_SPECS, if it is non-NULL. */
10082 cp_parser_function_specifier_opt (cp_parser* parser,
10083 cp_decl_specifier_seq *decl_specs)
10085 cp_token *token = cp_lexer_peek_token (parser->lexer);
10086 switch (token->keyword)
10090 ++decl_specs->specs[(int) ds_inline];
10094 /* 14.5.2.3 [temp.mem]
10096 A member function template shall not be virtual. */
10097 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10098 error_at (token->location, "templates may not be %<virtual%>");
10099 else if (decl_specs)
10100 ++decl_specs->specs[(int) ds_virtual];
10105 ++decl_specs->specs[(int) ds_explicit];
10112 /* Consume the token. */
10113 return cp_lexer_consume_token (parser->lexer)->u.value;
10116 /* Parse a linkage-specification.
10118 linkage-specification:
10119 extern string-literal { declaration-seq [opt] }
10120 extern string-literal declaration */
10123 cp_parser_linkage_specification (cp_parser* parser)
10127 /* Look for the `extern' keyword. */
10128 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10130 /* Look for the string-literal. */
10131 linkage = cp_parser_string_literal (parser, false, false);
10133 /* Transform the literal into an identifier. If the literal is a
10134 wide-character string, or contains embedded NULs, then we can't
10135 handle it as the user wants. */
10136 if (strlen (TREE_STRING_POINTER (linkage))
10137 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10139 cp_parser_error (parser, "invalid linkage-specification");
10140 /* Assume C++ linkage. */
10141 linkage = lang_name_cplusplus;
10144 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10146 /* We're now using the new linkage. */
10147 push_lang_context (linkage);
10149 /* If the next token is a `{', then we're using the first
10151 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10153 /* Consume the `{' token. */
10154 cp_lexer_consume_token (parser->lexer);
10155 /* Parse the declarations. */
10156 cp_parser_declaration_seq_opt (parser);
10157 /* Look for the closing `}'. */
10158 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10160 /* Otherwise, there's just one declaration. */
10163 bool saved_in_unbraced_linkage_specification_p;
10165 saved_in_unbraced_linkage_specification_p
10166 = parser->in_unbraced_linkage_specification_p;
10167 parser->in_unbraced_linkage_specification_p = true;
10168 cp_parser_declaration (parser);
10169 parser->in_unbraced_linkage_specification_p
10170 = saved_in_unbraced_linkage_specification_p;
10173 /* We're done with the linkage-specification. */
10174 pop_lang_context ();
10177 /* Parse a static_assert-declaration.
10179 static_assert-declaration:
10180 static_assert ( constant-expression , string-literal ) ;
10182 If MEMBER_P, this static_assert is a class member. */
10185 cp_parser_static_assert(cp_parser *parser, bool member_p)
10190 location_t saved_loc;
10192 /* Peek at the `static_assert' token so we can keep track of exactly
10193 where the static assertion started. */
10194 token = cp_lexer_peek_token (parser->lexer);
10195 saved_loc = token->location;
10197 /* Look for the `static_assert' keyword. */
10198 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10202 /* We know we are in a static assertion; commit to any tentative
10204 if (cp_parser_parsing_tentatively (parser))
10205 cp_parser_commit_to_tentative_parse (parser);
10207 /* Parse the `(' starting the static assertion condition. */
10208 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10210 /* Parse the constant-expression. */
10212 cp_parser_constant_expression (parser,
10213 /*allow_non_constant_p=*/false,
10214 /*non_constant_p=*/NULL);
10216 /* Parse the separating `,'. */
10217 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10219 /* Parse the string-literal message. */
10220 message = cp_parser_string_literal (parser,
10221 /*translate=*/false,
10224 /* A `)' completes the static assertion. */
10225 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10226 cp_parser_skip_to_closing_parenthesis (parser,
10227 /*recovering=*/true,
10228 /*or_comma=*/false,
10229 /*consume_paren=*/true);
10231 /* A semicolon terminates the declaration. */
10232 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10234 /* Complete the static assertion, which may mean either processing
10235 the static assert now or saving it for template instantiation. */
10236 finish_static_assert (condition, message, saved_loc, member_p);
10239 /* Parse a `decltype' type. Returns the type.
10241 simple-type-specifier:
10242 decltype ( expression ) */
10245 cp_parser_decltype (cp_parser *parser)
10248 bool id_expression_or_member_access_p = false;
10249 const char *saved_message;
10250 bool saved_integral_constant_expression_p;
10251 bool saved_non_integral_constant_expression_p;
10252 cp_token *id_expr_start_token;
10254 /* Look for the `decltype' token. */
10255 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10256 return error_mark_node;
10258 /* Types cannot be defined in a `decltype' expression. Save away the
10260 saved_message = parser->type_definition_forbidden_message;
10262 /* And create the new one. */
10263 parser->type_definition_forbidden_message
10264 = G_("types may not be defined in %<decltype%> expressions");
10266 /* The restrictions on constant-expressions do not apply inside
10267 decltype expressions. */
10268 saved_integral_constant_expression_p
10269 = parser->integral_constant_expression_p;
10270 saved_non_integral_constant_expression_p
10271 = parser->non_integral_constant_expression_p;
10272 parser->integral_constant_expression_p = false;
10274 /* Do not actually evaluate the expression. */
10275 ++cp_unevaluated_operand;
10277 /* Do not warn about problems with the expression. */
10278 ++c_inhibit_evaluation_warnings;
10280 /* Parse the opening `('. */
10281 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10282 return error_mark_node;
10284 /* First, try parsing an id-expression. */
10285 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10286 cp_parser_parse_tentatively (parser);
10287 expr = cp_parser_id_expression (parser,
10288 /*template_keyword_p=*/false,
10289 /*check_dependency_p=*/true,
10290 /*template_p=*/NULL,
10291 /*declarator_p=*/false,
10292 /*optional_p=*/false);
10294 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10296 bool non_integral_constant_expression_p = false;
10297 tree id_expression = expr;
10299 const char *error_msg;
10301 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10302 /* Lookup the name we got back from the id-expression. */
10303 expr = cp_parser_lookup_name (parser, expr,
10305 /*is_template=*/false,
10306 /*is_namespace=*/false,
10307 /*check_dependency=*/true,
10308 /*ambiguous_decls=*/NULL,
10309 id_expr_start_token->location);
10312 && expr != error_mark_node
10313 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10314 && TREE_CODE (expr) != TYPE_DECL
10315 && (TREE_CODE (expr) != BIT_NOT_EXPR
10316 || !TYPE_P (TREE_OPERAND (expr, 0)))
10317 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10319 /* Complete lookup of the id-expression. */
10320 expr = (finish_id_expression
10321 (id_expression, expr, parser->scope, &idk,
10322 /*integral_constant_expression_p=*/false,
10323 /*allow_non_integral_constant_expression_p=*/true,
10324 &non_integral_constant_expression_p,
10325 /*template_p=*/false,
10327 /*address_p=*/false,
10328 /*template_arg_p=*/false,
10330 id_expr_start_token->location));
10332 if (expr == error_mark_node)
10333 /* We found an id-expression, but it was something that we
10334 should not have found. This is an error, not something
10335 we can recover from, so note that we found an
10336 id-expression and we'll recover as gracefully as
10338 id_expression_or_member_access_p = true;
10342 && expr != error_mark_node
10343 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10344 /* We have an id-expression. */
10345 id_expression_or_member_access_p = true;
10348 if (!id_expression_or_member_access_p)
10350 /* Abort the id-expression parse. */
10351 cp_parser_abort_tentative_parse (parser);
10353 /* Parsing tentatively, again. */
10354 cp_parser_parse_tentatively (parser);
10356 /* Parse a class member access. */
10357 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10359 /*member_access_only_p=*/true, NULL);
10362 && expr != error_mark_node
10363 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10364 /* We have an id-expression. */
10365 id_expression_or_member_access_p = true;
10368 if (id_expression_or_member_access_p)
10369 /* We have parsed the complete id-expression or member access. */
10370 cp_parser_parse_definitely (parser);
10373 bool saved_greater_than_is_operator_p;
10375 /* Abort our attempt to parse an id-expression or member access
10377 cp_parser_abort_tentative_parse (parser);
10379 /* Within a parenthesized expression, a `>' token is always
10380 the greater-than operator. */
10381 saved_greater_than_is_operator_p
10382 = parser->greater_than_is_operator_p;
10383 parser->greater_than_is_operator_p = true;
10385 /* Parse a full expression. */
10386 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10388 /* The `>' token might be the end of a template-id or
10389 template-parameter-list now. */
10390 parser->greater_than_is_operator_p
10391 = saved_greater_than_is_operator_p;
10394 /* Go back to evaluating expressions. */
10395 --cp_unevaluated_operand;
10396 --c_inhibit_evaluation_warnings;
10398 /* Restore the old message and the integral constant expression
10400 parser->type_definition_forbidden_message = saved_message;
10401 parser->integral_constant_expression_p
10402 = saved_integral_constant_expression_p;
10403 parser->non_integral_constant_expression_p
10404 = saved_non_integral_constant_expression_p;
10406 if (expr == error_mark_node)
10408 /* Skip everything up to the closing `)'. */
10409 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10410 /*consume_paren=*/true);
10411 return error_mark_node;
10414 /* Parse to the closing `)'. */
10415 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10417 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10418 /*consume_paren=*/true);
10419 return error_mark_node;
10422 return finish_decltype_type (expr, id_expression_or_member_access_p);
10425 /* Special member functions [gram.special] */
10427 /* Parse a conversion-function-id.
10429 conversion-function-id:
10430 operator conversion-type-id
10432 Returns an IDENTIFIER_NODE representing the operator. */
10435 cp_parser_conversion_function_id (cp_parser* parser)
10439 tree saved_qualifying_scope;
10440 tree saved_object_scope;
10441 tree pushed_scope = NULL_TREE;
10443 /* Look for the `operator' token. */
10444 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10445 return error_mark_node;
10446 /* When we parse the conversion-type-id, the current scope will be
10447 reset. However, we need that information in able to look up the
10448 conversion function later, so we save it here. */
10449 saved_scope = parser->scope;
10450 saved_qualifying_scope = parser->qualifying_scope;
10451 saved_object_scope = parser->object_scope;
10452 /* We must enter the scope of the class so that the names of
10453 entities declared within the class are available in the
10454 conversion-type-id. For example, consider:
10461 S::operator I() { ... }
10463 In order to see that `I' is a type-name in the definition, we
10464 must be in the scope of `S'. */
10466 pushed_scope = push_scope (saved_scope);
10467 /* Parse the conversion-type-id. */
10468 type = cp_parser_conversion_type_id (parser);
10469 /* Leave the scope of the class, if any. */
10471 pop_scope (pushed_scope);
10472 /* Restore the saved scope. */
10473 parser->scope = saved_scope;
10474 parser->qualifying_scope = saved_qualifying_scope;
10475 parser->object_scope = saved_object_scope;
10476 /* If the TYPE is invalid, indicate failure. */
10477 if (type == error_mark_node)
10478 return error_mark_node;
10479 return mangle_conv_op_name_for_type (type);
10482 /* Parse a conversion-type-id:
10484 conversion-type-id:
10485 type-specifier-seq conversion-declarator [opt]
10487 Returns the TYPE specified. */
10490 cp_parser_conversion_type_id (cp_parser* parser)
10493 cp_decl_specifier_seq type_specifiers;
10494 cp_declarator *declarator;
10495 tree type_specified;
10497 /* Parse the attributes. */
10498 attributes = cp_parser_attributes_opt (parser);
10499 /* Parse the type-specifiers. */
10500 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10501 /*is_trailing_return=*/false,
10503 /* If that didn't work, stop. */
10504 if (type_specifiers.type == error_mark_node)
10505 return error_mark_node;
10506 /* Parse the conversion-declarator. */
10507 declarator = cp_parser_conversion_declarator_opt (parser);
10509 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10510 /*initialized=*/0, &attributes);
10512 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10514 /* Don't give this error when parsing tentatively. This happens to
10515 work because we always parse this definitively once. */
10516 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10517 && type_uses_auto (type_specified))
10519 error ("invalid use of %<auto%> in conversion operator");
10520 return error_mark_node;
10523 return type_specified;
10526 /* Parse an (optional) conversion-declarator.
10528 conversion-declarator:
10529 ptr-operator conversion-declarator [opt]
10533 static cp_declarator *
10534 cp_parser_conversion_declarator_opt (cp_parser* parser)
10536 enum tree_code code;
10538 cp_cv_quals cv_quals;
10540 /* We don't know if there's a ptr-operator next, or not. */
10541 cp_parser_parse_tentatively (parser);
10542 /* Try the ptr-operator. */
10543 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10544 /* If it worked, look for more conversion-declarators. */
10545 if (cp_parser_parse_definitely (parser))
10547 cp_declarator *declarator;
10549 /* Parse another optional declarator. */
10550 declarator = cp_parser_conversion_declarator_opt (parser);
10552 return cp_parser_make_indirect_declarator
10553 (code, class_type, cv_quals, declarator);
10559 /* Parse an (optional) ctor-initializer.
10562 : mem-initializer-list
10564 Returns TRUE iff the ctor-initializer was actually present. */
10567 cp_parser_ctor_initializer_opt (cp_parser* parser)
10569 /* If the next token is not a `:', then there is no
10570 ctor-initializer. */
10571 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10573 /* Do default initialization of any bases and members. */
10574 if (DECL_CONSTRUCTOR_P (current_function_decl))
10575 finish_mem_initializers (NULL_TREE);
10580 /* Consume the `:' token. */
10581 cp_lexer_consume_token (parser->lexer);
10582 /* And the mem-initializer-list. */
10583 cp_parser_mem_initializer_list (parser);
10588 /* Parse a mem-initializer-list.
10590 mem-initializer-list:
10591 mem-initializer ... [opt]
10592 mem-initializer ... [opt] , mem-initializer-list */
10595 cp_parser_mem_initializer_list (cp_parser* parser)
10597 tree mem_initializer_list = NULL_TREE;
10598 cp_token *token = cp_lexer_peek_token (parser->lexer);
10600 /* Let the semantic analysis code know that we are starting the
10601 mem-initializer-list. */
10602 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10603 error_at (token->location,
10604 "only constructors take member initializers");
10606 /* Loop through the list. */
10609 tree mem_initializer;
10611 token = cp_lexer_peek_token (parser->lexer);
10612 /* Parse the mem-initializer. */
10613 mem_initializer = cp_parser_mem_initializer (parser);
10614 /* If the next token is a `...', we're expanding member initializers. */
10615 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10617 /* Consume the `...'. */
10618 cp_lexer_consume_token (parser->lexer);
10620 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10621 can be expanded but members cannot. */
10622 if (mem_initializer != error_mark_node
10623 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10625 error_at (token->location,
10626 "cannot expand initializer for member %<%D%>",
10627 TREE_PURPOSE (mem_initializer));
10628 mem_initializer = error_mark_node;
10631 /* Construct the pack expansion type. */
10632 if (mem_initializer != error_mark_node)
10633 mem_initializer = make_pack_expansion (mem_initializer);
10635 /* Add it to the list, unless it was erroneous. */
10636 if (mem_initializer != error_mark_node)
10638 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10639 mem_initializer_list = mem_initializer;
10641 /* If the next token is not a `,', we're done. */
10642 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10644 /* Consume the `,' token. */
10645 cp_lexer_consume_token (parser->lexer);
10648 /* Perform semantic analysis. */
10649 if (DECL_CONSTRUCTOR_P (current_function_decl))
10650 finish_mem_initializers (mem_initializer_list);
10653 /* Parse a mem-initializer.
10656 mem-initializer-id ( expression-list [opt] )
10657 mem-initializer-id braced-init-list
10662 ( expression-list [opt] )
10664 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10665 class) or FIELD_DECL (for a non-static data member) to initialize;
10666 the TREE_VALUE is the expression-list. An empty initialization
10667 list is represented by void_list_node. */
10670 cp_parser_mem_initializer (cp_parser* parser)
10672 tree mem_initializer_id;
10673 tree expression_list;
10675 cp_token *token = cp_lexer_peek_token (parser->lexer);
10677 /* Find out what is being initialized. */
10678 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10680 permerror (token->location,
10681 "anachronistic old-style base class initializer");
10682 mem_initializer_id = NULL_TREE;
10686 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10687 if (mem_initializer_id == error_mark_node)
10688 return mem_initializer_id;
10690 member = expand_member_init (mem_initializer_id);
10691 if (member && !DECL_P (member))
10692 in_base_initializer = 1;
10694 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10696 bool expr_non_constant_p;
10697 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10698 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10699 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10700 expression_list = build_tree_list (NULL_TREE, expression_list);
10705 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10707 /*allow_expansion_p=*/true,
10708 /*non_constant_p=*/NULL);
10710 return error_mark_node;
10711 expression_list = build_tree_list_vec (vec);
10712 release_tree_vector (vec);
10715 if (expression_list == error_mark_node)
10716 return error_mark_node;
10717 if (!expression_list)
10718 expression_list = void_type_node;
10720 in_base_initializer = 0;
10722 return member ? build_tree_list (member, expression_list) : error_mark_node;
10725 /* Parse a mem-initializer-id.
10727 mem-initializer-id:
10728 :: [opt] nested-name-specifier [opt] class-name
10731 Returns a TYPE indicating the class to be initializer for the first
10732 production. Returns an IDENTIFIER_NODE indicating the data member
10733 to be initialized for the second production. */
10736 cp_parser_mem_initializer_id (cp_parser* parser)
10738 bool global_scope_p;
10739 bool nested_name_specifier_p;
10740 bool template_p = false;
10743 cp_token *token = cp_lexer_peek_token (parser->lexer);
10745 /* `typename' is not allowed in this context ([temp.res]). */
10746 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10748 error_at (token->location,
10749 "keyword %<typename%> not allowed in this context (a qualified "
10750 "member initializer is implicitly a type)");
10751 cp_lexer_consume_token (parser->lexer);
10753 /* Look for the optional `::' operator. */
10755 = (cp_parser_global_scope_opt (parser,
10756 /*current_scope_valid_p=*/false)
10758 /* Look for the optional nested-name-specifier. The simplest way to
10763 The keyword `typename' is not permitted in a base-specifier or
10764 mem-initializer; in these contexts a qualified name that
10765 depends on a template-parameter is implicitly assumed to be a
10768 is to assume that we have seen the `typename' keyword at this
10770 nested_name_specifier_p
10771 = (cp_parser_nested_name_specifier_opt (parser,
10772 /*typename_keyword_p=*/true,
10773 /*check_dependency_p=*/true,
10775 /*is_declaration=*/true)
10777 if (nested_name_specifier_p)
10778 template_p = cp_parser_optional_template_keyword (parser);
10779 /* If there is a `::' operator or a nested-name-specifier, then we
10780 are definitely looking for a class-name. */
10781 if (global_scope_p || nested_name_specifier_p)
10782 return cp_parser_class_name (parser,
10783 /*typename_keyword_p=*/true,
10784 /*template_keyword_p=*/template_p,
10786 /*check_dependency_p=*/true,
10787 /*class_head_p=*/false,
10788 /*is_declaration=*/true);
10789 /* Otherwise, we could also be looking for an ordinary identifier. */
10790 cp_parser_parse_tentatively (parser);
10791 /* Try a class-name. */
10792 id = cp_parser_class_name (parser,
10793 /*typename_keyword_p=*/true,
10794 /*template_keyword_p=*/false,
10796 /*check_dependency_p=*/true,
10797 /*class_head_p=*/false,
10798 /*is_declaration=*/true);
10799 /* If we found one, we're done. */
10800 if (cp_parser_parse_definitely (parser))
10802 /* Otherwise, look for an ordinary identifier. */
10803 return cp_parser_identifier (parser);
10806 /* Overloading [gram.over] */
10808 /* Parse an operator-function-id.
10810 operator-function-id:
10813 Returns an IDENTIFIER_NODE for the operator which is a
10814 human-readable spelling of the identifier, e.g., `operator +'. */
10817 cp_parser_operator_function_id (cp_parser* parser)
10819 /* Look for the `operator' keyword. */
10820 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10821 return error_mark_node;
10822 /* And then the name of the operator itself. */
10823 return cp_parser_operator (parser);
10826 /* Parse an operator.
10829 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10830 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10831 || ++ -- , ->* -> () []
10838 Returns an IDENTIFIER_NODE for the operator which is a
10839 human-readable spelling of the identifier, e.g., `operator +'. */
10842 cp_parser_operator (cp_parser* parser)
10844 tree id = NULL_TREE;
10847 /* Peek at the next token. */
10848 token = cp_lexer_peek_token (parser->lexer);
10849 /* Figure out which operator we have. */
10850 switch (token->type)
10856 /* The keyword should be either `new' or `delete'. */
10857 if (token->keyword == RID_NEW)
10859 else if (token->keyword == RID_DELETE)
10864 /* Consume the `new' or `delete' token. */
10865 cp_lexer_consume_token (parser->lexer);
10867 /* Peek at the next token. */
10868 token = cp_lexer_peek_token (parser->lexer);
10869 /* If it's a `[' token then this is the array variant of the
10871 if (token->type == CPP_OPEN_SQUARE)
10873 /* Consume the `[' token. */
10874 cp_lexer_consume_token (parser->lexer);
10875 /* Look for the `]' token. */
10876 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10877 id = ansi_opname (op == NEW_EXPR
10878 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10880 /* Otherwise, we have the non-array variant. */
10882 id = ansi_opname (op);
10888 id = ansi_opname (PLUS_EXPR);
10892 id = ansi_opname (MINUS_EXPR);
10896 id = ansi_opname (MULT_EXPR);
10900 id = ansi_opname (TRUNC_DIV_EXPR);
10904 id = ansi_opname (TRUNC_MOD_EXPR);
10908 id = ansi_opname (BIT_XOR_EXPR);
10912 id = ansi_opname (BIT_AND_EXPR);
10916 id = ansi_opname (BIT_IOR_EXPR);
10920 id = ansi_opname (BIT_NOT_EXPR);
10924 id = ansi_opname (TRUTH_NOT_EXPR);
10928 id = ansi_assopname (NOP_EXPR);
10932 id = ansi_opname (LT_EXPR);
10936 id = ansi_opname (GT_EXPR);
10940 id = ansi_assopname (PLUS_EXPR);
10944 id = ansi_assopname (MINUS_EXPR);
10948 id = ansi_assopname (MULT_EXPR);
10952 id = ansi_assopname (TRUNC_DIV_EXPR);
10956 id = ansi_assopname (TRUNC_MOD_EXPR);
10960 id = ansi_assopname (BIT_XOR_EXPR);
10964 id = ansi_assopname (BIT_AND_EXPR);
10968 id = ansi_assopname (BIT_IOR_EXPR);
10972 id = ansi_opname (LSHIFT_EXPR);
10976 id = ansi_opname (RSHIFT_EXPR);
10979 case CPP_LSHIFT_EQ:
10980 id = ansi_assopname (LSHIFT_EXPR);
10983 case CPP_RSHIFT_EQ:
10984 id = ansi_assopname (RSHIFT_EXPR);
10988 id = ansi_opname (EQ_EXPR);
10992 id = ansi_opname (NE_EXPR);
10996 id = ansi_opname (LE_EXPR);
10999 case CPP_GREATER_EQ:
11000 id = ansi_opname (GE_EXPR);
11004 id = ansi_opname (TRUTH_ANDIF_EXPR);
11008 id = ansi_opname (TRUTH_ORIF_EXPR);
11011 case CPP_PLUS_PLUS:
11012 id = ansi_opname (POSTINCREMENT_EXPR);
11015 case CPP_MINUS_MINUS:
11016 id = ansi_opname (PREDECREMENT_EXPR);
11020 id = ansi_opname (COMPOUND_EXPR);
11023 case CPP_DEREF_STAR:
11024 id = ansi_opname (MEMBER_REF);
11028 id = ansi_opname (COMPONENT_REF);
11031 case CPP_OPEN_PAREN:
11032 /* Consume the `('. */
11033 cp_lexer_consume_token (parser->lexer);
11034 /* Look for the matching `)'. */
11035 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11036 return ansi_opname (CALL_EXPR);
11038 case CPP_OPEN_SQUARE:
11039 /* Consume the `['. */
11040 cp_lexer_consume_token (parser->lexer);
11041 /* Look for the matching `]'. */
11042 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11043 return ansi_opname (ARRAY_REF);
11046 /* Anything else is an error. */
11050 /* If we have selected an identifier, we need to consume the
11053 cp_lexer_consume_token (parser->lexer);
11054 /* Otherwise, no valid operator name was present. */
11057 cp_parser_error (parser, "expected operator");
11058 id = error_mark_node;
11064 /* Parse a template-declaration.
11066 template-declaration:
11067 export [opt] template < template-parameter-list > declaration
11069 If MEMBER_P is TRUE, this template-declaration occurs within a
11072 The grammar rule given by the standard isn't correct. What
11073 is really meant is:
11075 template-declaration:
11076 export [opt] template-parameter-list-seq
11077 decl-specifier-seq [opt] init-declarator [opt] ;
11078 export [opt] template-parameter-list-seq
11079 function-definition
11081 template-parameter-list-seq:
11082 template-parameter-list-seq [opt]
11083 template < template-parameter-list > */
11086 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11088 /* Check for `export'. */
11089 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11091 /* Consume the `export' token. */
11092 cp_lexer_consume_token (parser->lexer);
11093 /* Warn that we do not support `export'. */
11094 warning (0, "keyword %<export%> not implemented, and will be ignored");
11097 cp_parser_template_declaration_after_export (parser, member_p);
11100 /* Parse a template-parameter-list.
11102 template-parameter-list:
11104 template-parameter-list , template-parameter
11106 Returns a TREE_LIST. Each node represents a template parameter.
11107 The nodes are connected via their TREE_CHAINs. */
11110 cp_parser_template_parameter_list (cp_parser* parser)
11112 tree parameter_list = NULL_TREE;
11114 begin_template_parm_list ();
11116 /* The loop below parses the template parms. We first need to know
11117 the total number of template parms to be able to compute proper
11118 canonical types of each dependent type. So after the loop, when
11119 we know the total number of template parms,
11120 end_template_parm_list computes the proper canonical types and
11121 fixes up the dependent types accordingly. */
11126 bool is_parameter_pack;
11127 location_t parm_loc;
11129 /* Parse the template-parameter. */
11130 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11131 parameter = cp_parser_template_parameter (parser,
11133 &is_parameter_pack);
11134 /* Add it to the list. */
11135 if (parameter != error_mark_node)
11136 parameter_list = process_template_parm (parameter_list,
11144 tree err_parm = build_tree_list (parameter, parameter);
11145 parameter_list = chainon (parameter_list, err_parm);
11148 /* If the next token is not a `,', we're done. */
11149 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11151 /* Otherwise, consume the `,' token. */
11152 cp_lexer_consume_token (parser->lexer);
11155 return end_template_parm_list (parameter_list);
11158 /* Parse a template-parameter.
11160 template-parameter:
11162 parameter-declaration
11164 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11165 the parameter. The TREE_PURPOSE is the default value, if any.
11166 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11167 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11168 set to true iff this parameter is a parameter pack. */
11171 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11172 bool *is_parameter_pack)
11175 cp_parameter_declarator *parameter_declarator;
11176 cp_declarator *id_declarator;
11179 /* Assume it is a type parameter or a template parameter. */
11180 *is_non_type = false;
11181 /* Assume it not a parameter pack. */
11182 *is_parameter_pack = false;
11183 /* Peek at the next token. */
11184 token = cp_lexer_peek_token (parser->lexer);
11185 /* If it is `class' or `template', we have a type-parameter. */
11186 if (token->keyword == RID_TEMPLATE)
11187 return cp_parser_type_parameter (parser, is_parameter_pack);
11188 /* If it is `class' or `typename' we do not know yet whether it is a
11189 type parameter or a non-type parameter. Consider:
11191 template <typename T, typename T::X X> ...
11195 template <class C, class D*> ...
11197 Here, the first parameter is a type parameter, and the second is
11198 a non-type parameter. We can tell by looking at the token after
11199 the identifier -- if it is a `,', `=', or `>' then we have a type
11201 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11203 /* Peek at the token after `class' or `typename'. */
11204 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11205 /* If it's an ellipsis, we have a template type parameter
11207 if (token->type == CPP_ELLIPSIS)
11208 return cp_parser_type_parameter (parser, is_parameter_pack);
11209 /* If it's an identifier, skip it. */
11210 if (token->type == CPP_NAME)
11211 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11212 /* Now, see if the token looks like the end of a template
11214 if (token->type == CPP_COMMA
11215 || token->type == CPP_EQ
11216 || token->type == CPP_GREATER)
11217 return cp_parser_type_parameter (parser, is_parameter_pack);
11220 /* Otherwise, it is a non-type parameter.
11224 When parsing a default template-argument for a non-type
11225 template-parameter, the first non-nested `>' is taken as the end
11226 of the template parameter-list rather than a greater-than
11228 *is_non_type = true;
11229 parameter_declarator
11230 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11231 /*parenthesized_p=*/NULL);
11233 /* If the parameter declaration is marked as a parameter pack, set
11234 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11235 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11237 if (parameter_declarator
11238 && parameter_declarator->declarator
11239 && parameter_declarator->declarator->parameter_pack_p)
11241 *is_parameter_pack = true;
11242 parameter_declarator->declarator->parameter_pack_p = false;
11245 /* If the next token is an ellipsis, and we don't already have it
11246 marked as a parameter pack, then we have a parameter pack (that
11247 has no declarator). */
11248 if (!*is_parameter_pack
11249 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11250 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11252 /* Consume the `...'. */
11253 cp_lexer_consume_token (parser->lexer);
11254 maybe_warn_variadic_templates ();
11256 *is_parameter_pack = true;
11258 /* We might end up with a pack expansion as the type of the non-type
11259 template parameter, in which case this is a non-type template
11261 else if (parameter_declarator
11262 && parameter_declarator->decl_specifiers.type
11263 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11265 *is_parameter_pack = true;
11266 parameter_declarator->decl_specifiers.type =
11267 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11270 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11272 /* Parameter packs cannot have default arguments. However, a
11273 user may try to do so, so we'll parse them and give an
11274 appropriate diagnostic here. */
11276 /* Consume the `='. */
11277 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11278 cp_lexer_consume_token (parser->lexer);
11280 /* Find the name of the parameter pack. */
11281 id_declarator = parameter_declarator->declarator;
11282 while (id_declarator && id_declarator->kind != cdk_id)
11283 id_declarator = id_declarator->declarator;
11285 if (id_declarator && id_declarator->kind == cdk_id)
11286 error_at (start_token->location,
11287 "template parameter pack %qD cannot have a default argument",
11288 id_declarator->u.id.unqualified_name);
11290 error_at (start_token->location,
11291 "template parameter pack cannot have a default argument");
11293 /* Parse the default argument, but throw away the result. */
11294 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11297 parm = grokdeclarator (parameter_declarator->declarator,
11298 ¶meter_declarator->decl_specifiers,
11299 TPARM, /*initialized=*/0,
11300 /*attrlist=*/NULL);
11301 if (parm == error_mark_node)
11302 return error_mark_node;
11304 return build_tree_list (parameter_declarator->default_argument, parm);
11307 /* Parse a type-parameter.
11310 class identifier [opt]
11311 class identifier [opt] = type-id
11312 typename identifier [opt]
11313 typename identifier [opt] = type-id
11314 template < template-parameter-list > class identifier [opt]
11315 template < template-parameter-list > class identifier [opt]
11318 GNU Extension (variadic templates):
11321 class ... identifier [opt]
11322 typename ... identifier [opt]
11324 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11325 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11326 the declaration of the parameter.
11328 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11331 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11336 /* Look for a keyword to tell us what kind of parameter this is. */
11337 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11339 return error_mark_node;
11341 switch (token->keyword)
11347 tree default_argument;
11349 /* If the next token is an ellipsis, we have a template
11351 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11353 /* Consume the `...' token. */
11354 cp_lexer_consume_token (parser->lexer);
11355 maybe_warn_variadic_templates ();
11357 *is_parameter_pack = true;
11360 /* If the next token is an identifier, then it names the
11362 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11363 identifier = cp_parser_identifier (parser);
11365 identifier = NULL_TREE;
11367 /* Create the parameter. */
11368 parameter = finish_template_type_parm (class_type_node, identifier);
11370 /* If the next token is an `=', we have a default argument. */
11371 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11373 /* Consume the `=' token. */
11374 cp_lexer_consume_token (parser->lexer);
11375 /* Parse the default-argument. */
11376 push_deferring_access_checks (dk_no_deferred);
11377 default_argument = cp_parser_type_id (parser);
11379 /* Template parameter packs cannot have default
11381 if (*is_parameter_pack)
11384 error_at (token->location,
11385 "template parameter pack %qD cannot have a "
11386 "default argument", identifier);
11388 error_at (token->location,
11389 "template parameter packs cannot have "
11390 "default arguments");
11391 default_argument = NULL_TREE;
11393 pop_deferring_access_checks ();
11396 default_argument = NULL_TREE;
11398 /* Create the combined representation of the parameter and the
11399 default argument. */
11400 parameter = build_tree_list (default_argument, parameter);
11407 tree default_argument;
11409 /* Look for the `<'. */
11410 cp_parser_require (parser, CPP_LESS, RT_LESS);
11411 /* Parse the template-parameter-list. */
11412 cp_parser_template_parameter_list (parser);
11413 /* Look for the `>'. */
11414 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11415 /* Look for the `class' keyword. */
11416 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11417 /* If the next token is an ellipsis, we have a template
11419 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11421 /* Consume the `...' token. */
11422 cp_lexer_consume_token (parser->lexer);
11423 maybe_warn_variadic_templates ();
11425 *is_parameter_pack = true;
11427 /* If the next token is an `=', then there is a
11428 default-argument. If the next token is a `>', we are at
11429 the end of the parameter-list. If the next token is a `,',
11430 then we are at the end of this parameter. */
11431 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11432 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11433 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11435 identifier = cp_parser_identifier (parser);
11436 /* Treat invalid names as if the parameter were nameless. */
11437 if (identifier == error_mark_node)
11438 identifier = NULL_TREE;
11441 identifier = NULL_TREE;
11443 /* Create the template parameter. */
11444 parameter = finish_template_template_parm (class_type_node,
11447 /* If the next token is an `=', then there is a
11448 default-argument. */
11449 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11453 /* Consume the `='. */
11454 cp_lexer_consume_token (parser->lexer);
11455 /* Parse the id-expression. */
11456 push_deferring_access_checks (dk_no_deferred);
11457 /* save token before parsing the id-expression, for error
11459 token = cp_lexer_peek_token (parser->lexer);
11461 = cp_parser_id_expression (parser,
11462 /*template_keyword_p=*/false,
11463 /*check_dependency_p=*/true,
11464 /*template_p=*/&is_template,
11465 /*declarator_p=*/false,
11466 /*optional_p=*/false);
11467 if (TREE_CODE (default_argument) == TYPE_DECL)
11468 /* If the id-expression was a template-id that refers to
11469 a template-class, we already have the declaration here,
11470 so no further lookup is needed. */
11473 /* Look up the name. */
11475 = cp_parser_lookup_name (parser, default_argument,
11477 /*is_template=*/is_template,
11478 /*is_namespace=*/false,
11479 /*check_dependency=*/true,
11480 /*ambiguous_decls=*/NULL,
11482 /* See if the default argument is valid. */
11484 = check_template_template_default_arg (default_argument);
11486 /* Template parameter packs cannot have default
11488 if (*is_parameter_pack)
11491 error_at (token->location,
11492 "template parameter pack %qD cannot "
11493 "have a default argument",
11496 error_at (token->location, "template parameter packs cannot "
11497 "have default arguments");
11498 default_argument = NULL_TREE;
11500 pop_deferring_access_checks ();
11503 default_argument = NULL_TREE;
11505 /* Create the combined representation of the parameter and the
11506 default argument. */
11507 parameter = build_tree_list (default_argument, parameter);
11512 gcc_unreachable ();
11519 /* Parse a template-id.
11522 template-name < template-argument-list [opt] >
11524 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11525 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11526 returned. Otherwise, if the template-name names a function, or set
11527 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11528 names a class, returns a TYPE_DECL for the specialization.
11530 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11531 uninstantiated templates. */
11534 cp_parser_template_id (cp_parser *parser,
11535 bool template_keyword_p,
11536 bool check_dependency_p,
11537 bool is_declaration)
11543 cp_token_position start_of_id = 0;
11544 deferred_access_check *chk;
11545 VEC (deferred_access_check,gc) *access_check;
11546 cp_token *next_token = NULL, *next_token_2 = NULL;
11547 bool is_identifier;
11549 /* If the next token corresponds to a template-id, there is no need
11551 next_token = cp_lexer_peek_token (parser->lexer);
11552 if (next_token->type == CPP_TEMPLATE_ID)
11554 struct tree_check *check_value;
11556 /* Get the stored value. */
11557 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11558 /* Perform any access checks that were deferred. */
11559 access_check = check_value->checks;
11562 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11563 perform_or_defer_access_check (chk->binfo,
11567 /* Return the stored value. */
11568 return check_value->value;
11571 /* Avoid performing name lookup if there is no possibility of
11572 finding a template-id. */
11573 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11574 || (next_token->type == CPP_NAME
11575 && !cp_parser_nth_token_starts_template_argument_list_p
11578 cp_parser_error (parser, "expected template-id");
11579 return error_mark_node;
11582 /* Remember where the template-id starts. */
11583 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11584 start_of_id = cp_lexer_token_position (parser->lexer, false);
11586 push_deferring_access_checks (dk_deferred);
11588 /* Parse the template-name. */
11589 is_identifier = false;
11590 templ = cp_parser_template_name (parser, template_keyword_p,
11591 check_dependency_p,
11594 if (templ == error_mark_node || is_identifier)
11596 pop_deferring_access_checks ();
11600 /* If we find the sequence `[:' after a template-name, it's probably
11601 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11602 parse correctly the argument list. */
11603 next_token = cp_lexer_peek_token (parser->lexer);
11604 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11605 if (next_token->type == CPP_OPEN_SQUARE
11606 && next_token->flags & DIGRAPH
11607 && next_token_2->type == CPP_COLON
11608 && !(next_token_2->flags & PREV_WHITE))
11610 cp_parser_parse_tentatively (parser);
11611 /* Change `:' into `::'. */
11612 next_token_2->type = CPP_SCOPE;
11613 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11615 cp_lexer_consume_token (parser->lexer);
11617 /* Parse the arguments. */
11618 arguments = cp_parser_enclosed_template_argument_list (parser);
11619 if (!cp_parser_parse_definitely (parser))
11621 /* If we couldn't parse an argument list, then we revert our changes
11622 and return simply an error. Maybe this is not a template-id
11624 next_token_2->type = CPP_COLON;
11625 cp_parser_error (parser, "expected %<<%>");
11626 pop_deferring_access_checks ();
11627 return error_mark_node;
11629 /* Otherwise, emit an error about the invalid digraph, but continue
11630 parsing because we got our argument list. */
11631 if (permerror (next_token->location,
11632 "%<<::%> cannot begin a template-argument list"))
11634 static bool hint = false;
11635 inform (next_token->location,
11636 "%<<:%> is an alternate spelling for %<[%>."
11637 " Insert whitespace between %<<%> and %<::%>");
11638 if (!hint && !flag_permissive)
11640 inform (next_token->location, "(if you use %<-fpermissive%>"
11641 " G++ will accept your code)");
11648 /* Look for the `<' that starts the template-argument-list. */
11649 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11651 pop_deferring_access_checks ();
11652 return error_mark_node;
11654 /* Parse the arguments. */
11655 arguments = cp_parser_enclosed_template_argument_list (parser);
11658 /* Build a representation of the specialization. */
11659 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11660 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11661 else if (DECL_CLASS_TEMPLATE_P (templ)
11662 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11664 bool entering_scope;
11665 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11666 template (rather than some instantiation thereof) only if
11667 is not nested within some other construct. For example, in
11668 "template <typename T> void f(T) { A<T>::", A<T> is just an
11669 instantiation of A. */
11670 entering_scope = (template_parm_scope_p ()
11671 && cp_lexer_next_token_is (parser->lexer,
11674 = finish_template_type (templ, arguments, entering_scope);
11678 /* If it's not a class-template or a template-template, it should be
11679 a function-template. */
11680 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11681 || TREE_CODE (templ) == OVERLOAD
11682 || BASELINK_P (templ)));
11684 template_id = lookup_template_function (templ, arguments);
11687 /* If parsing tentatively, replace the sequence of tokens that makes
11688 up the template-id with a CPP_TEMPLATE_ID token. That way,
11689 should we re-parse the token stream, we will not have to repeat
11690 the effort required to do the parse, nor will we issue duplicate
11691 error messages about problems during instantiation of the
11695 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11697 /* Reset the contents of the START_OF_ID token. */
11698 token->type = CPP_TEMPLATE_ID;
11699 /* Retrieve any deferred checks. Do not pop this access checks yet
11700 so the memory will not be reclaimed during token replacing below. */
11701 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11702 token->u.tree_check_value->value = template_id;
11703 token->u.tree_check_value->checks = get_deferred_access_checks ();
11704 token->keyword = RID_MAX;
11706 /* Purge all subsequent tokens. */
11707 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11709 /* ??? Can we actually assume that, if template_id ==
11710 error_mark_node, we will have issued a diagnostic to the
11711 user, as opposed to simply marking the tentative parse as
11713 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11714 error_at (token->location, "parse error in template argument list");
11717 pop_deferring_access_checks ();
11718 return template_id;
11721 /* Parse a template-name.
11726 The standard should actually say:
11730 operator-function-id
11732 A defect report has been filed about this issue.
11734 A conversion-function-id cannot be a template name because they cannot
11735 be part of a template-id. In fact, looking at this code:
11737 a.operator K<int>()
11739 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11740 It is impossible to call a templated conversion-function-id with an
11741 explicit argument list, since the only allowed template parameter is
11742 the type to which it is converting.
11744 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11745 `template' keyword, in a construction like:
11749 In that case `f' is taken to be a template-name, even though there
11750 is no way of knowing for sure.
11752 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11753 name refers to a set of overloaded functions, at least one of which
11754 is a template, or an IDENTIFIER_NODE with the name of the template,
11755 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11756 names are looked up inside uninstantiated templates. */
11759 cp_parser_template_name (cp_parser* parser,
11760 bool template_keyword_p,
11761 bool check_dependency_p,
11762 bool is_declaration,
11763 bool *is_identifier)
11768 cp_token *token = cp_lexer_peek_token (parser->lexer);
11770 /* If the next token is `operator', then we have either an
11771 operator-function-id or a conversion-function-id. */
11772 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11774 /* We don't know whether we're looking at an
11775 operator-function-id or a conversion-function-id. */
11776 cp_parser_parse_tentatively (parser);
11777 /* Try an operator-function-id. */
11778 identifier = cp_parser_operator_function_id (parser);
11779 /* If that didn't work, try a conversion-function-id. */
11780 if (!cp_parser_parse_definitely (parser))
11782 cp_parser_error (parser, "expected template-name");
11783 return error_mark_node;
11786 /* Look for the identifier. */
11788 identifier = cp_parser_identifier (parser);
11790 /* If we didn't find an identifier, we don't have a template-id. */
11791 if (identifier == error_mark_node)
11792 return error_mark_node;
11794 /* If the name immediately followed the `template' keyword, then it
11795 is a template-name. However, if the next token is not `<', then
11796 we do not treat it as a template-name, since it is not being used
11797 as part of a template-id. This enables us to handle constructs
11800 template <typename T> struct S { S(); };
11801 template <typename T> S<T>::S();
11803 correctly. We would treat `S' as a template -- if it were `S<T>'
11804 -- but we do not if there is no `<'. */
11806 if (processing_template_decl
11807 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11809 /* In a declaration, in a dependent context, we pretend that the
11810 "template" keyword was present in order to improve error
11811 recovery. For example, given:
11813 template <typename T> void f(T::X<int>);
11815 we want to treat "X<int>" as a template-id. */
11817 && !template_keyword_p
11818 && parser->scope && TYPE_P (parser->scope)
11819 && check_dependency_p
11820 && dependent_scope_p (parser->scope)
11821 /* Do not do this for dtors (or ctors), since they never
11822 need the template keyword before their name. */
11823 && !constructor_name_p (identifier, parser->scope))
11825 cp_token_position start = 0;
11827 /* Explain what went wrong. */
11828 error_at (token->location, "non-template %qD used as template",
11830 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11831 parser->scope, identifier);
11832 /* If parsing tentatively, find the location of the "<" token. */
11833 if (cp_parser_simulate_error (parser))
11834 start = cp_lexer_token_position (parser->lexer, true);
11835 /* Parse the template arguments so that we can issue error
11836 messages about them. */
11837 cp_lexer_consume_token (parser->lexer);
11838 cp_parser_enclosed_template_argument_list (parser);
11839 /* Skip tokens until we find a good place from which to
11840 continue parsing. */
11841 cp_parser_skip_to_closing_parenthesis (parser,
11842 /*recovering=*/true,
11844 /*consume_paren=*/false);
11845 /* If parsing tentatively, permanently remove the
11846 template argument list. That will prevent duplicate
11847 error messages from being issued about the missing
11848 "template" keyword. */
11850 cp_lexer_purge_tokens_after (parser->lexer, start);
11852 *is_identifier = true;
11856 /* If the "template" keyword is present, then there is generally
11857 no point in doing name-lookup, so we just return IDENTIFIER.
11858 But, if the qualifying scope is non-dependent then we can
11859 (and must) do name-lookup normally. */
11860 if (template_keyword_p
11862 || (TYPE_P (parser->scope)
11863 && dependent_type_p (parser->scope))))
11867 /* Look up the name. */
11868 decl = cp_parser_lookup_name (parser, identifier,
11870 /*is_template=*/true,
11871 /*is_namespace=*/false,
11872 check_dependency_p,
11873 /*ambiguous_decls=*/NULL,
11876 /* If DECL is a template, then the name was a template-name. */
11877 if (TREE_CODE (decl) == TEMPLATE_DECL)
11881 tree fn = NULL_TREE;
11883 /* The standard does not explicitly indicate whether a name that
11884 names a set of overloaded declarations, some of which are
11885 templates, is a template-name. However, such a name should
11886 be a template-name; otherwise, there is no way to form a
11887 template-id for the overloaded templates. */
11888 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11889 if (TREE_CODE (fns) == OVERLOAD)
11890 for (fn = fns; fn; fn = OVL_NEXT (fn))
11891 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11896 /* The name does not name a template. */
11897 cp_parser_error (parser, "expected template-name");
11898 return error_mark_node;
11902 /* If DECL is dependent, and refers to a function, then just return
11903 its name; we will look it up again during template instantiation. */
11904 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11906 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11907 if (TYPE_P (scope) && dependent_type_p (scope))
11914 /* Parse a template-argument-list.
11916 template-argument-list:
11917 template-argument ... [opt]
11918 template-argument-list , template-argument ... [opt]
11920 Returns a TREE_VEC containing the arguments. */
11923 cp_parser_template_argument_list (cp_parser* parser)
11925 tree fixed_args[10];
11926 unsigned n_args = 0;
11927 unsigned alloced = 10;
11928 tree *arg_ary = fixed_args;
11930 bool saved_in_template_argument_list_p;
11932 bool saved_non_ice_p;
11934 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11935 parser->in_template_argument_list_p = true;
11936 /* Even if the template-id appears in an integral
11937 constant-expression, the contents of the argument list do
11939 saved_ice_p = parser->integral_constant_expression_p;
11940 parser->integral_constant_expression_p = false;
11941 saved_non_ice_p = parser->non_integral_constant_expression_p;
11942 parser->non_integral_constant_expression_p = false;
11943 /* Parse the arguments. */
11949 /* Consume the comma. */
11950 cp_lexer_consume_token (parser->lexer);
11952 /* Parse the template-argument. */
11953 argument = cp_parser_template_argument (parser);
11955 /* If the next token is an ellipsis, we're expanding a template
11957 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11959 if (argument == error_mark_node)
11961 cp_token *token = cp_lexer_peek_token (parser->lexer);
11962 error_at (token->location,
11963 "expected parameter pack before %<...%>");
11965 /* Consume the `...' token. */
11966 cp_lexer_consume_token (parser->lexer);
11968 /* Make the argument into a TYPE_PACK_EXPANSION or
11969 EXPR_PACK_EXPANSION. */
11970 argument = make_pack_expansion (argument);
11973 if (n_args == alloced)
11977 if (arg_ary == fixed_args)
11979 arg_ary = XNEWVEC (tree, alloced);
11980 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11983 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11985 arg_ary[n_args++] = argument;
11987 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11989 vec = make_tree_vec (n_args);
11992 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11994 if (arg_ary != fixed_args)
11996 parser->non_integral_constant_expression_p = saved_non_ice_p;
11997 parser->integral_constant_expression_p = saved_ice_p;
11998 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11999 #ifdef ENABLE_CHECKING
12000 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12005 /* Parse a template-argument.
12008 assignment-expression
12012 The representation is that of an assignment-expression, type-id, or
12013 id-expression -- except that the qualified id-expression is
12014 evaluated, so that the value returned is either a DECL or an
12017 Although the standard says "assignment-expression", it forbids
12018 throw-expressions or assignments in the template argument.
12019 Therefore, we use "conditional-expression" instead. */
12022 cp_parser_template_argument (cp_parser* parser)
12027 bool maybe_type_id = false;
12028 cp_token *token = NULL, *argument_start_token = NULL;
12031 /* There's really no way to know what we're looking at, so we just
12032 try each alternative in order.
12036 In a template-argument, an ambiguity between a type-id and an
12037 expression is resolved to a type-id, regardless of the form of
12038 the corresponding template-parameter.
12040 Therefore, we try a type-id first. */
12041 cp_parser_parse_tentatively (parser);
12042 argument = cp_parser_template_type_arg (parser);
12043 /* If there was no error parsing the type-id but the next token is a
12044 '>>', our behavior depends on which dialect of C++ we're
12045 parsing. In C++98, we probably found a typo for '> >'. But there
12046 are type-id which are also valid expressions. For instance:
12048 struct X { int operator >> (int); };
12049 template <int V> struct Foo {};
12052 Here 'X()' is a valid type-id of a function type, but the user just
12053 wanted to write the expression "X() >> 5". Thus, we remember that we
12054 found a valid type-id, but we still try to parse the argument as an
12055 expression to see what happens.
12057 In C++0x, the '>>' will be considered two separate '>'
12059 if (!cp_parser_error_occurred (parser)
12060 && cxx_dialect == cxx98
12061 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12063 maybe_type_id = true;
12064 cp_parser_abort_tentative_parse (parser);
12068 /* If the next token isn't a `,' or a `>', then this argument wasn't
12069 really finished. This means that the argument is not a valid
12071 if (!cp_parser_next_token_ends_template_argument_p (parser))
12072 cp_parser_error (parser, "expected template-argument");
12073 /* If that worked, we're done. */
12074 if (cp_parser_parse_definitely (parser))
12077 /* We're still not sure what the argument will be. */
12078 cp_parser_parse_tentatively (parser);
12079 /* Try a template. */
12080 argument_start_token = cp_lexer_peek_token (parser->lexer);
12081 argument = cp_parser_id_expression (parser,
12082 /*template_keyword_p=*/false,
12083 /*check_dependency_p=*/true,
12085 /*declarator_p=*/false,
12086 /*optional_p=*/false);
12087 /* If the next token isn't a `,' or a `>', then this argument wasn't
12088 really finished. */
12089 if (!cp_parser_next_token_ends_template_argument_p (parser))
12090 cp_parser_error (parser, "expected template-argument");
12091 if (!cp_parser_error_occurred (parser))
12093 /* Figure out what is being referred to. If the id-expression
12094 was for a class template specialization, then we will have a
12095 TYPE_DECL at this point. There is no need to do name lookup
12096 at this point in that case. */
12097 if (TREE_CODE (argument) != TYPE_DECL)
12098 argument = cp_parser_lookup_name (parser, argument,
12100 /*is_template=*/template_p,
12101 /*is_namespace=*/false,
12102 /*check_dependency=*/true,
12103 /*ambiguous_decls=*/NULL,
12104 argument_start_token->location);
12105 if (TREE_CODE (argument) != TEMPLATE_DECL
12106 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12107 cp_parser_error (parser, "expected template-name");
12109 if (cp_parser_parse_definitely (parser))
12111 /* It must be a non-type argument. There permitted cases are given
12112 in [temp.arg.nontype]:
12114 -- an integral constant-expression of integral or enumeration
12117 -- the name of a non-type template-parameter; or
12119 -- the name of an object or function with external linkage...
12121 -- the address of an object or function with external linkage...
12123 -- a pointer to member... */
12124 /* Look for a non-type template parameter. */
12125 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12127 cp_parser_parse_tentatively (parser);
12128 argument = cp_parser_primary_expression (parser,
12129 /*address_p=*/false,
12131 /*template_arg_p=*/true,
12133 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12134 || !cp_parser_next_token_ends_template_argument_p (parser))
12135 cp_parser_simulate_error (parser);
12136 if (cp_parser_parse_definitely (parser))
12140 /* If the next token is "&", the argument must be the address of an
12141 object or function with external linkage. */
12142 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12144 cp_lexer_consume_token (parser->lexer);
12145 /* See if we might have an id-expression. */
12146 token = cp_lexer_peek_token (parser->lexer);
12147 if (token->type == CPP_NAME
12148 || token->keyword == RID_OPERATOR
12149 || token->type == CPP_SCOPE
12150 || token->type == CPP_TEMPLATE_ID
12151 || token->type == CPP_NESTED_NAME_SPECIFIER)
12153 cp_parser_parse_tentatively (parser);
12154 argument = cp_parser_primary_expression (parser,
12157 /*template_arg_p=*/true,
12159 if (cp_parser_error_occurred (parser)
12160 || !cp_parser_next_token_ends_template_argument_p (parser))
12161 cp_parser_abort_tentative_parse (parser);
12166 if (TREE_CODE (argument) == INDIRECT_REF)
12168 gcc_assert (REFERENCE_REF_P (argument));
12169 argument = TREE_OPERAND (argument, 0);
12172 /* If we're in a template, we represent a qualified-id referring
12173 to a static data member as a SCOPE_REF even if the scope isn't
12174 dependent so that we can check access control later. */
12176 if (TREE_CODE (probe) == SCOPE_REF)
12177 probe = TREE_OPERAND (probe, 1);
12178 if (TREE_CODE (probe) == VAR_DECL)
12180 /* A variable without external linkage might still be a
12181 valid constant-expression, so no error is issued here
12182 if the external-linkage check fails. */
12183 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12184 cp_parser_simulate_error (parser);
12186 else if (is_overloaded_fn (argument))
12187 /* All overloaded functions are allowed; if the external
12188 linkage test does not pass, an error will be issued
12192 && (TREE_CODE (argument) == OFFSET_REF
12193 || TREE_CODE (argument) == SCOPE_REF))
12194 /* A pointer-to-member. */
12196 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12199 cp_parser_simulate_error (parser);
12201 if (cp_parser_parse_definitely (parser))
12204 argument = build_x_unary_op (ADDR_EXPR, argument,
12205 tf_warning_or_error);
12210 /* If the argument started with "&", there are no other valid
12211 alternatives at this point. */
12214 cp_parser_error (parser, "invalid non-type template argument");
12215 return error_mark_node;
12218 /* If the argument wasn't successfully parsed as a type-id followed
12219 by '>>', the argument can only be a constant expression now.
12220 Otherwise, we try parsing the constant-expression tentatively,
12221 because the argument could really be a type-id. */
12223 cp_parser_parse_tentatively (parser);
12224 argument = cp_parser_constant_expression (parser,
12225 /*allow_non_constant_p=*/false,
12226 /*non_constant_p=*/NULL);
12227 argument = fold_non_dependent_expr (argument);
12228 if (!maybe_type_id)
12230 if (!cp_parser_next_token_ends_template_argument_p (parser))
12231 cp_parser_error (parser, "expected template-argument");
12232 if (cp_parser_parse_definitely (parser))
12234 /* We did our best to parse the argument as a non type-id, but that
12235 was the only alternative that matched (albeit with a '>' after
12236 it). We can assume it's just a typo from the user, and a
12237 diagnostic will then be issued. */
12238 return cp_parser_template_type_arg (parser);
12241 /* Parse an explicit-instantiation.
12243 explicit-instantiation:
12244 template declaration
12246 Although the standard says `declaration', what it really means is:
12248 explicit-instantiation:
12249 template decl-specifier-seq [opt] declarator [opt] ;
12251 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12252 supposed to be allowed. A defect report has been filed about this
12257 explicit-instantiation:
12258 storage-class-specifier template
12259 decl-specifier-seq [opt] declarator [opt] ;
12260 function-specifier template
12261 decl-specifier-seq [opt] declarator [opt] ; */
12264 cp_parser_explicit_instantiation (cp_parser* parser)
12266 int declares_class_or_enum;
12267 cp_decl_specifier_seq decl_specifiers;
12268 tree extension_specifier = NULL_TREE;
12270 /* Look for an (optional) storage-class-specifier or
12271 function-specifier. */
12272 if (cp_parser_allow_gnu_extensions_p (parser))
12274 extension_specifier
12275 = cp_parser_storage_class_specifier_opt (parser);
12276 if (!extension_specifier)
12277 extension_specifier
12278 = cp_parser_function_specifier_opt (parser,
12279 /*decl_specs=*/NULL);
12282 /* Look for the `template' keyword. */
12283 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12284 /* Let the front end know that we are processing an explicit
12286 begin_explicit_instantiation ();
12287 /* [temp.explicit] says that we are supposed to ignore access
12288 control while processing explicit instantiation directives. */
12289 push_deferring_access_checks (dk_no_check);
12290 /* Parse a decl-specifier-seq. */
12291 cp_parser_decl_specifier_seq (parser,
12292 CP_PARSER_FLAGS_OPTIONAL,
12294 &declares_class_or_enum);
12295 /* If there was exactly one decl-specifier, and it declared a class,
12296 and there's no declarator, then we have an explicit type
12298 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12302 type = check_tag_decl (&decl_specifiers);
12303 /* Turn access control back on for names used during
12304 template instantiation. */
12305 pop_deferring_access_checks ();
12307 do_type_instantiation (type, extension_specifier,
12308 /*complain=*/tf_error);
12312 cp_declarator *declarator;
12315 /* Parse the declarator. */
12317 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12318 /*ctor_dtor_or_conv_p=*/NULL,
12319 /*parenthesized_p=*/NULL,
12320 /*member_p=*/false);
12321 if (declares_class_or_enum & 2)
12322 cp_parser_check_for_definition_in_return_type (declarator,
12323 decl_specifiers.type,
12324 decl_specifiers.type_location);
12325 if (declarator != cp_error_declarator)
12327 if (decl_specifiers.specs[(int)ds_inline])
12328 permerror (input_location, "explicit instantiation shall not use"
12329 " %<inline%> specifier");
12330 if (decl_specifiers.specs[(int)ds_constexpr])
12331 permerror (input_location, "explicit instantiation shall not use"
12332 " %<constexpr%> specifier");
12334 decl = grokdeclarator (declarator, &decl_specifiers,
12335 NORMAL, 0, &decl_specifiers.attributes);
12336 /* Turn access control back on for names used during
12337 template instantiation. */
12338 pop_deferring_access_checks ();
12339 /* Do the explicit instantiation. */
12340 do_decl_instantiation (decl, extension_specifier);
12344 pop_deferring_access_checks ();
12345 /* Skip the body of the explicit instantiation. */
12346 cp_parser_skip_to_end_of_statement (parser);
12349 /* We're done with the instantiation. */
12350 end_explicit_instantiation ();
12352 cp_parser_consume_semicolon_at_end_of_statement (parser);
12355 /* Parse an explicit-specialization.
12357 explicit-specialization:
12358 template < > declaration
12360 Although the standard says `declaration', what it really means is:
12362 explicit-specialization:
12363 template <> decl-specifier [opt] init-declarator [opt] ;
12364 template <> function-definition
12365 template <> explicit-specialization
12366 template <> template-declaration */
12369 cp_parser_explicit_specialization (cp_parser* parser)
12371 bool need_lang_pop;
12372 cp_token *token = cp_lexer_peek_token (parser->lexer);
12374 /* Look for the `template' keyword. */
12375 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12376 /* Look for the `<'. */
12377 cp_parser_require (parser, CPP_LESS, RT_LESS);
12378 /* Look for the `>'. */
12379 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12380 /* We have processed another parameter list. */
12381 ++parser->num_template_parameter_lists;
12384 A template ... explicit specialization ... shall not have C
12386 if (current_lang_name == lang_name_c)
12388 error_at (token->location, "template specialization with C linkage");
12389 /* Give it C++ linkage to avoid confusing other parts of the
12391 push_lang_context (lang_name_cplusplus);
12392 need_lang_pop = true;
12395 need_lang_pop = false;
12396 /* Let the front end know that we are beginning a specialization. */
12397 if (!begin_specialization ())
12399 end_specialization ();
12403 /* If the next keyword is `template', we need to figure out whether
12404 or not we're looking a template-declaration. */
12405 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12407 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12408 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12409 cp_parser_template_declaration_after_export (parser,
12410 /*member_p=*/false);
12412 cp_parser_explicit_specialization (parser);
12415 /* Parse the dependent declaration. */
12416 cp_parser_single_declaration (parser,
12418 /*member_p=*/false,
12419 /*explicit_specialization_p=*/true,
12420 /*friend_p=*/NULL);
12421 /* We're done with the specialization. */
12422 end_specialization ();
12423 /* For the erroneous case of a template with C linkage, we pushed an
12424 implicit C++ linkage scope; exit that scope now. */
12426 pop_lang_context ();
12427 /* We're done with this parameter list. */
12428 --parser->num_template_parameter_lists;
12431 /* Parse a type-specifier.
12434 simple-type-specifier
12437 elaborated-type-specifier
12445 Returns a representation of the type-specifier. For a
12446 class-specifier, enum-specifier, or elaborated-type-specifier, a
12447 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12449 The parser flags FLAGS is used to control type-specifier parsing.
12451 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12452 in a decl-specifier-seq.
12454 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12455 class-specifier, enum-specifier, or elaborated-type-specifier, then
12456 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12457 if a type is declared; 2 if it is defined. Otherwise, it is set to
12460 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12461 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12462 is set to FALSE. */
12465 cp_parser_type_specifier (cp_parser* parser,
12466 cp_parser_flags flags,
12467 cp_decl_specifier_seq *decl_specs,
12468 bool is_declaration,
12469 int* declares_class_or_enum,
12470 bool* is_cv_qualifier)
12472 tree type_spec = NULL_TREE;
12475 cp_decl_spec ds = ds_last;
12477 /* Assume this type-specifier does not declare a new type. */
12478 if (declares_class_or_enum)
12479 *declares_class_or_enum = 0;
12480 /* And that it does not specify a cv-qualifier. */
12481 if (is_cv_qualifier)
12482 *is_cv_qualifier = false;
12483 /* Peek at the next token. */
12484 token = cp_lexer_peek_token (parser->lexer);
12486 /* If we're looking at a keyword, we can use that to guide the
12487 production we choose. */
12488 keyword = token->keyword;
12492 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12493 goto elaborated_type_specifier;
12495 /* Look for the enum-specifier. */
12496 type_spec = cp_parser_enum_specifier (parser);
12497 /* If that worked, we're done. */
12500 if (declares_class_or_enum)
12501 *declares_class_or_enum = 2;
12503 cp_parser_set_decl_spec_type (decl_specs,
12506 /*user_defined_p=*/true);
12510 goto elaborated_type_specifier;
12512 /* Any of these indicate either a class-specifier, or an
12513 elaborated-type-specifier. */
12517 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12518 goto elaborated_type_specifier;
12520 /* Parse tentatively so that we can back up if we don't find a
12521 class-specifier. */
12522 cp_parser_parse_tentatively (parser);
12523 /* Look for the class-specifier. */
12524 type_spec = cp_parser_class_specifier (parser);
12525 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12526 /* If that worked, we're done. */
12527 if (cp_parser_parse_definitely (parser))
12529 if (declares_class_or_enum)
12530 *declares_class_or_enum = 2;
12532 cp_parser_set_decl_spec_type (decl_specs,
12535 /*user_defined_p=*/true);
12539 /* Fall through. */
12540 elaborated_type_specifier:
12541 /* We're declaring (not defining) a class or enum. */
12542 if (declares_class_or_enum)
12543 *declares_class_or_enum = 1;
12545 /* Fall through. */
12547 /* Look for an elaborated-type-specifier. */
12549 = (cp_parser_elaborated_type_specifier
12551 decl_specs && decl_specs->specs[(int) ds_friend],
12554 cp_parser_set_decl_spec_type (decl_specs,
12557 /*user_defined_p=*/true);
12562 if (is_cv_qualifier)
12563 *is_cv_qualifier = true;
12568 if (is_cv_qualifier)
12569 *is_cv_qualifier = true;
12574 if (is_cv_qualifier)
12575 *is_cv_qualifier = true;
12579 /* The `__complex__' keyword is a GNU extension. */
12587 /* Handle simple keywords. */
12592 ++decl_specs->specs[(int)ds];
12593 decl_specs->any_specifiers_p = true;
12595 return cp_lexer_consume_token (parser->lexer)->u.value;
12598 /* If we do not already have a type-specifier, assume we are looking
12599 at a simple-type-specifier. */
12600 type_spec = cp_parser_simple_type_specifier (parser,
12604 /* If we didn't find a type-specifier, and a type-specifier was not
12605 optional in this context, issue an error message. */
12606 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12608 cp_parser_error (parser, "expected type specifier");
12609 return error_mark_node;
12615 /* Parse a simple-type-specifier.
12617 simple-type-specifier:
12618 :: [opt] nested-name-specifier [opt] type-name
12619 :: [opt] nested-name-specifier template template-id
12634 simple-type-specifier:
12636 decltype ( expression )
12642 simple-type-specifier:
12644 __typeof__ unary-expression
12645 __typeof__ ( type-id )
12647 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12648 appropriately updated. */
12651 cp_parser_simple_type_specifier (cp_parser* parser,
12652 cp_decl_specifier_seq *decl_specs,
12653 cp_parser_flags flags)
12655 tree type = NULL_TREE;
12658 /* Peek at the next token. */
12659 token = cp_lexer_peek_token (parser->lexer);
12661 /* If we're looking at a keyword, things are easy. */
12662 switch (token->keyword)
12666 decl_specs->explicit_char_p = true;
12667 type = char_type_node;
12670 type = char16_type_node;
12673 type = char32_type_node;
12676 type = wchar_type_node;
12679 type = boolean_type_node;
12683 ++decl_specs->specs[(int) ds_short];
12684 type = short_integer_type_node;
12688 decl_specs->explicit_int_p = true;
12689 type = integer_type_node;
12692 if (!int128_integer_type_node)
12695 decl_specs->explicit_int128_p = true;
12696 type = int128_integer_type_node;
12700 ++decl_specs->specs[(int) ds_long];
12701 type = long_integer_type_node;
12705 ++decl_specs->specs[(int) ds_signed];
12706 type = integer_type_node;
12710 ++decl_specs->specs[(int) ds_unsigned];
12711 type = unsigned_type_node;
12714 type = float_type_node;
12717 type = double_type_node;
12720 type = void_type_node;
12724 maybe_warn_cpp0x (CPP0X_AUTO);
12725 type = make_auto ();
12729 /* Parse the `decltype' type. */
12730 type = cp_parser_decltype (parser);
12733 cp_parser_set_decl_spec_type (decl_specs, type,
12735 /*user_defined_p=*/true);
12740 /* Consume the `typeof' token. */
12741 cp_lexer_consume_token (parser->lexer);
12742 /* Parse the operand to `typeof'. */
12743 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12744 /* If it is not already a TYPE, take its type. */
12745 if (!TYPE_P (type))
12746 type = finish_typeof (type);
12749 cp_parser_set_decl_spec_type (decl_specs, type,
12751 /*user_defined_p=*/true);
12759 /* If the type-specifier was for a built-in type, we're done. */
12762 /* Record the type. */
12764 && (token->keyword != RID_SIGNED
12765 && token->keyword != RID_UNSIGNED
12766 && token->keyword != RID_SHORT
12767 && token->keyword != RID_LONG))
12768 cp_parser_set_decl_spec_type (decl_specs,
12771 /*user_defined=*/false);
12773 decl_specs->any_specifiers_p = true;
12775 /* Consume the token. */
12776 cp_lexer_consume_token (parser->lexer);
12778 /* There is no valid C++ program where a non-template type is
12779 followed by a "<". That usually indicates that the user thought
12780 that the type was a template. */
12781 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12783 return TYPE_NAME (type);
12786 /* The type-specifier must be a user-defined type. */
12787 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12792 /* Don't gobble tokens or issue error messages if this is an
12793 optional type-specifier. */
12794 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12795 cp_parser_parse_tentatively (parser);
12797 /* Look for the optional `::' operator. */
12799 = (cp_parser_global_scope_opt (parser,
12800 /*current_scope_valid_p=*/false)
12802 /* Look for the nested-name specifier. */
12804 = (cp_parser_nested_name_specifier_opt (parser,
12805 /*typename_keyword_p=*/false,
12806 /*check_dependency_p=*/true,
12808 /*is_declaration=*/false)
12810 token = cp_lexer_peek_token (parser->lexer);
12811 /* If we have seen a nested-name-specifier, and the next token
12812 is `template', then we are using the template-id production. */
12814 && cp_parser_optional_template_keyword (parser))
12816 /* Look for the template-id. */
12817 type = cp_parser_template_id (parser,
12818 /*template_keyword_p=*/true,
12819 /*check_dependency_p=*/true,
12820 /*is_declaration=*/false);
12821 /* If the template-id did not name a type, we are out of
12823 if (TREE_CODE (type) != TYPE_DECL)
12825 cp_parser_error (parser, "expected template-id for type");
12829 /* Otherwise, look for a type-name. */
12831 type = cp_parser_type_name (parser);
12832 /* Keep track of all name-lookups performed in class scopes. */
12836 && TREE_CODE (type) == TYPE_DECL
12837 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12838 maybe_note_name_used_in_class (DECL_NAME (type), type);
12839 /* If it didn't work out, we don't have a TYPE. */
12840 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12841 && !cp_parser_parse_definitely (parser))
12843 if (type && decl_specs)
12844 cp_parser_set_decl_spec_type (decl_specs, type,
12846 /*user_defined=*/true);
12849 /* If we didn't get a type-name, issue an error message. */
12850 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12852 cp_parser_error (parser, "expected type-name");
12853 return error_mark_node;
12856 if (type && type != error_mark_node)
12858 /* See if TYPE is an Objective-C type, and if so, parse and
12859 accept any protocol references following it. Do this before
12860 the cp_parser_check_for_invalid_template_id() call, because
12861 Objective-C types can be followed by '<...>' which would
12862 enclose protocol names rather than template arguments, and so
12863 everything is fine. */
12864 if (c_dialect_objc () && !parser->scope
12865 && (objc_is_id (type) || objc_is_class_name (type)))
12867 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12868 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12870 /* Clobber the "unqualified" type previously entered into
12871 DECL_SPECS with the new, improved protocol-qualified version. */
12873 decl_specs->type = qual_type;
12878 /* There is no valid C++ program where a non-template type is
12879 followed by a "<". That usually indicates that the user
12880 thought that the type was a template. */
12881 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12888 /* Parse a type-name.
12901 Returns a TYPE_DECL for the type. */
12904 cp_parser_type_name (cp_parser* parser)
12908 /* We can't know yet whether it is a class-name or not. */
12909 cp_parser_parse_tentatively (parser);
12910 /* Try a class-name. */
12911 type_decl = cp_parser_class_name (parser,
12912 /*typename_keyword_p=*/false,
12913 /*template_keyword_p=*/false,
12915 /*check_dependency_p=*/true,
12916 /*class_head_p=*/false,
12917 /*is_declaration=*/false);
12918 /* If it's not a class-name, keep looking. */
12919 if (!cp_parser_parse_definitely (parser))
12921 /* It must be a typedef-name or an enum-name. */
12922 return cp_parser_nonclass_name (parser);
12928 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12936 Returns a TYPE_DECL for the type. */
12939 cp_parser_nonclass_name (cp_parser* parser)
12944 cp_token *token = cp_lexer_peek_token (parser->lexer);
12945 identifier = cp_parser_identifier (parser);
12946 if (identifier == error_mark_node)
12947 return error_mark_node;
12949 /* Look up the type-name. */
12950 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12952 if (TREE_CODE (type_decl) != TYPE_DECL
12953 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12955 /* See if this is an Objective-C type. */
12956 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12957 tree type = objc_get_protocol_qualified_type (identifier, protos);
12959 type_decl = TYPE_NAME (type);
12962 /* Issue an error if we did not find a type-name. */
12963 if (TREE_CODE (type_decl) != TYPE_DECL
12964 /* In Objective-C, we have the complication that class names are
12965 normally type names and start declarations (eg, the
12966 "NSObject" in "NSObject *object;"), but can be used in an
12967 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12968 is an expression. So, a classname followed by a dot is not a
12969 valid type-name. */
12970 || (objc_is_class_name (TREE_TYPE (type_decl))
12971 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12973 if (!cp_parser_simulate_error (parser))
12974 cp_parser_name_lookup_error (parser, identifier, type_decl,
12975 NLE_TYPE, token->location);
12976 return error_mark_node;
12978 /* Remember that the name was used in the definition of the
12979 current class so that we can check later to see if the
12980 meaning would have been different after the class was
12981 entirely defined. */
12982 else if (type_decl != error_mark_node
12984 maybe_note_name_used_in_class (identifier, type_decl);
12989 /* Parse an elaborated-type-specifier. Note that the grammar given
12990 here incorporates the resolution to DR68.
12992 elaborated-type-specifier:
12993 class-key :: [opt] nested-name-specifier [opt] identifier
12994 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12995 enum-key :: [opt] nested-name-specifier [opt] identifier
12996 typename :: [opt] nested-name-specifier identifier
12997 typename :: [opt] nested-name-specifier template [opt]
13002 elaborated-type-specifier:
13003 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13004 class-key attributes :: [opt] nested-name-specifier [opt]
13005 template [opt] template-id
13006 enum attributes :: [opt] nested-name-specifier [opt] identifier
13008 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13009 declared `friend'. If IS_DECLARATION is TRUE, then this
13010 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13011 something is being declared.
13013 Returns the TYPE specified. */
13016 cp_parser_elaborated_type_specifier (cp_parser* parser,
13018 bool is_declaration)
13020 enum tag_types tag_type;
13022 tree type = NULL_TREE;
13023 tree attributes = NULL_TREE;
13025 cp_token *token = NULL;
13027 /* See if we're looking at the `enum' keyword. */
13028 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13030 /* Consume the `enum' token. */
13031 cp_lexer_consume_token (parser->lexer);
13032 /* Remember that it's an enumeration type. */
13033 tag_type = enum_type;
13034 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13035 enums) is used here. */
13036 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13037 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13039 pedwarn (input_location, 0, "elaborated-type-specifier "
13040 "for a scoped enum must not use the %<%D%> keyword",
13041 cp_lexer_peek_token (parser->lexer)->u.value);
13042 /* Consume the `struct' or `class' and parse it anyway. */
13043 cp_lexer_consume_token (parser->lexer);
13045 /* Parse the attributes. */
13046 attributes = cp_parser_attributes_opt (parser);
13048 /* Or, it might be `typename'. */
13049 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13052 /* Consume the `typename' token. */
13053 cp_lexer_consume_token (parser->lexer);
13054 /* Remember that it's a `typename' type. */
13055 tag_type = typename_type;
13057 /* Otherwise it must be a class-key. */
13060 tag_type = cp_parser_class_key (parser);
13061 if (tag_type == none_type)
13062 return error_mark_node;
13063 /* Parse the attributes. */
13064 attributes = cp_parser_attributes_opt (parser);
13067 /* Look for the `::' operator. */
13068 globalscope = cp_parser_global_scope_opt (parser,
13069 /*current_scope_valid_p=*/false);
13070 /* Look for the nested-name-specifier. */
13071 if (tag_type == typename_type && !globalscope)
13073 if (!cp_parser_nested_name_specifier (parser,
13074 /*typename_keyword_p=*/true,
13075 /*check_dependency_p=*/true,
13078 return error_mark_node;
13081 /* Even though `typename' is not present, the proposed resolution
13082 to Core Issue 180 says that in `class A<T>::B', `B' should be
13083 considered a type-name, even if `A<T>' is dependent. */
13084 cp_parser_nested_name_specifier_opt (parser,
13085 /*typename_keyword_p=*/true,
13086 /*check_dependency_p=*/true,
13089 /* For everything but enumeration types, consider a template-id.
13090 For an enumeration type, consider only a plain identifier. */
13091 if (tag_type != enum_type)
13093 bool template_p = false;
13096 /* Allow the `template' keyword. */
13097 template_p = cp_parser_optional_template_keyword (parser);
13098 /* If we didn't see `template', we don't know if there's a
13099 template-id or not. */
13101 cp_parser_parse_tentatively (parser);
13102 /* Parse the template-id. */
13103 token = cp_lexer_peek_token (parser->lexer);
13104 decl = cp_parser_template_id (parser, template_p,
13105 /*check_dependency_p=*/true,
13107 /* If we didn't find a template-id, look for an ordinary
13109 if (!template_p && !cp_parser_parse_definitely (parser))
13111 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13112 in effect, then we must assume that, upon instantiation, the
13113 template will correspond to a class. */
13114 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13115 && tag_type == typename_type)
13116 type = make_typename_type (parser->scope, decl,
13118 /*complain=*/tf_error);
13119 /* If the `typename' keyword is in effect and DECL is not a type
13120 decl. Then type is non existant. */
13121 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13124 type = TREE_TYPE (decl);
13129 token = cp_lexer_peek_token (parser->lexer);
13130 identifier = cp_parser_identifier (parser);
13132 if (identifier == error_mark_node)
13134 parser->scope = NULL_TREE;
13135 return error_mark_node;
13138 /* For a `typename', we needn't call xref_tag. */
13139 if (tag_type == typename_type
13140 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13141 return cp_parser_make_typename_type (parser, parser->scope,
13144 /* Look up a qualified name in the usual way. */
13148 tree ambiguous_decls;
13150 decl = cp_parser_lookup_name (parser, identifier,
13152 /*is_template=*/false,
13153 /*is_namespace=*/false,
13154 /*check_dependency=*/true,
13158 /* If the lookup was ambiguous, an error will already have been
13160 if (ambiguous_decls)
13161 return error_mark_node;
13163 /* If we are parsing friend declaration, DECL may be a
13164 TEMPLATE_DECL tree node here. However, we need to check
13165 whether this TEMPLATE_DECL results in valid code. Consider
13166 the following example:
13169 template <class T> class C {};
13172 template <class T> friend class N::C; // #1, valid code
13174 template <class T> class Y {
13175 friend class N::C; // #2, invalid code
13178 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13179 name lookup of `N::C'. We see that friend declaration must
13180 be template for the code to be valid. Note that
13181 processing_template_decl does not work here since it is
13182 always 1 for the above two cases. */
13184 decl = (cp_parser_maybe_treat_template_as_class
13185 (decl, /*tag_name_p=*/is_friend
13186 && parser->num_template_parameter_lists));
13188 if (TREE_CODE (decl) != TYPE_DECL)
13190 cp_parser_diagnose_invalid_type_name (parser,
13194 return error_mark_node;
13197 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13199 bool allow_template = (parser->num_template_parameter_lists
13200 || DECL_SELF_REFERENCE_P (decl));
13201 type = check_elaborated_type_specifier (tag_type, decl,
13204 if (type == error_mark_node)
13205 return error_mark_node;
13208 /* Forward declarations of nested types, such as
13213 are invalid unless all components preceding the final '::'
13214 are complete. If all enclosing types are complete, these
13215 declarations become merely pointless.
13217 Invalid forward declarations of nested types are errors
13218 caught elsewhere in parsing. Those that are pointless arrive
13221 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13222 && !is_friend && !processing_explicit_instantiation)
13223 warning (0, "declaration %qD does not declare anything", decl);
13225 type = TREE_TYPE (decl);
13229 /* An elaborated-type-specifier sometimes introduces a new type and
13230 sometimes names an existing type. Normally, the rule is that it
13231 introduces a new type only if there is not an existing type of
13232 the same name already in scope. For example, given:
13235 void f() { struct S s; }
13237 the `struct S' in the body of `f' is the same `struct S' as in
13238 the global scope; the existing definition is used. However, if
13239 there were no global declaration, this would introduce a new
13240 local class named `S'.
13242 An exception to this rule applies to the following code:
13244 namespace N { struct S; }
13246 Here, the elaborated-type-specifier names a new type
13247 unconditionally; even if there is already an `S' in the
13248 containing scope this declaration names a new type.
13249 This exception only applies if the elaborated-type-specifier
13250 forms the complete declaration:
13254 A declaration consisting solely of `class-key identifier ;' is
13255 either a redeclaration of the name in the current scope or a
13256 forward declaration of the identifier as a class name. It
13257 introduces the name into the current scope.
13259 We are in this situation precisely when the next token is a `;'.
13261 An exception to the exception is that a `friend' declaration does
13262 *not* name a new type; i.e., given:
13264 struct S { friend struct T; };
13266 `T' is not a new type in the scope of `S'.
13268 Also, `new struct S' or `sizeof (struct S)' never results in the
13269 definition of a new type; a new type can only be declared in a
13270 declaration context. */
13276 /* Friends have special name lookup rules. */
13277 ts = ts_within_enclosing_non_class;
13278 else if (is_declaration
13279 && cp_lexer_next_token_is (parser->lexer,
13281 /* This is a `class-key identifier ;' */
13287 (parser->num_template_parameter_lists
13288 && (cp_parser_next_token_starts_class_definition_p (parser)
13289 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13290 /* An unqualified name was used to reference this type, so
13291 there were no qualifying templates. */
13292 if (!cp_parser_check_template_parameters (parser,
13293 /*num_templates=*/0,
13295 /*declarator=*/NULL))
13296 return error_mark_node;
13297 type = xref_tag (tag_type, identifier, ts, template_p);
13301 if (type == error_mark_node)
13302 return error_mark_node;
13304 /* Allow attributes on forward declarations of classes. */
13307 if (TREE_CODE (type) == TYPENAME_TYPE)
13308 warning (OPT_Wattributes,
13309 "attributes ignored on uninstantiated type");
13310 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13311 && ! processing_explicit_instantiation)
13312 warning (OPT_Wattributes,
13313 "attributes ignored on template instantiation");
13314 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13315 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13317 warning (OPT_Wattributes,
13318 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13321 if (tag_type != enum_type)
13322 cp_parser_check_class_key (tag_type, type);
13324 /* A "<" cannot follow an elaborated type specifier. If that
13325 happens, the user was probably trying to form a template-id. */
13326 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13331 /* Parse an enum-specifier.
13334 enum-head { enumerator-list [opt] }
13337 enum-key identifier [opt] enum-base [opt]
13338 enum-key nested-name-specifier identifier enum-base [opt]
13343 enum struct [C++0x]
13346 : type-specifier-seq
13348 opaque-enum-specifier:
13349 enum-key identifier enum-base [opt] ;
13352 enum-key attributes[opt] identifier [opt] enum-base [opt]
13353 { enumerator-list [opt] }attributes[opt]
13355 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13356 if the token stream isn't an enum-specifier after all. */
13359 cp_parser_enum_specifier (cp_parser* parser)
13362 tree type = NULL_TREE;
13364 tree nested_name_specifier = NULL_TREE;
13366 bool scoped_enum_p = false;
13367 bool has_underlying_type = false;
13368 bool nested_being_defined = false;
13369 bool new_value_list = false;
13370 bool is_new_type = false;
13371 bool is_anonymous = false;
13372 tree underlying_type = NULL_TREE;
13373 cp_token *type_start_token = NULL;
13374 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13376 parser->colon_corrects_to_scope_p = false;
13378 /* Parse tentatively so that we can back up if we don't find a
13380 cp_parser_parse_tentatively (parser);
13382 /* Caller guarantees that the current token is 'enum', an identifier
13383 possibly follows, and the token after that is an opening brace.
13384 If we don't have an identifier, fabricate an anonymous name for
13385 the enumeration being defined. */
13386 cp_lexer_consume_token (parser->lexer);
13388 /* Parse the "class" or "struct", which indicates a scoped
13389 enumeration type in C++0x. */
13390 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13391 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13393 if (cxx_dialect < cxx0x)
13394 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13396 /* Consume the `struct' or `class' token. */
13397 cp_lexer_consume_token (parser->lexer);
13399 scoped_enum_p = true;
13402 attributes = cp_parser_attributes_opt (parser);
13404 /* Clear the qualification. */
13405 parser->scope = NULL_TREE;
13406 parser->qualifying_scope = NULL_TREE;
13407 parser->object_scope = NULL_TREE;
13409 /* Figure out in what scope the declaration is being placed. */
13410 prev_scope = current_scope ();
13412 type_start_token = cp_lexer_peek_token (parser->lexer);
13414 push_deferring_access_checks (dk_no_check);
13415 nested_name_specifier
13416 = cp_parser_nested_name_specifier_opt (parser,
13417 /*typename_keyword_p=*/true,
13418 /*check_dependency_p=*/false,
13420 /*is_declaration=*/false);
13422 if (nested_name_specifier)
13426 identifier = cp_parser_identifier (parser);
13427 name = cp_parser_lookup_name (parser, identifier,
13429 /*is_template=*/false,
13430 /*is_namespace=*/false,
13431 /*check_dependency=*/true,
13432 /*ambiguous_decls=*/NULL,
13436 type = TREE_TYPE (name);
13437 if (TREE_CODE (type) == TYPENAME_TYPE)
13439 /* Are template enums allowed in ISO? */
13440 if (template_parm_scope_p ())
13441 pedwarn (type_start_token->location, OPT_pedantic,
13442 "%qD is an enumeration template", name);
13443 /* ignore a typename reference, for it will be solved by name
13449 error_at (type_start_token->location,
13450 "%qD is not an enumerator-name", identifier);
13454 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13455 identifier = cp_parser_identifier (parser);
13458 identifier = make_anon_name ();
13459 is_anonymous = true;
13462 pop_deferring_access_checks ();
13464 /* Check for the `:' that denotes a specified underlying type in C++0x.
13465 Note that a ':' could also indicate a bitfield width, however. */
13466 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13468 cp_decl_specifier_seq type_specifiers;
13470 /* Consume the `:'. */
13471 cp_lexer_consume_token (parser->lexer);
13473 /* Parse the type-specifier-seq. */
13474 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13475 /*is_trailing_return=*/false,
13478 /* At this point this is surely not elaborated type specifier. */
13479 if (!cp_parser_parse_definitely (parser))
13482 if (cxx_dialect < cxx0x)
13483 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13485 has_underlying_type = true;
13487 /* If that didn't work, stop. */
13488 if (type_specifiers.type != error_mark_node)
13490 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13491 /*initialized=*/0, NULL);
13492 if (underlying_type == error_mark_node)
13493 underlying_type = NULL_TREE;
13497 /* Look for the `{' but don't consume it yet. */
13498 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13500 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13502 cp_parser_error (parser, "expected %<{%>");
13503 if (has_underlying_type)
13509 /* An opaque-enum-specifier must have a ';' here. */
13510 if ((scoped_enum_p || underlying_type)
13511 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13513 cp_parser_error (parser, "expected %<;%> or %<{%>");
13514 if (has_underlying_type)
13522 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13525 if (nested_name_specifier)
13527 if (CLASS_TYPE_P (nested_name_specifier))
13529 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13530 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13531 push_scope (nested_name_specifier);
13533 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13535 push_nested_namespace (nested_name_specifier);
13539 /* Issue an error message if type-definitions are forbidden here. */
13540 if (!cp_parser_check_type_definition (parser))
13541 type = error_mark_node;
13543 /* Create the new type. We do this before consuming the opening
13544 brace so the enum will be recorded as being on the line of its
13545 tag (or the 'enum' keyword, if there is no tag). */
13546 type = start_enum (identifier, type, underlying_type,
13547 scoped_enum_p, &is_new_type);
13549 /* If the next token is not '{' it is an opaque-enum-specifier or an
13550 elaborated-type-specifier. */
13551 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13553 if (nested_name_specifier)
13555 /* The following catches invalid code such as:
13556 enum class S<int>::E { A, B, C }; */
13557 if (!processing_specialization
13558 && CLASS_TYPE_P (nested_name_specifier)
13559 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13560 error_at (type_start_token->location, "cannot add an enumerator "
13561 "list to a template instantiation");
13563 /* If that scope does not contain the scope in which the
13564 class was originally declared, the program is invalid. */
13565 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13567 if (at_namespace_scope_p ())
13568 error_at (type_start_token->location,
13569 "declaration of %qD in namespace %qD which does not "
13571 type, prev_scope, nested_name_specifier);
13573 error_at (type_start_token->location,
13574 "declaration of %qD in %qD which does not enclose %qD",
13575 type, prev_scope, nested_name_specifier);
13576 type = error_mark_node;
13581 begin_scope (sk_scoped_enum, type);
13583 /* Consume the opening brace. */
13584 cp_lexer_consume_token (parser->lexer);
13586 if (type == error_mark_node)
13587 ; /* Nothing to add */
13588 else if (OPAQUE_ENUM_P (type)
13589 || (cxx_dialect > cxx98 && processing_specialization))
13591 new_value_list = true;
13592 SET_OPAQUE_ENUM_P (type, false);
13593 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13597 error_at (type_start_token->location, "multiple definition of %q#T", type);
13598 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13599 "previous definition here");
13600 type = error_mark_node;
13603 if (type == error_mark_node)
13604 cp_parser_skip_to_end_of_block_or_statement (parser);
13605 /* If the next token is not '}', then there are some enumerators. */
13606 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13607 cp_parser_enumerator_list (parser, type);
13609 /* Consume the final '}'. */
13610 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13617 /* If a ';' follows, then it is an opaque-enum-specifier
13618 and additional restrictions apply. */
13619 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13622 error_at (type_start_token->location,
13623 "opaque-enum-specifier without name");
13624 else if (nested_name_specifier)
13625 error_at (type_start_token->location,
13626 "opaque-enum-specifier must use a simple identifier");
13630 /* Look for trailing attributes to apply to this enumeration, and
13631 apply them if appropriate. */
13632 if (cp_parser_allow_gnu_extensions_p (parser))
13634 tree trailing_attr = cp_parser_attributes_opt (parser);
13635 trailing_attr = chainon (trailing_attr, attributes);
13636 cplus_decl_attributes (&type,
13638 (int) ATTR_FLAG_TYPE_IN_PLACE);
13641 /* Finish up the enumeration. */
13642 if (type != error_mark_node)
13644 if (new_value_list)
13645 finish_enum_value_list (type);
13647 finish_enum (type);
13650 if (nested_name_specifier)
13652 if (CLASS_TYPE_P (nested_name_specifier))
13654 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13655 pop_scope (nested_name_specifier);
13657 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13659 pop_nested_namespace (nested_name_specifier);
13663 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13667 /* Parse an enumerator-list. The enumerators all have the indicated
13671 enumerator-definition
13672 enumerator-list , enumerator-definition */
13675 cp_parser_enumerator_list (cp_parser* parser, tree type)
13679 /* Parse an enumerator-definition. */
13680 cp_parser_enumerator_definition (parser, type);
13682 /* If the next token is not a ',', we've reached the end of
13684 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13686 /* Otherwise, consume the `,' and keep going. */
13687 cp_lexer_consume_token (parser->lexer);
13688 /* If the next token is a `}', there is a trailing comma. */
13689 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13691 if (!in_system_header)
13692 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13698 /* Parse an enumerator-definition. The enumerator has the indicated
13701 enumerator-definition:
13703 enumerator = constant-expression
13709 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13715 /* Save the input location because we are interested in the location
13716 of the identifier and not the location of the explicit value. */
13717 loc = cp_lexer_peek_token (parser->lexer)->location;
13719 /* Look for the identifier. */
13720 identifier = cp_parser_identifier (parser);
13721 if (identifier == error_mark_node)
13724 /* If the next token is an '=', then there is an explicit value. */
13725 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13727 /* Consume the `=' token. */
13728 cp_lexer_consume_token (parser->lexer);
13729 /* Parse the value. */
13730 value = cp_parser_constant_expression (parser,
13731 /*allow_non_constant_p=*/false,
13737 /* If we are processing a template, make sure the initializer of the
13738 enumerator doesn't contain any bare template parameter pack. */
13739 if (check_for_bare_parameter_packs (value))
13740 value = error_mark_node;
13742 /* Create the enumerator. */
13743 build_enumerator (identifier, value, type, loc);
13746 /* Parse a namespace-name.
13749 original-namespace-name
13752 Returns the NAMESPACE_DECL for the namespace. */
13755 cp_parser_namespace_name (cp_parser* parser)
13758 tree namespace_decl;
13760 cp_token *token = cp_lexer_peek_token (parser->lexer);
13762 /* Get the name of the namespace. */
13763 identifier = cp_parser_identifier (parser);
13764 if (identifier == error_mark_node)
13765 return error_mark_node;
13767 /* Look up the identifier in the currently active scope. Look only
13768 for namespaces, due to:
13770 [basic.lookup.udir]
13772 When looking up a namespace-name in a using-directive or alias
13773 definition, only namespace names are considered.
13777 [basic.lookup.qual]
13779 During the lookup of a name preceding the :: scope resolution
13780 operator, object, function, and enumerator names are ignored.
13782 (Note that cp_parser_qualifying_entity only calls this
13783 function if the token after the name is the scope resolution
13785 namespace_decl = cp_parser_lookup_name (parser, identifier,
13787 /*is_template=*/false,
13788 /*is_namespace=*/true,
13789 /*check_dependency=*/true,
13790 /*ambiguous_decls=*/NULL,
13792 /* If it's not a namespace, issue an error. */
13793 if (namespace_decl == error_mark_node
13794 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13796 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13797 error_at (token->location, "%qD is not a namespace-name", identifier);
13798 cp_parser_error (parser, "expected namespace-name");
13799 namespace_decl = error_mark_node;
13802 return namespace_decl;
13805 /* Parse a namespace-definition.
13807 namespace-definition:
13808 named-namespace-definition
13809 unnamed-namespace-definition
13811 named-namespace-definition:
13812 original-namespace-definition
13813 extension-namespace-definition
13815 original-namespace-definition:
13816 namespace identifier { namespace-body }
13818 extension-namespace-definition:
13819 namespace original-namespace-name { namespace-body }
13821 unnamed-namespace-definition:
13822 namespace { namespace-body } */
13825 cp_parser_namespace_definition (cp_parser* parser)
13827 tree identifier, attribs;
13828 bool has_visibility;
13831 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13833 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13835 cp_lexer_consume_token (parser->lexer);
13840 /* Look for the `namespace' keyword. */
13841 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13843 /* Get the name of the namespace. We do not attempt to distinguish
13844 between an original-namespace-definition and an
13845 extension-namespace-definition at this point. The semantic
13846 analysis routines are responsible for that. */
13847 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13848 identifier = cp_parser_identifier (parser);
13850 identifier = NULL_TREE;
13852 /* Parse any specified attributes. */
13853 attribs = cp_parser_attributes_opt (parser);
13855 /* Look for the `{' to start the namespace. */
13856 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13857 /* Start the namespace. */
13858 push_namespace (identifier);
13860 /* "inline namespace" is equivalent to a stub namespace definition
13861 followed by a strong using directive. */
13864 tree name_space = current_namespace;
13865 /* Set up namespace association. */
13866 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13867 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13868 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13869 /* Import the contents of the inline namespace. */
13871 do_using_directive (name_space);
13872 push_namespace (identifier);
13875 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13877 /* Parse the body of the namespace. */
13878 cp_parser_namespace_body (parser);
13880 if (has_visibility)
13881 pop_visibility (1);
13883 /* Finish the namespace. */
13885 /* Look for the final `}'. */
13886 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13889 /* Parse a namespace-body.
13892 declaration-seq [opt] */
13895 cp_parser_namespace_body (cp_parser* parser)
13897 cp_parser_declaration_seq_opt (parser);
13900 /* Parse a namespace-alias-definition.
13902 namespace-alias-definition:
13903 namespace identifier = qualified-namespace-specifier ; */
13906 cp_parser_namespace_alias_definition (cp_parser* parser)
13909 tree namespace_specifier;
13911 cp_token *token = cp_lexer_peek_token (parser->lexer);
13913 /* Look for the `namespace' keyword. */
13914 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13915 /* Look for the identifier. */
13916 identifier = cp_parser_identifier (parser);
13917 if (identifier == error_mark_node)
13919 /* Look for the `=' token. */
13920 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13921 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13923 error_at (token->location, "%<namespace%> definition is not allowed here");
13924 /* Skip the definition. */
13925 cp_lexer_consume_token (parser->lexer);
13926 if (cp_parser_skip_to_closing_brace (parser))
13927 cp_lexer_consume_token (parser->lexer);
13930 cp_parser_require (parser, CPP_EQ, RT_EQ);
13931 /* Look for the qualified-namespace-specifier. */
13932 namespace_specifier
13933 = cp_parser_qualified_namespace_specifier (parser);
13934 /* Look for the `;' token. */
13935 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13937 /* Register the alias in the symbol table. */
13938 do_namespace_alias (identifier, namespace_specifier);
13941 /* Parse a qualified-namespace-specifier.
13943 qualified-namespace-specifier:
13944 :: [opt] nested-name-specifier [opt] namespace-name
13946 Returns a NAMESPACE_DECL corresponding to the specified
13950 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13952 /* Look for the optional `::'. */
13953 cp_parser_global_scope_opt (parser,
13954 /*current_scope_valid_p=*/false);
13956 /* Look for the optional nested-name-specifier. */
13957 cp_parser_nested_name_specifier_opt (parser,
13958 /*typename_keyword_p=*/false,
13959 /*check_dependency_p=*/true,
13961 /*is_declaration=*/true);
13963 return cp_parser_namespace_name (parser);
13966 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13967 access declaration.
13970 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13971 using :: unqualified-id ;
13973 access-declaration:
13979 cp_parser_using_declaration (cp_parser* parser,
13980 bool access_declaration_p)
13983 bool typename_p = false;
13984 bool global_scope_p;
13989 if (access_declaration_p)
13990 cp_parser_parse_tentatively (parser);
13993 /* Look for the `using' keyword. */
13994 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13996 /* Peek at the next token. */
13997 token = cp_lexer_peek_token (parser->lexer);
13998 /* See if it's `typename'. */
13999 if (token->keyword == RID_TYPENAME)
14001 /* Remember that we've seen it. */
14003 /* Consume the `typename' token. */
14004 cp_lexer_consume_token (parser->lexer);
14008 /* Look for the optional global scope qualification. */
14010 = (cp_parser_global_scope_opt (parser,
14011 /*current_scope_valid_p=*/false)
14014 /* If we saw `typename', or didn't see `::', then there must be a
14015 nested-name-specifier present. */
14016 if (typename_p || !global_scope_p)
14017 qscope = cp_parser_nested_name_specifier (parser, typename_p,
14018 /*check_dependency_p=*/true,
14020 /*is_declaration=*/true);
14021 /* Otherwise, we could be in either of the two productions. In that
14022 case, treat the nested-name-specifier as optional. */
14024 qscope = cp_parser_nested_name_specifier_opt (parser,
14025 /*typename_keyword_p=*/false,
14026 /*check_dependency_p=*/true,
14028 /*is_declaration=*/true);
14030 qscope = global_namespace;
14032 if (access_declaration_p && cp_parser_error_occurred (parser))
14033 /* Something has already gone wrong; there's no need to parse
14034 further. Since an error has occurred, the return value of
14035 cp_parser_parse_definitely will be false, as required. */
14036 return cp_parser_parse_definitely (parser);
14038 token = cp_lexer_peek_token (parser->lexer);
14039 /* Parse the unqualified-id. */
14040 identifier = cp_parser_unqualified_id (parser,
14041 /*template_keyword_p=*/false,
14042 /*check_dependency_p=*/true,
14043 /*declarator_p=*/true,
14044 /*optional_p=*/false);
14046 if (access_declaration_p)
14048 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14049 cp_parser_simulate_error (parser);
14050 if (!cp_parser_parse_definitely (parser))
14054 /* The function we call to handle a using-declaration is different
14055 depending on what scope we are in. */
14056 if (qscope == error_mark_node || identifier == error_mark_node)
14058 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14059 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14060 /* [namespace.udecl]
14062 A using declaration shall not name a template-id. */
14063 error_at (token->location,
14064 "a template-id may not appear in a using-declaration");
14067 if (at_class_scope_p ())
14069 /* Create the USING_DECL. */
14070 decl = do_class_using_decl (parser->scope, identifier);
14072 if (check_for_bare_parameter_packs (decl))
14075 /* Add it to the list of members in this class. */
14076 finish_member_declaration (decl);
14080 decl = cp_parser_lookup_name_simple (parser,
14083 if (decl == error_mark_node)
14084 cp_parser_name_lookup_error (parser, identifier,
14087 else if (check_for_bare_parameter_packs (decl))
14089 else if (!at_namespace_scope_p ())
14090 do_local_using_decl (decl, qscope, identifier);
14092 do_toplevel_using_decl (decl, qscope, identifier);
14096 /* Look for the final `;'. */
14097 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14102 /* Parse a using-directive.
14105 using namespace :: [opt] nested-name-specifier [opt]
14106 namespace-name ; */
14109 cp_parser_using_directive (cp_parser* parser)
14111 tree namespace_decl;
14114 /* Look for the `using' keyword. */
14115 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14116 /* And the `namespace' keyword. */
14117 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14118 /* Look for the optional `::' operator. */
14119 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14120 /* And the optional nested-name-specifier. */
14121 cp_parser_nested_name_specifier_opt (parser,
14122 /*typename_keyword_p=*/false,
14123 /*check_dependency_p=*/true,
14125 /*is_declaration=*/true);
14126 /* Get the namespace being used. */
14127 namespace_decl = cp_parser_namespace_name (parser);
14128 /* And any specified attributes. */
14129 attribs = cp_parser_attributes_opt (parser);
14130 /* Update the symbol table. */
14131 parse_using_directive (namespace_decl, attribs);
14132 /* Look for the final `;'. */
14133 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14136 /* Parse an asm-definition.
14139 asm ( string-literal ) ;
14144 asm volatile [opt] ( string-literal ) ;
14145 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14146 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14147 : asm-operand-list [opt] ) ;
14148 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14149 : asm-operand-list [opt]
14150 : asm-clobber-list [opt] ) ;
14151 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14152 : asm-clobber-list [opt]
14153 : asm-goto-list ) ; */
14156 cp_parser_asm_definition (cp_parser* parser)
14159 tree outputs = NULL_TREE;
14160 tree inputs = NULL_TREE;
14161 tree clobbers = NULL_TREE;
14162 tree labels = NULL_TREE;
14164 bool volatile_p = false;
14165 bool extended_p = false;
14166 bool invalid_inputs_p = false;
14167 bool invalid_outputs_p = false;
14168 bool goto_p = false;
14169 required_token missing = RT_NONE;
14171 /* Look for the `asm' keyword. */
14172 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14173 /* See if the next token is `volatile'. */
14174 if (cp_parser_allow_gnu_extensions_p (parser)
14175 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14177 /* Remember that we saw the `volatile' keyword. */
14179 /* Consume the token. */
14180 cp_lexer_consume_token (parser->lexer);
14182 if (cp_parser_allow_gnu_extensions_p (parser)
14183 && parser->in_function_body
14184 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14186 /* Remember that we saw the `goto' keyword. */
14188 /* Consume the token. */
14189 cp_lexer_consume_token (parser->lexer);
14191 /* Look for the opening `('. */
14192 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14194 /* Look for the string. */
14195 string = cp_parser_string_literal (parser, false, false);
14196 if (string == error_mark_node)
14198 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14199 /*consume_paren=*/true);
14203 /* If we're allowing GNU extensions, check for the extended assembly
14204 syntax. Unfortunately, the `:' tokens need not be separated by
14205 a space in C, and so, for compatibility, we tolerate that here
14206 too. Doing that means that we have to treat the `::' operator as
14208 if (cp_parser_allow_gnu_extensions_p (parser)
14209 && parser->in_function_body
14210 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14211 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14213 bool inputs_p = false;
14214 bool clobbers_p = false;
14215 bool labels_p = false;
14217 /* The extended syntax was used. */
14220 /* Look for outputs. */
14221 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14223 /* Consume the `:'. */
14224 cp_lexer_consume_token (parser->lexer);
14225 /* Parse the output-operands. */
14226 if (cp_lexer_next_token_is_not (parser->lexer,
14228 && cp_lexer_next_token_is_not (parser->lexer,
14230 && cp_lexer_next_token_is_not (parser->lexer,
14233 outputs = cp_parser_asm_operand_list (parser);
14235 if (outputs == error_mark_node)
14236 invalid_outputs_p = true;
14238 /* If the next token is `::', there are no outputs, and the
14239 next token is the beginning of the inputs. */
14240 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14241 /* The inputs are coming next. */
14244 /* Look for inputs. */
14246 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14248 /* Consume the `:' or `::'. */
14249 cp_lexer_consume_token (parser->lexer);
14250 /* Parse the output-operands. */
14251 if (cp_lexer_next_token_is_not (parser->lexer,
14253 && cp_lexer_next_token_is_not (parser->lexer,
14255 && cp_lexer_next_token_is_not (parser->lexer,
14257 inputs = cp_parser_asm_operand_list (parser);
14259 if (inputs == error_mark_node)
14260 invalid_inputs_p = true;
14262 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14263 /* The clobbers are coming next. */
14266 /* Look for clobbers. */
14268 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14271 /* Consume the `:' or `::'. */
14272 cp_lexer_consume_token (parser->lexer);
14273 /* Parse the clobbers. */
14274 if (cp_lexer_next_token_is_not (parser->lexer,
14276 && cp_lexer_next_token_is_not (parser->lexer,
14278 clobbers = cp_parser_asm_clobber_list (parser);
14281 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14282 /* The labels are coming next. */
14285 /* Look for labels. */
14287 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14290 /* Consume the `:' or `::'. */
14291 cp_lexer_consume_token (parser->lexer);
14292 /* Parse the labels. */
14293 labels = cp_parser_asm_label_list (parser);
14296 if (goto_p && !labels_p)
14297 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14300 missing = RT_COLON_SCOPE;
14302 /* Look for the closing `)'. */
14303 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14304 missing ? missing : RT_CLOSE_PAREN))
14305 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14306 /*consume_paren=*/true);
14307 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14309 if (!invalid_inputs_p && !invalid_outputs_p)
14311 /* Create the ASM_EXPR. */
14312 if (parser->in_function_body)
14314 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14315 inputs, clobbers, labels);
14316 /* If the extended syntax was not used, mark the ASM_EXPR. */
14319 tree temp = asm_stmt;
14320 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14321 temp = TREE_OPERAND (temp, 0);
14323 ASM_INPUT_P (temp) = 1;
14327 cgraph_add_asm_node (string);
14331 /* Declarators [gram.dcl.decl] */
14333 /* Parse an init-declarator.
14336 declarator initializer [opt]
14341 declarator asm-specification [opt] attributes [opt] initializer [opt]
14343 function-definition:
14344 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14346 decl-specifier-seq [opt] declarator function-try-block
14350 function-definition:
14351 __extension__ function-definition
14353 The DECL_SPECIFIERS apply to this declarator. Returns a
14354 representation of the entity declared. If MEMBER_P is TRUE, then
14355 this declarator appears in a class scope. The new DECL created by
14356 this declarator is returned.
14358 The CHECKS are access checks that should be performed once we know
14359 what entity is being declared (and, therefore, what classes have
14362 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14363 for a function-definition here as well. If the declarator is a
14364 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14365 be TRUE upon return. By that point, the function-definition will
14366 have been completely parsed.
14368 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14372 cp_parser_init_declarator (cp_parser* parser,
14373 cp_decl_specifier_seq *decl_specifiers,
14374 VEC (deferred_access_check,gc)* checks,
14375 bool function_definition_allowed_p,
14377 int declares_class_or_enum,
14378 bool* function_definition_p)
14380 cp_token *token = NULL, *asm_spec_start_token = NULL,
14381 *attributes_start_token = NULL;
14382 cp_declarator *declarator;
14383 tree prefix_attributes;
14385 tree asm_specification;
14387 tree decl = NULL_TREE;
14389 int is_initialized;
14390 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14391 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14393 enum cpp_ttype initialization_kind;
14394 bool is_direct_init = false;
14395 bool is_non_constant_init;
14396 int ctor_dtor_or_conv_p;
14398 tree pushed_scope = NULL;
14400 /* Gather the attributes that were provided with the
14401 decl-specifiers. */
14402 prefix_attributes = decl_specifiers->attributes;
14404 /* Assume that this is not the declarator for a function
14406 if (function_definition_p)
14407 *function_definition_p = false;
14409 /* Defer access checks while parsing the declarator; we cannot know
14410 what names are accessible until we know what is being
14412 resume_deferring_access_checks ();
14414 /* Parse the declarator. */
14415 token = cp_lexer_peek_token (parser->lexer);
14417 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14418 &ctor_dtor_or_conv_p,
14419 /*parenthesized_p=*/NULL,
14420 /*member_p=*/false);
14421 /* Gather up the deferred checks. */
14422 stop_deferring_access_checks ();
14424 /* If the DECLARATOR was erroneous, there's no need to go
14426 if (declarator == cp_error_declarator)
14427 return error_mark_node;
14429 /* Check that the number of template-parameter-lists is OK. */
14430 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14432 return error_mark_node;
14434 if (declares_class_or_enum & 2)
14435 cp_parser_check_for_definition_in_return_type (declarator,
14436 decl_specifiers->type,
14437 decl_specifiers->type_location);
14439 /* Figure out what scope the entity declared by the DECLARATOR is
14440 located in. `grokdeclarator' sometimes changes the scope, so
14441 we compute it now. */
14442 scope = get_scope_of_declarator (declarator);
14444 /* Perform any lookups in the declared type which were thought to be
14445 dependent, but are not in the scope of the declarator. */
14446 decl_specifiers->type
14447 = maybe_update_decl_type (decl_specifiers->type, scope);
14449 /* If we're allowing GNU extensions, look for an asm-specification
14451 if (cp_parser_allow_gnu_extensions_p (parser))
14453 /* Look for an asm-specification. */
14454 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14455 asm_specification = cp_parser_asm_specification_opt (parser);
14456 /* And attributes. */
14457 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14458 attributes = cp_parser_attributes_opt (parser);
14462 asm_specification = NULL_TREE;
14463 attributes = NULL_TREE;
14466 /* Peek at the next token. */
14467 token = cp_lexer_peek_token (parser->lexer);
14468 /* Check to see if the token indicates the start of a
14469 function-definition. */
14470 if (function_declarator_p (declarator)
14471 && cp_parser_token_starts_function_definition_p (token))
14473 if (!function_definition_allowed_p)
14475 /* If a function-definition should not appear here, issue an
14477 cp_parser_error (parser,
14478 "a function-definition is not allowed here");
14479 return error_mark_node;
14483 location_t func_brace_location
14484 = cp_lexer_peek_token (parser->lexer)->location;
14486 /* Neither attributes nor an asm-specification are allowed
14487 on a function-definition. */
14488 if (asm_specification)
14489 error_at (asm_spec_start_token->location,
14490 "an asm-specification is not allowed "
14491 "on a function-definition");
14493 error_at (attributes_start_token->location,
14494 "attributes are not allowed on a function-definition");
14495 /* This is a function-definition. */
14496 *function_definition_p = true;
14498 /* Parse the function definition. */
14500 decl = cp_parser_save_member_function_body (parser,
14503 prefix_attributes);
14506 = (cp_parser_function_definition_from_specifiers_and_declarator
14507 (parser, decl_specifiers, prefix_attributes, declarator));
14509 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14511 /* This is where the prologue starts... */
14512 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14513 = func_brace_location;
14522 Only in function declarations for constructors, destructors, and
14523 type conversions can the decl-specifier-seq be omitted.
14525 We explicitly postpone this check past the point where we handle
14526 function-definitions because we tolerate function-definitions
14527 that are missing their return types in some modes. */
14528 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14530 cp_parser_error (parser,
14531 "expected constructor, destructor, or type conversion");
14532 return error_mark_node;
14535 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14536 if (token->type == CPP_EQ
14537 || token->type == CPP_OPEN_PAREN
14538 || token->type == CPP_OPEN_BRACE)
14540 is_initialized = SD_INITIALIZED;
14541 initialization_kind = token->type;
14543 if (token->type == CPP_EQ
14544 && function_declarator_p (declarator))
14546 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14547 if (t2->keyword == RID_DEFAULT)
14548 is_initialized = SD_DEFAULTED;
14549 else if (t2->keyword == RID_DELETE)
14550 is_initialized = SD_DELETED;
14555 /* If the init-declarator isn't initialized and isn't followed by a
14556 `,' or `;', it's not a valid init-declarator. */
14557 if (token->type != CPP_COMMA
14558 && token->type != CPP_SEMICOLON)
14560 cp_parser_error (parser, "expected initializer");
14561 return error_mark_node;
14563 is_initialized = SD_UNINITIALIZED;
14564 initialization_kind = CPP_EOF;
14567 /* Because start_decl has side-effects, we should only call it if we
14568 know we're going ahead. By this point, we know that we cannot
14569 possibly be looking at any other construct. */
14570 cp_parser_commit_to_tentative_parse (parser);
14572 /* If the decl specifiers were bad, issue an error now that we're
14573 sure this was intended to be a declarator. Then continue
14574 declaring the variable(s), as int, to try to cut down on further
14576 if (decl_specifiers->any_specifiers_p
14577 && decl_specifiers->type == error_mark_node)
14579 cp_parser_error (parser, "invalid type in declaration");
14580 decl_specifiers->type = integer_type_node;
14583 /* Check to see whether or not this declaration is a friend. */
14584 friend_p = cp_parser_friend_p (decl_specifiers);
14586 /* Enter the newly declared entry in the symbol table. If we're
14587 processing a declaration in a class-specifier, we wait until
14588 after processing the initializer. */
14591 if (parser->in_unbraced_linkage_specification_p)
14592 decl_specifiers->storage_class = sc_extern;
14593 decl = start_decl (declarator, decl_specifiers,
14594 is_initialized, attributes, prefix_attributes,
14596 /* Adjust location of decl if declarator->id_loc is more appropriate:
14597 set, and decl wasn't merged with another decl, in which case its
14598 location would be different from input_location, and more accurate. */
14600 && declarator->id_loc != UNKNOWN_LOCATION
14601 && DECL_SOURCE_LOCATION (decl) == input_location)
14602 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14605 /* Enter the SCOPE. That way unqualified names appearing in the
14606 initializer will be looked up in SCOPE. */
14607 pushed_scope = push_scope (scope);
14609 /* Perform deferred access control checks, now that we know in which
14610 SCOPE the declared entity resides. */
14611 if (!member_p && decl)
14613 tree saved_current_function_decl = NULL_TREE;
14615 /* If the entity being declared is a function, pretend that we
14616 are in its scope. If it is a `friend', it may have access to
14617 things that would not otherwise be accessible. */
14618 if (TREE_CODE (decl) == FUNCTION_DECL)
14620 saved_current_function_decl = current_function_decl;
14621 current_function_decl = decl;
14624 /* Perform access checks for template parameters. */
14625 cp_parser_perform_template_parameter_access_checks (checks);
14627 /* Perform the access control checks for the declarator and the
14628 decl-specifiers. */
14629 perform_deferred_access_checks ();
14631 /* Restore the saved value. */
14632 if (TREE_CODE (decl) == FUNCTION_DECL)
14633 current_function_decl = saved_current_function_decl;
14636 /* Parse the initializer. */
14637 initializer = NULL_TREE;
14638 is_direct_init = false;
14639 is_non_constant_init = true;
14640 if (is_initialized)
14642 if (function_declarator_p (declarator))
14644 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14645 if (initialization_kind == CPP_EQ)
14646 initializer = cp_parser_pure_specifier (parser);
14649 /* If the declaration was erroneous, we don't really
14650 know what the user intended, so just silently
14651 consume the initializer. */
14652 if (decl != error_mark_node)
14653 error_at (initializer_start_token->location,
14654 "initializer provided for function");
14655 cp_parser_skip_to_closing_parenthesis (parser,
14656 /*recovering=*/true,
14657 /*or_comma=*/false,
14658 /*consume_paren=*/true);
14663 /* We want to record the extra mangling scope for in-class
14664 initializers of class members and initializers of static data
14665 member templates. The former is a C++0x feature which isn't
14666 implemented yet, and I expect it will involve deferring
14667 parsing of the initializer until end of class as with default
14668 arguments. So right here we only handle the latter. */
14669 if (!member_p && processing_template_decl)
14670 start_lambda_scope (decl);
14671 initializer = cp_parser_initializer (parser,
14673 &is_non_constant_init);
14674 if (!member_p && processing_template_decl)
14675 finish_lambda_scope ();
14679 /* The old parser allows attributes to appear after a parenthesized
14680 initializer. Mark Mitchell proposed removing this functionality
14681 on the GCC mailing lists on 2002-08-13. This parser accepts the
14682 attributes -- but ignores them. */
14683 if (cp_parser_allow_gnu_extensions_p (parser)
14684 && initialization_kind == CPP_OPEN_PAREN)
14685 if (cp_parser_attributes_opt (parser))
14686 warning (OPT_Wattributes,
14687 "attributes after parenthesized initializer ignored");
14689 /* For an in-class declaration, use `grokfield' to create the
14695 pop_scope (pushed_scope);
14696 pushed_scope = false;
14698 decl = grokfield (declarator, decl_specifiers,
14699 initializer, !is_non_constant_init,
14700 /*asmspec=*/NULL_TREE,
14701 prefix_attributes);
14702 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14703 cp_parser_save_default_args (parser, decl);
14706 /* Finish processing the declaration. But, skip friend
14708 if (!friend_p && decl && decl != error_mark_node)
14710 cp_finish_decl (decl,
14711 initializer, !is_non_constant_init,
14713 /* If the initializer is in parentheses, then this is
14714 a direct-initialization, which means that an
14715 `explicit' constructor is OK. Otherwise, an
14716 `explicit' constructor cannot be used. */
14717 ((is_direct_init || !is_initialized)
14718 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14720 else if ((cxx_dialect != cxx98) && friend_p
14721 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14722 /* Core issue #226 (C++0x only): A default template-argument
14723 shall not be specified in a friend class template
14725 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14726 /*is_partial=*/0, /*is_friend_decl=*/1);
14728 if (!friend_p && pushed_scope)
14729 pop_scope (pushed_scope);
14734 /* Parse a declarator.
14738 ptr-operator declarator
14740 abstract-declarator:
14741 ptr-operator abstract-declarator [opt]
14742 direct-abstract-declarator
14747 attributes [opt] direct-declarator
14748 attributes [opt] ptr-operator declarator
14750 abstract-declarator:
14751 attributes [opt] ptr-operator abstract-declarator [opt]
14752 attributes [opt] direct-abstract-declarator
14754 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14755 detect constructor, destructor or conversion operators. It is set
14756 to -1 if the declarator is a name, and +1 if it is a
14757 function. Otherwise it is set to zero. Usually you just want to
14758 test for >0, but internally the negative value is used.
14760 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14761 a decl-specifier-seq unless it declares a constructor, destructor,
14762 or conversion. It might seem that we could check this condition in
14763 semantic analysis, rather than parsing, but that makes it difficult
14764 to handle something like `f()'. We want to notice that there are
14765 no decl-specifiers, and therefore realize that this is an
14766 expression, not a declaration.)
14768 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14769 the declarator is a direct-declarator of the form "(...)".
14771 MEMBER_P is true iff this declarator is a member-declarator. */
14773 static cp_declarator *
14774 cp_parser_declarator (cp_parser* parser,
14775 cp_parser_declarator_kind dcl_kind,
14776 int* ctor_dtor_or_conv_p,
14777 bool* parenthesized_p,
14780 cp_declarator *declarator;
14781 enum tree_code code;
14782 cp_cv_quals cv_quals;
14784 tree attributes = NULL_TREE;
14786 /* Assume this is not a constructor, destructor, or type-conversion
14788 if (ctor_dtor_or_conv_p)
14789 *ctor_dtor_or_conv_p = 0;
14791 if (cp_parser_allow_gnu_extensions_p (parser))
14792 attributes = cp_parser_attributes_opt (parser);
14794 /* Check for the ptr-operator production. */
14795 cp_parser_parse_tentatively (parser);
14796 /* Parse the ptr-operator. */
14797 code = cp_parser_ptr_operator (parser,
14800 /* If that worked, then we have a ptr-operator. */
14801 if (cp_parser_parse_definitely (parser))
14803 /* If a ptr-operator was found, then this declarator was not
14805 if (parenthesized_p)
14806 *parenthesized_p = true;
14807 /* The dependent declarator is optional if we are parsing an
14808 abstract-declarator. */
14809 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14810 cp_parser_parse_tentatively (parser);
14812 /* Parse the dependent declarator. */
14813 declarator = cp_parser_declarator (parser, dcl_kind,
14814 /*ctor_dtor_or_conv_p=*/NULL,
14815 /*parenthesized_p=*/NULL,
14816 /*member_p=*/false);
14818 /* If we are parsing an abstract-declarator, we must handle the
14819 case where the dependent declarator is absent. */
14820 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14821 && !cp_parser_parse_definitely (parser))
14824 declarator = cp_parser_make_indirect_declarator
14825 (code, class_type, cv_quals, declarator);
14827 /* Everything else is a direct-declarator. */
14830 if (parenthesized_p)
14831 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14833 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14834 ctor_dtor_or_conv_p,
14838 if (attributes && declarator && declarator != cp_error_declarator)
14839 declarator->attributes = attributes;
14844 /* Parse a direct-declarator or direct-abstract-declarator.
14848 direct-declarator ( parameter-declaration-clause )
14849 cv-qualifier-seq [opt]
14850 exception-specification [opt]
14851 direct-declarator [ constant-expression [opt] ]
14854 direct-abstract-declarator:
14855 direct-abstract-declarator [opt]
14856 ( parameter-declaration-clause )
14857 cv-qualifier-seq [opt]
14858 exception-specification [opt]
14859 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14860 ( abstract-declarator )
14862 Returns a representation of the declarator. DCL_KIND is
14863 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14864 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14865 we are parsing a direct-declarator. It is
14866 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14867 of ambiguity we prefer an abstract declarator, as per
14868 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14869 cp_parser_declarator. */
14871 static cp_declarator *
14872 cp_parser_direct_declarator (cp_parser* parser,
14873 cp_parser_declarator_kind dcl_kind,
14874 int* ctor_dtor_or_conv_p,
14878 cp_declarator *declarator = NULL;
14879 tree scope = NULL_TREE;
14880 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14881 bool saved_in_declarator_p = parser->in_declarator_p;
14883 tree pushed_scope = NULL_TREE;
14887 /* Peek at the next token. */
14888 token = cp_lexer_peek_token (parser->lexer);
14889 if (token->type == CPP_OPEN_PAREN)
14891 /* This is either a parameter-declaration-clause, or a
14892 parenthesized declarator. When we know we are parsing a
14893 named declarator, it must be a parenthesized declarator
14894 if FIRST is true. For instance, `(int)' is a
14895 parameter-declaration-clause, with an omitted
14896 direct-abstract-declarator. But `((*))', is a
14897 parenthesized abstract declarator. Finally, when T is a
14898 template parameter `(T)' is a
14899 parameter-declaration-clause, and not a parenthesized
14902 We first try and parse a parameter-declaration-clause,
14903 and then try a nested declarator (if FIRST is true).
14905 It is not an error for it not to be a
14906 parameter-declaration-clause, even when FIRST is
14912 The first is the declaration of a function while the
14913 second is the definition of a variable, including its
14916 Having seen only the parenthesis, we cannot know which of
14917 these two alternatives should be selected. Even more
14918 complex are examples like:
14923 The former is a function-declaration; the latter is a
14924 variable initialization.
14926 Thus again, we try a parameter-declaration-clause, and if
14927 that fails, we back out and return. */
14929 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14932 unsigned saved_num_template_parameter_lists;
14933 bool is_declarator = false;
14936 /* In a member-declarator, the only valid interpretation
14937 of a parenthesis is the start of a
14938 parameter-declaration-clause. (It is invalid to
14939 initialize a static data member with a parenthesized
14940 initializer; only the "=" form of initialization is
14943 cp_parser_parse_tentatively (parser);
14945 /* Consume the `('. */
14946 cp_lexer_consume_token (parser->lexer);
14949 /* If this is going to be an abstract declarator, we're
14950 in a declarator and we can't have default args. */
14951 parser->default_arg_ok_p = false;
14952 parser->in_declarator_p = true;
14955 /* Inside the function parameter list, surrounding
14956 template-parameter-lists do not apply. */
14957 saved_num_template_parameter_lists
14958 = parser->num_template_parameter_lists;
14959 parser->num_template_parameter_lists = 0;
14961 begin_scope (sk_function_parms, NULL_TREE);
14963 /* Parse the parameter-declaration-clause. */
14964 params = cp_parser_parameter_declaration_clause (parser);
14966 parser->num_template_parameter_lists
14967 = saved_num_template_parameter_lists;
14969 /* If all went well, parse the cv-qualifier-seq and the
14970 exception-specification. */
14971 if (member_p || cp_parser_parse_definitely (parser))
14973 cp_cv_quals cv_quals;
14974 tree exception_specification;
14977 is_declarator = true;
14979 if (ctor_dtor_or_conv_p)
14980 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14982 /* Consume the `)'. */
14983 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14985 /* Parse the cv-qualifier-seq. */
14986 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14987 /* And the exception-specification. */
14988 exception_specification
14989 = cp_parser_exception_specification_opt (parser);
14992 = cp_parser_late_return_type_opt (parser);
14994 /* Create the function-declarator. */
14995 declarator = make_call_declarator (declarator,
14998 exception_specification,
15000 /* Any subsequent parameter lists are to do with
15001 return type, so are not those of the declared
15003 parser->default_arg_ok_p = false;
15006 /* Remove the function parms from scope. */
15007 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15008 pop_binding (DECL_NAME (t), t);
15012 /* Repeat the main loop. */
15016 /* If this is the first, we can try a parenthesized
15020 bool saved_in_type_id_in_expr_p;
15022 parser->default_arg_ok_p = saved_default_arg_ok_p;
15023 parser->in_declarator_p = saved_in_declarator_p;
15025 /* Consume the `('. */
15026 cp_lexer_consume_token (parser->lexer);
15027 /* Parse the nested declarator. */
15028 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15029 parser->in_type_id_in_expr_p = true;
15031 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15032 /*parenthesized_p=*/NULL,
15034 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15036 /* Expect a `)'. */
15037 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15038 declarator = cp_error_declarator;
15039 if (declarator == cp_error_declarator)
15042 goto handle_declarator;
15044 /* Otherwise, we must be done. */
15048 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15049 && token->type == CPP_OPEN_SQUARE)
15051 /* Parse an array-declarator. */
15054 if (ctor_dtor_or_conv_p)
15055 *ctor_dtor_or_conv_p = 0;
15058 parser->default_arg_ok_p = false;
15059 parser->in_declarator_p = true;
15060 /* Consume the `['. */
15061 cp_lexer_consume_token (parser->lexer);
15062 /* Peek at the next token. */
15063 token = cp_lexer_peek_token (parser->lexer);
15064 /* If the next token is `]', then there is no
15065 constant-expression. */
15066 if (token->type != CPP_CLOSE_SQUARE)
15068 bool non_constant_p;
15071 = cp_parser_constant_expression (parser,
15072 /*allow_non_constant=*/true,
15074 if (!non_constant_p || cxx_dialect >= cxx0x)
15076 /* Normally, the array bound must be an integral constant
15077 expression. However, as an extension, we allow VLAs
15078 in function scopes as long as they aren't part of a
15079 parameter declaration. */
15080 else if (!parser->in_function_body
15081 || current_binding_level->kind == sk_function_parms)
15083 cp_parser_error (parser,
15084 "array bound is not an integer constant");
15085 bounds = error_mark_node;
15087 else if (processing_template_decl && !error_operand_p (bounds))
15089 /* Remember this wasn't a constant-expression. */
15090 bounds = build_nop (TREE_TYPE (bounds), bounds);
15091 TREE_SIDE_EFFECTS (bounds) = 1;
15095 bounds = NULL_TREE;
15096 /* Look for the closing `]'. */
15097 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15099 declarator = cp_error_declarator;
15103 declarator = make_array_declarator (declarator, bounds);
15105 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15108 tree qualifying_scope;
15109 tree unqualified_name;
15110 special_function_kind sfk;
15112 bool pack_expansion_p = false;
15113 cp_token *declarator_id_start_token;
15115 /* Parse a declarator-id */
15116 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15119 cp_parser_parse_tentatively (parser);
15121 /* If we see an ellipsis, we should be looking at a
15123 if (token->type == CPP_ELLIPSIS)
15125 /* Consume the `...' */
15126 cp_lexer_consume_token (parser->lexer);
15128 pack_expansion_p = true;
15132 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15134 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15135 qualifying_scope = parser->scope;
15140 if (!unqualified_name && pack_expansion_p)
15142 /* Check whether an error occurred. */
15143 okay = !cp_parser_error_occurred (parser);
15145 /* We already consumed the ellipsis to mark a
15146 parameter pack, but we have no way to report it,
15147 so abort the tentative parse. We will be exiting
15148 immediately anyway. */
15149 cp_parser_abort_tentative_parse (parser);
15152 okay = cp_parser_parse_definitely (parser);
15155 unqualified_name = error_mark_node;
15156 else if (unqualified_name
15157 && (qualifying_scope
15158 || (TREE_CODE (unqualified_name)
15159 != IDENTIFIER_NODE)))
15161 cp_parser_error (parser, "expected unqualified-id");
15162 unqualified_name = error_mark_node;
15166 if (!unqualified_name)
15168 if (unqualified_name == error_mark_node)
15170 declarator = cp_error_declarator;
15171 pack_expansion_p = false;
15172 declarator->parameter_pack_p = false;
15176 if (qualifying_scope && at_namespace_scope_p ()
15177 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15179 /* In the declaration of a member of a template class
15180 outside of the class itself, the SCOPE will sometimes
15181 be a TYPENAME_TYPE. For example, given:
15183 template <typename T>
15184 int S<T>::R::i = 3;
15186 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15187 this context, we must resolve S<T>::R to an ordinary
15188 type, rather than a typename type.
15190 The reason we normally avoid resolving TYPENAME_TYPEs
15191 is that a specialization of `S' might render
15192 `S<T>::R' not a type. However, if `S' is
15193 specialized, then this `i' will not be used, so there
15194 is no harm in resolving the types here. */
15197 /* Resolve the TYPENAME_TYPE. */
15198 type = resolve_typename_type (qualifying_scope,
15199 /*only_current_p=*/false);
15200 /* If that failed, the declarator is invalid. */
15201 if (TREE_CODE (type) == TYPENAME_TYPE)
15203 if (typedef_variant_p (type))
15204 error_at (declarator_id_start_token->location,
15205 "cannot define member of dependent typedef "
15208 error_at (declarator_id_start_token->location,
15209 "%<%T::%E%> is not a type",
15210 TYPE_CONTEXT (qualifying_scope),
15211 TYPE_IDENTIFIER (qualifying_scope));
15213 qualifying_scope = type;
15218 if (unqualified_name)
15222 if (qualifying_scope
15223 && CLASS_TYPE_P (qualifying_scope))
15224 class_type = qualifying_scope;
15226 class_type = current_class_type;
15228 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15230 tree name_type = TREE_TYPE (unqualified_name);
15231 if (class_type && same_type_p (name_type, class_type))
15233 if (qualifying_scope
15234 && CLASSTYPE_USE_TEMPLATE (name_type))
15236 error_at (declarator_id_start_token->location,
15237 "invalid use of constructor as a template");
15238 inform (declarator_id_start_token->location,
15239 "use %<%T::%D%> instead of %<%T::%D%> to "
15240 "name the constructor in a qualified name",
15242 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15243 class_type, name_type);
15244 declarator = cp_error_declarator;
15248 unqualified_name = constructor_name (class_type);
15252 /* We do not attempt to print the declarator
15253 here because we do not have enough
15254 information about its original syntactic
15256 cp_parser_error (parser, "invalid declarator");
15257 declarator = cp_error_declarator;
15264 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15265 sfk = sfk_destructor;
15266 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15267 sfk = sfk_conversion;
15268 else if (/* There's no way to declare a constructor
15269 for an anonymous type, even if the type
15270 got a name for linkage purposes. */
15271 !TYPE_WAS_ANONYMOUS (class_type)
15272 && constructor_name_p (unqualified_name,
15275 unqualified_name = constructor_name (class_type);
15276 sfk = sfk_constructor;
15278 else if (is_overloaded_fn (unqualified_name)
15279 && DECL_CONSTRUCTOR_P (get_first_fn
15280 (unqualified_name)))
15281 sfk = sfk_constructor;
15283 if (ctor_dtor_or_conv_p && sfk != sfk_none)
15284 *ctor_dtor_or_conv_p = -1;
15287 declarator = make_id_declarator (qualifying_scope,
15290 declarator->id_loc = token->location;
15291 declarator->parameter_pack_p = pack_expansion_p;
15293 if (pack_expansion_p)
15294 maybe_warn_variadic_templates ();
15297 handle_declarator:;
15298 scope = get_scope_of_declarator (declarator);
15300 /* Any names that appear after the declarator-id for a
15301 member are looked up in the containing scope. */
15302 pushed_scope = push_scope (scope);
15303 parser->in_declarator_p = true;
15304 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15305 || (declarator && declarator->kind == cdk_id))
15306 /* Default args are only allowed on function
15308 parser->default_arg_ok_p = saved_default_arg_ok_p;
15310 parser->default_arg_ok_p = false;
15319 /* For an abstract declarator, we might wind up with nothing at this
15320 point. That's an error; the declarator is not optional. */
15322 cp_parser_error (parser, "expected declarator");
15324 /* If we entered a scope, we must exit it now. */
15326 pop_scope (pushed_scope);
15328 parser->default_arg_ok_p = saved_default_arg_ok_p;
15329 parser->in_declarator_p = saved_in_declarator_p;
15334 /* Parse a ptr-operator.
15337 * cv-qualifier-seq [opt]
15339 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15344 & cv-qualifier-seq [opt]
15346 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15347 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15348 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15349 filled in with the TYPE containing the member. *CV_QUALS is
15350 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15351 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15352 Note that the tree codes returned by this function have nothing
15353 to do with the types of trees that will be eventually be created
15354 to represent the pointer or reference type being parsed. They are
15355 just constants with suggestive names. */
15356 static enum tree_code
15357 cp_parser_ptr_operator (cp_parser* parser,
15359 cp_cv_quals *cv_quals)
15361 enum tree_code code = ERROR_MARK;
15364 /* Assume that it's not a pointer-to-member. */
15366 /* And that there are no cv-qualifiers. */
15367 *cv_quals = TYPE_UNQUALIFIED;
15369 /* Peek at the next token. */
15370 token = cp_lexer_peek_token (parser->lexer);
15372 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15373 if (token->type == CPP_MULT)
15374 code = INDIRECT_REF;
15375 else if (token->type == CPP_AND)
15377 else if ((cxx_dialect != cxx98) &&
15378 token->type == CPP_AND_AND) /* C++0x only */
15379 code = NON_LVALUE_EXPR;
15381 if (code != ERROR_MARK)
15383 /* Consume the `*', `&' or `&&'. */
15384 cp_lexer_consume_token (parser->lexer);
15386 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15387 `&', if we are allowing GNU extensions. (The only qualifier
15388 that can legally appear after `&' is `restrict', but that is
15389 enforced during semantic analysis. */
15390 if (code == INDIRECT_REF
15391 || cp_parser_allow_gnu_extensions_p (parser))
15392 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15396 /* Try the pointer-to-member case. */
15397 cp_parser_parse_tentatively (parser);
15398 /* Look for the optional `::' operator. */
15399 cp_parser_global_scope_opt (parser,
15400 /*current_scope_valid_p=*/false);
15401 /* Look for the nested-name specifier. */
15402 token = cp_lexer_peek_token (parser->lexer);
15403 cp_parser_nested_name_specifier (parser,
15404 /*typename_keyword_p=*/false,
15405 /*check_dependency_p=*/true,
15407 /*is_declaration=*/false);
15408 /* If we found it, and the next token is a `*', then we are
15409 indeed looking at a pointer-to-member operator. */
15410 if (!cp_parser_error_occurred (parser)
15411 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15413 /* Indicate that the `*' operator was used. */
15414 code = INDIRECT_REF;
15416 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15417 error_at (token->location, "%qD is a namespace", parser->scope);
15420 /* The type of which the member is a member is given by the
15422 *type = parser->scope;
15423 /* The next name will not be qualified. */
15424 parser->scope = NULL_TREE;
15425 parser->qualifying_scope = NULL_TREE;
15426 parser->object_scope = NULL_TREE;
15427 /* Look for the optional cv-qualifier-seq. */
15428 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15431 /* If that didn't work we don't have a ptr-operator. */
15432 if (!cp_parser_parse_definitely (parser))
15433 cp_parser_error (parser, "expected ptr-operator");
15439 /* Parse an (optional) cv-qualifier-seq.
15442 cv-qualifier cv-qualifier-seq [opt]
15453 Returns a bitmask representing the cv-qualifiers. */
15456 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15458 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15463 cp_cv_quals cv_qualifier;
15465 /* Peek at the next token. */
15466 token = cp_lexer_peek_token (parser->lexer);
15467 /* See if it's a cv-qualifier. */
15468 switch (token->keyword)
15471 cv_qualifier = TYPE_QUAL_CONST;
15475 cv_qualifier = TYPE_QUAL_VOLATILE;
15479 cv_qualifier = TYPE_QUAL_RESTRICT;
15483 cv_qualifier = TYPE_UNQUALIFIED;
15490 if (cv_quals & cv_qualifier)
15492 error_at (token->location, "duplicate cv-qualifier");
15493 cp_lexer_purge_token (parser->lexer);
15497 cp_lexer_consume_token (parser->lexer);
15498 cv_quals |= cv_qualifier;
15505 /* Parse a late-specified return type, if any. This is not a separate
15506 non-terminal, but part of a function declarator, which looks like
15508 -> trailing-type-specifier-seq abstract-declarator(opt)
15510 Returns the type indicated by the type-id. */
15513 cp_parser_late_return_type_opt (cp_parser* parser)
15517 /* Peek at the next token. */
15518 token = cp_lexer_peek_token (parser->lexer);
15519 /* A late-specified return type is indicated by an initial '->'. */
15520 if (token->type != CPP_DEREF)
15523 /* Consume the ->. */
15524 cp_lexer_consume_token (parser->lexer);
15526 return cp_parser_trailing_type_id (parser);
15529 /* Parse a declarator-id.
15533 :: [opt] nested-name-specifier [opt] type-name
15535 In the `id-expression' case, the value returned is as for
15536 cp_parser_id_expression if the id-expression was an unqualified-id.
15537 If the id-expression was a qualified-id, then a SCOPE_REF is
15538 returned. The first operand is the scope (either a NAMESPACE_DECL
15539 or TREE_TYPE), but the second is still just a representation of an
15543 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15546 /* The expression must be an id-expression. Assume that qualified
15547 names are the names of types so that:
15550 int S<T>::R::i = 3;
15552 will work; we must treat `S<T>::R' as the name of a type.
15553 Similarly, assume that qualified names are templates, where
15557 int S<T>::R<T>::i = 3;
15560 id = cp_parser_id_expression (parser,
15561 /*template_keyword_p=*/false,
15562 /*check_dependency_p=*/false,
15563 /*template_p=*/NULL,
15564 /*declarator_p=*/true,
15566 if (id && BASELINK_P (id))
15567 id = BASELINK_FUNCTIONS (id);
15571 /* Parse a type-id.
15574 type-specifier-seq abstract-declarator [opt]
15576 Returns the TYPE specified. */
15579 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15580 bool is_trailing_return)
15582 cp_decl_specifier_seq type_specifier_seq;
15583 cp_declarator *abstract_declarator;
15585 /* Parse the type-specifier-seq. */
15586 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15587 is_trailing_return,
15588 &type_specifier_seq);
15589 if (type_specifier_seq.type == error_mark_node)
15590 return error_mark_node;
15592 /* There might or might not be an abstract declarator. */
15593 cp_parser_parse_tentatively (parser);
15594 /* Look for the declarator. */
15595 abstract_declarator
15596 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15597 /*parenthesized_p=*/NULL,
15598 /*member_p=*/false);
15599 /* Check to see if there really was a declarator. */
15600 if (!cp_parser_parse_definitely (parser))
15601 abstract_declarator = NULL;
15603 if (type_specifier_seq.type
15604 && type_uses_auto (type_specifier_seq.type))
15606 /* A type-id with type 'auto' is only ok if the abstract declarator
15607 is a function declarator with a late-specified return type. */
15608 if (abstract_declarator
15609 && abstract_declarator->kind == cdk_function
15610 && abstract_declarator->u.function.late_return_type)
15614 error ("invalid use of %<auto%>");
15615 return error_mark_node;
15619 return groktypename (&type_specifier_seq, abstract_declarator,
15623 static tree cp_parser_type_id (cp_parser *parser)
15625 return cp_parser_type_id_1 (parser, false, false);
15628 static tree cp_parser_template_type_arg (cp_parser *parser)
15630 return cp_parser_type_id_1 (parser, true, false);
15633 static tree cp_parser_trailing_type_id (cp_parser *parser)
15635 return cp_parser_type_id_1 (parser, false, true);
15638 /* Parse a type-specifier-seq.
15640 type-specifier-seq:
15641 type-specifier type-specifier-seq [opt]
15645 type-specifier-seq:
15646 attributes type-specifier-seq [opt]
15648 If IS_DECLARATION is true, we are at the start of a "condition" or
15649 exception-declaration, so we might be followed by a declarator-id.
15651 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15652 i.e. we've just seen "->".
15654 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15657 cp_parser_type_specifier_seq (cp_parser* parser,
15658 bool is_declaration,
15659 bool is_trailing_return,
15660 cp_decl_specifier_seq *type_specifier_seq)
15662 bool seen_type_specifier = false;
15663 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15664 cp_token *start_token = NULL;
15666 /* Clear the TYPE_SPECIFIER_SEQ. */
15667 clear_decl_specs (type_specifier_seq);
15669 /* In the context of a trailing return type, enum E { } is an
15670 elaborated-type-specifier followed by a function-body, not an
15672 if (is_trailing_return)
15673 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15675 /* Parse the type-specifiers and attributes. */
15678 tree type_specifier;
15679 bool is_cv_qualifier;
15681 /* Check for attributes first. */
15682 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15684 type_specifier_seq->attributes =
15685 chainon (type_specifier_seq->attributes,
15686 cp_parser_attributes_opt (parser));
15690 /* record the token of the beginning of the type specifier seq,
15691 for error reporting purposes*/
15693 start_token = cp_lexer_peek_token (parser->lexer);
15695 /* Look for the type-specifier. */
15696 type_specifier = cp_parser_type_specifier (parser,
15698 type_specifier_seq,
15699 /*is_declaration=*/false,
15702 if (!type_specifier)
15704 /* If the first type-specifier could not be found, this is not a
15705 type-specifier-seq at all. */
15706 if (!seen_type_specifier)
15708 cp_parser_error (parser, "expected type-specifier");
15709 type_specifier_seq->type = error_mark_node;
15712 /* If subsequent type-specifiers could not be found, the
15713 type-specifier-seq is complete. */
15717 seen_type_specifier = true;
15718 /* The standard says that a condition can be:
15720 type-specifier-seq declarator = assignment-expression
15727 we should treat the "S" as a declarator, not as a
15728 type-specifier. The standard doesn't say that explicitly for
15729 type-specifier-seq, but it does say that for
15730 decl-specifier-seq in an ordinary declaration. Perhaps it
15731 would be clearer just to allow a decl-specifier-seq here, and
15732 then add a semantic restriction that if any decl-specifiers
15733 that are not type-specifiers appear, the program is invalid. */
15734 if (is_declaration && !is_cv_qualifier)
15735 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15738 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15741 /* Parse a parameter-declaration-clause.
15743 parameter-declaration-clause:
15744 parameter-declaration-list [opt] ... [opt]
15745 parameter-declaration-list , ...
15747 Returns a representation for the parameter declarations. A return
15748 value of NULL indicates a parameter-declaration-clause consisting
15749 only of an ellipsis. */
15752 cp_parser_parameter_declaration_clause (cp_parser* parser)
15759 /* Peek at the next token. */
15760 token = cp_lexer_peek_token (parser->lexer);
15761 /* Check for trivial parameter-declaration-clauses. */
15762 if (token->type == CPP_ELLIPSIS)
15764 /* Consume the `...' token. */
15765 cp_lexer_consume_token (parser->lexer);
15768 else if (token->type == CPP_CLOSE_PAREN)
15769 /* There are no parameters. */
15771 #ifndef NO_IMPLICIT_EXTERN_C
15772 if (in_system_header && current_class_type == NULL
15773 && current_lang_name == lang_name_c)
15777 return void_list_node;
15779 /* Check for `(void)', too, which is a special case. */
15780 else if (token->keyword == RID_VOID
15781 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15782 == CPP_CLOSE_PAREN))
15784 /* Consume the `void' token. */
15785 cp_lexer_consume_token (parser->lexer);
15786 /* There are no parameters. */
15787 return void_list_node;
15790 /* Parse the parameter-declaration-list. */
15791 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15792 /* If a parse error occurred while parsing the
15793 parameter-declaration-list, then the entire
15794 parameter-declaration-clause is erroneous. */
15798 /* Peek at the next token. */
15799 token = cp_lexer_peek_token (parser->lexer);
15800 /* If it's a `,', the clause should terminate with an ellipsis. */
15801 if (token->type == CPP_COMMA)
15803 /* Consume the `,'. */
15804 cp_lexer_consume_token (parser->lexer);
15805 /* Expect an ellipsis. */
15807 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15809 /* It might also be `...' if the optional trailing `,' was
15811 else if (token->type == CPP_ELLIPSIS)
15813 /* Consume the `...' token. */
15814 cp_lexer_consume_token (parser->lexer);
15815 /* And remember that we saw it. */
15819 ellipsis_p = false;
15821 /* Finish the parameter list. */
15823 parameters = chainon (parameters, void_list_node);
15828 /* Parse a parameter-declaration-list.
15830 parameter-declaration-list:
15831 parameter-declaration
15832 parameter-declaration-list , parameter-declaration
15834 Returns a representation of the parameter-declaration-list, as for
15835 cp_parser_parameter_declaration_clause. However, the
15836 `void_list_node' is never appended to the list. Upon return,
15837 *IS_ERROR will be true iff an error occurred. */
15840 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15842 tree parameters = NULL_TREE;
15843 tree *tail = ¶meters;
15844 bool saved_in_unbraced_linkage_specification_p;
15847 /* Assume all will go well. */
15849 /* The special considerations that apply to a function within an
15850 unbraced linkage specifications do not apply to the parameters
15851 to the function. */
15852 saved_in_unbraced_linkage_specification_p
15853 = parser->in_unbraced_linkage_specification_p;
15854 parser->in_unbraced_linkage_specification_p = false;
15856 /* Look for more parameters. */
15859 cp_parameter_declarator *parameter;
15860 tree decl = error_mark_node;
15861 bool parenthesized_p;
15862 /* Parse the parameter. */
15864 = cp_parser_parameter_declaration (parser,
15865 /*template_parm_p=*/false,
15868 /* We don't know yet if the enclosing context is deprecated, so wait
15869 and warn in grokparms if appropriate. */
15870 deprecated_state = DEPRECATED_SUPPRESS;
15873 decl = grokdeclarator (parameter->declarator,
15874 ¶meter->decl_specifiers,
15876 parameter->default_argument != NULL_TREE,
15877 ¶meter->decl_specifiers.attributes);
15879 deprecated_state = DEPRECATED_NORMAL;
15881 /* If a parse error occurred parsing the parameter declaration,
15882 then the entire parameter-declaration-list is erroneous. */
15883 if (decl == error_mark_node)
15886 parameters = error_mark_node;
15890 if (parameter->decl_specifiers.attributes)
15891 cplus_decl_attributes (&decl,
15892 parameter->decl_specifiers.attributes,
15894 if (DECL_NAME (decl))
15895 decl = pushdecl (decl);
15897 if (decl != error_mark_node)
15899 retrofit_lang_decl (decl);
15900 DECL_PARM_INDEX (decl) = ++index;
15903 /* Add the new parameter to the list. */
15904 *tail = build_tree_list (parameter->default_argument, decl);
15905 tail = &TREE_CHAIN (*tail);
15907 /* Peek at the next token. */
15908 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15909 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15910 /* These are for Objective-C++ */
15911 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15912 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15913 /* The parameter-declaration-list is complete. */
15915 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15919 /* Peek at the next token. */
15920 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15921 /* If it's an ellipsis, then the list is complete. */
15922 if (token->type == CPP_ELLIPSIS)
15924 /* Otherwise, there must be more parameters. Consume the
15926 cp_lexer_consume_token (parser->lexer);
15927 /* When parsing something like:
15929 int i(float f, double d)
15931 we can tell after seeing the declaration for "f" that we
15932 are not looking at an initialization of a variable "i",
15933 but rather at the declaration of a function "i".
15935 Due to the fact that the parsing of template arguments
15936 (as specified to a template-id) requires backtracking we
15937 cannot use this technique when inside a template argument
15939 if (!parser->in_template_argument_list_p
15940 && !parser->in_type_id_in_expr_p
15941 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15942 /* However, a parameter-declaration of the form
15943 "foat(f)" (which is a valid declaration of a
15944 parameter "f") can also be interpreted as an
15945 expression (the conversion of "f" to "float"). */
15946 && !parenthesized_p)
15947 cp_parser_commit_to_tentative_parse (parser);
15951 cp_parser_error (parser, "expected %<,%> or %<...%>");
15952 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15953 cp_parser_skip_to_closing_parenthesis (parser,
15954 /*recovering=*/true,
15955 /*or_comma=*/false,
15956 /*consume_paren=*/false);
15961 parser->in_unbraced_linkage_specification_p
15962 = saved_in_unbraced_linkage_specification_p;
15967 /* Parse a parameter declaration.
15969 parameter-declaration:
15970 decl-specifier-seq ... [opt] declarator
15971 decl-specifier-seq declarator = assignment-expression
15972 decl-specifier-seq ... [opt] abstract-declarator [opt]
15973 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15975 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15976 declares a template parameter. (In that case, a non-nested `>'
15977 token encountered during the parsing of the assignment-expression
15978 is not interpreted as a greater-than operator.)
15980 Returns a representation of the parameter, or NULL if an error
15981 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15982 true iff the declarator is of the form "(p)". */
15984 static cp_parameter_declarator *
15985 cp_parser_parameter_declaration (cp_parser *parser,
15986 bool template_parm_p,
15987 bool *parenthesized_p)
15989 int declares_class_or_enum;
15990 cp_decl_specifier_seq decl_specifiers;
15991 cp_declarator *declarator;
15992 tree default_argument;
15993 cp_token *token = NULL, *declarator_token_start = NULL;
15994 const char *saved_message;
15996 /* In a template parameter, `>' is not an operator.
16000 When parsing a default template-argument for a non-type
16001 template-parameter, the first non-nested `>' is taken as the end
16002 of the template parameter-list rather than a greater-than
16005 /* Type definitions may not appear in parameter types. */
16006 saved_message = parser->type_definition_forbidden_message;
16007 parser->type_definition_forbidden_message
16008 = G_("types may not be defined in parameter types");
16010 /* Parse the declaration-specifiers. */
16011 cp_parser_decl_specifier_seq (parser,
16012 CP_PARSER_FLAGS_NONE,
16014 &declares_class_or_enum);
16016 /* Complain about missing 'typename' or other invalid type names. */
16017 if (!decl_specifiers.any_type_specifiers_p)
16018 cp_parser_parse_and_diagnose_invalid_type_name (parser);
16020 /* If an error occurred, there's no reason to attempt to parse the
16021 rest of the declaration. */
16022 if (cp_parser_error_occurred (parser))
16024 parser->type_definition_forbidden_message = saved_message;
16028 /* Peek at the next token. */
16029 token = cp_lexer_peek_token (parser->lexer);
16031 /* If the next token is a `)', `,', `=', `>', or `...', then there
16032 is no declarator. However, when variadic templates are enabled,
16033 there may be a declarator following `...'. */
16034 if (token->type == CPP_CLOSE_PAREN
16035 || token->type == CPP_COMMA
16036 || token->type == CPP_EQ
16037 || token->type == CPP_GREATER)
16040 if (parenthesized_p)
16041 *parenthesized_p = false;
16043 /* Otherwise, there should be a declarator. */
16046 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16047 parser->default_arg_ok_p = false;
16049 /* After seeing a decl-specifier-seq, if the next token is not a
16050 "(", there is no possibility that the code is a valid
16051 expression. Therefore, if parsing tentatively, we commit at
16053 if (!parser->in_template_argument_list_p
16054 /* In an expression context, having seen:
16058 we cannot be sure whether we are looking at a
16059 function-type (taking a "char" as a parameter) or a cast
16060 of some object of type "char" to "int". */
16061 && !parser->in_type_id_in_expr_p
16062 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16063 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16064 cp_parser_commit_to_tentative_parse (parser);
16065 /* Parse the declarator. */
16066 declarator_token_start = token;
16067 declarator = cp_parser_declarator (parser,
16068 CP_PARSER_DECLARATOR_EITHER,
16069 /*ctor_dtor_or_conv_p=*/NULL,
16071 /*member_p=*/false);
16072 parser->default_arg_ok_p = saved_default_arg_ok_p;
16073 /* After the declarator, allow more attributes. */
16074 decl_specifiers.attributes
16075 = chainon (decl_specifiers.attributes,
16076 cp_parser_attributes_opt (parser));
16079 /* If the next token is an ellipsis, and we have not seen a
16080 declarator name, and the type of the declarator contains parameter
16081 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16082 a parameter pack expansion expression. Otherwise, leave the
16083 ellipsis for a C-style variadic function. */
16084 token = cp_lexer_peek_token (parser->lexer);
16085 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16087 tree type = decl_specifiers.type;
16089 if (type && DECL_P (type))
16090 type = TREE_TYPE (type);
16093 && TREE_CODE (type) != TYPE_PACK_EXPANSION
16094 && declarator_can_be_parameter_pack (declarator)
16095 && (!declarator || !declarator->parameter_pack_p)
16096 && uses_parameter_packs (type))
16098 /* Consume the `...'. */
16099 cp_lexer_consume_token (parser->lexer);
16100 maybe_warn_variadic_templates ();
16102 /* Build a pack expansion type */
16104 declarator->parameter_pack_p = true;
16106 decl_specifiers.type = make_pack_expansion (type);
16110 /* The restriction on defining new types applies only to the type
16111 of the parameter, not to the default argument. */
16112 parser->type_definition_forbidden_message = saved_message;
16114 /* If the next token is `=', then process a default argument. */
16115 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16117 /* Consume the `='. */
16118 cp_lexer_consume_token (parser->lexer);
16120 /* If we are defining a class, then the tokens that make up the
16121 default argument must be saved and processed later. */
16122 if (!template_parm_p && at_class_scope_p ()
16123 && TYPE_BEING_DEFINED (current_class_type)
16124 && !LAMBDA_TYPE_P (current_class_type))
16126 unsigned depth = 0;
16127 int maybe_template_id = 0;
16128 cp_token *first_token;
16131 /* Add tokens until we have processed the entire default
16132 argument. We add the range [first_token, token). */
16133 first_token = cp_lexer_peek_token (parser->lexer);
16138 /* Peek at the next token. */
16139 token = cp_lexer_peek_token (parser->lexer);
16140 /* What we do depends on what token we have. */
16141 switch (token->type)
16143 /* In valid code, a default argument must be
16144 immediately followed by a `,' `)', or `...'. */
16146 if (depth == 0 && maybe_template_id)
16148 /* If we've seen a '<', we might be in a
16149 template-argument-list. Until Core issue 325 is
16150 resolved, we don't know how this situation ought
16151 to be handled, so try to DTRT. We check whether
16152 what comes after the comma is a valid parameter
16153 declaration list. If it is, then the comma ends
16154 the default argument; otherwise the default
16155 argument continues. */
16156 bool error = false;
16159 /* Set ITALP so cp_parser_parameter_declaration_list
16160 doesn't decide to commit to this parse. */
16161 bool saved_italp = parser->in_template_argument_list_p;
16162 parser->in_template_argument_list_p = true;
16164 cp_parser_parse_tentatively (parser);
16165 cp_lexer_consume_token (parser->lexer);
16166 begin_scope (sk_function_parms, NULL_TREE);
16167 cp_parser_parameter_declaration_list (parser, &error);
16168 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16169 pop_binding (DECL_NAME (t), t);
16171 if (!cp_parser_error_occurred (parser) && !error)
16173 cp_parser_abort_tentative_parse (parser);
16175 parser->in_template_argument_list_p = saved_italp;
16178 case CPP_CLOSE_PAREN:
16180 /* If we run into a non-nested `;', `}', or `]',
16181 then the code is invalid -- but the default
16182 argument is certainly over. */
16183 case CPP_SEMICOLON:
16184 case CPP_CLOSE_BRACE:
16185 case CPP_CLOSE_SQUARE:
16188 /* Update DEPTH, if necessary. */
16189 else if (token->type == CPP_CLOSE_PAREN
16190 || token->type == CPP_CLOSE_BRACE
16191 || token->type == CPP_CLOSE_SQUARE)
16195 case CPP_OPEN_PAREN:
16196 case CPP_OPEN_SQUARE:
16197 case CPP_OPEN_BRACE:
16203 /* This might be the comparison operator, or it might
16204 start a template argument list. */
16205 ++maybe_template_id;
16209 if (cxx_dialect == cxx98)
16211 /* Fall through for C++0x, which treats the `>>'
16212 operator like two `>' tokens in certain
16218 /* This might be an operator, or it might close a
16219 template argument list. But if a previous '<'
16220 started a template argument list, this will have
16221 closed it, so we can't be in one anymore. */
16222 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16223 if (maybe_template_id < 0)
16224 maybe_template_id = 0;
16228 /* If we run out of tokens, issue an error message. */
16230 case CPP_PRAGMA_EOL:
16231 error_at (token->location, "file ends in default argument");
16237 /* In these cases, we should look for template-ids.
16238 For example, if the default argument is
16239 `X<int, double>()', we need to do name lookup to
16240 figure out whether or not `X' is a template; if
16241 so, the `,' does not end the default argument.
16243 That is not yet done. */
16250 /* If we've reached the end, stop. */
16254 /* Add the token to the token block. */
16255 token = cp_lexer_consume_token (parser->lexer);
16258 /* Create a DEFAULT_ARG to represent the unparsed default
16260 default_argument = make_node (DEFAULT_ARG);
16261 DEFARG_TOKENS (default_argument)
16262 = cp_token_cache_new (first_token, token);
16263 DEFARG_INSTANTIATIONS (default_argument) = NULL;
16265 /* Outside of a class definition, we can just parse the
16266 assignment-expression. */
16269 token = cp_lexer_peek_token (parser->lexer);
16271 = cp_parser_default_argument (parser, template_parm_p);
16274 if (!parser->default_arg_ok_p)
16276 if (flag_permissive)
16277 warning (0, "deprecated use of default argument for parameter of non-function");
16280 error_at (token->location,
16281 "default arguments are only "
16282 "permitted for function parameters");
16283 default_argument = NULL_TREE;
16286 else if ((declarator && declarator->parameter_pack_p)
16287 || (decl_specifiers.type
16288 && PACK_EXPANSION_P (decl_specifiers.type)))
16290 /* Find the name of the parameter pack. */
16291 cp_declarator *id_declarator = declarator;
16292 while (id_declarator && id_declarator->kind != cdk_id)
16293 id_declarator = id_declarator->declarator;
16295 if (id_declarator && id_declarator->kind == cdk_id)
16296 error_at (declarator_token_start->location,
16298 ? "template parameter pack %qD"
16299 " cannot have a default argument"
16300 : "parameter pack %qD cannot have a default argument",
16301 id_declarator->u.id.unqualified_name);
16303 error_at (declarator_token_start->location,
16305 ? "template parameter pack cannot have a default argument"
16306 : "parameter pack cannot have a default argument");
16308 default_argument = NULL_TREE;
16312 default_argument = NULL_TREE;
16314 return make_parameter_declarator (&decl_specifiers,
16319 /* Parse a default argument and return it.
16321 TEMPLATE_PARM_P is true if this is a default argument for a
16322 non-type template parameter. */
16324 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16326 tree default_argument = NULL_TREE;
16327 bool saved_greater_than_is_operator_p;
16328 bool saved_local_variables_forbidden_p;
16330 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16332 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16333 parser->greater_than_is_operator_p = !template_parm_p;
16334 /* Local variable names (and the `this' keyword) may not
16335 appear in a default argument. */
16336 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16337 parser->local_variables_forbidden_p = true;
16338 /* Parse the assignment-expression. */
16339 if (template_parm_p)
16340 push_deferring_access_checks (dk_no_deferred);
16342 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16343 if (template_parm_p)
16344 pop_deferring_access_checks ();
16345 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16346 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16348 return default_argument;
16351 /* Parse a function-body.
16354 compound_statement */
16357 cp_parser_function_body (cp_parser *parser)
16359 cp_parser_compound_statement (parser, NULL, false);
16362 /* Parse a ctor-initializer-opt followed by a function-body. Return
16363 true if a ctor-initializer was present. */
16366 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16369 bool ctor_initializer_p;
16370 const bool check_body_p =
16371 DECL_CONSTRUCTOR_P (current_function_decl)
16372 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16375 /* Begin the function body. */
16376 body = begin_function_body ();
16377 /* Parse the optional ctor-initializer. */
16378 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16380 /* If we're parsing a constexpr constructor definition, we need
16381 to check that the constructor body is indeed empty. However,
16382 before we get to cp_parser_function_body lot of junk has been
16383 generated, so we can't just check that we have an empty block.
16384 Rather we take a snapshot of the outermost block, and check whether
16385 cp_parser_function_body changed its state. */
16389 if (TREE_CODE (list) == BIND_EXPR)
16390 list = BIND_EXPR_BODY (list);
16391 if (TREE_CODE (list) == STATEMENT_LIST
16392 && STATEMENT_LIST_TAIL (list) != NULL)
16393 last = STATEMENT_LIST_TAIL (list)->stmt;
16395 /* Parse the function-body. */
16396 cp_parser_function_body (parser);
16398 check_constexpr_ctor_body (last, list);
16399 /* Finish the function body. */
16400 finish_function_body (body);
16402 return ctor_initializer_p;
16405 /* Parse an initializer.
16408 = initializer-clause
16409 ( expression-list )
16411 Returns an expression representing the initializer. If no
16412 initializer is present, NULL_TREE is returned.
16414 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16415 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16416 set to TRUE if there is no initializer present. If there is an
16417 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16418 is set to true; otherwise it is set to false. */
16421 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16422 bool* non_constant_p)
16427 /* Peek at the next token. */
16428 token = cp_lexer_peek_token (parser->lexer);
16430 /* Let our caller know whether or not this initializer was
16432 *is_direct_init = (token->type != CPP_EQ);
16433 /* Assume that the initializer is constant. */
16434 *non_constant_p = false;
16436 if (token->type == CPP_EQ)
16438 /* Consume the `='. */
16439 cp_lexer_consume_token (parser->lexer);
16440 /* Parse the initializer-clause. */
16441 init = cp_parser_initializer_clause (parser, non_constant_p);
16443 else if (token->type == CPP_OPEN_PAREN)
16446 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16448 /*allow_expansion_p=*/true,
16451 return error_mark_node;
16452 init = build_tree_list_vec (vec);
16453 release_tree_vector (vec);
16455 else if (token->type == CPP_OPEN_BRACE)
16457 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16458 init = cp_parser_braced_list (parser, non_constant_p);
16459 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16463 /* Anything else is an error. */
16464 cp_parser_error (parser, "expected initializer");
16465 init = error_mark_node;
16471 /* Parse an initializer-clause.
16473 initializer-clause:
16474 assignment-expression
16477 Returns an expression representing the initializer.
16479 If the `assignment-expression' production is used the value
16480 returned is simply a representation for the expression.
16482 Otherwise, calls cp_parser_braced_list. */
16485 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16489 /* Assume the expression is constant. */
16490 *non_constant_p = false;
16492 /* If it is not a `{', then we are looking at an
16493 assignment-expression. */
16494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16497 = cp_parser_constant_expression (parser,
16498 /*allow_non_constant_p=*/true,
16500 if (!*non_constant_p)
16502 /* We only want to fold if this is really a constant
16503 expression. FIXME Actually, we don't want to fold here, but in
16505 tree folded = fold_non_dependent_expr (initializer);
16506 folded = maybe_constant_value (folded);
16507 if (TREE_CONSTANT (folded))
16508 initializer = folded;
16512 initializer = cp_parser_braced_list (parser, non_constant_p);
16514 return initializer;
16517 /* Parse a brace-enclosed initializer list.
16520 { initializer-list , [opt] }
16523 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16524 the elements of the initializer-list (or NULL, if the last
16525 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16526 NULL_TREE. There is no way to detect whether or not the optional
16527 trailing `,' was provided. NON_CONSTANT_P is as for
16528 cp_parser_initializer. */
16531 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16535 /* Consume the `{' token. */
16536 cp_lexer_consume_token (parser->lexer);
16537 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16538 initializer = make_node (CONSTRUCTOR);
16539 /* If it's not a `}', then there is a non-trivial initializer. */
16540 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16542 /* Parse the initializer list. */
16543 CONSTRUCTOR_ELTS (initializer)
16544 = cp_parser_initializer_list (parser, non_constant_p);
16545 /* A trailing `,' token is allowed. */
16546 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16547 cp_lexer_consume_token (parser->lexer);
16549 /* Now, there should be a trailing `}'. */
16550 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16551 TREE_TYPE (initializer) = init_list_type_node;
16552 return initializer;
16555 /* Parse an initializer-list.
16558 initializer-clause ... [opt]
16559 initializer-list , initializer-clause ... [opt]
16564 identifier : initializer-clause
16565 initializer-list, identifier : initializer-clause
16567 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16568 for the initializer. If the INDEX of the elt is non-NULL, it is the
16569 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16570 as for cp_parser_initializer. */
16572 static VEC(constructor_elt,gc) *
16573 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16575 VEC(constructor_elt,gc) *v = NULL;
16577 /* Assume all of the expressions are constant. */
16578 *non_constant_p = false;
16580 /* Parse the rest of the list. */
16586 bool clause_non_constant_p;
16588 /* If the next token is an identifier and the following one is a
16589 colon, we are looking at the GNU designated-initializer
16591 if (cp_parser_allow_gnu_extensions_p (parser)
16592 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16593 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16595 /* Warn the user that they are using an extension. */
16596 pedwarn (input_location, OPT_pedantic,
16597 "ISO C++ does not allow designated initializers");
16598 /* Consume the identifier. */
16599 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16600 /* Consume the `:'. */
16601 cp_lexer_consume_token (parser->lexer);
16604 identifier = NULL_TREE;
16606 /* Parse the initializer. */
16607 initializer = cp_parser_initializer_clause (parser,
16608 &clause_non_constant_p);
16609 /* If any clause is non-constant, so is the entire initializer. */
16610 if (clause_non_constant_p)
16611 *non_constant_p = true;
16613 /* If we have an ellipsis, this is an initializer pack
16615 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16617 /* Consume the `...'. */
16618 cp_lexer_consume_token (parser->lexer);
16620 /* Turn the initializer into an initializer expansion. */
16621 initializer = make_pack_expansion (initializer);
16624 /* Add it to the vector. */
16625 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16627 /* If the next token is not a comma, we have reached the end of
16629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16632 /* Peek at the next token. */
16633 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16634 /* If the next token is a `}', then we're still done. An
16635 initializer-clause can have a trailing `,' after the
16636 initializer-list and before the closing `}'. */
16637 if (token->type == CPP_CLOSE_BRACE)
16640 /* Consume the `,' token. */
16641 cp_lexer_consume_token (parser->lexer);
16647 /* Classes [gram.class] */
16649 /* Parse a class-name.
16655 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16656 to indicate that names looked up in dependent types should be
16657 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16658 keyword has been used to indicate that the name that appears next
16659 is a template. TAG_TYPE indicates the explicit tag given before
16660 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16661 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16662 is the class being defined in a class-head.
16664 Returns the TYPE_DECL representing the class. */
16667 cp_parser_class_name (cp_parser *parser,
16668 bool typename_keyword_p,
16669 bool template_keyword_p,
16670 enum tag_types tag_type,
16671 bool check_dependency_p,
16673 bool is_declaration)
16679 tree identifier = NULL_TREE;
16681 /* All class-names start with an identifier. */
16682 token = cp_lexer_peek_token (parser->lexer);
16683 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16685 cp_parser_error (parser, "expected class-name");
16686 return error_mark_node;
16689 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16690 to a template-id, so we save it here. */
16691 scope = parser->scope;
16692 if (scope == error_mark_node)
16693 return error_mark_node;
16695 /* Any name names a type if we're following the `typename' keyword
16696 in a qualified name where the enclosing scope is type-dependent. */
16697 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16698 && dependent_type_p (scope));
16699 /* Handle the common case (an identifier, but not a template-id)
16701 if (token->type == CPP_NAME
16702 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16704 cp_token *identifier_token;
16707 /* Look for the identifier. */
16708 identifier_token = cp_lexer_peek_token (parser->lexer);
16709 ambiguous_p = identifier_token->ambiguous_p;
16710 identifier = cp_parser_identifier (parser);
16711 /* If the next token isn't an identifier, we are certainly not
16712 looking at a class-name. */
16713 if (identifier == error_mark_node)
16714 decl = error_mark_node;
16715 /* If we know this is a type-name, there's no need to look it
16717 else if (typename_p)
16721 tree ambiguous_decls;
16722 /* If we already know that this lookup is ambiguous, then
16723 we've already issued an error message; there's no reason
16727 cp_parser_simulate_error (parser);
16728 return error_mark_node;
16730 /* If the next token is a `::', then the name must be a type
16733 [basic.lookup.qual]
16735 During the lookup for a name preceding the :: scope
16736 resolution operator, object, function, and enumerator
16737 names are ignored. */
16738 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16739 tag_type = typename_type;
16740 /* Look up the name. */
16741 decl = cp_parser_lookup_name (parser, identifier,
16743 /*is_template=*/false,
16744 /*is_namespace=*/false,
16745 check_dependency_p,
16747 identifier_token->location);
16748 if (ambiguous_decls)
16750 if (cp_parser_parsing_tentatively (parser))
16751 cp_parser_simulate_error (parser);
16752 return error_mark_node;
16758 /* Try a template-id. */
16759 decl = cp_parser_template_id (parser, template_keyword_p,
16760 check_dependency_p,
16762 if (decl == error_mark_node)
16763 return error_mark_node;
16766 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16768 /* If this is a typename, create a TYPENAME_TYPE. */
16769 if (typename_p && decl != error_mark_node)
16771 decl = make_typename_type (scope, decl, typename_type,
16772 /*complain=*/tf_error);
16773 if (decl != error_mark_node)
16774 decl = TYPE_NAME (decl);
16777 /* Check to see that it is really the name of a class. */
16778 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16779 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16780 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16781 /* Situations like this:
16783 template <typename T> struct A {
16784 typename T::template X<int>::I i;
16787 are problematic. Is `T::template X<int>' a class-name? The
16788 standard does not seem to be definitive, but there is no other
16789 valid interpretation of the following `::'. Therefore, those
16790 names are considered class-names. */
16792 decl = make_typename_type (scope, decl, tag_type, tf_error);
16793 if (decl != error_mark_node)
16794 decl = TYPE_NAME (decl);
16796 else if (TREE_CODE (decl) != TYPE_DECL
16797 || TREE_TYPE (decl) == error_mark_node
16798 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16799 /* In Objective-C 2.0, a classname followed by '.' starts a
16800 dot-syntax expression, and it's not a type-name. */
16801 || (c_dialect_objc ()
16802 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
16803 && objc_is_class_name (decl)))
16804 decl = error_mark_node;
16806 if (decl == error_mark_node)
16807 cp_parser_error (parser, "expected class-name");
16808 else if (identifier && !parser->scope)
16809 maybe_note_name_used_in_class (identifier, decl);
16814 /* Parse a class-specifier.
16817 class-head { member-specification [opt] }
16819 Returns the TREE_TYPE representing the class. */
16822 cp_parser_class_specifier (cp_parser* parser)
16825 tree attributes = NULL_TREE;
16826 bool nested_name_specifier_p;
16827 unsigned saved_num_template_parameter_lists;
16828 bool saved_in_function_body;
16829 bool saved_in_unbraced_linkage_specification_p;
16830 tree old_scope = NULL_TREE;
16831 tree scope = NULL_TREE;
16834 push_deferring_access_checks (dk_no_deferred);
16836 /* Parse the class-head. */
16837 type = cp_parser_class_head (parser,
16838 &nested_name_specifier_p,
16841 /* If the class-head was a semantic disaster, skip the entire body
16845 cp_parser_skip_to_end_of_block_or_statement (parser);
16846 pop_deferring_access_checks ();
16847 return error_mark_node;
16850 /* Look for the `{'. */
16851 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16853 pop_deferring_access_checks ();
16854 return error_mark_node;
16857 /* Process the base classes. If they're invalid, skip the
16858 entire class body. */
16859 if (!xref_basetypes (type, bases))
16861 /* Consuming the closing brace yields better error messages
16863 if (cp_parser_skip_to_closing_brace (parser))
16864 cp_lexer_consume_token (parser->lexer);
16865 pop_deferring_access_checks ();
16866 return error_mark_node;
16869 /* Issue an error message if type-definitions are forbidden here. */
16870 cp_parser_check_type_definition (parser);
16871 /* Remember that we are defining one more class. */
16872 ++parser->num_classes_being_defined;
16873 /* Inside the class, surrounding template-parameter-lists do not
16875 saved_num_template_parameter_lists
16876 = parser->num_template_parameter_lists;
16877 parser->num_template_parameter_lists = 0;
16878 /* We are not in a function body. */
16879 saved_in_function_body = parser->in_function_body;
16880 parser->in_function_body = false;
16881 /* We are not immediately inside an extern "lang" block. */
16882 saved_in_unbraced_linkage_specification_p
16883 = parser->in_unbraced_linkage_specification_p;
16884 parser->in_unbraced_linkage_specification_p = false;
16886 /* Start the class. */
16887 if (nested_name_specifier_p)
16889 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16890 old_scope = push_inner_scope (scope);
16892 type = begin_class_definition (type, attributes);
16894 if (type == error_mark_node)
16895 /* If the type is erroneous, skip the entire body of the class. */
16896 cp_parser_skip_to_closing_brace (parser);
16898 /* Parse the member-specification. */
16899 cp_parser_member_specification_opt (parser);
16901 /* Look for the trailing `}'. */
16902 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16903 /* Look for trailing attributes to apply to this class. */
16904 if (cp_parser_allow_gnu_extensions_p (parser))
16905 attributes = cp_parser_attributes_opt (parser);
16906 if (type != error_mark_node)
16907 type = finish_struct (type, attributes);
16908 if (nested_name_specifier_p)
16909 pop_inner_scope (old_scope, scope);
16911 /* We've finished a type definition. Check for the common syntax
16912 error of forgetting a semicolon after the definition. We need to
16913 be careful, as we can't just check for not-a-semicolon and be done
16914 with it; the user might have typed:
16916 class X { } c = ...;
16917 class X { } *p = ...;
16919 and so forth. Instead, enumerate all the possible tokens that
16920 might follow this production; if we don't see one of them, then
16921 complain and silently insert the semicolon. */
16923 cp_token *token = cp_lexer_peek_token (parser->lexer);
16924 bool want_semicolon = true;
16926 switch (token->type)
16929 case CPP_SEMICOLON:
16932 case CPP_OPEN_PAREN:
16933 case CPP_CLOSE_PAREN:
16935 want_semicolon = false;
16938 /* While it's legal for type qualifiers and storage class
16939 specifiers to follow type definitions in the grammar, only
16940 compiler testsuites contain code like that. Assume that if
16941 we see such code, then what we're really seeing is a case
16945 const <type> var = ...;
16950 static <type> func (...) ...
16952 i.e. the qualifier or specifier applies to the next
16953 declaration. To do so, however, we need to look ahead one
16954 more token to see if *that* token is a type specifier.
16956 This code could be improved to handle:
16959 static const <type> var = ...; */
16961 if (keyword_is_storage_class_specifier (token->keyword)
16962 || keyword_is_type_qualifier (token->keyword))
16964 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16966 if (lookahead->type == CPP_KEYWORD
16967 && !keyword_begins_type_specifier (lookahead->keyword))
16968 want_semicolon = false;
16969 else if (lookahead->type == CPP_NAME)
16970 /* Handling user-defined types here would be nice, but
16972 want_semicolon = false;
16979 /* If we don't have a type, then something is very wrong and we
16980 shouldn't try to do anything clever. */
16981 if (TYPE_P (type) && want_semicolon)
16983 cp_token_position prev
16984 = cp_lexer_previous_token_position (parser->lexer);
16985 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16986 location_t loc = prev_token->location;
16988 if (CLASSTYPE_DECLARED_CLASS (type))
16989 error_at (loc, "expected %<;%> after class definition");
16990 else if (TREE_CODE (type) == RECORD_TYPE)
16991 error_at (loc, "expected %<;%> after struct definition");
16992 else if (TREE_CODE (type) == UNION_TYPE)
16993 error_at (loc, "expected %<;%> after union definition");
16995 gcc_unreachable ();
16997 /* Unget one token and smash it to look as though we encountered
16998 a semicolon in the input stream. */
16999 cp_lexer_set_token_position (parser->lexer, prev);
17000 token = cp_lexer_peek_token (parser->lexer);
17001 token->type = CPP_SEMICOLON;
17002 token->keyword = RID_MAX;
17006 /* If this class is not itself within the scope of another class,
17007 then we need to parse the bodies of all of the queued function
17008 definitions. Note that the queued functions defined in a class
17009 are not always processed immediately following the
17010 class-specifier for that class. Consider:
17013 struct B { void f() { sizeof (A); } };
17016 If `f' were processed before the processing of `A' were
17017 completed, there would be no way to compute the size of `A'.
17018 Note that the nesting we are interested in here is lexical --
17019 not the semantic nesting given by TYPE_CONTEXT. In particular,
17022 struct A { struct B; };
17023 struct A::B { void f() { } };
17025 there is no need to delay the parsing of `A::B::f'. */
17026 if (--parser->num_classes_being_defined == 0)
17029 tree class_type = NULL_TREE;
17030 tree pushed_scope = NULL_TREE;
17032 cp_default_arg_entry *e;
17034 /* In a first pass, parse default arguments to the functions.
17035 Then, in a second pass, parse the bodies of the functions.
17036 This two-phased approach handles cases like:
17044 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17048 /* If there are default arguments that have not yet been processed,
17049 take care of them now. */
17050 if (class_type != e->class_type)
17053 pop_scope (pushed_scope);
17054 class_type = e->class_type;
17055 pushed_scope = push_scope (class_type);
17057 /* Make sure that any template parameters are in scope. */
17058 maybe_begin_member_template_processing (fn);
17059 /* Parse the default argument expressions. */
17060 cp_parser_late_parsing_default_args (parser, fn);
17061 /* Remove any template parameters from the symbol table. */
17062 maybe_end_member_template_processing ();
17065 pop_scope (pushed_scope);
17066 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17067 /* Now parse the body of the functions. */
17068 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17069 cp_parser_late_parsing_for_member (parser, fn);
17070 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17073 /* Put back any saved access checks. */
17074 pop_deferring_access_checks ();
17076 /* Restore saved state. */
17077 parser->in_function_body = saved_in_function_body;
17078 parser->num_template_parameter_lists
17079 = saved_num_template_parameter_lists;
17080 parser->in_unbraced_linkage_specification_p
17081 = saved_in_unbraced_linkage_specification_p;
17086 /* Parse a class-head.
17089 class-key identifier [opt] base-clause [opt]
17090 class-key nested-name-specifier identifier base-clause [opt]
17091 class-key nested-name-specifier [opt] template-id
17095 class-key attributes identifier [opt] base-clause [opt]
17096 class-key attributes nested-name-specifier identifier base-clause [opt]
17097 class-key attributes nested-name-specifier [opt] template-id
17100 Upon return BASES is initialized to the list of base classes (or
17101 NULL, if there are none) in the same form returned by
17102 cp_parser_base_clause.
17104 Returns the TYPE of the indicated class. Sets
17105 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17106 involving a nested-name-specifier was used, and FALSE otherwise.
17108 Returns error_mark_node if this is not a class-head.
17110 Returns NULL_TREE if the class-head is syntactically valid, but
17111 semantically invalid in a way that means we should skip the entire
17112 body of the class. */
17115 cp_parser_class_head (cp_parser* parser,
17116 bool* nested_name_specifier_p,
17117 tree *attributes_p,
17120 tree nested_name_specifier;
17121 enum tag_types class_key;
17122 tree id = NULL_TREE;
17123 tree type = NULL_TREE;
17125 bool template_id_p = false;
17126 bool qualified_p = false;
17127 bool invalid_nested_name_p = false;
17128 bool invalid_explicit_specialization_p = false;
17129 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17130 tree pushed_scope = NULL_TREE;
17131 unsigned num_templates;
17132 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17133 /* Assume no nested-name-specifier will be present. */
17134 *nested_name_specifier_p = false;
17135 /* Assume no template parameter lists will be used in defining the
17138 parser->colon_corrects_to_scope_p = false;
17140 *bases = NULL_TREE;
17142 /* Look for the class-key. */
17143 class_key = cp_parser_class_key (parser);
17144 if (class_key == none_type)
17145 return error_mark_node;
17147 /* Parse the attributes. */
17148 attributes = cp_parser_attributes_opt (parser);
17150 /* If the next token is `::', that is invalid -- but sometimes
17151 people do try to write:
17155 Handle this gracefully by accepting the extra qualifier, and then
17156 issuing an error about it later if this really is a
17157 class-head. If it turns out just to be an elaborated type
17158 specifier, remain silent. */
17159 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17160 qualified_p = true;
17162 push_deferring_access_checks (dk_no_check);
17164 /* Determine the name of the class. Begin by looking for an
17165 optional nested-name-specifier. */
17166 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17167 nested_name_specifier
17168 = cp_parser_nested_name_specifier_opt (parser,
17169 /*typename_keyword_p=*/false,
17170 /*check_dependency_p=*/false,
17172 /*is_declaration=*/false);
17173 /* If there was a nested-name-specifier, then there *must* be an
17175 if (nested_name_specifier)
17177 type_start_token = cp_lexer_peek_token (parser->lexer);
17178 /* Although the grammar says `identifier', it really means
17179 `class-name' or `template-name'. You are only allowed to
17180 define a class that has already been declared with this
17183 The proposed resolution for Core Issue 180 says that wherever
17184 you see `class T::X' you should treat `X' as a type-name.
17186 It is OK to define an inaccessible class; for example:
17188 class A { class B; };
17191 We do not know if we will see a class-name, or a
17192 template-name. We look for a class-name first, in case the
17193 class-name is a template-id; if we looked for the
17194 template-name first we would stop after the template-name. */
17195 cp_parser_parse_tentatively (parser);
17196 type = cp_parser_class_name (parser,
17197 /*typename_keyword_p=*/false,
17198 /*template_keyword_p=*/false,
17200 /*check_dependency_p=*/false,
17201 /*class_head_p=*/true,
17202 /*is_declaration=*/false);
17203 /* If that didn't work, ignore the nested-name-specifier. */
17204 if (!cp_parser_parse_definitely (parser))
17206 invalid_nested_name_p = true;
17207 type_start_token = cp_lexer_peek_token (parser->lexer);
17208 id = cp_parser_identifier (parser);
17209 if (id == error_mark_node)
17212 /* If we could not find a corresponding TYPE, treat this
17213 declaration like an unqualified declaration. */
17214 if (type == error_mark_node)
17215 nested_name_specifier = NULL_TREE;
17216 /* Otherwise, count the number of templates used in TYPE and its
17217 containing scopes. */
17222 for (scope = TREE_TYPE (type);
17223 scope && TREE_CODE (scope) != NAMESPACE_DECL;
17224 scope = (TYPE_P (scope)
17225 ? TYPE_CONTEXT (scope)
17226 : DECL_CONTEXT (scope)))
17228 && CLASS_TYPE_P (scope)
17229 && CLASSTYPE_TEMPLATE_INFO (scope)
17230 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17231 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17235 /* Otherwise, the identifier is optional. */
17238 /* We don't know whether what comes next is a template-id,
17239 an identifier, or nothing at all. */
17240 cp_parser_parse_tentatively (parser);
17241 /* Check for a template-id. */
17242 type_start_token = cp_lexer_peek_token (parser->lexer);
17243 id = cp_parser_template_id (parser,
17244 /*template_keyword_p=*/false,
17245 /*check_dependency_p=*/true,
17246 /*is_declaration=*/true);
17247 /* If that didn't work, it could still be an identifier. */
17248 if (!cp_parser_parse_definitely (parser))
17250 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17252 type_start_token = cp_lexer_peek_token (parser->lexer);
17253 id = cp_parser_identifier (parser);
17260 template_id_p = true;
17265 pop_deferring_access_checks ();
17268 cp_parser_check_for_invalid_template_id (parser, id,
17269 type_start_token->location);
17271 /* If it's not a `:' or a `{' then we can't really be looking at a
17272 class-head, since a class-head only appears as part of a
17273 class-specifier. We have to detect this situation before calling
17274 xref_tag, since that has irreversible side-effects. */
17275 if (!cp_parser_next_token_starts_class_definition_p (parser))
17277 cp_parser_error (parser, "expected %<{%> or %<:%>");
17278 type = error_mark_node;
17282 /* At this point, we're going ahead with the class-specifier, even
17283 if some other problem occurs. */
17284 cp_parser_commit_to_tentative_parse (parser);
17285 /* Issue the error about the overly-qualified name now. */
17288 cp_parser_error (parser,
17289 "global qualification of class name is invalid");
17290 type = error_mark_node;
17293 else if (invalid_nested_name_p)
17295 cp_parser_error (parser,
17296 "qualified name does not name a class");
17297 type = error_mark_node;
17300 else if (nested_name_specifier)
17304 /* Reject typedef-names in class heads. */
17305 if (!DECL_IMPLICIT_TYPEDEF_P (type))
17307 error_at (type_start_token->location,
17308 "invalid class name in declaration of %qD",
17314 /* Figure out in what scope the declaration is being placed. */
17315 scope = current_scope ();
17316 /* If that scope does not contain the scope in which the
17317 class was originally declared, the program is invalid. */
17318 if (scope && !is_ancestor (scope, nested_name_specifier))
17320 if (at_namespace_scope_p ())
17321 error_at (type_start_token->location,
17322 "declaration of %qD in namespace %qD which does not "
17324 type, scope, nested_name_specifier);
17326 error_at (type_start_token->location,
17327 "declaration of %qD in %qD which does not enclose %qD",
17328 type, scope, nested_name_specifier);
17334 A declarator-id shall not be qualified except for the
17335 definition of a ... nested class outside of its class
17336 ... [or] the definition or explicit instantiation of a
17337 class member of a namespace outside of its namespace. */
17338 if (scope == nested_name_specifier)
17340 permerror (nested_name_specifier_token_start->location,
17341 "extra qualification not allowed");
17342 nested_name_specifier = NULL_TREE;
17346 /* An explicit-specialization must be preceded by "template <>". If
17347 it is not, try to recover gracefully. */
17348 if (at_namespace_scope_p ()
17349 && parser->num_template_parameter_lists == 0
17352 error_at (type_start_token->location,
17353 "an explicit specialization must be preceded by %<template <>%>");
17354 invalid_explicit_specialization_p = true;
17355 /* Take the same action that would have been taken by
17356 cp_parser_explicit_specialization. */
17357 ++parser->num_template_parameter_lists;
17358 begin_specialization ();
17360 /* There must be no "return" statements between this point and the
17361 end of this function; set "type "to the correct return value and
17362 use "goto done;" to return. */
17363 /* Make sure that the right number of template parameters were
17365 if (!cp_parser_check_template_parameters (parser, num_templates,
17366 type_start_token->location,
17367 /*declarator=*/NULL))
17369 /* If something went wrong, there is no point in even trying to
17370 process the class-definition. */
17375 /* Look up the type. */
17378 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17379 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17380 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17382 error_at (type_start_token->location,
17383 "function template %qD redeclared as a class template", id);
17384 type = error_mark_node;
17388 type = TREE_TYPE (id);
17389 type = maybe_process_partial_specialization (type);
17391 if (nested_name_specifier)
17392 pushed_scope = push_scope (nested_name_specifier);
17394 else if (nested_name_specifier)
17400 template <typename T> struct S { struct T };
17401 template <typename T> struct S<T>::T { };
17403 we will get a TYPENAME_TYPE when processing the definition of
17404 `S::T'. We need to resolve it to the actual type before we
17405 try to define it. */
17406 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17408 class_type = resolve_typename_type (TREE_TYPE (type),
17409 /*only_current_p=*/false);
17410 if (TREE_CODE (class_type) != TYPENAME_TYPE)
17411 type = TYPE_NAME (class_type);
17414 cp_parser_error (parser, "could not resolve typename type");
17415 type = error_mark_node;
17419 if (maybe_process_partial_specialization (TREE_TYPE (type))
17420 == error_mark_node)
17426 class_type = current_class_type;
17427 /* Enter the scope indicated by the nested-name-specifier. */
17428 pushed_scope = push_scope (nested_name_specifier);
17429 /* Get the canonical version of this type. */
17430 type = TYPE_MAIN_DECL (TREE_TYPE (type));
17431 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17432 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17434 type = push_template_decl (type);
17435 if (type == error_mark_node)
17442 type = TREE_TYPE (type);
17443 *nested_name_specifier_p = true;
17445 else /* The name is not a nested name. */
17447 /* If the class was unnamed, create a dummy name. */
17449 id = make_anon_name ();
17450 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17451 parser->num_template_parameter_lists);
17454 /* Indicate whether this class was declared as a `class' or as a
17456 if (TREE_CODE (type) == RECORD_TYPE)
17457 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17458 cp_parser_check_class_key (class_key, type);
17460 /* If this type was already complete, and we see another definition,
17461 that's an error. */
17462 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17464 error_at (type_start_token->location, "redefinition of %q#T",
17466 error_at (type_start_token->location, "previous definition of %q+#T",
17471 else if (type == error_mark_node)
17474 /* We will have entered the scope containing the class; the names of
17475 base classes should be looked up in that context. For example:
17477 struct A { struct B {}; struct C; };
17478 struct A::C : B {};
17482 /* Get the list of base-classes, if there is one. */
17483 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17484 *bases = cp_parser_base_clause (parser);
17487 /* Leave the scope given by the nested-name-specifier. We will
17488 enter the class scope itself while processing the members. */
17490 pop_scope (pushed_scope);
17492 if (invalid_explicit_specialization_p)
17494 end_specialization ();
17495 --parser->num_template_parameter_lists;
17499 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17500 *attributes_p = attributes;
17502 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17506 /* Parse a class-key.
17513 Returns the kind of class-key specified, or none_type to indicate
17516 static enum tag_types
17517 cp_parser_class_key (cp_parser* parser)
17520 enum tag_types tag_type;
17522 /* Look for the class-key. */
17523 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17527 /* Check to see if the TOKEN is a class-key. */
17528 tag_type = cp_parser_token_is_class_key (token);
17530 cp_parser_error (parser, "expected class-key");
17534 /* Parse an (optional) member-specification.
17536 member-specification:
17537 member-declaration member-specification [opt]
17538 access-specifier : member-specification [opt] */
17541 cp_parser_member_specification_opt (cp_parser* parser)
17548 /* Peek at the next token. */
17549 token = cp_lexer_peek_token (parser->lexer);
17550 /* If it's a `}', or EOF then we've seen all the members. */
17551 if (token->type == CPP_CLOSE_BRACE
17552 || token->type == CPP_EOF
17553 || token->type == CPP_PRAGMA_EOL)
17556 /* See if this token is a keyword. */
17557 keyword = token->keyword;
17561 case RID_PROTECTED:
17563 /* Consume the access-specifier. */
17564 cp_lexer_consume_token (parser->lexer);
17565 /* Remember which access-specifier is active. */
17566 current_access_specifier = token->u.value;
17567 /* Look for the `:'. */
17568 cp_parser_require (parser, CPP_COLON, RT_COLON);
17572 /* Accept #pragmas at class scope. */
17573 if (token->type == CPP_PRAGMA)
17575 cp_parser_pragma (parser, pragma_external);
17579 /* Otherwise, the next construction must be a
17580 member-declaration. */
17581 cp_parser_member_declaration (parser);
17586 /* Parse a member-declaration.
17588 member-declaration:
17589 decl-specifier-seq [opt] member-declarator-list [opt] ;
17590 function-definition ; [opt]
17591 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17593 template-declaration
17595 member-declarator-list:
17597 member-declarator-list , member-declarator
17600 declarator pure-specifier [opt]
17601 declarator constant-initializer [opt]
17602 identifier [opt] : constant-expression
17606 member-declaration:
17607 __extension__ member-declaration
17610 declarator attributes [opt] pure-specifier [opt]
17611 declarator attributes [opt] constant-initializer [opt]
17612 identifier [opt] attributes [opt] : constant-expression
17616 member-declaration:
17617 static_assert-declaration */
17620 cp_parser_member_declaration (cp_parser* parser)
17622 cp_decl_specifier_seq decl_specifiers;
17623 tree prefix_attributes;
17625 int declares_class_or_enum;
17627 cp_token *token = NULL;
17628 cp_token *decl_spec_token_start = NULL;
17629 cp_token *initializer_token_start = NULL;
17630 int saved_pedantic;
17631 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17633 /* Check for the `__extension__' keyword. */
17634 if (cp_parser_extension_opt (parser, &saved_pedantic))
17637 cp_parser_member_declaration (parser);
17638 /* Restore the old value of the PEDANTIC flag. */
17639 pedantic = saved_pedantic;
17644 /* Check for a template-declaration. */
17645 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17647 /* An explicit specialization here is an error condition, and we
17648 expect the specialization handler to detect and report this. */
17649 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17650 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17651 cp_parser_explicit_specialization (parser);
17653 cp_parser_template_declaration (parser, /*member_p=*/true);
17658 /* Check for a using-declaration. */
17659 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17661 /* Parse the using-declaration. */
17662 cp_parser_using_declaration (parser,
17663 /*access_declaration_p=*/false);
17667 /* Check for @defs. */
17668 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17671 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17672 ivar = ivar_chains;
17676 ivar = TREE_CHAIN (member);
17677 TREE_CHAIN (member) = NULL_TREE;
17678 finish_member_declaration (member);
17683 /* If the next token is `static_assert' we have a static assertion. */
17684 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17686 cp_parser_static_assert (parser, /*member_p=*/true);
17690 parser->colon_corrects_to_scope_p = false;
17692 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17695 /* Parse the decl-specifier-seq. */
17696 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17697 cp_parser_decl_specifier_seq (parser,
17698 CP_PARSER_FLAGS_OPTIONAL,
17700 &declares_class_or_enum);
17701 prefix_attributes = decl_specifiers.attributes;
17702 decl_specifiers.attributes = NULL_TREE;
17703 /* Check for an invalid type-name. */
17704 if (!decl_specifiers.any_type_specifiers_p
17705 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17707 /* If there is no declarator, then the decl-specifier-seq should
17709 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17711 /* If there was no decl-specifier-seq, and the next token is a
17712 `;', then we have something like:
17718 Each member-declaration shall declare at least one member
17719 name of the class. */
17720 if (!decl_specifiers.any_specifiers_p)
17722 cp_token *token = cp_lexer_peek_token (parser->lexer);
17723 if (!in_system_header_at (token->location))
17724 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17730 /* See if this declaration is a friend. */
17731 friend_p = cp_parser_friend_p (&decl_specifiers);
17732 /* If there were decl-specifiers, check to see if there was
17733 a class-declaration. */
17734 type = check_tag_decl (&decl_specifiers);
17735 /* Nested classes have already been added to the class, but
17736 a `friend' needs to be explicitly registered. */
17739 /* If the `friend' keyword was present, the friend must
17740 be introduced with a class-key. */
17741 if (!declares_class_or_enum)
17742 error_at (decl_spec_token_start->location,
17743 "a class-key must be used when declaring a friend");
17746 template <typename T> struct A {
17747 friend struct A<T>::B;
17750 A<T>::B will be represented by a TYPENAME_TYPE, and
17751 therefore not recognized by check_tag_decl. */
17753 && decl_specifiers.type
17754 && TYPE_P (decl_specifiers.type))
17755 type = decl_specifiers.type;
17756 if (!type || !TYPE_P (type))
17757 error_at (decl_spec_token_start->location,
17758 "friend declaration does not name a class or "
17761 make_friend_class (current_class_type, type,
17762 /*complain=*/true);
17764 /* If there is no TYPE, an error message will already have
17766 else if (!type || type == error_mark_node)
17768 /* An anonymous aggregate has to be handled specially; such
17769 a declaration really declares a data member (with a
17770 particular type), as opposed to a nested class. */
17771 else if (ANON_AGGR_TYPE_P (type))
17773 /* Remove constructors and such from TYPE, now that we
17774 know it is an anonymous aggregate. */
17775 fixup_anonymous_aggr (type);
17776 /* And make the corresponding data member. */
17777 decl = build_decl (decl_spec_token_start->location,
17778 FIELD_DECL, NULL_TREE, type);
17779 /* Add it to the class. */
17780 finish_member_declaration (decl);
17783 cp_parser_check_access_in_redeclaration
17785 decl_spec_token_start->location);
17790 bool assume_semicolon = false;
17792 /* See if these declarations will be friends. */
17793 friend_p = cp_parser_friend_p (&decl_specifiers);
17795 /* Keep going until we hit the `;' at the end of the
17797 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17799 tree attributes = NULL_TREE;
17800 tree first_attribute;
17802 /* Peek at the next token. */
17803 token = cp_lexer_peek_token (parser->lexer);
17805 /* Check for a bitfield declaration. */
17806 if (token->type == CPP_COLON
17807 || (token->type == CPP_NAME
17808 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17814 /* Get the name of the bitfield. Note that we cannot just
17815 check TOKEN here because it may have been invalidated by
17816 the call to cp_lexer_peek_nth_token above. */
17817 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17818 identifier = cp_parser_identifier (parser);
17820 identifier = NULL_TREE;
17822 /* Consume the `:' token. */
17823 cp_lexer_consume_token (parser->lexer);
17824 /* Get the width of the bitfield. */
17826 = cp_parser_constant_expression (parser,
17827 /*allow_non_constant=*/false,
17830 /* Look for attributes that apply to the bitfield. */
17831 attributes = cp_parser_attributes_opt (parser);
17832 /* Remember which attributes are prefix attributes and
17834 first_attribute = attributes;
17835 /* Combine the attributes. */
17836 attributes = chainon (prefix_attributes, attributes);
17838 /* Create the bitfield declaration. */
17839 decl = grokbitfield (identifier
17840 ? make_id_declarator (NULL_TREE,
17850 cp_declarator *declarator;
17852 tree asm_specification;
17853 int ctor_dtor_or_conv_p;
17855 /* Parse the declarator. */
17857 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17858 &ctor_dtor_or_conv_p,
17859 /*parenthesized_p=*/NULL,
17860 /*member_p=*/true);
17862 /* If something went wrong parsing the declarator, make sure
17863 that we at least consume some tokens. */
17864 if (declarator == cp_error_declarator)
17866 /* Skip to the end of the statement. */
17867 cp_parser_skip_to_end_of_statement (parser);
17868 /* If the next token is not a semicolon, that is
17869 probably because we just skipped over the body of
17870 a function. So, we consume a semicolon if
17871 present, but do not issue an error message if it
17873 if (cp_lexer_next_token_is (parser->lexer,
17875 cp_lexer_consume_token (parser->lexer);
17879 if (declares_class_or_enum & 2)
17880 cp_parser_check_for_definition_in_return_type
17881 (declarator, decl_specifiers.type,
17882 decl_specifiers.type_location);
17884 /* Look for an asm-specification. */
17885 asm_specification = cp_parser_asm_specification_opt (parser);
17886 /* Look for attributes that apply to the declaration. */
17887 attributes = cp_parser_attributes_opt (parser);
17888 /* Remember which attributes are prefix attributes and
17890 first_attribute = attributes;
17891 /* Combine the attributes. */
17892 attributes = chainon (prefix_attributes, attributes);
17894 /* If it's an `=', then we have a constant-initializer or a
17895 pure-specifier. It is not correct to parse the
17896 initializer before registering the member declaration
17897 since the member declaration should be in scope while
17898 its initializer is processed. However, the rest of the
17899 front end does not yet provide an interface that allows
17900 us to handle this correctly. */
17901 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17905 A pure-specifier shall be used only in the declaration of
17906 a virtual function.
17908 A member-declarator can contain a constant-initializer
17909 only if it declares a static member of integral or
17912 Therefore, if the DECLARATOR is for a function, we look
17913 for a pure-specifier; otherwise, we look for a
17914 constant-initializer. When we call `grokfield', it will
17915 perform more stringent semantics checks. */
17916 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17917 if (function_declarator_p (declarator))
17918 initializer = cp_parser_pure_specifier (parser);
17920 /* Parse the initializer. */
17921 initializer = cp_parser_constant_initializer (parser);
17923 /* Otherwise, there is no initializer. */
17925 initializer = NULL_TREE;
17927 /* See if we are probably looking at a function
17928 definition. We are certainly not looking at a
17929 member-declarator. Calling `grokfield' has
17930 side-effects, so we must not do it unless we are sure
17931 that we are looking at a member-declarator. */
17932 if (cp_parser_token_starts_function_definition_p
17933 (cp_lexer_peek_token (parser->lexer)))
17935 /* The grammar does not allow a pure-specifier to be
17936 used when a member function is defined. (It is
17937 possible that this fact is an oversight in the
17938 standard, since a pure function may be defined
17939 outside of the class-specifier. */
17941 error_at (initializer_token_start->location,
17942 "pure-specifier on function-definition");
17943 decl = cp_parser_save_member_function_body (parser,
17947 /* If the member was not a friend, declare it here. */
17949 finish_member_declaration (decl);
17950 /* Peek at the next token. */
17951 token = cp_lexer_peek_token (parser->lexer);
17952 /* If the next token is a semicolon, consume it. */
17953 if (token->type == CPP_SEMICOLON)
17954 cp_lexer_consume_token (parser->lexer);
17958 if (declarator->kind == cdk_function)
17959 declarator->id_loc = token->location;
17960 /* Create the declaration. */
17961 decl = grokfield (declarator, &decl_specifiers,
17962 initializer, /*init_const_expr_p=*/true,
17967 /* Reset PREFIX_ATTRIBUTES. */
17968 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17969 attributes = TREE_CHAIN (attributes);
17971 TREE_CHAIN (attributes) = NULL_TREE;
17973 /* If there is any qualification still in effect, clear it
17974 now; we will be starting fresh with the next declarator. */
17975 parser->scope = NULL_TREE;
17976 parser->qualifying_scope = NULL_TREE;
17977 parser->object_scope = NULL_TREE;
17978 /* If it's a `,', then there are more declarators. */
17979 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17980 cp_lexer_consume_token (parser->lexer);
17981 /* If the next token isn't a `;', then we have a parse error. */
17982 else if (cp_lexer_next_token_is_not (parser->lexer,
17985 /* The next token might be a ways away from where the
17986 actual semicolon is missing. Find the previous token
17987 and use that for our error position. */
17988 cp_token *token = cp_lexer_previous_token (parser->lexer);
17989 error_at (token->location,
17990 "expected %<;%> at end of member declaration");
17992 /* Assume that the user meant to provide a semicolon. If
17993 we were to cp_parser_skip_to_end_of_statement, we might
17994 skip to a semicolon inside a member function definition
17995 and issue nonsensical error messages. */
17996 assume_semicolon = true;
18001 /* Add DECL to the list of members. */
18003 finish_member_declaration (decl);
18005 if (TREE_CODE (decl) == FUNCTION_DECL)
18006 cp_parser_save_default_args (parser, decl);
18009 if (assume_semicolon)
18014 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18016 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18019 /* Parse a pure-specifier.
18024 Returns INTEGER_ZERO_NODE if a pure specifier is found.
18025 Otherwise, ERROR_MARK_NODE is returned. */
18028 cp_parser_pure_specifier (cp_parser* parser)
18032 /* Look for the `=' token. */
18033 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18034 return error_mark_node;
18035 /* Look for the `0' token. */
18036 token = cp_lexer_peek_token (parser->lexer);
18038 if (token->type == CPP_EOF
18039 || token->type == CPP_PRAGMA_EOL)
18040 return error_mark_node;
18042 cp_lexer_consume_token (parser->lexer);
18044 /* Accept = default or = delete in c++0x mode. */
18045 if (token->keyword == RID_DEFAULT
18046 || token->keyword == RID_DELETE)
18048 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18049 return token->u.value;
18052 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
18053 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18055 cp_parser_error (parser,
18056 "invalid pure specifier (only %<= 0%> is allowed)");
18057 cp_parser_skip_to_end_of_statement (parser);
18058 return error_mark_node;
18060 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18062 error_at (token->location, "templates may not be %<virtual%>");
18063 return error_mark_node;
18066 return integer_zero_node;
18069 /* Parse a constant-initializer.
18071 constant-initializer:
18072 = constant-expression
18074 Returns a representation of the constant-expression. */
18077 cp_parser_constant_initializer (cp_parser* parser)
18079 /* Look for the `=' token. */
18080 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18081 return error_mark_node;
18083 /* It is invalid to write:
18085 struct S { static const int i = { 7 }; };
18088 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18090 cp_parser_error (parser,
18091 "a brace-enclosed initializer is not allowed here");
18092 /* Consume the opening brace. */
18093 cp_lexer_consume_token (parser->lexer);
18094 /* Skip the initializer. */
18095 cp_parser_skip_to_closing_brace (parser);
18096 /* Look for the trailing `}'. */
18097 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18099 return error_mark_node;
18102 return cp_parser_constant_expression (parser,
18103 /*allow_non_constant=*/false,
18107 /* Derived classes [gram.class.derived] */
18109 /* Parse a base-clause.
18112 : base-specifier-list
18114 base-specifier-list:
18115 base-specifier ... [opt]
18116 base-specifier-list , base-specifier ... [opt]
18118 Returns a TREE_LIST representing the base-classes, in the order in
18119 which they were declared. The representation of each node is as
18120 described by cp_parser_base_specifier.
18122 In the case that no bases are specified, this function will return
18123 NULL_TREE, not ERROR_MARK_NODE. */
18126 cp_parser_base_clause (cp_parser* parser)
18128 tree bases = NULL_TREE;
18130 /* Look for the `:' that begins the list. */
18131 cp_parser_require (parser, CPP_COLON, RT_COLON);
18133 /* Scan the base-specifier-list. */
18138 bool pack_expansion_p = false;
18140 /* Look for the base-specifier. */
18141 base = cp_parser_base_specifier (parser);
18142 /* Look for the (optional) ellipsis. */
18143 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18145 /* Consume the `...'. */
18146 cp_lexer_consume_token (parser->lexer);
18148 pack_expansion_p = true;
18151 /* Add BASE to the front of the list. */
18152 if (base != error_mark_node)
18154 if (pack_expansion_p)
18155 /* Make this a pack expansion type. */
18156 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18159 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18161 TREE_CHAIN (base) = bases;
18165 /* Peek at the next token. */
18166 token = cp_lexer_peek_token (parser->lexer);
18167 /* If it's not a comma, then the list is complete. */
18168 if (token->type != CPP_COMMA)
18170 /* Consume the `,'. */
18171 cp_lexer_consume_token (parser->lexer);
18174 /* PARSER->SCOPE may still be non-NULL at this point, if the last
18175 base class had a qualified name. However, the next name that
18176 appears is certainly not qualified. */
18177 parser->scope = NULL_TREE;
18178 parser->qualifying_scope = NULL_TREE;
18179 parser->object_scope = NULL_TREE;
18181 return nreverse (bases);
18184 /* Parse a base-specifier.
18187 :: [opt] nested-name-specifier [opt] class-name
18188 virtual access-specifier [opt] :: [opt] nested-name-specifier
18190 access-specifier virtual [opt] :: [opt] nested-name-specifier
18193 Returns a TREE_LIST. The TREE_PURPOSE will be one of
18194 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18195 indicate the specifiers provided. The TREE_VALUE will be a TYPE
18196 (or the ERROR_MARK_NODE) indicating the type that was specified. */
18199 cp_parser_base_specifier (cp_parser* parser)
18203 bool virtual_p = false;
18204 bool duplicate_virtual_error_issued_p = false;
18205 bool duplicate_access_error_issued_p = false;
18206 bool class_scope_p, template_p;
18207 tree access = access_default_node;
18210 /* Process the optional `virtual' and `access-specifier'. */
18213 /* Peek at the next token. */
18214 token = cp_lexer_peek_token (parser->lexer);
18215 /* Process `virtual'. */
18216 switch (token->keyword)
18219 /* If `virtual' appears more than once, issue an error. */
18220 if (virtual_p && !duplicate_virtual_error_issued_p)
18222 cp_parser_error (parser,
18223 "%<virtual%> specified more than once in base-specified");
18224 duplicate_virtual_error_issued_p = true;
18229 /* Consume the `virtual' token. */
18230 cp_lexer_consume_token (parser->lexer);
18235 case RID_PROTECTED:
18237 /* If more than one access specifier appears, issue an
18239 if (access != access_default_node
18240 && !duplicate_access_error_issued_p)
18242 cp_parser_error (parser,
18243 "more than one access specifier in base-specified");
18244 duplicate_access_error_issued_p = true;
18247 access = ridpointers[(int) token->keyword];
18249 /* Consume the access-specifier. */
18250 cp_lexer_consume_token (parser->lexer);
18259 /* It is not uncommon to see programs mechanically, erroneously, use
18260 the 'typename' keyword to denote (dependent) qualified types
18261 as base classes. */
18262 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18264 token = cp_lexer_peek_token (parser->lexer);
18265 if (!processing_template_decl)
18266 error_at (token->location,
18267 "keyword %<typename%> not allowed outside of templates");
18269 error_at (token->location,
18270 "keyword %<typename%> not allowed in this context "
18271 "(the base class is implicitly a type)");
18272 cp_lexer_consume_token (parser->lexer);
18275 /* Look for the optional `::' operator. */
18276 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18277 /* Look for the nested-name-specifier. The simplest way to
18282 The keyword `typename' is not permitted in a base-specifier or
18283 mem-initializer; in these contexts a qualified name that
18284 depends on a template-parameter is implicitly assumed to be a
18287 is to pretend that we have seen the `typename' keyword at this
18289 cp_parser_nested_name_specifier_opt (parser,
18290 /*typename_keyword_p=*/true,
18291 /*check_dependency_p=*/true,
18293 /*is_declaration=*/true);
18294 /* If the base class is given by a qualified name, assume that names
18295 we see are type names or templates, as appropriate. */
18296 class_scope_p = (parser->scope && TYPE_P (parser->scope));
18297 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18299 /* Finally, look for the class-name. */
18300 type = cp_parser_class_name (parser,
18304 /*check_dependency_p=*/true,
18305 /*class_head_p=*/false,
18306 /*is_declaration=*/true);
18308 if (type == error_mark_node)
18309 return error_mark_node;
18311 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18314 /* Exception handling [gram.exception] */
18316 /* Parse an (optional) exception-specification.
18318 exception-specification:
18319 throw ( type-id-list [opt] )
18321 Returns a TREE_LIST representing the exception-specification. The
18322 TREE_VALUE of each node is a type. */
18325 cp_parser_exception_specification_opt (cp_parser* parser)
18329 const char *saved_message;
18331 /* Peek at the next token. */
18332 token = cp_lexer_peek_token (parser->lexer);
18334 /* Is it a noexcept-specification? */
18335 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18338 cp_lexer_consume_token (parser->lexer);
18340 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18342 cp_lexer_consume_token (parser->lexer);
18344 /* Types may not be defined in an exception-specification. */
18345 saved_message = parser->type_definition_forbidden_message;
18346 parser->type_definition_forbidden_message
18347 = G_("types may not be defined in an exception-specification");
18349 expr = cp_parser_constant_expression (parser, false, NULL);
18351 /* Restore the saved message. */
18352 parser->type_definition_forbidden_message = saved_message;
18354 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18357 expr = boolean_true_node;
18359 return build_noexcept_spec (expr, tf_warning_or_error);
18362 /* If it's not `throw', then there's no exception-specification. */
18363 if (!cp_parser_is_keyword (token, RID_THROW))
18367 /* Enable this once a lot of code has transitioned to noexcept? */
18368 if (cxx_dialect == cxx0x && !in_system_header)
18369 warning (OPT_Wdeprecated, "dynamic exception specifications are "
18370 "deprecated in C++0x; use %<noexcept%> instead");
18373 /* Consume the `throw'. */
18374 cp_lexer_consume_token (parser->lexer);
18376 /* Look for the `('. */
18377 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18379 /* Peek at the next token. */
18380 token = cp_lexer_peek_token (parser->lexer);
18381 /* If it's not a `)', then there is a type-id-list. */
18382 if (token->type != CPP_CLOSE_PAREN)
18384 /* Types may not be defined in an exception-specification. */
18385 saved_message = parser->type_definition_forbidden_message;
18386 parser->type_definition_forbidden_message
18387 = G_("types may not be defined in an exception-specification");
18388 /* Parse the type-id-list. */
18389 type_id_list = cp_parser_type_id_list (parser);
18390 /* Restore the saved message. */
18391 parser->type_definition_forbidden_message = saved_message;
18394 type_id_list = empty_except_spec;
18396 /* Look for the `)'. */
18397 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18399 return type_id_list;
18402 /* Parse an (optional) type-id-list.
18406 type-id-list , type-id ... [opt]
18408 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18409 in the order that the types were presented. */
18412 cp_parser_type_id_list (cp_parser* parser)
18414 tree types = NULL_TREE;
18421 /* Get the next type-id. */
18422 type = cp_parser_type_id (parser);
18423 /* Parse the optional ellipsis. */
18424 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18426 /* Consume the `...'. */
18427 cp_lexer_consume_token (parser->lexer);
18429 /* Turn the type into a pack expansion expression. */
18430 type = make_pack_expansion (type);
18432 /* Add it to the list. */
18433 types = add_exception_specifier (types, type, /*complain=*/1);
18434 /* Peek at the next token. */
18435 token = cp_lexer_peek_token (parser->lexer);
18436 /* If it is not a `,', we are done. */
18437 if (token->type != CPP_COMMA)
18439 /* Consume the `,'. */
18440 cp_lexer_consume_token (parser->lexer);
18443 return nreverse (types);
18446 /* Parse a try-block.
18449 try compound-statement handler-seq */
18452 cp_parser_try_block (cp_parser* parser)
18456 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18457 try_block = begin_try_block ();
18458 cp_parser_compound_statement (parser, NULL, true);
18459 finish_try_block (try_block);
18460 cp_parser_handler_seq (parser);
18461 finish_handler_sequence (try_block);
18466 /* Parse a function-try-block.
18468 function-try-block:
18469 try ctor-initializer [opt] function-body handler-seq */
18472 cp_parser_function_try_block (cp_parser* parser)
18474 tree compound_stmt;
18476 bool ctor_initializer_p;
18478 /* Look for the `try' keyword. */
18479 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18481 /* Let the rest of the front end know where we are. */
18482 try_block = begin_function_try_block (&compound_stmt);
18483 /* Parse the function-body. */
18485 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18486 /* We're done with the `try' part. */
18487 finish_function_try_block (try_block);
18488 /* Parse the handlers. */
18489 cp_parser_handler_seq (parser);
18490 /* We're done with the handlers. */
18491 finish_function_handler_sequence (try_block, compound_stmt);
18493 return ctor_initializer_p;
18496 /* Parse a handler-seq.
18499 handler handler-seq [opt] */
18502 cp_parser_handler_seq (cp_parser* parser)
18508 /* Parse the handler. */
18509 cp_parser_handler (parser);
18510 /* Peek at the next token. */
18511 token = cp_lexer_peek_token (parser->lexer);
18512 /* If it's not `catch' then there are no more handlers. */
18513 if (!cp_parser_is_keyword (token, RID_CATCH))
18518 /* Parse a handler.
18521 catch ( exception-declaration ) compound-statement */
18524 cp_parser_handler (cp_parser* parser)
18529 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18530 handler = begin_handler ();
18531 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18532 declaration = cp_parser_exception_declaration (parser);
18533 finish_handler_parms (declaration, handler);
18534 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18535 cp_parser_compound_statement (parser, NULL, false);
18536 finish_handler (handler);
18539 /* Parse an exception-declaration.
18541 exception-declaration:
18542 type-specifier-seq declarator
18543 type-specifier-seq abstract-declarator
18547 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18548 ellipsis variant is used. */
18551 cp_parser_exception_declaration (cp_parser* parser)
18553 cp_decl_specifier_seq type_specifiers;
18554 cp_declarator *declarator;
18555 const char *saved_message;
18557 /* If it's an ellipsis, it's easy to handle. */
18558 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18560 /* Consume the `...' token. */
18561 cp_lexer_consume_token (parser->lexer);
18565 /* Types may not be defined in exception-declarations. */
18566 saved_message = parser->type_definition_forbidden_message;
18567 parser->type_definition_forbidden_message
18568 = G_("types may not be defined in exception-declarations");
18570 /* Parse the type-specifier-seq. */
18571 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18572 /*is_trailing_return=*/false,
18574 /* If it's a `)', then there is no declarator. */
18575 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18578 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18579 /*ctor_dtor_or_conv_p=*/NULL,
18580 /*parenthesized_p=*/NULL,
18581 /*member_p=*/false);
18583 /* Restore the saved message. */
18584 parser->type_definition_forbidden_message = saved_message;
18586 if (!type_specifiers.any_specifiers_p)
18587 return error_mark_node;
18589 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18592 /* Parse a throw-expression.
18595 throw assignment-expression [opt]
18597 Returns a THROW_EXPR representing the throw-expression. */
18600 cp_parser_throw_expression (cp_parser* parser)
18605 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18606 token = cp_lexer_peek_token (parser->lexer);
18607 /* Figure out whether or not there is an assignment-expression
18608 following the "throw" keyword. */
18609 if (token->type == CPP_COMMA
18610 || token->type == CPP_SEMICOLON
18611 || token->type == CPP_CLOSE_PAREN
18612 || token->type == CPP_CLOSE_SQUARE
18613 || token->type == CPP_CLOSE_BRACE
18614 || token->type == CPP_COLON)
18615 expression = NULL_TREE;
18617 expression = cp_parser_assignment_expression (parser,
18618 /*cast_p=*/false, NULL);
18620 return build_throw (expression);
18623 /* GNU Extensions */
18625 /* Parse an (optional) asm-specification.
18628 asm ( string-literal )
18630 If the asm-specification is present, returns a STRING_CST
18631 corresponding to the string-literal. Otherwise, returns
18635 cp_parser_asm_specification_opt (cp_parser* parser)
18638 tree asm_specification;
18640 /* Peek at the next token. */
18641 token = cp_lexer_peek_token (parser->lexer);
18642 /* If the next token isn't the `asm' keyword, then there's no
18643 asm-specification. */
18644 if (!cp_parser_is_keyword (token, RID_ASM))
18647 /* Consume the `asm' token. */
18648 cp_lexer_consume_token (parser->lexer);
18649 /* Look for the `('. */
18650 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18652 /* Look for the string-literal. */
18653 asm_specification = cp_parser_string_literal (parser, false, false);
18655 /* Look for the `)'. */
18656 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18658 return asm_specification;
18661 /* Parse an asm-operand-list.
18665 asm-operand-list , asm-operand
18668 string-literal ( expression )
18669 [ string-literal ] string-literal ( expression )
18671 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18672 each node is the expression. The TREE_PURPOSE is itself a
18673 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18674 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18675 is a STRING_CST for the string literal before the parenthesis. Returns
18676 ERROR_MARK_NODE if any of the operands are invalid. */
18679 cp_parser_asm_operand_list (cp_parser* parser)
18681 tree asm_operands = NULL_TREE;
18682 bool invalid_operands = false;
18686 tree string_literal;
18690 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18692 /* Consume the `[' token. */
18693 cp_lexer_consume_token (parser->lexer);
18694 /* Read the operand name. */
18695 name = cp_parser_identifier (parser);
18696 if (name != error_mark_node)
18697 name = build_string (IDENTIFIER_LENGTH (name),
18698 IDENTIFIER_POINTER (name));
18699 /* Look for the closing `]'. */
18700 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18704 /* Look for the string-literal. */
18705 string_literal = cp_parser_string_literal (parser, false, false);
18707 /* Look for the `('. */
18708 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18709 /* Parse the expression. */
18710 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18711 /* Look for the `)'. */
18712 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18714 if (name == error_mark_node
18715 || string_literal == error_mark_node
18716 || expression == error_mark_node)
18717 invalid_operands = true;
18719 /* Add this operand to the list. */
18720 asm_operands = tree_cons (build_tree_list (name, string_literal),
18723 /* If the next token is not a `,', there are no more
18725 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18727 /* Consume the `,'. */
18728 cp_lexer_consume_token (parser->lexer);
18731 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18734 /* Parse an asm-clobber-list.
18738 asm-clobber-list , string-literal
18740 Returns a TREE_LIST, indicating the clobbers in the order that they
18741 appeared. The TREE_VALUE of each node is a STRING_CST. */
18744 cp_parser_asm_clobber_list (cp_parser* parser)
18746 tree clobbers = NULL_TREE;
18750 tree string_literal;
18752 /* Look for the string literal. */
18753 string_literal = cp_parser_string_literal (parser, false, false);
18754 /* Add it to the list. */
18755 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18756 /* If the next token is not a `,', then the list is
18758 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18760 /* Consume the `,' token. */
18761 cp_lexer_consume_token (parser->lexer);
18767 /* Parse an asm-label-list.
18771 asm-label-list , identifier
18773 Returns a TREE_LIST, indicating the labels in the order that they
18774 appeared. The TREE_VALUE of each node is a label. */
18777 cp_parser_asm_label_list (cp_parser* parser)
18779 tree labels = NULL_TREE;
18783 tree identifier, label, name;
18785 /* Look for the identifier. */
18786 identifier = cp_parser_identifier (parser);
18787 if (!error_operand_p (identifier))
18789 label = lookup_label (identifier);
18790 if (TREE_CODE (label) == LABEL_DECL)
18792 TREE_USED (label) = 1;
18793 check_goto (label);
18794 name = build_string (IDENTIFIER_LENGTH (identifier),
18795 IDENTIFIER_POINTER (identifier));
18796 labels = tree_cons (name, label, labels);
18799 /* If the next token is not a `,', then the list is
18801 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18803 /* Consume the `,' token. */
18804 cp_lexer_consume_token (parser->lexer);
18807 return nreverse (labels);
18810 /* Parse an (optional) series of attributes.
18813 attributes attribute
18816 __attribute__ (( attribute-list [opt] ))
18818 The return value is as for cp_parser_attribute_list. */
18821 cp_parser_attributes_opt (cp_parser* parser)
18823 tree attributes = NULL_TREE;
18828 tree attribute_list;
18830 /* Peek at the next token. */
18831 token = cp_lexer_peek_token (parser->lexer);
18832 /* If it's not `__attribute__', then we're done. */
18833 if (token->keyword != RID_ATTRIBUTE)
18836 /* Consume the `__attribute__' keyword. */
18837 cp_lexer_consume_token (parser->lexer);
18838 /* Look for the two `(' tokens. */
18839 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18840 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18842 /* Peek at the next token. */
18843 token = cp_lexer_peek_token (parser->lexer);
18844 if (token->type != CPP_CLOSE_PAREN)
18845 /* Parse the attribute-list. */
18846 attribute_list = cp_parser_attribute_list (parser);
18848 /* If the next token is a `)', then there is no attribute
18850 attribute_list = NULL;
18852 /* Look for the two `)' tokens. */
18853 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18854 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18856 /* Add these new attributes to the list. */
18857 attributes = chainon (attributes, attribute_list);
18863 /* Parse an attribute-list.
18867 attribute-list , attribute
18871 identifier ( identifier )
18872 identifier ( identifier , expression-list )
18873 identifier ( expression-list )
18875 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18876 to an attribute. The TREE_PURPOSE of each node is the identifier
18877 indicating which attribute is in use. The TREE_VALUE represents
18878 the arguments, if any. */
18881 cp_parser_attribute_list (cp_parser* parser)
18883 tree attribute_list = NULL_TREE;
18884 bool save_translate_strings_p = parser->translate_strings_p;
18886 parser->translate_strings_p = false;
18893 /* Look for the identifier. We also allow keywords here; for
18894 example `__attribute__ ((const))' is legal. */
18895 token = cp_lexer_peek_token (parser->lexer);
18896 if (token->type == CPP_NAME
18897 || token->type == CPP_KEYWORD)
18899 tree arguments = NULL_TREE;
18901 /* Consume the token. */
18902 token = cp_lexer_consume_token (parser->lexer);
18904 /* Save away the identifier that indicates which attribute
18906 identifier = (token->type == CPP_KEYWORD)
18907 /* For keywords, use the canonical spelling, not the
18908 parsed identifier. */
18909 ? ridpointers[(int) token->keyword]
18912 attribute = build_tree_list (identifier, NULL_TREE);
18914 /* Peek at the next token. */
18915 token = cp_lexer_peek_token (parser->lexer);
18916 /* If it's an `(', then parse the attribute arguments. */
18917 if (token->type == CPP_OPEN_PAREN)
18920 int attr_flag = (attribute_takes_identifier_p (identifier)
18921 ? id_attr : normal_attr);
18922 vec = cp_parser_parenthesized_expression_list
18923 (parser, attr_flag, /*cast_p=*/false,
18924 /*allow_expansion_p=*/false,
18925 /*non_constant_p=*/NULL);
18927 arguments = error_mark_node;
18930 arguments = build_tree_list_vec (vec);
18931 release_tree_vector (vec);
18933 /* Save the arguments away. */
18934 TREE_VALUE (attribute) = arguments;
18937 if (arguments != error_mark_node)
18939 /* Add this attribute to the list. */
18940 TREE_CHAIN (attribute) = attribute_list;
18941 attribute_list = attribute;
18944 token = cp_lexer_peek_token (parser->lexer);
18946 /* Now, look for more attributes. If the next token isn't a
18947 `,', we're done. */
18948 if (token->type != CPP_COMMA)
18951 /* Consume the comma and keep going. */
18952 cp_lexer_consume_token (parser->lexer);
18954 parser->translate_strings_p = save_translate_strings_p;
18956 /* We built up the list in reverse order. */
18957 return nreverse (attribute_list);
18960 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18961 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18962 current value of the PEDANTIC flag, regardless of whether or not
18963 the `__extension__' keyword is present. The caller is responsible
18964 for restoring the value of the PEDANTIC flag. */
18967 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18969 /* Save the old value of the PEDANTIC flag. */
18970 *saved_pedantic = pedantic;
18972 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18974 /* Consume the `__extension__' token. */
18975 cp_lexer_consume_token (parser->lexer);
18976 /* We're not being pedantic while the `__extension__' keyword is
18986 /* Parse a label declaration.
18989 __label__ label-declarator-seq ;
18991 label-declarator-seq:
18992 identifier , label-declarator-seq
18996 cp_parser_label_declaration (cp_parser* parser)
18998 /* Look for the `__label__' keyword. */
18999 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19005 /* Look for an identifier. */
19006 identifier = cp_parser_identifier (parser);
19007 /* If we failed, stop. */
19008 if (identifier == error_mark_node)
19010 /* Declare it as a label. */
19011 finish_label_decl (identifier);
19012 /* If the next token is a `;', stop. */
19013 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19015 /* Look for the `,' separating the label declarations. */
19016 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19019 /* Look for the final `;'. */
19020 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19023 /* Support Functions */
19025 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19026 NAME should have one of the representations used for an
19027 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19028 is returned. If PARSER->SCOPE is a dependent type, then a
19029 SCOPE_REF is returned.
19031 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19032 returned; the name was already resolved when the TEMPLATE_ID_EXPR
19033 was formed. Abstractly, such entities should not be passed to this
19034 function, because they do not need to be looked up, but it is
19035 simpler to check for this special case here, rather than at the
19038 In cases not explicitly covered above, this function returns a
19039 DECL, OVERLOAD, or baselink representing the result of the lookup.
19040 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19043 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19044 (e.g., "struct") that was used. In that case bindings that do not
19045 refer to types are ignored.
19047 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19050 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19053 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19056 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19057 TREE_LIST of candidates if name-lookup results in an ambiguity, and
19058 NULL_TREE otherwise. */
19061 cp_parser_lookup_name (cp_parser *parser, tree name,
19062 enum tag_types tag_type,
19065 bool check_dependency,
19066 tree *ambiguous_decls,
19067 location_t name_location)
19071 tree object_type = parser->context->object_type;
19073 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19074 flags |= LOOKUP_COMPLAIN;
19076 /* Assume that the lookup will be unambiguous. */
19077 if (ambiguous_decls)
19078 *ambiguous_decls = NULL_TREE;
19080 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19081 no longer valid. Note that if we are parsing tentatively, and
19082 the parse fails, OBJECT_TYPE will be automatically restored. */
19083 parser->context->object_type = NULL_TREE;
19085 if (name == error_mark_node)
19086 return error_mark_node;
19088 /* A template-id has already been resolved; there is no lookup to
19090 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19092 if (BASELINK_P (name))
19094 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19095 == TEMPLATE_ID_EXPR);
19099 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
19100 it should already have been checked to make sure that the name
19101 used matches the type being destroyed. */
19102 if (TREE_CODE (name) == BIT_NOT_EXPR)
19106 /* Figure out to which type this destructor applies. */
19108 type = parser->scope;
19109 else if (object_type)
19110 type = object_type;
19112 type = current_class_type;
19113 /* If that's not a class type, there is no destructor. */
19114 if (!type || !CLASS_TYPE_P (type))
19115 return error_mark_node;
19116 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19117 lazily_declare_fn (sfk_destructor, type);
19118 if (!CLASSTYPE_DESTRUCTORS (type))
19119 return error_mark_node;
19120 /* If it was a class type, return the destructor. */
19121 return CLASSTYPE_DESTRUCTORS (type);
19124 /* By this point, the NAME should be an ordinary identifier. If
19125 the id-expression was a qualified name, the qualifying scope is
19126 stored in PARSER->SCOPE at this point. */
19127 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19129 /* Perform the lookup. */
19134 if (parser->scope == error_mark_node)
19135 return error_mark_node;
19137 /* If the SCOPE is dependent, the lookup must be deferred until
19138 the template is instantiated -- unless we are explicitly
19139 looking up names in uninstantiated templates. Even then, we
19140 cannot look up the name if the scope is not a class type; it
19141 might, for example, be a template type parameter. */
19142 dependent_p = (TYPE_P (parser->scope)
19143 && dependent_scope_p (parser->scope));
19144 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19146 /* Defer lookup. */
19147 decl = error_mark_node;
19150 tree pushed_scope = NULL_TREE;
19152 /* If PARSER->SCOPE is a dependent type, then it must be a
19153 class type, and we must not be checking dependencies;
19154 otherwise, we would have processed this lookup above. So
19155 that PARSER->SCOPE is not considered a dependent base by
19156 lookup_member, we must enter the scope here. */
19158 pushed_scope = push_scope (parser->scope);
19160 /* If the PARSER->SCOPE is a template specialization, it
19161 may be instantiated during name lookup. In that case,
19162 errors may be issued. Even if we rollback the current
19163 tentative parse, those errors are valid. */
19164 decl = lookup_qualified_name (parser->scope, name,
19165 tag_type != none_type,
19166 /*complain=*/true);
19168 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19169 lookup result and the nested-name-specifier nominates a class C:
19170 * if the name specified after the nested-name-specifier, when
19171 looked up in C, is the injected-class-name of C (Clause 9), or
19172 * if the name specified after the nested-name-specifier is the
19173 same as the identifier or the simple-template-id's template-
19174 name in the last component of the nested-name-specifier,
19175 the name is instead considered to name the constructor of
19176 class C. [ Note: for example, the constructor is not an
19177 acceptable lookup result in an elaborated-type-specifier so
19178 the constructor would not be used in place of the
19179 injected-class-name. --end note ] Such a constructor name
19180 shall be used only in the declarator-id of a declaration that
19181 names a constructor or in a using-declaration. */
19182 if (tag_type == none_type
19183 && DECL_SELF_REFERENCE_P (decl)
19184 && same_type_p (DECL_CONTEXT (decl), parser->scope))
19185 decl = lookup_qualified_name (parser->scope, ctor_identifier,
19186 tag_type != none_type,
19187 /*complain=*/true);
19189 /* If we have a single function from a using decl, pull it out. */
19190 if (TREE_CODE (decl) == OVERLOAD
19191 && !really_overloaded_fn (decl))
19192 decl = OVL_FUNCTION (decl);
19195 pop_scope (pushed_scope);
19198 /* If the scope is a dependent type and either we deferred lookup or
19199 we did lookup but didn't find the name, rememeber the name. */
19200 if (decl == error_mark_node && TYPE_P (parser->scope)
19201 && dependent_type_p (parser->scope))
19207 /* The resolution to Core Issue 180 says that `struct
19208 A::B' should be considered a type-name, even if `A'
19210 type = make_typename_type (parser->scope, name, tag_type,
19211 /*complain=*/tf_error);
19212 decl = TYPE_NAME (type);
19214 else if (is_template
19215 && (cp_parser_next_token_ends_template_argument_p (parser)
19216 || cp_lexer_next_token_is (parser->lexer,
19218 decl = make_unbound_class_template (parser->scope,
19220 /*complain=*/tf_error);
19222 decl = build_qualified_name (/*type=*/NULL_TREE,
19223 parser->scope, name,
19226 parser->qualifying_scope = parser->scope;
19227 parser->object_scope = NULL_TREE;
19229 else if (object_type)
19231 tree object_decl = NULL_TREE;
19232 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19233 OBJECT_TYPE is not a class. */
19234 if (CLASS_TYPE_P (object_type))
19235 /* If the OBJECT_TYPE is a template specialization, it may
19236 be instantiated during name lookup. In that case, errors
19237 may be issued. Even if we rollback the current tentative
19238 parse, those errors are valid. */
19239 object_decl = lookup_member (object_type,
19242 tag_type != none_type);
19243 /* Look it up in the enclosing context, too. */
19244 decl = lookup_name_real (name, tag_type != none_type,
19246 /*block_p=*/true, is_namespace, flags);
19247 parser->object_scope = object_type;
19248 parser->qualifying_scope = NULL_TREE;
19250 decl = object_decl;
19254 decl = lookup_name_real (name, tag_type != none_type,
19256 /*block_p=*/true, is_namespace, flags);
19257 parser->qualifying_scope = NULL_TREE;
19258 parser->object_scope = NULL_TREE;
19261 /* If the lookup failed, let our caller know. */
19262 if (!decl || decl == error_mark_node)
19263 return error_mark_node;
19265 /* Pull out the template from an injected-class-name (or multiple). */
19267 decl = maybe_get_template_decl_from_type_decl (decl);
19269 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19270 if (TREE_CODE (decl) == TREE_LIST)
19272 if (ambiguous_decls)
19273 *ambiguous_decls = decl;
19274 /* The error message we have to print is too complicated for
19275 cp_parser_error, so we incorporate its actions directly. */
19276 if (!cp_parser_simulate_error (parser))
19278 error_at (name_location, "reference to %qD is ambiguous",
19280 print_candidates (decl);
19282 return error_mark_node;
19285 gcc_assert (DECL_P (decl)
19286 || TREE_CODE (decl) == OVERLOAD
19287 || TREE_CODE (decl) == SCOPE_REF
19288 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19289 || BASELINK_P (decl));
19291 /* If we have resolved the name of a member declaration, check to
19292 see if the declaration is accessible. When the name resolves to
19293 set of overloaded functions, accessibility is checked when
19294 overload resolution is done.
19296 During an explicit instantiation, access is not checked at all,
19297 as per [temp.explicit]. */
19299 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19304 /* Like cp_parser_lookup_name, but for use in the typical case where
19305 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19306 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19309 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19311 return cp_parser_lookup_name (parser, name,
19313 /*is_template=*/false,
19314 /*is_namespace=*/false,
19315 /*check_dependency=*/true,
19316 /*ambiguous_decls=*/NULL,
19320 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19321 the current context, return the TYPE_DECL. If TAG_NAME_P is
19322 true, the DECL indicates the class being defined in a class-head,
19323 or declared in an elaborated-type-specifier.
19325 Otherwise, return DECL. */
19328 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19330 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19331 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19334 template <typename T> struct B;
19337 template <typename T> struct A::B {};
19339 Similarly, in an elaborated-type-specifier:
19341 namespace N { struct X{}; }
19344 template <typename T> friend struct N::X;
19347 However, if the DECL refers to a class type, and we are in
19348 the scope of the class, then the name lookup automatically
19349 finds the TYPE_DECL created by build_self_reference rather
19350 than a TEMPLATE_DECL. For example, in:
19352 template <class T> struct S {
19356 there is no need to handle such case. */
19358 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19359 return DECL_TEMPLATE_RESULT (decl);
19364 /* If too many, or too few, template-parameter lists apply to the
19365 declarator, issue an error message. Returns TRUE if all went well,
19366 and FALSE otherwise. */
19369 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19370 cp_declarator *declarator,
19371 location_t declarator_location)
19373 unsigned num_templates;
19375 /* We haven't seen any classes that involve template parameters yet. */
19378 switch (declarator->kind)
19381 if (declarator->u.id.qualifying_scope)
19385 scope = declarator->u.id.qualifying_scope;
19387 while (scope && CLASS_TYPE_P (scope))
19389 /* You're supposed to have one `template <...>'
19390 for every template class, but you don't need one
19391 for a full specialization. For example:
19393 template <class T> struct S{};
19394 template <> struct S<int> { void f(); };
19395 void S<int>::f () {}
19397 is correct; there shouldn't be a `template <>' for
19398 the definition of `S<int>::f'. */
19399 if (!CLASSTYPE_TEMPLATE_INFO (scope))
19400 /* If SCOPE does not have template information of any
19401 kind, then it is not a template, nor is it nested
19402 within a template. */
19404 if (explicit_class_specialization_p (scope))
19406 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19409 scope = TYPE_CONTEXT (scope);
19412 else if (TREE_CODE (declarator->u.id.unqualified_name)
19413 == TEMPLATE_ID_EXPR)
19414 /* If the DECLARATOR has the form `X<y>' then it uses one
19415 additional level of template parameters. */
19418 return cp_parser_check_template_parameters
19419 (parser, num_templates, declarator_location, declarator);
19425 case cdk_reference:
19427 return (cp_parser_check_declarator_template_parameters
19428 (parser, declarator->declarator, declarator_location));
19434 gcc_unreachable ();
19439 /* NUM_TEMPLATES were used in the current declaration. If that is
19440 invalid, return FALSE and issue an error messages. Otherwise,
19441 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19442 declarator and we can print more accurate diagnostics. */
19445 cp_parser_check_template_parameters (cp_parser* parser,
19446 unsigned num_templates,
19447 location_t location,
19448 cp_declarator *declarator)
19450 /* If there are the same number of template classes and parameter
19451 lists, that's OK. */
19452 if (parser->num_template_parameter_lists == num_templates)
19454 /* If there are more, but only one more, then we are referring to a
19455 member template. That's OK too. */
19456 if (parser->num_template_parameter_lists == num_templates + 1)
19458 /* If there are more template classes than parameter lists, we have
19461 template <class T> void S<T>::R<T>::f (); */
19462 if (parser->num_template_parameter_lists < num_templates)
19464 if (declarator && !current_function_decl)
19465 error_at (location, "specializing member %<%T::%E%> "
19466 "requires %<template<>%> syntax",
19467 declarator->u.id.qualifying_scope,
19468 declarator->u.id.unqualified_name);
19469 else if (declarator)
19470 error_at (location, "invalid declaration of %<%T::%E%>",
19471 declarator->u.id.qualifying_scope,
19472 declarator->u.id.unqualified_name);
19474 error_at (location, "too few template-parameter-lists");
19477 /* Otherwise, there are too many template parameter lists. We have
19480 template <class T> template <class U> void S::f(); */
19481 error_at (location, "too many template-parameter-lists");
19485 /* Parse an optional `::' token indicating that the following name is
19486 from the global namespace. If so, PARSER->SCOPE is set to the
19487 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19488 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19489 Returns the new value of PARSER->SCOPE, if the `::' token is
19490 present, and NULL_TREE otherwise. */
19493 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19497 /* Peek at the next token. */
19498 token = cp_lexer_peek_token (parser->lexer);
19499 /* If we're looking at a `::' token then we're starting from the
19500 global namespace, not our current location. */
19501 if (token->type == CPP_SCOPE)
19503 /* Consume the `::' token. */
19504 cp_lexer_consume_token (parser->lexer);
19505 /* Set the SCOPE so that we know where to start the lookup. */
19506 parser->scope = global_namespace;
19507 parser->qualifying_scope = global_namespace;
19508 parser->object_scope = NULL_TREE;
19510 return parser->scope;
19512 else if (!current_scope_valid_p)
19514 parser->scope = NULL_TREE;
19515 parser->qualifying_scope = NULL_TREE;
19516 parser->object_scope = NULL_TREE;
19522 /* Returns TRUE if the upcoming token sequence is the start of a
19523 constructor declarator. If FRIEND_P is true, the declarator is
19524 preceded by the `friend' specifier. */
19527 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19529 bool constructor_p;
19530 tree nested_name_specifier;
19531 cp_token *next_token;
19533 /* The common case is that this is not a constructor declarator, so
19534 try to avoid doing lots of work if at all possible. It's not
19535 valid declare a constructor at function scope. */
19536 if (parser->in_function_body)
19538 /* And only certain tokens can begin a constructor declarator. */
19539 next_token = cp_lexer_peek_token (parser->lexer);
19540 if (next_token->type != CPP_NAME
19541 && next_token->type != CPP_SCOPE
19542 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19543 && next_token->type != CPP_TEMPLATE_ID)
19546 /* Parse tentatively; we are going to roll back all of the tokens
19548 cp_parser_parse_tentatively (parser);
19549 /* Assume that we are looking at a constructor declarator. */
19550 constructor_p = true;
19552 /* Look for the optional `::' operator. */
19553 cp_parser_global_scope_opt (parser,
19554 /*current_scope_valid_p=*/false);
19555 /* Look for the nested-name-specifier. */
19556 nested_name_specifier
19557 = (cp_parser_nested_name_specifier_opt (parser,
19558 /*typename_keyword_p=*/false,
19559 /*check_dependency_p=*/false,
19561 /*is_declaration=*/false));
19562 /* Outside of a class-specifier, there must be a
19563 nested-name-specifier. */
19564 if (!nested_name_specifier &&
19565 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19567 constructor_p = false;
19568 else if (nested_name_specifier == error_mark_node)
19569 constructor_p = false;
19571 /* If we have a class scope, this is easy; DR 147 says that S::S always
19572 names the constructor, and no other qualified name could. */
19573 if (constructor_p && nested_name_specifier
19574 && TYPE_P (nested_name_specifier))
19576 tree id = cp_parser_unqualified_id (parser,
19577 /*template_keyword_p=*/false,
19578 /*check_dependency_p=*/false,
19579 /*declarator_p=*/true,
19580 /*optional_p=*/false);
19581 if (is_overloaded_fn (id))
19582 id = DECL_NAME (get_first_fn (id));
19583 if (!constructor_name_p (id, nested_name_specifier))
19584 constructor_p = false;
19586 /* If we still think that this might be a constructor-declarator,
19587 look for a class-name. */
19588 else if (constructor_p)
19592 template <typename T> struct S {
19596 we must recognize that the nested `S' names a class. */
19598 type_decl = cp_parser_class_name (parser,
19599 /*typename_keyword_p=*/false,
19600 /*template_keyword_p=*/false,
19602 /*check_dependency_p=*/false,
19603 /*class_head_p=*/false,
19604 /*is_declaration=*/false);
19605 /* If there was no class-name, then this is not a constructor. */
19606 constructor_p = !cp_parser_error_occurred (parser);
19608 /* If we're still considering a constructor, we have to see a `(',
19609 to begin the parameter-declaration-clause, followed by either a
19610 `)', an `...', or a decl-specifier. We need to check for a
19611 type-specifier to avoid being fooled into thinking that:
19615 is a constructor. (It is actually a function named `f' that
19616 takes one parameter (of type `int') and returns a value of type
19619 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19620 constructor_p = false;
19623 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19624 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19625 /* A parameter declaration begins with a decl-specifier,
19626 which is either the "attribute" keyword, a storage class
19627 specifier, or (usually) a type-specifier. */
19628 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19631 tree pushed_scope = NULL_TREE;
19632 unsigned saved_num_template_parameter_lists;
19634 /* Names appearing in the type-specifier should be looked up
19635 in the scope of the class. */
19636 if (current_class_type)
19640 type = TREE_TYPE (type_decl);
19641 if (TREE_CODE (type) == TYPENAME_TYPE)
19643 type = resolve_typename_type (type,
19644 /*only_current_p=*/false);
19645 if (TREE_CODE (type) == TYPENAME_TYPE)
19647 cp_parser_abort_tentative_parse (parser);
19651 pushed_scope = push_scope (type);
19654 /* Inside the constructor parameter list, surrounding
19655 template-parameter-lists do not apply. */
19656 saved_num_template_parameter_lists
19657 = parser->num_template_parameter_lists;
19658 parser->num_template_parameter_lists = 0;
19660 /* Look for the type-specifier. */
19661 cp_parser_type_specifier (parser,
19662 CP_PARSER_FLAGS_NONE,
19663 /*decl_specs=*/NULL,
19664 /*is_declarator=*/true,
19665 /*declares_class_or_enum=*/NULL,
19666 /*is_cv_qualifier=*/NULL);
19668 parser->num_template_parameter_lists
19669 = saved_num_template_parameter_lists;
19671 /* Leave the scope of the class. */
19673 pop_scope (pushed_scope);
19675 constructor_p = !cp_parser_error_occurred (parser);
19679 /* We did not really want to consume any tokens. */
19680 cp_parser_abort_tentative_parse (parser);
19682 return constructor_p;
19685 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19686 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19687 they must be performed once we are in the scope of the function.
19689 Returns the function defined. */
19692 cp_parser_function_definition_from_specifiers_and_declarator
19693 (cp_parser* parser,
19694 cp_decl_specifier_seq *decl_specifiers,
19696 const cp_declarator *declarator)
19701 /* Begin the function-definition. */
19702 success_p = start_function (decl_specifiers, declarator, attributes);
19704 /* The things we're about to see are not directly qualified by any
19705 template headers we've seen thus far. */
19706 reset_specialization ();
19708 /* If there were names looked up in the decl-specifier-seq that we
19709 did not check, check them now. We must wait until we are in the
19710 scope of the function to perform the checks, since the function
19711 might be a friend. */
19712 perform_deferred_access_checks ();
19716 /* Skip the entire function. */
19717 cp_parser_skip_to_end_of_block_or_statement (parser);
19718 fn = error_mark_node;
19720 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19722 /* Seen already, skip it. An error message has already been output. */
19723 cp_parser_skip_to_end_of_block_or_statement (parser);
19724 fn = current_function_decl;
19725 current_function_decl = NULL_TREE;
19726 /* If this is a function from a class, pop the nested class. */
19727 if (current_class_name)
19728 pop_nested_class ();
19731 fn = cp_parser_function_definition_after_declarator (parser,
19732 /*inline_p=*/false);
19737 /* Parse the part of a function-definition that follows the
19738 declarator. INLINE_P is TRUE iff this function is an inline
19739 function defined within a class-specifier.
19741 Returns the function defined. */
19744 cp_parser_function_definition_after_declarator (cp_parser* parser,
19748 bool ctor_initializer_p = false;
19749 bool saved_in_unbraced_linkage_specification_p;
19750 bool saved_in_function_body;
19751 unsigned saved_num_template_parameter_lists;
19754 saved_in_function_body = parser->in_function_body;
19755 parser->in_function_body = true;
19756 /* If the next token is `return', then the code may be trying to
19757 make use of the "named return value" extension that G++ used to
19759 token = cp_lexer_peek_token (parser->lexer);
19760 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19762 /* Consume the `return' keyword. */
19763 cp_lexer_consume_token (parser->lexer);
19764 /* Look for the identifier that indicates what value is to be
19766 cp_parser_identifier (parser);
19767 /* Issue an error message. */
19768 error_at (token->location,
19769 "named return values are no longer supported");
19770 /* Skip tokens until we reach the start of the function body. */
19773 cp_token *token = cp_lexer_peek_token (parser->lexer);
19774 if (token->type == CPP_OPEN_BRACE
19775 || token->type == CPP_EOF
19776 || token->type == CPP_PRAGMA_EOL)
19778 cp_lexer_consume_token (parser->lexer);
19781 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19782 anything declared inside `f'. */
19783 saved_in_unbraced_linkage_specification_p
19784 = parser->in_unbraced_linkage_specification_p;
19785 parser->in_unbraced_linkage_specification_p = false;
19786 /* Inside the function, surrounding template-parameter-lists do not
19788 saved_num_template_parameter_lists
19789 = parser->num_template_parameter_lists;
19790 parser->num_template_parameter_lists = 0;
19792 start_lambda_scope (current_function_decl);
19794 /* If the next token is `try', then we are looking at a
19795 function-try-block. */
19796 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19797 ctor_initializer_p = cp_parser_function_try_block (parser);
19798 /* A function-try-block includes the function-body, so we only do
19799 this next part if we're not processing a function-try-block. */
19802 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19804 finish_lambda_scope ();
19806 /* Finish the function. */
19807 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19808 (inline_p ? 2 : 0));
19809 /* Generate code for it, if necessary. */
19810 expand_or_defer_fn (fn);
19811 /* Restore the saved values. */
19812 parser->in_unbraced_linkage_specification_p
19813 = saved_in_unbraced_linkage_specification_p;
19814 parser->num_template_parameter_lists
19815 = saved_num_template_parameter_lists;
19816 parser->in_function_body = saved_in_function_body;
19821 /* Parse a template-declaration, assuming that the `export' (and
19822 `extern') keywords, if present, has already been scanned. MEMBER_P
19823 is as for cp_parser_template_declaration. */
19826 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19828 tree decl = NULL_TREE;
19829 VEC (deferred_access_check,gc) *checks;
19830 tree parameter_list;
19831 bool friend_p = false;
19832 bool need_lang_pop;
19835 /* Look for the `template' keyword. */
19836 token = cp_lexer_peek_token (parser->lexer);
19837 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19841 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19843 if (at_class_scope_p () && current_function_decl)
19845 /* 14.5.2.2 [temp.mem]
19847 A local class shall not have member templates. */
19848 error_at (token->location,
19849 "invalid declaration of member template in local class");
19850 cp_parser_skip_to_end_of_block_or_statement (parser);
19855 A template ... shall not have C linkage. */
19856 if (current_lang_name == lang_name_c)
19858 error_at (token->location, "template with C linkage");
19859 /* Give it C++ linkage to avoid confusing other parts of the
19861 push_lang_context (lang_name_cplusplus);
19862 need_lang_pop = true;
19865 need_lang_pop = false;
19867 /* We cannot perform access checks on the template parameter
19868 declarations until we know what is being declared, just as we
19869 cannot check the decl-specifier list. */
19870 push_deferring_access_checks (dk_deferred);
19872 /* If the next token is `>', then we have an invalid
19873 specialization. Rather than complain about an invalid template
19874 parameter, issue an error message here. */
19875 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19877 cp_parser_error (parser, "invalid explicit specialization");
19878 begin_specialization ();
19879 parameter_list = NULL_TREE;
19882 /* Parse the template parameters. */
19883 parameter_list = cp_parser_template_parameter_list (parser);
19885 /* Get the deferred access checks from the parameter list. These
19886 will be checked once we know what is being declared, as for a
19887 member template the checks must be performed in the scope of the
19888 class containing the member. */
19889 checks = get_deferred_access_checks ();
19891 /* Look for the `>'. */
19892 cp_parser_skip_to_end_of_template_parameter_list (parser);
19893 /* We just processed one more parameter list. */
19894 ++parser->num_template_parameter_lists;
19895 /* If the next token is `template', there are more template
19897 if (cp_lexer_next_token_is_keyword (parser->lexer,
19899 cp_parser_template_declaration_after_export (parser, member_p);
19902 /* There are no access checks when parsing a template, as we do not
19903 know if a specialization will be a friend. */
19904 push_deferring_access_checks (dk_no_check);
19905 token = cp_lexer_peek_token (parser->lexer);
19906 decl = cp_parser_single_declaration (parser,
19909 /*explicit_specialization_p=*/false,
19911 pop_deferring_access_checks ();
19913 /* If this is a member template declaration, let the front
19915 if (member_p && !friend_p && decl)
19917 if (TREE_CODE (decl) == TYPE_DECL)
19918 cp_parser_check_access_in_redeclaration (decl, token->location);
19920 decl = finish_member_template_decl (decl);
19922 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19923 make_friend_class (current_class_type, TREE_TYPE (decl),
19924 /*complain=*/true);
19926 /* We are done with the current parameter list. */
19927 --parser->num_template_parameter_lists;
19929 pop_deferring_access_checks ();
19932 finish_template_decl (parameter_list);
19934 /* Register member declarations. */
19935 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19936 finish_member_declaration (decl);
19937 /* For the erroneous case of a template with C linkage, we pushed an
19938 implicit C++ linkage scope; exit that scope now. */
19940 pop_lang_context ();
19941 /* If DECL is a function template, we must return to parse it later.
19942 (Even though there is no definition, there might be default
19943 arguments that need handling.) */
19944 if (member_p && decl
19945 && (TREE_CODE (decl) == FUNCTION_DECL
19946 || DECL_FUNCTION_TEMPLATE_P (decl)))
19947 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19950 /* Perform the deferred access checks from a template-parameter-list.
19951 CHECKS is a TREE_LIST of access checks, as returned by
19952 get_deferred_access_checks. */
19955 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19957 ++processing_template_parmlist;
19958 perform_access_checks (checks);
19959 --processing_template_parmlist;
19962 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19963 `function-definition' sequence. MEMBER_P is true, this declaration
19964 appears in a class scope.
19966 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19967 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19970 cp_parser_single_declaration (cp_parser* parser,
19971 VEC (deferred_access_check,gc)* checks,
19973 bool explicit_specialization_p,
19976 int declares_class_or_enum;
19977 tree decl = NULL_TREE;
19978 cp_decl_specifier_seq decl_specifiers;
19979 bool function_definition_p = false;
19980 cp_token *decl_spec_token_start;
19982 /* This function is only used when processing a template
19984 gcc_assert (innermost_scope_kind () == sk_template_parms
19985 || innermost_scope_kind () == sk_template_spec);
19987 /* Defer access checks until we know what is being declared. */
19988 push_deferring_access_checks (dk_deferred);
19990 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19992 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19993 cp_parser_decl_specifier_seq (parser,
19994 CP_PARSER_FLAGS_OPTIONAL,
19996 &declares_class_or_enum);
19998 *friend_p = cp_parser_friend_p (&decl_specifiers);
20000 /* There are no template typedefs. */
20001 if (decl_specifiers.specs[(int) ds_typedef])
20003 error_at (decl_spec_token_start->location,
20004 "template declaration of %<typedef%>");
20005 decl = error_mark_node;
20008 /* Gather up the access checks that occurred the
20009 decl-specifier-seq. */
20010 stop_deferring_access_checks ();
20012 /* Check for the declaration of a template class. */
20013 if (declares_class_or_enum)
20015 if (cp_parser_declares_only_class_p (parser))
20017 decl = shadow_tag (&decl_specifiers);
20022 friend template <typename T> struct A<T>::B;
20025 A<T>::B will be represented by a TYPENAME_TYPE, and
20026 therefore not recognized by shadow_tag. */
20027 if (friend_p && *friend_p
20029 && decl_specifiers.type
20030 && TYPE_P (decl_specifiers.type))
20031 decl = decl_specifiers.type;
20033 if (decl && decl != error_mark_node)
20034 decl = TYPE_NAME (decl);
20036 decl = error_mark_node;
20038 /* Perform access checks for template parameters. */
20039 cp_parser_perform_template_parameter_access_checks (checks);
20043 /* Complain about missing 'typename' or other invalid type names. */
20044 if (!decl_specifiers.any_type_specifiers_p)
20045 cp_parser_parse_and_diagnose_invalid_type_name (parser);
20047 /* If it's not a template class, try for a template function. If
20048 the next token is a `;', then this declaration does not declare
20049 anything. But, if there were errors in the decl-specifiers, then
20050 the error might well have come from an attempted class-specifier.
20051 In that case, there's no need to warn about a missing declarator. */
20053 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20054 || decl_specifiers.type != error_mark_node))
20056 decl = cp_parser_init_declarator (parser,
20059 /*function_definition_allowed_p=*/true,
20061 declares_class_or_enum,
20062 &function_definition_p);
20064 /* 7.1.1-1 [dcl.stc]
20066 A storage-class-specifier shall not be specified in an explicit
20067 specialization... */
20069 && explicit_specialization_p
20070 && decl_specifiers.storage_class != sc_none)
20072 error_at (decl_spec_token_start->location,
20073 "explicit template specialization cannot have a storage class");
20074 decl = error_mark_node;
20078 pop_deferring_access_checks ();
20080 /* Clear any current qualification; whatever comes next is the start
20081 of something new. */
20082 parser->scope = NULL_TREE;
20083 parser->qualifying_scope = NULL_TREE;
20084 parser->object_scope = NULL_TREE;
20085 /* Look for a trailing `;' after the declaration. */
20086 if (!function_definition_p
20087 && (decl == error_mark_node
20088 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20089 cp_parser_skip_to_end_of_block_or_statement (parser);
20094 /* Parse a cast-expression that is not the operand of a unary "&". */
20097 cp_parser_simple_cast_expression (cp_parser *parser)
20099 return cp_parser_cast_expression (parser, /*address_p=*/false,
20100 /*cast_p=*/false, NULL);
20103 /* Parse a functional cast to TYPE. Returns an expression
20104 representing the cast. */
20107 cp_parser_functional_cast (cp_parser* parser, tree type)
20110 tree expression_list;
20114 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20116 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20117 expression_list = cp_parser_braced_list (parser, &nonconst_p);
20118 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20119 if (TREE_CODE (type) == TYPE_DECL)
20120 type = TREE_TYPE (type);
20121 return finish_compound_literal (type, expression_list);
20125 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20127 /*allow_expansion_p=*/true,
20128 /*non_constant_p=*/NULL);
20130 expression_list = error_mark_node;
20133 expression_list = build_tree_list_vec (vec);
20134 release_tree_vector (vec);
20137 cast = build_functional_cast (type, expression_list,
20138 tf_warning_or_error);
20139 /* [expr.const]/1: In an integral constant expression "only type
20140 conversions to integral or enumeration type can be used". */
20141 if (TREE_CODE (type) == TYPE_DECL)
20142 type = TREE_TYPE (type);
20143 if (cast != error_mark_node
20144 && !cast_valid_in_integral_constant_expression_p (type)
20145 && cp_parser_non_integral_constant_expression (parser,
20147 return error_mark_node;
20151 /* Save the tokens that make up the body of a member function defined
20152 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
20153 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
20154 specifiers applied to the declaration. Returns the FUNCTION_DECL
20155 for the member function. */
20158 cp_parser_save_member_function_body (cp_parser* parser,
20159 cp_decl_specifier_seq *decl_specifiers,
20160 cp_declarator *declarator,
20167 /* Create the FUNCTION_DECL. */
20168 fn = grokmethod (decl_specifiers, declarator, attributes);
20169 /* If something went badly wrong, bail out now. */
20170 if (fn == error_mark_node)
20172 /* If there's a function-body, skip it. */
20173 if (cp_parser_token_starts_function_definition_p
20174 (cp_lexer_peek_token (parser->lexer)))
20175 cp_parser_skip_to_end_of_block_or_statement (parser);
20176 return error_mark_node;
20179 /* Remember it, if there default args to post process. */
20180 cp_parser_save_default_args (parser, fn);
20182 /* Save away the tokens that make up the body of the
20184 first = parser->lexer->next_token;
20185 /* We can have braced-init-list mem-initializers before the fn body. */
20186 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20188 cp_lexer_consume_token (parser->lexer);
20189 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20190 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20192 /* cache_group will stop after an un-nested { } pair, too. */
20193 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20196 /* variadic mem-inits have ... after the ')'. */
20197 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20198 cp_lexer_consume_token (parser->lexer);
20201 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20202 /* Handle function try blocks. */
20203 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20204 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20205 last = parser->lexer->next_token;
20207 /* Save away the inline definition; we will process it when the
20208 class is complete. */
20209 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20210 DECL_PENDING_INLINE_P (fn) = 1;
20212 /* We need to know that this was defined in the class, so that
20213 friend templates are handled correctly. */
20214 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20216 /* Add FN to the queue of functions to be parsed later. */
20217 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20222 /* Parse a template-argument-list, as well as the trailing ">" (but
20223 not the opening ">"). See cp_parser_template_argument_list for the
20227 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20231 tree saved_qualifying_scope;
20232 tree saved_object_scope;
20233 bool saved_greater_than_is_operator_p;
20234 int saved_unevaluated_operand;
20235 int saved_inhibit_evaluation_warnings;
20239 When parsing a template-id, the first non-nested `>' is taken as
20240 the end of the template-argument-list rather than a greater-than
20242 saved_greater_than_is_operator_p
20243 = parser->greater_than_is_operator_p;
20244 parser->greater_than_is_operator_p = false;
20245 /* Parsing the argument list may modify SCOPE, so we save it
20247 saved_scope = parser->scope;
20248 saved_qualifying_scope = parser->qualifying_scope;
20249 saved_object_scope = parser->object_scope;
20250 /* We need to evaluate the template arguments, even though this
20251 template-id may be nested within a "sizeof". */
20252 saved_unevaluated_operand = cp_unevaluated_operand;
20253 cp_unevaluated_operand = 0;
20254 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20255 c_inhibit_evaluation_warnings = 0;
20256 /* Parse the template-argument-list itself. */
20257 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20258 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20259 arguments = NULL_TREE;
20261 arguments = cp_parser_template_argument_list (parser);
20262 /* Look for the `>' that ends the template-argument-list. If we find
20263 a '>>' instead, it's probably just a typo. */
20264 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20266 if (cxx_dialect != cxx98)
20268 /* In C++0x, a `>>' in a template argument list or cast
20269 expression is considered to be two separate `>'
20270 tokens. So, change the current token to a `>', but don't
20271 consume it: it will be consumed later when the outer
20272 template argument list (or cast expression) is parsed.
20273 Note that this replacement of `>' for `>>' is necessary
20274 even if we are parsing tentatively: in the tentative
20275 case, after calling
20276 cp_parser_enclosed_template_argument_list we will always
20277 throw away all of the template arguments and the first
20278 closing `>', either because the template argument list
20279 was erroneous or because we are replacing those tokens
20280 with a CPP_TEMPLATE_ID token. The second `>' (which will
20281 not have been thrown away) is needed either to close an
20282 outer template argument list or to complete a new-style
20284 cp_token *token = cp_lexer_peek_token (parser->lexer);
20285 token->type = CPP_GREATER;
20287 else if (!saved_greater_than_is_operator_p)
20289 /* If we're in a nested template argument list, the '>>' has
20290 to be a typo for '> >'. We emit the error message, but we
20291 continue parsing and we push a '>' as next token, so that
20292 the argument list will be parsed correctly. Note that the
20293 global source location is still on the token before the
20294 '>>', so we need to say explicitly where we want it. */
20295 cp_token *token = cp_lexer_peek_token (parser->lexer);
20296 error_at (token->location, "%<>>%> should be %<> >%> "
20297 "within a nested template argument list");
20299 token->type = CPP_GREATER;
20303 /* If this is not a nested template argument list, the '>>'
20304 is a typo for '>'. Emit an error message and continue.
20305 Same deal about the token location, but here we can get it
20306 right by consuming the '>>' before issuing the diagnostic. */
20307 cp_token *token = cp_lexer_consume_token (parser->lexer);
20308 error_at (token->location,
20309 "spurious %<>>%>, use %<>%> to terminate "
20310 "a template argument list");
20314 cp_parser_skip_to_end_of_template_parameter_list (parser);
20315 /* The `>' token might be a greater-than operator again now. */
20316 parser->greater_than_is_operator_p
20317 = saved_greater_than_is_operator_p;
20318 /* Restore the SAVED_SCOPE. */
20319 parser->scope = saved_scope;
20320 parser->qualifying_scope = saved_qualifying_scope;
20321 parser->object_scope = saved_object_scope;
20322 cp_unevaluated_operand = saved_unevaluated_operand;
20323 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20328 /* MEMBER_FUNCTION is a member function, or a friend. If default
20329 arguments, or the body of the function have not yet been parsed,
20333 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20335 /* If this member is a template, get the underlying
20337 if (DECL_FUNCTION_TEMPLATE_P (member_function))
20338 member_function = DECL_TEMPLATE_RESULT (member_function);
20340 /* There should not be any class definitions in progress at this
20341 point; the bodies of members are only parsed outside of all class
20343 gcc_assert (parser->num_classes_being_defined == 0);
20344 /* While we're parsing the member functions we might encounter more
20345 classes. We want to handle them right away, but we don't want
20346 them getting mixed up with functions that are currently in the
20348 push_unparsed_function_queues (parser);
20350 /* Make sure that any template parameters are in scope. */
20351 maybe_begin_member_template_processing (member_function);
20353 /* If the body of the function has not yet been parsed, parse it
20355 if (DECL_PENDING_INLINE_P (member_function))
20357 tree function_scope;
20358 cp_token_cache *tokens;
20360 /* The function is no longer pending; we are processing it. */
20361 tokens = DECL_PENDING_INLINE_INFO (member_function);
20362 DECL_PENDING_INLINE_INFO (member_function) = NULL;
20363 DECL_PENDING_INLINE_P (member_function) = 0;
20365 /* If this is a local class, enter the scope of the containing
20367 function_scope = current_function_decl;
20368 if (function_scope)
20369 push_function_context ();
20371 /* Push the body of the function onto the lexer stack. */
20372 cp_parser_push_lexer_for_tokens (parser, tokens);
20374 /* Let the front end know that we going to be defining this
20376 start_preparsed_function (member_function, NULL_TREE,
20377 SF_PRE_PARSED | SF_INCLASS_INLINE);
20379 /* Don't do access checking if it is a templated function. */
20380 if (processing_template_decl)
20381 push_deferring_access_checks (dk_no_check);
20383 /* Now, parse the body of the function. */
20384 cp_parser_function_definition_after_declarator (parser,
20385 /*inline_p=*/true);
20387 if (processing_template_decl)
20388 pop_deferring_access_checks ();
20390 /* Leave the scope of the containing function. */
20391 if (function_scope)
20392 pop_function_context ();
20393 cp_parser_pop_lexer (parser);
20396 /* Remove any template parameters from the symbol table. */
20397 maybe_end_member_template_processing ();
20399 /* Restore the queue. */
20400 pop_unparsed_function_queues (parser);
20403 /* If DECL contains any default args, remember it on the unparsed
20404 functions queue. */
20407 cp_parser_save_default_args (cp_parser* parser, tree decl)
20411 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20413 probe = TREE_CHAIN (probe))
20414 if (TREE_PURPOSE (probe))
20416 cp_default_arg_entry *entry
20417 = VEC_safe_push (cp_default_arg_entry, gc,
20418 unparsed_funs_with_default_args, NULL);
20419 entry->class_type = current_class_type;
20420 entry->decl = decl;
20425 /* FN is a FUNCTION_DECL which may contains a parameter with an
20426 unparsed DEFAULT_ARG. Parse the default args now. This function
20427 assumes that the current scope is the scope in which the default
20428 argument should be processed. */
20431 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20433 bool saved_local_variables_forbidden_p;
20434 tree parm, parmdecl;
20436 /* While we're parsing the default args, we might (due to the
20437 statement expression extension) encounter more classes. We want
20438 to handle them right away, but we don't want them getting mixed
20439 up with default args that are currently in the queue. */
20440 push_unparsed_function_queues (parser);
20442 /* Local variable names (and the `this' keyword) may not appear
20443 in a default argument. */
20444 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20445 parser->local_variables_forbidden_p = true;
20447 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20448 parmdecl = DECL_ARGUMENTS (fn);
20449 parm && parm != void_list_node;
20450 parm = TREE_CHAIN (parm),
20451 parmdecl = DECL_CHAIN (parmdecl))
20453 cp_token_cache *tokens;
20454 tree default_arg = TREE_PURPOSE (parm);
20456 VEC(tree,gc) *insts;
20463 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20464 /* This can happen for a friend declaration for a function
20465 already declared with default arguments. */
20468 /* Push the saved tokens for the default argument onto the parser's
20470 tokens = DEFARG_TOKENS (default_arg);
20471 cp_parser_push_lexer_for_tokens (parser, tokens);
20473 start_lambda_scope (parmdecl);
20475 /* Parse the assignment-expression. */
20476 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20477 if (parsed_arg == error_mark_node)
20479 cp_parser_pop_lexer (parser);
20483 if (!processing_template_decl)
20484 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20486 TREE_PURPOSE (parm) = parsed_arg;
20488 /* Update any instantiations we've already created. */
20489 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20490 VEC_iterate (tree, insts, ix, copy); ix++)
20491 TREE_PURPOSE (copy) = parsed_arg;
20493 finish_lambda_scope ();
20495 /* If the token stream has not been completely used up, then
20496 there was extra junk after the end of the default
20498 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20499 cp_parser_error (parser, "expected %<,%>");
20501 /* Revert to the main lexer. */
20502 cp_parser_pop_lexer (parser);
20505 /* Make sure no default arg is missing. */
20506 check_default_args (fn);
20508 /* Restore the state of local_variables_forbidden_p. */
20509 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20511 /* Restore the queue. */
20512 pop_unparsed_function_queues (parser);
20515 /* Parse the operand of `sizeof' (or a similar operator). Returns
20516 either a TYPE or an expression, depending on the form of the
20517 input. The KEYWORD indicates which kind of expression we have
20521 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20523 tree expr = NULL_TREE;
20524 const char *saved_message;
20526 bool saved_integral_constant_expression_p;
20527 bool saved_non_integral_constant_expression_p;
20528 bool pack_expansion_p = false;
20530 /* Types cannot be defined in a `sizeof' expression. Save away the
20532 saved_message = parser->type_definition_forbidden_message;
20533 /* And create the new one. */
20534 tmp = concat ("types may not be defined in %<",
20535 IDENTIFIER_POINTER (ridpointers[keyword]),
20536 "%> expressions", NULL);
20537 parser->type_definition_forbidden_message = tmp;
20539 /* The restrictions on constant-expressions do not apply inside
20540 sizeof expressions. */
20541 saved_integral_constant_expression_p
20542 = parser->integral_constant_expression_p;
20543 saved_non_integral_constant_expression_p
20544 = parser->non_integral_constant_expression_p;
20545 parser->integral_constant_expression_p = false;
20547 /* If it's a `...', then we are computing the length of a parameter
20549 if (keyword == RID_SIZEOF
20550 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20552 /* Consume the `...'. */
20553 cp_lexer_consume_token (parser->lexer);
20554 maybe_warn_variadic_templates ();
20556 /* Note that this is an expansion. */
20557 pack_expansion_p = true;
20560 /* Do not actually evaluate the expression. */
20561 ++cp_unevaluated_operand;
20562 ++c_inhibit_evaluation_warnings;
20563 /* If it's a `(', then we might be looking at the type-id
20565 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20568 bool saved_in_type_id_in_expr_p;
20570 /* We can't be sure yet whether we're looking at a type-id or an
20572 cp_parser_parse_tentatively (parser);
20573 /* Consume the `('. */
20574 cp_lexer_consume_token (parser->lexer);
20575 /* Parse the type-id. */
20576 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20577 parser->in_type_id_in_expr_p = true;
20578 type = cp_parser_type_id (parser);
20579 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20580 /* Now, look for the trailing `)'. */
20581 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20582 /* If all went well, then we're done. */
20583 if (cp_parser_parse_definitely (parser))
20585 cp_decl_specifier_seq decl_specs;
20587 /* Build a trivial decl-specifier-seq. */
20588 clear_decl_specs (&decl_specs);
20589 decl_specs.type = type;
20591 /* Call grokdeclarator to figure out what type this is. */
20592 expr = grokdeclarator (NULL,
20596 /*attrlist=*/NULL);
20600 /* If the type-id production did not work out, then we must be
20601 looking at the unary-expression production. */
20603 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20604 /*cast_p=*/false, NULL);
20606 if (pack_expansion_p)
20607 /* Build a pack expansion. */
20608 expr = make_pack_expansion (expr);
20610 /* Go back to evaluating expressions. */
20611 --cp_unevaluated_operand;
20612 --c_inhibit_evaluation_warnings;
20614 /* Free the message we created. */
20616 /* And restore the old one. */
20617 parser->type_definition_forbidden_message = saved_message;
20618 parser->integral_constant_expression_p
20619 = saved_integral_constant_expression_p;
20620 parser->non_integral_constant_expression_p
20621 = saved_non_integral_constant_expression_p;
20626 /* If the current declaration has no declarator, return true. */
20629 cp_parser_declares_only_class_p (cp_parser *parser)
20631 /* If the next token is a `;' or a `,' then there is no
20633 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20634 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20637 /* Update the DECL_SPECS to reflect the storage class indicated by
20641 cp_parser_set_storage_class (cp_parser *parser,
20642 cp_decl_specifier_seq *decl_specs,
20644 location_t location)
20646 cp_storage_class storage_class;
20648 if (parser->in_unbraced_linkage_specification_p)
20650 error_at (location, "invalid use of %qD in linkage specification",
20651 ridpointers[keyword]);
20654 else if (decl_specs->storage_class != sc_none)
20656 decl_specs->conflicting_specifiers_p = true;
20660 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20661 && decl_specs->specs[(int) ds_thread])
20663 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20664 decl_specs->specs[(int) ds_thread] = 0;
20670 storage_class = sc_auto;
20673 storage_class = sc_register;
20676 storage_class = sc_static;
20679 storage_class = sc_extern;
20682 storage_class = sc_mutable;
20685 gcc_unreachable ();
20687 decl_specs->storage_class = storage_class;
20689 /* A storage class specifier cannot be applied alongside a typedef
20690 specifier. If there is a typedef specifier present then set
20691 conflicting_specifiers_p which will trigger an error later
20692 on in grokdeclarator. */
20693 if (decl_specs->specs[(int)ds_typedef])
20694 decl_specs->conflicting_specifiers_p = true;
20697 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20698 is true, the type is a user-defined type; otherwise it is a
20699 built-in type specified by a keyword. */
20702 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20704 location_t location,
20705 bool user_defined_p)
20707 decl_specs->any_specifiers_p = true;
20709 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20710 (with, for example, in "typedef int wchar_t;") we remember that
20711 this is what happened. In system headers, we ignore these
20712 declarations so that G++ can work with system headers that are not
20714 if (decl_specs->specs[(int) ds_typedef]
20716 && (type_spec == boolean_type_node
20717 || type_spec == char16_type_node
20718 || type_spec == char32_type_node
20719 || type_spec == wchar_type_node)
20720 && (decl_specs->type
20721 || decl_specs->specs[(int) ds_long]
20722 || decl_specs->specs[(int) ds_short]
20723 || decl_specs->specs[(int) ds_unsigned]
20724 || decl_specs->specs[(int) ds_signed]))
20726 decl_specs->redefined_builtin_type = type_spec;
20727 if (!decl_specs->type)
20729 decl_specs->type = type_spec;
20730 decl_specs->user_defined_type_p = false;
20731 decl_specs->type_location = location;
20734 else if (decl_specs->type)
20735 decl_specs->multiple_types_p = true;
20738 decl_specs->type = type_spec;
20739 decl_specs->user_defined_type_p = user_defined_p;
20740 decl_specs->redefined_builtin_type = NULL_TREE;
20741 decl_specs->type_location = location;
20745 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20746 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20749 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20751 return decl_specifiers->specs[(int) ds_friend] != 0;
20754 /* Issue an error message indicating that TOKEN_DESC was expected.
20755 If KEYWORD is true, it indicated this function is called by
20756 cp_parser_require_keword and the required token can only be
20757 a indicated keyword. */
20760 cp_parser_required_error (cp_parser *parser,
20761 required_token token_desc,
20764 switch (token_desc)
20767 cp_parser_error (parser, "expected %<new%>");
20770 cp_parser_error (parser, "expected %<delete%>");
20773 cp_parser_error (parser, "expected %<return%>");
20776 cp_parser_error (parser, "expected %<while%>");
20779 cp_parser_error (parser, "expected %<extern%>");
20781 case RT_STATIC_ASSERT:
20782 cp_parser_error (parser, "expected %<static_assert%>");
20785 cp_parser_error (parser, "expected %<decltype%>");
20788 cp_parser_error (parser, "expected %<operator%>");
20791 cp_parser_error (parser, "expected %<class%>");
20794 cp_parser_error (parser, "expected %<template%>");
20797 cp_parser_error (parser, "expected %<namespace%>");
20800 cp_parser_error (parser, "expected %<using%>");
20803 cp_parser_error (parser, "expected %<asm%>");
20806 cp_parser_error (parser, "expected %<try%>");
20809 cp_parser_error (parser, "expected %<catch%>");
20812 cp_parser_error (parser, "expected %<throw%>");
20815 cp_parser_error (parser, "expected %<__label__%>");
20818 cp_parser_error (parser, "expected %<@try%>");
20820 case RT_AT_SYNCHRONIZED:
20821 cp_parser_error (parser, "expected %<@synchronized%>");
20824 cp_parser_error (parser, "expected %<@throw%>");
20831 switch (token_desc)
20834 cp_parser_error (parser, "expected %<;%>");
20836 case RT_OPEN_PAREN:
20837 cp_parser_error (parser, "expected %<(%>");
20839 case RT_CLOSE_BRACE:
20840 cp_parser_error (parser, "expected %<}%>");
20842 case RT_OPEN_BRACE:
20843 cp_parser_error (parser, "expected %<{%>");
20845 case RT_CLOSE_SQUARE:
20846 cp_parser_error (parser, "expected %<]%>");
20848 case RT_OPEN_SQUARE:
20849 cp_parser_error (parser, "expected %<[%>");
20852 cp_parser_error (parser, "expected %<,%>");
20855 cp_parser_error (parser, "expected %<::%>");
20858 cp_parser_error (parser, "expected %<<%>");
20861 cp_parser_error (parser, "expected %<>%>");
20864 cp_parser_error (parser, "expected %<=%>");
20867 cp_parser_error (parser, "expected %<...%>");
20870 cp_parser_error (parser, "expected %<*%>");
20873 cp_parser_error (parser, "expected %<~%>");
20876 cp_parser_error (parser, "expected %<:%>");
20878 case RT_COLON_SCOPE:
20879 cp_parser_error (parser, "expected %<:%> or %<::%>");
20881 case RT_CLOSE_PAREN:
20882 cp_parser_error (parser, "expected %<)%>");
20884 case RT_COMMA_CLOSE_PAREN:
20885 cp_parser_error (parser, "expected %<,%> or %<)%>");
20887 case RT_PRAGMA_EOL:
20888 cp_parser_error (parser, "expected end of line");
20891 cp_parser_error (parser, "expected identifier");
20894 cp_parser_error (parser, "expected selection-statement");
20896 case RT_INTERATION:
20897 cp_parser_error (parser, "expected iteration-statement");
20900 cp_parser_error (parser, "expected jump-statement");
20903 cp_parser_error (parser, "expected class-key");
20905 case RT_CLASS_TYPENAME_TEMPLATE:
20906 cp_parser_error (parser,
20907 "expected %<class%>, %<typename%>, or %<template%>");
20910 gcc_unreachable ();
20914 gcc_unreachable ();
20919 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20920 issue an error message indicating that TOKEN_DESC was expected.
20922 Returns the token consumed, if the token had the appropriate type.
20923 Otherwise, returns NULL. */
20926 cp_parser_require (cp_parser* parser,
20927 enum cpp_ttype type,
20928 required_token token_desc)
20930 if (cp_lexer_next_token_is (parser->lexer, type))
20931 return cp_lexer_consume_token (parser->lexer);
20934 /* Output the MESSAGE -- unless we're parsing tentatively. */
20935 if (!cp_parser_simulate_error (parser))
20936 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20941 /* An error message is produced if the next token is not '>'.
20942 All further tokens are skipped until the desired token is
20943 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20946 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20948 /* Current level of '< ... >'. */
20949 unsigned level = 0;
20950 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20951 unsigned nesting_depth = 0;
20953 /* Are we ready, yet? If not, issue error message. */
20954 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20957 /* Skip tokens until the desired token is found. */
20960 /* Peek at the next token. */
20961 switch (cp_lexer_peek_token (parser->lexer)->type)
20964 if (!nesting_depth)
20969 if (cxx_dialect == cxx98)
20970 /* C++0x views the `>>' operator as two `>' tokens, but
20973 else if (!nesting_depth && level-- == 0)
20975 /* We've hit a `>>' where the first `>' closes the
20976 template argument list, and the second `>' is
20977 spurious. Just consume the `>>' and stop; we've
20978 already produced at least one error. */
20979 cp_lexer_consume_token (parser->lexer);
20982 /* Fall through for C++0x, so we handle the second `>' in
20986 if (!nesting_depth && level-- == 0)
20988 /* We've reached the token we want, consume it and stop. */
20989 cp_lexer_consume_token (parser->lexer);
20994 case CPP_OPEN_PAREN:
20995 case CPP_OPEN_SQUARE:
20999 case CPP_CLOSE_PAREN:
21000 case CPP_CLOSE_SQUARE:
21001 if (nesting_depth-- == 0)
21006 case CPP_PRAGMA_EOL:
21007 case CPP_SEMICOLON:
21008 case CPP_OPEN_BRACE:
21009 case CPP_CLOSE_BRACE:
21010 /* The '>' was probably forgotten, don't look further. */
21017 /* Consume this token. */
21018 cp_lexer_consume_token (parser->lexer);
21022 /* If the next token is the indicated keyword, consume it. Otherwise,
21023 issue an error message indicating that TOKEN_DESC was expected.
21025 Returns the token consumed, if the token had the appropriate type.
21026 Otherwise, returns NULL. */
21029 cp_parser_require_keyword (cp_parser* parser,
21031 required_token token_desc)
21033 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21035 if (token && token->keyword != keyword)
21037 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
21044 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21045 function-definition. */
21048 cp_parser_token_starts_function_definition_p (cp_token* token)
21050 return (/* An ordinary function-body begins with an `{'. */
21051 token->type == CPP_OPEN_BRACE
21052 /* A ctor-initializer begins with a `:'. */
21053 || token->type == CPP_COLON
21054 /* A function-try-block begins with `try'. */
21055 || token->keyword == RID_TRY
21056 /* The named return value extension begins with `return'. */
21057 || token->keyword == RID_RETURN);
21060 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21064 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21068 token = cp_lexer_peek_token (parser->lexer);
21069 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21072 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21073 C++0x) ending a template-argument. */
21076 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21080 token = cp_lexer_peek_token (parser->lexer);
21081 return (token->type == CPP_COMMA
21082 || token->type == CPP_GREATER
21083 || token->type == CPP_ELLIPSIS
21084 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21087 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21088 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
21091 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21096 token = cp_lexer_peek_nth_token (parser->lexer, n);
21097 if (token->type == CPP_LESS)
21099 /* Check for the sequence `<::' in the original code. It would be lexed as
21100 `[:', where `[' is a digraph, and there is no whitespace before
21102 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21105 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21106 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21112 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21113 or none_type otherwise. */
21115 static enum tag_types
21116 cp_parser_token_is_class_key (cp_token* token)
21118 switch (token->keyword)
21123 return record_type;
21132 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
21135 cp_parser_check_class_key (enum tag_types class_key, tree type)
21137 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21138 permerror (input_location, "%qs tag used in naming %q#T",
21139 class_key == union_type ? "union"
21140 : class_key == record_type ? "struct" : "class",
21144 /* Issue an error message if DECL is redeclared with different
21145 access than its original declaration [class.access.spec/3].
21146 This applies to nested classes and nested class templates.
21150 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21152 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21155 if ((TREE_PRIVATE (decl)
21156 != (current_access_specifier == access_private_node))
21157 || (TREE_PROTECTED (decl)
21158 != (current_access_specifier == access_protected_node)))
21159 error_at (location, "%qD redeclared with different access", decl);
21162 /* Look for the `template' keyword, as a syntactic disambiguator.
21163 Return TRUE iff it is present, in which case it will be
21167 cp_parser_optional_template_keyword (cp_parser *parser)
21169 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21171 /* The `template' keyword can only be used within templates;
21172 outside templates the parser can always figure out what is a
21173 template and what is not. */
21174 if (!processing_template_decl)
21176 cp_token *token = cp_lexer_peek_token (parser->lexer);
21177 error_at (token->location,
21178 "%<template%> (as a disambiguator) is only allowed "
21179 "within templates");
21180 /* If this part of the token stream is rescanned, the same
21181 error message would be generated. So, we purge the token
21182 from the stream. */
21183 cp_lexer_purge_token (parser->lexer);
21188 /* Consume the `template' keyword. */
21189 cp_lexer_consume_token (parser->lexer);
21197 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
21198 set PARSER->SCOPE, and perform other related actions. */
21201 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21204 struct tree_check *check_value;
21205 deferred_access_check *chk;
21206 VEC (deferred_access_check,gc) *checks;
21208 /* Get the stored value. */
21209 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21210 /* Perform any access checks that were deferred. */
21211 checks = check_value->checks;
21214 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21215 perform_or_defer_access_check (chk->binfo,
21219 /* Set the scope from the stored value. */
21220 parser->scope = check_value->value;
21221 parser->qualifying_scope = check_value->qualifying_scope;
21222 parser->object_scope = NULL_TREE;
21225 /* Consume tokens up through a non-nested END token. Returns TRUE if we
21226 encounter the end of a block before what we were looking for. */
21229 cp_parser_cache_group (cp_parser *parser,
21230 enum cpp_ttype end,
21235 cp_token *token = cp_lexer_peek_token (parser->lexer);
21237 /* Abort a parenthesized expression if we encounter a semicolon. */
21238 if ((end == CPP_CLOSE_PAREN || depth == 0)
21239 && token->type == CPP_SEMICOLON)
21241 /* If we've reached the end of the file, stop. */
21242 if (token->type == CPP_EOF
21243 || (end != CPP_PRAGMA_EOL
21244 && token->type == CPP_PRAGMA_EOL))
21246 if (token->type == CPP_CLOSE_BRACE && depth == 0)
21247 /* We've hit the end of an enclosing block, so there's been some
21248 kind of syntax error. */
21251 /* Consume the token. */
21252 cp_lexer_consume_token (parser->lexer);
21253 /* See if it starts a new group. */
21254 if (token->type == CPP_OPEN_BRACE)
21256 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21257 /* In theory this should probably check end == '}', but
21258 cp_parser_save_member_function_body needs it to exit
21259 after either '}' or ')' when called with ')'. */
21263 else if (token->type == CPP_OPEN_PAREN)
21265 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21266 if (depth == 0 && end == CPP_CLOSE_PAREN)
21269 else if (token->type == CPP_PRAGMA)
21270 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21271 else if (token->type == end)
21276 /* Begin parsing tentatively. We always save tokens while parsing
21277 tentatively so that if the tentative parsing fails we can restore the
21281 cp_parser_parse_tentatively (cp_parser* parser)
21283 /* Enter a new parsing context. */
21284 parser->context = cp_parser_context_new (parser->context);
21285 /* Begin saving tokens. */
21286 cp_lexer_save_tokens (parser->lexer);
21287 /* In order to avoid repetitive access control error messages,
21288 access checks are queued up until we are no longer parsing
21290 push_deferring_access_checks (dk_deferred);
21293 /* Commit to the currently active tentative parse. */
21296 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21298 cp_parser_context *context;
21301 /* Mark all of the levels as committed. */
21302 lexer = parser->lexer;
21303 for (context = parser->context; context->next; context = context->next)
21305 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21307 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21308 while (!cp_lexer_saving_tokens (lexer))
21309 lexer = lexer->next;
21310 cp_lexer_commit_tokens (lexer);
21314 /* Abort the currently active tentative parse. All consumed tokens
21315 will be rolled back, and no diagnostics will be issued. */
21318 cp_parser_abort_tentative_parse (cp_parser* parser)
21320 cp_parser_simulate_error (parser);
21321 /* Now, pretend that we want to see if the construct was
21322 successfully parsed. */
21323 cp_parser_parse_definitely (parser);
21326 /* Stop parsing tentatively. If a parse error has occurred, restore the
21327 token stream. Otherwise, commit to the tokens we have consumed.
21328 Returns true if no error occurred; false otherwise. */
21331 cp_parser_parse_definitely (cp_parser* parser)
21333 bool error_occurred;
21334 cp_parser_context *context;
21336 /* Remember whether or not an error occurred, since we are about to
21337 destroy that information. */
21338 error_occurred = cp_parser_error_occurred (parser);
21339 /* Remove the topmost context from the stack. */
21340 context = parser->context;
21341 parser->context = context->next;
21342 /* If no parse errors occurred, commit to the tentative parse. */
21343 if (!error_occurred)
21345 /* Commit to the tokens read tentatively, unless that was
21347 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21348 cp_lexer_commit_tokens (parser->lexer);
21350 pop_to_parent_deferring_access_checks ();
21352 /* Otherwise, if errors occurred, roll back our state so that things
21353 are just as they were before we began the tentative parse. */
21356 cp_lexer_rollback_tokens (parser->lexer);
21357 pop_deferring_access_checks ();
21359 /* Add the context to the front of the free list. */
21360 context->next = cp_parser_context_free_list;
21361 cp_parser_context_free_list = context;
21363 return !error_occurred;
21366 /* Returns true if we are parsing tentatively and are not committed to
21367 this tentative parse. */
21370 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21372 return (cp_parser_parsing_tentatively (parser)
21373 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21376 /* Returns nonzero iff an error has occurred during the most recent
21377 tentative parse. */
21380 cp_parser_error_occurred (cp_parser* parser)
21382 return (cp_parser_parsing_tentatively (parser)
21383 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21386 /* Returns nonzero if GNU extensions are allowed. */
21389 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21391 return parser->allow_gnu_extensions_p;
21394 /* Objective-C++ Productions */
21397 /* Parse an Objective-C expression, which feeds into a primary-expression
21401 objc-message-expression
21402 objc-string-literal
21403 objc-encode-expression
21404 objc-protocol-expression
21405 objc-selector-expression
21407 Returns a tree representation of the expression. */
21410 cp_parser_objc_expression (cp_parser* parser)
21412 /* Try to figure out what kind of declaration is present. */
21413 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21417 case CPP_OPEN_SQUARE:
21418 return cp_parser_objc_message_expression (parser);
21420 case CPP_OBJC_STRING:
21421 kwd = cp_lexer_consume_token (parser->lexer);
21422 return objc_build_string_object (kwd->u.value);
21425 switch (kwd->keyword)
21427 case RID_AT_ENCODE:
21428 return cp_parser_objc_encode_expression (parser);
21430 case RID_AT_PROTOCOL:
21431 return cp_parser_objc_protocol_expression (parser);
21433 case RID_AT_SELECTOR:
21434 return cp_parser_objc_selector_expression (parser);
21440 error_at (kwd->location,
21441 "misplaced %<@%D%> Objective-C++ construct",
21443 cp_parser_skip_to_end_of_block_or_statement (parser);
21446 return error_mark_node;
21449 /* Parse an Objective-C message expression.
21451 objc-message-expression:
21452 [ objc-message-receiver objc-message-args ]
21454 Returns a representation of an Objective-C message. */
21457 cp_parser_objc_message_expression (cp_parser* parser)
21459 tree receiver, messageargs;
21461 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
21462 receiver = cp_parser_objc_message_receiver (parser);
21463 messageargs = cp_parser_objc_message_args (parser);
21464 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21466 return objc_build_message_expr (build_tree_list (receiver, messageargs));
21469 /* Parse an objc-message-receiver.
21471 objc-message-receiver:
21473 simple-type-specifier
21475 Returns a representation of the type or expression. */
21478 cp_parser_objc_message_receiver (cp_parser* parser)
21482 /* An Objective-C message receiver may be either (1) a type
21483 or (2) an expression. */
21484 cp_parser_parse_tentatively (parser);
21485 rcv = cp_parser_expression (parser, false, NULL);
21487 if (cp_parser_parse_definitely (parser))
21490 rcv = cp_parser_simple_type_specifier (parser,
21491 /*decl_specs=*/NULL,
21492 CP_PARSER_FLAGS_NONE);
21494 return objc_get_class_reference (rcv);
21497 /* Parse the arguments and selectors comprising an Objective-C message.
21502 objc-selector-args , objc-comma-args
21504 objc-selector-args:
21505 objc-selector [opt] : assignment-expression
21506 objc-selector-args objc-selector [opt] : assignment-expression
21509 assignment-expression
21510 objc-comma-args , assignment-expression
21512 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21513 selector arguments and TREE_VALUE containing a list of comma
21517 cp_parser_objc_message_args (cp_parser* parser)
21519 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21520 bool maybe_unary_selector_p = true;
21521 cp_token *token = cp_lexer_peek_token (parser->lexer);
21523 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21525 tree selector = NULL_TREE, arg;
21527 if (token->type != CPP_COLON)
21528 selector = cp_parser_objc_selector (parser);
21530 /* Detect if we have a unary selector. */
21531 if (maybe_unary_selector_p
21532 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21533 return build_tree_list (selector, NULL_TREE);
21535 maybe_unary_selector_p = false;
21536 cp_parser_require (parser, CPP_COLON, RT_COLON);
21537 arg = cp_parser_assignment_expression (parser, false, NULL);
21540 = chainon (sel_args,
21541 build_tree_list (selector, arg));
21543 token = cp_lexer_peek_token (parser->lexer);
21546 /* Handle non-selector arguments, if any. */
21547 while (token->type == CPP_COMMA)
21551 cp_lexer_consume_token (parser->lexer);
21552 arg = cp_parser_assignment_expression (parser, false, NULL);
21555 = chainon (addl_args,
21556 build_tree_list (NULL_TREE, arg));
21558 token = cp_lexer_peek_token (parser->lexer);
21561 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21563 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21564 return build_tree_list (error_mark_node, error_mark_node);
21567 return build_tree_list (sel_args, addl_args);
21570 /* Parse an Objective-C encode expression.
21572 objc-encode-expression:
21573 @encode objc-typename
21575 Returns an encoded representation of the type argument. */
21578 cp_parser_objc_encode_expression (cp_parser* parser)
21583 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21584 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21585 token = cp_lexer_peek_token (parser->lexer);
21586 type = complete_type (cp_parser_type_id (parser));
21587 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21591 error_at (token->location,
21592 "%<@encode%> must specify a type as an argument");
21593 return error_mark_node;
21596 /* This happens if we find @encode(T) (where T is a template
21597 typename or something dependent on a template typename) when
21598 parsing a template. In that case, we can't compile it
21599 immediately, but we rather create an AT_ENCODE_EXPR which will
21600 need to be instantiated when the template is used.
21602 if (dependent_type_p (type))
21604 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21605 TREE_READONLY (value) = 1;
21609 return objc_build_encode_expr (type);
21612 /* Parse an Objective-C @defs expression. */
21615 cp_parser_objc_defs_expression (cp_parser *parser)
21619 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21620 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21621 name = cp_parser_identifier (parser);
21622 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21624 return objc_get_class_ivars (name);
21627 /* Parse an Objective-C protocol expression.
21629 objc-protocol-expression:
21630 @protocol ( identifier )
21632 Returns a representation of the protocol expression. */
21635 cp_parser_objc_protocol_expression (cp_parser* parser)
21639 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21640 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21641 proto = cp_parser_identifier (parser);
21642 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21644 return objc_build_protocol_expr (proto);
21647 /* Parse an Objective-C selector expression.
21649 objc-selector-expression:
21650 @selector ( objc-method-signature )
21652 objc-method-signature:
21658 objc-selector-seq objc-selector :
21660 Returns a representation of the method selector. */
21663 cp_parser_objc_selector_expression (cp_parser* parser)
21665 tree sel_seq = NULL_TREE;
21666 bool maybe_unary_selector_p = true;
21668 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21670 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
21671 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21672 token = cp_lexer_peek_token (parser->lexer);
21674 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21675 || token->type == CPP_SCOPE)
21677 tree selector = NULL_TREE;
21679 if (token->type != CPP_COLON
21680 || token->type == CPP_SCOPE)
21681 selector = cp_parser_objc_selector (parser);
21683 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21684 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21686 /* Detect if we have a unary selector. */
21687 if (maybe_unary_selector_p)
21689 sel_seq = selector;
21690 goto finish_selector;
21694 cp_parser_error (parser, "expected %<:%>");
21697 maybe_unary_selector_p = false;
21698 token = cp_lexer_consume_token (parser->lexer);
21700 if (token->type == CPP_SCOPE)
21703 = chainon (sel_seq,
21704 build_tree_list (selector, NULL_TREE));
21706 = chainon (sel_seq,
21707 build_tree_list (NULL_TREE, NULL_TREE));
21711 = chainon (sel_seq,
21712 build_tree_list (selector, NULL_TREE));
21714 token = cp_lexer_peek_token (parser->lexer);
21718 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21720 return objc_build_selector_expr (loc, sel_seq);
21723 /* Parse a list of identifiers.
21725 objc-identifier-list:
21727 objc-identifier-list , identifier
21729 Returns a TREE_LIST of identifier nodes. */
21732 cp_parser_objc_identifier_list (cp_parser* parser)
21738 identifier = cp_parser_identifier (parser);
21739 if (identifier == error_mark_node)
21740 return error_mark_node;
21742 list = build_tree_list (NULL_TREE, identifier);
21743 sep = cp_lexer_peek_token (parser->lexer);
21745 while (sep->type == CPP_COMMA)
21747 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21748 identifier = cp_parser_identifier (parser);
21749 if (identifier == error_mark_node)
21752 list = chainon (list, build_tree_list (NULL_TREE,
21754 sep = cp_lexer_peek_token (parser->lexer);
21760 /* Parse an Objective-C alias declaration.
21762 objc-alias-declaration:
21763 @compatibility_alias identifier identifier ;
21765 This function registers the alias mapping with the Objective-C front end.
21766 It returns nothing. */
21769 cp_parser_objc_alias_declaration (cp_parser* parser)
21773 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21774 alias = cp_parser_identifier (parser);
21775 orig = cp_parser_identifier (parser);
21776 objc_declare_alias (alias, orig);
21777 cp_parser_consume_semicolon_at_end_of_statement (parser);
21780 /* Parse an Objective-C class forward-declaration.
21782 objc-class-declaration:
21783 @class objc-identifier-list ;
21785 The function registers the forward declarations with the Objective-C
21786 front end. It returns nothing. */
21789 cp_parser_objc_class_declaration (cp_parser* parser)
21791 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21792 objc_declare_class (cp_parser_objc_identifier_list (parser));
21793 cp_parser_consume_semicolon_at_end_of_statement (parser);
21796 /* Parse a list of Objective-C protocol references.
21798 objc-protocol-refs-opt:
21799 objc-protocol-refs [opt]
21801 objc-protocol-refs:
21802 < objc-identifier-list >
21804 Returns a TREE_LIST of identifiers, if any. */
21807 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21809 tree protorefs = NULL_TREE;
21811 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21813 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21814 protorefs = cp_parser_objc_identifier_list (parser);
21815 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21821 /* Parse a Objective-C visibility specification. */
21824 cp_parser_objc_visibility_spec (cp_parser* parser)
21826 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21828 switch (vis->keyword)
21830 case RID_AT_PRIVATE:
21831 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21833 case RID_AT_PROTECTED:
21834 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21836 case RID_AT_PUBLIC:
21837 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21839 case RID_AT_PACKAGE:
21840 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21846 /* Eat '@private'/'@protected'/'@public'. */
21847 cp_lexer_consume_token (parser->lexer);
21850 /* Parse an Objective-C method type. Return 'true' if it is a class
21851 (+) method, and 'false' if it is an instance (-) method. */
21854 cp_parser_objc_method_type (cp_parser* parser)
21856 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21862 /* Parse an Objective-C protocol qualifier. */
21865 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21867 tree quals = NULL_TREE, node;
21868 cp_token *token = cp_lexer_peek_token (parser->lexer);
21870 node = token->u.value;
21872 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21873 && (node == ridpointers [(int) RID_IN]
21874 || node == ridpointers [(int) RID_OUT]
21875 || node == ridpointers [(int) RID_INOUT]
21876 || node == ridpointers [(int) RID_BYCOPY]
21877 || node == ridpointers [(int) RID_BYREF]
21878 || node == ridpointers [(int) RID_ONEWAY]))
21880 quals = tree_cons (NULL_TREE, node, quals);
21881 cp_lexer_consume_token (parser->lexer);
21882 token = cp_lexer_peek_token (parser->lexer);
21883 node = token->u.value;
21889 /* Parse an Objective-C typename. */
21892 cp_parser_objc_typename (cp_parser* parser)
21894 tree type_name = NULL_TREE;
21896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21898 tree proto_quals, cp_type = NULL_TREE;
21900 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21901 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21903 /* An ObjC type name may consist of just protocol qualifiers, in which
21904 case the type shall default to 'id'. */
21905 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21906 cp_type = cp_parser_type_id (parser);
21908 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21909 type_name = build_tree_list (proto_quals, cp_type);
21915 /* Check to see if TYPE refers to an Objective-C selector name. */
21918 cp_parser_objc_selector_p (enum cpp_ttype type)
21920 return (type == CPP_NAME || type == CPP_KEYWORD
21921 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21922 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21923 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21924 || type == CPP_XOR || type == CPP_XOR_EQ);
21927 /* Parse an Objective-C selector. */
21930 cp_parser_objc_selector (cp_parser* parser)
21932 cp_token *token = cp_lexer_consume_token (parser->lexer);
21934 if (!cp_parser_objc_selector_p (token->type))
21936 error_at (token->location, "invalid Objective-C++ selector name");
21937 return error_mark_node;
21940 /* C++ operator names are allowed to appear in ObjC selectors. */
21941 switch (token->type)
21943 case CPP_AND_AND: return get_identifier ("and");
21944 case CPP_AND_EQ: return get_identifier ("and_eq");
21945 case CPP_AND: return get_identifier ("bitand");
21946 case CPP_OR: return get_identifier ("bitor");
21947 case CPP_COMPL: return get_identifier ("compl");
21948 case CPP_NOT: return get_identifier ("not");
21949 case CPP_NOT_EQ: return get_identifier ("not_eq");
21950 case CPP_OR_OR: return get_identifier ("or");
21951 case CPP_OR_EQ: return get_identifier ("or_eq");
21952 case CPP_XOR: return get_identifier ("xor");
21953 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21954 default: return token->u.value;
21958 /* Parse an Objective-C params list. */
21961 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21963 tree params = NULL_TREE;
21964 bool maybe_unary_selector_p = true;
21965 cp_token *token = cp_lexer_peek_token (parser->lexer);
21967 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21969 tree selector = NULL_TREE, type_name, identifier;
21970 tree parm_attr = NULL_TREE;
21972 if (token->keyword == RID_ATTRIBUTE)
21975 if (token->type != CPP_COLON)
21976 selector = cp_parser_objc_selector (parser);
21978 /* Detect if we have a unary selector. */
21979 if (maybe_unary_selector_p
21980 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21982 params = selector; /* Might be followed by attributes. */
21986 maybe_unary_selector_p = false;
21987 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21989 /* Something went quite wrong. There should be a colon
21990 here, but there is not. Stop parsing parameters. */
21993 type_name = cp_parser_objc_typename (parser);
21994 /* New ObjC allows attributes on parameters too. */
21995 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21996 parm_attr = cp_parser_attributes_opt (parser);
21997 identifier = cp_parser_identifier (parser);
22001 objc_build_keyword_decl (selector,
22006 token = cp_lexer_peek_token (parser->lexer);
22009 if (params == NULL_TREE)
22011 cp_parser_error (parser, "objective-c++ method declaration is expected");
22012 return error_mark_node;
22015 /* We allow tail attributes for the method. */
22016 if (token->keyword == RID_ATTRIBUTE)
22018 *attributes = cp_parser_attributes_opt (parser);
22019 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22020 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22022 cp_parser_error (parser,
22023 "method attributes must be specified at the end");
22024 return error_mark_node;
22027 if (params == NULL_TREE)
22029 cp_parser_error (parser, "objective-c++ method declaration is expected");
22030 return error_mark_node;
22035 /* Parse the non-keyword Objective-C params. */
22038 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
22041 tree params = make_node (TREE_LIST);
22042 cp_token *token = cp_lexer_peek_token (parser->lexer);
22043 *ellipsisp = false; /* Initially, assume no ellipsis. */
22045 while (token->type == CPP_COMMA)
22047 cp_parameter_declarator *parmdecl;
22050 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22051 token = cp_lexer_peek_token (parser->lexer);
22053 if (token->type == CPP_ELLIPSIS)
22055 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
22057 token = cp_lexer_peek_token (parser->lexer);
22061 /* TODO: parse attributes for tail parameters. */
22062 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22063 parm = grokdeclarator (parmdecl->declarator,
22064 &parmdecl->decl_specifiers,
22065 PARM, /*initialized=*/0,
22066 /*attrlist=*/NULL);
22068 chainon (params, build_tree_list (NULL_TREE, parm));
22069 token = cp_lexer_peek_token (parser->lexer);
22072 /* We allow tail attributes for the method. */
22073 if (token->keyword == RID_ATTRIBUTE)
22075 if (*attributes == NULL_TREE)
22077 *attributes = cp_parser_attributes_opt (parser);
22078 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22079 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22083 /* We have an error, but parse the attributes, so that we can
22085 *attributes = cp_parser_attributes_opt (parser);
22087 cp_parser_error (parser,
22088 "method attributes must be specified at the end");
22089 return error_mark_node;
22095 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
22098 cp_parser_objc_interstitial_code (cp_parser* parser)
22100 cp_token *token = cp_lexer_peek_token (parser->lexer);
22102 /* If the next token is `extern' and the following token is a string
22103 literal, then we have a linkage specification. */
22104 if (token->keyword == RID_EXTERN
22105 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22106 cp_parser_linkage_specification (parser);
22107 /* Handle #pragma, if any. */
22108 else if (token->type == CPP_PRAGMA)
22109 cp_parser_pragma (parser, pragma_external);
22110 /* Allow stray semicolons. */
22111 else if (token->type == CPP_SEMICOLON)
22112 cp_lexer_consume_token (parser->lexer);
22113 /* Mark methods as optional or required, when building protocols. */
22114 else if (token->keyword == RID_AT_OPTIONAL)
22116 cp_lexer_consume_token (parser->lexer);
22117 objc_set_method_opt (true);
22119 else if (token->keyword == RID_AT_REQUIRED)
22121 cp_lexer_consume_token (parser->lexer);
22122 objc_set_method_opt (false);
22124 else if (token->keyword == RID_NAMESPACE)
22125 cp_parser_namespace_definition (parser);
22126 /* Other stray characters must generate errors. */
22127 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22129 cp_lexer_consume_token (parser->lexer);
22130 error ("stray %qs between Objective-C++ methods",
22131 token->type == CPP_OPEN_BRACE ? "{" : "}");
22133 /* Finally, try to parse a block-declaration, or a function-definition. */
22135 cp_parser_block_declaration (parser, /*statement_p=*/false);
22138 /* Parse a method signature. */
22141 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22143 tree rettype, kwdparms, optparms;
22144 bool ellipsis = false;
22145 bool is_class_method;
22147 is_class_method = cp_parser_objc_method_type (parser);
22148 rettype = cp_parser_objc_typename (parser);
22149 *attributes = NULL_TREE;
22150 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22151 if (kwdparms == error_mark_node)
22152 return error_mark_node;
22153 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22154 if (optparms == error_mark_node)
22155 return error_mark_node;
22157 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22161 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22164 cp_lexer_save_tokens (parser->lexer);
22165 tattr = cp_parser_attributes_opt (parser);
22166 gcc_assert (tattr) ;
22168 /* If the attributes are followed by a method introducer, this is not allowed.
22169 Dump the attributes and flag the situation. */
22170 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22171 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22174 /* Otherwise, the attributes introduce some interstitial code, possibly so
22175 rewind to allow that check. */
22176 cp_lexer_rollback_tokens (parser->lexer);
22180 /* Parse an Objective-C method prototype list. */
22183 cp_parser_objc_method_prototype_list (cp_parser* parser)
22185 cp_token *token = cp_lexer_peek_token (parser->lexer);
22187 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22189 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22191 tree attributes, sig;
22192 bool is_class_method;
22193 if (token->type == CPP_PLUS)
22194 is_class_method = true;
22196 is_class_method = false;
22197 sig = cp_parser_objc_method_signature (parser, &attributes);
22198 if (sig == error_mark_node)
22200 cp_parser_skip_to_end_of_block_or_statement (parser);
22201 token = cp_lexer_peek_token (parser->lexer);
22204 objc_add_method_declaration (is_class_method, sig, attributes);
22205 cp_parser_consume_semicolon_at_end_of_statement (parser);
22207 else if (token->keyword == RID_AT_PROPERTY)
22208 cp_parser_objc_at_property_declaration (parser);
22209 else if (token->keyword == RID_ATTRIBUTE
22210 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22211 warning_at (cp_lexer_peek_token (parser->lexer)->location,
22213 "prefix attributes are ignored for methods");
22215 /* Allow for interspersed non-ObjC++ code. */
22216 cp_parser_objc_interstitial_code (parser);
22218 token = cp_lexer_peek_token (parser->lexer);
22221 if (token->type != CPP_EOF)
22222 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22224 cp_parser_error (parser, "expected %<@end%>");
22226 objc_finish_interface ();
22229 /* Parse an Objective-C method definition list. */
22232 cp_parser_objc_method_definition_list (cp_parser* parser)
22234 cp_token *token = cp_lexer_peek_token (parser->lexer);
22236 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22240 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22243 tree sig, attribute;
22244 bool is_class_method;
22245 if (token->type == CPP_PLUS)
22246 is_class_method = true;
22248 is_class_method = false;
22249 push_deferring_access_checks (dk_deferred);
22250 sig = cp_parser_objc_method_signature (parser, &attribute);
22251 if (sig == error_mark_node)
22253 cp_parser_skip_to_end_of_block_or_statement (parser);
22254 token = cp_lexer_peek_token (parser->lexer);
22257 objc_start_method_definition (is_class_method, sig, attribute);
22259 /* For historical reasons, we accept an optional semicolon. */
22260 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22261 cp_lexer_consume_token (parser->lexer);
22263 ptk = cp_lexer_peek_token (parser->lexer);
22264 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
22265 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22267 perform_deferred_access_checks ();
22268 stop_deferring_access_checks ();
22269 meth = cp_parser_function_definition_after_declarator (parser,
22271 pop_deferring_access_checks ();
22272 objc_finish_method_definition (meth);
22275 /* The following case will be removed once @synthesize is
22276 completely implemented. */
22277 else if (token->keyword == RID_AT_PROPERTY)
22278 cp_parser_objc_at_property_declaration (parser);
22279 else if (token->keyword == RID_AT_SYNTHESIZE)
22280 cp_parser_objc_at_synthesize_declaration (parser);
22281 else if (token->keyword == RID_AT_DYNAMIC)
22282 cp_parser_objc_at_dynamic_declaration (parser);
22283 else if (token->keyword == RID_ATTRIBUTE
22284 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22285 warning_at (token->location, OPT_Wattributes,
22286 "prefix attributes are ignored for methods");
22288 /* Allow for interspersed non-ObjC++ code. */
22289 cp_parser_objc_interstitial_code (parser);
22291 token = cp_lexer_peek_token (parser->lexer);
22294 if (token->type != CPP_EOF)
22295 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22297 cp_parser_error (parser, "expected %<@end%>");
22299 objc_finish_implementation ();
22302 /* Parse Objective-C ivars. */
22305 cp_parser_objc_class_ivars (cp_parser* parser)
22307 cp_token *token = cp_lexer_peek_token (parser->lexer);
22309 if (token->type != CPP_OPEN_BRACE)
22310 return; /* No ivars specified. */
22312 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
22313 token = cp_lexer_peek_token (parser->lexer);
22315 while (token->type != CPP_CLOSE_BRACE
22316 && token->keyword != RID_AT_END && token->type != CPP_EOF)
22318 cp_decl_specifier_seq declspecs;
22319 int decl_class_or_enum_p;
22320 tree prefix_attributes;
22322 cp_parser_objc_visibility_spec (parser);
22324 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22327 cp_parser_decl_specifier_seq (parser,
22328 CP_PARSER_FLAGS_OPTIONAL,
22330 &decl_class_or_enum_p);
22332 /* auto, register, static, extern, mutable. */
22333 if (declspecs.storage_class != sc_none)
22335 cp_parser_error (parser, "invalid type for instance variable");
22336 declspecs.storage_class = sc_none;
22340 if (declspecs.specs[(int) ds_thread])
22342 cp_parser_error (parser, "invalid type for instance variable");
22343 declspecs.specs[(int) ds_thread] = 0;
22347 if (declspecs.specs[(int) ds_typedef])
22349 cp_parser_error (parser, "invalid type for instance variable");
22350 declspecs.specs[(int) ds_typedef] = 0;
22353 prefix_attributes = declspecs.attributes;
22354 declspecs.attributes = NULL_TREE;
22356 /* Keep going until we hit the `;' at the end of the
22358 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22360 tree width = NULL_TREE, attributes, first_attribute, decl;
22361 cp_declarator *declarator = NULL;
22362 int ctor_dtor_or_conv_p;
22364 /* Check for a (possibly unnamed) bitfield declaration. */
22365 token = cp_lexer_peek_token (parser->lexer);
22366 if (token->type == CPP_COLON)
22369 if (token->type == CPP_NAME
22370 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22373 /* Get the name of the bitfield. */
22374 declarator = make_id_declarator (NULL_TREE,
22375 cp_parser_identifier (parser),
22379 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22380 /* Get the width of the bitfield. */
22382 = cp_parser_constant_expression (parser,
22383 /*allow_non_constant=*/false,
22388 /* Parse the declarator. */
22390 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22391 &ctor_dtor_or_conv_p,
22392 /*parenthesized_p=*/NULL,
22393 /*member_p=*/false);
22396 /* Look for attributes that apply to the ivar. */
22397 attributes = cp_parser_attributes_opt (parser);
22398 /* Remember which attributes are prefix attributes and
22400 first_attribute = attributes;
22401 /* Combine the attributes. */
22402 attributes = chainon (prefix_attributes, attributes);
22405 /* Create the bitfield declaration. */
22406 decl = grokbitfield (declarator, &declspecs,
22410 decl = grokfield (declarator, &declspecs,
22411 NULL_TREE, /*init_const_expr_p=*/false,
22412 NULL_TREE, attributes);
22414 /* Add the instance variable. */
22415 objc_add_instance_variable (decl);
22417 /* Reset PREFIX_ATTRIBUTES. */
22418 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22419 attributes = TREE_CHAIN (attributes);
22421 TREE_CHAIN (attributes) = NULL_TREE;
22423 token = cp_lexer_peek_token (parser->lexer);
22425 if (token->type == CPP_COMMA)
22427 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22433 cp_parser_consume_semicolon_at_end_of_statement (parser);
22434 token = cp_lexer_peek_token (parser->lexer);
22437 if (token->keyword == RID_AT_END)
22438 cp_parser_error (parser, "expected %<}%>");
22440 /* Do not consume the RID_AT_END, so it will be read again as terminating
22441 the @interface of @implementation. */
22442 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22443 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
22445 /* For historical reasons, we accept an optional semicolon. */
22446 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22447 cp_lexer_consume_token (parser->lexer);
22450 /* Parse an Objective-C protocol declaration. */
22453 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22455 tree proto, protorefs;
22458 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22461 tok = cp_lexer_peek_token (parser->lexer);
22462 error_at (tok->location, "identifier expected after %<@protocol%>");
22466 /* See if we have a forward declaration or a definition. */
22467 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22469 /* Try a forward declaration first. */
22470 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22472 objc_declare_protocols (cp_parser_objc_identifier_list (parser),
22475 cp_parser_consume_semicolon_at_end_of_statement (parser);
22478 /* Ok, we got a full-fledged definition (or at least should). */
22481 proto = cp_parser_identifier (parser);
22482 protorefs = cp_parser_objc_protocol_refs_opt (parser);
22483 objc_start_protocol (proto, protorefs, attributes);
22484 cp_parser_objc_method_prototype_list (parser);
22488 /* Parse an Objective-C superclass or category. */
22491 cp_parser_objc_superclass_or_category (cp_parser *parser,
22494 tree *categ, bool *is_class_extension)
22496 cp_token *next = cp_lexer_peek_token (parser->lexer);
22498 *super = *categ = NULL_TREE;
22499 *is_class_extension = false;
22500 if (next->type == CPP_COLON)
22502 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22503 *super = cp_parser_identifier (parser);
22505 else if (next->type == CPP_OPEN_PAREN)
22507 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
22509 /* If there is no category name, and this is an @interface, we
22510 have a class extension. */
22511 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22513 *categ = NULL_TREE;
22514 *is_class_extension = true;
22517 *categ = cp_parser_identifier (parser);
22519 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22523 /* Parse an Objective-C class interface. */
22526 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22528 tree name, super, categ, protos;
22529 bool is_class_extension;
22531 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
22532 name = cp_parser_identifier (parser);
22533 if (name == error_mark_node)
22535 /* It's hard to recover because even if valid @interface stuff
22536 is to follow, we can't compile it (or validate it) if we
22537 don't even know which class it refers to. Let's assume this
22538 was a stray '@interface' token in the stream and skip it.
22542 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22543 &is_class_extension);
22544 protos = cp_parser_objc_protocol_refs_opt (parser);
22546 /* We have either a class or a category on our hands. */
22547 if (categ || is_class_extension)
22548 objc_start_category_interface (name, categ, protos, attributes);
22551 objc_start_class_interface (name, super, protos, attributes);
22552 /* Handle instance variable declarations, if any. */
22553 cp_parser_objc_class_ivars (parser);
22554 objc_continue_interface ();
22557 cp_parser_objc_method_prototype_list (parser);
22560 /* Parse an Objective-C class implementation. */
22563 cp_parser_objc_class_implementation (cp_parser* parser)
22565 tree name, super, categ;
22566 bool is_class_extension;
22568 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22569 name = cp_parser_identifier (parser);
22570 if (name == error_mark_node)
22572 /* It's hard to recover because even if valid @implementation
22573 stuff is to follow, we can't compile it (or validate it) if
22574 we don't even know which class it refers to. Let's assume
22575 this was a stray '@implementation' token in the stream and
22580 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22581 &is_class_extension);
22583 /* We have either a class or a category on our hands. */
22585 objc_start_category_implementation (name, categ);
22588 objc_start_class_implementation (name, super);
22589 /* Handle instance variable declarations, if any. */
22590 cp_parser_objc_class_ivars (parser);
22591 objc_continue_implementation ();
22594 cp_parser_objc_method_definition_list (parser);
22597 /* Consume the @end token and finish off the implementation. */
22600 cp_parser_objc_end_implementation (cp_parser* parser)
22602 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22603 objc_finish_implementation ();
22606 /* Parse an Objective-C declaration. */
22609 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22611 /* Try to figure out what kind of declaration is present. */
22612 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22615 switch (kwd->keyword)
22620 error_at (kwd->location, "attributes may not be specified before"
22621 " the %<@%D%> Objective-C++ keyword",
22625 case RID_AT_IMPLEMENTATION:
22626 warning_at (kwd->location, OPT_Wattributes,
22627 "prefix attributes are ignored before %<@%D%>",
22634 switch (kwd->keyword)
22637 cp_parser_objc_alias_declaration (parser);
22640 cp_parser_objc_class_declaration (parser);
22642 case RID_AT_PROTOCOL:
22643 cp_parser_objc_protocol_declaration (parser, attributes);
22645 case RID_AT_INTERFACE:
22646 cp_parser_objc_class_interface (parser, attributes);
22648 case RID_AT_IMPLEMENTATION:
22649 cp_parser_objc_class_implementation (parser);
22652 cp_parser_objc_end_implementation (parser);
22655 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22657 cp_parser_skip_to_end_of_block_or_statement (parser);
22661 /* Parse an Objective-C try-catch-finally statement.
22663 objc-try-catch-finally-stmt:
22664 @try compound-statement objc-catch-clause-seq [opt]
22665 objc-finally-clause [opt]
22667 objc-catch-clause-seq:
22668 objc-catch-clause objc-catch-clause-seq [opt]
22671 @catch ( objc-exception-declaration ) compound-statement
22673 objc-finally-clause:
22674 @finally compound-statement
22676 objc-exception-declaration:
22677 parameter-declaration
22680 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22684 PS: This function is identical to c_parser_objc_try_catch_finally_statement
22685 for C. Keep them in sync. */
22688 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22690 location_t location;
22693 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22694 location = cp_lexer_peek_token (parser->lexer)->location;
22695 objc_maybe_warn_exceptions (location);
22696 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22697 node, lest it get absorbed into the surrounding block. */
22698 stmt = push_stmt_list ();
22699 cp_parser_compound_statement (parser, NULL, false);
22700 objc_begin_try_stmt (location, pop_stmt_list (stmt));
22702 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22704 cp_parameter_declarator *parm;
22705 tree parameter_declaration = error_mark_node;
22706 bool seen_open_paren = false;
22708 cp_lexer_consume_token (parser->lexer);
22709 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22710 seen_open_paren = true;
22711 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22713 /* We have "@catch (...)" (where the '...' are literally
22714 what is in the code). Skip the '...'.
22715 parameter_declaration is set to NULL_TREE, and
22716 objc_being_catch_clauses() knows that that means
22718 cp_lexer_consume_token (parser->lexer);
22719 parameter_declaration = NULL_TREE;
22723 /* We have "@catch (NSException *exception)" or something
22724 like that. Parse the parameter declaration. */
22725 parm = cp_parser_parameter_declaration (parser, false, NULL);
22727 parameter_declaration = error_mark_node;
22729 parameter_declaration = grokdeclarator (parm->declarator,
22730 &parm->decl_specifiers,
22731 PARM, /*initialized=*/0,
22732 /*attrlist=*/NULL);
22734 if (seen_open_paren)
22735 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22738 /* If there was no open parenthesis, we are recovering from
22739 an error, and we are trying to figure out what mistake
22740 the user has made. */
22742 /* If there is an immediate closing parenthesis, the user
22743 probably forgot the opening one (ie, they typed "@catch
22744 NSException *e)". Parse the closing parenthesis and keep
22746 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22747 cp_lexer_consume_token (parser->lexer);
22749 /* If these is no immediate closing parenthesis, the user
22750 probably doesn't know that parenthesis are required at
22751 all (ie, they typed "@catch NSException *e"). So, just
22752 forget about the closing parenthesis and keep going. */
22754 objc_begin_catch_clause (parameter_declaration);
22755 cp_parser_compound_statement (parser, NULL, false);
22756 objc_finish_catch_clause ();
22758 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22760 cp_lexer_consume_token (parser->lexer);
22761 location = cp_lexer_peek_token (parser->lexer)->location;
22762 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22763 node, lest it get absorbed into the surrounding block. */
22764 stmt = push_stmt_list ();
22765 cp_parser_compound_statement (parser, NULL, false);
22766 objc_build_finally_clause (location, pop_stmt_list (stmt));
22769 return objc_finish_try_stmt ();
22772 /* Parse an Objective-C synchronized statement.
22774 objc-synchronized-stmt:
22775 @synchronized ( expression ) compound-statement
22777 Returns NULL_TREE. */
22780 cp_parser_objc_synchronized_statement (cp_parser *parser)
22782 location_t location;
22785 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22787 location = cp_lexer_peek_token (parser->lexer)->location;
22788 objc_maybe_warn_exceptions (location);
22789 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22790 lock = cp_parser_expression (parser, false, NULL);
22791 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22793 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22794 node, lest it get absorbed into the surrounding block. */
22795 stmt = push_stmt_list ();
22796 cp_parser_compound_statement (parser, NULL, false);
22798 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22801 /* Parse an Objective-C throw statement.
22804 @throw assignment-expression [opt] ;
22806 Returns a constructed '@throw' statement. */
22809 cp_parser_objc_throw_statement (cp_parser *parser)
22811 tree expr = NULL_TREE;
22812 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22814 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22817 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22819 cp_parser_consume_semicolon_at_end_of_statement (parser);
22821 return objc_build_throw_stmt (loc, expr);
22824 /* Parse an Objective-C statement. */
22827 cp_parser_objc_statement (cp_parser * parser)
22829 /* Try to figure out what kind of declaration is present. */
22830 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22832 switch (kwd->keyword)
22835 return cp_parser_objc_try_catch_finally_statement (parser);
22836 case RID_AT_SYNCHRONIZED:
22837 return cp_parser_objc_synchronized_statement (parser);
22839 return cp_parser_objc_throw_statement (parser);
22841 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22843 cp_parser_skip_to_end_of_block_or_statement (parser);
22846 return error_mark_node;
22849 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22850 look ahead to see if an objc keyword follows the attributes. This
22851 is to detect the use of prefix attributes on ObjC @interface and
22855 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22857 cp_lexer_save_tokens (parser->lexer);
22858 *attrib = cp_parser_attributes_opt (parser);
22859 gcc_assert (*attrib);
22860 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22862 cp_lexer_commit_tokens (parser->lexer);
22865 cp_lexer_rollback_tokens (parser->lexer);
22869 /* This routine is a minimal replacement for
22870 c_parser_struct_declaration () used when parsing the list of
22871 types/names or ObjC++ properties. For example, when parsing the
22874 @property (readonly) int a, b, c;
22876 this function is responsible for parsing "int a, int b, int c" and
22877 returning the declarations as CHAIN of DECLs.
22879 TODO: Share this code with cp_parser_objc_class_ivars. It's very
22880 similar parsing. */
22882 cp_parser_objc_struct_declaration (cp_parser *parser)
22884 tree decls = NULL_TREE;
22885 cp_decl_specifier_seq declspecs;
22886 int decl_class_or_enum_p;
22887 tree prefix_attributes;
22889 cp_parser_decl_specifier_seq (parser,
22890 CP_PARSER_FLAGS_NONE,
22892 &decl_class_or_enum_p);
22894 if (declspecs.type == error_mark_node)
22895 return error_mark_node;
22897 /* auto, register, static, extern, mutable. */
22898 if (declspecs.storage_class != sc_none)
22900 cp_parser_error (parser, "invalid type for property");
22901 declspecs.storage_class = sc_none;
22905 if (declspecs.specs[(int) ds_thread])
22907 cp_parser_error (parser, "invalid type for property");
22908 declspecs.specs[(int) ds_thread] = 0;
22912 if (declspecs.specs[(int) ds_typedef])
22914 cp_parser_error (parser, "invalid type for property");
22915 declspecs.specs[(int) ds_typedef] = 0;
22918 prefix_attributes = declspecs.attributes;
22919 declspecs.attributes = NULL_TREE;
22921 /* Keep going until we hit the `;' at the end of the declaration. */
22922 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22924 tree attributes, first_attribute, decl;
22925 cp_declarator *declarator;
22928 /* Parse the declarator. */
22929 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22930 NULL, NULL, false);
22932 /* Look for attributes that apply to the ivar. */
22933 attributes = cp_parser_attributes_opt (parser);
22934 /* Remember which attributes are prefix attributes and
22936 first_attribute = attributes;
22937 /* Combine the attributes. */
22938 attributes = chainon (prefix_attributes, attributes);
22940 decl = grokfield (declarator, &declspecs,
22941 NULL_TREE, /*init_const_expr_p=*/false,
22942 NULL_TREE, attributes);
22944 if (decl == error_mark_node || decl == NULL_TREE)
22945 return error_mark_node;
22947 /* Reset PREFIX_ATTRIBUTES. */
22948 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22949 attributes = TREE_CHAIN (attributes);
22951 TREE_CHAIN (attributes) = NULL_TREE;
22953 DECL_CHAIN (decl) = decls;
22956 token = cp_lexer_peek_token (parser->lexer);
22957 if (token->type == CPP_COMMA)
22959 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22968 /* Parse an Objective-C @property declaration. The syntax is:
22970 objc-property-declaration:
22971 '@property' objc-property-attributes[opt] struct-declaration ;
22973 objc-property-attributes:
22974 '(' objc-property-attribute-list ')'
22976 objc-property-attribute-list:
22977 objc-property-attribute
22978 objc-property-attribute-list, objc-property-attribute
22980 objc-property-attribute
22981 'getter' = identifier
22982 'setter' = identifier
22991 @property NSString *name;
22992 @property (readonly) id object;
22993 @property (retain, nonatomic, getter=getTheName) id name;
22994 @property int a, b, c;
22996 PS: This function is identical to
22997 c_parser_objc_at_property_declaration for C. Keep them in sync. */
22999 cp_parser_objc_at_property_declaration (cp_parser *parser)
23001 /* The following variables hold the attributes of the properties as
23002 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
23003 seen. When we see an attribute, we set them to 'true' (if they
23004 are boolean properties) or to the identifier (if they have an
23005 argument, ie, for getter and setter). Note that here we only
23006 parse the list of attributes, check the syntax and accumulate the
23007 attributes that we find. objc_add_property_declaration() will
23008 then process the information. */
23009 bool property_assign = false;
23010 bool property_copy = false;
23011 tree property_getter_ident = NULL_TREE;
23012 bool property_nonatomic = false;
23013 bool property_readonly = false;
23014 bool property_readwrite = false;
23015 bool property_retain = false;
23016 tree property_setter_ident = NULL_TREE;
23018 /* 'properties' is the list of properties that we read. Usually a
23019 single one, but maybe more (eg, in "@property int a, b, c;" there
23024 loc = cp_lexer_peek_token (parser->lexer)->location;
23026 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
23028 /* Parse the optional attribute list... */
23029 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23032 cp_lexer_consume_token (parser->lexer);
23036 bool syntax_error = false;
23037 cp_token *token = cp_lexer_peek_token (parser->lexer);
23040 if (token->type != CPP_NAME)
23042 cp_parser_error (parser, "expected identifier");
23045 keyword = C_RID_CODE (token->u.value);
23046 cp_lexer_consume_token (parser->lexer);
23049 case RID_ASSIGN: property_assign = true; break;
23050 case RID_COPY: property_copy = true; break;
23051 case RID_NONATOMIC: property_nonatomic = true; break;
23052 case RID_READONLY: property_readonly = true; break;
23053 case RID_READWRITE: property_readwrite = true; break;
23054 case RID_RETAIN: property_retain = true; break;
23058 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23060 cp_parser_error (parser,
23061 "getter/setter/ivar attribute must be followed by %<=%>");
23062 syntax_error = true;
23065 cp_lexer_consume_token (parser->lexer); /* eat the = */
23066 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23068 cp_parser_error (parser, "expected identifier");
23069 syntax_error = true;
23072 if (keyword == RID_SETTER)
23074 if (property_setter_ident != NULL_TREE)
23075 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23077 property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23078 cp_lexer_consume_token (parser->lexer);
23079 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23080 cp_parser_error (parser, "setter name must terminate with %<:%>");
23082 cp_lexer_consume_token (parser->lexer);
23086 if (property_getter_ident != NULL_TREE)
23087 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23089 property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23090 cp_lexer_consume_token (parser->lexer);
23094 cp_parser_error (parser, "unknown property attribute");
23095 syntax_error = true;
23102 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23103 cp_lexer_consume_token (parser->lexer);
23108 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23110 cp_parser_skip_to_closing_parenthesis (parser,
23111 /*recovering=*/true,
23112 /*or_comma=*/false,
23113 /*consume_paren=*/true);
23117 /* ... and the property declaration(s). */
23118 properties = cp_parser_objc_struct_declaration (parser);
23120 if (properties == error_mark_node)
23122 cp_parser_skip_to_end_of_statement (parser);
23123 /* If the next token is now a `;', consume it. */
23124 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23125 cp_lexer_consume_token (parser->lexer);
23129 if (properties == NULL_TREE)
23130 cp_parser_error (parser, "expected identifier");
23133 /* Comma-separated properties are chained together in
23134 reverse order; add them one by one. */
23135 properties = nreverse (properties);
23137 for (; properties; properties = TREE_CHAIN (properties))
23138 objc_add_property_declaration (loc, copy_node (properties),
23139 property_readonly, property_readwrite,
23140 property_assign, property_retain,
23141 property_copy, property_nonatomic,
23142 property_getter_ident, property_setter_ident);
23145 cp_parser_consume_semicolon_at_end_of_statement (parser);
23148 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
23150 objc-synthesize-declaration:
23151 @synthesize objc-synthesize-identifier-list ;
23153 objc-synthesize-identifier-list:
23154 objc-synthesize-identifier
23155 objc-synthesize-identifier-list, objc-synthesize-identifier
23157 objc-synthesize-identifier
23159 identifier = identifier
23162 @synthesize MyProperty;
23163 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23165 PS: This function is identical to c_parser_objc_at_synthesize_declaration
23166 for C. Keep them in sync.
23169 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23171 tree list = NULL_TREE;
23173 loc = cp_lexer_peek_token (parser->lexer)->location;
23175 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
23178 tree property, ivar;
23179 property = cp_parser_identifier (parser);
23180 if (property == error_mark_node)
23182 cp_parser_consume_semicolon_at_end_of_statement (parser);
23185 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23187 cp_lexer_consume_token (parser->lexer);
23188 ivar = cp_parser_identifier (parser);
23189 if (ivar == error_mark_node)
23191 cp_parser_consume_semicolon_at_end_of_statement (parser);
23197 list = chainon (list, build_tree_list (ivar, property));
23198 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23199 cp_lexer_consume_token (parser->lexer);
23203 cp_parser_consume_semicolon_at_end_of_statement (parser);
23204 objc_add_synthesize_declaration (loc, list);
23207 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
23209 objc-dynamic-declaration:
23210 @dynamic identifier-list ;
23213 @dynamic MyProperty;
23214 @dynamic MyProperty, AnotherProperty;
23216 PS: This function is identical to c_parser_objc_at_dynamic_declaration
23217 for C. Keep them in sync.
23220 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23222 tree list = NULL_TREE;
23224 loc = cp_lexer_peek_token (parser->lexer)->location;
23226 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
23230 property = cp_parser_identifier (parser);
23231 if (property == error_mark_node)
23233 cp_parser_consume_semicolon_at_end_of_statement (parser);
23236 list = chainon (list, build_tree_list (NULL, property));
23237 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23238 cp_lexer_consume_token (parser->lexer);
23242 cp_parser_consume_semicolon_at_end_of_statement (parser);
23243 objc_add_dynamic_declaration (loc, list);
23247 /* OpenMP 2.5 parsing routines. */
23249 /* Returns name of the next clause.
23250 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23251 the token is not consumed. Otherwise appropriate pragma_omp_clause is
23252 returned and the token is consumed. */
23254 static pragma_omp_clause
23255 cp_parser_omp_clause_name (cp_parser *parser)
23257 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23259 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23260 result = PRAGMA_OMP_CLAUSE_IF;
23261 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23262 result = PRAGMA_OMP_CLAUSE_DEFAULT;
23263 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23264 result = PRAGMA_OMP_CLAUSE_PRIVATE;
23265 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23267 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23268 const char *p = IDENTIFIER_POINTER (id);
23273 if (!strcmp ("collapse", p))
23274 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23275 else if (!strcmp ("copyin", p))
23276 result = PRAGMA_OMP_CLAUSE_COPYIN;
23277 else if (!strcmp ("copyprivate", p))
23278 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23281 if (!strcmp ("firstprivate", p))
23282 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23285 if (!strcmp ("lastprivate", p))
23286 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23289 if (!strcmp ("nowait", p))
23290 result = PRAGMA_OMP_CLAUSE_NOWAIT;
23291 else if (!strcmp ("num_threads", p))
23292 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23295 if (!strcmp ("ordered", p))
23296 result = PRAGMA_OMP_CLAUSE_ORDERED;
23299 if (!strcmp ("reduction", p))
23300 result = PRAGMA_OMP_CLAUSE_REDUCTION;
23303 if (!strcmp ("schedule", p))
23304 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23305 else if (!strcmp ("shared", p))
23306 result = PRAGMA_OMP_CLAUSE_SHARED;
23309 if (!strcmp ("untied", p))
23310 result = PRAGMA_OMP_CLAUSE_UNTIED;
23315 if (result != PRAGMA_OMP_CLAUSE_NONE)
23316 cp_lexer_consume_token (parser->lexer);
23321 /* Validate that a clause of the given type does not already exist. */
23324 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23325 const char *name, location_t location)
23329 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23330 if (OMP_CLAUSE_CODE (c) == code)
23332 error_at (location, "too many %qs clauses", name);
23340 variable-list , identifier
23342 In addition, we match a closing parenthesis. An opening parenthesis
23343 will have been consumed by the caller.
23345 If KIND is nonzero, create the appropriate node and install the decl
23346 in OMP_CLAUSE_DECL and add the node to the head of the list.
23348 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23349 return the list created. */
23352 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23360 token = cp_lexer_peek_token (parser->lexer);
23361 name = cp_parser_id_expression (parser, /*template_p=*/false,
23362 /*check_dependency_p=*/true,
23363 /*template_p=*/NULL,
23364 /*declarator_p=*/false,
23365 /*optional_p=*/false);
23366 if (name == error_mark_node)
23369 decl = cp_parser_lookup_name_simple (parser, name, token->location);
23370 if (decl == error_mark_node)
23371 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23373 else if (kind != 0)
23375 tree u = build_omp_clause (token->location, kind);
23376 OMP_CLAUSE_DECL (u) = decl;
23377 OMP_CLAUSE_CHAIN (u) = list;
23381 list = tree_cons (decl, NULL_TREE, list);
23384 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23386 cp_lexer_consume_token (parser->lexer);
23389 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23393 /* Try to resync to an unnested comma. Copied from
23394 cp_parser_parenthesized_expression_list. */
23396 ending = cp_parser_skip_to_closing_parenthesis (parser,
23397 /*recovering=*/true,
23399 /*consume_paren=*/true);
23407 /* Similarly, but expect leading and trailing parenthesis. This is a very
23408 common case for omp clauses. */
23411 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23413 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23414 return cp_parser_omp_var_list_no_open (parser, kind, list);
23419 collapse ( constant-expression ) */
23422 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23428 loc = cp_lexer_peek_token (parser->lexer)->location;
23429 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23432 num = cp_parser_constant_expression (parser, false, NULL);
23434 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23435 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23436 /*or_comma=*/false,
23437 /*consume_paren=*/true);
23439 if (num == error_mark_node)
23441 num = fold_non_dependent_expr (num);
23442 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23443 || !host_integerp (num, 0)
23444 || (n = tree_low_cst (num, 0)) <= 0
23447 error_at (loc, "collapse argument needs positive constant integer expression");
23451 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23452 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23453 OMP_CLAUSE_CHAIN (c) = list;
23454 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23460 default ( shared | none ) */
23463 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23465 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23468 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23470 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23472 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23473 const char *p = IDENTIFIER_POINTER (id);
23478 if (strcmp ("none", p) != 0)
23480 kind = OMP_CLAUSE_DEFAULT_NONE;
23484 if (strcmp ("shared", p) != 0)
23486 kind = OMP_CLAUSE_DEFAULT_SHARED;
23493 cp_lexer_consume_token (parser->lexer);
23498 cp_parser_error (parser, "expected %<none%> or %<shared%>");
23501 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23502 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23503 /*or_comma=*/false,
23504 /*consume_paren=*/true);
23506 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23509 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23510 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23511 OMP_CLAUSE_CHAIN (c) = list;
23512 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23518 if ( expression ) */
23521 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23525 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23528 t = cp_parser_condition (parser);
23530 if (t == error_mark_node
23531 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23532 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23533 /*or_comma=*/false,
23534 /*consume_paren=*/true);
23536 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23538 c = build_omp_clause (location, OMP_CLAUSE_IF);
23539 OMP_CLAUSE_IF_EXPR (c) = t;
23540 OMP_CLAUSE_CHAIN (c) = list;
23549 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23550 tree list, location_t location)
23554 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23556 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23557 OMP_CLAUSE_CHAIN (c) = list;
23562 num_threads ( expression ) */
23565 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23566 location_t location)
23570 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23573 t = cp_parser_expression (parser, false, NULL);
23575 if (t == error_mark_node
23576 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23577 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23578 /*or_comma=*/false,
23579 /*consume_paren=*/true);
23581 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23582 "num_threads", location);
23584 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23585 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23586 OMP_CLAUSE_CHAIN (c) = list;
23595 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23596 tree list, location_t location)
23600 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23601 "ordered", location);
23603 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23604 OMP_CLAUSE_CHAIN (c) = list;
23609 reduction ( reduction-operator : variable-list )
23611 reduction-operator:
23612 One of: + * - & ^ | && || */
23615 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23617 enum tree_code code;
23620 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23623 switch (cp_lexer_peek_token (parser->lexer)->type)
23635 code = BIT_AND_EXPR;
23638 code = BIT_XOR_EXPR;
23641 code = BIT_IOR_EXPR;
23644 code = TRUTH_ANDIF_EXPR;
23647 code = TRUTH_ORIF_EXPR;
23650 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23651 "%<|%>, %<&&%>, or %<||%>");
23653 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23654 /*or_comma=*/false,
23655 /*consume_paren=*/true);
23658 cp_lexer_consume_token (parser->lexer);
23660 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23663 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23664 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23665 OMP_CLAUSE_REDUCTION_CODE (c) = code;
23671 schedule ( schedule-kind )
23672 schedule ( schedule-kind , expression )
23675 static | dynamic | guided | runtime | auto */
23678 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23682 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23685 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23687 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23689 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23690 const char *p = IDENTIFIER_POINTER (id);
23695 if (strcmp ("dynamic", p) != 0)
23697 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23701 if (strcmp ("guided", p) != 0)
23703 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23707 if (strcmp ("runtime", p) != 0)
23709 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23716 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23717 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23718 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23719 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23722 cp_lexer_consume_token (parser->lexer);
23724 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23727 cp_lexer_consume_token (parser->lexer);
23729 token = cp_lexer_peek_token (parser->lexer);
23730 t = cp_parser_assignment_expression (parser, false, NULL);
23732 if (t == error_mark_node)
23734 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23735 error_at (token->location, "schedule %<runtime%> does not take "
23736 "a %<chunk_size%> parameter");
23737 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23738 error_at (token->location, "schedule %<auto%> does not take "
23739 "a %<chunk_size%> parameter");
23741 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23743 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23746 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23749 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23750 OMP_CLAUSE_CHAIN (c) = list;
23754 cp_parser_error (parser, "invalid schedule kind");
23756 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23757 /*or_comma=*/false,
23758 /*consume_paren=*/true);
23766 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23767 tree list, location_t location)
23771 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23773 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23774 OMP_CLAUSE_CHAIN (c) = list;
23778 /* Parse all OpenMP clauses. The set clauses allowed by the directive
23779 is a bitmask in MASK. Return the list of clauses found; the result
23780 of clause default goes in *pdefault. */
23783 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23784 const char *where, cp_token *pragma_tok)
23786 tree clauses = NULL;
23788 cp_token *token = NULL;
23790 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23792 pragma_omp_clause c_kind;
23793 const char *c_name;
23794 tree prev = clauses;
23796 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23797 cp_lexer_consume_token (parser->lexer);
23799 token = cp_lexer_peek_token (parser->lexer);
23800 c_kind = cp_parser_omp_clause_name (parser);
23805 case PRAGMA_OMP_CLAUSE_COLLAPSE:
23806 clauses = cp_parser_omp_clause_collapse (parser, clauses,
23808 c_name = "collapse";
23810 case PRAGMA_OMP_CLAUSE_COPYIN:
23811 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23814 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23815 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23817 c_name = "copyprivate";
23819 case PRAGMA_OMP_CLAUSE_DEFAULT:
23820 clauses = cp_parser_omp_clause_default (parser, clauses,
23822 c_name = "default";
23824 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23825 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23827 c_name = "firstprivate";
23829 case PRAGMA_OMP_CLAUSE_IF:
23830 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23833 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23834 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23836 c_name = "lastprivate";
23838 case PRAGMA_OMP_CLAUSE_NOWAIT:
23839 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23842 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23843 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23845 c_name = "num_threads";
23847 case PRAGMA_OMP_CLAUSE_ORDERED:
23848 clauses = cp_parser_omp_clause_ordered (parser, clauses,
23850 c_name = "ordered";
23852 case PRAGMA_OMP_CLAUSE_PRIVATE:
23853 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23855 c_name = "private";
23857 case PRAGMA_OMP_CLAUSE_REDUCTION:
23858 clauses = cp_parser_omp_clause_reduction (parser, clauses);
23859 c_name = "reduction";
23861 case PRAGMA_OMP_CLAUSE_SCHEDULE:
23862 clauses = cp_parser_omp_clause_schedule (parser, clauses,
23864 c_name = "schedule";
23866 case PRAGMA_OMP_CLAUSE_SHARED:
23867 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23871 case PRAGMA_OMP_CLAUSE_UNTIED:
23872 clauses = cp_parser_omp_clause_untied (parser, clauses,
23877 cp_parser_error (parser, "expected %<#pragma omp%> clause");
23881 if (((mask >> c_kind) & 1) == 0)
23883 /* Remove the invalid clause(s) from the list to avoid
23884 confusing the rest of the compiler. */
23886 error_at (token->location, "%qs is not valid for %qs", c_name, where);
23890 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23891 return finish_omp_clauses (clauses);
23898 In practice, we're also interested in adding the statement to an
23899 outer node. So it is convenient if we work around the fact that
23900 cp_parser_statement calls add_stmt. */
23903 cp_parser_begin_omp_structured_block (cp_parser *parser)
23905 unsigned save = parser->in_statement;
23907 /* Only move the values to IN_OMP_BLOCK if they weren't false.
23908 This preserves the "not within loop or switch" style error messages
23909 for nonsense cases like
23915 if (parser->in_statement)
23916 parser->in_statement = IN_OMP_BLOCK;
23922 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23924 parser->in_statement = save;
23928 cp_parser_omp_structured_block (cp_parser *parser)
23930 tree stmt = begin_omp_structured_block ();
23931 unsigned int save = cp_parser_begin_omp_structured_block (parser);
23933 cp_parser_statement (parser, NULL_TREE, false, NULL);
23935 cp_parser_end_omp_structured_block (parser, save);
23936 return finish_omp_structured_block (stmt);
23940 # pragma omp atomic new-line
23944 x binop= expr | x++ | ++x | x-- | --x
23946 +, *, -, /, &, ^, |, <<, >>
23948 where x is an lvalue expression with scalar type. */
23951 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23954 enum tree_code code;
23956 cp_parser_require_pragma_eol (parser, pragma_tok);
23958 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23959 /*cast_p=*/false, NULL);
23960 switch (TREE_CODE (lhs))
23965 case PREINCREMENT_EXPR:
23966 case POSTINCREMENT_EXPR:
23967 lhs = TREE_OPERAND (lhs, 0);
23969 rhs = integer_one_node;
23972 case PREDECREMENT_EXPR:
23973 case POSTDECREMENT_EXPR:
23974 lhs = TREE_OPERAND (lhs, 0);
23976 rhs = integer_one_node;
23979 case COMPOUND_EXPR:
23980 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23981 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23982 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23983 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23984 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23985 (TREE_OPERAND (lhs, 1), 0), 0)))
23987 /* Undo effects of boolean_increment for post {in,de}crement. */
23988 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
23991 if (TREE_CODE (lhs) == MODIFY_EXPR
23992 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
23994 /* Undo effects of boolean_increment. */
23995 if (integer_onep (TREE_OPERAND (lhs, 1)))
23997 /* This is pre or post increment. */
23998 rhs = TREE_OPERAND (lhs, 1);
23999 lhs = TREE_OPERAND (lhs, 0);
24006 switch (cp_lexer_peek_token (parser->lexer)->type)
24012 code = TRUNC_DIV_EXPR;
24020 case CPP_LSHIFT_EQ:
24021 code = LSHIFT_EXPR;
24023 case CPP_RSHIFT_EQ:
24024 code = RSHIFT_EXPR;
24027 code = BIT_AND_EXPR;
24030 code = BIT_IOR_EXPR;
24033 code = BIT_XOR_EXPR;
24036 cp_parser_error (parser,
24037 "invalid operator for %<#pragma omp atomic%>");
24040 cp_lexer_consume_token (parser->lexer);
24042 rhs = cp_parser_expression (parser, false, NULL);
24043 if (rhs == error_mark_node)
24047 finish_omp_atomic (code, lhs, rhs);
24048 cp_parser_consume_semicolon_at_end_of_statement (parser);
24052 cp_parser_skip_to_end_of_block_or_statement (parser);
24057 # pragma omp barrier new-line */
24060 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24062 cp_parser_require_pragma_eol (parser, pragma_tok);
24063 finish_omp_barrier ();
24067 # pragma omp critical [(name)] new-line
24068 structured-block */
24071 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24073 tree stmt, name = NULL;
24075 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24077 cp_lexer_consume_token (parser->lexer);
24079 name = cp_parser_identifier (parser);
24081 if (name == error_mark_node
24082 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24083 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24084 /*or_comma=*/false,
24085 /*consume_paren=*/true);
24086 if (name == error_mark_node)
24089 cp_parser_require_pragma_eol (parser, pragma_tok);
24091 stmt = cp_parser_omp_structured_block (parser);
24092 return c_finish_omp_critical (input_location, stmt, name);
24096 # pragma omp flush flush-vars[opt] new-line
24099 ( variable-list ) */
24102 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24104 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24105 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24106 cp_parser_require_pragma_eol (parser, pragma_tok);
24108 finish_omp_flush ();
24111 /* Helper function, to parse omp for increment expression. */
24114 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24116 tree cond = cp_parser_binary_expression (parser, false, true,
24117 PREC_NOT_OPERATOR, NULL);
24120 if (cond == error_mark_node
24121 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24123 cp_parser_skip_to_end_of_statement (parser);
24124 return error_mark_node;
24127 switch (TREE_CODE (cond))
24135 return error_mark_node;
24138 /* If decl is an iterator, preserve LHS and RHS of the relational
24139 expr until finish_omp_for. */
24141 && (type_dependent_expression_p (decl)
24142 || CLASS_TYPE_P (TREE_TYPE (decl))))
24145 return build_x_binary_op (TREE_CODE (cond),
24146 TREE_OPERAND (cond, 0), ERROR_MARK,
24147 TREE_OPERAND (cond, 1), ERROR_MARK,
24148 &overloaded_p, tf_warning_or_error);
24151 /* Helper function, to parse omp for increment expression. */
24154 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24156 cp_token *token = cp_lexer_peek_token (parser->lexer);
24162 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24164 op = (token->type == CPP_PLUS_PLUS
24165 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24166 cp_lexer_consume_token (parser->lexer);
24167 lhs = cp_parser_cast_expression (parser, false, false, NULL);
24169 return error_mark_node;
24170 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24173 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24175 return error_mark_node;
24177 token = cp_lexer_peek_token (parser->lexer);
24178 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24180 op = (token->type == CPP_PLUS_PLUS
24181 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24182 cp_lexer_consume_token (parser->lexer);
24183 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24186 op = cp_parser_assignment_operator_opt (parser);
24187 if (op == ERROR_MARK)
24188 return error_mark_node;
24190 if (op != NOP_EXPR)
24192 rhs = cp_parser_assignment_expression (parser, false, NULL);
24193 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24194 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24197 lhs = cp_parser_binary_expression (parser, false, false,
24198 PREC_ADDITIVE_EXPRESSION, NULL);
24199 token = cp_lexer_peek_token (parser->lexer);
24200 decl_first = lhs == decl;
24203 if (token->type != CPP_PLUS
24204 && token->type != CPP_MINUS)
24205 return error_mark_node;
24209 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24210 cp_lexer_consume_token (parser->lexer);
24211 rhs = cp_parser_binary_expression (parser, false, false,
24212 PREC_ADDITIVE_EXPRESSION, NULL);
24213 token = cp_lexer_peek_token (parser->lexer);
24214 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24216 if (lhs == NULL_TREE)
24218 if (op == PLUS_EXPR)
24221 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24224 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24225 NULL, tf_warning_or_error);
24228 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24232 if (rhs != decl || op == MINUS_EXPR)
24233 return error_mark_node;
24234 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24237 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24239 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24242 /* Parse the restricted form of the for statement allowed by OpenMP. */
24245 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24247 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24248 tree real_decl, initv, condv, incrv, declv;
24249 tree this_pre_body, cl;
24250 location_t loc_first;
24251 bool collapse_err = false;
24252 int i, collapse = 1, nbraces = 0;
24253 VEC(tree,gc) *for_block = make_tree_vector ();
24255 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24256 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24257 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24259 gcc_assert (collapse >= 1);
24261 declv = make_tree_vec (collapse);
24262 initv = make_tree_vec (collapse);
24263 condv = make_tree_vec (collapse);
24264 incrv = make_tree_vec (collapse);
24266 loc_first = cp_lexer_peek_token (parser->lexer)->location;
24268 for (i = 0; i < collapse; i++)
24270 int bracecount = 0;
24271 bool add_private_clause = false;
24274 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24276 cp_parser_error (parser, "for statement expected");
24279 loc = cp_lexer_consume_token (parser->lexer)->location;
24281 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24284 init = decl = real_decl = NULL;
24285 this_pre_body = push_stmt_list ();
24286 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24288 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24292 integer-type var = lb
24293 random-access-iterator-type var = lb
24294 pointer-type var = lb
24296 cp_decl_specifier_seq type_specifiers;
24298 /* First, try to parse as an initialized declaration. See
24299 cp_parser_condition, from whence the bulk of this is copied. */
24301 cp_parser_parse_tentatively (parser);
24302 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24303 /*is_trailing_return=*/false,
24305 if (cp_parser_parse_definitely (parser))
24307 /* If parsing a type specifier seq succeeded, then this
24308 MUST be a initialized declaration. */
24309 tree asm_specification, attributes;
24310 cp_declarator *declarator;
24312 declarator = cp_parser_declarator (parser,
24313 CP_PARSER_DECLARATOR_NAMED,
24314 /*ctor_dtor_or_conv_p=*/NULL,
24315 /*parenthesized_p=*/NULL,
24316 /*member_p=*/false);
24317 attributes = cp_parser_attributes_opt (parser);
24318 asm_specification = cp_parser_asm_specification_opt (parser);
24320 if (declarator == cp_error_declarator)
24321 cp_parser_skip_to_end_of_statement (parser);
24325 tree pushed_scope, auto_node;
24327 decl = start_decl (declarator, &type_specifiers,
24328 SD_INITIALIZED, attributes,
24329 /*prefix_attributes=*/NULL_TREE,
24332 auto_node = type_uses_auto (TREE_TYPE (decl));
24333 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24335 if (cp_lexer_next_token_is (parser->lexer,
24337 error ("parenthesized initialization is not allowed in "
24338 "OpenMP %<for%> loop");
24340 /* Trigger an error. */
24341 cp_parser_require (parser, CPP_EQ, RT_EQ);
24343 init = error_mark_node;
24344 cp_parser_skip_to_end_of_statement (parser);
24346 else if (CLASS_TYPE_P (TREE_TYPE (decl))
24347 || type_dependent_expression_p (decl)
24350 bool is_direct_init, is_non_constant_init;
24352 init = cp_parser_initializer (parser,
24354 &is_non_constant_init);
24356 if (auto_node && describable_type (init))
24359 = do_auto_deduction (TREE_TYPE (decl), init,
24362 if (!CLASS_TYPE_P (TREE_TYPE (decl))
24363 && !type_dependent_expression_p (decl))
24367 cp_finish_decl (decl, init, !is_non_constant_init,
24369 LOOKUP_ONLYCONVERTING);
24370 if (CLASS_TYPE_P (TREE_TYPE (decl)))
24372 VEC_safe_push (tree, gc, for_block, this_pre_body);
24376 init = pop_stmt_list (this_pre_body);
24377 this_pre_body = NULL_TREE;
24382 cp_lexer_consume_token (parser->lexer);
24383 init = cp_parser_assignment_expression (parser, false, NULL);
24386 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24387 init = error_mark_node;
24389 cp_finish_decl (decl, NULL_TREE,
24390 /*init_const_expr_p=*/false,
24392 LOOKUP_ONLYCONVERTING);
24396 pop_scope (pushed_scope);
24402 /* If parsing a type specifier sequence failed, then
24403 this MUST be a simple expression. */
24404 cp_parser_parse_tentatively (parser);
24405 decl = cp_parser_primary_expression (parser, false, false,
24407 if (!cp_parser_error_occurred (parser)
24410 && CLASS_TYPE_P (TREE_TYPE (decl)))
24414 cp_parser_parse_definitely (parser);
24415 cp_parser_require (parser, CPP_EQ, RT_EQ);
24416 rhs = cp_parser_assignment_expression (parser, false, NULL);
24417 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24419 tf_warning_or_error));
24420 add_private_clause = true;
24425 cp_parser_abort_tentative_parse (parser);
24426 init = cp_parser_expression (parser, false, NULL);
24429 if (TREE_CODE (init) == MODIFY_EXPR
24430 || TREE_CODE (init) == MODOP_EXPR)
24431 real_decl = TREE_OPERAND (init, 0);
24436 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24439 this_pre_body = pop_stmt_list (this_pre_body);
24443 pre_body = push_stmt_list ();
24445 add_stmt (this_pre_body);
24446 pre_body = pop_stmt_list (pre_body);
24449 pre_body = this_pre_body;
24454 if (par_clauses != NULL && real_decl != NULL_TREE)
24457 for (c = par_clauses; *c ; )
24458 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24459 && OMP_CLAUSE_DECL (*c) == real_decl)
24461 error_at (loc, "iteration variable %qD"
24462 " should not be firstprivate", real_decl);
24463 *c = OMP_CLAUSE_CHAIN (*c);
24465 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24466 && OMP_CLAUSE_DECL (*c) == real_decl)
24468 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24469 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
24470 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24471 OMP_CLAUSE_DECL (l) = real_decl;
24472 OMP_CLAUSE_CHAIN (l) = clauses;
24473 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24475 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24476 CP_OMP_CLAUSE_INFO (*c) = NULL;
24477 add_private_clause = false;
24481 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24482 && OMP_CLAUSE_DECL (*c) == real_decl)
24483 add_private_clause = false;
24484 c = &OMP_CLAUSE_CHAIN (*c);
24488 if (add_private_clause)
24491 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24493 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24494 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24495 && OMP_CLAUSE_DECL (c) == decl)
24497 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24498 && OMP_CLAUSE_DECL (c) == decl)
24499 error_at (loc, "iteration variable %qD "
24500 "should not be firstprivate",
24502 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24503 && OMP_CLAUSE_DECL (c) == decl)
24504 error_at (loc, "iteration variable %qD should not be reduction",
24509 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24510 OMP_CLAUSE_DECL (c) = decl;
24511 c = finish_omp_clauses (c);
24514 OMP_CLAUSE_CHAIN (c) = clauses;
24521 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24522 cond = cp_parser_omp_for_cond (parser, decl);
24523 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24526 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24528 /* If decl is an iterator, preserve the operator on decl
24529 until finish_omp_for. */
24531 && (type_dependent_expression_p (decl)
24532 || CLASS_TYPE_P (TREE_TYPE (decl))))
24533 incr = cp_parser_omp_for_incr (parser, decl);
24535 incr = cp_parser_expression (parser, false, NULL);
24538 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24539 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24540 /*or_comma=*/false,
24541 /*consume_paren=*/true);
24543 TREE_VEC_ELT (declv, i) = decl;
24544 TREE_VEC_ELT (initv, i) = init;
24545 TREE_VEC_ELT (condv, i) = cond;
24546 TREE_VEC_ELT (incrv, i) = incr;
24548 if (i == collapse - 1)
24551 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24552 in between the collapsed for loops to be still considered perfectly
24553 nested. Hopefully the final version clarifies this.
24554 For now handle (multiple) {'s and empty statements. */
24555 cp_parser_parse_tentatively (parser);
24558 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24560 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24562 cp_lexer_consume_token (parser->lexer);
24565 else if (bracecount
24566 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24567 cp_lexer_consume_token (parser->lexer);
24570 loc = cp_lexer_peek_token (parser->lexer)->location;
24571 error_at (loc, "not enough collapsed for loops");
24572 collapse_err = true;
24573 cp_parser_abort_tentative_parse (parser);
24582 cp_parser_parse_definitely (parser);
24583 nbraces += bracecount;
24587 /* Note that we saved the original contents of this flag when we entered
24588 the structured block, and so we don't need to re-save it here. */
24589 parser->in_statement = IN_OMP_FOR;
24591 /* Note that the grammar doesn't call for a structured block here,
24592 though the loop as a whole is a structured block. */
24593 body = push_stmt_list ();
24594 cp_parser_statement (parser, NULL_TREE, false, NULL);
24595 body = pop_stmt_list (body);
24597 if (declv == NULL_TREE)
24600 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24601 pre_body, clauses);
24605 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24607 cp_lexer_consume_token (parser->lexer);
24610 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24611 cp_lexer_consume_token (parser->lexer);
24616 error_at (cp_lexer_peek_token (parser->lexer)->location,
24617 "collapsed loops not perfectly nested");
24619 collapse_err = true;
24620 cp_parser_statement_seq_opt (parser, NULL);
24621 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24626 while (!VEC_empty (tree, for_block))
24627 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24628 release_tree_vector (for_block);
24634 #pragma omp for for-clause[optseq] new-line
24637 #define OMP_FOR_CLAUSE_MASK \
24638 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24639 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24640 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24641 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24642 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
24643 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
24644 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
24645 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24648 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24650 tree clauses, sb, ret;
24653 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24654 "#pragma omp for", pragma_tok);
24656 sb = begin_omp_structured_block ();
24657 save = cp_parser_begin_omp_structured_block (parser);
24659 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24661 cp_parser_end_omp_structured_block (parser, save);
24662 add_stmt (finish_omp_structured_block (sb));
24668 # pragma omp master new-line
24669 structured-block */
24672 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24674 cp_parser_require_pragma_eol (parser, pragma_tok);
24675 return c_finish_omp_master (input_location,
24676 cp_parser_omp_structured_block (parser));
24680 # pragma omp ordered new-line
24681 structured-block */
24684 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24686 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24687 cp_parser_require_pragma_eol (parser, pragma_tok);
24688 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24694 { section-sequence }
24697 section-directive[opt] structured-block
24698 section-sequence section-directive structured-block */
24701 cp_parser_omp_sections_scope (cp_parser *parser)
24703 tree stmt, substmt;
24704 bool error_suppress = false;
24707 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24710 stmt = push_stmt_list ();
24712 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24716 substmt = begin_omp_structured_block ();
24717 save = cp_parser_begin_omp_structured_block (parser);
24721 cp_parser_statement (parser, NULL_TREE, false, NULL);
24723 tok = cp_lexer_peek_token (parser->lexer);
24724 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24726 if (tok->type == CPP_CLOSE_BRACE)
24728 if (tok->type == CPP_EOF)
24732 cp_parser_end_omp_structured_block (parser, save);
24733 substmt = finish_omp_structured_block (substmt);
24734 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24735 add_stmt (substmt);
24740 tok = cp_lexer_peek_token (parser->lexer);
24741 if (tok->type == CPP_CLOSE_BRACE)
24743 if (tok->type == CPP_EOF)
24746 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24748 cp_lexer_consume_token (parser->lexer);
24749 cp_parser_require_pragma_eol (parser, tok);
24750 error_suppress = false;
24752 else if (!error_suppress)
24754 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24755 error_suppress = true;
24758 substmt = cp_parser_omp_structured_block (parser);
24759 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24760 add_stmt (substmt);
24762 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24764 substmt = pop_stmt_list (stmt);
24766 stmt = make_node (OMP_SECTIONS);
24767 TREE_TYPE (stmt) = void_type_node;
24768 OMP_SECTIONS_BODY (stmt) = substmt;
24775 # pragma omp sections sections-clause[optseq] newline
24778 #define OMP_SECTIONS_CLAUSE_MASK \
24779 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24780 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24781 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24782 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24783 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24786 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24790 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24791 "#pragma omp sections", pragma_tok);
24793 ret = cp_parser_omp_sections_scope (parser);
24795 OMP_SECTIONS_CLAUSES (ret) = clauses;
24801 # pragma parallel parallel-clause new-line
24802 # pragma parallel for parallel-for-clause new-line
24803 # pragma parallel sections parallel-sections-clause new-line */
24805 #define OMP_PARALLEL_CLAUSE_MASK \
24806 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24807 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24808 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24809 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24810 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
24811 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
24812 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24813 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24816 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24818 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24819 const char *p_name = "#pragma omp parallel";
24820 tree stmt, clauses, par_clause, ws_clause, block;
24821 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24823 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24825 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24827 cp_lexer_consume_token (parser->lexer);
24828 p_kind = PRAGMA_OMP_PARALLEL_FOR;
24829 p_name = "#pragma omp parallel for";
24830 mask |= OMP_FOR_CLAUSE_MASK;
24831 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24833 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24835 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24836 const char *p = IDENTIFIER_POINTER (id);
24837 if (strcmp (p, "sections") == 0)
24839 cp_lexer_consume_token (parser->lexer);
24840 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24841 p_name = "#pragma omp parallel sections";
24842 mask |= OMP_SECTIONS_CLAUSE_MASK;
24843 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24847 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24848 block = begin_omp_parallel ();
24849 save = cp_parser_begin_omp_structured_block (parser);
24853 case PRAGMA_OMP_PARALLEL:
24854 cp_parser_statement (parser, NULL_TREE, false, NULL);
24855 par_clause = clauses;
24858 case PRAGMA_OMP_PARALLEL_FOR:
24859 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24860 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24863 case PRAGMA_OMP_PARALLEL_SECTIONS:
24864 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24865 stmt = cp_parser_omp_sections_scope (parser);
24867 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24871 gcc_unreachable ();
24874 cp_parser_end_omp_structured_block (parser, save);
24875 stmt = finish_omp_parallel (par_clause, block);
24876 if (p_kind != PRAGMA_OMP_PARALLEL)
24877 OMP_PARALLEL_COMBINED (stmt) = 1;
24882 # pragma omp single single-clause[optseq] new-line
24883 structured-block */
24885 #define OMP_SINGLE_CLAUSE_MASK \
24886 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24887 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24888 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24889 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24892 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24894 tree stmt = make_node (OMP_SINGLE);
24895 TREE_TYPE (stmt) = void_type_node;
24897 OMP_SINGLE_CLAUSES (stmt)
24898 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24899 "#pragma omp single", pragma_tok);
24900 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24902 return add_stmt (stmt);
24906 # pragma omp task task-clause[optseq] new-line
24907 structured-block */
24909 #define OMP_TASK_CLAUSE_MASK \
24910 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24911 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
24912 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24913 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24914 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24915 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24918 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24920 tree clauses, block;
24923 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24924 "#pragma omp task", pragma_tok);
24925 block = begin_omp_task ();
24926 save = cp_parser_begin_omp_structured_block (parser);
24927 cp_parser_statement (parser, NULL_TREE, false, NULL);
24928 cp_parser_end_omp_structured_block (parser, save);
24929 return finish_omp_task (clauses, block);
24933 # pragma omp taskwait new-line */
24936 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24938 cp_parser_require_pragma_eol (parser, pragma_tok);
24939 finish_omp_taskwait ();
24943 # pragma omp threadprivate (variable-list) */
24946 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24950 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24951 cp_parser_require_pragma_eol (parser, pragma_tok);
24953 finish_omp_threadprivate (vars);
24956 /* Main entry point to OpenMP statement pragmas. */
24959 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24963 switch (pragma_tok->pragma_kind)
24965 case PRAGMA_OMP_ATOMIC:
24966 cp_parser_omp_atomic (parser, pragma_tok);
24968 case PRAGMA_OMP_CRITICAL:
24969 stmt = cp_parser_omp_critical (parser, pragma_tok);
24971 case PRAGMA_OMP_FOR:
24972 stmt = cp_parser_omp_for (parser, pragma_tok);
24974 case PRAGMA_OMP_MASTER:
24975 stmt = cp_parser_omp_master (parser, pragma_tok);
24977 case PRAGMA_OMP_ORDERED:
24978 stmt = cp_parser_omp_ordered (parser, pragma_tok);
24980 case PRAGMA_OMP_PARALLEL:
24981 stmt = cp_parser_omp_parallel (parser, pragma_tok);
24983 case PRAGMA_OMP_SECTIONS:
24984 stmt = cp_parser_omp_sections (parser, pragma_tok);
24986 case PRAGMA_OMP_SINGLE:
24987 stmt = cp_parser_omp_single (parser, pragma_tok);
24989 case PRAGMA_OMP_TASK:
24990 stmt = cp_parser_omp_task (parser, pragma_tok);
24993 gcc_unreachable ();
24997 SET_EXPR_LOCATION (stmt, pragma_tok->location);
25002 static GTY (()) cp_parser *the_parser;
25005 /* Special handling for the first token or line in the file. The first
25006 thing in the file might be #pragma GCC pch_preprocess, which loads a
25007 PCH file, which is a GC collection point. So we need to handle this
25008 first pragma without benefit of an existing lexer structure.
25010 Always returns one token to the caller in *FIRST_TOKEN. This is
25011 either the true first token of the file, or the first token after
25012 the initial pragma. */
25015 cp_parser_initial_pragma (cp_token *first_token)
25019 cp_lexer_get_preprocessor_token (NULL, first_token);
25020 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25023 cp_lexer_get_preprocessor_token (NULL, first_token);
25024 if (first_token->type == CPP_STRING)
25026 name = first_token->u.value;
25028 cp_lexer_get_preprocessor_token (NULL, first_token);
25029 if (first_token->type != CPP_PRAGMA_EOL)
25030 error_at (first_token->location,
25031 "junk at end of %<#pragma GCC pch_preprocess%>");
25034 error_at (first_token->location, "expected string literal");
25036 /* Skip to the end of the pragma. */
25037 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25038 cp_lexer_get_preprocessor_token (NULL, first_token);
25040 /* Now actually load the PCH file. */
25042 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25044 /* Read one more token to return to our caller. We have to do this
25045 after reading the PCH file in, since its pointers have to be
25047 cp_lexer_get_preprocessor_token (NULL, first_token);
25050 /* Normal parsing of a pragma token. Here we can (and must) use the
25054 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25056 cp_token *pragma_tok;
25059 pragma_tok = cp_lexer_consume_token (parser->lexer);
25060 gcc_assert (pragma_tok->type == CPP_PRAGMA);
25061 parser->lexer->in_pragma = true;
25063 id = pragma_tok->pragma_kind;
25066 case PRAGMA_GCC_PCH_PREPROCESS:
25067 error_at (pragma_tok->location,
25068 "%<#pragma GCC pch_preprocess%> must be first");
25071 case PRAGMA_OMP_BARRIER:
25074 case pragma_compound:
25075 cp_parser_omp_barrier (parser, pragma_tok);
25078 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25079 "used in compound statements");
25086 case PRAGMA_OMP_FLUSH:
25089 case pragma_compound:
25090 cp_parser_omp_flush (parser, pragma_tok);
25093 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25094 "used in compound statements");
25101 case PRAGMA_OMP_TASKWAIT:
25104 case pragma_compound:
25105 cp_parser_omp_taskwait (parser, pragma_tok);
25108 error_at (pragma_tok->location,
25109 "%<#pragma omp taskwait%> may only be "
25110 "used in compound statements");
25117 case PRAGMA_OMP_THREADPRIVATE:
25118 cp_parser_omp_threadprivate (parser, pragma_tok);
25121 case PRAGMA_OMP_ATOMIC:
25122 case PRAGMA_OMP_CRITICAL:
25123 case PRAGMA_OMP_FOR:
25124 case PRAGMA_OMP_MASTER:
25125 case PRAGMA_OMP_ORDERED:
25126 case PRAGMA_OMP_PARALLEL:
25127 case PRAGMA_OMP_SECTIONS:
25128 case PRAGMA_OMP_SINGLE:
25129 case PRAGMA_OMP_TASK:
25130 if (context == pragma_external)
25132 cp_parser_omp_construct (parser, pragma_tok);
25135 case PRAGMA_OMP_SECTION:
25136 error_at (pragma_tok->location,
25137 "%<#pragma omp section%> may only be used in "
25138 "%<#pragma omp sections%> construct");
25142 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25143 c_invoke_pragma_handler (id);
25147 cp_parser_error (parser, "expected declaration specifiers");
25151 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25155 /* The interface the pragma parsers have to the lexer. */
25158 pragma_lex (tree *value)
25161 enum cpp_ttype ret;
25163 tok = cp_lexer_peek_token (the_parser->lexer);
25166 *value = tok->u.value;
25168 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25170 else if (ret == CPP_STRING)
25171 *value = cp_parser_string_literal (the_parser, false, false);
25174 cp_lexer_consume_token (the_parser->lexer);
25175 if (ret == CPP_KEYWORD)
25183 /* External interface. */
25185 /* Parse one entire translation unit. */
25188 c_parse_file (void)
25190 static bool already_called = false;
25192 if (already_called)
25194 sorry ("inter-module optimizations not implemented for C++");
25197 already_called = true;
25199 the_parser = cp_parser_new ();
25200 push_deferring_access_checks (flag_access_control
25201 ? dk_no_deferred : dk_no_check);
25202 cp_parser_translation_unit (the_parser);
25206 #include "gt-cp-parser.h"