2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010, 2011 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"
40 #include "tree-pretty-print.h"
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 static cp_token eof_token
=
51 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, false, false, false, 0, { NULL
}
54 /* The various kinds of non integral constant we encounter. */
55 typedef enum non_integral_constant
{
57 /* floating-point literal */
61 /* %<__FUNCTION__%> */
63 /* %<__PRETTY_FUNCTION__%> */
71 /* %<typeid%> operator */
73 /* non-constant compound literals */
81 /* an array reference */
87 /* the address of a label */
101 /* calls to overloaded operators */
105 /* a comma operator */
107 /* a call to a constructor */
109 } non_integral_constant
;
111 /* The various kinds of errors about name-lookup failing. */
112 typedef enum name_lookup_error
{
117 /* is not a class or namespace */
119 /* is not a class, namespace, or enumeration */
123 /* The various kinds of required token */
124 typedef enum required_token
{
126 RT_SEMICOLON
, /* ';' */
127 RT_OPEN_PAREN
, /* '(' */
128 RT_CLOSE_BRACE
, /* '}' */
129 RT_OPEN_BRACE
, /* '{' */
130 RT_CLOSE_SQUARE
, /* ']' */
131 RT_OPEN_SQUARE
, /* '[' */
135 RT_GREATER
, /* '>' */
137 RT_ELLIPSIS
, /* '...' */
141 RT_COLON_SCOPE
, /* ':' or '::' */
142 RT_CLOSE_PAREN
, /* ')' */
143 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
144 RT_PRAGMA_EOL
, /* end of line */
145 RT_NAME
, /* identifier */
147 /* The type is CPP_KEYWORD */
149 RT_DELETE
, /* delete */
150 RT_RETURN
, /* return */
151 RT_WHILE
, /* while */
152 RT_EXTERN
, /* extern */
153 RT_STATIC_ASSERT
, /* static_assert */
154 RT_DECLTYPE
, /* decltype */
155 RT_OPERATOR
, /* operator */
156 RT_CLASS
, /* class */
157 RT_TEMPLATE
, /* template */
158 RT_NAMESPACE
, /* namespace */
159 RT_USING
, /* using */
162 RT_CATCH
, /* catch */
163 RT_THROW
, /* throw */
164 RT_LABEL
, /* __label__ */
165 RT_AT_TRY
, /* @try */
166 RT_AT_SYNCHRONIZED
, /* @synchronized */
167 RT_AT_THROW
, /* @throw */
169 RT_SELECT
, /* selection-statement */
170 RT_INTERATION
, /* iteration-statement */
171 RT_JUMP
, /* jump-statement */
172 RT_CLASS_KEY
, /* class-key */
173 RT_CLASS_TYPENAME_TEMPLATE
/* class, typename, or template */
178 static cp_lexer
*cp_lexer_new_main
180 static cp_lexer
*cp_lexer_new_from_tokens
181 (cp_token_cache
*tokens
);
182 static void cp_lexer_destroy
184 static int cp_lexer_saving_tokens
186 static cp_token
*cp_lexer_token_at
187 (cp_lexer
*, cp_token_position
);
188 static void cp_lexer_get_preprocessor_token
189 (cp_lexer
*, cp_token
*);
190 static inline cp_token
*cp_lexer_peek_token
192 static cp_token
*cp_lexer_peek_nth_token
193 (cp_lexer
*, size_t);
194 static inline bool cp_lexer_next_token_is
195 (cp_lexer
*, enum cpp_ttype
);
196 static bool cp_lexer_next_token_is_not
197 (cp_lexer
*, enum cpp_ttype
);
198 static bool cp_lexer_next_token_is_keyword
199 (cp_lexer
*, enum rid
);
200 static cp_token
*cp_lexer_consume_token
202 static void cp_lexer_purge_token
204 static void cp_lexer_purge_tokens_after
205 (cp_lexer
*, cp_token_position
);
206 static void cp_lexer_save_tokens
208 static void cp_lexer_commit_tokens
210 static void cp_lexer_rollback_tokens
212 #ifdef ENABLE_CHECKING
213 static void cp_lexer_print_token
214 (FILE *, cp_token
*);
215 static inline bool cp_lexer_debugging_p
217 static void cp_lexer_start_debugging
218 (cp_lexer
*) ATTRIBUTE_UNUSED
;
219 static void cp_lexer_stop_debugging
220 (cp_lexer
*) ATTRIBUTE_UNUSED
;
222 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
223 about passing NULL to functions that require non-NULL arguments
224 (fputs, fprintf). It will never be used, so all we need is a value
225 of the right type that's guaranteed not to be NULL. */
226 #define cp_lexer_debug_stream stdout
227 #define cp_lexer_print_token(str, tok) (void) 0
228 #define cp_lexer_debugging_p(lexer) 0
229 #endif /* ENABLE_CHECKING */
231 static cp_token_cache
*cp_token_cache_new
232 (cp_token
*, cp_token
*);
234 static void cp_parser_initial_pragma
237 /* Manifest constants. */
238 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
239 #define CP_SAVED_TOKEN_STACK 5
243 #ifdef ENABLE_CHECKING
244 /* The stream to which debugging output should be written. */
245 static FILE *cp_lexer_debug_stream
;
246 #endif /* ENABLE_CHECKING */
248 /* Nonzero if we are parsing an unevaluated operand: an operand to
249 sizeof, typeof, or alignof. */
250 int cp_unevaluated_operand
;
252 #ifdef ENABLE_CHECKING
253 /* Dump up to NUM tokens in BUFFER to FILE. If NUM is 0, dump all the
257 cp_lexer_dump_tokens (FILE *file
, VEC(cp_token
,gc
) *buffer
, unsigned num
)
262 fprintf (file
, "%u tokens\n", VEC_length (cp_token
, buffer
));
265 num
= VEC_length (cp_token
, buffer
);
267 for (i
= 0; VEC_iterate (cp_token
, buffer
, i
, token
) && i
< num
; i
++)
269 cp_lexer_print_token (file
, token
);
274 case CPP_CLOSE_BRACE
:
284 if (i
== num
&& i
< VEC_length (cp_token
, buffer
))
286 fprintf (file
, " ... ");
287 cp_lexer_print_token (file
, VEC_index (cp_token
, buffer
,
288 VEC_length (cp_token
, buffer
) - 1));
291 fprintf (file
, "\n");
295 /* Dump all tokens in BUFFER to stderr. */
298 cp_lexer_debug_tokens (VEC(cp_token
,gc
) *buffer
)
300 cp_lexer_dump_tokens (stderr
, buffer
, 0);
305 /* Allocate memory for a new lexer object and return it. */
308 cp_lexer_alloc (void)
312 c_common_no_more_pch ();
314 /* Allocate the memory. */
315 lexer
= ggc_alloc_cleared_cp_lexer ();
317 #ifdef ENABLE_CHECKING
318 /* Initially we are not debugging. */
319 lexer
->debugging_p
= false;
320 #endif /* ENABLE_CHECKING */
321 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
322 CP_SAVED_TOKEN_STACK
);
324 /* Create the buffer. */
325 lexer
->buffer
= VEC_alloc (cp_token
, gc
, CP_LEXER_BUFFER_SIZE
);
331 /* Create a new main C++ lexer, the lexer that gets tokens from the
335 cp_lexer_new_main (void)
340 /* It's possible that parsing the first pragma will load a PCH file,
341 which is a GC collection point. So we have to do that before
342 allocating any memory. */
343 cp_parser_initial_pragma (&token
);
345 lexer
= cp_lexer_alloc ();
347 /* Put the first token in the buffer. */
348 VEC_quick_push (cp_token
, lexer
->buffer
, &token
);
350 /* Get the remaining tokens from the preprocessor. */
351 while (token
.type
!= CPP_EOF
)
353 cp_lexer_get_preprocessor_token (lexer
, &token
);
354 VEC_safe_push (cp_token
, gc
, lexer
->buffer
, &token
);
357 lexer
->last_token
= VEC_address (cp_token
, lexer
->buffer
)
358 + VEC_length (cp_token
, lexer
->buffer
)
360 lexer
->next_token
= VEC_length (cp_token
, lexer
->buffer
)
361 ? VEC_address (cp_token
, lexer
->buffer
)
364 /* Subsequent preprocessor diagnostics should use compiler
365 diagnostic functions to get the compiler source location. */
368 gcc_assert (!lexer
->next_token
->purged_p
);
372 /* Create a new lexer whose token stream is primed with the tokens in
373 CACHE. When these tokens are exhausted, no new tokens will be read. */
376 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
378 cp_token
*first
= cache
->first
;
379 cp_token
*last
= cache
->last
;
380 cp_lexer
*lexer
= ggc_alloc_cleared_cp_lexer ();
382 /* We do not own the buffer. */
383 lexer
->buffer
= NULL
;
384 lexer
->next_token
= first
== last
? &eof_token
: first
;
385 lexer
->last_token
= last
;
387 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
388 CP_SAVED_TOKEN_STACK
);
390 #ifdef ENABLE_CHECKING
391 /* Initially we are not debugging. */
392 lexer
->debugging_p
= false;
395 gcc_assert (!lexer
->next_token
->purged_p
);
399 /* Frees all resources associated with LEXER. */
402 cp_lexer_destroy (cp_lexer
*lexer
)
404 VEC_free (cp_token
, gc
, lexer
->buffer
);
405 VEC_free (cp_token_position
, heap
, lexer
->saved_tokens
);
409 /* Returns nonzero if debugging information should be output. */
411 #ifdef ENABLE_CHECKING
414 cp_lexer_debugging_p (cp_lexer
*lexer
)
416 return lexer
->debugging_p
;
419 #endif /* ENABLE_CHECKING */
421 static inline cp_token_position
422 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
424 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
426 return lexer
->next_token
- previous_p
;
429 static inline cp_token
*
430 cp_lexer_token_at (cp_lexer
*lexer ATTRIBUTE_UNUSED
, cp_token_position pos
)
436 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
438 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
441 static inline cp_token_position
442 cp_lexer_previous_token_position (cp_lexer
*lexer
)
444 if (lexer
->next_token
== &eof_token
)
445 return lexer
->last_token
- 1;
447 return cp_lexer_token_position (lexer
, true);
450 static inline cp_token
*
451 cp_lexer_previous_token (cp_lexer
*lexer
)
453 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
455 return cp_lexer_token_at (lexer
, tp
);
458 /* nonzero if we are presently saving tokens. */
461 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
463 return VEC_length (cp_token_position
, lexer
->saved_tokens
) != 0;
466 /* Store the next token from the preprocessor in *TOKEN. Return true
467 if we reach EOF. If LEXER is NULL, assume we are handling an
468 initial #pragma pch_preprocess, and thus want the lexer to return
469 processed strings. */
472 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
474 static int is_extern_c
= 0;
476 /* Get a new token from the preprocessor. */
478 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
479 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
480 token
->keyword
= RID_MAX
;
481 token
->pragma_kind
= PRAGMA_NONE
;
482 token
->purged_p
= false;
484 /* On some systems, some header files are surrounded by an
485 implicit extern "C" block. Set a flag in the token if it
486 comes from such a header. */
487 is_extern_c
+= pending_lang_change
;
488 pending_lang_change
= 0;
489 token
->implicit_extern_c
= is_extern_c
> 0;
491 /* Check to see if this token is a keyword. */
492 if (token
->type
== CPP_NAME
)
494 if (C_IS_RESERVED_WORD (token
->u
.value
))
496 /* Mark this token as a keyword. */
497 token
->type
= CPP_KEYWORD
;
498 /* Record which keyword. */
499 token
->keyword
= C_RID_CODE (token
->u
.value
);
503 if (warn_cxx0x_compat
504 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
505 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
507 /* Warn about the C++0x keyword (but still treat it as
509 warning (OPT_Wc__0x_compat
,
510 "identifier %qE will become a keyword in C++0x",
513 /* Clear out the C_RID_CODE so we don't warn about this
514 particular identifier-turned-keyword again. */
515 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
518 token
->ambiguous_p
= false;
519 token
->keyword
= RID_MAX
;
522 else if (token
->type
== CPP_AT_NAME
)
524 /* This only happens in Objective-C++; it must be a keyword. */
525 token
->type
= CPP_KEYWORD
;
526 switch (C_RID_CODE (token
->u
.value
))
528 /* Replace 'class' with '@class', 'private' with '@private',
529 etc. This prevents confusion with the C++ keyword
530 'class', and makes the tokens consistent with other
531 Objective-C 'AT' keywords. For example '@class' is
532 reported as RID_AT_CLASS which is consistent with
533 '@synchronized', which is reported as
536 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
537 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
538 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
539 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
540 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
541 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
542 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
543 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
546 else if (token
->type
== CPP_PRAGMA
)
548 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
549 token
->pragma_kind
= ((enum pragma_kind
)
550 TREE_INT_CST_LOW (token
->u
.value
));
551 token
->u
.value
= NULL_TREE
;
555 /* Update the globals input_location and the input file stack from TOKEN. */
557 cp_lexer_set_source_position_from_token (cp_token
*token
)
559 if (token
->type
!= CPP_EOF
)
561 input_location
= token
->location
;
565 /* Return a pointer to the next token in the token stream, but do not
568 static inline cp_token
*
569 cp_lexer_peek_token (cp_lexer
*lexer
)
571 if (cp_lexer_debugging_p (lexer
))
573 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
574 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
575 putc ('\n', cp_lexer_debug_stream
);
577 return lexer
->next_token
;
580 /* Return true if the next token has the indicated TYPE. */
583 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
585 return cp_lexer_peek_token (lexer
)->type
== type
;
588 /* Return true if the next token does not have the indicated TYPE. */
591 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
593 return !cp_lexer_next_token_is (lexer
, type
);
596 /* Return true if the next token is the indicated KEYWORD. */
599 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
601 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
604 /* Return true if the next token is not the indicated KEYWORD. */
607 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
609 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
612 /* Return true if the next token is a keyword for a decl-specifier. */
615 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
619 token
= cp_lexer_peek_token (lexer
);
620 switch (token
->keyword
)
622 /* auto specifier: storage-class-specifier in C++,
623 simple-type-specifier in C++0x. */
625 /* Storage classes. */
631 /* Elaborated type specifiers. */
637 /* Simple type specifiers. */
652 /* GNU extensions. */
655 /* C++0x extensions. */
657 case RID_UNDERLYING_TYPE
:
665 /* Return a pointer to the Nth token in the token stream. If N is 1,
666 then this is precisely equivalent to cp_lexer_peek_token (except
667 that it is not inline). One would like to disallow that case, but
668 there is one case (cp_parser_nth_token_starts_template_id) where
669 the caller passes a variable for N and it might be 1. */
672 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
676 /* N is 1-based, not zero-based. */
679 if (cp_lexer_debugging_p (lexer
))
680 fprintf (cp_lexer_debug_stream
,
681 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
684 token
= lexer
->next_token
;
685 gcc_assert (!n
|| token
!= &eof_token
);
689 if (token
== lexer
->last_token
)
695 if (!token
->purged_p
)
699 if (cp_lexer_debugging_p (lexer
))
701 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
702 putc ('\n', cp_lexer_debug_stream
);
708 /* Return the next token, and advance the lexer's next_token pointer
709 to point to the next non-purged token. */
712 cp_lexer_consume_token (cp_lexer
* lexer
)
714 cp_token
*token
= lexer
->next_token
;
716 gcc_assert (token
!= &eof_token
);
717 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
722 if (lexer
->next_token
== lexer
->last_token
)
724 lexer
->next_token
= &eof_token
;
729 while (lexer
->next_token
->purged_p
);
731 cp_lexer_set_source_position_from_token (token
);
733 /* Provide debugging output. */
734 if (cp_lexer_debugging_p (lexer
))
736 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
737 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
738 putc ('\n', cp_lexer_debug_stream
);
744 /* Permanently remove the next token from the token stream, and
745 advance the next_token pointer to refer to the next non-purged
749 cp_lexer_purge_token (cp_lexer
*lexer
)
751 cp_token
*tok
= lexer
->next_token
;
753 gcc_assert (tok
!= &eof_token
);
754 tok
->purged_p
= true;
755 tok
->location
= UNKNOWN_LOCATION
;
756 tok
->u
.value
= NULL_TREE
;
757 tok
->keyword
= RID_MAX
;
762 if (tok
== lexer
->last_token
)
768 while (tok
->purged_p
);
769 lexer
->next_token
= tok
;
772 /* Permanently remove all tokens after TOK, up to, but not
773 including, the token that will be returned next by
774 cp_lexer_peek_token. */
777 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
779 cp_token
*peek
= lexer
->next_token
;
781 if (peek
== &eof_token
)
782 peek
= lexer
->last_token
;
784 gcc_assert (tok
< peek
);
786 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
788 tok
->purged_p
= true;
789 tok
->location
= UNKNOWN_LOCATION
;
790 tok
->u
.value
= NULL_TREE
;
791 tok
->keyword
= RID_MAX
;
795 /* Begin saving tokens. All tokens consumed after this point will be
799 cp_lexer_save_tokens (cp_lexer
* lexer
)
801 /* Provide debugging output. */
802 if (cp_lexer_debugging_p (lexer
))
803 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
805 VEC_safe_push (cp_token_position
, heap
,
806 lexer
->saved_tokens
, lexer
->next_token
);
809 /* Commit to the portion of the token stream most recently saved. */
812 cp_lexer_commit_tokens (cp_lexer
* lexer
)
814 /* Provide debugging output. */
815 if (cp_lexer_debugging_p (lexer
))
816 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
818 VEC_pop (cp_token_position
, lexer
->saved_tokens
);
821 /* Return all tokens saved since the last call to cp_lexer_save_tokens
822 to the token stream. Stop saving tokens. */
825 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
827 /* Provide debugging output. */
828 if (cp_lexer_debugging_p (lexer
))
829 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
831 lexer
->next_token
= VEC_pop (cp_token_position
, lexer
->saved_tokens
);
834 /* Print a representation of the TOKEN on the STREAM. */
836 #ifdef ENABLE_CHECKING
839 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
841 /* We don't use cpp_type2name here because the parser defines
842 a few tokens of its own. */
843 static const char *const token_names
[] = {
844 /* cpplib-defined token types */
850 /* C++ parser token types - see "Manifest constants", above. */
853 "NESTED_NAME_SPECIFIER",
856 /* For some tokens, print the associated data. */
860 /* Some keywords have a value that is not an IDENTIFIER_NODE.
861 For example, `struct' is mapped to an INTEGER_CST. */
862 if (TREE_CODE (token
->u
.value
) != IDENTIFIER_NODE
)
864 /* else fall through */
866 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
874 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
878 print_generic_expr (stream
, token
->u
.value
, 0);
882 /* If we have a name for the token, print it out. Otherwise, we
883 simply give the numeric code. */
884 if (token
->type
< ARRAY_SIZE(token_names
))
885 fputs (token_names
[token
->type
], stream
);
887 fprintf (stream
, "[%d]", token
->type
);
892 /* Start emitting debugging information. */
895 cp_lexer_start_debugging (cp_lexer
* lexer
)
897 lexer
->debugging_p
= true;
900 /* Stop emitting debugging information. */
903 cp_lexer_stop_debugging (cp_lexer
* lexer
)
905 lexer
->debugging_p
= false;
908 #endif /* ENABLE_CHECKING */
910 /* Create a new cp_token_cache, representing a range of tokens. */
912 static cp_token_cache
*
913 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
915 cp_token_cache
*cache
= ggc_alloc_cp_token_cache ();
916 cache
->first
= first
;
922 /* Decl-specifiers. */
924 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
927 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
929 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
934 /* Nothing other than the parser should be creating declarators;
935 declarators are a semi-syntactic representation of C++ entities.
936 Other parts of the front end that need to create entities (like
937 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
939 static cp_declarator
*make_call_declarator
940 (cp_declarator
*, tree
, cp_cv_quals
, tree
, tree
);
941 static cp_declarator
*make_array_declarator
942 (cp_declarator
*, tree
);
943 static cp_declarator
*make_pointer_declarator
944 (cp_cv_quals
, cp_declarator
*);
945 static cp_declarator
*make_reference_declarator
946 (cp_cv_quals
, cp_declarator
*, bool);
947 static cp_parameter_declarator
*make_parameter_declarator
948 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
949 static cp_declarator
*make_ptrmem_declarator
950 (cp_cv_quals
, tree
, cp_declarator
*);
952 /* An erroneous declarator. */
953 static cp_declarator
*cp_error_declarator
;
955 /* The obstack on which declarators and related data structures are
957 static struct obstack declarator_obstack
;
959 /* Alloc BYTES from the declarator memory pool. */
962 alloc_declarator (size_t bytes
)
964 return obstack_alloc (&declarator_obstack
, bytes
);
967 /* Allocate a declarator of the indicated KIND. Clear fields that are
968 common to all declarators. */
970 static cp_declarator
*
971 make_declarator (cp_declarator_kind kind
)
973 cp_declarator
*declarator
;
975 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
976 declarator
->kind
= kind
;
977 declarator
->attributes
= NULL_TREE
;
978 declarator
->declarator
= NULL
;
979 declarator
->parameter_pack_p
= false;
980 declarator
->id_loc
= UNKNOWN_LOCATION
;
985 /* Make a declarator for a generalized identifier. If
986 QUALIFYING_SCOPE is non-NULL, the identifier is
987 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
988 UNQUALIFIED_NAME. SFK indicates the kind of special function this
991 static cp_declarator
*
992 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
993 special_function_kind sfk
)
995 cp_declarator
*declarator
;
997 /* It is valid to write:
999 class C { void f(); };
1003 The standard is not clear about whether `typedef const C D' is
1004 legal; as of 2002-09-15 the committee is considering that
1005 question. EDG 3.0 allows that syntax. Therefore, we do as
1007 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1008 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1010 gcc_assert (TREE_CODE (unqualified_name
) == IDENTIFIER_NODE
1011 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1012 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1014 declarator
= make_declarator (cdk_id
);
1015 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1016 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1017 declarator
->u
.id
.sfk
= sfk
;
1022 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1023 of modifiers such as const or volatile to apply to the pointer
1024 type, represented as identifiers. */
1027 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
1029 cp_declarator
*declarator
;
1031 declarator
= make_declarator (cdk_pointer
);
1032 declarator
->declarator
= target
;
1033 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1034 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1037 declarator
->id_loc
= target
->id_loc
;
1038 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1039 target
->parameter_pack_p
= false;
1042 declarator
->parameter_pack_p
= false;
1047 /* Like make_pointer_declarator -- but for references. */
1050 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1053 cp_declarator
*declarator
;
1055 declarator
= make_declarator (cdk_reference
);
1056 declarator
->declarator
= target
;
1057 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1058 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1061 declarator
->id_loc
= target
->id_loc
;
1062 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1063 target
->parameter_pack_p
= false;
1066 declarator
->parameter_pack_p
= false;
1071 /* Like make_pointer_declarator -- but for a pointer to a non-static
1072 member of CLASS_TYPE. */
1075 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1076 cp_declarator
*pointee
)
1078 cp_declarator
*declarator
;
1080 declarator
= make_declarator (cdk_ptrmem
);
1081 declarator
->declarator
= pointee
;
1082 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1083 declarator
->u
.pointer
.class_type
= class_type
;
1087 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1088 pointee
->parameter_pack_p
= false;
1091 declarator
->parameter_pack_p
= false;
1096 /* Make a declarator for the function given by TARGET, with the
1097 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1098 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1099 indicates what exceptions can be thrown. */
1102 make_call_declarator (cp_declarator
*target
,
1104 cp_cv_quals cv_qualifiers
,
1105 tree exception_specification
,
1106 tree late_return_type
)
1108 cp_declarator
*declarator
;
1110 declarator
= make_declarator (cdk_function
);
1111 declarator
->declarator
= target
;
1112 declarator
->u
.function
.parameters
= parms
;
1113 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1114 declarator
->u
.function
.exception_specification
= exception_specification
;
1115 declarator
->u
.function
.late_return_type
= late_return_type
;
1118 declarator
->id_loc
= target
->id_loc
;
1119 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1120 target
->parameter_pack_p
= false;
1123 declarator
->parameter_pack_p
= false;
1128 /* Make a declarator for an array of BOUNDS elements, each of which is
1129 defined by ELEMENT. */
1132 make_array_declarator (cp_declarator
*element
, tree bounds
)
1134 cp_declarator
*declarator
;
1136 declarator
= make_declarator (cdk_array
);
1137 declarator
->declarator
= element
;
1138 declarator
->u
.array
.bounds
= bounds
;
1141 declarator
->id_loc
= element
->id_loc
;
1142 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1143 element
->parameter_pack_p
= false;
1146 declarator
->parameter_pack_p
= false;
1151 /* Determine whether the declarator we've seen so far can be a
1152 parameter pack, when followed by an ellipsis. */
1154 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1156 /* Search for a declarator name, or any other declarator that goes
1157 after the point where the ellipsis could appear in a parameter
1158 pack. If we find any of these, then this declarator can not be
1159 made into a parameter pack. */
1161 while (declarator
&& !found
)
1163 switch ((int)declarator
->kind
)
1174 declarator
= declarator
->declarator
;
1182 cp_parameter_declarator
*no_parameters
;
1184 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1185 DECLARATOR and DEFAULT_ARGUMENT. */
1187 cp_parameter_declarator
*
1188 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1189 cp_declarator
*declarator
,
1190 tree default_argument
)
1192 cp_parameter_declarator
*parameter
;
1194 parameter
= ((cp_parameter_declarator
*)
1195 alloc_declarator (sizeof (cp_parameter_declarator
)));
1196 parameter
->next
= NULL
;
1197 if (decl_specifiers
)
1198 parameter
->decl_specifiers
= *decl_specifiers
;
1200 clear_decl_specs (¶meter
->decl_specifiers
);
1201 parameter
->declarator
= declarator
;
1202 parameter
->default_argument
= default_argument
;
1203 parameter
->ellipsis_p
= false;
1208 /* Returns true iff DECLARATOR is a declaration for a function. */
1211 function_declarator_p (const cp_declarator
*declarator
)
1215 if (declarator
->kind
== cdk_function
1216 && declarator
->declarator
->kind
== cdk_id
)
1218 if (declarator
->kind
== cdk_id
1219 || declarator
->kind
== cdk_error
)
1221 declarator
= declarator
->declarator
;
1231 A cp_parser parses the token stream as specified by the C++
1232 grammar. Its job is purely parsing, not semantic analysis. For
1233 example, the parser breaks the token stream into declarators,
1234 expressions, statements, and other similar syntactic constructs.
1235 It does not check that the types of the expressions on either side
1236 of an assignment-statement are compatible, or that a function is
1237 not declared with a parameter of type `void'.
1239 The parser invokes routines elsewhere in the compiler to perform
1240 semantic analysis and to build up the abstract syntax tree for the
1243 The parser (and the template instantiation code, which is, in a
1244 way, a close relative of parsing) are the only parts of the
1245 compiler that should be calling push_scope and pop_scope, or
1246 related functions. The parser (and template instantiation code)
1247 keeps track of what scope is presently active; everything else
1248 should simply honor that. (The code that generates static
1249 initializers may also need to set the scope, in order to check
1250 access control correctly when emitting the initializers.)
1255 The parser is of the standard recursive-descent variety. Upcoming
1256 tokens in the token stream are examined in order to determine which
1257 production to use when parsing a non-terminal. Some C++ constructs
1258 require arbitrary look ahead to disambiguate. For example, it is
1259 impossible, in the general case, to tell whether a statement is an
1260 expression or declaration without scanning the entire statement.
1261 Therefore, the parser is capable of "parsing tentatively." When the
1262 parser is not sure what construct comes next, it enters this mode.
1263 Then, while we attempt to parse the construct, the parser queues up
1264 error messages, rather than issuing them immediately, and saves the
1265 tokens it consumes. If the construct is parsed successfully, the
1266 parser "commits", i.e., it issues any queued error messages and
1267 the tokens that were being preserved are permanently discarded.
1268 If, however, the construct is not parsed successfully, the parser
1269 rolls back its state completely so that it can resume parsing using
1270 a different alternative.
1275 The performance of the parser could probably be improved substantially.
1276 We could often eliminate the need to parse tentatively by looking ahead
1277 a little bit. In some places, this approach might not entirely eliminate
1278 the need to parse tentatively, but it might still speed up the average
1281 /* Flags that are passed to some parsing functions. These values can
1282 be bitwise-ored together. */
1287 CP_PARSER_FLAGS_NONE
= 0x0,
1288 /* The construct is optional. If it is not present, then no error
1289 should be issued. */
1290 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1291 /* When parsing a type-specifier, treat user-defined type-names
1292 as non-type identifiers. */
1293 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1294 /* When parsing a type-specifier, do not try to parse a class-specifier
1295 or enum-specifier. */
1296 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1297 /* When parsing a decl-specifier-seq, only allow type-specifier or
1299 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8
1302 /* This type is used for parameters and variables which hold
1303 combinations of the above flags. */
1304 typedef int cp_parser_flags
;
1306 /* The different kinds of declarators we want to parse. */
1308 typedef enum cp_parser_declarator_kind
1310 /* We want an abstract declarator. */
1311 CP_PARSER_DECLARATOR_ABSTRACT
,
1312 /* We want a named declarator. */
1313 CP_PARSER_DECLARATOR_NAMED
,
1314 /* We don't mind, but the name must be an unqualified-id. */
1315 CP_PARSER_DECLARATOR_EITHER
1316 } cp_parser_declarator_kind
;
1318 /* The precedence values used to parse binary expressions. The minimum value
1319 of PREC must be 1, because zero is reserved to quickly discriminate
1320 binary operators from other tokens. */
1325 PREC_LOGICAL_OR_EXPRESSION
,
1326 PREC_LOGICAL_AND_EXPRESSION
,
1327 PREC_INCLUSIVE_OR_EXPRESSION
,
1328 PREC_EXCLUSIVE_OR_EXPRESSION
,
1329 PREC_AND_EXPRESSION
,
1330 PREC_EQUALITY_EXPRESSION
,
1331 PREC_RELATIONAL_EXPRESSION
,
1332 PREC_SHIFT_EXPRESSION
,
1333 PREC_ADDITIVE_EXPRESSION
,
1334 PREC_MULTIPLICATIVE_EXPRESSION
,
1336 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1339 /* A mapping from a token type to a corresponding tree node type, with a
1340 precedence value. */
1342 typedef struct cp_parser_binary_operations_map_node
1344 /* The token type. */
1345 enum cpp_ttype token_type
;
1346 /* The corresponding tree code. */
1347 enum tree_code tree_type
;
1348 /* The precedence of this operator. */
1349 enum cp_parser_prec prec
;
1350 } cp_parser_binary_operations_map_node
;
1352 typedef struct cp_parser_expression_stack_entry
1354 /* Left hand side of the binary operation we are currently
1357 /* Original tree code for left hand side, if it was a binary
1358 expression itself (used for -Wparentheses). */
1359 enum tree_code lhs_type
;
1360 /* Tree code for the binary operation we are parsing. */
1361 enum tree_code tree_type
;
1362 /* Precedence of the binary operation we are parsing. */
1363 enum cp_parser_prec prec
;
1364 } cp_parser_expression_stack_entry
;
1366 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1367 entries because precedence levels on the stack are monotonically
1369 typedef struct cp_parser_expression_stack_entry
1370 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1374 /* Constructors and destructors. */
1376 static cp_parser_context
*cp_parser_context_new
1377 (cp_parser_context
*);
1379 /* Class variables. */
1381 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1383 /* The operator-precedence table used by cp_parser_binary_expression.
1384 Transformed into an associative array (binops_by_token) by
1387 static const cp_parser_binary_operations_map_node binops
[] = {
1388 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1389 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1391 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1392 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1393 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1395 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1396 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1398 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1399 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1401 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1402 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1403 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1404 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1406 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1407 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1409 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1411 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1413 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1415 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1417 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1420 /* The same as binops, but initialized by cp_parser_new so that
1421 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1423 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1425 /* Constructors and destructors. */
1427 /* Construct a new context. The context below this one on the stack
1428 is given by NEXT. */
1430 static cp_parser_context
*
1431 cp_parser_context_new (cp_parser_context
* next
)
1433 cp_parser_context
*context
;
1435 /* Allocate the storage. */
1436 if (cp_parser_context_free_list
!= NULL
)
1438 /* Pull the first entry from the free list. */
1439 context
= cp_parser_context_free_list
;
1440 cp_parser_context_free_list
= context
->next
;
1441 memset (context
, 0, sizeof (*context
));
1444 context
= ggc_alloc_cleared_cp_parser_context ();
1446 /* No errors have occurred yet in this context. */
1447 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1448 /* If this is not the bottommost context, copy information that we
1449 need from the previous context. */
1452 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1453 expression, then we are parsing one in this context, too. */
1454 context
->object_type
= next
->object_type
;
1455 /* Thread the stack. */
1456 context
->next
= next
;
1462 /* Managing the unparsed function queues. */
1464 #define unparsed_funs_with_default_args \
1465 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1466 #define unparsed_funs_with_definitions \
1467 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1470 push_unparsed_function_queues (cp_parser
*parser
)
1472 VEC_safe_push (cp_unparsed_functions_entry
, gc
,
1473 parser
->unparsed_queues
, NULL
);
1474 unparsed_funs_with_default_args
= NULL
;
1475 unparsed_funs_with_definitions
= make_tree_vector ();
1479 pop_unparsed_function_queues (cp_parser
*parser
)
1481 release_tree_vector (unparsed_funs_with_definitions
);
1482 VEC_pop (cp_unparsed_functions_entry
, parser
->unparsed_queues
);
1487 /* Constructors and destructors. */
1489 static cp_parser
*cp_parser_new
1492 /* Routines to parse various constructs.
1494 Those that return `tree' will return the error_mark_node (rather
1495 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1496 Sometimes, they will return an ordinary node if error-recovery was
1497 attempted, even though a parse error occurred. So, to check
1498 whether or not a parse error occurred, you should always use
1499 cp_parser_error_occurred. If the construct is optional (indicated
1500 either by an `_opt' in the name of the function that does the
1501 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1502 the construct is not present. */
1504 /* Lexical conventions [gram.lex] */
1506 static tree cp_parser_identifier
1508 static tree cp_parser_string_literal
1509 (cp_parser
*, bool, bool);
1511 /* Basic concepts [gram.basic] */
1513 static bool cp_parser_translation_unit
1516 /* Expressions [gram.expr] */
1518 static tree cp_parser_primary_expression
1519 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1520 static tree cp_parser_id_expression
1521 (cp_parser
*, bool, bool, bool *, bool, bool);
1522 static tree cp_parser_unqualified_id
1523 (cp_parser
*, bool, bool, bool, bool);
1524 static tree cp_parser_nested_name_specifier_opt
1525 (cp_parser
*, bool, bool, bool, bool);
1526 static tree cp_parser_nested_name_specifier
1527 (cp_parser
*, bool, bool, bool, bool);
1528 static tree cp_parser_qualifying_entity
1529 (cp_parser
*, bool, bool, bool, bool, bool);
1530 static tree cp_parser_postfix_expression
1531 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1532 static tree cp_parser_postfix_open_square_expression
1533 (cp_parser
*, tree
, bool);
1534 static tree cp_parser_postfix_dot_deref_expression
1535 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1536 static VEC(tree
,gc
) *cp_parser_parenthesized_expression_list
1537 (cp_parser
*, int, bool, bool, bool *);
1538 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1539 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1540 static void cp_parser_pseudo_destructor_name
1541 (cp_parser
*, tree
*, tree
*);
1542 static tree cp_parser_unary_expression
1543 (cp_parser
*, bool, bool, cp_id_kind
*);
1544 static enum tree_code cp_parser_unary_operator
1546 static tree cp_parser_new_expression
1548 static VEC(tree
,gc
) *cp_parser_new_placement
1550 static tree cp_parser_new_type_id
1551 (cp_parser
*, tree
*);
1552 static cp_declarator
*cp_parser_new_declarator_opt
1554 static cp_declarator
*cp_parser_direct_new_declarator
1556 static VEC(tree
,gc
) *cp_parser_new_initializer
1558 static tree cp_parser_delete_expression
1560 static tree cp_parser_cast_expression
1561 (cp_parser
*, bool, bool, cp_id_kind
*);
1562 static tree cp_parser_binary_expression
1563 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
1564 static tree cp_parser_question_colon_clause
1565 (cp_parser
*, tree
);
1566 static tree cp_parser_assignment_expression
1567 (cp_parser
*, bool, cp_id_kind
*);
1568 static enum tree_code cp_parser_assignment_operator_opt
1570 static tree cp_parser_expression
1571 (cp_parser
*, bool, cp_id_kind
*);
1572 static tree cp_parser_constant_expression
1573 (cp_parser
*, bool, bool *);
1574 static tree cp_parser_builtin_offsetof
1576 static tree cp_parser_lambda_expression
1578 static void cp_parser_lambda_introducer
1579 (cp_parser
*, tree
);
1580 static void cp_parser_lambda_declarator_opt
1581 (cp_parser
*, tree
);
1582 static void cp_parser_lambda_body
1583 (cp_parser
*, tree
);
1585 /* Statements [gram.stmt.stmt] */
1587 static void cp_parser_statement
1588 (cp_parser
*, tree
, bool, bool *);
1589 static void cp_parser_label_for_labeled_statement
1591 static tree cp_parser_expression_statement
1592 (cp_parser
*, tree
);
1593 static tree cp_parser_compound_statement
1594 (cp_parser
*, tree
, bool, bool);
1595 static void cp_parser_statement_seq_opt
1596 (cp_parser
*, tree
);
1597 static tree cp_parser_selection_statement
1598 (cp_parser
*, bool *);
1599 static tree cp_parser_condition
1601 static tree cp_parser_iteration_statement
1603 static bool cp_parser_for_init_statement
1604 (cp_parser
*, tree
*decl
);
1605 static tree cp_parser_for
1607 static tree cp_parser_c_for
1608 (cp_parser
*, tree
, tree
);
1609 static tree cp_parser_range_for
1610 (cp_parser
*, tree
, tree
, tree
);
1611 static tree cp_parser_perform_range_for_lookup
1612 (tree
, tree
*, tree
*);
1613 static tree cp_parser_range_for_member_function
1615 static tree cp_parser_jump_statement
1617 static void cp_parser_declaration_statement
1620 static tree cp_parser_implicitly_scoped_statement
1621 (cp_parser
*, bool *);
1622 static void cp_parser_already_scoped_statement
1625 /* Declarations [gram.dcl.dcl] */
1627 static void cp_parser_declaration_seq_opt
1629 static void cp_parser_declaration
1631 static void cp_parser_block_declaration
1632 (cp_parser
*, bool);
1633 static void cp_parser_simple_declaration
1634 (cp_parser
*, bool, tree
*);
1635 static void cp_parser_decl_specifier_seq
1636 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1637 static tree cp_parser_storage_class_specifier_opt
1639 static tree cp_parser_function_specifier_opt
1640 (cp_parser
*, cp_decl_specifier_seq
*);
1641 static tree cp_parser_type_specifier
1642 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1644 static tree cp_parser_simple_type_specifier
1645 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1646 static tree cp_parser_type_name
1648 static tree cp_parser_nonclass_name
1649 (cp_parser
* parser
);
1650 static tree cp_parser_elaborated_type_specifier
1651 (cp_parser
*, bool, bool);
1652 static tree cp_parser_enum_specifier
1654 static void cp_parser_enumerator_list
1655 (cp_parser
*, tree
);
1656 static void cp_parser_enumerator_definition
1657 (cp_parser
*, tree
);
1658 static tree cp_parser_namespace_name
1660 static void cp_parser_namespace_definition
1662 static void cp_parser_namespace_body
1664 static tree cp_parser_qualified_namespace_specifier
1666 static void cp_parser_namespace_alias_definition
1668 static bool cp_parser_using_declaration
1669 (cp_parser
*, bool);
1670 static void cp_parser_using_directive
1672 static void cp_parser_asm_definition
1674 static void cp_parser_linkage_specification
1676 static void cp_parser_static_assert
1677 (cp_parser
*, bool);
1678 static tree cp_parser_decltype
1681 /* Declarators [gram.dcl.decl] */
1683 static tree cp_parser_init_declarator
1684 (cp_parser
*, cp_decl_specifier_seq
*, VEC (deferred_access_check
,gc
)*, bool, bool, int, bool *, tree
*);
1685 static cp_declarator
*cp_parser_declarator
1686 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1687 static cp_declarator
*cp_parser_direct_declarator
1688 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1689 static enum tree_code cp_parser_ptr_operator
1690 (cp_parser
*, tree
*, cp_cv_quals
*);
1691 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1693 static tree cp_parser_late_return_type_opt
1695 static tree cp_parser_declarator_id
1696 (cp_parser
*, bool);
1697 static tree cp_parser_type_id
1699 static tree cp_parser_template_type_arg
1701 static tree
cp_parser_trailing_type_id (cp_parser
*);
1702 static tree cp_parser_type_id_1
1703 (cp_parser
*, bool, bool);
1704 static void cp_parser_type_specifier_seq
1705 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
1706 static tree cp_parser_parameter_declaration_clause
1708 static tree cp_parser_parameter_declaration_list
1709 (cp_parser
*, bool *);
1710 static cp_parameter_declarator
*cp_parser_parameter_declaration
1711 (cp_parser
*, bool, bool *);
1712 static tree cp_parser_default_argument
1713 (cp_parser
*, bool);
1714 static void cp_parser_function_body
1716 static tree cp_parser_initializer
1717 (cp_parser
*, bool *, bool *);
1718 static tree cp_parser_initializer_clause
1719 (cp_parser
*, bool *);
1720 static tree cp_parser_braced_list
1721 (cp_parser
*, bool*);
1722 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1723 (cp_parser
*, bool *);
1725 static bool cp_parser_ctor_initializer_opt_and_function_body
1728 /* Classes [gram.class] */
1730 static tree cp_parser_class_name
1731 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1732 static tree cp_parser_class_specifier
1734 static tree cp_parser_class_head
1735 (cp_parser
*, bool *, tree
*, tree
*);
1736 static enum tag_types cp_parser_class_key
1738 static void cp_parser_member_specification_opt
1740 static void cp_parser_member_declaration
1742 static tree cp_parser_pure_specifier
1744 static tree cp_parser_constant_initializer
1747 /* Derived classes [gram.class.derived] */
1749 static tree cp_parser_base_clause
1751 static tree cp_parser_base_specifier
1754 /* Special member functions [gram.special] */
1756 static tree cp_parser_conversion_function_id
1758 static tree cp_parser_conversion_type_id
1760 static cp_declarator
*cp_parser_conversion_declarator_opt
1762 static bool cp_parser_ctor_initializer_opt
1764 static void cp_parser_mem_initializer_list
1766 static tree cp_parser_mem_initializer
1768 static tree cp_parser_mem_initializer_id
1771 /* Overloading [gram.over] */
1773 static tree cp_parser_operator_function_id
1775 static tree cp_parser_operator
1778 /* Templates [gram.temp] */
1780 static void cp_parser_template_declaration
1781 (cp_parser
*, bool);
1782 static tree cp_parser_template_parameter_list
1784 static tree cp_parser_template_parameter
1785 (cp_parser
*, bool *, bool *);
1786 static tree cp_parser_type_parameter
1787 (cp_parser
*, bool *);
1788 static tree cp_parser_template_id
1789 (cp_parser
*, bool, bool, bool);
1790 static tree cp_parser_template_name
1791 (cp_parser
*, bool, bool, bool, bool *);
1792 static tree cp_parser_template_argument_list
1794 static tree cp_parser_template_argument
1796 static void cp_parser_explicit_instantiation
1798 static void cp_parser_explicit_specialization
1801 /* Exception handling [gram.exception] */
1803 static tree cp_parser_try_block
1805 static bool cp_parser_function_try_block
1807 static void cp_parser_handler_seq
1809 static void cp_parser_handler
1811 static tree cp_parser_exception_declaration
1813 static tree cp_parser_throw_expression
1815 static tree cp_parser_exception_specification_opt
1817 static tree cp_parser_type_id_list
1820 /* GNU Extensions */
1822 static tree cp_parser_asm_specification_opt
1824 static tree cp_parser_asm_operand_list
1826 static tree cp_parser_asm_clobber_list
1828 static tree cp_parser_asm_label_list
1830 static tree cp_parser_attributes_opt
1832 static tree cp_parser_attribute_list
1834 static bool cp_parser_extension_opt
1835 (cp_parser
*, int *);
1836 static void cp_parser_label_declaration
1839 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
1840 static bool cp_parser_pragma
1841 (cp_parser
*, enum pragma_context
);
1843 /* Objective-C++ Productions */
1845 static tree cp_parser_objc_message_receiver
1847 static tree cp_parser_objc_message_args
1849 static tree cp_parser_objc_message_expression
1851 static tree cp_parser_objc_encode_expression
1853 static tree cp_parser_objc_defs_expression
1855 static tree cp_parser_objc_protocol_expression
1857 static tree cp_parser_objc_selector_expression
1859 static tree cp_parser_objc_expression
1861 static bool cp_parser_objc_selector_p
1863 static tree cp_parser_objc_selector
1865 static tree cp_parser_objc_protocol_refs_opt
1867 static void cp_parser_objc_declaration
1868 (cp_parser
*, tree
);
1869 static tree cp_parser_objc_statement
1871 static bool cp_parser_objc_valid_prefix_attributes
1872 (cp_parser
*, tree
*);
1873 static void cp_parser_objc_at_property_declaration
1875 static void cp_parser_objc_at_synthesize_declaration
1877 static void cp_parser_objc_at_dynamic_declaration
1879 static tree cp_parser_objc_struct_declaration
1882 /* Utility Routines */
1884 static tree cp_parser_lookup_name
1885 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
1886 static tree cp_parser_lookup_name_simple
1887 (cp_parser
*, tree
, location_t
);
1888 static tree cp_parser_maybe_treat_template_as_class
1890 static bool cp_parser_check_declarator_template_parameters
1891 (cp_parser
*, cp_declarator
*, location_t
);
1892 static bool cp_parser_check_template_parameters
1893 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
1894 static tree cp_parser_simple_cast_expression
1896 static tree cp_parser_global_scope_opt
1897 (cp_parser
*, bool);
1898 static bool cp_parser_constructor_declarator_p
1899 (cp_parser
*, bool);
1900 static tree cp_parser_function_definition_from_specifiers_and_declarator
1901 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1902 static tree cp_parser_function_definition_after_declarator
1903 (cp_parser
*, bool);
1904 static void cp_parser_template_declaration_after_export
1905 (cp_parser
*, bool);
1906 static void cp_parser_perform_template_parameter_access_checks
1907 (VEC (deferred_access_check
,gc
)*);
1908 static tree cp_parser_single_declaration
1909 (cp_parser
*, VEC (deferred_access_check
,gc
)*, bool, bool, bool *);
1910 static tree cp_parser_functional_cast
1911 (cp_parser
*, tree
);
1912 static tree cp_parser_save_member_function_body
1913 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1914 static tree cp_parser_enclosed_template_argument_list
1916 static void cp_parser_save_default_args
1917 (cp_parser
*, tree
);
1918 static void cp_parser_late_parsing_for_member
1919 (cp_parser
*, tree
);
1920 static void cp_parser_late_parsing_default_args
1921 (cp_parser
*, tree
);
1922 static tree cp_parser_sizeof_operand
1923 (cp_parser
*, enum rid
);
1924 static tree cp_parser_trait_expr
1925 (cp_parser
*, enum rid
);
1926 static bool cp_parser_declares_only_class_p
1928 static void cp_parser_set_storage_class
1929 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, location_t
);
1930 static void cp_parser_set_decl_spec_type
1931 (cp_decl_specifier_seq
*, tree
, location_t
, bool);
1932 static bool cp_parser_friend_p
1933 (const cp_decl_specifier_seq
*);
1934 static void cp_parser_required_error
1935 (cp_parser
*, required_token
, bool);
1936 static cp_token
*cp_parser_require
1937 (cp_parser
*, enum cpp_ttype
, required_token
);
1938 static cp_token
*cp_parser_require_keyword
1939 (cp_parser
*, enum rid
, required_token
);
1940 static bool cp_parser_token_starts_function_definition_p
1942 static bool cp_parser_next_token_starts_class_definition_p
1944 static bool cp_parser_next_token_ends_template_argument_p
1946 static bool cp_parser_nth_token_starts_template_argument_list_p
1947 (cp_parser
*, size_t);
1948 static enum tag_types cp_parser_token_is_class_key
1950 static void cp_parser_check_class_key
1951 (enum tag_types
, tree type
);
1952 static void cp_parser_check_access_in_redeclaration
1953 (tree type
, location_t location
);
1954 static bool cp_parser_optional_template_keyword
1956 static void cp_parser_pre_parsed_nested_name_specifier
1958 static bool cp_parser_cache_group
1959 (cp_parser
*, enum cpp_ttype
, unsigned);
1960 static void cp_parser_parse_tentatively
1962 static void cp_parser_commit_to_tentative_parse
1964 static void cp_parser_abort_tentative_parse
1966 static bool cp_parser_parse_definitely
1968 static inline bool cp_parser_parsing_tentatively
1970 static bool cp_parser_uncommitted_to_tentative_parse_p
1972 static void cp_parser_error
1973 (cp_parser
*, const char *);
1974 static void cp_parser_name_lookup_error
1975 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
1976 static bool cp_parser_simulate_error
1978 static bool cp_parser_check_type_definition
1980 static void cp_parser_check_for_definition_in_return_type
1981 (cp_declarator
*, tree
, location_t type_location
);
1982 static void cp_parser_check_for_invalid_template_id
1983 (cp_parser
*, tree
, location_t location
);
1984 static bool cp_parser_non_integral_constant_expression
1985 (cp_parser
*, non_integral_constant
);
1986 static void cp_parser_diagnose_invalid_type_name
1987 (cp_parser
*, tree
, tree
, location_t
);
1988 static bool cp_parser_parse_and_diagnose_invalid_type_name
1990 static int cp_parser_skip_to_closing_parenthesis
1991 (cp_parser
*, bool, bool, bool);
1992 static void cp_parser_skip_to_end_of_statement
1994 static void cp_parser_consume_semicolon_at_end_of_statement
1996 static void cp_parser_skip_to_end_of_block_or_statement
1998 static bool cp_parser_skip_to_closing_brace
2000 static void cp_parser_skip_to_end_of_template_parameter_list
2002 static void cp_parser_skip_to_pragma_eol
2003 (cp_parser
*, cp_token
*);
2004 static bool cp_parser_error_occurred
2006 static bool cp_parser_allow_gnu_extensions_p
2008 static bool cp_parser_is_string_literal
2010 static bool cp_parser_is_keyword
2011 (cp_token
*, enum rid
);
2012 static tree cp_parser_make_typename_type
2013 (cp_parser
*, tree
, tree
, location_t location
);
2014 static cp_declarator
* cp_parser_make_indirect_declarator
2015 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*);
2017 /* Returns nonzero if we are parsing tentatively. */
2020 cp_parser_parsing_tentatively (cp_parser
* parser
)
2022 return parser
->context
->next
!= NULL
;
2025 /* Returns nonzero if TOKEN is a string literal. */
2028 cp_parser_is_string_literal (cp_token
* token
)
2030 return (token
->type
== CPP_STRING
||
2031 token
->type
== CPP_STRING16
||
2032 token
->type
== CPP_STRING32
||
2033 token
->type
== CPP_WSTRING
||
2034 token
->type
== CPP_UTF8STRING
);
2037 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2040 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2042 return token
->keyword
== keyword
;
2045 /* If not parsing tentatively, issue a diagnostic of the form
2046 FILE:LINE: MESSAGE before TOKEN
2047 where TOKEN is the next token in the input stream. MESSAGE
2048 (specified by the caller) is usually of the form "expected
2052 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2054 if (!cp_parser_simulate_error (parser
))
2056 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2057 /* This diagnostic makes more sense if it is tagged to the line
2058 of the token we just peeked at. */
2059 cp_lexer_set_source_position_from_token (token
);
2061 if (token
->type
== CPP_PRAGMA
)
2063 error_at (token
->location
,
2064 "%<#pragma%> is not allowed here");
2065 cp_parser_skip_to_pragma_eol (parser
, token
);
2069 c_parse_error (gmsgid
,
2070 /* Because c_parser_error does not understand
2071 CPP_KEYWORD, keywords are treated like
2073 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2074 token
->u
.value
, token
->flags
);
2078 /* Issue an error about name-lookup failing. NAME is the
2079 IDENTIFIER_NODE DECL is the result of
2080 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2081 the thing that we hoped to find. */
2084 cp_parser_name_lookup_error (cp_parser
* parser
,
2087 name_lookup_error desired
,
2088 location_t location
)
2090 /* If name lookup completely failed, tell the user that NAME was not
2092 if (decl
== error_mark_node
)
2094 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2095 error_at (location
, "%<%E::%E%> has not been declared",
2096 parser
->scope
, name
);
2097 else if (parser
->scope
== global_namespace
)
2098 error_at (location
, "%<::%E%> has not been declared", name
);
2099 else if (parser
->object_scope
2100 && !CLASS_TYPE_P (parser
->object_scope
))
2101 error_at (location
, "request for member %qE in non-class type %qT",
2102 name
, parser
->object_scope
);
2103 else if (parser
->object_scope
)
2104 error_at (location
, "%<%T::%E%> has not been declared",
2105 parser
->object_scope
, name
);
2107 error_at (location
, "%qE has not been declared", name
);
2109 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2114 error_at (location
, "%<%E::%E%> is not a type",
2115 parser
->scope
, name
);
2118 error_at (location
, "%<%E::%E%> is not a class or namespace",
2119 parser
->scope
, name
);
2123 "%<%E::%E%> is not a class, namespace, or enumeration",
2124 parser
->scope
, name
);
2131 else if (parser
->scope
== global_namespace
)
2136 error_at (location
, "%<::%E%> is not a type", name
);
2139 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2143 "%<::%E%> is not a class, namespace, or enumeration",
2155 error_at (location
, "%qE is not a type", name
);
2158 error_at (location
, "%qE is not a class or namespace", name
);
2162 "%qE is not a class, namespace, or enumeration", name
);
2170 /* If we are parsing tentatively, remember that an error has occurred
2171 during this tentative parse. Returns true if the error was
2172 simulated; false if a message should be issued by the caller. */
2175 cp_parser_simulate_error (cp_parser
* parser
)
2177 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2179 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2185 /* Check for repeated decl-specifiers. */
2188 cp_parser_check_decl_spec (cp_decl_specifier_seq
*decl_specs
,
2189 location_t location
)
2193 for (ds
= ds_first
; ds
!= ds_last
; ++ds
)
2195 unsigned count
= decl_specs
->specs
[ds
];
2198 /* The "long" specifier is a special case because of "long long". */
2202 error_at (location
, "%<long long long%> is too long for GCC");
2204 pedwarn_cxx98 (location
, OPT_Wlong_long
,
2205 "ISO C++ 1998 does not support %<long long%>");
2209 static const char *const decl_spec_names
[] = {
2226 error_at (location
, "duplicate %qs", decl_spec_names
[ds
]);
2231 /* This function is called when a type is defined. If type
2232 definitions are forbidden at this point, an error message is
2236 cp_parser_check_type_definition (cp_parser
* parser
)
2238 /* If types are forbidden here, issue a message. */
2239 if (parser
->type_definition_forbidden_message
)
2241 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2242 in the message need to be interpreted. */
2243 error (parser
->type_definition_forbidden_message
);
2249 /* This function is called when the DECLARATOR is processed. The TYPE
2250 was a type defined in the decl-specifiers. If it is invalid to
2251 define a type in the decl-specifiers for DECLARATOR, an error is
2252 issued. TYPE_LOCATION is the location of TYPE and is used
2253 for error reporting. */
2256 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2257 tree type
, location_t type_location
)
2259 /* [dcl.fct] forbids type definitions in return types.
2260 Unfortunately, it's not easy to know whether or not we are
2261 processing a return type until after the fact. */
2263 && (declarator
->kind
== cdk_pointer
2264 || declarator
->kind
== cdk_reference
2265 || declarator
->kind
== cdk_ptrmem
))
2266 declarator
= declarator
->declarator
;
2268 && declarator
->kind
== cdk_function
)
2270 error_at (type_location
,
2271 "new types may not be defined in a return type");
2272 inform (type_location
,
2273 "(perhaps a semicolon is missing after the definition of %qT)",
2278 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2279 "<" in any valid C++ program. If the next token is indeed "<",
2280 issue a message warning the user about what appears to be an
2281 invalid attempt to form a template-id. LOCATION is the location
2282 of the type-specifier (TYPE) */
2285 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2286 tree type
, location_t location
)
2288 cp_token_position start
= 0;
2290 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2293 error_at (location
, "%qT is not a template", type
);
2294 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2295 error_at (location
, "%qE is not a template", type
);
2297 error_at (location
, "invalid template-id");
2298 /* Remember the location of the invalid "<". */
2299 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2300 start
= cp_lexer_token_position (parser
->lexer
, true);
2301 /* Consume the "<". */
2302 cp_lexer_consume_token (parser
->lexer
);
2303 /* Parse the template arguments. */
2304 cp_parser_enclosed_template_argument_list (parser
);
2305 /* Permanently remove the invalid template arguments so that
2306 this error message is not issued again. */
2308 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2312 /* If parsing an integral constant-expression, issue an error message
2313 about the fact that THING appeared and return true. Otherwise,
2314 return false. In either case, set
2315 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2318 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2319 non_integral_constant thing
)
2321 parser
->non_integral_constant_expression_p
= true;
2322 if (parser
->integral_constant_expression_p
)
2324 if (!parser
->allow_non_integral_constant_expression_p
)
2326 const char *msg
= NULL
;
2330 error ("floating-point literal "
2331 "cannot appear in a constant-expression");
2334 error ("a cast to a type other than an integral or "
2335 "enumeration type cannot appear in a "
2336 "constant-expression");
2339 error ("%<typeid%> operator "
2340 "cannot appear in a constant-expression");
2343 error ("non-constant compound literals "
2344 "cannot appear in a constant-expression");
2347 error ("a function call "
2348 "cannot appear in a constant-expression");
2351 error ("an increment "
2352 "cannot appear in a constant-expression");
2355 error ("an decrement "
2356 "cannot appear in a constant-expression");
2359 error ("an array reference "
2360 "cannot appear in a constant-expression");
2362 case NIC_ADDR_LABEL
:
2363 error ("the address of a label "
2364 "cannot appear in a constant-expression");
2366 case NIC_OVERLOADED
:
2367 error ("calls to overloaded operators "
2368 "cannot appear in a constant-expression");
2370 case NIC_ASSIGNMENT
:
2371 error ("an assignment cannot appear in a constant-expression");
2374 error ("a comma operator "
2375 "cannot appear in a constant-expression");
2377 case NIC_CONSTRUCTOR
:
2378 error ("a call to a constructor "
2379 "cannot appear in a constant-expression");
2385 msg
= "__FUNCTION__";
2387 case NIC_PRETTY_FUNC
:
2388 msg
= "__PRETTY_FUNCTION__";
2408 case NIC_PREINCREMENT
:
2411 case NIC_PREDECREMENT
:
2424 error ("%qs cannot appear in a constant-expression", msg
);
2431 /* Emit a diagnostic for an invalid type name. SCOPE is the
2432 qualifying scope (or NULL, if none) for ID. This function commits
2433 to the current active tentative parse, if any. (Otherwise, the
2434 problematic construct might be encountered again later, resulting
2435 in duplicate error messages.) LOCATION is the location of ID. */
2438 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
,
2439 tree scope
, tree id
,
2440 location_t location
)
2442 tree decl
, old_scope
;
2443 /* Try to lookup the identifier. */
2444 old_scope
= parser
->scope
;
2445 parser
->scope
= scope
;
2446 decl
= cp_parser_lookup_name_simple (parser
, id
, location
);
2447 parser
->scope
= old_scope
;
2448 /* If the lookup found a template-name, it means that the user forgot
2449 to specify an argument list. Emit a useful error message. */
2450 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2452 "invalid use of template-name %qE without an argument list",
2454 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2455 error_at (location
, "invalid use of destructor %qD as a type", id
);
2456 else if (TREE_CODE (decl
) == TYPE_DECL
)
2457 /* Something like 'unsigned A a;' */
2458 error_at (location
, "invalid combination of multiple type-specifiers");
2459 else if (!parser
->scope
)
2461 /* Issue an error message. */
2462 error_at (location
, "%qE does not name a type", id
);
2463 /* If we're in a template class, it's possible that the user was
2464 referring to a type from a base class. For example:
2466 template <typename T> struct A { typedef T X; };
2467 template <typename T> struct B : public A<T> { X x; };
2469 The user should have said "typename A<T>::X". */
2470 if (cxx_dialect
< cxx0x
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
2471 inform (location
, "C++0x %<constexpr%> only available with "
2472 "-std=c++0x or -std=gnu++0x");
2473 else if (processing_template_decl
&& current_class_type
2474 && TYPE_BINFO (current_class_type
))
2478 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2482 tree base_type
= BINFO_TYPE (b
);
2483 if (CLASS_TYPE_P (base_type
)
2484 && dependent_type_p (base_type
))
2487 /* Go from a particular instantiation of the
2488 template (which will have an empty TYPE_FIELDs),
2489 to the main version. */
2490 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2491 for (field
= TYPE_FIELDS (base_type
);
2493 field
= DECL_CHAIN (field
))
2494 if (TREE_CODE (field
) == TYPE_DECL
2495 && DECL_NAME (field
) == id
)
2498 "(perhaps %<typename %T::%E%> was intended)",
2499 BINFO_TYPE (b
), id
);
2508 /* Here we diagnose qualified-ids where the scope is actually correct,
2509 but the identifier does not resolve to a valid type name. */
2510 else if (parser
->scope
!= error_mark_node
)
2512 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2513 error_at (location
, "%qE in namespace %qE does not name a type",
2515 else if (CLASS_TYPE_P (parser
->scope
)
2516 && constructor_name_p (id
, parser
->scope
))
2519 error_at (location
, "%<%T::%E%> names the constructor, not"
2520 " the type", parser
->scope
, id
);
2521 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2522 error_at (location
, "and %qT has no template constructors",
2525 else if (TYPE_P (parser
->scope
)
2526 && dependent_scope_p (parser
->scope
))
2527 error_at (location
, "need %<typename%> before %<%T::%E%> because "
2528 "%qT is a dependent scope",
2529 parser
->scope
, id
, parser
->scope
);
2530 else if (TYPE_P (parser
->scope
))
2531 error_at (location
, "%qE in class %qT does not name a type",
2536 cp_parser_commit_to_tentative_parse (parser
);
2539 /* Check for a common situation where a type-name should be present,
2540 but is not, and issue a sensible error message. Returns true if an
2541 invalid type-name was detected.
2543 The situation handled by this function are variable declarations of the
2544 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2545 Usually, `ID' should name a type, but if we got here it means that it
2546 does not. We try to emit the best possible error message depending on
2547 how exactly the id-expression looks like. */
2550 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2553 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2555 /* Avoid duplicate error about ambiguous lookup. */
2556 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
2558 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
2559 if (next
->type
== CPP_NAME
&& next
->ambiguous_p
)
2563 cp_parser_parse_tentatively (parser
);
2564 id
= cp_parser_id_expression (parser
,
2565 /*template_keyword_p=*/false,
2566 /*check_dependency_p=*/true,
2567 /*template_p=*/NULL
,
2568 /*declarator_p=*/true,
2569 /*optional_p=*/false);
2570 /* If the next token is a (, this is a function with no explicit return
2571 type, i.e. constructor, destructor or conversion op. */
2572 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
2573 || TREE_CODE (id
) == TYPE_DECL
)
2575 cp_parser_abort_tentative_parse (parser
);
2578 if (!cp_parser_parse_definitely (parser
))
2581 /* Emit a diagnostic for the invalid type. */
2582 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
,
2583 id
, token
->location
);
2585 /* If we aren't in the middle of a declarator (i.e. in a
2586 parameter-declaration-clause), skip to the end of the declaration;
2587 there's no point in trying to process it. */
2588 if (!parser
->in_declarator_p
)
2589 cp_parser_skip_to_end_of_block_or_statement (parser
);
2593 /* Consume tokens up to, and including, the next non-nested closing `)'.
2594 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2595 are doing error recovery. Returns -1 if OR_COMMA is true and we
2596 found an unnested comma. */
2599 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2604 unsigned paren_depth
= 0;
2605 unsigned brace_depth
= 0;
2606 unsigned square_depth
= 0;
2608 if (recovering
&& !or_comma
2609 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2614 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2616 switch (token
->type
)
2619 case CPP_PRAGMA_EOL
:
2620 /* If we've run out of tokens, then there is no closing `)'. */
2623 /* This is good for lambda expression capture-lists. */
2624 case CPP_OPEN_SQUARE
:
2627 case CPP_CLOSE_SQUARE
:
2628 if (!square_depth
--)
2633 /* This matches the processing in skip_to_end_of_statement. */
2638 case CPP_OPEN_BRACE
:
2641 case CPP_CLOSE_BRACE
:
2647 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
2652 case CPP_OPEN_PAREN
:
2657 case CPP_CLOSE_PAREN
:
2658 if (!brace_depth
&& !paren_depth
--)
2661 cp_lexer_consume_token (parser
->lexer
);
2670 /* Consume the token. */
2671 cp_lexer_consume_token (parser
->lexer
);
2675 /* Consume tokens until we reach the end of the current statement.
2676 Normally, that will be just before consuming a `;'. However, if a
2677 non-nested `}' comes first, then we stop before consuming that. */
2680 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2682 unsigned nesting_depth
= 0;
2686 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2688 switch (token
->type
)
2691 case CPP_PRAGMA_EOL
:
2692 /* If we've run out of tokens, stop. */
2696 /* If the next token is a `;', we have reached the end of the
2702 case CPP_CLOSE_BRACE
:
2703 /* If this is a non-nested '}', stop before consuming it.
2704 That way, when confronted with something like:
2708 we stop before consuming the closing '}', even though we
2709 have not yet reached a `;'. */
2710 if (nesting_depth
== 0)
2713 /* If it is the closing '}' for a block that we have
2714 scanned, stop -- but only after consuming the token.
2720 we will stop after the body of the erroneously declared
2721 function, but before consuming the following `typedef'
2723 if (--nesting_depth
== 0)
2725 cp_lexer_consume_token (parser
->lexer
);
2729 case CPP_OPEN_BRACE
:
2737 /* Consume the token. */
2738 cp_lexer_consume_token (parser
->lexer
);
2742 /* This function is called at the end of a statement or declaration.
2743 If the next token is a semicolon, it is consumed; otherwise, error
2744 recovery is attempted. */
2747 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2749 /* Look for the trailing `;'. */
2750 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
2752 /* If there is additional (erroneous) input, skip to the end of
2754 cp_parser_skip_to_end_of_statement (parser
);
2755 /* If the next token is now a `;', consume it. */
2756 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2757 cp_lexer_consume_token (parser
->lexer
);
2761 /* Skip tokens until we have consumed an entire block, or until we
2762 have consumed a non-nested `;'. */
2765 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2767 int nesting_depth
= 0;
2769 while (nesting_depth
>= 0)
2771 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2773 switch (token
->type
)
2776 case CPP_PRAGMA_EOL
:
2777 /* If we've run out of tokens, stop. */
2781 /* Stop if this is an unnested ';'. */
2786 case CPP_CLOSE_BRACE
:
2787 /* Stop if this is an unnested '}', or closes the outermost
2790 if (nesting_depth
< 0)
2796 case CPP_OPEN_BRACE
:
2805 /* Consume the token. */
2806 cp_lexer_consume_token (parser
->lexer
);
2810 /* Skip tokens until a non-nested closing curly brace is the next
2811 token, or there are no more tokens. Return true in the first case,
2815 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2817 unsigned nesting_depth
= 0;
2821 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2823 switch (token
->type
)
2826 case CPP_PRAGMA_EOL
:
2827 /* If we've run out of tokens, stop. */
2830 case CPP_CLOSE_BRACE
:
2831 /* If the next token is a non-nested `}', then we have reached
2832 the end of the current block. */
2833 if (nesting_depth
-- == 0)
2837 case CPP_OPEN_BRACE
:
2838 /* If it the next token is a `{', then we are entering a new
2839 block. Consume the entire block. */
2847 /* Consume the token. */
2848 cp_lexer_consume_token (parser
->lexer
);
2852 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2853 parameter is the PRAGMA token, allowing us to purge the entire pragma
2857 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
2861 parser
->lexer
->in_pragma
= false;
2864 token
= cp_lexer_consume_token (parser
->lexer
);
2865 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
2867 /* Ensure that the pragma is not parsed again. */
2868 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
2871 /* Require pragma end of line, resyncing with it as necessary. The
2872 arguments are as for cp_parser_skip_to_pragma_eol. */
2875 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
2877 parser
->lexer
->in_pragma
= false;
2878 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
2879 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
2882 /* This is a simple wrapper around make_typename_type. When the id is
2883 an unresolved identifier node, we can provide a superior diagnostic
2884 using cp_parser_diagnose_invalid_type_name. */
2887 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
,
2888 tree id
, location_t id_location
)
2891 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2893 result
= make_typename_type (scope
, id
, typename_type
,
2894 /*complain=*/tf_none
);
2895 if (result
== error_mark_node
)
2896 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
, id_location
);
2899 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2902 /* This is a wrapper around the
2903 make_{pointer,ptrmem,reference}_declarator functions that decides
2904 which one to call based on the CODE and CLASS_TYPE arguments. The
2905 CODE argument should be one of the values returned by
2906 cp_parser_ptr_operator. */
2907 static cp_declarator
*
2908 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
2909 cp_cv_quals cv_qualifiers
,
2910 cp_declarator
*target
)
2912 if (code
== ERROR_MARK
)
2913 return cp_error_declarator
;
2915 if (code
== INDIRECT_REF
)
2916 if (class_type
== NULL_TREE
)
2917 return make_pointer_declarator (cv_qualifiers
, target
);
2919 return make_ptrmem_declarator (cv_qualifiers
, class_type
, target
);
2920 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
2921 return make_reference_declarator (cv_qualifiers
, target
, false);
2922 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
2923 return make_reference_declarator (cv_qualifiers
, target
, true);
2927 /* Create a new C++ parser. */
2930 cp_parser_new (void)
2936 /* cp_lexer_new_main is called before doing GC allocation because
2937 cp_lexer_new_main might load a PCH file. */
2938 lexer
= cp_lexer_new_main ();
2940 /* Initialize the binops_by_token so that we can get the tree
2941 directly from the token. */
2942 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2943 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2945 parser
= ggc_alloc_cleared_cp_parser ();
2946 parser
->lexer
= lexer
;
2947 parser
->context
= cp_parser_context_new (NULL
);
2949 /* For now, we always accept GNU extensions. */
2950 parser
->allow_gnu_extensions_p
= 1;
2952 /* The `>' token is a greater-than operator, not the end of a
2954 parser
->greater_than_is_operator_p
= true;
2956 parser
->default_arg_ok_p
= true;
2958 /* We are not parsing a constant-expression. */
2959 parser
->integral_constant_expression_p
= false;
2960 parser
->allow_non_integral_constant_expression_p
= false;
2961 parser
->non_integral_constant_expression_p
= false;
2963 /* Local variable names are not forbidden. */
2964 parser
->local_variables_forbidden_p
= false;
2966 /* We are not processing an `extern "C"' declaration. */
2967 parser
->in_unbraced_linkage_specification_p
= false;
2969 /* We are not processing a declarator. */
2970 parser
->in_declarator_p
= false;
2972 /* We are not processing a template-argument-list. */
2973 parser
->in_template_argument_list_p
= false;
2975 /* We are not in an iteration statement. */
2976 parser
->in_statement
= 0;
2978 /* We are not in a switch statement. */
2979 parser
->in_switch_statement_p
= false;
2981 /* We are not parsing a type-id inside an expression. */
2982 parser
->in_type_id_in_expr_p
= false;
2984 /* Declarations aren't implicitly extern "C". */
2985 parser
->implicit_extern_c
= false;
2987 /* String literals should be translated to the execution character set. */
2988 parser
->translate_strings_p
= true;
2990 /* We are not parsing a function body. */
2991 parser
->in_function_body
= false;
2993 /* We can correct until told otherwise. */
2994 parser
->colon_corrects_to_scope_p
= true;
2996 /* The unparsed function queue is empty. */
2997 push_unparsed_function_queues (parser
);
2999 /* There are no classes being defined. */
3000 parser
->num_classes_being_defined
= 0;
3002 /* No template parameters apply. */
3003 parser
->num_template_parameter_lists
= 0;
3008 /* Create a cp_lexer structure which will emit the tokens in CACHE
3009 and push it onto the parser's lexer stack. This is used for delayed
3010 parsing of in-class method bodies and default arguments, and should
3011 not be confused with tentative parsing. */
3013 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3015 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3016 lexer
->next
= parser
->lexer
;
3017 parser
->lexer
= lexer
;
3019 /* Move the current source position to that of the first token in the
3021 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3024 /* Pop the top lexer off the parser stack. This is never used for the
3025 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3027 cp_parser_pop_lexer (cp_parser
*parser
)
3029 cp_lexer
*lexer
= parser
->lexer
;
3030 parser
->lexer
= lexer
->next
;
3031 cp_lexer_destroy (lexer
);
3033 /* Put the current source position back where it was before this
3034 lexer was pushed. */
3035 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3038 /* Lexical conventions [gram.lex] */
3040 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3044 cp_parser_identifier (cp_parser
* parser
)
3048 /* Look for the identifier. */
3049 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3050 /* Return the value. */
3051 return token
? token
->u
.value
: error_mark_node
;
3054 /* Parse a sequence of adjacent string constants. Returns a
3055 TREE_STRING representing the combined, nul-terminated string
3056 constant. If TRANSLATE is true, translate the string to the
3057 execution character set. If WIDE_OK is true, a wide string is
3060 C++98 [lex.string] says that if a narrow string literal token is
3061 adjacent to a wide string literal token, the behavior is undefined.
3062 However, C99 6.4.5p4 says that this results in a wide string literal.
3063 We follow C99 here, for consistency with the C front end.
3065 This code is largely lifted from lex_string() in c-lex.c.
3067 FUTURE: ObjC++ will need to handle @-strings here. */
3069 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
3073 struct obstack str_ob
;
3074 cpp_string str
, istr
, *strs
;
3076 enum cpp_ttype type
;
3078 tok
= cp_lexer_peek_token (parser
->lexer
);
3079 if (!cp_parser_is_string_literal (tok
))
3081 cp_parser_error (parser
, "expected string-literal");
3082 return error_mark_node
;
3087 /* Try to avoid the overhead of creating and destroying an obstack
3088 for the common case of just one string. */
3089 if (!cp_parser_is_string_literal
3090 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3092 cp_lexer_consume_token (parser
->lexer
);
3094 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
3095 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
3102 gcc_obstack_init (&str_ob
);
3107 cp_lexer_consume_token (parser
->lexer
);
3109 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
3110 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
3112 if (type
!= tok
->type
)
3114 if (type
== CPP_STRING
)
3116 else if (tok
->type
!= CPP_STRING
)
3117 error_at (tok
->location
,
3118 "unsupported non-standard concatenation "
3119 "of string literals");
3122 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3124 tok
= cp_lexer_peek_token (parser
->lexer
);
3126 while (cp_parser_is_string_literal (tok
));
3128 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3131 if (type
!= CPP_STRING
&& !wide_ok
)
3133 cp_parser_error (parser
, "a wide string is invalid in this context");
3137 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3138 (parse_in
, strs
, count
, &istr
, type
))
3140 value
= build_string (istr
.len
, (const char *)istr
.text
);
3141 free (CONST_CAST (unsigned char *, istr
.text
));
3147 case CPP_UTF8STRING
:
3148 TREE_TYPE (value
) = char_array_type_node
;
3151 TREE_TYPE (value
) = char16_array_type_node
;
3154 TREE_TYPE (value
) = char32_array_type_node
;
3157 TREE_TYPE (value
) = wchar_array_type_node
;
3161 value
= fix_string_type (value
);
3164 /* cpp_interpret_string has issued an error. */
3165 value
= error_mark_node
;
3168 obstack_free (&str_ob
, 0);
3174 /* Basic concepts [gram.basic] */
3176 /* Parse a translation-unit.
3179 declaration-seq [opt]
3181 Returns TRUE if all went well. */
3184 cp_parser_translation_unit (cp_parser
* parser
)
3186 /* The address of the first non-permanent object on the declarator
3188 static void *declarator_obstack_base
;
3192 /* Create the declarator obstack, if necessary. */
3193 if (!cp_error_declarator
)
3195 gcc_obstack_init (&declarator_obstack
);
3196 /* Create the error declarator. */
3197 cp_error_declarator
= make_declarator (cdk_error
);
3198 /* Create the empty parameter list. */
3199 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
3200 /* Remember where the base of the declarator obstack lies. */
3201 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
3204 cp_parser_declaration_seq_opt (parser
);
3206 /* If there are no tokens left then all went well. */
3207 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
3209 /* Get rid of the token array; we don't need it any more. */
3210 cp_lexer_destroy (parser
->lexer
);
3211 parser
->lexer
= NULL
;
3213 /* This file might have been a context that's implicitly extern
3214 "C". If so, pop the lang context. (Only relevant for PCH.) */
3215 if (parser
->implicit_extern_c
)
3217 pop_lang_context ();
3218 parser
->implicit_extern_c
= false;
3222 finish_translation_unit ();
3228 cp_parser_error (parser
, "expected declaration");
3232 /* Make sure the declarator obstack was fully cleaned up. */
3233 gcc_assert (obstack_next_free (&declarator_obstack
)
3234 == declarator_obstack_base
);
3236 /* All went well. */
3240 /* Expressions [gram.expr] */
3242 /* Parse a primary-expression.
3253 ( compound-statement )
3254 __builtin_va_arg ( assignment-expression , type-id )
3255 __builtin_offsetof ( type-id , offsetof-expression )
3258 __has_nothrow_assign ( type-id )
3259 __has_nothrow_constructor ( type-id )
3260 __has_nothrow_copy ( type-id )
3261 __has_trivial_assign ( type-id )
3262 __has_trivial_constructor ( type-id )
3263 __has_trivial_copy ( type-id )
3264 __has_trivial_destructor ( type-id )
3265 __has_virtual_destructor ( type-id )
3266 __is_abstract ( type-id )
3267 __is_base_of ( type-id , type-id )
3268 __is_class ( type-id )
3269 __is_convertible_to ( type-id , type-id )
3270 __is_empty ( type-id )
3271 __is_enum ( type-id )
3272 __is_pod ( type-id )
3273 __is_polymorphic ( type-id )
3274 __is_union ( type-id )
3276 Objective-C++ Extension:
3284 ADDRESS_P is true iff this expression was immediately preceded by
3285 "&" and therefore might denote a pointer-to-member. CAST_P is true
3286 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3287 true iff this expression is a template argument.
3289 Returns a representation of the expression. Upon return, *IDK
3290 indicates what kind of id-expression (if any) was present. */
3293 cp_parser_primary_expression (cp_parser
*parser
,
3296 bool template_arg_p
,
3299 cp_token
*token
= NULL
;
3301 /* Assume the primary expression is not an id-expression. */
3302 *idk
= CP_ID_KIND_NONE
;
3304 /* Peek at the next token. */
3305 token
= cp_lexer_peek_token (parser
->lexer
);
3306 switch (token
->type
)
3319 token
= cp_lexer_consume_token (parser
->lexer
);
3320 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
3322 error_at (token
->location
,
3323 "fixed-point types not supported in C++");
3324 return error_mark_node
;
3326 /* Floating-point literals are only allowed in an integral
3327 constant expression if they are cast to an integral or
3328 enumeration type. */
3329 if (TREE_CODE (token
->u
.value
) == REAL_CST
3330 && parser
->integral_constant_expression_p
3333 /* CAST_P will be set even in invalid code like "int(2.7 +
3334 ...)". Therefore, we have to check that the next token
3335 is sure to end the cast. */
3338 cp_token
*next_token
;
3340 next_token
= cp_lexer_peek_token (parser
->lexer
);
3341 if (/* The comma at the end of an
3342 enumerator-definition. */
3343 next_token
->type
!= CPP_COMMA
3344 /* The curly brace at the end of an enum-specifier. */
3345 && next_token
->type
!= CPP_CLOSE_BRACE
3346 /* The end of a statement. */
3347 && next_token
->type
!= CPP_SEMICOLON
3348 /* The end of the cast-expression. */
3349 && next_token
->type
!= CPP_CLOSE_PAREN
3350 /* The end of an array bound. */
3351 && next_token
->type
!= CPP_CLOSE_SQUARE
3352 /* The closing ">" in a template-argument-list. */
3353 && (next_token
->type
!= CPP_GREATER
3354 || parser
->greater_than_is_operator_p
)
3355 /* C++0x only: A ">>" treated like two ">" tokens,
3356 in a template-argument-list. */
3357 && (next_token
->type
!= CPP_RSHIFT
3358 || (cxx_dialect
== cxx98
)
3359 || parser
->greater_than_is_operator_p
))
3363 /* If we are within a cast, then the constraint that the
3364 cast is to an integral or enumeration type will be
3365 checked at that point. If we are not within a cast, then
3366 this code is invalid. */
3368 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
3370 return token
->u
.value
;
3376 case CPP_UTF8STRING
:
3377 /* ??? Should wide strings be allowed when parser->translate_strings_p
3378 is false (i.e. in attributes)? If not, we can kill the third
3379 argument to cp_parser_string_literal. */
3380 return cp_parser_string_literal (parser
,
3381 parser
->translate_strings_p
,
3384 case CPP_OPEN_PAREN
:
3387 bool saved_greater_than_is_operator_p
;
3389 /* Consume the `('. */
3390 cp_lexer_consume_token (parser
->lexer
);
3391 /* Within a parenthesized expression, a `>' token is always
3392 the greater-than operator. */
3393 saved_greater_than_is_operator_p
3394 = parser
->greater_than_is_operator_p
;
3395 parser
->greater_than_is_operator_p
= true;
3396 /* If we see `( { ' then we are looking at the beginning of
3397 a GNU statement-expression. */
3398 if (cp_parser_allow_gnu_extensions_p (parser
)
3399 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
3401 /* Statement-expressions are not allowed by the standard. */
3402 pedwarn (token
->location
, OPT_pedantic
,
3403 "ISO C++ forbids braced-groups within expressions");
3405 /* And they're not allowed outside of a function-body; you
3406 cannot, for example, write:
3408 int i = ({ int j = 3; j + 1; });
3410 at class or namespace scope. */
3411 if (!parser
->in_function_body
3412 || parser
->in_template_argument_list_p
)
3414 error_at (token
->location
,
3415 "statement-expressions are not allowed outside "
3416 "functions nor in template-argument lists");
3417 cp_parser_skip_to_end_of_block_or_statement (parser
);
3418 expr
= error_mark_node
;
3422 /* Start the statement-expression. */
3423 expr
= begin_stmt_expr ();
3424 /* Parse the compound-statement. */
3425 cp_parser_compound_statement (parser
, expr
, false, false);
3427 expr
= finish_stmt_expr (expr
, false);
3432 /* Parse the parenthesized expression. */
3433 expr
= cp_parser_expression (parser
, cast_p
, idk
);
3434 /* Let the front end know that this expression was
3435 enclosed in parentheses. This matters in case, for
3436 example, the expression is of the form `A::B', since
3437 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3439 finish_parenthesized_expr (expr
);
3440 /* DR 705: Wrapping an unqualified name in parentheses
3441 suppresses arg-dependent lookup. We want to pass back
3442 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3443 (c++/37862), but none of the others. */
3444 if (*idk
!= CP_ID_KIND_QUALIFIED
)
3445 *idk
= CP_ID_KIND_NONE
;
3447 /* The `>' token might be the end of a template-id or
3448 template-parameter-list now. */
3449 parser
->greater_than_is_operator_p
3450 = saved_greater_than_is_operator_p
;
3451 /* Consume the `)'. */
3452 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
3453 cp_parser_skip_to_end_of_statement (parser
);
3458 case CPP_OPEN_SQUARE
:
3459 if (c_dialect_objc ())
3460 /* We have an Objective-C++ message. */
3461 return cp_parser_objc_expression (parser
);
3463 tree lam
= cp_parser_lambda_expression (parser
);
3464 /* Don't warn about a failed tentative parse. */
3465 if (cp_parser_error_occurred (parser
))
3466 return error_mark_node
;
3467 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
3471 case CPP_OBJC_STRING
:
3472 if (c_dialect_objc ())
3473 /* We have an Objective-C++ string literal. */
3474 return cp_parser_objc_expression (parser
);
3475 cp_parser_error (parser
, "expected primary-expression");
3476 return error_mark_node
;
3479 switch (token
->keyword
)
3481 /* These two are the boolean literals. */
3483 cp_lexer_consume_token (parser
->lexer
);
3484 return boolean_true_node
;
3486 cp_lexer_consume_token (parser
->lexer
);
3487 return boolean_false_node
;
3489 /* The `__null' literal. */
3491 cp_lexer_consume_token (parser
->lexer
);
3494 /* The `nullptr' literal. */
3496 cp_lexer_consume_token (parser
->lexer
);
3497 return nullptr_node
;
3499 /* Recognize the `this' keyword. */
3501 cp_lexer_consume_token (parser
->lexer
);
3502 if (parser
->local_variables_forbidden_p
)
3504 error_at (token
->location
,
3505 "%<this%> may not be used in this context");
3506 return error_mark_node
;
3508 /* Pointers cannot appear in constant-expressions. */
3509 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
3510 return error_mark_node
;
3511 return finish_this_expr ();
3513 /* The `operator' keyword can be the beginning of an
3518 case RID_FUNCTION_NAME
:
3519 case RID_PRETTY_FUNCTION_NAME
:
3520 case RID_C99_FUNCTION_NAME
:
3522 non_integral_constant name
;
3524 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3525 __func__ are the names of variables -- but they are
3526 treated specially. Therefore, they are handled here,
3527 rather than relying on the generic id-expression logic
3528 below. Grammatically, these names are id-expressions.
3530 Consume the token. */
3531 token
= cp_lexer_consume_token (parser
->lexer
);
3533 switch (token
->keyword
)
3535 case RID_FUNCTION_NAME
:
3536 name
= NIC_FUNC_NAME
;
3538 case RID_PRETTY_FUNCTION_NAME
:
3539 name
= NIC_PRETTY_FUNC
;
3541 case RID_C99_FUNCTION_NAME
:
3542 name
= NIC_C99_FUNC
;
3548 if (cp_parser_non_integral_constant_expression (parser
, name
))
3549 return error_mark_node
;
3551 /* Look up the name. */
3552 return finish_fname (token
->u
.value
);
3560 /* The `__builtin_va_arg' construct is used to handle
3561 `va_arg'. Consume the `__builtin_va_arg' token. */
3562 cp_lexer_consume_token (parser
->lexer
);
3563 /* Look for the opening `('. */
3564 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
3565 /* Now, parse the assignment-expression. */
3566 expression
= cp_parser_assignment_expression (parser
,
3567 /*cast_p=*/false, NULL
);
3568 /* Look for the `,'. */
3569 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
3570 /* Parse the type-id. */
3571 type
= cp_parser_type_id (parser
);
3572 /* Look for the closing `)'. */
3573 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
3574 /* Using `va_arg' in a constant-expression is not
3576 if (cp_parser_non_integral_constant_expression (parser
,
3578 return error_mark_node
;
3579 return build_x_va_arg (expression
, type
);
3583 return cp_parser_builtin_offsetof (parser
);
3585 case RID_HAS_NOTHROW_ASSIGN
:
3586 case RID_HAS_NOTHROW_CONSTRUCTOR
:
3587 case RID_HAS_NOTHROW_COPY
:
3588 case RID_HAS_TRIVIAL_ASSIGN
:
3589 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
3590 case RID_HAS_TRIVIAL_COPY
:
3591 case RID_HAS_TRIVIAL_DESTRUCTOR
:
3592 case RID_HAS_VIRTUAL_DESTRUCTOR
:
3593 case RID_IS_ABSTRACT
:
3594 case RID_IS_BASE_OF
:
3596 case RID_IS_CONVERTIBLE_TO
:
3600 case RID_IS_POLYMORPHIC
:
3601 case RID_IS_STD_LAYOUT
:
3602 case RID_IS_TRIVIAL
:
3604 case RID_IS_LITERAL_TYPE
:
3605 return cp_parser_trait_expr (parser
, token
->keyword
);
3607 /* Objective-C++ expressions. */
3609 case RID_AT_PROTOCOL
:
3610 case RID_AT_SELECTOR
:
3611 return cp_parser_objc_expression (parser
);
3614 if (parser
->in_function_body
3615 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3618 error_at (token
->location
,
3619 "a template declaration cannot appear at block scope");
3620 cp_parser_skip_to_end_of_block_or_statement (parser
);
3621 return error_mark_node
;
3624 cp_parser_error (parser
, "expected primary-expression");
3625 return error_mark_node
;
3628 /* An id-expression can start with either an identifier, a
3629 `::' as the beginning of a qualified-id, or the "operator"
3633 case CPP_TEMPLATE_ID
:
3634 case CPP_NESTED_NAME_SPECIFIER
:
3638 const char *error_msg
;
3641 cp_token
*id_expr_token
;
3644 /* Parse the id-expression. */
3646 = cp_parser_id_expression (parser
,
3647 /*template_keyword_p=*/false,
3648 /*check_dependency_p=*/true,
3650 /*declarator_p=*/false,
3651 /*optional_p=*/false);
3652 if (id_expression
== error_mark_node
)
3653 return error_mark_node
;
3654 id_expr_token
= token
;
3655 token
= cp_lexer_peek_token (parser
->lexer
);
3656 done
= (token
->type
!= CPP_OPEN_SQUARE
3657 && token
->type
!= CPP_OPEN_PAREN
3658 && token
->type
!= CPP_DOT
3659 && token
->type
!= CPP_DEREF
3660 && token
->type
!= CPP_PLUS_PLUS
3661 && token
->type
!= CPP_MINUS_MINUS
);
3662 /* If we have a template-id, then no further lookup is
3663 required. If the template-id was for a template-class, we
3664 will sometimes have a TYPE_DECL at this point. */
3665 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
3666 || TREE_CODE (id_expression
) == TYPE_DECL
)
3667 decl
= id_expression
;
3668 /* Look up the name. */
3671 tree ambiguous_decls
;
3673 /* If we already know that this lookup is ambiguous, then
3674 we've already issued an error message; there's no reason
3676 if (id_expr_token
->type
== CPP_NAME
3677 && id_expr_token
->ambiguous_p
)
3679 cp_parser_simulate_error (parser
);
3680 return error_mark_node
;
3683 decl
= cp_parser_lookup_name (parser
, id_expression
,
3686 /*is_namespace=*/false,
3687 /*check_dependency=*/true,
3689 id_expr_token
->location
);
3690 /* If the lookup was ambiguous, an error will already have
3692 if (ambiguous_decls
)
3693 return error_mark_node
;
3695 /* In Objective-C++, we may have an Objective-C 2.0
3696 dot-syntax for classes here. */
3697 if (c_dialect_objc ()
3698 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
3699 && TREE_CODE (decl
) == TYPE_DECL
3700 && objc_is_class_name (decl
))
3703 cp_lexer_consume_token (parser
->lexer
);
3704 component
= cp_parser_identifier (parser
);
3705 if (component
== error_mark_node
)
3706 return error_mark_node
;
3708 return objc_build_class_component_ref (id_expression
, component
);
3711 /* In Objective-C++, an instance variable (ivar) may be preferred
3712 to whatever cp_parser_lookup_name() found. */
3713 decl
= objc_lookup_ivar (decl
, id_expression
);
3715 /* If name lookup gives us a SCOPE_REF, then the
3716 qualifying scope was dependent. */
3717 if (TREE_CODE (decl
) == SCOPE_REF
)
3719 /* At this point, we do not know if DECL is a valid
3720 integral constant expression. We assume that it is
3721 in fact such an expression, so that code like:
3723 template <int N> struct A {
3727 is accepted. At template-instantiation time, we
3728 will check that B<N>::i is actually a constant. */
3731 /* Check to see if DECL is a local variable in a context
3732 where that is forbidden. */
3733 if (parser
->local_variables_forbidden_p
3734 && local_variable_p (decl
))
3736 /* It might be that we only found DECL because we are
3737 trying to be generous with pre-ISO scoping rules.
3738 For example, consider:
3742 for (int i = 0; i < 10; ++i) {}
3743 extern void f(int j = i);
3746 Here, name look up will originally find the out
3747 of scope `i'. We need to issue a warning message,
3748 but then use the global `i'. */
3749 decl
= check_for_out_of_scope_variable (decl
);
3750 if (local_variable_p (decl
))
3752 error_at (id_expr_token
->location
,
3753 "local variable %qD may not appear in this context",
3755 return error_mark_node
;
3760 decl
= (finish_id_expression
3761 (id_expression
, decl
, parser
->scope
,
3763 parser
->integral_constant_expression_p
,
3764 parser
->allow_non_integral_constant_expression_p
,
3765 &parser
->non_integral_constant_expression_p
,
3766 template_p
, done
, address_p
,
3769 id_expr_token
->location
));
3771 cp_parser_error (parser
, error_msg
);
3775 /* Anything else is an error. */
3777 cp_parser_error (parser
, "expected primary-expression");
3778 return error_mark_node
;
3782 /* Parse an id-expression.
3789 :: [opt] nested-name-specifier template [opt] unqualified-id
3791 :: operator-function-id
3794 Return a representation of the unqualified portion of the
3795 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3796 a `::' or nested-name-specifier.
3798 Often, if the id-expression was a qualified-id, the caller will
3799 want to make a SCOPE_REF to represent the qualified-id. This
3800 function does not do this in order to avoid wastefully creating
3801 SCOPE_REFs when they are not required.
3803 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3806 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3807 uninstantiated templates.
3809 If *TEMPLATE_P is non-NULL, it is set to true iff the
3810 `template' keyword is used to explicitly indicate that the entity
3811 named is a template.
3813 If DECLARATOR_P is true, the id-expression is appearing as part of
3814 a declarator, rather than as part of an expression. */
3817 cp_parser_id_expression (cp_parser
*parser
,
3818 bool template_keyword_p
,
3819 bool check_dependency_p
,
3824 bool global_scope_p
;
3825 bool nested_name_specifier_p
;
3827 /* Assume the `template' keyword was not used. */
3829 *template_p
= template_keyword_p
;
3831 /* Look for the optional `::' operator. */
3833 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3835 /* Look for the optional nested-name-specifier. */
3836 nested_name_specifier_p
3837 = (cp_parser_nested_name_specifier_opt (parser
,
3838 /*typename_keyword_p=*/false,
3843 /* If there is a nested-name-specifier, then we are looking at
3844 the first qualified-id production. */
3845 if (nested_name_specifier_p
)
3848 tree saved_object_scope
;
3849 tree saved_qualifying_scope
;
3850 tree unqualified_id
;
3853 /* See if the next token is the `template' keyword. */
3855 template_p
= &is_template
;
3856 *template_p
= cp_parser_optional_template_keyword (parser
);
3857 /* Name lookup we do during the processing of the
3858 unqualified-id might obliterate SCOPE. */
3859 saved_scope
= parser
->scope
;
3860 saved_object_scope
= parser
->object_scope
;
3861 saved_qualifying_scope
= parser
->qualifying_scope
;
3862 /* Process the final unqualified-id. */
3863 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3866 /*optional_p=*/false);
3867 /* Restore the SAVED_SCOPE for our caller. */
3868 parser
->scope
= saved_scope
;
3869 parser
->object_scope
= saved_object_scope
;
3870 parser
->qualifying_scope
= saved_qualifying_scope
;
3872 return unqualified_id
;
3874 /* Otherwise, if we are in global scope, then we are looking at one
3875 of the other qualified-id productions. */
3876 else if (global_scope_p
)
3881 /* Peek at the next token. */
3882 token
= cp_lexer_peek_token (parser
->lexer
);
3884 /* If it's an identifier, and the next token is not a "<", then
3885 we can avoid the template-id case. This is an optimization
3886 for this common case. */
3887 if (token
->type
== CPP_NAME
3888 && !cp_parser_nth_token_starts_template_argument_list_p
3890 return cp_parser_identifier (parser
);
3892 cp_parser_parse_tentatively (parser
);
3893 /* Try a template-id. */
3894 id
= cp_parser_template_id (parser
,
3895 /*template_keyword_p=*/false,
3896 /*check_dependency_p=*/true,
3898 /* If that worked, we're done. */
3899 if (cp_parser_parse_definitely (parser
))
3902 /* Peek at the next token. (Changes in the token buffer may
3903 have invalidated the pointer obtained above.) */
3904 token
= cp_lexer_peek_token (parser
->lexer
);
3906 switch (token
->type
)
3909 return cp_parser_identifier (parser
);
3912 if (token
->keyword
== RID_OPERATOR
)
3913 return cp_parser_operator_function_id (parser
);
3917 cp_parser_error (parser
, "expected id-expression");
3918 return error_mark_node
;
3922 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3923 /*check_dependency_p=*/true,
3928 /* Parse an unqualified-id.
3932 operator-function-id
3933 conversion-function-id
3937 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3938 keyword, in a construct like `A::template ...'.
3940 Returns a representation of unqualified-id. For the `identifier'
3941 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3942 production a BIT_NOT_EXPR is returned; the operand of the
3943 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3944 other productions, see the documentation accompanying the
3945 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3946 names are looked up in uninstantiated templates. If DECLARATOR_P
3947 is true, the unqualified-id is appearing as part of a declarator,
3948 rather than as part of an expression. */
3951 cp_parser_unqualified_id (cp_parser
* parser
,
3952 bool template_keyword_p
,
3953 bool check_dependency_p
,
3959 /* Peek at the next token. */
3960 token
= cp_lexer_peek_token (parser
->lexer
);
3962 switch (token
->type
)
3968 /* We don't know yet whether or not this will be a
3970 cp_parser_parse_tentatively (parser
);
3971 /* Try a template-id. */
3972 id
= cp_parser_template_id (parser
, template_keyword_p
,
3975 /* If it worked, we're done. */
3976 if (cp_parser_parse_definitely (parser
))
3978 /* Otherwise, it's an ordinary identifier. */
3979 return cp_parser_identifier (parser
);
3982 case CPP_TEMPLATE_ID
:
3983 return cp_parser_template_id (parser
, template_keyword_p
,
3990 tree qualifying_scope
;
3995 /* Consume the `~' token. */
3996 cp_lexer_consume_token (parser
->lexer
);
3997 /* Parse the class-name. The standard, as written, seems to
4000 template <typename T> struct S { ~S (); };
4001 template <typename T> S<T>::~S() {}
4003 is invalid, since `~' must be followed by a class-name, but
4004 `S<T>' is dependent, and so not known to be a class.
4005 That's not right; we need to look in uninstantiated
4006 templates. A further complication arises from:
4008 template <typename T> void f(T t) {
4012 Here, it is not possible to look up `T' in the scope of `T'
4013 itself. We must look in both the current scope, and the
4014 scope of the containing complete expression.
4016 Yet another issue is:
4025 The standard does not seem to say that the `S' in `~S'
4026 should refer to the type `S' and not the data member
4029 /* DR 244 says that we look up the name after the "~" in the
4030 same scope as we looked up the qualifying name. That idea
4031 isn't fully worked out; it's more complicated than that. */
4032 scope
= parser
->scope
;
4033 object_scope
= parser
->object_scope
;
4034 qualifying_scope
= parser
->qualifying_scope
;
4036 /* Check for invalid scopes. */
4037 if (scope
== error_mark_node
)
4039 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4040 cp_lexer_consume_token (parser
->lexer
);
4041 return error_mark_node
;
4043 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
4045 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4046 error_at (token
->location
,
4047 "scope %qT before %<~%> is not a class-name",
4049 cp_parser_simulate_error (parser
);
4050 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4051 cp_lexer_consume_token (parser
->lexer
);
4052 return error_mark_node
;
4054 gcc_assert (!scope
|| TYPE_P (scope
));
4056 /* If the name is of the form "X::~X" it's OK even if X is a
4058 token
= cp_lexer_peek_token (parser
->lexer
);
4060 && token
->type
== CPP_NAME
4061 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4063 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
4064 || constructor_name_p (token
->u
.value
, scope
)))
4066 cp_lexer_consume_token (parser
->lexer
);
4067 return build_nt (BIT_NOT_EXPR
, scope
);
4070 /* If there was an explicit qualification (S::~T), first look
4071 in the scope given by the qualification (i.e., S).
4073 Note: in the calls to cp_parser_class_name below we pass
4074 typename_type so that lookup finds the injected-class-name
4075 rather than the constructor. */
4077 type_decl
= NULL_TREE
;
4080 cp_parser_parse_tentatively (parser
);
4081 type_decl
= cp_parser_class_name (parser
,
4082 /*typename_keyword_p=*/false,
4083 /*template_keyword_p=*/false,
4085 /*check_dependency=*/false,
4086 /*class_head_p=*/false,
4088 if (cp_parser_parse_definitely (parser
))
4091 /* In "N::S::~S", look in "N" as well. */
4092 if (!done
&& scope
&& qualifying_scope
)
4094 cp_parser_parse_tentatively (parser
);
4095 parser
->scope
= qualifying_scope
;
4096 parser
->object_scope
= NULL_TREE
;
4097 parser
->qualifying_scope
= NULL_TREE
;
4099 = cp_parser_class_name (parser
,
4100 /*typename_keyword_p=*/false,
4101 /*template_keyword_p=*/false,
4103 /*check_dependency=*/false,
4104 /*class_head_p=*/false,
4106 if (cp_parser_parse_definitely (parser
))
4109 /* In "p->S::~T", look in the scope given by "*p" as well. */
4110 else if (!done
&& object_scope
)
4112 cp_parser_parse_tentatively (parser
);
4113 parser
->scope
= object_scope
;
4114 parser
->object_scope
= NULL_TREE
;
4115 parser
->qualifying_scope
= NULL_TREE
;
4117 = cp_parser_class_name (parser
,
4118 /*typename_keyword_p=*/false,
4119 /*template_keyword_p=*/false,
4121 /*check_dependency=*/false,
4122 /*class_head_p=*/false,
4124 if (cp_parser_parse_definitely (parser
))
4127 /* Look in the surrounding context. */
4130 parser
->scope
= NULL_TREE
;
4131 parser
->object_scope
= NULL_TREE
;
4132 parser
->qualifying_scope
= NULL_TREE
;
4133 if (processing_template_decl
)
4134 cp_parser_parse_tentatively (parser
);
4136 = cp_parser_class_name (parser
,
4137 /*typename_keyword_p=*/false,
4138 /*template_keyword_p=*/false,
4140 /*check_dependency=*/false,
4141 /*class_head_p=*/false,
4143 if (processing_template_decl
4144 && ! cp_parser_parse_definitely (parser
))
4146 /* We couldn't find a type with this name, so just accept
4147 it and check for a match at instantiation time. */
4148 type_decl
= cp_parser_identifier (parser
);
4149 if (type_decl
!= error_mark_node
)
4150 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
4154 /* If an error occurred, assume that the name of the
4155 destructor is the same as the name of the qualifying
4156 class. That allows us to keep parsing after running
4157 into ill-formed destructor names. */
4158 if (type_decl
== error_mark_node
&& scope
)
4159 return build_nt (BIT_NOT_EXPR
, scope
);
4160 else if (type_decl
== error_mark_node
)
4161 return error_mark_node
;
4163 /* Check that destructor name and scope match. */
4164 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
4166 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4167 error_at (token
->location
,
4168 "declaration of %<~%T%> as member of %qT",
4170 cp_parser_simulate_error (parser
);
4171 return error_mark_node
;
4176 A typedef-name that names a class shall not be used as the
4177 identifier in the declarator for a destructor declaration. */
4179 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
4180 && !DECL_SELF_REFERENCE_P (type_decl
)
4181 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
4182 error_at (token
->location
,
4183 "typedef-name %qD used as destructor declarator",
4186 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
4190 if (token
->keyword
== RID_OPERATOR
)
4194 /* This could be a template-id, so we try that first. */
4195 cp_parser_parse_tentatively (parser
);
4196 /* Try a template-id. */
4197 id
= cp_parser_template_id (parser
, template_keyword_p
,
4198 /*check_dependency_p=*/true,
4200 /* If that worked, we're done. */
4201 if (cp_parser_parse_definitely (parser
))
4203 /* We still don't know whether we're looking at an
4204 operator-function-id or a conversion-function-id. */
4205 cp_parser_parse_tentatively (parser
);
4206 /* Try an operator-function-id. */
4207 id
= cp_parser_operator_function_id (parser
);
4208 /* If that didn't work, try a conversion-function-id. */
4209 if (!cp_parser_parse_definitely (parser
))
4210 id
= cp_parser_conversion_function_id (parser
);
4219 cp_parser_error (parser
, "expected unqualified-id");
4220 return error_mark_node
;
4224 /* Parse an (optional) nested-name-specifier.
4226 nested-name-specifier: [C++98]
4227 class-or-namespace-name :: nested-name-specifier [opt]
4228 class-or-namespace-name :: template nested-name-specifier [opt]
4230 nested-name-specifier: [C++0x]
4233 nested-name-specifier identifier ::
4234 nested-name-specifier template [opt] simple-template-id ::
4236 PARSER->SCOPE should be set appropriately before this function is
4237 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4238 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4241 Sets PARSER->SCOPE to the class (TYPE) or namespace
4242 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4243 it unchanged if there is no nested-name-specifier. Returns the new
4244 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4246 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4247 part of a declaration and/or decl-specifier. */
4250 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
4251 bool typename_keyword_p
,
4252 bool check_dependency_p
,
4254 bool is_declaration
)
4256 bool success
= false;
4257 cp_token_position start
= 0;
4260 /* Remember where the nested-name-specifier starts. */
4261 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4263 start
= cp_lexer_token_position (parser
->lexer
, false);
4264 push_deferring_access_checks (dk_deferred
);
4271 tree saved_qualifying_scope
;
4272 bool template_keyword_p
;
4274 /* Spot cases that cannot be the beginning of a
4275 nested-name-specifier. */
4276 token
= cp_lexer_peek_token (parser
->lexer
);
4278 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4279 the already parsed nested-name-specifier. */
4280 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
4282 /* Grab the nested-name-specifier and continue the loop. */
4283 cp_parser_pre_parsed_nested_name_specifier (parser
);
4284 /* If we originally encountered this nested-name-specifier
4285 with IS_DECLARATION set to false, we will not have
4286 resolved TYPENAME_TYPEs, so we must do so here. */
4288 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
4290 new_scope
= resolve_typename_type (parser
->scope
,
4291 /*only_current_p=*/false);
4292 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
4293 parser
->scope
= new_scope
;
4299 /* Spot cases that cannot be the beginning of a
4300 nested-name-specifier. On the second and subsequent times
4301 through the loop, we look for the `template' keyword. */
4302 if (success
&& token
->keyword
== RID_TEMPLATE
)
4304 /* A template-id can start a nested-name-specifier. */
4305 else if (token
->type
== CPP_TEMPLATE_ID
)
4309 /* If the next token is not an identifier, then it is
4310 definitely not a type-name or namespace-name. */
4311 if (token
->type
!= CPP_NAME
)
4313 /* If the following token is neither a `<' (to begin a
4314 template-id), nor a `::', then we are not looking at a
4315 nested-name-specifier. */
4316 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
4318 if (token
->type
== CPP_COLON
4319 && parser
->colon_corrects_to_scope_p
4320 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
4322 error_at (token
->location
,
4323 "found %<:%> in nested-name-specifier, expected %<::%>");
4324 token
->type
= CPP_SCOPE
;
4327 if (token
->type
!= CPP_SCOPE
4328 && !cp_parser_nth_token_starts_template_argument_list_p
4333 /* The nested-name-specifier is optional, so we parse
4335 cp_parser_parse_tentatively (parser
);
4337 /* Look for the optional `template' keyword, if this isn't the
4338 first time through the loop. */
4340 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
4342 template_keyword_p
= false;
4344 /* Save the old scope since the name lookup we are about to do
4345 might destroy it. */
4346 old_scope
= parser
->scope
;
4347 saved_qualifying_scope
= parser
->qualifying_scope
;
4348 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4349 look up names in "X<T>::I" in order to determine that "Y" is
4350 a template. So, if we have a typename at this point, we make
4351 an effort to look through it. */
4353 && !typename_keyword_p
4355 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
4356 parser
->scope
= resolve_typename_type (parser
->scope
,
4357 /*only_current_p=*/false);
4358 /* Parse the qualifying entity. */
4360 = cp_parser_qualifying_entity (parser
,
4366 /* Look for the `::' token. */
4367 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
4369 /* If we found what we wanted, we keep going; otherwise, we're
4371 if (!cp_parser_parse_definitely (parser
))
4373 bool error_p
= false;
4375 /* Restore the OLD_SCOPE since it was valid before the
4376 failed attempt at finding the last
4377 class-or-namespace-name. */
4378 parser
->scope
= old_scope
;
4379 parser
->qualifying_scope
= saved_qualifying_scope
;
4380 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4382 /* If the next token is an identifier, and the one after
4383 that is a `::', then any valid interpretation would have
4384 found a class-or-namespace-name. */
4385 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
4386 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4388 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
4391 token
= cp_lexer_consume_token (parser
->lexer
);
4394 if (!token
->ambiguous_p
)
4397 tree ambiguous_decls
;
4399 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
4401 /*is_template=*/false,
4402 /*is_namespace=*/false,
4403 /*check_dependency=*/true,
4406 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4407 error_at (token
->location
,
4408 "%qD used without template parameters",
4410 else if (ambiguous_decls
)
4412 error_at (token
->location
,
4413 "reference to %qD is ambiguous",
4415 print_candidates (ambiguous_decls
);
4416 decl
= error_mark_node
;
4420 if (cxx_dialect
!= cxx98
)
4421 cp_parser_name_lookup_error
4422 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
4425 cp_parser_name_lookup_error
4426 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
4430 parser
->scope
= error_mark_node
;
4432 /* Treat this as a successful nested-name-specifier
4437 If the name found is not a class-name (clause
4438 _class_) or namespace-name (_namespace.def_), the
4439 program is ill-formed. */
4442 cp_lexer_consume_token (parser
->lexer
);
4446 /* We've found one valid nested-name-specifier. */
4448 /* Name lookup always gives us a DECL. */
4449 if (TREE_CODE (new_scope
) == TYPE_DECL
)
4450 new_scope
= TREE_TYPE (new_scope
);
4451 /* Uses of "template" must be followed by actual templates. */
4452 if (template_keyword_p
4453 && !(CLASS_TYPE_P (new_scope
)
4454 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
4455 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
4456 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
4457 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
4458 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
4459 == TEMPLATE_ID_EXPR
)))
4460 permerror (input_location
, TYPE_P (new_scope
)
4461 ? "%qT is not a template"
4462 : "%qD is not a template",
4464 /* If it is a class scope, try to complete it; we are about to
4465 be looking up names inside the class. */
4466 if (TYPE_P (new_scope
)
4467 /* Since checking types for dependency can be expensive,
4468 avoid doing it if the type is already complete. */
4469 && !COMPLETE_TYPE_P (new_scope
)
4470 /* Do not try to complete dependent types. */
4471 && !dependent_type_p (new_scope
))
4473 new_scope
= complete_type (new_scope
);
4474 /* If it is a typedef to current class, use the current
4475 class instead, as the typedef won't have any names inside
4477 if (!COMPLETE_TYPE_P (new_scope
)
4478 && currently_open_class (new_scope
))
4479 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
4481 /* Make sure we look in the right scope the next time through
4483 parser
->scope
= new_scope
;
4486 /* If parsing tentatively, replace the sequence of tokens that makes
4487 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4488 token. That way, should we re-parse the token stream, we will
4489 not have to repeat the effort required to do the parse, nor will
4490 we issue duplicate error messages. */
4491 if (success
&& start
)
4495 token
= cp_lexer_token_at (parser
->lexer
, start
);
4496 /* Reset the contents of the START token. */
4497 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
4498 /* Retrieve any deferred checks. Do not pop this access checks yet
4499 so the memory will not be reclaimed during token replacing below. */
4500 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
4501 token
->u
.tree_check_value
->value
= parser
->scope
;
4502 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
4503 token
->u
.tree_check_value
->qualifying_scope
=
4504 parser
->qualifying_scope
;
4505 token
->keyword
= RID_MAX
;
4507 /* Purge all subsequent tokens. */
4508 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4512 pop_to_parent_deferring_access_checks ();
4514 return success
? parser
->scope
: NULL_TREE
;
4517 /* Parse a nested-name-specifier. See
4518 cp_parser_nested_name_specifier_opt for details. This function
4519 behaves identically, except that it will an issue an error if no
4520 nested-name-specifier is present. */
4523 cp_parser_nested_name_specifier (cp_parser
*parser
,
4524 bool typename_keyword_p
,
4525 bool check_dependency_p
,
4527 bool is_declaration
)
4531 /* Look for the nested-name-specifier. */
4532 scope
= cp_parser_nested_name_specifier_opt (parser
,
4537 /* If it was not present, issue an error message. */
4540 cp_parser_error (parser
, "expected nested-name-specifier");
4541 parser
->scope
= NULL_TREE
;
4547 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4548 this is either a class-name or a namespace-name (which corresponds
4549 to the class-or-namespace-name production in the grammar). For
4550 C++0x, it can also be a type-name that refers to an enumeration
4553 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4554 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4555 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4556 TYPE_P is TRUE iff the next name should be taken as a class-name,
4557 even the same name is declared to be another entity in the same
4560 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4561 specified by the class-or-namespace-name. If neither is found the
4562 ERROR_MARK_NODE is returned. */
4565 cp_parser_qualifying_entity (cp_parser
*parser
,
4566 bool typename_keyword_p
,
4567 bool template_keyword_p
,
4568 bool check_dependency_p
,
4570 bool is_declaration
)
4573 tree saved_qualifying_scope
;
4574 tree saved_object_scope
;
4577 bool successful_parse_p
;
4579 /* Before we try to parse the class-name, we must save away the
4580 current PARSER->SCOPE since cp_parser_class_name will destroy
4582 saved_scope
= parser
->scope
;
4583 saved_qualifying_scope
= parser
->qualifying_scope
;
4584 saved_object_scope
= parser
->object_scope
;
4585 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4586 there is no need to look for a namespace-name. */
4587 only_class_p
= template_keyword_p
4588 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
4590 cp_parser_parse_tentatively (parser
);
4591 scope
= cp_parser_class_name (parser
,
4594 type_p
? class_type
: none_type
,
4596 /*class_head_p=*/false,
4598 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
4599 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4601 && cxx_dialect
!= cxx98
4602 && !successful_parse_p
)
4604 /* Restore the saved scope. */
4605 parser
->scope
= saved_scope
;
4606 parser
->qualifying_scope
= saved_qualifying_scope
;
4607 parser
->object_scope
= saved_object_scope
;
4609 /* Parse tentatively. */
4610 cp_parser_parse_tentatively (parser
);
4612 /* Parse a typedef-name or enum-name. */
4613 scope
= cp_parser_nonclass_name (parser
);
4615 /* "If the name found does not designate a namespace or a class,
4616 enumeration, or dependent type, the program is ill-formed."
4618 We cover classes and dependent types above and namespaces below,
4619 so this code is only looking for enums. */
4620 if (!scope
|| TREE_CODE (scope
) != TYPE_DECL
4621 || TREE_CODE (TREE_TYPE (scope
)) != ENUMERAL_TYPE
)
4622 cp_parser_simulate_error (parser
);
4624 successful_parse_p
= cp_parser_parse_definitely (parser
);
4626 /* If that didn't work, try for a namespace-name. */
4627 if (!only_class_p
&& !successful_parse_p
)
4629 /* Restore the saved scope. */
4630 parser
->scope
= saved_scope
;
4631 parser
->qualifying_scope
= saved_qualifying_scope
;
4632 parser
->object_scope
= saved_object_scope
;
4633 /* If we are not looking at an identifier followed by the scope
4634 resolution operator, then this is not part of a
4635 nested-name-specifier. (Note that this function is only used
4636 to parse the components of a nested-name-specifier.) */
4637 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
4638 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
4639 return error_mark_node
;
4640 scope
= cp_parser_namespace_name (parser
);
4646 /* Parse a postfix-expression.
4650 postfix-expression [ expression ]
4651 postfix-expression ( expression-list [opt] )
4652 simple-type-specifier ( expression-list [opt] )
4653 typename :: [opt] nested-name-specifier identifier
4654 ( expression-list [opt] )
4655 typename :: [opt] nested-name-specifier template [opt] template-id
4656 ( expression-list [opt] )
4657 postfix-expression . template [opt] id-expression
4658 postfix-expression -> template [opt] id-expression
4659 postfix-expression . pseudo-destructor-name
4660 postfix-expression -> pseudo-destructor-name
4661 postfix-expression ++
4662 postfix-expression --
4663 dynamic_cast < type-id > ( expression )
4664 static_cast < type-id > ( expression )
4665 reinterpret_cast < type-id > ( expression )
4666 const_cast < type-id > ( expression )
4667 typeid ( expression )
4673 ( type-id ) { initializer-list , [opt] }
4675 This extension is a GNU version of the C99 compound-literal
4676 construct. (The C99 grammar uses `type-name' instead of `type-id',
4677 but they are essentially the same concept.)
4679 If ADDRESS_P is true, the postfix expression is the operand of the
4680 `&' operator. CAST_P is true if this expression is the target of a
4683 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4684 class member access expressions [expr.ref].
4686 Returns a representation of the expression. */
4689 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
4690 bool member_access_only_p
,
4691 cp_id_kind
* pidk_return
)
4695 cp_id_kind idk
= CP_ID_KIND_NONE
;
4696 tree postfix_expression
= NULL_TREE
;
4697 bool is_member_access
= false;
4699 /* Peek at the next token. */
4700 token
= cp_lexer_peek_token (parser
->lexer
);
4701 /* Some of the productions are determined by keywords. */
4702 keyword
= token
->keyword
;
4712 const char *saved_message
;
4714 /* All of these can be handled in the same way from the point
4715 of view of parsing. Begin by consuming the token
4716 identifying the cast. */
4717 cp_lexer_consume_token (parser
->lexer
);
4719 /* New types cannot be defined in the cast. */
4720 saved_message
= parser
->type_definition_forbidden_message
;
4721 parser
->type_definition_forbidden_message
4722 = G_("types may not be defined in casts");
4724 /* Look for the opening `<'. */
4725 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
4726 /* Parse the type to which we are casting. */
4727 type
= cp_parser_type_id (parser
);
4728 /* Look for the closing `>'. */
4729 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
4730 /* Restore the old message. */
4731 parser
->type_definition_forbidden_message
= saved_message
;
4733 /* And the expression which is being cast. */
4734 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4735 expression
= cp_parser_expression (parser
, /*cast_p=*/true, & idk
);
4736 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4738 /* Only type conversions to integral or enumeration types
4739 can be used in constant-expressions. */
4740 if (!cast_valid_in_integral_constant_expression_p (type
)
4741 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
4742 return error_mark_node
;
4748 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
4752 = build_static_cast (type
, expression
, tf_warning_or_error
);
4756 = build_reinterpret_cast (type
, expression
,
4757 tf_warning_or_error
);
4761 = build_const_cast (type
, expression
, tf_warning_or_error
);
4772 const char *saved_message
;
4773 bool saved_in_type_id_in_expr_p
;
4775 /* Consume the `typeid' token. */
4776 cp_lexer_consume_token (parser
->lexer
);
4777 /* Look for the `(' token. */
4778 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4779 /* Types cannot be defined in a `typeid' expression. */
4780 saved_message
= parser
->type_definition_forbidden_message
;
4781 parser
->type_definition_forbidden_message
4782 = G_("types may not be defined in a %<typeid%> expression");
4783 /* We can't be sure yet whether we're looking at a type-id or an
4785 cp_parser_parse_tentatively (parser
);
4786 /* Try a type-id first. */
4787 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4788 parser
->in_type_id_in_expr_p
= true;
4789 type
= cp_parser_type_id (parser
);
4790 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4791 /* Look for the `)' token. Otherwise, we can't be sure that
4792 we're not looking at an expression: consider `typeid (int
4793 (3))', for example. */
4794 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4795 /* If all went well, simply lookup the type-id. */
4796 if (cp_parser_parse_definitely (parser
))
4797 postfix_expression
= get_typeid (type
);
4798 /* Otherwise, fall back to the expression variant. */
4803 /* Look for an expression. */
4804 expression
= cp_parser_expression (parser
, /*cast_p=*/false, & idk
);
4805 /* Compute its typeid. */
4806 postfix_expression
= build_typeid (expression
);
4807 /* Look for the `)' token. */
4808 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4810 /* Restore the saved message. */
4811 parser
->type_definition_forbidden_message
= saved_message
;
4812 /* `typeid' may not appear in an integral constant expression. */
4813 if (cp_parser_non_integral_constant_expression(parser
, NIC_TYPEID
))
4814 return error_mark_node
;
4821 /* The syntax permitted here is the same permitted for an
4822 elaborated-type-specifier. */
4823 type
= cp_parser_elaborated_type_specifier (parser
,
4824 /*is_friend=*/false,
4825 /*is_declaration=*/false);
4826 postfix_expression
= cp_parser_functional_cast (parser
, type
);
4834 /* If the next thing is a simple-type-specifier, we may be
4835 looking at a functional cast. We could also be looking at
4836 an id-expression. So, we try the functional cast, and if
4837 that doesn't work we fall back to the primary-expression. */
4838 cp_parser_parse_tentatively (parser
);
4839 /* Look for the simple-type-specifier. */
4840 type
= cp_parser_simple_type_specifier (parser
,
4841 /*decl_specs=*/NULL
,
4842 CP_PARSER_FLAGS_NONE
);
4843 /* Parse the cast itself. */
4844 if (!cp_parser_error_occurred (parser
))
4846 = cp_parser_functional_cast (parser
, type
);
4847 /* If that worked, we're done. */
4848 if (cp_parser_parse_definitely (parser
))
4851 /* If the functional-cast didn't work out, try a
4852 compound-literal. */
4853 if (cp_parser_allow_gnu_extensions_p (parser
)
4854 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4856 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
4857 bool saved_in_type_id_in_expr_p
;
4859 cp_parser_parse_tentatively (parser
);
4860 /* Consume the `('. */
4861 cp_lexer_consume_token (parser
->lexer
);
4862 /* Parse the type. */
4863 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4864 parser
->in_type_id_in_expr_p
= true;
4865 type
= cp_parser_type_id (parser
);
4866 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4867 /* Look for the `)'. */
4868 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4869 /* Look for the `{'. */
4870 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
4871 /* If things aren't going well, there's no need to
4873 if (!cp_parser_error_occurred (parser
))
4875 bool non_constant_p
;
4876 /* Parse the initializer-list. */
4878 = cp_parser_initializer_list (parser
, &non_constant_p
);
4879 /* Allow a trailing `,'. */
4880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
4881 cp_lexer_consume_token (parser
->lexer
);
4882 /* Look for the final `}'. */
4883 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
4885 /* If that worked, we're definitely looking at a
4886 compound-literal expression. */
4887 if (cp_parser_parse_definitely (parser
))
4889 /* Warn the user that a compound literal is not
4890 allowed in standard C++. */
4891 pedwarn (input_location
, OPT_pedantic
, "ISO C++ forbids compound-literals");
4892 /* For simplicity, we disallow compound literals in
4893 constant-expressions. We could
4894 allow compound literals of integer type, whose
4895 initializer was a constant, in constant
4896 expressions. Permitting that usage, as a further
4897 extension, would not change the meaning of any
4898 currently accepted programs. (Of course, as
4899 compound literals are not part of ISO C++, the
4900 standard has nothing to say.) */
4901 if (cp_parser_non_integral_constant_expression (parser
,
4904 postfix_expression
= error_mark_node
;
4907 /* Form the representation of the compound-literal. */
4909 = (finish_compound_literal
4910 (type
, build_constructor (init_list_type_node
,
4912 tf_warning_or_error
));
4917 /* It must be a primary-expression. */
4919 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
4920 /*template_arg_p=*/false,
4926 /* Keep looping until the postfix-expression is complete. */
4929 if (idk
== CP_ID_KIND_UNQUALIFIED
4930 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4931 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4932 /* It is not a Koenig lookup function call. */
4934 = unqualified_name_lookup_error (postfix_expression
);
4936 /* Peek at the next token. */
4937 token
= cp_lexer_peek_token (parser
->lexer
);
4939 switch (token
->type
)
4941 case CPP_OPEN_SQUARE
:
4943 = cp_parser_postfix_open_square_expression (parser
,
4946 idk
= CP_ID_KIND_NONE
;
4947 is_member_access
= false;
4950 case CPP_OPEN_PAREN
:
4951 /* postfix-expression ( expression-list [opt] ) */
4954 bool is_builtin_constant_p
;
4955 bool saved_integral_constant_expression_p
= false;
4956 bool saved_non_integral_constant_expression_p
= false;
4959 is_member_access
= false;
4961 is_builtin_constant_p
4962 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
4963 if (is_builtin_constant_p
)
4965 /* The whole point of __builtin_constant_p is to allow
4966 non-constant expressions to appear as arguments. */
4967 saved_integral_constant_expression_p
4968 = parser
->integral_constant_expression_p
;
4969 saved_non_integral_constant_expression_p
4970 = parser
->non_integral_constant_expression_p
;
4971 parser
->integral_constant_expression_p
= false;
4973 args
= (cp_parser_parenthesized_expression_list
4975 /*cast_p=*/false, /*allow_expansion_p=*/true,
4976 /*non_constant_p=*/NULL
));
4977 if (is_builtin_constant_p
)
4979 parser
->integral_constant_expression_p
4980 = saved_integral_constant_expression_p
;
4981 parser
->non_integral_constant_expression_p
4982 = saved_non_integral_constant_expression_p
;
4987 postfix_expression
= error_mark_node
;
4991 /* Function calls are not permitted in
4992 constant-expressions. */
4993 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
4994 && cp_parser_non_integral_constant_expression (parser
,
4997 postfix_expression
= error_mark_node
;
4998 release_tree_vector (args
);
5003 if (idk
== CP_ID_KIND_UNQUALIFIED
5004 || idk
== CP_ID_KIND_TEMPLATE_ID
)
5006 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
5008 if (!VEC_empty (tree
, args
))
5011 if (!any_type_dependent_arguments_p (args
))
5013 = perform_koenig_lookup (postfix_expression
, args
,
5014 /*include_std=*/false);
5018 = unqualified_fn_lookup_error (postfix_expression
);
5020 /* We do not perform argument-dependent lookup if
5021 normal lookup finds a non-function, in accordance
5022 with the expected resolution of DR 218. */
5023 else if (!VEC_empty (tree
, args
)
5024 && is_overloaded_fn (postfix_expression
))
5026 tree fn
= get_first_fn (postfix_expression
);
5027 fn
= STRIP_TEMPLATE (fn
);
5029 /* Do not do argument dependent lookup if regular
5030 lookup finds a member function or a block-scope
5031 function declaration. [basic.lookup.argdep]/3 */
5032 if (!DECL_FUNCTION_MEMBER_P (fn
)
5033 && !DECL_LOCAL_FUNCTION_P (fn
))
5036 if (!any_type_dependent_arguments_p (args
))
5038 = perform_koenig_lookup (postfix_expression
, args
,
5039 /*include_std=*/false);
5044 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
5046 tree instance
= TREE_OPERAND (postfix_expression
, 0);
5047 tree fn
= TREE_OPERAND (postfix_expression
, 1);
5049 if (processing_template_decl
5050 && (type_dependent_expression_p (instance
)
5051 || (!BASELINK_P (fn
)
5052 && TREE_CODE (fn
) != FIELD_DECL
)
5053 || type_dependent_expression_p (fn
)
5054 || any_type_dependent_arguments_p (args
)))
5057 = build_nt_call_vec (postfix_expression
, args
);
5058 release_tree_vector (args
);
5062 if (BASELINK_P (fn
))
5065 = (build_new_method_call
5066 (instance
, fn
, &args
, NULL_TREE
,
5067 (idk
== CP_ID_KIND_QUALIFIED
5068 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
5071 tf_warning_or_error
));
5075 = finish_call_expr (postfix_expression
, &args
,
5076 /*disallow_virtual=*/false,
5078 tf_warning_or_error
);
5080 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
5081 || TREE_CODE (postfix_expression
) == MEMBER_REF
5082 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
5083 postfix_expression
= (build_offset_ref_call_from_tree
5084 (postfix_expression
, &args
));
5085 else if (idk
== CP_ID_KIND_QUALIFIED
)
5086 /* A call to a static class member, or a namespace-scope
5089 = finish_call_expr (postfix_expression
, &args
,
5090 /*disallow_virtual=*/true,
5092 tf_warning_or_error
);
5094 /* All other function calls. */
5096 = finish_call_expr (postfix_expression
, &args
,
5097 /*disallow_virtual=*/false,
5099 tf_warning_or_error
);
5101 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5102 idk
= CP_ID_KIND_NONE
;
5104 release_tree_vector (args
);
5110 /* postfix-expression . template [opt] id-expression
5111 postfix-expression . pseudo-destructor-name
5112 postfix-expression -> template [opt] id-expression
5113 postfix-expression -> pseudo-destructor-name */
5115 /* Consume the `.' or `->' operator. */
5116 cp_lexer_consume_token (parser
->lexer
);
5119 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
5124 is_member_access
= true;
5128 /* postfix-expression ++ */
5129 /* Consume the `++' token. */
5130 cp_lexer_consume_token (parser
->lexer
);
5131 /* Generate a representation for the complete expression. */
5133 = finish_increment_expr (postfix_expression
,
5134 POSTINCREMENT_EXPR
);
5135 /* Increments may not appear in constant-expressions. */
5136 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
5137 postfix_expression
= error_mark_node
;
5138 idk
= CP_ID_KIND_NONE
;
5139 is_member_access
= false;
5142 case CPP_MINUS_MINUS
:
5143 /* postfix-expression -- */
5144 /* Consume the `--' token. */
5145 cp_lexer_consume_token (parser
->lexer
);
5146 /* Generate a representation for the complete expression. */
5148 = finish_increment_expr (postfix_expression
,
5149 POSTDECREMENT_EXPR
);
5150 /* Decrements may not appear in constant-expressions. */
5151 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
5152 postfix_expression
= error_mark_node
;
5153 idk
= CP_ID_KIND_NONE
;
5154 is_member_access
= false;
5158 if (pidk_return
!= NULL
)
5159 * pidk_return
= idk
;
5160 if (member_access_only_p
)
5161 return is_member_access
? postfix_expression
: error_mark_node
;
5163 return postfix_expression
;
5167 /* We should never get here. */
5169 return error_mark_node
;
5172 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5173 by cp_parser_builtin_offsetof. We're looking for
5175 postfix-expression [ expression ]
5177 FOR_OFFSETOF is set if we're being called in that context, which
5178 changes how we deal with integer constant expressions. */
5181 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
5182 tree postfix_expression
,
5187 /* Consume the `[' token. */
5188 cp_lexer_consume_token (parser
->lexer
);
5190 /* Parse the index expression. */
5191 /* ??? For offsetof, there is a question of what to allow here. If
5192 offsetof is not being used in an integral constant expression context,
5193 then we *could* get the right answer by computing the value at runtime.
5194 If we are in an integral constant expression context, then we might
5195 could accept any constant expression; hard to say without analysis.
5196 Rather than open the barn door too wide right away, allow only integer
5197 constant expressions here. */
5199 index
= cp_parser_constant_expression (parser
, false, NULL
);
5201 index
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
5203 /* Look for the closing `]'. */
5204 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
5206 /* Build the ARRAY_REF. */
5207 postfix_expression
= grok_array_decl (postfix_expression
, index
);
5209 /* When not doing offsetof, array references are not permitted in
5210 constant-expressions. */
5212 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
5213 postfix_expression
= error_mark_node
;
5215 return postfix_expression
;
5218 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5219 by cp_parser_builtin_offsetof. We're looking for
5221 postfix-expression . template [opt] id-expression
5222 postfix-expression . pseudo-destructor-name
5223 postfix-expression -> template [opt] id-expression
5224 postfix-expression -> pseudo-destructor-name
5226 FOR_OFFSETOF is set if we're being called in that context. That sorta
5227 limits what of the above we'll actually accept, but nevermind.
5228 TOKEN_TYPE is the "." or "->" token, which will already have been
5229 removed from the stream. */
5232 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
5233 enum cpp_ttype token_type
,
5234 tree postfix_expression
,
5235 bool for_offsetof
, cp_id_kind
*idk
,
5236 location_t location
)
5240 bool pseudo_destructor_p
;
5241 tree scope
= NULL_TREE
;
5243 /* If this is a `->' operator, dereference the pointer. */
5244 if (token_type
== CPP_DEREF
)
5245 postfix_expression
= build_x_arrow (postfix_expression
);
5246 /* Check to see whether or not the expression is type-dependent. */
5247 dependent_p
= type_dependent_expression_p (postfix_expression
);
5248 /* The identifier following the `->' or `.' is not qualified. */
5249 parser
->scope
= NULL_TREE
;
5250 parser
->qualifying_scope
= NULL_TREE
;
5251 parser
->object_scope
= NULL_TREE
;
5252 *idk
= CP_ID_KIND_NONE
;
5254 /* Enter the scope corresponding to the type of the object
5255 given by the POSTFIX_EXPRESSION. */
5256 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
5258 scope
= TREE_TYPE (postfix_expression
);
5259 /* According to the standard, no expression should ever have
5260 reference type. Unfortunately, we do not currently match
5261 the standard in this respect in that our internal representation
5262 of an expression may have reference type even when the standard
5263 says it does not. Therefore, we have to manually obtain the
5264 underlying type here. */
5265 scope
= non_reference (scope
);
5266 /* The type of the POSTFIX_EXPRESSION must be complete. */
5267 if (scope
== unknown_type_node
)
5269 error_at (location
, "%qE does not have class type",
5270 postfix_expression
);
5274 scope
= complete_type_or_else (scope
, NULL_TREE
);
5275 /* Let the name lookup machinery know that we are processing a
5276 class member access expression. */
5277 parser
->context
->object_type
= scope
;
5278 /* If something went wrong, we want to be able to discern that case,
5279 as opposed to the case where there was no SCOPE due to the type
5280 of expression being dependent. */
5282 scope
= error_mark_node
;
5283 /* If the SCOPE was erroneous, make the various semantic analysis
5284 functions exit quickly -- and without issuing additional error
5286 if (scope
== error_mark_node
)
5287 postfix_expression
= error_mark_node
;
5290 /* Assume this expression is not a pseudo-destructor access. */
5291 pseudo_destructor_p
= false;
5293 /* If the SCOPE is a scalar type, then, if this is a valid program,
5294 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5295 is type dependent, it can be pseudo-destructor-name or something else.
5296 Try to parse it as pseudo-destructor-name first. */
5297 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
5302 cp_parser_parse_tentatively (parser
);
5303 /* Parse the pseudo-destructor-name. */
5305 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
5307 && (cp_parser_error_occurred (parser
)
5308 || TREE_CODE (type
) != TYPE_DECL
5309 || !SCALAR_TYPE_P (TREE_TYPE (type
))))
5310 cp_parser_abort_tentative_parse (parser
);
5311 else if (cp_parser_parse_definitely (parser
))
5313 pseudo_destructor_p
= true;
5315 = finish_pseudo_destructor_expr (postfix_expression
,
5316 s
, TREE_TYPE (type
));
5320 if (!pseudo_destructor_p
)
5322 /* If the SCOPE is not a scalar type, we are looking at an
5323 ordinary class member access expression, rather than a
5324 pseudo-destructor-name. */
5326 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
5327 /* Parse the id-expression. */
5328 name
= (cp_parser_id_expression
5330 cp_parser_optional_template_keyword (parser
),
5331 /*check_dependency_p=*/true,
5333 /*declarator_p=*/false,
5334 /*optional_p=*/false));
5335 /* In general, build a SCOPE_REF if the member name is qualified.
5336 However, if the name was not dependent and has already been
5337 resolved; there is no need to build the SCOPE_REF. For example;
5339 struct X { void f(); };
5340 template <typename T> void f(T* t) { t->X::f(); }
5342 Even though "t" is dependent, "X::f" is not and has been resolved
5343 to a BASELINK; there is no need to include scope information. */
5345 /* But we do need to remember that there was an explicit scope for
5346 virtual function calls. */
5348 *idk
= CP_ID_KIND_QUALIFIED
;
5350 /* If the name is a template-id that names a type, we will get a
5351 TYPE_DECL here. That is invalid code. */
5352 if (TREE_CODE (name
) == TYPE_DECL
)
5354 error_at (token
->location
, "invalid use of %qD", name
);
5355 postfix_expression
= error_mark_node
;
5359 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
5361 name
= build_qualified_name (/*type=*/NULL_TREE
,
5365 parser
->scope
= NULL_TREE
;
5366 parser
->qualifying_scope
= NULL_TREE
;
5367 parser
->object_scope
= NULL_TREE
;
5369 if (scope
&& name
&& BASELINK_P (name
))
5370 adjust_result_of_qualified_name_lookup
5371 (name
, BINFO_TYPE (BASELINK_ACCESS_BINFO (name
)), scope
);
5373 = finish_class_member_access_expr (postfix_expression
, name
,
5375 tf_warning_or_error
);
5379 /* We no longer need to look up names in the scope of the object on
5380 the left-hand side of the `.' or `->' operator. */
5381 parser
->context
->object_type
= NULL_TREE
;
5383 /* Outside of offsetof, these operators may not appear in
5384 constant-expressions. */
5386 && (cp_parser_non_integral_constant_expression
5387 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
5388 postfix_expression
= error_mark_node
;
5390 return postfix_expression
;
5393 /* Parse a parenthesized expression-list.
5396 assignment-expression
5397 expression-list, assignment-expression
5402 identifier, expression-list
5404 CAST_P is true if this expression is the target of a cast.
5406 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5409 Returns a vector of trees. Each element is a representation of an
5410 assignment-expression. NULL is returned if the ( and or ) are
5411 missing. An empty, but allocated, vector is returned on no
5412 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5413 if we are parsing an attribute list for an attribute that wants a
5414 plain identifier argument, normal_attr for an attribute that wants
5415 an expression, or non_attr if we aren't parsing an attribute list. If
5416 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5417 not all of the expressions in the list were constant. */
5419 static VEC(tree
,gc
) *
5420 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
5421 int is_attribute_list
,
5423 bool allow_expansion_p
,
5424 bool *non_constant_p
)
5426 VEC(tree
,gc
) *expression_list
;
5427 bool fold_expr_p
= is_attribute_list
!= non_attr
;
5428 tree identifier
= NULL_TREE
;
5429 bool saved_greater_than_is_operator_p
;
5431 /* Assume all the expressions will be constant. */
5433 *non_constant_p
= false;
5435 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
5438 expression_list
= make_tree_vector ();
5440 /* Within a parenthesized expression, a `>' token is always
5441 the greater-than operator. */
5442 saved_greater_than_is_operator_p
5443 = parser
->greater_than_is_operator_p
;
5444 parser
->greater_than_is_operator_p
= true;
5446 /* Consume expressions until there are no more. */
5447 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
5452 /* At the beginning of attribute lists, check to see if the
5453 next token is an identifier. */
5454 if (is_attribute_list
== id_attr
5455 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
5459 /* Consume the identifier. */
5460 token
= cp_lexer_consume_token (parser
->lexer
);
5461 /* Save the identifier. */
5462 identifier
= token
->u
.value
;
5466 bool expr_non_constant_p
;
5468 /* Parse the next assignment-expression. */
5469 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
5471 /* A braced-init-list. */
5472 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
5473 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
5474 if (non_constant_p
&& expr_non_constant_p
)
5475 *non_constant_p
= true;
5477 else if (non_constant_p
)
5479 expr
= (cp_parser_constant_expression
5480 (parser
, /*allow_non_constant_p=*/true,
5481 &expr_non_constant_p
));
5482 if (expr_non_constant_p
)
5483 *non_constant_p
= true;
5486 expr
= cp_parser_assignment_expression (parser
, cast_p
, NULL
);
5489 expr
= fold_non_dependent_expr (expr
);
5491 /* If we have an ellipsis, then this is an expression
5493 if (allow_expansion_p
5494 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5496 /* Consume the `...'. */
5497 cp_lexer_consume_token (parser
->lexer
);
5499 /* Build the argument pack. */
5500 expr
= make_pack_expansion (expr
);
5503 /* Add it to the list. We add error_mark_node
5504 expressions to the list, so that we can still tell if
5505 the correct form for a parenthesized expression-list
5506 is found. That gives better errors. */
5507 VEC_safe_push (tree
, gc
, expression_list
, expr
);
5509 if (expr
== error_mark_node
)
5513 /* After the first item, attribute lists look the same as
5514 expression lists. */
5515 is_attribute_list
= non_attr
;
5518 /* If the next token isn't a `,', then we are done. */
5519 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5522 /* Otherwise, consume the `,' and keep going. */
5523 cp_lexer_consume_token (parser
->lexer
);
5526 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
5531 /* We try and resync to an unnested comma, as that will give the
5532 user better diagnostics. */
5533 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
5534 /*recovering=*/true,
5536 /*consume_paren=*/true);
5541 parser
->greater_than_is_operator_p
5542 = saved_greater_than_is_operator_p
;
5547 parser
->greater_than_is_operator_p
5548 = saved_greater_than_is_operator_p
;
5551 VEC_safe_insert (tree
, gc
, expression_list
, 0, identifier
);
5553 return expression_list
;
5556 /* Parse a pseudo-destructor-name.
5558 pseudo-destructor-name:
5559 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5560 :: [opt] nested-name-specifier template template-id :: ~ type-name
5561 :: [opt] nested-name-specifier [opt] ~ type-name
5563 If either of the first two productions is used, sets *SCOPE to the
5564 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5565 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5566 or ERROR_MARK_NODE if the parse fails. */
5569 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
5573 bool nested_name_specifier_p
;
5575 /* Assume that things will not work out. */
5576 *type
= error_mark_node
;
5578 /* Look for the optional `::' operator. */
5579 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
5580 /* Look for the optional nested-name-specifier. */
5581 nested_name_specifier_p
5582 = (cp_parser_nested_name_specifier_opt (parser
,
5583 /*typename_keyword_p=*/false,
5584 /*check_dependency_p=*/true,
5586 /*is_declaration=*/false)
5588 /* Now, if we saw a nested-name-specifier, we might be doing the
5589 second production. */
5590 if (nested_name_specifier_p
5591 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
5593 /* Consume the `template' keyword. */
5594 cp_lexer_consume_token (parser
->lexer
);
5595 /* Parse the template-id. */
5596 cp_parser_template_id (parser
,
5597 /*template_keyword_p=*/true,
5598 /*check_dependency_p=*/false,
5599 /*is_declaration=*/true);
5600 /* Look for the `::' token. */
5601 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5603 /* If the next token is not a `~', then there might be some
5604 additional qualification. */
5605 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
5607 /* At this point, we're looking for "type-name :: ~". The type-name
5608 must not be a class-name, since this is a pseudo-destructor. So,
5609 it must be either an enum-name, or a typedef-name -- both of which
5610 are just identifiers. So, we peek ahead to check that the "::"
5611 and "~" tokens are present; if they are not, then we can avoid
5612 calling type_name. */
5613 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
5614 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
5615 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
5617 cp_parser_error (parser
, "non-scalar type");
5621 /* Look for the type-name. */
5622 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
5623 if (*scope
== error_mark_node
)
5626 /* Look for the `::' token. */
5627 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5632 /* Look for the `~'. */
5633 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
5634 /* Look for the type-name again. We are not responsible for
5635 checking that it matches the first type-name. */
5636 *type
= cp_parser_nonclass_name (parser
);
5639 /* Parse a unary-expression.
5645 unary-operator cast-expression
5646 sizeof unary-expression
5648 alignof ( type-id ) [C++0x]
5655 __extension__ cast-expression
5656 __alignof__ unary-expression
5657 __alignof__ ( type-id )
5658 alignof unary-expression [C++0x]
5659 __real__ cast-expression
5660 __imag__ cast-expression
5663 ADDRESS_P is true iff the unary-expression is appearing as the
5664 operand of the `&' operator. CAST_P is true if this expression is
5665 the target of a cast.
5667 Returns a representation of the expression. */
5670 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5674 enum tree_code unary_operator
;
5676 /* Peek at the next token. */
5677 token
= cp_lexer_peek_token (parser
->lexer
);
5678 /* Some keywords give away the kind of expression. */
5679 if (token
->type
== CPP_KEYWORD
)
5681 enum rid keyword
= token
->keyword
;
5691 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
5692 /* Consume the token. */
5693 cp_lexer_consume_token (parser
->lexer
);
5694 /* Parse the operand. */
5695 operand
= cp_parser_sizeof_operand (parser
, keyword
);
5697 if (TYPE_P (operand
))
5698 return cxx_sizeof_or_alignof_type (operand
, op
, true);
5701 /* ISO C++ defines alignof only with types, not with
5702 expressions. So pedwarn if alignof is used with a non-
5703 type expression. However, __alignof__ is ok. */
5704 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
5705 pedwarn (token
->location
, OPT_pedantic
,
5706 "ISO C++ does not allow %<alignof%> "
5709 return cxx_sizeof_or_alignof_expr (operand
, op
, true);
5714 return cp_parser_new_expression (parser
);
5717 return cp_parser_delete_expression (parser
);
5721 /* The saved value of the PEDANTIC flag. */
5725 /* Save away the PEDANTIC flag. */
5726 cp_parser_extension_opt (parser
, &saved_pedantic
);
5727 /* Parse the cast-expression. */
5728 expr
= cp_parser_simple_cast_expression (parser
);
5729 /* Restore the PEDANTIC flag. */
5730 pedantic
= saved_pedantic
;
5740 /* Consume the `__real__' or `__imag__' token. */
5741 cp_lexer_consume_token (parser
->lexer
);
5742 /* Parse the cast-expression. */
5743 expression
= cp_parser_simple_cast_expression (parser
);
5744 /* Create the complete representation. */
5745 return build_x_unary_op ((keyword
== RID_REALPART
5746 ? REALPART_EXPR
: IMAGPART_EXPR
),
5748 tf_warning_or_error
);
5755 const char *saved_message
;
5756 bool saved_integral_constant_expression_p
;
5757 bool saved_non_integral_constant_expression_p
;
5758 bool saved_greater_than_is_operator_p
;
5760 cp_lexer_consume_token (parser
->lexer
);
5761 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5763 saved_message
= parser
->type_definition_forbidden_message
;
5764 parser
->type_definition_forbidden_message
5765 = G_("types may not be defined in %<noexcept%> expressions");
5767 saved_integral_constant_expression_p
5768 = parser
->integral_constant_expression_p
;
5769 saved_non_integral_constant_expression_p
5770 = parser
->non_integral_constant_expression_p
;
5771 parser
->integral_constant_expression_p
= false;
5773 saved_greater_than_is_operator_p
5774 = parser
->greater_than_is_operator_p
;
5775 parser
->greater_than_is_operator_p
= true;
5777 ++cp_unevaluated_operand
;
5778 ++c_inhibit_evaluation_warnings
;
5779 expr
= cp_parser_expression (parser
, false, NULL
);
5780 --c_inhibit_evaluation_warnings
;
5781 --cp_unevaluated_operand
;
5783 parser
->greater_than_is_operator_p
5784 = saved_greater_than_is_operator_p
;
5786 parser
->integral_constant_expression_p
5787 = saved_integral_constant_expression_p
;
5788 parser
->non_integral_constant_expression_p
5789 = saved_non_integral_constant_expression_p
;
5791 parser
->type_definition_forbidden_message
= saved_message
;
5793 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5794 return finish_noexcept_expr (expr
, tf_warning_or_error
);
5802 /* Look for the `:: new' and `:: delete', which also signal the
5803 beginning of a new-expression, or delete-expression,
5804 respectively. If the next token is `::', then it might be one of
5806 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
5810 /* See if the token after the `::' is one of the keywords in
5811 which we're interested. */
5812 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
5813 /* If it's `new', we have a new-expression. */
5814 if (keyword
== RID_NEW
)
5815 return cp_parser_new_expression (parser
);
5816 /* Similarly, for `delete'. */
5817 else if (keyword
== RID_DELETE
)
5818 return cp_parser_delete_expression (parser
);
5821 /* Look for a unary operator. */
5822 unary_operator
= cp_parser_unary_operator (token
);
5823 /* The `++' and `--' operators can be handled similarly, even though
5824 they are not technically unary-operators in the grammar. */
5825 if (unary_operator
== ERROR_MARK
)
5827 if (token
->type
== CPP_PLUS_PLUS
)
5828 unary_operator
= PREINCREMENT_EXPR
;
5829 else if (token
->type
== CPP_MINUS_MINUS
)
5830 unary_operator
= PREDECREMENT_EXPR
;
5831 /* Handle the GNU address-of-label extension. */
5832 else if (cp_parser_allow_gnu_extensions_p (parser
)
5833 && token
->type
== CPP_AND_AND
)
5837 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
5839 /* Consume the '&&' token. */
5840 cp_lexer_consume_token (parser
->lexer
);
5841 /* Look for the identifier. */
5842 identifier
= cp_parser_identifier (parser
);
5843 /* Create an expression representing the address. */
5844 expression
= finish_label_address_expr (identifier
, loc
);
5845 if (cp_parser_non_integral_constant_expression (parser
,
5847 expression
= error_mark_node
;
5851 if (unary_operator
!= ERROR_MARK
)
5853 tree cast_expression
;
5854 tree expression
= error_mark_node
;
5855 non_integral_constant non_constant_p
= NIC_NONE
;
5857 /* Consume the operator token. */
5858 token
= cp_lexer_consume_token (parser
->lexer
);
5859 /* Parse the cast-expression. */
5861 = cp_parser_cast_expression (parser
,
5862 unary_operator
== ADDR_EXPR
,
5863 /*cast_p=*/false, pidk
);
5864 /* Now, build an appropriate representation. */
5865 switch (unary_operator
)
5868 non_constant_p
= NIC_STAR
;
5869 expression
= build_x_indirect_ref (cast_expression
, RO_UNARY_STAR
,
5870 tf_warning_or_error
);
5874 non_constant_p
= NIC_ADDR
;
5877 expression
= build_x_unary_op (unary_operator
, cast_expression
,
5878 tf_warning_or_error
);
5881 case PREINCREMENT_EXPR
:
5882 case PREDECREMENT_EXPR
:
5883 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
5884 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
5886 case UNARY_PLUS_EXPR
:
5888 case TRUTH_NOT_EXPR
:
5889 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
5896 if (non_constant_p
!= NIC_NONE
5897 && cp_parser_non_integral_constant_expression (parser
,
5899 expression
= error_mark_node
;
5904 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
5905 /*member_access_only_p=*/false,
5909 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5910 unary-operator, the corresponding tree code is returned. */
5912 static enum tree_code
5913 cp_parser_unary_operator (cp_token
* token
)
5915 switch (token
->type
)
5918 return INDIRECT_REF
;
5924 return UNARY_PLUS_EXPR
;
5930 return TRUTH_NOT_EXPR
;
5933 return BIT_NOT_EXPR
;
5940 /* Parse a new-expression.
5943 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5944 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5946 Returns a representation of the expression. */
5949 cp_parser_new_expression (cp_parser
* parser
)
5951 bool global_scope_p
;
5952 VEC(tree
,gc
) *placement
;
5954 VEC(tree
,gc
) *initializer
;
5958 /* Look for the optional `::' operator. */
5960 = (cp_parser_global_scope_opt (parser
,
5961 /*current_scope_valid_p=*/false)
5963 /* Look for the `new' operator. */
5964 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
5965 /* There's no easy way to tell a new-placement from the
5966 `( type-id )' construct. */
5967 cp_parser_parse_tentatively (parser
);
5968 /* Look for a new-placement. */
5969 placement
= cp_parser_new_placement (parser
);
5970 /* If that didn't work out, there's no new-placement. */
5971 if (!cp_parser_parse_definitely (parser
))
5973 if (placement
!= NULL
)
5974 release_tree_vector (placement
);
5978 /* If the next token is a `(', then we have a parenthesized
5980 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5983 /* Consume the `('. */
5984 cp_lexer_consume_token (parser
->lexer
);
5985 /* Parse the type-id. */
5986 type
= cp_parser_type_id (parser
);
5987 /* Look for the closing `)'. */
5988 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5989 token
= cp_lexer_peek_token (parser
->lexer
);
5990 /* There should not be a direct-new-declarator in this production,
5991 but GCC used to allowed this, so we check and emit a sensible error
5992 message for this case. */
5993 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5995 error_at (token
->location
,
5996 "array bound forbidden after parenthesized type-id");
5997 inform (token
->location
,
5998 "try removing the parentheses around the type-id");
5999 cp_parser_direct_new_declarator (parser
);
6003 /* Otherwise, there must be a new-type-id. */
6005 type
= cp_parser_new_type_id (parser
, &nelts
);
6007 /* If the next token is a `(' or '{', then we have a new-initializer. */
6008 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
6009 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6010 initializer
= cp_parser_new_initializer (parser
);
6014 /* A new-expression may not appear in an integral constant
6016 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
6017 ret
= error_mark_node
;
6020 /* Create a representation of the new-expression. */
6021 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
6022 tf_warning_or_error
);
6025 if (placement
!= NULL
)
6026 release_tree_vector (placement
);
6027 if (initializer
!= NULL
)
6028 release_tree_vector (initializer
);
6033 /* Parse a new-placement.
6038 Returns the same representation as for an expression-list. */
6040 static VEC(tree
,gc
) *
6041 cp_parser_new_placement (cp_parser
* parser
)
6043 VEC(tree
,gc
) *expression_list
;
6045 /* Parse the expression-list. */
6046 expression_list
= (cp_parser_parenthesized_expression_list
6047 (parser
, non_attr
, /*cast_p=*/false,
6048 /*allow_expansion_p=*/true,
6049 /*non_constant_p=*/NULL
));
6051 return expression_list
;
6054 /* Parse a new-type-id.
6057 type-specifier-seq new-declarator [opt]
6059 Returns the TYPE allocated. If the new-type-id indicates an array
6060 type, *NELTS is set to the number of elements in the last array
6061 bound; the TYPE will not include the last array bound. */
6064 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
6066 cp_decl_specifier_seq type_specifier_seq
;
6067 cp_declarator
*new_declarator
;
6068 cp_declarator
*declarator
;
6069 cp_declarator
*outer_declarator
;
6070 const char *saved_message
;
6073 /* The type-specifier sequence must not contain type definitions.
6074 (It cannot contain declarations of new types either, but if they
6075 are not definitions we will catch that because they are not
6077 saved_message
= parser
->type_definition_forbidden_message
;
6078 parser
->type_definition_forbidden_message
6079 = G_("types may not be defined in a new-type-id");
6080 /* Parse the type-specifier-seq. */
6081 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
6082 /*is_trailing_return=*/false,
6083 &type_specifier_seq
);
6084 /* Restore the old message. */
6085 parser
->type_definition_forbidden_message
= saved_message
;
6086 /* Parse the new-declarator. */
6087 new_declarator
= cp_parser_new_declarator_opt (parser
);
6089 /* Determine the number of elements in the last array dimension, if
6092 /* Skip down to the last array dimension. */
6093 declarator
= new_declarator
;
6094 outer_declarator
= NULL
;
6095 while (declarator
&& (declarator
->kind
== cdk_pointer
6096 || declarator
->kind
== cdk_ptrmem
))
6098 outer_declarator
= declarator
;
6099 declarator
= declarator
->declarator
;
6102 && declarator
->kind
== cdk_array
6103 && declarator
->declarator
6104 && declarator
->declarator
->kind
== cdk_array
)
6106 outer_declarator
= declarator
;
6107 declarator
= declarator
->declarator
;
6110 if (declarator
&& declarator
->kind
== cdk_array
)
6112 *nelts
= declarator
->u
.array
.bounds
;
6113 if (*nelts
== error_mark_node
)
6114 *nelts
= integer_one_node
;
6116 if (outer_declarator
)
6117 outer_declarator
->declarator
= declarator
->declarator
;
6119 new_declarator
= NULL
;
6122 type
= groktypename (&type_specifier_seq
, new_declarator
, false);
6126 /* Parse an (optional) new-declarator.
6129 ptr-operator new-declarator [opt]
6130 direct-new-declarator
6132 Returns the declarator. */
6134 static cp_declarator
*
6135 cp_parser_new_declarator_opt (cp_parser
* parser
)
6137 enum tree_code code
;
6139 cp_cv_quals cv_quals
;
6141 /* We don't know if there's a ptr-operator next, or not. */
6142 cp_parser_parse_tentatively (parser
);
6143 /* Look for a ptr-operator. */
6144 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
6145 /* If that worked, look for more new-declarators. */
6146 if (cp_parser_parse_definitely (parser
))
6148 cp_declarator
*declarator
;
6150 /* Parse another optional declarator. */
6151 declarator
= cp_parser_new_declarator_opt (parser
);
6153 return cp_parser_make_indirect_declarator
6154 (code
, type
, cv_quals
, declarator
);
6157 /* If the next token is a `[', there is a direct-new-declarator. */
6158 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6159 return cp_parser_direct_new_declarator (parser
);
6164 /* Parse a direct-new-declarator.
6166 direct-new-declarator:
6168 direct-new-declarator [constant-expression]
6172 static cp_declarator
*
6173 cp_parser_direct_new_declarator (cp_parser
* parser
)
6175 cp_declarator
*declarator
= NULL
;
6181 /* Look for the opening `['. */
6182 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
6183 /* The first expression is not required to be constant. */
6186 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6187 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
6188 /* The standard requires that the expression have integral
6189 type. DR 74 adds enumeration types. We believe that the
6190 real intent is that these expressions be handled like the
6191 expression in a `switch' condition, which also allows
6192 classes with a single conversion to integral or
6193 enumeration type. */
6194 if (!processing_template_decl
)
6197 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
6202 error_at (token
->location
,
6203 "expression in new-declarator must have integral "
6204 "or enumeration type");
6205 expression
= error_mark_node
;
6209 /* But all the other expressions must be. */
6212 = cp_parser_constant_expression (parser
,
6213 /*allow_non_constant=*/false,
6215 /* Look for the closing `]'. */
6216 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6218 /* Add this bound to the declarator. */
6219 declarator
= make_array_declarator (declarator
, expression
);
6221 /* If the next token is not a `[', then there are no more
6223 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
6230 /* Parse a new-initializer.
6233 ( expression-list [opt] )
6236 Returns a representation of the expression-list. */
6238 static VEC(tree
,gc
) *
6239 cp_parser_new_initializer (cp_parser
* parser
)
6241 VEC(tree
,gc
) *expression_list
;
6243 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6246 bool expr_non_constant_p
;
6247 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6248 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6249 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
6250 expression_list
= make_tree_vector_single (t
);
6253 expression_list
= (cp_parser_parenthesized_expression_list
6254 (parser
, non_attr
, /*cast_p=*/false,
6255 /*allow_expansion_p=*/true,
6256 /*non_constant_p=*/NULL
));
6258 return expression_list
;
6261 /* Parse a delete-expression.
6264 :: [opt] delete cast-expression
6265 :: [opt] delete [ ] cast-expression
6267 Returns a representation of the expression. */
6270 cp_parser_delete_expression (cp_parser
* parser
)
6272 bool global_scope_p
;
6276 /* Look for the optional `::' operator. */
6278 = (cp_parser_global_scope_opt (parser
,
6279 /*current_scope_valid_p=*/false)
6281 /* Look for the `delete' keyword. */
6282 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
6283 /* See if the array syntax is in use. */
6284 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6286 /* Consume the `[' token. */
6287 cp_lexer_consume_token (parser
->lexer
);
6288 /* Look for the `]' token. */
6289 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6290 /* Remember that this is the `[]' construct. */
6296 /* Parse the cast-expression. */
6297 expression
= cp_parser_simple_cast_expression (parser
);
6299 /* A delete-expression may not appear in an integral constant
6301 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
6302 return error_mark_node
;
6304 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
6305 tf_warning_or_error
);
6308 /* Returns true if TOKEN may start a cast-expression and false
6312 cp_parser_token_starts_cast_expression (cp_token
*token
)
6314 switch (token
->type
)
6320 case CPP_CLOSE_SQUARE
:
6321 case CPP_CLOSE_PAREN
:
6322 case CPP_CLOSE_BRACE
:
6326 case CPP_DEREF_STAR
:
6334 case CPP_GREATER_EQ
:
6354 /* '[' may start a primary-expression in obj-c++. */
6355 case CPP_OPEN_SQUARE
:
6356 return c_dialect_objc ();
6363 /* Parse a cast-expression.
6367 ( type-id ) cast-expression
6369 ADDRESS_P is true iff the unary-expression is appearing as the
6370 operand of the `&' operator. CAST_P is true if this expression is
6371 the target of a cast.
6373 Returns a representation of the expression. */
6376 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6379 /* If it's a `(', then we might be looking at a cast. */
6380 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6382 tree type
= NULL_TREE
;
6383 tree expr
= NULL_TREE
;
6384 bool compound_literal_p
;
6385 const char *saved_message
;
6387 /* There's no way to know yet whether or not this is a cast.
6388 For example, `(int (3))' is a unary-expression, while `(int)
6389 3' is a cast. So, we resort to parsing tentatively. */
6390 cp_parser_parse_tentatively (parser
);
6391 /* Types may not be defined in a cast. */
6392 saved_message
= parser
->type_definition_forbidden_message
;
6393 parser
->type_definition_forbidden_message
6394 = G_("types may not be defined in casts");
6395 /* Consume the `('. */
6396 cp_lexer_consume_token (parser
->lexer
);
6397 /* A very tricky bit is that `(struct S) { 3 }' is a
6398 compound-literal (which we permit in C++ as an extension).
6399 But, that construct is not a cast-expression -- it is a
6400 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6401 is legal; if the compound-literal were a cast-expression,
6402 you'd need an extra set of parentheses.) But, if we parse
6403 the type-id, and it happens to be a class-specifier, then we
6404 will commit to the parse at that point, because we cannot
6405 undo the action that is done when creating a new class. So,
6406 then we cannot back up and do a postfix-expression.
6408 Therefore, we scan ahead to the closing `)', and check to see
6409 if the token after the `)' is a `{'. If so, we are not
6410 looking at a cast-expression.
6412 Save tokens so that we can put them back. */
6413 cp_lexer_save_tokens (parser
->lexer
);
6414 /* Skip tokens until the next token is a closing parenthesis.
6415 If we find the closing `)', and the next token is a `{', then
6416 we are looking at a compound-literal. */
6418 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6419 /*consume_paren=*/true)
6420 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6421 /* Roll back the tokens we skipped. */
6422 cp_lexer_rollback_tokens (parser
->lexer
);
6423 /* If we were looking at a compound-literal, simulate an error
6424 so that the call to cp_parser_parse_definitely below will
6426 if (compound_literal_p
)
6427 cp_parser_simulate_error (parser
);
6430 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6431 parser
->in_type_id_in_expr_p
= true;
6432 /* Look for the type-id. */
6433 type
= cp_parser_type_id (parser
);
6434 /* Look for the closing `)'. */
6435 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6436 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6439 /* Restore the saved message. */
6440 parser
->type_definition_forbidden_message
= saved_message
;
6442 /* At this point this can only be either a cast or a
6443 parenthesized ctor such as `(T ())' that looks like a cast to
6444 function returning T. */
6445 if (!cp_parser_error_occurred (parser
)
6446 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6449 cp_parser_parse_definitely (parser
);
6450 expr
= cp_parser_cast_expression (parser
,
6451 /*address_p=*/false,
6452 /*cast_p=*/true, pidk
);
6454 /* Warn about old-style casts, if so requested. */
6455 if (warn_old_style_cast
6456 && !in_system_header
6457 && !VOID_TYPE_P (type
)
6458 && current_lang_name
!= lang_name_c
)
6459 warning (OPT_Wold_style_cast
, "use of old-style cast");
6461 /* Only type conversions to integral or enumeration types
6462 can be used in constant-expressions. */
6463 if (!cast_valid_in_integral_constant_expression_p (type
)
6464 && cp_parser_non_integral_constant_expression (parser
,
6466 return error_mark_node
;
6468 /* Perform the cast. */
6469 expr
= build_c_cast (input_location
, type
, expr
);
6473 cp_parser_abort_tentative_parse (parser
);
6476 /* If we get here, then it's not a cast, so it must be a
6477 unary-expression. */
6478 return cp_parser_unary_expression (parser
, address_p
, cast_p
, pidk
);
6481 /* Parse a binary expression of the general form:
6485 pm-expression .* cast-expression
6486 pm-expression ->* cast-expression
6488 multiplicative-expression:
6490 multiplicative-expression * pm-expression
6491 multiplicative-expression / pm-expression
6492 multiplicative-expression % pm-expression
6494 additive-expression:
6495 multiplicative-expression
6496 additive-expression + multiplicative-expression
6497 additive-expression - multiplicative-expression
6501 shift-expression << additive-expression
6502 shift-expression >> additive-expression
6504 relational-expression:
6506 relational-expression < shift-expression
6507 relational-expression > shift-expression
6508 relational-expression <= shift-expression
6509 relational-expression >= shift-expression
6513 relational-expression:
6514 relational-expression <? shift-expression
6515 relational-expression >? shift-expression
6517 equality-expression:
6518 relational-expression
6519 equality-expression == relational-expression
6520 equality-expression != relational-expression
6524 and-expression & equality-expression
6526 exclusive-or-expression:
6528 exclusive-or-expression ^ and-expression
6530 inclusive-or-expression:
6531 exclusive-or-expression
6532 inclusive-or-expression | exclusive-or-expression
6534 logical-and-expression:
6535 inclusive-or-expression
6536 logical-and-expression && inclusive-or-expression
6538 logical-or-expression:
6539 logical-and-expression
6540 logical-or-expression || logical-and-expression
6542 All these are implemented with a single function like:
6545 simple-cast-expression
6546 binary-expression <token> binary-expression
6548 CAST_P is true if this expression is the target of a cast.
6550 The binops_by_token map is used to get the tree codes for each <token> type.
6551 binary-expressions are associated according to a precedence table. */
6553 #define TOKEN_PRECEDENCE(token) \
6554 (((token->type == CPP_GREATER \
6555 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6556 && !parser->greater_than_is_operator_p) \
6557 ? PREC_NOT_OPERATOR \
6558 : binops_by_token[token->type].prec)
6561 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
6562 bool no_toplevel_fold_p
,
6563 enum cp_parser_prec prec
,
6566 cp_parser_expression_stack stack
;
6567 cp_parser_expression_stack_entry
*sp
= &stack
[0];
6570 enum tree_code tree_type
, lhs_type
, rhs_type
;
6571 enum cp_parser_prec new_prec
, lookahead_prec
;
6574 /* Parse the first expression. */
6575 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
, pidk
);
6576 lhs_type
= ERROR_MARK
;
6580 /* Get an operator token. */
6581 token
= cp_lexer_peek_token (parser
->lexer
);
6583 if (warn_cxx0x_compat
6584 && token
->type
== CPP_RSHIFT
6585 && !parser
->greater_than_is_operator_p
)
6587 if (warning_at (token
->location
, OPT_Wc__0x_compat
,
6588 "%<>>%> operator will be treated as"
6589 " two right angle brackets in C++0x"))
6590 inform (token
->location
,
6591 "suggest parentheses around %<>>%> expression");
6594 new_prec
= TOKEN_PRECEDENCE (token
);
6596 /* Popping an entry off the stack means we completed a subexpression:
6597 - either we found a token which is not an operator (`>' where it is not
6598 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6599 will happen repeatedly;
6600 - or, we found an operator which has lower priority. This is the case
6601 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6603 if (new_prec
<= prec
)
6612 tree_type
= binops_by_token
[token
->type
].tree_type
;
6614 /* We used the operator token. */
6615 cp_lexer_consume_token (parser
->lexer
);
6617 /* For "false && x" or "true || x", x will never be executed;
6618 disable warnings while evaluating it. */
6619 if (tree_type
== TRUTH_ANDIF_EXPR
)
6620 c_inhibit_evaluation_warnings
+= lhs
== truthvalue_false_node
;
6621 else if (tree_type
== TRUTH_ORIF_EXPR
)
6622 c_inhibit_evaluation_warnings
+= lhs
== truthvalue_true_node
;
6624 /* Extract another operand. It may be the RHS of this expression
6625 or the LHS of a new, higher priority expression. */
6626 rhs
= cp_parser_simple_cast_expression (parser
);
6627 rhs_type
= ERROR_MARK
;
6629 /* Get another operator token. Look up its precedence to avoid
6630 building a useless (immediately popped) stack entry for common
6631 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6632 token
= cp_lexer_peek_token (parser
->lexer
);
6633 lookahead_prec
= TOKEN_PRECEDENCE (token
);
6634 if (lookahead_prec
> new_prec
)
6636 /* ... and prepare to parse the RHS of the new, higher priority
6637 expression. Since precedence levels on the stack are
6638 monotonically increasing, we do not have to care about
6641 sp
->tree_type
= tree_type
;
6643 sp
->lhs_type
= lhs_type
;
6646 lhs_type
= rhs_type
;
6648 new_prec
= lookahead_prec
;
6652 lookahead_prec
= new_prec
;
6653 /* If the stack is not empty, we have parsed into LHS the right side
6654 (`4' in the example above) of an expression we had suspended.
6655 We can use the information on the stack to recover the LHS (`3')
6656 from the stack together with the tree code (`MULT_EXPR'), and
6657 the precedence of the higher level subexpression
6658 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6659 which will be used to actually build the additive expression. */
6662 tree_type
= sp
->tree_type
;
6664 rhs_type
= lhs_type
;
6666 lhs_type
= sp
->lhs_type
;
6669 /* Undo the disabling of warnings done above. */
6670 if (tree_type
== TRUTH_ANDIF_EXPR
)
6671 c_inhibit_evaluation_warnings
-= lhs
== truthvalue_false_node
;
6672 else if (tree_type
== TRUTH_ORIF_EXPR
)
6673 c_inhibit_evaluation_warnings
-= lhs
== truthvalue_true_node
;
6675 overloaded_p
= false;
6676 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6677 ERROR_MARK for everything that is not a binary expression.
6678 This makes warn_about_parentheses miss some warnings that
6679 involve unary operators. For unary expressions we should
6680 pass the correct tree_code unless the unary expression was
6681 surrounded by parentheses.
6683 if (no_toplevel_fold_p
6684 && lookahead_prec
<= prec
6686 && TREE_CODE_CLASS (tree_type
) == tcc_comparison
)
6687 lhs
= build2 (tree_type
, boolean_type_node
, lhs
, rhs
);
6689 lhs
= build_x_binary_op (tree_type
, lhs
, lhs_type
, rhs
, rhs_type
,
6690 &overloaded_p
, tf_warning_or_error
);
6691 lhs_type
= tree_type
;
6693 /* If the binary operator required the use of an overloaded operator,
6694 then this expression cannot be an integral constant-expression.
6695 An overloaded operator can be used even if both operands are
6696 otherwise permissible in an integral constant-expression if at
6697 least one of the operands is of enumeration type. */
6700 && cp_parser_non_integral_constant_expression (parser
,
6702 return error_mark_node
;
6709 /* Parse the `? expression : assignment-expression' part of a
6710 conditional-expression. The LOGICAL_OR_EXPR is the
6711 logical-or-expression that started the conditional-expression.
6712 Returns a representation of the entire conditional-expression.
6714 This routine is used by cp_parser_assignment_expression.
6716 ? expression : assignment-expression
6720 ? : assignment-expression */
6723 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
6726 tree assignment_expr
;
6727 struct cp_token
*token
;
6729 /* Consume the `?' token. */
6730 cp_lexer_consume_token (parser
->lexer
);
6731 token
= cp_lexer_peek_token (parser
->lexer
);
6732 if (cp_parser_allow_gnu_extensions_p (parser
)
6733 && token
->type
== CPP_COLON
)
6735 pedwarn (token
->location
, OPT_pedantic
,
6736 "ISO C++ does not allow ?: with omitted middle operand");
6737 /* Implicit true clause. */
6739 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
6740 warn_for_omitted_condop (token
->location
, logical_or_expr
);
6744 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
6745 parser
->colon_corrects_to_scope_p
= false;
6746 /* Parse the expression. */
6747 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
6748 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
6749 c_inhibit_evaluation_warnings
+=
6750 ((logical_or_expr
== truthvalue_true_node
)
6751 - (logical_or_expr
== truthvalue_false_node
));
6752 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
6755 /* The next token should be a `:'. */
6756 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
6757 /* Parse the assignment-expression. */
6758 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
6759 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
6761 /* Build the conditional-expression. */
6762 return build_x_conditional_expr (logical_or_expr
,
6765 tf_warning_or_error
);
6768 /* Parse an assignment-expression.
6770 assignment-expression:
6771 conditional-expression
6772 logical-or-expression assignment-operator assignment_expression
6775 CAST_P is true if this expression is the target of a cast.
6777 Returns a representation for the expression. */
6780 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
6785 /* If the next token is the `throw' keyword, then we're looking at
6786 a throw-expression. */
6787 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
6788 expr
= cp_parser_throw_expression (parser
);
6789 /* Otherwise, it must be that we are looking at a
6790 logical-or-expression. */
6793 /* Parse the binary expressions (logical-or-expression). */
6794 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
6795 PREC_NOT_OPERATOR
, pidk
);
6796 /* If the next token is a `?' then we're actually looking at a
6797 conditional-expression. */
6798 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
6799 return cp_parser_question_colon_clause (parser
, expr
);
6802 enum tree_code assignment_operator
;
6804 /* If it's an assignment-operator, we're using the second
6807 = cp_parser_assignment_operator_opt (parser
);
6808 if (assignment_operator
!= ERROR_MARK
)
6810 bool non_constant_p
;
6812 /* Parse the right-hand side of the assignment. */
6813 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
6815 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
6816 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6818 /* An assignment may not appear in a
6819 constant-expression. */
6820 if (cp_parser_non_integral_constant_expression (parser
,
6822 return error_mark_node
;
6823 /* Build the assignment expression. */
6824 expr
= build_x_modify_expr (expr
,
6825 assignment_operator
,
6827 tf_warning_or_error
);
6835 /* Parse an (optional) assignment-operator.
6837 assignment-operator: one of
6838 = *= /= %= += -= >>= <<= &= ^= |=
6842 assignment-operator: one of
6845 If the next token is an assignment operator, the corresponding tree
6846 code is returned, and the token is consumed. For example, for
6847 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6848 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6849 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6850 operator, ERROR_MARK is returned. */
6852 static enum tree_code
6853 cp_parser_assignment_operator_opt (cp_parser
* parser
)
6858 /* Peek at the next token. */
6859 token
= cp_lexer_peek_token (parser
->lexer
);
6861 switch (token
->type
)
6872 op
= TRUNC_DIV_EXPR
;
6876 op
= TRUNC_MOD_EXPR
;
6908 /* Nothing else is an assignment operator. */
6912 /* If it was an assignment operator, consume it. */
6913 if (op
!= ERROR_MARK
)
6914 cp_lexer_consume_token (parser
->lexer
);
6919 /* Parse an expression.
6922 assignment-expression
6923 expression , assignment-expression
6925 CAST_P is true if this expression is the target of a cast.
6927 Returns a representation of the expression. */
6930 cp_parser_expression (cp_parser
* parser
, bool cast_p
, cp_id_kind
* pidk
)
6932 tree expression
= NULL_TREE
;
6936 tree assignment_expression
;
6938 /* Parse the next assignment-expression. */
6939 assignment_expression
6940 = cp_parser_assignment_expression (parser
, cast_p
, pidk
);
6941 /* If this is the first assignment-expression, we can just
6944 expression
= assignment_expression
;
6946 expression
= build_x_compound_expr (expression
,
6947 assignment_expression
,
6948 tf_warning_or_error
);
6949 /* If the next token is not a comma, then we are done with the
6951 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6953 /* Consume the `,'. */
6954 cp_lexer_consume_token (parser
->lexer
);
6955 /* A comma operator cannot appear in a constant-expression. */
6956 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
6957 expression
= error_mark_node
;
6963 /* Parse a constant-expression.
6965 constant-expression:
6966 conditional-expression
6968 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6969 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6970 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6971 is false, NON_CONSTANT_P should be NULL. */
6974 cp_parser_constant_expression (cp_parser
* parser
,
6975 bool allow_non_constant_p
,
6976 bool *non_constant_p
)
6978 bool saved_integral_constant_expression_p
;
6979 bool saved_allow_non_integral_constant_expression_p
;
6980 bool saved_non_integral_constant_expression_p
;
6983 /* It might seem that we could simply parse the
6984 conditional-expression, and then check to see if it were
6985 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6986 one that the compiler can figure out is constant, possibly after
6987 doing some simplifications or optimizations. The standard has a
6988 precise definition of constant-expression, and we must honor
6989 that, even though it is somewhat more restrictive.
6995 is not a legal declaration, because `(2, 3)' is not a
6996 constant-expression. The `,' operator is forbidden in a
6997 constant-expression. However, GCC's constant-folding machinery
6998 will fold this operation to an INTEGER_CST for `3'. */
7000 /* Save the old settings. */
7001 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
7002 saved_allow_non_integral_constant_expression_p
7003 = parser
->allow_non_integral_constant_expression_p
;
7004 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
7005 /* We are now parsing a constant-expression. */
7006 parser
->integral_constant_expression_p
= true;
7007 parser
->allow_non_integral_constant_expression_p
7008 = (allow_non_constant_p
|| cxx_dialect
>= cxx0x
);
7009 parser
->non_integral_constant_expression_p
= false;
7010 /* Although the grammar says "conditional-expression", we parse an
7011 "assignment-expression", which also permits "throw-expression"
7012 and the use of assignment operators. In the case that
7013 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7014 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7015 actually essential that we look for an assignment-expression.
7016 For example, cp_parser_initializer_clauses uses this function to
7017 determine whether a particular assignment-expression is in fact
7019 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
7020 /* Restore the old settings. */
7021 parser
->integral_constant_expression_p
7022 = saved_integral_constant_expression_p
;
7023 parser
->allow_non_integral_constant_expression_p
7024 = saved_allow_non_integral_constant_expression_p
;
7025 if (cxx_dialect
>= cxx0x
)
7027 /* Require an rvalue constant expression here; that's what our
7028 callers expect. Reference constant expressions are handled
7029 separately in e.g. cp_parser_template_argument. */
7030 bool is_const
= potential_rvalue_constant_expression (expression
);
7031 parser
->non_integral_constant_expression_p
= !is_const
;
7032 if (!is_const
&& !allow_non_constant_p
)
7033 require_potential_rvalue_constant_expression (expression
);
7035 if (allow_non_constant_p
)
7036 *non_constant_p
= parser
->non_integral_constant_expression_p
;
7037 else if (parser
->non_integral_constant_expression_p
)
7038 expression
= error_mark_node
;
7039 parser
->non_integral_constant_expression_p
7040 = saved_non_integral_constant_expression_p
;
7045 /* Parse __builtin_offsetof.
7047 offsetof-expression:
7048 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7050 offsetof-member-designator:
7052 | offsetof-member-designator "." id-expression
7053 | offsetof-member-designator "[" expression "]"
7054 | offsetof-member-designator "->" id-expression */
7057 cp_parser_builtin_offsetof (cp_parser
*parser
)
7059 int save_ice_p
, save_non_ice_p
;
7064 /* We're about to accept non-integral-constant things, but will
7065 definitely yield an integral constant expression. Save and
7066 restore these values around our local parsing. */
7067 save_ice_p
= parser
->integral_constant_expression_p
;
7068 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
7070 /* Consume the "__builtin_offsetof" token. */
7071 cp_lexer_consume_token (parser
->lexer
);
7072 /* Consume the opening `('. */
7073 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7074 /* Parse the type-id. */
7075 type
= cp_parser_type_id (parser
);
7076 /* Look for the `,'. */
7077 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7078 token
= cp_lexer_peek_token (parser
->lexer
);
7080 /* Build the (type *)null that begins the traditional offsetof macro. */
7081 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
7082 tf_warning_or_error
);
7084 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7085 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
7086 true, &dummy
, token
->location
);
7089 token
= cp_lexer_peek_token (parser
->lexer
);
7090 switch (token
->type
)
7092 case CPP_OPEN_SQUARE
:
7093 /* offsetof-member-designator "[" expression "]" */
7094 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
7098 /* offsetof-member-designator "->" identifier */
7099 expr
= grok_array_decl (expr
, integer_zero_node
);
7103 /* offsetof-member-designator "." identifier */
7104 cp_lexer_consume_token (parser
->lexer
);
7105 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
7110 case CPP_CLOSE_PAREN
:
7111 /* Consume the ")" token. */
7112 cp_lexer_consume_token (parser
->lexer
);
7116 /* Error. We know the following require will fail, but
7117 that gives the proper error message. */
7118 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7119 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
7120 expr
= error_mark_node
;
7126 /* If we're processing a template, we can't finish the semantics yet.
7127 Otherwise we can fold the entire expression now. */
7128 if (processing_template_decl
)
7129 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
7131 expr
= finish_offsetof (expr
);
7134 parser
->integral_constant_expression_p
= save_ice_p
;
7135 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
7140 /* Parse a trait expression.
7142 Returns a representation of the expression, the underlying type
7143 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7146 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
7149 tree type1
, type2
= NULL_TREE
;
7150 bool binary
= false;
7151 cp_decl_specifier_seq decl_specs
;
7155 case RID_HAS_NOTHROW_ASSIGN
:
7156 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
7158 case RID_HAS_NOTHROW_CONSTRUCTOR
:
7159 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
7161 case RID_HAS_NOTHROW_COPY
:
7162 kind
= CPTK_HAS_NOTHROW_COPY
;
7164 case RID_HAS_TRIVIAL_ASSIGN
:
7165 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
7167 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
7168 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
7170 case RID_HAS_TRIVIAL_COPY
:
7171 kind
= CPTK_HAS_TRIVIAL_COPY
;
7173 case RID_HAS_TRIVIAL_DESTRUCTOR
:
7174 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
7176 case RID_HAS_VIRTUAL_DESTRUCTOR
:
7177 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
7179 case RID_IS_ABSTRACT
:
7180 kind
= CPTK_IS_ABSTRACT
;
7182 case RID_IS_BASE_OF
:
7183 kind
= CPTK_IS_BASE_OF
;
7187 kind
= CPTK_IS_CLASS
;
7189 case RID_IS_CONVERTIBLE_TO
:
7190 kind
= CPTK_IS_CONVERTIBLE_TO
;
7194 kind
= CPTK_IS_EMPTY
;
7197 kind
= CPTK_IS_ENUM
;
7199 case RID_IS_LITERAL_TYPE
:
7200 kind
= CPTK_IS_LITERAL_TYPE
;
7205 case RID_IS_POLYMORPHIC
:
7206 kind
= CPTK_IS_POLYMORPHIC
;
7208 case RID_IS_STD_LAYOUT
:
7209 kind
= CPTK_IS_STD_LAYOUT
;
7211 case RID_IS_TRIVIAL
:
7212 kind
= CPTK_IS_TRIVIAL
;
7215 kind
= CPTK_IS_UNION
;
7217 case RID_UNDERLYING_TYPE
:
7218 kind
= CPTK_UNDERLYING_TYPE
;
7224 /* Consume the token. */
7225 cp_lexer_consume_token (parser
->lexer
);
7227 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7229 type1
= cp_parser_type_id (parser
);
7231 if (type1
== error_mark_node
)
7232 return error_mark_node
;
7234 /* Build a trivial decl-specifier-seq. */
7235 clear_decl_specs (&decl_specs
);
7236 decl_specs
.type
= type1
;
7238 /* Call grokdeclarator to figure out what type this is. */
7239 type1
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
7240 /*initialized=*/0, /*attrlist=*/NULL
);
7244 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7246 type2
= cp_parser_type_id (parser
);
7248 if (type2
== error_mark_node
)
7249 return error_mark_node
;
7251 /* Build a trivial decl-specifier-seq. */
7252 clear_decl_specs (&decl_specs
);
7253 decl_specs
.type
= type2
;
7255 /* Call grokdeclarator to figure out what type this is. */
7256 type2
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
7257 /*initialized=*/0, /*attrlist=*/NULL
);
7260 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7262 /* Complete the trait expression, which may mean either processing
7263 the trait expr now or saving it for template instantiation. */
7264 return kind
!= CPTK_UNDERLYING_TYPE
7265 ? finish_trait_expr (kind
, type1
, type2
)
7266 : finish_underlying_type (type1
);
7269 /* Lambdas that appear in variable initializer or default argument scope
7270 get that in their mangling, so we need to record it. We might as well
7271 use the count for function and namespace scopes as well. */
7272 static GTY(()) tree lambda_scope
;
7273 static GTY(()) int lambda_count
;
7274 typedef struct GTY(()) tree_int
7279 DEF_VEC_O(tree_int
);
7280 DEF_VEC_ALLOC_O(tree_int
,gc
);
7281 static GTY(()) VEC(tree_int
,gc
) *lambda_scope_stack
;
7284 start_lambda_scope (tree decl
)
7288 /* Once we're inside a function, we ignore other scopes and just push
7289 the function again so that popping works properly. */
7290 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
7291 decl
= current_function_decl
;
7292 ti
.t
= lambda_scope
;
7293 ti
.i
= lambda_count
;
7294 VEC_safe_push (tree_int
, gc
, lambda_scope_stack
, &ti
);
7295 if (lambda_scope
!= decl
)
7297 /* Don't reset the count if we're still in the same function. */
7298 lambda_scope
= decl
;
7304 record_lambda_scope (tree lambda
)
7306 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
7307 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
7311 finish_lambda_scope (void)
7313 tree_int
*p
= VEC_last (tree_int
, lambda_scope_stack
);
7314 if (lambda_scope
!= p
->t
)
7316 lambda_scope
= p
->t
;
7317 lambda_count
= p
->i
;
7319 VEC_pop (tree_int
, lambda_scope_stack
);
7322 /* Parse a lambda expression.
7325 lambda-introducer lambda-declarator [opt] compound-statement
7327 Returns a representation of the expression. */
7330 cp_parser_lambda_expression (cp_parser
* parser
)
7332 tree lambda_expr
= build_lambda_expr ();
7335 LAMBDA_EXPR_LOCATION (lambda_expr
)
7336 = cp_lexer_peek_token (parser
->lexer
)->location
;
7338 if (cp_unevaluated_operand
)
7339 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
7340 "lambda-expression in unevaluated context");
7342 /* We may be in the middle of deferred access check. Disable
7344 push_deferring_access_checks (dk_no_deferred
);
7346 cp_parser_lambda_introducer (parser
, lambda_expr
);
7348 type
= begin_lambda_type (lambda_expr
);
7350 record_lambda_scope (lambda_expr
);
7352 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7353 determine_visibility (TYPE_NAME (type
));
7355 /* Now that we've started the type, add the capture fields for any
7356 explicit captures. */
7357 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
7360 /* Inside the class, surrounding template-parameter-lists do not apply. */
7361 unsigned int saved_num_template_parameter_lists
7362 = parser
->num_template_parameter_lists
;
7364 parser
->num_template_parameter_lists
= 0;
7366 /* By virtue of defining a local class, a lambda expression has access to
7367 the private variables of enclosing classes. */
7369 cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
7371 cp_parser_lambda_body (parser
, lambda_expr
);
7373 /* The capture list was built up in reverse order; fix that now. */
7375 tree newlist
= NULL_TREE
;
7378 for (elt
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
);
7381 tree field
= TREE_PURPOSE (elt
);
7384 next
= TREE_CHAIN (elt
);
7385 TREE_CHAIN (elt
) = newlist
;
7388 /* Also add __ to the beginning of the field name so that code
7389 outside the lambda body can't see the captured name. We could
7390 just remove the name entirely, but this is more useful for
7392 if (field
== LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
7393 /* The 'this' capture already starts with __. */
7396 buf
= (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field
)) + 3);
7397 buf
[1] = buf
[0] = '_';
7398 memcpy (buf
+ 2, IDENTIFIER_POINTER (DECL_NAME (field
)),
7399 IDENTIFIER_LENGTH (DECL_NAME (field
)) + 1);
7400 DECL_NAME (field
) = get_identifier (buf
);
7402 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
) = newlist
;
7405 maybe_add_lambda_conv_op (type
);
7407 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
7409 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
7412 pop_deferring_access_checks ();
7414 return build_lambda_object (lambda_expr
);
7417 /* Parse the beginning of a lambda expression.
7420 [ lambda-capture [opt] ]
7422 LAMBDA_EXPR is the current representation of the lambda expression. */
7425 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
7427 /* Need commas after the first capture. */
7430 /* Eat the leading `['. */
7431 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
7433 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7434 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
7435 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
7436 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
7437 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7438 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
7440 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
7442 cp_lexer_consume_token (parser
->lexer
);
7446 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
7448 cp_token
* capture_token
;
7450 tree capture_init_expr
;
7451 cp_id_kind idk
= CP_ID_KIND_NONE
;
7452 bool explicit_init_p
= false;
7454 enum capture_kind_type
7459 enum capture_kind_type capture_kind
= BY_COPY
;
7461 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
7463 error ("expected end of capture-list");
7470 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7472 /* Possibly capture `this'. */
7473 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
7475 cp_lexer_consume_token (parser
->lexer
);
7476 add_capture (lambda_expr
,
7477 /*id=*/get_identifier ("__this"),
7478 /*initializer=*/finish_this_expr(),
7479 /*by_reference_p=*/false,
7484 /* Remember whether we want to capture as a reference or not. */
7485 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
7487 capture_kind
= BY_REFERENCE
;
7488 cp_lexer_consume_token (parser
->lexer
);
7491 /* Get the identifier. */
7492 capture_token
= cp_lexer_peek_token (parser
->lexer
);
7493 capture_id
= cp_parser_identifier (parser
);
7495 if (capture_id
== error_mark_node
)
7496 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7497 delimiters, but I modified this to stop on unnested ']' as well. It
7498 was already changed to stop on unnested '}', so the
7499 "closing_parenthesis" name is no more misleading with my change. */
7501 cp_parser_skip_to_closing_parenthesis (parser
,
7502 /*recovering=*/true,
7504 /*consume_paren=*/true);
7508 /* Find the initializer for this capture. */
7509 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7511 /* An explicit expression exists. */
7512 cp_lexer_consume_token (parser
->lexer
);
7513 pedwarn (input_location
, OPT_pedantic
,
7514 "ISO C++ does not allow initializers "
7515 "in lambda expression capture lists");
7516 capture_init_expr
= cp_parser_assignment_expression (parser
,
7519 explicit_init_p
= true;
7523 const char* error_msg
;
7525 /* Turn the identifier into an id-expression. */
7527 = cp_parser_lookup_name
7531 /*is_template=*/false,
7532 /*is_namespace=*/false,
7533 /*check_dependency=*/true,
7534 /*ambiguous_decls=*/NULL
,
7535 capture_token
->location
);
7538 = finish_id_expression
7543 /*integral_constant_expression_p=*/false,
7544 /*allow_non_integral_constant_expression_p=*/false,
7545 /*non_integral_constant_expression_p=*/NULL
,
7546 /*template_p=*/false,
7548 /*address_p=*/false,
7549 /*template_arg_p=*/false,
7551 capture_token
->location
);
7554 if (TREE_CODE (capture_init_expr
) == IDENTIFIER_NODE
)
7556 = unqualified_name_lookup_error (capture_init_expr
);
7558 add_capture (lambda_expr
,
7561 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
7565 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7568 /* Parse the (optional) middle of a lambda expression.
7571 ( parameter-declaration-clause [opt] )
7572 attribute-specifier [opt]
7574 exception-specification [opt]
7575 lambda-return-type-clause [opt]
7577 LAMBDA_EXPR is the current representation of the lambda expression. */
7580 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
7582 /* 5.1.1.4 of the standard says:
7583 If a lambda-expression does not include a lambda-declarator, it is as if
7584 the lambda-declarator were ().
7585 This means an empty parameter list, no attributes, and no exception
7587 tree param_list
= void_list_node
;
7588 tree attributes
= NULL_TREE
;
7589 tree exception_spec
= NULL_TREE
;
7592 /* The lambda-declarator is optional, but must begin with an opening
7593 parenthesis if present. */
7594 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7596 cp_lexer_consume_token (parser
->lexer
);
7598 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
7600 /* Parse parameters. */
7601 param_list
= cp_parser_parameter_declaration_clause (parser
);
7603 /* Default arguments shall not be specified in the
7604 parameter-declaration-clause of a lambda-declarator. */
7605 for (t
= param_list
; t
; t
= TREE_CHAIN (t
))
7606 if (TREE_PURPOSE (t
))
7607 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_pedantic
,
7608 "default argument specified for lambda parameter");
7610 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7612 attributes
= cp_parser_attributes_opt (parser
);
7614 /* Parse optional `mutable' keyword. */
7615 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
7617 cp_lexer_consume_token (parser
->lexer
);
7618 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
7621 /* Parse optional exception specification. */
7622 exception_spec
= cp_parser_exception_specification_opt (parser
);
7624 /* Parse optional trailing return type. */
7625 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
7627 cp_lexer_consume_token (parser
->lexer
);
7628 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
) = cp_parser_type_id (parser
);
7631 /* The function parameters must be in scope all the way until after the
7632 trailing-return-type in case of decltype. */
7633 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
7634 pop_binding (DECL_NAME (t
), t
);
7639 /* Create the function call operator.
7641 Messing with declarators like this is no uglier than building up the
7642 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7645 cp_decl_specifier_seq return_type_specs
;
7646 cp_declarator
* declarator
;
7651 clear_decl_specs (&return_type_specs
);
7652 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
7653 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
7655 /* Maybe we will deduce the return type later, but we can use void
7656 as a placeholder return type anyways. */
7657 return_type_specs
.type
= void_type_node
;
7659 p
= obstack_alloc (&declarator_obstack
, 0);
7661 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
7664 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
7665 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
7666 declarator
= make_call_declarator (declarator
, param_list
, quals
,
7668 /*late_return_type=*/NULL_TREE
);
7669 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
7671 fco
= grokmethod (&return_type_specs
,
7674 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
7675 DECL_ARTIFICIAL (fco
) = 1;
7677 finish_member_declaration (fco
);
7679 obstack_free (&declarator_obstack
, p
);
7683 /* Parse the body of a lambda expression, which is simply
7687 but which requires special handling.
7688 LAMBDA_EXPR is the current representation of the lambda expression. */
7691 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
7693 bool nested
= (current_function_decl
!= NULL_TREE
);
7695 push_function_context ();
7697 /* Finish the function call operator
7699 + late_parsing_for_member
7700 + function_definition_after_declarator
7701 + ctor_initializer_opt_and_function_body */
7703 tree fco
= lambda_function (lambda_expr
);
7707 /* Let the front end know that we are going to be defining this
7709 start_preparsed_function (fco
,
7711 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
7713 start_lambda_scope (fco
);
7714 body
= begin_function_body ();
7716 /* 5.1.1.4 of the standard says:
7717 If a lambda-expression does not include a trailing-return-type, it
7718 is as if the trailing-return-type denotes the following type:
7719 * if the compound-statement is of the form
7720 { return attribute-specifier [opt] expression ; }
7721 the type of the returned expression after lvalue-to-rvalue
7722 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7723 (_conv.array_ 4.2), and function-to-pointer conversion
7725 * otherwise, void. */
7727 /* In a lambda that has neither a lambda-return-type-clause
7728 nor a deducible form, errors should be reported for return statements
7729 in the body. Since we used void as the placeholder return type, parsing
7730 the body as usual will give such desired behavior. */
7731 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
7732 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
7733 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
== RID_RETURN
7734 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_SEMICOLON
)
7737 tree expr
= NULL_TREE
;
7738 cp_id_kind idk
= CP_ID_KIND_NONE
;
7740 /* Parse tentatively in case there's more after the initial return
7742 cp_parser_parse_tentatively (parser
);
7744 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
7745 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
7747 expr
= cp_parser_expression (parser
, /*cast_p=*/false, &idk
);
7749 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
7750 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
7752 if (cp_parser_parse_definitely (parser
))
7754 apply_lambda_return_type (lambda_expr
, lambda_return_type (expr
));
7756 compound_stmt
= begin_compound_stmt (0);
7757 /* Will get error here if type not deduced yet. */
7758 finish_return_stmt (expr
);
7759 finish_compound_stmt (compound_stmt
);
7767 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
7768 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr
) = true;
7769 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7770 cp_parser_compound_stmt does not pass it. */
7771 cp_parser_function_body (parser
);
7772 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr
) = false;
7775 finish_function_body (body
);
7776 finish_lambda_scope ();
7778 /* Finish the function and generate code for it if necessary. */
7779 expand_or_defer_fn (finish_function (/*inline*/2));
7783 pop_function_context();
7786 /* Statements [gram.stmt.stmt] */
7788 /* Parse a statement.
7792 expression-statement
7797 declaration-statement
7800 IN_COMPOUND is true when the statement is nested inside a
7801 cp_parser_compound_statement; this matters for certain pragmas.
7803 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7804 is a (possibly labeled) if statement which is not enclosed in braces
7805 and has an else clause. This is used to implement -Wparentheses. */
7808 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
7809 bool in_compound
, bool *if_p
)
7813 location_t statement_location
;
7818 /* There is no statement yet. */
7819 statement
= NULL_TREE
;
7820 /* Peek at the next token. */
7821 token
= cp_lexer_peek_token (parser
->lexer
);
7822 /* Remember the location of the first token in the statement. */
7823 statement_location
= token
->location
;
7824 /* If this is a keyword, then that will often determine what kind of
7825 statement we have. */
7826 if (token
->type
== CPP_KEYWORD
)
7828 enum rid keyword
= token
->keyword
;
7834 /* Looks like a labeled-statement with a case label.
7835 Parse the label, and then use tail recursion to parse
7837 cp_parser_label_for_labeled_statement (parser
);
7842 statement
= cp_parser_selection_statement (parser
, if_p
);
7848 statement
= cp_parser_iteration_statement (parser
);
7855 statement
= cp_parser_jump_statement (parser
);
7858 /* Objective-C++ exception-handling constructs. */
7861 case RID_AT_FINALLY
:
7862 case RID_AT_SYNCHRONIZED
:
7864 statement
= cp_parser_objc_statement (parser
);
7868 statement
= cp_parser_try_block (parser
);
7872 /* This must be a namespace alias definition. */
7873 cp_parser_declaration_statement (parser
);
7877 /* It might be a keyword like `int' that can start a
7878 declaration-statement. */
7882 else if (token
->type
== CPP_NAME
)
7884 /* If the next token is a `:', then we are looking at a
7885 labeled-statement. */
7886 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7887 if (token
->type
== CPP_COLON
)
7889 /* Looks like a labeled-statement with an ordinary label.
7890 Parse the label, and then use tail recursion to parse
7892 cp_parser_label_for_labeled_statement (parser
);
7896 /* Anything that starts with a `{' must be a compound-statement. */
7897 else if (token
->type
== CPP_OPEN_BRACE
)
7898 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
7899 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7900 a statement all its own. */
7901 else if (token
->type
== CPP_PRAGMA
)
7903 /* Only certain OpenMP pragmas are attached to statements, and thus
7904 are considered statements themselves. All others are not. In
7905 the context of a compound, accept the pragma as a "statement" and
7906 return so that we can check for a close brace. Otherwise we
7907 require a real statement and must go back and read one. */
7909 cp_parser_pragma (parser
, pragma_compound
);
7910 else if (!cp_parser_pragma (parser
, pragma_stmt
))
7914 else if (token
->type
== CPP_EOF
)
7916 cp_parser_error (parser
, "expected statement");
7920 /* Everything else must be a declaration-statement or an
7921 expression-statement. Try for the declaration-statement
7922 first, unless we are looking at a `;', in which case we know that
7923 we have an expression-statement. */
7926 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7928 cp_parser_parse_tentatively (parser
);
7929 /* Try to parse the declaration-statement. */
7930 cp_parser_declaration_statement (parser
);
7931 /* If that worked, we're done. */
7932 if (cp_parser_parse_definitely (parser
))
7935 /* Look for an expression-statement instead. */
7936 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
7939 /* Set the line number for the statement. */
7940 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
7941 SET_EXPR_LOCATION (statement
, statement_location
);
7944 /* Parse the label for a labeled-statement, i.e.
7947 case constant-expression :
7951 case constant-expression ... constant-expression : statement
7953 When a label is parsed without errors, the label is added to the
7954 parse tree by the finish_* functions, so this function doesn't
7955 have to return the label. */
7958 cp_parser_label_for_labeled_statement (cp_parser
* parser
)
7961 tree label
= NULL_TREE
;
7962 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
7964 /* The next token should be an identifier. */
7965 token
= cp_lexer_peek_token (parser
->lexer
);
7966 if (token
->type
!= CPP_NAME
7967 && token
->type
!= CPP_KEYWORD
)
7969 cp_parser_error (parser
, "expected labeled-statement");
7973 parser
->colon_corrects_to_scope_p
= false;
7974 switch (token
->keyword
)
7981 /* Consume the `case' token. */
7982 cp_lexer_consume_token (parser
->lexer
);
7983 /* Parse the constant-expression. */
7984 expr
= cp_parser_constant_expression (parser
,
7985 /*allow_non_constant_p=*/false,
7988 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
7989 if (ellipsis
->type
== CPP_ELLIPSIS
)
7991 /* Consume the `...' token. */
7992 cp_lexer_consume_token (parser
->lexer
);
7994 cp_parser_constant_expression (parser
,
7995 /*allow_non_constant_p=*/false,
7997 /* We don't need to emit warnings here, as the common code
7998 will do this for us. */
8001 expr_hi
= NULL_TREE
;
8003 if (parser
->in_switch_statement_p
)
8004 finish_case_label (token
->location
, expr
, expr_hi
);
8006 error_at (token
->location
,
8007 "case label %qE not within a switch statement",
8013 /* Consume the `default' token. */
8014 cp_lexer_consume_token (parser
->lexer
);
8016 if (parser
->in_switch_statement_p
)
8017 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
8019 error_at (token
->location
, "case label not within a switch statement");
8023 /* Anything else must be an ordinary label. */
8024 label
= finish_label_stmt (cp_parser_identifier (parser
));
8028 /* Require the `:' token. */
8029 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8031 /* An ordinary label may optionally be followed by attributes.
8032 However, this is only permitted if the attributes are then
8033 followed by a semicolon. This is because, for backward
8034 compatibility, when parsing
8035 lab: __attribute__ ((unused)) int i;
8036 we want the attribute to attach to "i", not "lab". */
8037 if (label
!= NULL_TREE
8038 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
8042 cp_parser_parse_tentatively (parser
);
8043 attrs
= cp_parser_attributes_opt (parser
);
8044 if (attrs
== NULL_TREE
8045 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8046 cp_parser_abort_tentative_parse (parser
);
8047 else if (!cp_parser_parse_definitely (parser
))
8050 cplus_decl_attributes (&label
, attrs
, 0);
8053 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
8056 /* Parse an expression-statement.
8058 expression-statement:
8061 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8062 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8063 indicates whether this expression-statement is part of an
8064 expression statement. */
8067 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
8069 tree statement
= NULL_TREE
;
8070 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8072 /* If the next token is a ';', then there is no expression
8074 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8075 statement
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8077 /* Give a helpful message for "A<T>::type t;" and the like. */
8078 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
8079 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
8081 if (TREE_CODE (statement
) == SCOPE_REF
)
8082 error_at (token
->location
, "need %<typename%> before %qE because "
8083 "%qT is a dependent scope",
8084 statement
, TREE_OPERAND (statement
, 0));
8085 else if (is_overloaded_fn (statement
)
8086 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
8089 tree fn
= get_first_fn (statement
);
8090 error_at (token
->location
,
8091 "%<%T::%D%> names the constructor, not the type",
8092 DECL_CONTEXT (fn
), DECL_NAME (fn
));
8096 /* Consume the final `;'. */
8097 cp_parser_consume_semicolon_at_end_of_statement (parser
);
8099 if (in_statement_expr
8100 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
8101 /* This is the final expression statement of a statement
8103 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
8105 statement
= finish_expr_stmt (statement
);
8112 /* Parse a compound-statement.
8115 { statement-seq [opt] }
8120 { label-declaration-seq [opt] statement-seq [opt] }
8122 label-declaration-seq:
8124 label-declaration-seq label-declaration
8126 Returns a tree representing the statement. */
8129 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
8130 bool in_try
, bool function_body
)
8134 /* Consume the `{'. */
8135 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
8136 return error_mark_node
;
8137 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
8139 pedwarn (input_location
, OPT_pedantic
,
8140 "compound-statement in constexpr function");
8141 /* Begin the compound-statement. */
8142 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
8143 /* If the next keyword is `__label__' we have a label declaration. */
8144 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
8145 cp_parser_label_declaration (parser
);
8146 /* Parse an (optional) statement-seq. */
8147 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
8148 /* Finish the compound-statement. */
8149 finish_compound_stmt (compound_stmt
);
8150 /* Consume the `}'. */
8151 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
8153 return compound_stmt
;
8156 /* Parse an (optional) statement-seq.
8160 statement-seq [opt] statement */
8163 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
8165 /* Scan statements until there aren't any more. */
8168 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8170 /* If we are looking at a `}', then we have run out of
8171 statements; the same is true if we have reached the end
8172 of file, or have stumbled upon a stray '@end'. */
8173 if (token
->type
== CPP_CLOSE_BRACE
8174 || token
->type
== CPP_EOF
8175 || token
->type
== CPP_PRAGMA_EOL
8176 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
8179 /* If we are in a compound statement and find 'else' then
8180 something went wrong. */
8181 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
8183 if (parser
->in_statement
& IN_IF_STMT
)
8187 token
= cp_lexer_consume_token (parser
->lexer
);
8188 error_at (token
->location
, "%<else%> without a previous %<if%>");
8192 /* Parse the statement. */
8193 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
8197 /* Parse a selection-statement.
8199 selection-statement:
8200 if ( condition ) statement
8201 if ( condition ) statement else statement
8202 switch ( condition ) statement
8204 Returns the new IF_STMT or SWITCH_STMT.
8206 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8207 is a (possibly labeled) if statement which is not enclosed in
8208 braces and has an else clause. This is used to implement
8212 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
8220 /* Peek at the next token. */
8221 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
8223 /* See what kind of keyword it is. */
8224 keyword
= token
->keyword
;
8233 /* Look for the `('. */
8234 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
8236 cp_parser_skip_to_end_of_statement (parser
);
8237 return error_mark_node
;
8240 /* Begin the selection-statement. */
8241 if (keyword
== RID_IF
)
8242 statement
= begin_if_stmt ();
8244 statement
= begin_switch_stmt ();
8246 /* Parse the condition. */
8247 condition
= cp_parser_condition (parser
);
8248 /* Look for the `)'. */
8249 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
8250 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
8251 /*consume_paren=*/true);
8253 if (keyword
== RID_IF
)
8256 unsigned char in_statement
;
8258 /* Add the condition. */
8259 finish_if_stmt_cond (condition
, statement
);
8261 /* Parse the then-clause. */
8262 in_statement
= parser
->in_statement
;
8263 parser
->in_statement
|= IN_IF_STMT
;
8264 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
8266 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8267 add_stmt (build_empty_stmt (loc
));
8268 cp_lexer_consume_token (parser
->lexer
);
8269 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
8270 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
8271 "empty body in an %<if%> statement");
8275 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
8276 parser
->in_statement
= in_statement
;
8278 finish_then_clause (statement
);
8280 /* If the next token is `else', parse the else-clause. */
8281 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
8284 /* Consume the `else' keyword. */
8285 cp_lexer_consume_token (parser
->lexer
);
8286 begin_else_clause (statement
);
8287 /* Parse the else-clause. */
8288 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
8291 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8293 OPT_Wempty_body
, "suggest braces around "
8294 "empty body in an %<else%> statement");
8295 add_stmt (build_empty_stmt (loc
));
8296 cp_lexer_consume_token (parser
->lexer
);
8299 cp_parser_implicitly_scoped_statement (parser
, NULL
);
8301 finish_else_clause (statement
);
8303 /* If we are currently parsing a then-clause, then
8304 IF_P will not be NULL. We set it to true to
8305 indicate that this if statement has an else clause.
8306 This may trigger the Wparentheses warning below
8307 when we get back up to the parent if statement. */
8313 /* This if statement does not have an else clause. If
8314 NESTED_IF is true, then the then-clause is an if
8315 statement which does have an else clause. We warn
8316 about the potential ambiguity. */
8318 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
8319 "suggest explicit braces to avoid ambiguous"
8323 /* Now we're all done with the if-statement. */
8324 finish_if_stmt (statement
);
8328 bool in_switch_statement_p
;
8329 unsigned char in_statement
;
8331 /* Add the condition. */
8332 finish_switch_cond (condition
, statement
);
8334 /* Parse the body of the switch-statement. */
8335 in_switch_statement_p
= parser
->in_switch_statement_p
;
8336 in_statement
= parser
->in_statement
;
8337 parser
->in_switch_statement_p
= true;
8338 parser
->in_statement
|= IN_SWITCH_STMT
;
8339 cp_parser_implicitly_scoped_statement (parser
, NULL
);
8340 parser
->in_switch_statement_p
= in_switch_statement_p
;
8341 parser
->in_statement
= in_statement
;
8343 /* Now we're all done with the switch-statement. */
8344 finish_switch_stmt (statement
);
8352 cp_parser_error (parser
, "expected selection-statement");
8353 return error_mark_node
;
8357 /* Parse a condition.
8361 type-specifier-seq declarator = initializer-clause
8362 type-specifier-seq declarator braced-init-list
8367 type-specifier-seq declarator asm-specification [opt]
8368 attributes [opt] = assignment-expression
8370 Returns the expression that should be tested. */
8373 cp_parser_condition (cp_parser
* parser
)
8375 cp_decl_specifier_seq type_specifiers
;
8376 const char *saved_message
;
8377 int declares_class_or_enum
;
8379 /* Try the declaration first. */
8380 cp_parser_parse_tentatively (parser
);
8381 /* New types are not allowed in the type-specifier-seq for a
8383 saved_message
= parser
->type_definition_forbidden_message
;
8384 parser
->type_definition_forbidden_message
8385 = G_("types may not be defined in conditions");
8386 /* Parse the type-specifier-seq. */
8387 cp_parser_decl_specifier_seq (parser
,
8388 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
8390 &declares_class_or_enum
);
8391 /* Restore the saved message. */
8392 parser
->type_definition_forbidden_message
= saved_message
;
8393 /* If all is well, we might be looking at a declaration. */
8394 if (!cp_parser_error_occurred (parser
))
8397 tree asm_specification
;
8399 cp_declarator
*declarator
;
8400 tree initializer
= NULL_TREE
;
8402 /* Parse the declarator. */
8403 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
8404 /*ctor_dtor_or_conv_p=*/NULL
,
8405 /*parenthesized_p=*/NULL
,
8406 /*member_p=*/false);
8407 /* Parse the attributes. */
8408 attributes
= cp_parser_attributes_opt (parser
);
8409 /* Parse the asm-specification. */
8410 asm_specification
= cp_parser_asm_specification_opt (parser
);
8411 /* If the next token is not an `=' or '{', then we might still be
8412 looking at an expression. For example:
8416 looks like a decl-specifier-seq and a declarator -- but then
8417 there is no `=', so this is an expression. */
8418 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
8419 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
8420 cp_parser_simulate_error (parser
);
8422 /* If we did see an `=' or '{', then we are looking at a declaration
8424 if (cp_parser_parse_definitely (parser
))
8427 bool non_constant_p
;
8428 bool flags
= LOOKUP_ONLYCONVERTING
;
8430 /* Create the declaration. */
8431 decl
= start_decl (declarator
, &type_specifiers
,
8432 /*initialized_p=*/true,
8433 attributes
, /*prefix_attributes=*/NULL_TREE
,
8436 /* Parse the initializer. */
8437 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8439 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
8440 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
8445 /* Consume the `='. */
8446 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
8447 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
8449 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
8450 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8452 /* Process the initializer. */
8453 cp_finish_decl (decl
,
8454 initializer
, !non_constant_p
,
8459 pop_scope (pushed_scope
);
8461 return convert_from_reference (decl
);
8464 /* If we didn't even get past the declarator successfully, we are
8465 definitely not looking at a declaration. */
8467 cp_parser_abort_tentative_parse (parser
);
8469 /* Otherwise, we are looking at an expression. */
8470 return cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8473 /* Parses a for-statement or range-for-statement until the closing ')',
8477 cp_parser_for (cp_parser
*parser
)
8479 tree init
, scope
, decl
;
8482 /* Begin the for-statement. */
8483 scope
= begin_for_scope (&init
);
8485 /* Parse the initialization. */
8486 is_range_for
= cp_parser_for_init_statement (parser
, &decl
);
8489 return cp_parser_range_for (parser
, scope
, init
, decl
);
8491 return cp_parser_c_for (parser
, scope
, init
);
8495 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
)
8497 /* Normal for loop */
8498 tree condition
= NULL_TREE
;
8499 tree expression
= NULL_TREE
;
8502 stmt
= begin_for_stmt (scope
, init
);
8503 /* The for-init-statement has already been parsed in
8504 cp_parser_for_init_statement, so no work is needed here. */
8505 finish_for_init_stmt (stmt
);
8507 /* If there's a condition, process it. */
8508 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8509 condition
= cp_parser_condition (parser
);
8510 finish_for_cond (condition
, stmt
);
8511 /* Look for the `;'. */
8512 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
8514 /* If there's an expression, process it. */
8515 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
8516 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8517 finish_for_expr (expression
, stmt
);
8522 /* Tries to parse a range-based for-statement:
8525 decl-specifier-seq declarator : expression
8527 The decl-specifier-seq declarator and the `:' are already parsed by
8528 cp_parser_for_init_statement. If processing_template_decl it returns a
8529 newly created RANGE_FOR_STMT; if not, it is converted to a
8530 regular FOR_STMT. */
8533 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
)
8535 tree stmt
, range_expr
;
8537 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8539 bool expr_non_constant_p
;
8540 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8543 range_expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8545 /* If in template, STMT is converted to a normal for-statement
8546 at instantiation. If not, it is done just ahead. */
8547 if (processing_template_decl
)
8549 stmt
= begin_range_for_stmt (scope
, init
);
8550 finish_range_for_decl (stmt
, range_decl
, range_expr
);
8554 stmt
= begin_for_stmt (scope
, init
);
8555 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
);
8560 /* Converts a range-based for-statement into a normal
8561 for-statement, as per the definition.
8563 for (RANGE_DECL : RANGE_EXPR)
8566 should be equivalent to:
8569 auto &&__range = RANGE_EXPR;
8570 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8574 RANGE_DECL = *__begin;
8579 If RANGE_EXPR is an array:
8580 BEGIN_EXPR = __range
8581 END_EXPR = __range + ARRAY_SIZE(__range)
8582 Else if RANGE_EXPR has a member 'begin' or 'end':
8583 BEGIN_EXPR = __range.begin()
8584 END_EXPR = __range.end()
8586 BEGIN_EXPR = begin(__range)
8587 END_EXPR = end(__range);
8589 If __range has a member 'begin' but not 'end', or vice versa, we must
8590 still use the second alternative (it will surely fail, however).
8591 When calling begin()/end() in the third alternative we must use
8592 argument dependent lookup, but always considering 'std' as an associated
8596 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
)
8598 tree range_type
, range_temp
;
8600 tree iter_type
, begin_expr
, end_expr
;
8601 tree condition
, expression
;
8603 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
8604 /* If an error happened previously do nothing or else a lot of
8605 unhelpful errors would be issued. */
8606 begin_expr
= end_expr
= iter_type
= error_mark_node
;
8609 /* Find out the type deduced by the declaration
8610 `auto &&__range = range_expr'. */
8611 range_type
= cp_build_reference_type (make_auto (), true);
8612 range_type
= do_auto_deduction (range_type
, range_expr
,
8613 type_uses_auto (range_type
));
8615 /* Create the __range variable. */
8616 range_temp
= build_decl (input_location
, VAR_DECL
,
8617 get_identifier ("__for_range"), range_type
);
8618 TREE_USED (range_temp
) = 1;
8619 DECL_ARTIFICIAL (range_temp
) = 1;
8620 pushdecl (range_temp
);
8621 cp_finish_decl (range_temp
, range_expr
,
8622 /*is_constant_init*/false, NULL_TREE
,
8623 LOOKUP_ONLYCONVERTING
);
8625 range_temp
= convert_from_reference (range_temp
);
8626 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
8627 &begin_expr
, &end_expr
);
8630 /* The new for initialization statement. */
8631 begin
= build_decl (input_location
, VAR_DECL
,
8632 get_identifier ("__for_begin"), iter_type
);
8633 TREE_USED (begin
) = 1;
8634 DECL_ARTIFICIAL (begin
) = 1;
8636 cp_finish_decl (begin
, begin_expr
,
8637 /*is_constant_init*/false, NULL_TREE
,
8638 LOOKUP_ONLYCONVERTING
);
8640 end
= build_decl (input_location
, VAR_DECL
,
8641 get_identifier ("__for_end"), iter_type
);
8642 TREE_USED (end
) = 1;
8643 DECL_ARTIFICIAL (end
) = 1;
8645 cp_finish_decl (end
, end_expr
,
8646 /*is_constant_init*/false, NULL_TREE
,
8647 LOOKUP_ONLYCONVERTING
);
8649 finish_for_init_stmt (statement
);
8651 /* The new for condition. */
8652 condition
= build_x_binary_op (NE_EXPR
,
8655 NULL
, tf_warning_or_error
);
8656 finish_for_cond (condition
, statement
);
8658 /* The new increment expression. */
8659 expression
= finish_unary_op_expr (PREINCREMENT_EXPR
, begin
);
8660 finish_for_expr (expression
, statement
);
8662 /* The declaration is initialized with *__begin inside the loop body. */
8663 cp_finish_decl (range_decl
,
8664 build_x_indirect_ref (begin
, RO_NULL
, tf_warning_or_error
),
8665 /*is_constant_init*/false, NULL_TREE
,
8666 LOOKUP_ONLYCONVERTING
);
8671 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
8672 We need to solve both at the same time because the method used
8673 depends on the existence of members begin or end.
8674 Returns the type deduced for the iterator expression. */
8677 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
8679 if (!COMPLETE_TYPE_P (TREE_TYPE (range
)))
8681 error ("range-based %<for%> expression of type %qT "
8682 "has incomplete type", TREE_TYPE (range
));
8683 *begin
= *end
= error_mark_node
;
8684 return error_mark_node
;
8686 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
8688 /* If RANGE is an array, we will use pointer arithmetic. */
8690 *end
= build_binary_op (input_location
, PLUS_EXPR
,
8692 array_type_nelts_top (TREE_TYPE (range
)),
8694 return build_pointer_type (TREE_TYPE (TREE_TYPE (range
)));
8698 /* If it is not an array, we must do a bit of magic. */
8699 tree id_begin
, id_end
;
8700 tree member_begin
, member_end
;
8702 *begin
= *end
= error_mark_node
;
8704 id_begin
= get_identifier ("begin");
8705 id_end
= get_identifier ("end");
8706 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
8707 /*protect=*/2, /*want_type=*/false);
8708 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
8709 /*protect=*/2, /*want_type=*/false);
8711 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
8713 /* Use the member functions. */
8714 if (member_begin
!= NULL_TREE
)
8715 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
8717 error ("range-based %<for%> expression of type %qT has an "
8718 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
8720 if (member_end
!= NULL_TREE
)
8721 *end
= cp_parser_range_for_member_function (range
, id_end
);
8723 error ("range-based %<for%> expression of type %qT has a "
8724 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
8728 /* Use global functions with ADL. */
8730 vec
= make_tree_vector ();
8732 VEC_safe_push (tree
, gc
, vec
, range
);
8734 member_begin
= perform_koenig_lookup (id_begin
, vec
,
8735 /*include_std=*/true);
8736 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
8737 tf_warning_or_error
);
8738 member_end
= perform_koenig_lookup (id_end
, vec
,
8739 /*include_std=*/true);
8740 *end
= finish_call_expr (member_end
, &vec
, false, true,
8741 tf_warning_or_error
);
8743 release_tree_vector (vec
);
8746 /* Last common checks. */
8747 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
8749 /* If one of the expressions is an error do no more checks. */
8750 *begin
= *end
= error_mark_node
;
8751 return error_mark_node
;
8755 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
8756 /* The unqualified type of the __begin and __end temporaries should
8757 be the same, as required by the multiple auto declaration. */
8758 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
8759 error ("inconsistent begin/end types in range-based %<for%> "
8760 "statement: %qT and %qT",
8761 TREE_TYPE (*begin
), TREE_TYPE (*end
));
8767 /* Helper function for cp_parser_perform_range_for_lookup.
8768 Builds a tree for RANGE.IDENTIFIER(). */
8771 cp_parser_range_for_member_function (tree range
, tree identifier
)
8776 member
= finish_class_member_access_expr (range
, identifier
,
8777 false, tf_warning_or_error
);
8778 if (member
== error_mark_node
)
8779 return error_mark_node
;
8781 vec
= make_tree_vector ();
8782 res
= finish_call_expr (member
, &vec
,
8783 /*disallow_virtual=*/false,
8785 tf_warning_or_error
);
8786 release_tree_vector (vec
);
8790 /* Parse an iteration-statement.
8792 iteration-statement:
8793 while ( condition ) statement
8794 do statement while ( expression ) ;
8795 for ( for-init-statement condition [opt] ; expression [opt] )
8798 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8801 cp_parser_iteration_statement (cp_parser
* parser
)
8806 unsigned char in_statement
;
8808 /* Peek at the next token. */
8809 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
8811 return error_mark_node
;
8813 /* Remember whether or not we are already within an iteration
8815 in_statement
= parser
->in_statement
;
8817 /* See what kind of keyword it is. */
8818 keyword
= token
->keyword
;
8825 /* Begin the while-statement. */
8826 statement
= begin_while_stmt ();
8827 /* Look for the `('. */
8828 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8829 /* Parse the condition. */
8830 condition
= cp_parser_condition (parser
);
8831 finish_while_stmt_cond (condition
, statement
);
8832 /* Look for the `)'. */
8833 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8834 /* Parse the dependent statement. */
8835 parser
->in_statement
= IN_ITERATION_STMT
;
8836 cp_parser_already_scoped_statement (parser
);
8837 parser
->in_statement
= in_statement
;
8838 /* We're done with the while-statement. */
8839 finish_while_stmt (statement
);
8847 /* Begin the do-statement. */
8848 statement
= begin_do_stmt ();
8849 /* Parse the body of the do-statement. */
8850 parser
->in_statement
= IN_ITERATION_STMT
;
8851 cp_parser_implicitly_scoped_statement (parser
, NULL
);
8852 parser
->in_statement
= in_statement
;
8853 finish_do_body (statement
);
8854 /* Look for the `while' keyword. */
8855 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
8856 /* Look for the `('. */
8857 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8858 /* Parse the expression. */
8859 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8860 /* We're done with the do-statement. */
8861 finish_do_stmt (expression
, statement
);
8862 /* Look for the `)'. */
8863 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8864 /* Look for the `;'. */
8865 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
8871 /* Look for the `('. */
8872 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8874 statement
= cp_parser_for (parser
);
8876 /* Look for the `)'. */
8877 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8879 /* Parse the body of the for-statement. */
8880 parser
->in_statement
= IN_ITERATION_STMT
;
8881 cp_parser_already_scoped_statement (parser
);
8882 parser
->in_statement
= in_statement
;
8884 /* We're done with the for-statement. */
8885 finish_for_stmt (statement
);
8890 cp_parser_error (parser
, "expected iteration-statement");
8891 statement
= error_mark_node
;
8898 /* Parse a for-init-statement or the declarator of a range-based-for.
8899 Returns true if a range-based-for declaration is seen.
8902 expression-statement
8903 simple-declaration */
8906 cp_parser_for_init_statement (cp_parser
* parser
, tree
*decl
)
8908 /* If the next token is a `;', then we have an empty
8909 expression-statement. Grammatically, this is also a
8910 simple-declaration, but an invalid one, because it does not
8911 declare anything. Therefore, if we did not handle this case
8912 specially, we would issue an error message about an invalid
8914 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8916 bool is_range_for
= false;
8917 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
8919 parser
->colon_corrects_to_scope_p
= false;
8921 /* We're going to speculatively look for a declaration, falling back
8922 to an expression, if necessary. */
8923 cp_parser_parse_tentatively (parser
);
8924 /* Parse the declaration. */
8925 cp_parser_simple_declaration (parser
,
8926 /*function_definition_allowed_p=*/false,
8928 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
8929 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
8931 /* It is a range-for, consume the ':' */
8932 cp_lexer_consume_token (parser
->lexer
);
8933 is_range_for
= true;
8934 if (cxx_dialect
< cxx0x
)
8936 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
8937 "range-based %<for%> loops are not allowed "
8939 *decl
= error_mark_node
;
8943 /* The ';' is not consumed yet because we told
8944 cp_parser_simple_declaration not to. */
8945 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
8947 if (cp_parser_parse_definitely (parser
))
8948 return is_range_for
;
8949 /* If the tentative parse failed, then we shall need to look for an
8950 expression-statement. */
8952 /* If we are here, it is an expression-statement. */
8953 cp_parser_expression_statement (parser
, NULL_TREE
);
8957 /* Parse a jump-statement.
8962 return expression [opt] ;
8963 return braced-init-list ;
8971 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8974 cp_parser_jump_statement (cp_parser
* parser
)
8976 tree statement
= error_mark_node
;
8979 unsigned char in_statement
;
8981 /* Peek at the next token. */
8982 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
8984 return error_mark_node
;
8986 /* See what kind of keyword it is. */
8987 keyword
= token
->keyword
;
8991 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
8992 switch (in_statement
)
8995 error_at (token
->location
, "break statement not within loop or switch");
8998 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
8999 || in_statement
== IN_ITERATION_STMT
);
9000 statement
= finish_break_stmt ();
9003 error_at (token
->location
, "invalid exit from OpenMP structured block");
9006 error_at (token
->location
, "break statement used with OpenMP for loop");
9009 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9013 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
9016 error_at (token
->location
, "continue statement not within a loop");
9018 case IN_ITERATION_STMT
:
9020 statement
= finish_continue_stmt ();
9023 error_at (token
->location
, "invalid exit from OpenMP structured block");
9028 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9034 bool expr_non_constant_p
;
9036 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9038 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9039 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
9041 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9042 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9044 /* If the next token is a `;', then there is no
9047 /* Build the return-statement. */
9048 statement
= finish_return_stmt (expr
);
9049 /* Look for the final `;'. */
9050 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9055 /* Create the goto-statement. */
9056 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
9058 /* Issue a warning about this use of a GNU extension. */
9059 pedwarn (token
->location
, OPT_pedantic
, "ISO C++ forbids computed gotos");
9060 /* Consume the '*' token. */
9061 cp_lexer_consume_token (parser
->lexer
);
9062 /* Parse the dependent expression. */
9063 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false, NULL
));
9066 finish_goto_stmt (cp_parser_identifier (parser
));
9067 /* Look for the final `;'. */
9068 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9072 cp_parser_error (parser
, "expected jump-statement");
9079 /* Parse a declaration-statement.
9081 declaration-statement:
9082 block-declaration */
9085 cp_parser_declaration_statement (cp_parser
* parser
)
9089 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9090 p
= obstack_alloc (&declarator_obstack
, 0);
9092 /* Parse the block-declaration. */
9093 cp_parser_block_declaration (parser
, /*statement_p=*/true);
9095 /* Free any declarators allocated. */
9096 obstack_free (&declarator_obstack
, p
);
9098 /* Finish off the statement. */
9102 /* Some dependent statements (like `if (cond) statement'), are
9103 implicitly in their own scope. In other words, if the statement is
9104 a single statement (as opposed to a compound-statement), it is
9105 none-the-less treated as if it were enclosed in braces. Any
9106 declarations appearing in the dependent statement are out of scope
9107 after control passes that point. This function parses a statement,
9108 but ensures that is in its own scope, even if it is not a
9111 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9112 is a (possibly labeled) if statement which is not enclosed in
9113 braces and has an else clause. This is used to implement
9116 Returns the new statement. */
9119 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
9126 /* Mark if () ; with a special NOP_EXPR. */
9127 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9129 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9130 cp_lexer_consume_token (parser
->lexer
);
9131 statement
= add_stmt (build_empty_stmt (loc
));
9133 /* if a compound is opened, we simply parse the statement directly. */
9134 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9135 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
9136 /* If the token is not a `{', then we must take special action. */
9139 /* Create a compound-statement. */
9140 statement
= begin_compound_stmt (0);
9141 /* Parse the dependent-statement. */
9142 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
9143 /* Finish the dummy compound-statement. */
9144 finish_compound_stmt (statement
);
9147 /* Return the statement. */
9151 /* For some dependent statements (like `while (cond) statement'), we
9152 have already created a scope. Therefore, even if the dependent
9153 statement is a compound-statement, we do not want to create another
9157 cp_parser_already_scoped_statement (cp_parser
* parser
)
9159 /* If the token is a `{', then we must take special action. */
9160 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
9161 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
9164 /* Avoid calling cp_parser_compound_statement, so that we
9165 don't create a new scope. Do everything else by hand. */
9166 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
9167 /* If the next keyword is `__label__' we have a label declaration. */
9168 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9169 cp_parser_label_declaration (parser
);
9170 /* Parse an (optional) statement-seq. */
9171 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
9172 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9176 /* Declarations [gram.dcl.dcl] */
9178 /* Parse an optional declaration-sequence.
9182 declaration-seq declaration */
9185 cp_parser_declaration_seq_opt (cp_parser
* parser
)
9191 token
= cp_lexer_peek_token (parser
->lexer
);
9193 if (token
->type
== CPP_CLOSE_BRACE
9194 || token
->type
== CPP_EOF
9195 || token
->type
== CPP_PRAGMA_EOL
)
9198 if (token
->type
== CPP_SEMICOLON
)
9200 /* A declaration consisting of a single semicolon is
9201 invalid. Allow it unless we're being pedantic. */
9202 cp_lexer_consume_token (parser
->lexer
);
9203 if (!in_system_header
)
9204 pedwarn (input_location
, OPT_pedantic
, "extra %<;%>");
9208 /* If we're entering or exiting a region that's implicitly
9209 extern "C", modify the lang context appropriately. */
9210 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
9212 push_lang_context (lang_name_c
);
9213 parser
->implicit_extern_c
= true;
9215 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
9217 pop_lang_context ();
9218 parser
->implicit_extern_c
= false;
9221 if (token
->type
== CPP_PRAGMA
)
9223 /* A top-level declaration can consist solely of a #pragma.
9224 A nested declaration cannot, so this is done here and not
9225 in cp_parser_declaration. (A #pragma at block scope is
9226 handled in cp_parser_statement.) */
9227 cp_parser_pragma (parser
, pragma_external
);
9231 /* Parse the declaration itself. */
9232 cp_parser_declaration (parser
);
9236 /* Parse a declaration.
9241 template-declaration
9242 explicit-instantiation
9243 explicit-specialization
9244 linkage-specification
9245 namespace-definition
9250 __extension__ declaration */
9253 cp_parser_declaration (cp_parser
* parser
)
9259 tree attributes
= NULL_TREE
;
9261 /* Check for the `__extension__' keyword. */
9262 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
9264 /* Parse the qualified declaration. */
9265 cp_parser_declaration (parser
);
9266 /* Restore the PEDANTIC flag. */
9267 pedantic
= saved_pedantic
;
9272 /* Try to figure out what kind of declaration is present. */
9273 token1
= *cp_lexer_peek_token (parser
->lexer
);
9275 if (token1
.type
!= CPP_EOF
)
9276 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
9279 token2
.type
= CPP_EOF
;
9280 token2
.keyword
= RID_MAX
;
9283 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9284 p
= obstack_alloc (&declarator_obstack
, 0);
9286 /* If the next token is `extern' and the following token is a string
9287 literal, then we have a linkage specification. */
9288 if (token1
.keyword
== RID_EXTERN
9289 && cp_parser_is_string_literal (&token2
))
9290 cp_parser_linkage_specification (parser
);
9291 /* If the next token is `template', then we have either a template
9292 declaration, an explicit instantiation, or an explicit
9294 else if (token1
.keyword
== RID_TEMPLATE
)
9296 /* `template <>' indicates a template specialization. */
9297 if (token2
.type
== CPP_LESS
9298 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
9299 cp_parser_explicit_specialization (parser
);
9300 /* `template <' indicates a template declaration. */
9301 else if (token2
.type
== CPP_LESS
)
9302 cp_parser_template_declaration (parser
, /*member_p=*/false);
9303 /* Anything else must be an explicit instantiation. */
9305 cp_parser_explicit_instantiation (parser
);
9307 /* If the next token is `export', then we have a template
9309 else if (token1
.keyword
== RID_EXPORT
)
9310 cp_parser_template_declaration (parser
, /*member_p=*/false);
9311 /* If the next token is `extern', 'static' or 'inline' and the one
9312 after that is `template', we have a GNU extended explicit
9313 instantiation directive. */
9314 else if (cp_parser_allow_gnu_extensions_p (parser
)
9315 && (token1
.keyword
== RID_EXTERN
9316 || token1
.keyword
== RID_STATIC
9317 || token1
.keyword
== RID_INLINE
)
9318 && token2
.keyword
== RID_TEMPLATE
)
9319 cp_parser_explicit_instantiation (parser
);
9320 /* If the next token is `namespace', check for a named or unnamed
9321 namespace definition. */
9322 else if (token1
.keyword
== RID_NAMESPACE
9323 && (/* A named namespace definition. */
9324 (token2
.type
== CPP_NAME
9325 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
9327 /* An unnamed namespace definition. */
9328 || token2
.type
== CPP_OPEN_BRACE
9329 || token2
.keyword
== RID_ATTRIBUTE
))
9330 cp_parser_namespace_definition (parser
);
9331 /* An inline (associated) namespace definition. */
9332 else if (token1
.keyword
== RID_INLINE
9333 && token2
.keyword
== RID_NAMESPACE
)
9334 cp_parser_namespace_definition (parser
);
9335 /* Objective-C++ declaration/definition. */
9336 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
9337 cp_parser_objc_declaration (parser
, NULL_TREE
);
9338 else if (c_dialect_objc ()
9339 && token1
.keyword
== RID_ATTRIBUTE
9340 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
9341 cp_parser_objc_declaration (parser
, attributes
);
9342 /* We must have either a block declaration or a function
9345 /* Try to parse a block-declaration, or a function-definition. */
9346 cp_parser_block_declaration (parser
, /*statement_p=*/false);
9348 /* Free any declarators allocated. */
9349 obstack_free (&declarator_obstack
, p
);
9352 /* Parse a block-declaration.
9357 namespace-alias-definition
9364 __extension__ block-declaration
9369 static_assert-declaration
9371 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9372 part of a declaration-statement. */
9375 cp_parser_block_declaration (cp_parser
*parser
,
9381 /* Check for the `__extension__' keyword. */
9382 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
9384 /* Parse the qualified declaration. */
9385 cp_parser_block_declaration (parser
, statement_p
);
9386 /* Restore the PEDANTIC flag. */
9387 pedantic
= saved_pedantic
;
9392 /* Peek at the next token to figure out which kind of declaration is
9394 token1
= cp_lexer_peek_token (parser
->lexer
);
9396 /* If the next keyword is `asm', we have an asm-definition. */
9397 if (token1
->keyword
== RID_ASM
)
9400 cp_parser_commit_to_tentative_parse (parser
);
9401 cp_parser_asm_definition (parser
);
9403 /* If the next keyword is `namespace', we have a
9404 namespace-alias-definition. */
9405 else if (token1
->keyword
== RID_NAMESPACE
)
9406 cp_parser_namespace_alias_definition (parser
);
9407 /* If the next keyword is `using', we have either a
9408 using-declaration or a using-directive. */
9409 else if (token1
->keyword
== RID_USING
)
9414 cp_parser_commit_to_tentative_parse (parser
);
9415 /* If the token after `using' is `namespace', then we have a
9417 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9418 if (token2
->keyword
== RID_NAMESPACE
)
9419 cp_parser_using_directive (parser
);
9420 /* Otherwise, it's a using-declaration. */
9422 cp_parser_using_declaration (parser
,
9423 /*access_declaration_p=*/false);
9425 /* If the next keyword is `__label__' we have a misplaced label
9427 else if (token1
->keyword
== RID_LABEL
)
9429 cp_lexer_consume_token (parser
->lexer
);
9430 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
9431 cp_parser_skip_to_end_of_statement (parser
);
9432 /* If the next token is now a `;', consume it. */
9433 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9434 cp_lexer_consume_token (parser
->lexer
);
9436 /* If the next token is `static_assert' we have a static assertion. */
9437 else if (token1
->keyword
== RID_STATIC_ASSERT
)
9438 cp_parser_static_assert (parser
, /*member_p=*/false);
9439 /* Anything else must be a simple-declaration. */
9441 cp_parser_simple_declaration (parser
, !statement_p
,
9442 /*maybe_range_for_decl*/NULL
);
9445 /* Parse a simple-declaration.
9448 decl-specifier-seq [opt] init-declarator-list [opt] ;
9450 init-declarator-list:
9452 init-declarator-list , init-declarator
9454 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9455 function-definition as a simple-declaration.
9457 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9458 parsed declaration if it is an uninitialized single declarator not followed
9459 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9460 if present, will not be consumed. */
9463 cp_parser_simple_declaration (cp_parser
* parser
,
9464 bool function_definition_allowed_p
,
9465 tree
*maybe_range_for_decl
)
9467 cp_decl_specifier_seq decl_specifiers
;
9468 int declares_class_or_enum
;
9469 bool saw_declarator
;
9471 if (maybe_range_for_decl
)
9472 *maybe_range_for_decl
= NULL_TREE
;
9474 /* Defer access checks until we know what is being declared; the
9475 checks for names appearing in the decl-specifier-seq should be
9476 done as if we were in the scope of the thing being declared. */
9477 push_deferring_access_checks (dk_deferred
);
9479 /* Parse the decl-specifier-seq. We have to keep track of whether
9480 or not the decl-specifier-seq declares a named class or
9481 enumeration type, since that is the only case in which the
9482 init-declarator-list is allowed to be empty.
9486 In a simple-declaration, the optional init-declarator-list can be
9487 omitted only when declaring a class or enumeration, that is when
9488 the decl-specifier-seq contains either a class-specifier, an
9489 elaborated-type-specifier, or an enum-specifier. */
9490 cp_parser_decl_specifier_seq (parser
,
9491 CP_PARSER_FLAGS_OPTIONAL
,
9493 &declares_class_or_enum
);
9494 /* We no longer need to defer access checks. */
9495 stop_deferring_access_checks ();
9497 /* In a block scope, a valid declaration must always have a
9498 decl-specifier-seq. By not trying to parse declarators, we can
9499 resolve the declaration/expression ambiguity more quickly. */
9500 if (!function_definition_allowed_p
9501 && !decl_specifiers
.any_specifiers_p
)
9503 cp_parser_error (parser
, "expected declaration");
9507 /* If the next two tokens are both identifiers, the code is
9508 erroneous. The usual cause of this situation is code like:
9512 where "T" should name a type -- but does not. */
9513 if (!decl_specifiers
.any_type_specifiers_p
9514 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
9516 /* If parsing tentatively, we should commit; we really are
9517 looking at a declaration. */
9518 cp_parser_commit_to_tentative_parse (parser
);
9523 /* If we have seen at least one decl-specifier, and the next token
9524 is not a parenthesis, then we must be looking at a declaration.
9525 (After "int (" we might be looking at a functional cast.) */
9526 if (decl_specifiers
.any_specifiers_p
9527 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
9528 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
9529 && !cp_parser_error_occurred (parser
))
9530 cp_parser_commit_to_tentative_parse (parser
);
9532 /* Keep going until we hit the `;' at the end of the simple
9534 saw_declarator
= false;
9535 while (cp_lexer_next_token_is_not (parser
->lexer
,
9539 bool function_definition_p
;
9544 /* If we are processing next declarator, coma is expected */
9545 token
= cp_lexer_peek_token (parser
->lexer
);
9546 gcc_assert (token
->type
== CPP_COMMA
);
9547 cp_lexer_consume_token (parser
->lexer
);
9548 if (maybe_range_for_decl
)
9549 *maybe_range_for_decl
= error_mark_node
;
9552 saw_declarator
= true;
9554 /* Parse the init-declarator. */
9555 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
9557 function_definition_allowed_p
,
9559 declares_class_or_enum
,
9560 &function_definition_p
,
9561 maybe_range_for_decl
);
9562 /* If an error occurred while parsing tentatively, exit quickly.
9563 (That usually happens when in the body of a function; each
9564 statement is treated as a declaration-statement until proven
9566 if (cp_parser_error_occurred (parser
))
9568 /* Handle function definitions specially. */
9569 if (function_definition_p
)
9571 /* If the next token is a `,', then we are probably
9572 processing something like:
9576 which is erroneous. */
9577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9579 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9580 error_at (token
->location
,
9582 " declarations and function-definitions is forbidden");
9584 /* Otherwise, we're done with the list of declarators. */
9587 pop_deferring_access_checks ();
9591 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
9592 *maybe_range_for_decl
= decl
;
9593 /* The next token should be either a `,' or a `;'. */
9594 token
= cp_lexer_peek_token (parser
->lexer
);
9595 /* If it's a `,', there are more declarators to come. */
9596 if (token
->type
== CPP_COMMA
)
9597 /* will be consumed next time around */;
9598 /* If it's a `;', we are done. */
9599 else if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
9601 /* Anything else is an error. */
9604 /* If we have already issued an error message we don't need
9605 to issue another one. */
9606 if (decl
!= error_mark_node
9607 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
9608 cp_parser_error (parser
, "expected %<,%> or %<;%>");
9609 /* Skip tokens until we reach the end of the statement. */
9610 cp_parser_skip_to_end_of_statement (parser
);
9611 /* If the next token is now a `;', consume it. */
9612 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9613 cp_lexer_consume_token (parser
->lexer
);
9616 /* After the first time around, a function-definition is not
9617 allowed -- even if it was OK at first. For example:
9622 function_definition_allowed_p
= false;
9625 /* Issue an error message if no declarators are present, and the
9626 decl-specifier-seq does not itself declare a class or
9628 if (!saw_declarator
)
9630 if (cp_parser_declares_only_class_p (parser
))
9631 shadow_tag (&decl_specifiers
);
9632 /* Perform any deferred access checks. */
9633 perform_deferred_access_checks ();
9636 /* Consume the `;'. */
9637 if (!maybe_range_for_decl
)
9638 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9641 pop_deferring_access_checks ();
9644 /* Parse a decl-specifier-seq.
9647 decl-specifier-seq [opt] decl-specifier
9650 storage-class-specifier
9661 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9663 The parser flags FLAGS is used to control type-specifier parsing.
9665 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9668 1: one of the decl-specifiers is an elaborated-type-specifier
9669 (i.e., a type declaration)
9670 2: one of the decl-specifiers is an enum-specifier or a
9671 class-specifier (i.e., a type definition)
9676 cp_parser_decl_specifier_seq (cp_parser
* parser
,
9677 cp_parser_flags flags
,
9678 cp_decl_specifier_seq
*decl_specs
,
9679 int* declares_class_or_enum
)
9681 bool constructor_possible_p
= !parser
->in_declarator_p
;
9682 cp_token
*start_token
= NULL
;
9684 /* Clear DECL_SPECS. */
9685 clear_decl_specs (decl_specs
);
9687 /* Assume no class or enumeration type is declared. */
9688 *declares_class_or_enum
= 0;
9690 /* Keep reading specifiers until there are no more to read. */
9694 bool found_decl_spec
;
9697 /* Peek at the next token. */
9698 token
= cp_lexer_peek_token (parser
->lexer
);
9700 /* Save the first token of the decl spec list for error
9703 start_token
= token
;
9704 /* Handle attributes. */
9705 if (token
->keyword
== RID_ATTRIBUTE
)
9707 /* Parse the attributes. */
9708 decl_specs
->attributes
9709 = chainon (decl_specs
->attributes
,
9710 cp_parser_attributes_opt (parser
));
9713 /* Assume we will find a decl-specifier keyword. */
9714 found_decl_spec
= true;
9715 /* If the next token is an appropriate keyword, we can simply
9716 add it to the list. */
9717 switch (token
->keyword
)
9723 if (!at_class_scope_p ())
9725 error_at (token
->location
, "%<friend%> used outside of class");
9726 cp_lexer_purge_token (parser
->lexer
);
9730 ++decl_specs
->specs
[(int) ds_friend
];
9731 /* Consume the token. */
9732 cp_lexer_consume_token (parser
->lexer
);
9737 ++decl_specs
->specs
[(int) ds_constexpr
];
9738 cp_lexer_consume_token (parser
->lexer
);
9741 /* function-specifier:
9748 cp_parser_function_specifier_opt (parser
, decl_specs
);
9754 ++decl_specs
->specs
[(int) ds_typedef
];
9755 /* Consume the token. */
9756 cp_lexer_consume_token (parser
->lexer
);
9757 /* A constructor declarator cannot appear in a typedef. */
9758 constructor_possible_p
= false;
9759 /* The "typedef" keyword can only occur in a declaration; we
9760 may as well commit at this point. */
9761 cp_parser_commit_to_tentative_parse (parser
);
9763 if (decl_specs
->storage_class
!= sc_none
)
9764 decl_specs
->conflicting_specifiers_p
= true;
9767 /* storage-class-specifier:
9777 if (cxx_dialect
== cxx98
)
9779 /* Consume the token. */
9780 cp_lexer_consume_token (parser
->lexer
);
9782 /* Complain about `auto' as a storage specifier, if
9783 we're complaining about C++0x compatibility. */
9784 warning_at (token
->location
, OPT_Wc__0x_compat
, "%<auto%>"
9785 " will change meaning in C++0x; please remove it");
9787 /* Set the storage class anyway. */
9788 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
9792 /* C++0x auto type-specifier. */
9793 found_decl_spec
= false;
9800 /* Consume the token. */
9801 cp_lexer_consume_token (parser
->lexer
);
9802 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
9806 /* Consume the token. */
9807 cp_lexer_consume_token (parser
->lexer
);
9808 ++decl_specs
->specs
[(int) ds_thread
];
9812 /* We did not yet find a decl-specifier yet. */
9813 found_decl_spec
= false;
9818 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
9819 && token
->keyword
!= RID_CONSTEXPR
)
9820 error ("decl-specifier invalid in condition");
9822 /* Constructors are a special case. The `S' in `S()' is not a
9823 decl-specifier; it is the beginning of the declarator. */
9826 && constructor_possible_p
9827 && (cp_parser_constructor_declarator_p
9828 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
9830 /* If we don't have a DECL_SPEC yet, then we must be looking at
9831 a type-specifier. */
9832 if (!found_decl_spec
&& !constructor_p
)
9834 int decl_spec_declares_class_or_enum
;
9835 bool is_cv_qualifier
;
9839 = cp_parser_type_specifier (parser
, flags
,
9841 /*is_declaration=*/true,
9842 &decl_spec_declares_class_or_enum
,
9844 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
9846 /* If this type-specifier referenced a user-defined type
9847 (a typedef, class-name, etc.), then we can't allow any
9848 more such type-specifiers henceforth.
9852 The longest sequence of decl-specifiers that could
9853 possibly be a type name is taken as the
9854 decl-specifier-seq of a declaration. The sequence shall
9855 be self-consistent as described below.
9859 As a general rule, at most one type-specifier is allowed
9860 in the complete decl-specifier-seq of a declaration. The
9861 only exceptions are the following:
9863 -- const or volatile can be combined with any other
9866 -- signed or unsigned can be combined with char, long,
9874 void g (const int Pc);
9876 Here, Pc is *not* part of the decl-specifier seq; it's
9877 the declarator. Therefore, once we see a type-specifier
9878 (other than a cv-qualifier), we forbid any additional
9879 user-defined types. We *do* still allow things like `int
9880 int' to be considered a decl-specifier-seq, and issue the
9881 error message later. */
9882 if (type_spec
&& !is_cv_qualifier
)
9883 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
9884 /* A constructor declarator cannot follow a type-specifier. */
9887 constructor_possible_p
= false;
9888 found_decl_spec
= true;
9889 if (!is_cv_qualifier
)
9890 decl_specs
->any_type_specifiers_p
= true;
9894 /* If we still do not have a DECL_SPEC, then there are no more
9896 if (!found_decl_spec
)
9899 decl_specs
->any_specifiers_p
= true;
9900 /* After we see one decl-specifier, further decl-specifiers are
9902 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
9905 cp_parser_check_decl_spec (decl_specs
, start_token
->location
);
9907 /* Don't allow a friend specifier with a class definition. */
9908 if (decl_specs
->specs
[(int) ds_friend
] != 0
9909 && (*declares_class_or_enum
& 2))
9910 error_at (start_token
->location
,
9911 "class definition may not be declared a friend");
9914 /* Parse an (optional) storage-class-specifier.
9916 storage-class-specifier:
9925 storage-class-specifier:
9928 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9931 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
9933 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
9936 if (cxx_dialect
!= cxx98
)
9938 /* Fall through for C++98. */
9945 /* Consume the token. */
9946 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
9953 /* Parse an (optional) function-specifier.
9960 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9961 Updates DECL_SPECS, if it is non-NULL. */
9964 cp_parser_function_specifier_opt (cp_parser
* parser
,
9965 cp_decl_specifier_seq
*decl_specs
)
9967 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9968 switch (token
->keyword
)
9972 ++decl_specs
->specs
[(int) ds_inline
];
9976 /* 14.5.2.3 [temp.mem]
9978 A member function template shall not be virtual. */
9979 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9980 error_at (token
->location
, "templates may not be %<virtual%>");
9981 else if (decl_specs
)
9982 ++decl_specs
->specs
[(int) ds_virtual
];
9987 ++decl_specs
->specs
[(int) ds_explicit
];
9994 /* Consume the token. */
9995 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
9998 /* Parse a linkage-specification.
10000 linkage-specification:
10001 extern string-literal { declaration-seq [opt] }
10002 extern string-literal declaration */
10005 cp_parser_linkage_specification (cp_parser
* parser
)
10009 /* Look for the `extern' keyword. */
10010 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
10012 /* Look for the string-literal. */
10013 linkage
= cp_parser_string_literal (parser
, false, false);
10015 /* Transform the literal into an identifier. If the literal is a
10016 wide-character string, or contains embedded NULs, then we can't
10017 handle it as the user wants. */
10018 if (strlen (TREE_STRING_POINTER (linkage
))
10019 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
10021 cp_parser_error (parser
, "invalid linkage-specification");
10022 /* Assume C++ linkage. */
10023 linkage
= lang_name_cplusplus
;
10026 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
10028 /* We're now using the new linkage. */
10029 push_lang_context (linkage
);
10031 /* If the next token is a `{', then we're using the first
10033 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10035 /* Consume the `{' token. */
10036 cp_lexer_consume_token (parser
->lexer
);
10037 /* Parse the declarations. */
10038 cp_parser_declaration_seq_opt (parser
);
10039 /* Look for the closing `}'. */
10040 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10042 /* Otherwise, there's just one declaration. */
10045 bool saved_in_unbraced_linkage_specification_p
;
10047 saved_in_unbraced_linkage_specification_p
10048 = parser
->in_unbraced_linkage_specification_p
;
10049 parser
->in_unbraced_linkage_specification_p
= true;
10050 cp_parser_declaration (parser
);
10051 parser
->in_unbraced_linkage_specification_p
10052 = saved_in_unbraced_linkage_specification_p
;
10055 /* We're done with the linkage-specification. */
10056 pop_lang_context ();
10059 /* Parse a static_assert-declaration.
10061 static_assert-declaration:
10062 static_assert ( constant-expression , string-literal ) ;
10064 If MEMBER_P, this static_assert is a class member. */
10067 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
10072 location_t saved_loc
;
10075 /* Peek at the `static_assert' token so we can keep track of exactly
10076 where the static assertion started. */
10077 token
= cp_lexer_peek_token (parser
->lexer
);
10078 saved_loc
= token
->location
;
10080 /* Look for the `static_assert' keyword. */
10081 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
10085 /* We know we are in a static assertion; commit to any tentative
10087 if (cp_parser_parsing_tentatively (parser
))
10088 cp_parser_commit_to_tentative_parse (parser
);
10090 /* Parse the `(' starting the static assertion condition. */
10091 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10093 /* Parse the constant-expression. Allow a non-constant expression
10094 here in order to give better diagnostics in finish_static_assert. */
10096 cp_parser_constant_expression (parser
,
10097 /*allow_non_constant_p=*/true,
10098 /*non_constant_p=*/&dummy
);
10100 /* Parse the separating `,'. */
10101 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10103 /* Parse the string-literal message. */
10104 message
= cp_parser_string_literal (parser
,
10105 /*translate=*/false,
10108 /* A `)' completes the static assertion. */
10109 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10110 cp_parser_skip_to_closing_parenthesis (parser
,
10111 /*recovering=*/true,
10112 /*or_comma=*/false,
10113 /*consume_paren=*/true);
10115 /* A semicolon terminates the declaration. */
10116 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10118 /* Complete the static assertion, which may mean either processing
10119 the static assert now or saving it for template instantiation. */
10120 finish_static_assert (condition
, message
, saved_loc
, member_p
);
10123 /* Parse a `decltype' type. Returns the type.
10125 simple-type-specifier:
10126 decltype ( expression ) */
10129 cp_parser_decltype (cp_parser
*parser
)
10132 bool id_expression_or_member_access_p
= false;
10133 const char *saved_message
;
10134 bool saved_integral_constant_expression_p
;
10135 bool saved_non_integral_constant_expression_p
;
10136 cp_token
*id_expr_start_token
;
10138 /* Look for the `decltype' token. */
10139 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
10140 return error_mark_node
;
10142 /* Types cannot be defined in a `decltype' expression. Save away the
10144 saved_message
= parser
->type_definition_forbidden_message
;
10146 /* And create the new one. */
10147 parser
->type_definition_forbidden_message
10148 = G_("types may not be defined in %<decltype%> expressions");
10150 /* The restrictions on constant-expressions do not apply inside
10151 decltype expressions. */
10152 saved_integral_constant_expression_p
10153 = parser
->integral_constant_expression_p
;
10154 saved_non_integral_constant_expression_p
10155 = parser
->non_integral_constant_expression_p
;
10156 parser
->integral_constant_expression_p
= false;
10158 /* Do not actually evaluate the expression. */
10159 ++cp_unevaluated_operand
;
10161 /* Do not warn about problems with the expression. */
10162 ++c_inhibit_evaluation_warnings
;
10164 /* Parse the opening `('. */
10165 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
10166 return error_mark_node
;
10168 /* First, try parsing an id-expression. */
10169 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
10170 cp_parser_parse_tentatively (parser
);
10171 expr
= cp_parser_id_expression (parser
,
10172 /*template_keyword_p=*/false,
10173 /*check_dependency_p=*/true,
10174 /*template_p=*/NULL
,
10175 /*declarator_p=*/false,
10176 /*optional_p=*/false);
10178 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
10180 bool non_integral_constant_expression_p
= false;
10181 tree id_expression
= expr
;
10183 const char *error_msg
;
10185 if (TREE_CODE (expr
) == IDENTIFIER_NODE
)
10186 /* Lookup the name we got back from the id-expression. */
10187 expr
= cp_parser_lookup_name (parser
, expr
,
10189 /*is_template=*/false,
10190 /*is_namespace=*/false,
10191 /*check_dependency=*/true,
10192 /*ambiguous_decls=*/NULL
,
10193 id_expr_start_token
->location
);
10196 && expr
!= error_mark_node
10197 && TREE_CODE (expr
) != TEMPLATE_ID_EXPR
10198 && TREE_CODE (expr
) != TYPE_DECL
10199 && (TREE_CODE (expr
) != BIT_NOT_EXPR
10200 || !TYPE_P (TREE_OPERAND (expr
, 0)))
10201 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
10203 /* Complete lookup of the id-expression. */
10204 expr
= (finish_id_expression
10205 (id_expression
, expr
, parser
->scope
, &idk
,
10206 /*integral_constant_expression_p=*/false,
10207 /*allow_non_integral_constant_expression_p=*/true,
10208 &non_integral_constant_expression_p
,
10209 /*template_p=*/false,
10211 /*address_p=*/false,
10212 /*template_arg_p=*/false,
10214 id_expr_start_token
->location
));
10216 if (expr
== error_mark_node
)
10217 /* We found an id-expression, but it was something that we
10218 should not have found. This is an error, not something
10219 we can recover from, so note that we found an
10220 id-expression and we'll recover as gracefully as
10222 id_expression_or_member_access_p
= true;
10226 && expr
!= error_mark_node
10227 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
10228 /* We have an id-expression. */
10229 id_expression_or_member_access_p
= true;
10232 if (!id_expression_or_member_access_p
)
10234 /* Abort the id-expression parse. */
10235 cp_parser_abort_tentative_parse (parser
);
10237 /* Parsing tentatively, again. */
10238 cp_parser_parse_tentatively (parser
);
10240 /* Parse a class member access. */
10241 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
10243 /*member_access_only_p=*/true, NULL
);
10246 && expr
!= error_mark_node
10247 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
10248 /* We have an id-expression. */
10249 id_expression_or_member_access_p
= true;
10252 if (id_expression_or_member_access_p
)
10253 /* We have parsed the complete id-expression or member access. */
10254 cp_parser_parse_definitely (parser
);
10257 bool saved_greater_than_is_operator_p
;
10259 /* Abort our attempt to parse an id-expression or member access
10261 cp_parser_abort_tentative_parse (parser
);
10263 /* Within a parenthesized expression, a `>' token is always
10264 the greater-than operator. */
10265 saved_greater_than_is_operator_p
10266 = parser
->greater_than_is_operator_p
;
10267 parser
->greater_than_is_operator_p
= true;
10269 /* Parse a full expression. */
10270 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10272 /* The `>' token might be the end of a template-id or
10273 template-parameter-list now. */
10274 parser
->greater_than_is_operator_p
10275 = saved_greater_than_is_operator_p
;
10278 /* Go back to evaluating expressions. */
10279 --cp_unevaluated_operand
;
10280 --c_inhibit_evaluation_warnings
;
10282 /* Restore the old message and the integral constant expression
10284 parser
->type_definition_forbidden_message
= saved_message
;
10285 parser
->integral_constant_expression_p
10286 = saved_integral_constant_expression_p
;
10287 parser
->non_integral_constant_expression_p
10288 = saved_non_integral_constant_expression_p
;
10290 if (expr
== error_mark_node
)
10292 /* Skip everything up to the closing `)'. */
10293 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10294 /*consume_paren=*/true);
10295 return error_mark_node
;
10298 /* Parse to the closing `)'. */
10299 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10301 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10302 /*consume_paren=*/true);
10303 return error_mark_node
;
10306 return finish_decltype_type (expr
, id_expression_or_member_access_p
,
10307 tf_warning_or_error
);
10310 /* Special member functions [gram.special] */
10312 /* Parse a conversion-function-id.
10314 conversion-function-id:
10315 operator conversion-type-id
10317 Returns an IDENTIFIER_NODE representing the operator. */
10320 cp_parser_conversion_function_id (cp_parser
* parser
)
10324 tree saved_qualifying_scope
;
10325 tree saved_object_scope
;
10326 tree pushed_scope
= NULL_TREE
;
10328 /* Look for the `operator' token. */
10329 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
10330 return error_mark_node
;
10331 /* When we parse the conversion-type-id, the current scope will be
10332 reset. However, we need that information in able to look up the
10333 conversion function later, so we save it here. */
10334 saved_scope
= parser
->scope
;
10335 saved_qualifying_scope
= parser
->qualifying_scope
;
10336 saved_object_scope
= parser
->object_scope
;
10337 /* We must enter the scope of the class so that the names of
10338 entities declared within the class are available in the
10339 conversion-type-id. For example, consider:
10346 S::operator I() { ... }
10348 In order to see that `I' is a type-name in the definition, we
10349 must be in the scope of `S'. */
10351 pushed_scope
= push_scope (saved_scope
);
10352 /* Parse the conversion-type-id. */
10353 type
= cp_parser_conversion_type_id (parser
);
10354 /* Leave the scope of the class, if any. */
10356 pop_scope (pushed_scope
);
10357 /* Restore the saved scope. */
10358 parser
->scope
= saved_scope
;
10359 parser
->qualifying_scope
= saved_qualifying_scope
;
10360 parser
->object_scope
= saved_object_scope
;
10361 /* If the TYPE is invalid, indicate failure. */
10362 if (type
== error_mark_node
)
10363 return error_mark_node
;
10364 return mangle_conv_op_name_for_type (type
);
10367 /* Parse a conversion-type-id:
10369 conversion-type-id:
10370 type-specifier-seq conversion-declarator [opt]
10372 Returns the TYPE specified. */
10375 cp_parser_conversion_type_id (cp_parser
* parser
)
10378 cp_decl_specifier_seq type_specifiers
;
10379 cp_declarator
*declarator
;
10380 tree type_specified
;
10382 /* Parse the attributes. */
10383 attributes
= cp_parser_attributes_opt (parser
);
10384 /* Parse the type-specifiers. */
10385 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
10386 /*is_trailing_return=*/false,
10388 /* If that didn't work, stop. */
10389 if (type_specifiers
.type
== error_mark_node
)
10390 return error_mark_node
;
10391 /* Parse the conversion-declarator. */
10392 declarator
= cp_parser_conversion_declarator_opt (parser
);
10394 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
10395 /*initialized=*/0, &attributes
);
10397 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
10399 /* Don't give this error when parsing tentatively. This happens to
10400 work because we always parse this definitively once. */
10401 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
10402 && type_uses_auto (type_specified
))
10404 error ("invalid use of %<auto%> in conversion operator");
10405 return error_mark_node
;
10408 return type_specified
;
10411 /* Parse an (optional) conversion-declarator.
10413 conversion-declarator:
10414 ptr-operator conversion-declarator [opt]
10418 static cp_declarator
*
10419 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
10421 enum tree_code code
;
10423 cp_cv_quals cv_quals
;
10425 /* We don't know if there's a ptr-operator next, or not. */
10426 cp_parser_parse_tentatively (parser
);
10427 /* Try the ptr-operator. */
10428 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
10429 /* If it worked, look for more conversion-declarators. */
10430 if (cp_parser_parse_definitely (parser
))
10432 cp_declarator
*declarator
;
10434 /* Parse another optional declarator. */
10435 declarator
= cp_parser_conversion_declarator_opt (parser
);
10437 return cp_parser_make_indirect_declarator
10438 (code
, class_type
, cv_quals
, declarator
);
10444 /* Parse an (optional) ctor-initializer.
10447 : mem-initializer-list
10449 Returns TRUE iff the ctor-initializer was actually present. */
10452 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
10454 /* If the next token is not a `:', then there is no
10455 ctor-initializer. */
10456 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
10458 /* Do default initialization of any bases and members. */
10459 if (DECL_CONSTRUCTOR_P (current_function_decl
))
10460 finish_mem_initializers (NULL_TREE
);
10465 /* Consume the `:' token. */
10466 cp_lexer_consume_token (parser
->lexer
);
10467 /* And the mem-initializer-list. */
10468 cp_parser_mem_initializer_list (parser
);
10473 /* Parse a mem-initializer-list.
10475 mem-initializer-list:
10476 mem-initializer ... [opt]
10477 mem-initializer ... [opt] , mem-initializer-list */
10480 cp_parser_mem_initializer_list (cp_parser
* parser
)
10482 tree mem_initializer_list
= NULL_TREE
;
10483 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10485 /* Let the semantic analysis code know that we are starting the
10486 mem-initializer-list. */
10487 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
10488 error_at (token
->location
,
10489 "only constructors take member initializers");
10491 /* Loop through the list. */
10494 tree mem_initializer
;
10496 token
= cp_lexer_peek_token (parser
->lexer
);
10497 /* Parse the mem-initializer. */
10498 mem_initializer
= cp_parser_mem_initializer (parser
);
10499 /* If the next token is a `...', we're expanding member initializers. */
10500 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10502 /* Consume the `...'. */
10503 cp_lexer_consume_token (parser
->lexer
);
10505 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10506 can be expanded but members cannot. */
10507 if (mem_initializer
!= error_mark_node
10508 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
10510 error_at (token
->location
,
10511 "cannot expand initializer for member %<%D%>",
10512 TREE_PURPOSE (mem_initializer
));
10513 mem_initializer
= error_mark_node
;
10516 /* Construct the pack expansion type. */
10517 if (mem_initializer
!= error_mark_node
)
10518 mem_initializer
= make_pack_expansion (mem_initializer
);
10520 /* Add it to the list, unless it was erroneous. */
10521 if (mem_initializer
!= error_mark_node
)
10523 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
10524 mem_initializer_list
= mem_initializer
;
10526 /* If the next token is not a `,', we're done. */
10527 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
10529 /* Consume the `,' token. */
10530 cp_lexer_consume_token (parser
->lexer
);
10533 /* Perform semantic analysis. */
10534 if (DECL_CONSTRUCTOR_P (current_function_decl
))
10535 finish_mem_initializers (mem_initializer_list
);
10538 /* Parse a mem-initializer.
10541 mem-initializer-id ( expression-list [opt] )
10542 mem-initializer-id braced-init-list
10547 ( expression-list [opt] )
10549 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10550 class) or FIELD_DECL (for a non-static data member) to initialize;
10551 the TREE_VALUE is the expression-list. An empty initialization
10552 list is represented by void_list_node. */
10555 cp_parser_mem_initializer (cp_parser
* parser
)
10557 tree mem_initializer_id
;
10558 tree expression_list
;
10560 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10562 /* Find out what is being initialized. */
10563 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10565 permerror (token
->location
,
10566 "anachronistic old-style base class initializer");
10567 mem_initializer_id
= NULL_TREE
;
10571 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
10572 if (mem_initializer_id
== error_mark_node
)
10573 return mem_initializer_id
;
10575 member
= expand_member_init (mem_initializer_id
);
10576 if (member
&& !DECL_P (member
))
10577 in_base_initializer
= 1;
10579 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10581 bool expr_non_constant_p
;
10582 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10583 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10584 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
10585 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
10590 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
10592 /*allow_expansion_p=*/true,
10593 /*non_constant_p=*/NULL
);
10595 return error_mark_node
;
10596 expression_list
= build_tree_list_vec (vec
);
10597 release_tree_vector (vec
);
10600 if (expression_list
== error_mark_node
)
10601 return error_mark_node
;
10602 if (!expression_list
)
10603 expression_list
= void_type_node
;
10605 in_base_initializer
= 0;
10607 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
10610 /* Parse a mem-initializer-id.
10612 mem-initializer-id:
10613 :: [opt] nested-name-specifier [opt] class-name
10616 Returns a TYPE indicating the class to be initializer for the first
10617 production. Returns an IDENTIFIER_NODE indicating the data member
10618 to be initialized for the second production. */
10621 cp_parser_mem_initializer_id (cp_parser
* parser
)
10623 bool global_scope_p
;
10624 bool nested_name_specifier_p
;
10625 bool template_p
= false;
10628 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10630 /* `typename' is not allowed in this context ([temp.res]). */
10631 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
10633 error_at (token
->location
,
10634 "keyword %<typename%> not allowed in this context (a qualified "
10635 "member initializer is implicitly a type)");
10636 cp_lexer_consume_token (parser
->lexer
);
10638 /* Look for the optional `::' operator. */
10640 = (cp_parser_global_scope_opt (parser
,
10641 /*current_scope_valid_p=*/false)
10643 /* Look for the optional nested-name-specifier. The simplest way to
10648 The keyword `typename' is not permitted in a base-specifier or
10649 mem-initializer; in these contexts a qualified name that
10650 depends on a template-parameter is implicitly assumed to be a
10653 is to assume that we have seen the `typename' keyword at this
10655 nested_name_specifier_p
10656 = (cp_parser_nested_name_specifier_opt (parser
,
10657 /*typename_keyword_p=*/true,
10658 /*check_dependency_p=*/true,
10660 /*is_declaration=*/true)
10662 if (nested_name_specifier_p
)
10663 template_p
= cp_parser_optional_template_keyword (parser
);
10664 /* If there is a `::' operator or a nested-name-specifier, then we
10665 are definitely looking for a class-name. */
10666 if (global_scope_p
|| nested_name_specifier_p
)
10667 return cp_parser_class_name (parser
,
10668 /*typename_keyword_p=*/true,
10669 /*template_keyword_p=*/template_p
,
10671 /*check_dependency_p=*/true,
10672 /*class_head_p=*/false,
10673 /*is_declaration=*/true);
10674 /* Otherwise, we could also be looking for an ordinary identifier. */
10675 cp_parser_parse_tentatively (parser
);
10676 /* Try a class-name. */
10677 id
= cp_parser_class_name (parser
,
10678 /*typename_keyword_p=*/true,
10679 /*template_keyword_p=*/false,
10681 /*check_dependency_p=*/true,
10682 /*class_head_p=*/false,
10683 /*is_declaration=*/true);
10684 /* If we found one, we're done. */
10685 if (cp_parser_parse_definitely (parser
))
10687 /* Otherwise, look for an ordinary identifier. */
10688 return cp_parser_identifier (parser
);
10691 /* Overloading [gram.over] */
10693 /* Parse an operator-function-id.
10695 operator-function-id:
10698 Returns an IDENTIFIER_NODE for the operator which is a
10699 human-readable spelling of the identifier, e.g., `operator +'. */
10702 cp_parser_operator_function_id (cp_parser
* parser
)
10704 /* Look for the `operator' keyword. */
10705 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
10706 return error_mark_node
;
10707 /* And then the name of the operator itself. */
10708 return cp_parser_operator (parser
);
10711 /* Parse an operator.
10714 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10715 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10716 || ++ -- , ->* -> () []
10723 Returns an IDENTIFIER_NODE for the operator which is a
10724 human-readable spelling of the identifier, e.g., `operator +'. */
10727 cp_parser_operator (cp_parser
* parser
)
10729 tree id
= NULL_TREE
;
10732 /* Peek at the next token. */
10733 token
= cp_lexer_peek_token (parser
->lexer
);
10734 /* Figure out which operator we have. */
10735 switch (token
->type
)
10741 /* The keyword should be either `new' or `delete'. */
10742 if (token
->keyword
== RID_NEW
)
10744 else if (token
->keyword
== RID_DELETE
)
10749 /* Consume the `new' or `delete' token. */
10750 cp_lexer_consume_token (parser
->lexer
);
10752 /* Peek at the next token. */
10753 token
= cp_lexer_peek_token (parser
->lexer
);
10754 /* If it's a `[' token then this is the array variant of the
10756 if (token
->type
== CPP_OPEN_SQUARE
)
10758 /* Consume the `[' token. */
10759 cp_lexer_consume_token (parser
->lexer
);
10760 /* Look for the `]' token. */
10761 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10762 id
= ansi_opname (op
== NEW_EXPR
10763 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
10765 /* Otherwise, we have the non-array variant. */
10767 id
= ansi_opname (op
);
10773 id
= ansi_opname (PLUS_EXPR
);
10777 id
= ansi_opname (MINUS_EXPR
);
10781 id
= ansi_opname (MULT_EXPR
);
10785 id
= ansi_opname (TRUNC_DIV_EXPR
);
10789 id
= ansi_opname (TRUNC_MOD_EXPR
);
10793 id
= ansi_opname (BIT_XOR_EXPR
);
10797 id
= ansi_opname (BIT_AND_EXPR
);
10801 id
= ansi_opname (BIT_IOR_EXPR
);
10805 id
= ansi_opname (BIT_NOT_EXPR
);
10809 id
= ansi_opname (TRUTH_NOT_EXPR
);
10813 id
= ansi_assopname (NOP_EXPR
);
10817 id
= ansi_opname (LT_EXPR
);
10821 id
= ansi_opname (GT_EXPR
);
10825 id
= ansi_assopname (PLUS_EXPR
);
10829 id
= ansi_assopname (MINUS_EXPR
);
10833 id
= ansi_assopname (MULT_EXPR
);
10837 id
= ansi_assopname (TRUNC_DIV_EXPR
);
10841 id
= ansi_assopname (TRUNC_MOD_EXPR
);
10845 id
= ansi_assopname (BIT_XOR_EXPR
);
10849 id
= ansi_assopname (BIT_AND_EXPR
);
10853 id
= ansi_assopname (BIT_IOR_EXPR
);
10857 id
= ansi_opname (LSHIFT_EXPR
);
10861 id
= ansi_opname (RSHIFT_EXPR
);
10864 case CPP_LSHIFT_EQ
:
10865 id
= ansi_assopname (LSHIFT_EXPR
);
10868 case CPP_RSHIFT_EQ
:
10869 id
= ansi_assopname (RSHIFT_EXPR
);
10873 id
= ansi_opname (EQ_EXPR
);
10877 id
= ansi_opname (NE_EXPR
);
10881 id
= ansi_opname (LE_EXPR
);
10884 case CPP_GREATER_EQ
:
10885 id
= ansi_opname (GE_EXPR
);
10889 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
10893 id
= ansi_opname (TRUTH_ORIF_EXPR
);
10896 case CPP_PLUS_PLUS
:
10897 id
= ansi_opname (POSTINCREMENT_EXPR
);
10900 case CPP_MINUS_MINUS
:
10901 id
= ansi_opname (PREDECREMENT_EXPR
);
10905 id
= ansi_opname (COMPOUND_EXPR
);
10908 case CPP_DEREF_STAR
:
10909 id
= ansi_opname (MEMBER_REF
);
10913 id
= ansi_opname (COMPONENT_REF
);
10916 case CPP_OPEN_PAREN
:
10917 /* Consume the `('. */
10918 cp_lexer_consume_token (parser
->lexer
);
10919 /* Look for the matching `)'. */
10920 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10921 return ansi_opname (CALL_EXPR
);
10923 case CPP_OPEN_SQUARE
:
10924 /* Consume the `['. */
10925 cp_lexer_consume_token (parser
->lexer
);
10926 /* Look for the matching `]'. */
10927 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10928 return ansi_opname (ARRAY_REF
);
10931 /* Anything else is an error. */
10935 /* If we have selected an identifier, we need to consume the
10938 cp_lexer_consume_token (parser
->lexer
);
10939 /* Otherwise, no valid operator name was present. */
10942 cp_parser_error (parser
, "expected operator");
10943 id
= error_mark_node
;
10949 /* Parse a template-declaration.
10951 template-declaration:
10952 export [opt] template < template-parameter-list > declaration
10954 If MEMBER_P is TRUE, this template-declaration occurs within a
10957 The grammar rule given by the standard isn't correct. What
10958 is really meant is:
10960 template-declaration:
10961 export [opt] template-parameter-list-seq
10962 decl-specifier-seq [opt] init-declarator [opt] ;
10963 export [opt] template-parameter-list-seq
10964 function-definition
10966 template-parameter-list-seq:
10967 template-parameter-list-seq [opt]
10968 template < template-parameter-list > */
10971 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
10973 /* Check for `export'. */
10974 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
10976 /* Consume the `export' token. */
10977 cp_lexer_consume_token (parser
->lexer
);
10978 /* Warn that we do not support `export'. */
10979 warning (0, "keyword %<export%> not implemented, and will be ignored");
10982 cp_parser_template_declaration_after_export (parser
, member_p
);
10985 /* Parse a template-parameter-list.
10987 template-parameter-list:
10989 template-parameter-list , template-parameter
10991 Returns a TREE_LIST. Each node represents a template parameter.
10992 The nodes are connected via their TREE_CHAINs. */
10995 cp_parser_template_parameter_list (cp_parser
* parser
)
10997 tree parameter_list
= NULL_TREE
;
10999 begin_template_parm_list ();
11001 /* The loop below parses the template parms. We first need to know
11002 the total number of template parms to be able to compute proper
11003 canonical types of each dependent type. So after the loop, when
11004 we know the total number of template parms,
11005 end_template_parm_list computes the proper canonical types and
11006 fixes up the dependent types accordingly. */
11011 bool is_parameter_pack
;
11012 location_t parm_loc
;
11014 /* Parse the template-parameter. */
11015 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11016 parameter
= cp_parser_template_parameter (parser
,
11018 &is_parameter_pack
);
11019 /* Add it to the list. */
11020 if (parameter
!= error_mark_node
)
11021 parameter_list
= process_template_parm (parameter_list
,
11029 tree err_parm
= build_tree_list (parameter
, parameter
);
11030 parameter_list
= chainon (parameter_list
, err_parm
);
11033 /* If the next token is not a `,', we're done. */
11034 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11036 /* Otherwise, consume the `,' token. */
11037 cp_lexer_consume_token (parser
->lexer
);
11040 return end_template_parm_list (parameter_list
);
11043 /* Parse a template-parameter.
11045 template-parameter:
11047 parameter-declaration
11049 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11050 the parameter. The TREE_PURPOSE is the default value, if any.
11051 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11052 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11053 set to true iff this parameter is a parameter pack. */
11056 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
11057 bool *is_parameter_pack
)
11060 cp_parameter_declarator
*parameter_declarator
;
11061 cp_declarator
*id_declarator
;
11064 /* Assume it is a type parameter or a template parameter. */
11065 *is_non_type
= false;
11066 /* Assume it not a parameter pack. */
11067 *is_parameter_pack
= false;
11068 /* Peek at the next token. */
11069 token
= cp_lexer_peek_token (parser
->lexer
);
11070 /* If it is `class' or `template', we have a type-parameter. */
11071 if (token
->keyword
== RID_TEMPLATE
)
11072 return cp_parser_type_parameter (parser
, is_parameter_pack
);
11073 /* If it is `class' or `typename' we do not know yet whether it is a
11074 type parameter or a non-type parameter. Consider:
11076 template <typename T, typename T::X X> ...
11080 template <class C, class D*> ...
11082 Here, the first parameter is a type parameter, and the second is
11083 a non-type parameter. We can tell by looking at the token after
11084 the identifier -- if it is a `,', `=', or `>' then we have a type
11086 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
11088 /* Peek at the token after `class' or `typename'. */
11089 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11090 /* If it's an ellipsis, we have a template type parameter
11092 if (token
->type
== CPP_ELLIPSIS
)
11093 return cp_parser_type_parameter (parser
, is_parameter_pack
);
11094 /* If it's an identifier, skip it. */
11095 if (token
->type
== CPP_NAME
)
11096 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
11097 /* Now, see if the token looks like the end of a template
11099 if (token
->type
== CPP_COMMA
11100 || token
->type
== CPP_EQ
11101 || token
->type
== CPP_GREATER
)
11102 return cp_parser_type_parameter (parser
, is_parameter_pack
);
11105 /* Otherwise, it is a non-type parameter.
11109 When parsing a default template-argument for a non-type
11110 template-parameter, the first non-nested `>' is taken as the end
11111 of the template parameter-list rather than a greater-than
11113 *is_non_type
= true;
11114 parameter_declarator
11115 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
11116 /*parenthesized_p=*/NULL
);
11118 /* If the parameter declaration is marked as a parameter pack, set
11119 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11120 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11122 if (parameter_declarator
11123 && parameter_declarator
->declarator
11124 && parameter_declarator
->declarator
->parameter_pack_p
)
11126 *is_parameter_pack
= true;
11127 parameter_declarator
->declarator
->parameter_pack_p
= false;
11130 /* If the next token is an ellipsis, and we don't already have it
11131 marked as a parameter pack, then we have a parameter pack (that
11132 has no declarator). */
11133 if (!*is_parameter_pack
11134 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
11135 && declarator_can_be_parameter_pack (parameter_declarator
->declarator
))
11137 /* Consume the `...'. */
11138 cp_lexer_consume_token (parser
->lexer
);
11139 maybe_warn_variadic_templates ();
11141 *is_parameter_pack
= true;
11143 /* We might end up with a pack expansion as the type of the non-type
11144 template parameter, in which case this is a non-type template
11146 else if (parameter_declarator
11147 && parameter_declarator
->decl_specifiers
.type
11148 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
11150 *is_parameter_pack
= true;
11151 parameter_declarator
->decl_specifiers
.type
=
11152 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
11155 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11157 /* Parameter packs cannot have default arguments. However, a
11158 user may try to do so, so we'll parse them and give an
11159 appropriate diagnostic here. */
11161 /* Consume the `='. */
11162 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
11163 cp_lexer_consume_token (parser
->lexer
);
11165 /* Find the name of the parameter pack. */
11166 id_declarator
= parameter_declarator
->declarator
;
11167 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
11168 id_declarator
= id_declarator
->declarator
;
11170 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
11171 error_at (start_token
->location
,
11172 "template parameter pack %qD cannot have a default argument",
11173 id_declarator
->u
.id
.unqualified_name
);
11175 error_at (start_token
->location
,
11176 "template parameter pack cannot have a default argument");
11178 /* Parse the default argument, but throw away the result. */
11179 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
11182 parm
= grokdeclarator (parameter_declarator
->declarator
,
11183 ¶meter_declarator
->decl_specifiers
,
11184 TPARM
, /*initialized=*/0,
11185 /*attrlist=*/NULL
);
11186 if (parm
== error_mark_node
)
11187 return error_mark_node
;
11189 return build_tree_list (parameter_declarator
->default_argument
, parm
);
11192 /* Parse a type-parameter.
11195 class identifier [opt]
11196 class identifier [opt] = type-id
11197 typename identifier [opt]
11198 typename identifier [opt] = type-id
11199 template < template-parameter-list > class identifier [opt]
11200 template < template-parameter-list > class identifier [opt]
11203 GNU Extension (variadic templates):
11206 class ... identifier [opt]
11207 typename ... identifier [opt]
11209 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11210 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11211 the declaration of the parameter.
11213 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11216 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
11221 /* Look for a keyword to tell us what kind of parameter this is. */
11222 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
11224 return error_mark_node
;
11226 switch (token
->keyword
)
11232 tree default_argument
;
11234 /* If the next token is an ellipsis, we have a template
11236 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11238 /* Consume the `...' token. */
11239 cp_lexer_consume_token (parser
->lexer
);
11240 maybe_warn_variadic_templates ();
11242 *is_parameter_pack
= true;
11245 /* If the next token is an identifier, then it names the
11247 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11248 identifier
= cp_parser_identifier (parser
);
11250 identifier
= NULL_TREE
;
11252 /* Create the parameter. */
11253 parameter
= finish_template_type_parm (class_type_node
, identifier
);
11255 /* If the next token is an `=', we have a default argument. */
11256 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11258 /* Consume the `=' token. */
11259 cp_lexer_consume_token (parser
->lexer
);
11260 /* Parse the default-argument. */
11261 push_deferring_access_checks (dk_no_deferred
);
11262 default_argument
= cp_parser_type_id (parser
);
11264 /* Template parameter packs cannot have default
11266 if (*is_parameter_pack
)
11269 error_at (token
->location
,
11270 "template parameter pack %qD cannot have a "
11271 "default argument", identifier
);
11273 error_at (token
->location
,
11274 "template parameter packs cannot have "
11275 "default arguments");
11276 default_argument
= NULL_TREE
;
11278 pop_deferring_access_checks ();
11281 default_argument
= NULL_TREE
;
11283 /* Create the combined representation of the parameter and the
11284 default argument. */
11285 parameter
= build_tree_list (default_argument
, parameter
);
11292 tree default_argument
;
11294 /* Look for the `<'. */
11295 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
11296 /* Parse the template-parameter-list. */
11297 cp_parser_template_parameter_list (parser
);
11298 /* Look for the `>'. */
11299 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
11300 /* Look for the `class' keyword. */
11301 cp_parser_require_keyword (parser
, RID_CLASS
, RT_CLASS
);
11302 /* If the next token is an ellipsis, we have a template
11304 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11306 /* Consume the `...' token. */
11307 cp_lexer_consume_token (parser
->lexer
);
11308 maybe_warn_variadic_templates ();
11310 *is_parameter_pack
= true;
11312 /* If the next token is an `=', then there is a
11313 default-argument. If the next token is a `>', we are at
11314 the end of the parameter-list. If the next token is a `,',
11315 then we are at the end of this parameter. */
11316 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11317 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
11318 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11320 identifier
= cp_parser_identifier (parser
);
11321 /* Treat invalid names as if the parameter were nameless. */
11322 if (identifier
== error_mark_node
)
11323 identifier
= NULL_TREE
;
11326 identifier
= NULL_TREE
;
11328 /* Create the template parameter. */
11329 parameter
= finish_template_template_parm (class_type_node
,
11332 /* If the next token is an `=', then there is a
11333 default-argument. */
11334 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11338 /* Consume the `='. */
11339 cp_lexer_consume_token (parser
->lexer
);
11340 /* Parse the id-expression. */
11341 push_deferring_access_checks (dk_no_deferred
);
11342 /* save token before parsing the id-expression, for error
11344 token
= cp_lexer_peek_token (parser
->lexer
);
11346 = cp_parser_id_expression (parser
,
11347 /*template_keyword_p=*/false,
11348 /*check_dependency_p=*/true,
11349 /*template_p=*/&is_template
,
11350 /*declarator_p=*/false,
11351 /*optional_p=*/false);
11352 if (TREE_CODE (default_argument
) == TYPE_DECL
)
11353 /* If the id-expression was a template-id that refers to
11354 a template-class, we already have the declaration here,
11355 so no further lookup is needed. */
11358 /* Look up the name. */
11360 = cp_parser_lookup_name (parser
, default_argument
,
11362 /*is_template=*/is_template
,
11363 /*is_namespace=*/false,
11364 /*check_dependency=*/true,
11365 /*ambiguous_decls=*/NULL
,
11367 /* See if the default argument is valid. */
11369 = check_template_template_default_arg (default_argument
);
11371 /* Template parameter packs cannot have default
11373 if (*is_parameter_pack
)
11376 error_at (token
->location
,
11377 "template parameter pack %qD cannot "
11378 "have a default argument",
11381 error_at (token
->location
, "template parameter packs cannot "
11382 "have default arguments");
11383 default_argument
= NULL_TREE
;
11385 pop_deferring_access_checks ();
11388 default_argument
= NULL_TREE
;
11390 /* Create the combined representation of the parameter and the
11391 default argument. */
11392 parameter
= build_tree_list (default_argument
, parameter
);
11397 gcc_unreachable ();
11404 /* Parse a template-id.
11407 template-name < template-argument-list [opt] >
11409 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11410 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11411 returned. Otherwise, if the template-name names a function, or set
11412 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11413 names a class, returns a TYPE_DECL for the specialization.
11415 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11416 uninstantiated templates. */
11419 cp_parser_template_id (cp_parser
*parser
,
11420 bool template_keyword_p
,
11421 bool check_dependency_p
,
11422 bool is_declaration
)
11428 cp_token_position start_of_id
= 0;
11429 deferred_access_check
*chk
;
11430 VEC (deferred_access_check
,gc
) *access_check
;
11431 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
11432 bool is_identifier
;
11434 /* If the next token corresponds to a template-id, there is no need
11436 next_token
= cp_lexer_peek_token (parser
->lexer
);
11437 if (next_token
->type
== CPP_TEMPLATE_ID
)
11439 struct tree_check
*check_value
;
11441 /* Get the stored value. */
11442 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
11443 /* Perform any access checks that were deferred. */
11444 access_check
= check_value
->checks
;
11447 FOR_EACH_VEC_ELT (deferred_access_check
, access_check
, i
, chk
)
11448 perform_or_defer_access_check (chk
->binfo
,
11452 /* Return the stored value. */
11453 return check_value
->value
;
11456 /* Avoid performing name lookup if there is no possibility of
11457 finding a template-id. */
11458 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
11459 || (next_token
->type
== CPP_NAME
11460 && !cp_parser_nth_token_starts_template_argument_list_p
11463 cp_parser_error (parser
, "expected template-id");
11464 return error_mark_node
;
11467 /* Remember where the template-id starts. */
11468 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
11469 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
11471 push_deferring_access_checks (dk_deferred
);
11473 /* Parse the template-name. */
11474 is_identifier
= false;
11475 templ
= cp_parser_template_name (parser
, template_keyword_p
,
11476 check_dependency_p
,
11479 if (templ
== error_mark_node
|| is_identifier
)
11481 pop_deferring_access_checks ();
11485 /* If we find the sequence `[:' after a template-name, it's probably
11486 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11487 parse correctly the argument list. */
11488 next_token
= cp_lexer_peek_token (parser
->lexer
);
11489 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11490 if (next_token
->type
== CPP_OPEN_SQUARE
11491 && next_token
->flags
& DIGRAPH
11492 && next_token_2
->type
== CPP_COLON
11493 && !(next_token_2
->flags
& PREV_WHITE
))
11495 cp_parser_parse_tentatively (parser
);
11496 /* Change `:' into `::'. */
11497 next_token_2
->type
= CPP_SCOPE
;
11498 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11500 cp_lexer_consume_token (parser
->lexer
);
11502 /* Parse the arguments. */
11503 arguments
= cp_parser_enclosed_template_argument_list (parser
);
11504 if (!cp_parser_parse_definitely (parser
))
11506 /* If we couldn't parse an argument list, then we revert our changes
11507 and return simply an error. Maybe this is not a template-id
11509 next_token_2
->type
= CPP_COLON
;
11510 cp_parser_error (parser
, "expected %<<%>");
11511 pop_deferring_access_checks ();
11512 return error_mark_node
;
11514 /* Otherwise, emit an error about the invalid digraph, but continue
11515 parsing because we got our argument list. */
11516 if (permerror (next_token
->location
,
11517 "%<<::%> cannot begin a template-argument list"))
11519 static bool hint
= false;
11520 inform (next_token
->location
,
11521 "%<<:%> is an alternate spelling for %<[%>."
11522 " Insert whitespace between %<<%> and %<::%>");
11523 if (!hint
&& !flag_permissive
)
11525 inform (next_token
->location
, "(if you use %<-fpermissive%>"
11526 " G++ will accept your code)");
11533 /* Look for the `<' that starts the template-argument-list. */
11534 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
11536 pop_deferring_access_checks ();
11537 return error_mark_node
;
11539 /* Parse the arguments. */
11540 arguments
= cp_parser_enclosed_template_argument_list (parser
);
11543 /* Build a representation of the specialization. */
11544 if (TREE_CODE (templ
) == IDENTIFIER_NODE
)
11545 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, templ
, arguments
);
11546 else if (DECL_CLASS_TEMPLATE_P (templ
)
11547 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
11549 bool entering_scope
;
11550 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11551 template (rather than some instantiation thereof) only if
11552 is not nested within some other construct. For example, in
11553 "template <typename T> void f(T) { A<T>::", A<T> is just an
11554 instantiation of A. */
11555 entering_scope
= (template_parm_scope_p ()
11556 && cp_lexer_next_token_is (parser
->lexer
,
11559 = finish_template_type (templ
, arguments
, entering_scope
);
11563 /* If it's not a class-template or a template-template, it should be
11564 a function-template. */
11565 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
11566 || TREE_CODE (templ
) == OVERLOAD
11567 || BASELINK_P (templ
)));
11569 template_id
= lookup_template_function (templ
, arguments
);
11572 /* If parsing tentatively, replace the sequence of tokens that makes
11573 up the template-id with a CPP_TEMPLATE_ID token. That way,
11574 should we re-parse the token stream, we will not have to repeat
11575 the effort required to do the parse, nor will we issue duplicate
11576 error messages about problems during instantiation of the
11580 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
11582 /* Reset the contents of the START_OF_ID token. */
11583 token
->type
= CPP_TEMPLATE_ID
;
11584 /* Retrieve any deferred checks. Do not pop this access checks yet
11585 so the memory will not be reclaimed during token replacing below. */
11586 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
11587 token
->u
.tree_check_value
->value
= template_id
;
11588 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
11589 token
->keyword
= RID_MAX
;
11591 /* Purge all subsequent tokens. */
11592 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
11594 /* ??? Can we actually assume that, if template_id ==
11595 error_mark_node, we will have issued a diagnostic to the
11596 user, as opposed to simply marking the tentative parse as
11598 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
11599 error_at (token
->location
, "parse error in template argument list");
11602 pop_deferring_access_checks ();
11603 return template_id
;
11606 /* Parse a template-name.
11611 The standard should actually say:
11615 operator-function-id
11617 A defect report has been filed about this issue.
11619 A conversion-function-id cannot be a template name because they cannot
11620 be part of a template-id. In fact, looking at this code:
11622 a.operator K<int>()
11624 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11625 It is impossible to call a templated conversion-function-id with an
11626 explicit argument list, since the only allowed template parameter is
11627 the type to which it is converting.
11629 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11630 `template' keyword, in a construction like:
11634 In that case `f' is taken to be a template-name, even though there
11635 is no way of knowing for sure.
11637 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11638 name refers to a set of overloaded functions, at least one of which
11639 is a template, or an IDENTIFIER_NODE with the name of the template,
11640 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11641 names are looked up inside uninstantiated templates. */
11644 cp_parser_template_name (cp_parser
* parser
,
11645 bool template_keyword_p
,
11646 bool check_dependency_p
,
11647 bool is_declaration
,
11648 bool *is_identifier
)
11653 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11655 /* If the next token is `operator', then we have either an
11656 operator-function-id or a conversion-function-id. */
11657 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
11659 /* We don't know whether we're looking at an
11660 operator-function-id or a conversion-function-id. */
11661 cp_parser_parse_tentatively (parser
);
11662 /* Try an operator-function-id. */
11663 identifier
= cp_parser_operator_function_id (parser
);
11664 /* If that didn't work, try a conversion-function-id. */
11665 if (!cp_parser_parse_definitely (parser
))
11667 cp_parser_error (parser
, "expected template-name");
11668 return error_mark_node
;
11671 /* Look for the identifier. */
11673 identifier
= cp_parser_identifier (parser
);
11675 /* If we didn't find an identifier, we don't have a template-id. */
11676 if (identifier
== error_mark_node
)
11677 return error_mark_node
;
11679 /* If the name immediately followed the `template' keyword, then it
11680 is a template-name. However, if the next token is not `<', then
11681 we do not treat it as a template-name, since it is not being used
11682 as part of a template-id. This enables us to handle constructs
11685 template <typename T> struct S { S(); };
11686 template <typename T> S<T>::S();
11688 correctly. We would treat `S' as a template -- if it were `S<T>'
11689 -- but we do not if there is no `<'. */
11691 if (processing_template_decl
11692 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
11694 /* In a declaration, in a dependent context, we pretend that the
11695 "template" keyword was present in order to improve error
11696 recovery. For example, given:
11698 template <typename T> void f(T::X<int>);
11700 we want to treat "X<int>" as a template-id. */
11702 && !template_keyword_p
11703 && parser
->scope
&& TYPE_P (parser
->scope
)
11704 && check_dependency_p
11705 && dependent_scope_p (parser
->scope
)
11706 /* Do not do this for dtors (or ctors), since they never
11707 need the template keyword before their name. */
11708 && !constructor_name_p (identifier
, parser
->scope
))
11710 cp_token_position start
= 0;
11712 /* Explain what went wrong. */
11713 error_at (token
->location
, "non-template %qD used as template",
11715 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
11716 parser
->scope
, identifier
);
11717 /* If parsing tentatively, find the location of the "<" token. */
11718 if (cp_parser_simulate_error (parser
))
11719 start
= cp_lexer_token_position (parser
->lexer
, true);
11720 /* Parse the template arguments so that we can issue error
11721 messages about them. */
11722 cp_lexer_consume_token (parser
->lexer
);
11723 cp_parser_enclosed_template_argument_list (parser
);
11724 /* Skip tokens until we find a good place from which to
11725 continue parsing. */
11726 cp_parser_skip_to_closing_parenthesis (parser
,
11727 /*recovering=*/true,
11729 /*consume_paren=*/false);
11730 /* If parsing tentatively, permanently remove the
11731 template argument list. That will prevent duplicate
11732 error messages from being issued about the missing
11733 "template" keyword. */
11735 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
11737 *is_identifier
= true;
11741 /* If the "template" keyword is present, then there is generally
11742 no point in doing name-lookup, so we just return IDENTIFIER.
11743 But, if the qualifying scope is non-dependent then we can
11744 (and must) do name-lookup normally. */
11745 if (template_keyword_p
11747 || (TYPE_P (parser
->scope
)
11748 && dependent_type_p (parser
->scope
))))
11752 /* Look up the name. */
11753 decl
= cp_parser_lookup_name (parser
, identifier
,
11755 /*is_template=*/true,
11756 /*is_namespace=*/false,
11757 check_dependency_p
,
11758 /*ambiguous_decls=*/NULL
,
11761 /* If DECL is a template, then the name was a template-name. */
11762 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
11766 tree fn
= NULL_TREE
;
11768 /* The standard does not explicitly indicate whether a name that
11769 names a set of overloaded declarations, some of which are
11770 templates, is a template-name. However, such a name should
11771 be a template-name; otherwise, there is no way to form a
11772 template-id for the overloaded templates. */
11773 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
11774 if (TREE_CODE (fns
) == OVERLOAD
)
11775 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
11776 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
11781 /* The name does not name a template. */
11782 cp_parser_error (parser
, "expected template-name");
11783 return error_mark_node
;
11787 /* If DECL is dependent, and refers to a function, then just return
11788 its name; we will look it up again during template instantiation. */
11789 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
11791 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
11792 if (TYPE_P (scope
) && dependent_type_p (scope
))
11799 /* Parse a template-argument-list.
11801 template-argument-list:
11802 template-argument ... [opt]
11803 template-argument-list , template-argument ... [opt]
11805 Returns a TREE_VEC containing the arguments. */
11808 cp_parser_template_argument_list (cp_parser
* parser
)
11810 tree fixed_args
[10];
11811 unsigned n_args
= 0;
11812 unsigned alloced
= 10;
11813 tree
*arg_ary
= fixed_args
;
11815 bool saved_in_template_argument_list_p
;
11817 bool saved_non_ice_p
;
11819 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
11820 parser
->in_template_argument_list_p
= true;
11821 /* Even if the template-id appears in an integral
11822 constant-expression, the contents of the argument list do
11824 saved_ice_p
= parser
->integral_constant_expression_p
;
11825 parser
->integral_constant_expression_p
= false;
11826 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
11827 parser
->non_integral_constant_expression_p
= false;
11828 /* Parse the arguments. */
11834 /* Consume the comma. */
11835 cp_lexer_consume_token (parser
->lexer
);
11837 /* Parse the template-argument. */
11838 argument
= cp_parser_template_argument (parser
);
11840 /* If the next token is an ellipsis, we're expanding a template
11842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11844 if (argument
== error_mark_node
)
11846 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11847 error_at (token
->location
,
11848 "expected parameter pack before %<...%>");
11850 /* Consume the `...' token. */
11851 cp_lexer_consume_token (parser
->lexer
);
11853 /* Make the argument into a TYPE_PACK_EXPANSION or
11854 EXPR_PACK_EXPANSION. */
11855 argument
= make_pack_expansion (argument
);
11858 if (n_args
== alloced
)
11862 if (arg_ary
== fixed_args
)
11864 arg_ary
= XNEWVEC (tree
, alloced
);
11865 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
11868 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
11870 arg_ary
[n_args
++] = argument
;
11872 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
11874 vec
= make_tree_vec (n_args
);
11877 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
11879 if (arg_ary
!= fixed_args
)
11881 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
11882 parser
->integral_constant_expression_p
= saved_ice_p
;
11883 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
11884 #ifdef ENABLE_CHECKING
11885 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
11890 /* Parse a template-argument.
11893 assignment-expression
11897 The representation is that of an assignment-expression, type-id, or
11898 id-expression -- except that the qualified id-expression is
11899 evaluated, so that the value returned is either a DECL or an
11902 Although the standard says "assignment-expression", it forbids
11903 throw-expressions or assignments in the template argument.
11904 Therefore, we use "conditional-expression" instead. */
11907 cp_parser_template_argument (cp_parser
* parser
)
11912 bool maybe_type_id
= false;
11913 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
11916 /* There's really no way to know what we're looking at, so we just
11917 try each alternative in order.
11921 In a template-argument, an ambiguity between a type-id and an
11922 expression is resolved to a type-id, regardless of the form of
11923 the corresponding template-parameter.
11925 Therefore, we try a type-id first. */
11926 cp_parser_parse_tentatively (parser
);
11927 argument
= cp_parser_template_type_arg (parser
);
11928 /* If there was no error parsing the type-id but the next token is a
11929 '>>', our behavior depends on which dialect of C++ we're
11930 parsing. In C++98, we probably found a typo for '> >'. But there
11931 are type-id which are also valid expressions. For instance:
11933 struct X { int operator >> (int); };
11934 template <int V> struct Foo {};
11937 Here 'X()' is a valid type-id of a function type, but the user just
11938 wanted to write the expression "X() >> 5". Thus, we remember that we
11939 found a valid type-id, but we still try to parse the argument as an
11940 expression to see what happens.
11942 In C++0x, the '>>' will be considered two separate '>'
11944 if (!cp_parser_error_occurred (parser
)
11945 && cxx_dialect
== cxx98
11946 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
11948 maybe_type_id
= true;
11949 cp_parser_abort_tentative_parse (parser
);
11953 /* If the next token isn't a `,' or a `>', then this argument wasn't
11954 really finished. This means that the argument is not a valid
11956 if (!cp_parser_next_token_ends_template_argument_p (parser
))
11957 cp_parser_error (parser
, "expected template-argument");
11958 /* If that worked, we're done. */
11959 if (cp_parser_parse_definitely (parser
))
11962 /* We're still not sure what the argument will be. */
11963 cp_parser_parse_tentatively (parser
);
11964 /* Try a template. */
11965 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
11966 argument
= cp_parser_id_expression (parser
,
11967 /*template_keyword_p=*/false,
11968 /*check_dependency_p=*/true,
11970 /*declarator_p=*/false,
11971 /*optional_p=*/false);
11972 /* If the next token isn't a `,' or a `>', then this argument wasn't
11973 really finished. */
11974 if (!cp_parser_next_token_ends_template_argument_p (parser
))
11975 cp_parser_error (parser
, "expected template-argument");
11976 if (!cp_parser_error_occurred (parser
))
11978 /* Figure out what is being referred to. If the id-expression
11979 was for a class template specialization, then we will have a
11980 TYPE_DECL at this point. There is no need to do name lookup
11981 at this point in that case. */
11982 if (TREE_CODE (argument
) != TYPE_DECL
)
11983 argument
= cp_parser_lookup_name (parser
, argument
,
11985 /*is_template=*/template_p
,
11986 /*is_namespace=*/false,
11987 /*check_dependency=*/true,
11988 /*ambiguous_decls=*/NULL
,
11989 argument_start_token
->location
);
11990 if (TREE_CODE (argument
) != TEMPLATE_DECL
11991 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
11992 cp_parser_error (parser
, "expected template-name");
11994 if (cp_parser_parse_definitely (parser
))
11996 /* It must be a non-type argument. There permitted cases are given
11997 in [temp.arg.nontype]:
11999 -- an integral constant-expression of integral or enumeration
12002 -- the name of a non-type template-parameter; or
12004 -- the name of an object or function with external linkage...
12006 -- the address of an object or function with external linkage...
12008 -- a pointer to member... */
12009 /* Look for a non-type template parameter. */
12010 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12012 cp_parser_parse_tentatively (parser
);
12013 argument
= cp_parser_primary_expression (parser
,
12014 /*address_p=*/false,
12016 /*template_arg_p=*/true,
12018 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
12019 || !cp_parser_next_token_ends_template_argument_p (parser
))
12020 cp_parser_simulate_error (parser
);
12021 if (cp_parser_parse_definitely (parser
))
12025 /* If the next token is "&", the argument must be the address of an
12026 object or function with external linkage. */
12027 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
12029 cp_lexer_consume_token (parser
->lexer
);
12030 /* See if we might have an id-expression. */
12031 token
= cp_lexer_peek_token (parser
->lexer
);
12032 if (token
->type
== CPP_NAME
12033 || token
->keyword
== RID_OPERATOR
12034 || token
->type
== CPP_SCOPE
12035 || token
->type
== CPP_TEMPLATE_ID
12036 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
12038 cp_parser_parse_tentatively (parser
);
12039 argument
= cp_parser_primary_expression (parser
,
12042 /*template_arg_p=*/true,
12044 if (cp_parser_error_occurred (parser
)
12045 || !cp_parser_next_token_ends_template_argument_p (parser
))
12046 cp_parser_abort_tentative_parse (parser
);
12051 if (TREE_CODE (argument
) == INDIRECT_REF
)
12053 gcc_assert (REFERENCE_REF_P (argument
));
12054 argument
= TREE_OPERAND (argument
, 0);
12057 /* If we're in a template, we represent a qualified-id referring
12058 to a static data member as a SCOPE_REF even if the scope isn't
12059 dependent so that we can check access control later. */
12061 if (TREE_CODE (probe
) == SCOPE_REF
)
12062 probe
= TREE_OPERAND (probe
, 1);
12063 if (TREE_CODE (probe
) == VAR_DECL
)
12065 /* A variable without external linkage might still be a
12066 valid constant-expression, so no error is issued here
12067 if the external-linkage check fails. */
12068 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
12069 cp_parser_simulate_error (parser
);
12071 else if (is_overloaded_fn (argument
))
12072 /* All overloaded functions are allowed; if the external
12073 linkage test does not pass, an error will be issued
12077 && (TREE_CODE (argument
) == OFFSET_REF
12078 || TREE_CODE (argument
) == SCOPE_REF
))
12079 /* A pointer-to-member. */
12081 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
12084 cp_parser_simulate_error (parser
);
12086 if (cp_parser_parse_definitely (parser
))
12089 argument
= build_x_unary_op (ADDR_EXPR
, argument
,
12090 tf_warning_or_error
);
12095 /* If the argument started with "&", there are no other valid
12096 alternatives at this point. */
12099 cp_parser_error (parser
, "invalid non-type template argument");
12100 return error_mark_node
;
12103 /* If the argument wasn't successfully parsed as a type-id followed
12104 by '>>', the argument can only be a constant expression now.
12105 Otherwise, we try parsing the constant-expression tentatively,
12106 because the argument could really be a type-id. */
12108 cp_parser_parse_tentatively (parser
);
12109 argument
= cp_parser_constant_expression (parser
,
12110 /*allow_non_constant_p=*/false,
12111 /*non_constant_p=*/NULL
);
12112 argument
= fold_non_dependent_expr (argument
);
12113 if (!maybe_type_id
)
12115 if (!cp_parser_next_token_ends_template_argument_p (parser
))
12116 cp_parser_error (parser
, "expected template-argument");
12117 if (cp_parser_parse_definitely (parser
))
12119 /* We did our best to parse the argument as a non type-id, but that
12120 was the only alternative that matched (albeit with a '>' after
12121 it). We can assume it's just a typo from the user, and a
12122 diagnostic will then be issued. */
12123 return cp_parser_template_type_arg (parser
);
12126 /* Parse an explicit-instantiation.
12128 explicit-instantiation:
12129 template declaration
12131 Although the standard says `declaration', what it really means is:
12133 explicit-instantiation:
12134 template decl-specifier-seq [opt] declarator [opt] ;
12136 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12137 supposed to be allowed. A defect report has been filed about this
12142 explicit-instantiation:
12143 storage-class-specifier template
12144 decl-specifier-seq [opt] declarator [opt] ;
12145 function-specifier template
12146 decl-specifier-seq [opt] declarator [opt] ; */
12149 cp_parser_explicit_instantiation (cp_parser
* parser
)
12151 int declares_class_or_enum
;
12152 cp_decl_specifier_seq decl_specifiers
;
12153 tree extension_specifier
= NULL_TREE
;
12155 /* Look for an (optional) storage-class-specifier or
12156 function-specifier. */
12157 if (cp_parser_allow_gnu_extensions_p (parser
))
12159 extension_specifier
12160 = cp_parser_storage_class_specifier_opt (parser
);
12161 if (!extension_specifier
)
12162 extension_specifier
12163 = cp_parser_function_specifier_opt (parser
,
12164 /*decl_specs=*/NULL
);
12167 /* Look for the `template' keyword. */
12168 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
12169 /* Let the front end know that we are processing an explicit
12171 begin_explicit_instantiation ();
12172 /* [temp.explicit] says that we are supposed to ignore access
12173 control while processing explicit instantiation directives. */
12174 push_deferring_access_checks (dk_no_check
);
12175 /* Parse a decl-specifier-seq. */
12176 cp_parser_decl_specifier_seq (parser
,
12177 CP_PARSER_FLAGS_OPTIONAL
,
12179 &declares_class_or_enum
);
12180 /* If there was exactly one decl-specifier, and it declared a class,
12181 and there's no declarator, then we have an explicit type
12183 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
12187 type
= check_tag_decl (&decl_specifiers
);
12188 /* Turn access control back on for names used during
12189 template instantiation. */
12190 pop_deferring_access_checks ();
12192 do_type_instantiation (type
, extension_specifier
,
12193 /*complain=*/tf_error
);
12197 cp_declarator
*declarator
;
12200 /* Parse the declarator. */
12202 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12203 /*ctor_dtor_or_conv_p=*/NULL
,
12204 /*parenthesized_p=*/NULL
,
12205 /*member_p=*/false);
12206 if (declares_class_or_enum
& 2)
12207 cp_parser_check_for_definition_in_return_type (declarator
,
12208 decl_specifiers
.type
,
12209 decl_specifiers
.type_location
);
12210 if (declarator
!= cp_error_declarator
)
12212 if (decl_specifiers
.specs
[(int)ds_inline
])
12213 permerror (input_location
, "explicit instantiation shall not use"
12214 " %<inline%> specifier");
12215 if (decl_specifiers
.specs
[(int)ds_constexpr
])
12216 permerror (input_location
, "explicit instantiation shall not use"
12217 " %<constexpr%> specifier");
12219 decl
= grokdeclarator (declarator
, &decl_specifiers
,
12220 NORMAL
, 0, &decl_specifiers
.attributes
);
12221 /* Turn access control back on for names used during
12222 template instantiation. */
12223 pop_deferring_access_checks ();
12224 /* Do the explicit instantiation. */
12225 do_decl_instantiation (decl
, extension_specifier
);
12229 pop_deferring_access_checks ();
12230 /* Skip the body of the explicit instantiation. */
12231 cp_parser_skip_to_end_of_statement (parser
);
12234 /* We're done with the instantiation. */
12235 end_explicit_instantiation ();
12237 cp_parser_consume_semicolon_at_end_of_statement (parser
);
12240 /* Parse an explicit-specialization.
12242 explicit-specialization:
12243 template < > declaration
12245 Although the standard says `declaration', what it really means is:
12247 explicit-specialization:
12248 template <> decl-specifier [opt] init-declarator [opt] ;
12249 template <> function-definition
12250 template <> explicit-specialization
12251 template <> template-declaration */
12254 cp_parser_explicit_specialization (cp_parser
* parser
)
12256 bool need_lang_pop
;
12257 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12259 /* Look for the `template' keyword. */
12260 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
12261 /* Look for the `<'. */
12262 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
12263 /* Look for the `>'. */
12264 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
12265 /* We have processed another parameter list. */
12266 ++parser
->num_template_parameter_lists
;
12269 A template ... explicit specialization ... shall not have C
12271 if (current_lang_name
== lang_name_c
)
12273 error_at (token
->location
, "template specialization with C linkage");
12274 /* Give it C++ linkage to avoid confusing other parts of the
12276 push_lang_context (lang_name_cplusplus
);
12277 need_lang_pop
= true;
12280 need_lang_pop
= false;
12281 /* Let the front end know that we are beginning a specialization. */
12282 if (!begin_specialization ())
12284 end_specialization ();
12288 /* If the next keyword is `template', we need to figure out whether
12289 or not we're looking a template-declaration. */
12290 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
12292 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
12293 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
12294 cp_parser_template_declaration_after_export (parser
,
12295 /*member_p=*/false);
12297 cp_parser_explicit_specialization (parser
);
12300 /* Parse the dependent declaration. */
12301 cp_parser_single_declaration (parser
,
12303 /*member_p=*/false,
12304 /*explicit_specialization_p=*/true,
12305 /*friend_p=*/NULL
);
12306 /* We're done with the specialization. */
12307 end_specialization ();
12308 /* For the erroneous case of a template with C linkage, we pushed an
12309 implicit C++ linkage scope; exit that scope now. */
12311 pop_lang_context ();
12312 /* We're done with this parameter list. */
12313 --parser
->num_template_parameter_lists
;
12316 /* Parse a type-specifier.
12319 simple-type-specifier
12322 elaborated-type-specifier
12330 Returns a representation of the type-specifier. For a
12331 class-specifier, enum-specifier, or elaborated-type-specifier, a
12332 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12334 The parser flags FLAGS is used to control type-specifier parsing.
12336 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12337 in a decl-specifier-seq.
12339 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12340 class-specifier, enum-specifier, or elaborated-type-specifier, then
12341 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12342 if a type is declared; 2 if it is defined. Otherwise, it is set to
12345 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12346 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12347 is set to FALSE. */
12350 cp_parser_type_specifier (cp_parser
* parser
,
12351 cp_parser_flags flags
,
12352 cp_decl_specifier_seq
*decl_specs
,
12353 bool is_declaration
,
12354 int* declares_class_or_enum
,
12355 bool* is_cv_qualifier
)
12357 tree type_spec
= NULL_TREE
;
12360 cp_decl_spec ds
= ds_last
;
12362 /* Assume this type-specifier does not declare a new type. */
12363 if (declares_class_or_enum
)
12364 *declares_class_or_enum
= 0;
12365 /* And that it does not specify a cv-qualifier. */
12366 if (is_cv_qualifier
)
12367 *is_cv_qualifier
= false;
12368 /* Peek at the next token. */
12369 token
= cp_lexer_peek_token (parser
->lexer
);
12371 /* If we're looking at a keyword, we can use that to guide the
12372 production we choose. */
12373 keyword
= token
->keyword
;
12377 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
12378 goto elaborated_type_specifier
;
12380 /* Look for the enum-specifier. */
12381 type_spec
= cp_parser_enum_specifier (parser
);
12382 /* If that worked, we're done. */
12385 if (declares_class_or_enum
)
12386 *declares_class_or_enum
= 2;
12388 cp_parser_set_decl_spec_type (decl_specs
,
12391 /*user_defined_p=*/true);
12395 goto elaborated_type_specifier
;
12397 /* Any of these indicate either a class-specifier, or an
12398 elaborated-type-specifier. */
12402 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
12403 goto elaborated_type_specifier
;
12405 /* Parse tentatively so that we can back up if we don't find a
12406 class-specifier. */
12407 cp_parser_parse_tentatively (parser
);
12408 /* Look for the class-specifier. */
12409 type_spec
= cp_parser_class_specifier (parser
);
12410 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
12411 /* If that worked, we're done. */
12412 if (cp_parser_parse_definitely (parser
))
12414 if (declares_class_or_enum
)
12415 *declares_class_or_enum
= 2;
12417 cp_parser_set_decl_spec_type (decl_specs
,
12420 /*user_defined_p=*/true);
12424 /* Fall through. */
12425 elaborated_type_specifier
:
12426 /* We're declaring (not defining) a class or enum. */
12427 if (declares_class_or_enum
)
12428 *declares_class_or_enum
= 1;
12430 /* Fall through. */
12432 /* Look for an elaborated-type-specifier. */
12434 = (cp_parser_elaborated_type_specifier
12436 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
12439 cp_parser_set_decl_spec_type (decl_specs
,
12442 /*user_defined_p=*/true);
12447 if (is_cv_qualifier
)
12448 *is_cv_qualifier
= true;
12453 if (is_cv_qualifier
)
12454 *is_cv_qualifier
= true;
12459 if (is_cv_qualifier
)
12460 *is_cv_qualifier
= true;
12464 /* The `__complex__' keyword is a GNU extension. */
12472 /* Handle simple keywords. */
12477 ++decl_specs
->specs
[(int)ds
];
12478 decl_specs
->any_specifiers_p
= true;
12480 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12483 /* If we do not already have a type-specifier, assume we are looking
12484 at a simple-type-specifier. */
12485 type_spec
= cp_parser_simple_type_specifier (parser
,
12489 /* If we didn't find a type-specifier, and a type-specifier was not
12490 optional in this context, issue an error message. */
12491 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
12493 cp_parser_error (parser
, "expected type specifier");
12494 return error_mark_node
;
12500 /* Parse a simple-type-specifier.
12502 simple-type-specifier:
12503 :: [opt] nested-name-specifier [opt] type-name
12504 :: [opt] nested-name-specifier template template-id
12519 simple-type-specifier:
12521 decltype ( expression )
12524 __underlying_type ( type-id )
12528 simple-type-specifier:
12530 __typeof__ unary-expression
12531 __typeof__ ( type-id )
12533 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12534 appropriately updated. */
12537 cp_parser_simple_type_specifier (cp_parser
* parser
,
12538 cp_decl_specifier_seq
*decl_specs
,
12539 cp_parser_flags flags
)
12541 tree type
= NULL_TREE
;
12544 /* Peek at the next token. */
12545 token
= cp_lexer_peek_token (parser
->lexer
);
12547 /* If we're looking at a keyword, things are easy. */
12548 switch (token
->keyword
)
12552 decl_specs
->explicit_char_p
= true;
12553 type
= char_type_node
;
12556 type
= char16_type_node
;
12559 type
= char32_type_node
;
12562 type
= wchar_type_node
;
12565 type
= boolean_type_node
;
12569 ++decl_specs
->specs
[(int) ds_short
];
12570 type
= short_integer_type_node
;
12574 decl_specs
->explicit_int_p
= true;
12575 type
= integer_type_node
;
12578 if (!int128_integer_type_node
)
12581 decl_specs
->explicit_int128_p
= true;
12582 type
= int128_integer_type_node
;
12586 ++decl_specs
->specs
[(int) ds_long
];
12587 type
= long_integer_type_node
;
12591 ++decl_specs
->specs
[(int) ds_signed
];
12592 type
= integer_type_node
;
12596 ++decl_specs
->specs
[(int) ds_unsigned
];
12597 type
= unsigned_type_node
;
12600 type
= float_type_node
;
12603 type
= double_type_node
;
12606 type
= void_type_node
;
12610 maybe_warn_cpp0x (CPP0X_AUTO
);
12611 type
= make_auto ();
12615 /* Parse the `decltype' type. */
12616 type
= cp_parser_decltype (parser
);
12619 cp_parser_set_decl_spec_type (decl_specs
, type
,
12621 /*user_defined_p=*/true);
12626 /* Consume the `typeof' token. */
12627 cp_lexer_consume_token (parser
->lexer
);
12628 /* Parse the operand to `typeof'. */
12629 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
12630 /* If it is not already a TYPE, take its type. */
12631 if (!TYPE_P (type
))
12632 type
= finish_typeof (type
);
12635 cp_parser_set_decl_spec_type (decl_specs
, type
,
12637 /*user_defined_p=*/true);
12641 case RID_UNDERLYING_TYPE
:
12642 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
12645 cp_parser_set_decl_spec_type (decl_specs
, type
,
12647 /*user_defined_p=*/true);
12655 /* If the type-specifier was for a built-in type, we're done. */
12658 /* Record the type. */
12660 && (token
->keyword
!= RID_SIGNED
12661 && token
->keyword
!= RID_UNSIGNED
12662 && token
->keyword
!= RID_SHORT
12663 && token
->keyword
!= RID_LONG
))
12664 cp_parser_set_decl_spec_type (decl_specs
,
12667 /*user_defined=*/false);
12669 decl_specs
->any_specifiers_p
= true;
12671 /* Consume the token. */
12672 cp_lexer_consume_token (parser
->lexer
);
12674 /* There is no valid C++ program where a non-template type is
12675 followed by a "<". That usually indicates that the user thought
12676 that the type was a template. */
12677 cp_parser_check_for_invalid_template_id (parser
, type
, token
->location
);
12679 return TYPE_NAME (type
);
12682 /* The type-specifier must be a user-defined type. */
12683 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
12688 /* Don't gobble tokens or issue error messages if this is an
12689 optional type-specifier. */
12690 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
12691 cp_parser_parse_tentatively (parser
);
12693 /* Look for the optional `::' operator. */
12695 = (cp_parser_global_scope_opt (parser
,
12696 /*current_scope_valid_p=*/false)
12698 /* Look for the nested-name specifier. */
12700 = (cp_parser_nested_name_specifier_opt (parser
,
12701 /*typename_keyword_p=*/false,
12702 /*check_dependency_p=*/true,
12704 /*is_declaration=*/false)
12706 token
= cp_lexer_peek_token (parser
->lexer
);
12707 /* If we have seen a nested-name-specifier, and the next token
12708 is `template', then we are using the template-id production. */
12710 && cp_parser_optional_template_keyword (parser
))
12712 /* Look for the template-id. */
12713 type
= cp_parser_template_id (parser
,
12714 /*template_keyword_p=*/true,
12715 /*check_dependency_p=*/true,
12716 /*is_declaration=*/false);
12717 /* If the template-id did not name a type, we are out of
12719 if (TREE_CODE (type
) != TYPE_DECL
)
12721 cp_parser_error (parser
, "expected template-id for type");
12725 /* Otherwise, look for a type-name. */
12727 type
= cp_parser_type_name (parser
);
12728 /* Keep track of all name-lookups performed in class scopes. */
12732 && TREE_CODE (type
) == TYPE_DECL
12733 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
12734 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
12735 /* If it didn't work out, we don't have a TYPE. */
12736 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
12737 && !cp_parser_parse_definitely (parser
))
12739 if (type
&& decl_specs
)
12740 cp_parser_set_decl_spec_type (decl_specs
, type
,
12742 /*user_defined=*/true);
12745 /* If we didn't get a type-name, issue an error message. */
12746 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
12748 cp_parser_error (parser
, "expected type-name");
12749 return error_mark_node
;
12752 if (type
&& type
!= error_mark_node
)
12754 /* See if TYPE is an Objective-C type, and if so, parse and
12755 accept any protocol references following it. Do this before
12756 the cp_parser_check_for_invalid_template_id() call, because
12757 Objective-C types can be followed by '<...>' which would
12758 enclose protocol names rather than template arguments, and so
12759 everything is fine. */
12760 if (c_dialect_objc () && !parser
->scope
12761 && (objc_is_id (type
) || objc_is_class_name (type
)))
12763 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
12764 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
12766 /* Clobber the "unqualified" type previously entered into
12767 DECL_SPECS with the new, improved protocol-qualified version. */
12769 decl_specs
->type
= qual_type
;
12774 /* There is no valid C++ program where a non-template type is
12775 followed by a "<". That usually indicates that the user
12776 thought that the type was a template. */
12777 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
12784 /* Parse a type-name.
12797 Returns a TYPE_DECL for the type. */
12800 cp_parser_type_name (cp_parser
* parser
)
12804 /* We can't know yet whether it is a class-name or not. */
12805 cp_parser_parse_tentatively (parser
);
12806 /* Try a class-name. */
12807 type_decl
= cp_parser_class_name (parser
,
12808 /*typename_keyword_p=*/false,
12809 /*template_keyword_p=*/false,
12811 /*check_dependency_p=*/true,
12812 /*class_head_p=*/false,
12813 /*is_declaration=*/false);
12814 /* If it's not a class-name, keep looking. */
12815 if (!cp_parser_parse_definitely (parser
))
12817 /* It must be a typedef-name or an enum-name. */
12818 return cp_parser_nonclass_name (parser
);
12824 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12832 Returns a TYPE_DECL for the type. */
12835 cp_parser_nonclass_name (cp_parser
* parser
)
12840 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12841 identifier
= cp_parser_identifier (parser
);
12842 if (identifier
== error_mark_node
)
12843 return error_mark_node
;
12845 /* Look up the type-name. */
12846 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
12848 if (TREE_CODE (type_decl
) != TYPE_DECL
12849 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
12851 /* See if this is an Objective-C type. */
12852 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
12853 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
12855 type_decl
= TYPE_NAME (type
);
12858 /* Issue an error if we did not find a type-name. */
12859 if (TREE_CODE (type_decl
) != TYPE_DECL
12860 /* In Objective-C, we have the complication that class names are
12861 normally type names and start declarations (eg, the
12862 "NSObject" in "NSObject *object;"), but can be used in an
12863 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12864 is an expression. So, a classname followed by a dot is not a
12865 valid type-name. */
12866 || (objc_is_class_name (TREE_TYPE (type_decl
))
12867 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
12869 if (!cp_parser_simulate_error (parser
))
12870 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
12871 NLE_TYPE
, token
->location
);
12872 return error_mark_node
;
12874 /* Remember that the name was used in the definition of the
12875 current class so that we can check later to see if the
12876 meaning would have been different after the class was
12877 entirely defined. */
12878 else if (type_decl
!= error_mark_node
12880 maybe_note_name_used_in_class (identifier
, type_decl
);
12885 /* Parse an elaborated-type-specifier. Note that the grammar given
12886 here incorporates the resolution to DR68.
12888 elaborated-type-specifier:
12889 class-key :: [opt] nested-name-specifier [opt] identifier
12890 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12891 enum-key :: [opt] nested-name-specifier [opt] identifier
12892 typename :: [opt] nested-name-specifier identifier
12893 typename :: [opt] nested-name-specifier template [opt]
12898 elaborated-type-specifier:
12899 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12900 class-key attributes :: [opt] nested-name-specifier [opt]
12901 template [opt] template-id
12902 enum attributes :: [opt] nested-name-specifier [opt] identifier
12904 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12905 declared `friend'. If IS_DECLARATION is TRUE, then this
12906 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12907 something is being declared.
12909 Returns the TYPE specified. */
12912 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
12914 bool is_declaration
)
12916 enum tag_types tag_type
;
12918 tree type
= NULL_TREE
;
12919 tree attributes
= NULL_TREE
;
12921 cp_token
*token
= NULL
;
12923 /* See if we're looking at the `enum' keyword. */
12924 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
12926 /* Consume the `enum' token. */
12927 cp_lexer_consume_token (parser
->lexer
);
12928 /* Remember that it's an enumeration type. */
12929 tag_type
= enum_type
;
12930 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12931 enums) is used here. */
12932 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
12933 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
12935 pedwarn (input_location
, 0, "elaborated-type-specifier "
12936 "for a scoped enum must not use the %<%D%> keyword",
12937 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
12938 /* Consume the `struct' or `class' and parse it anyway. */
12939 cp_lexer_consume_token (parser
->lexer
);
12941 /* Parse the attributes. */
12942 attributes
= cp_parser_attributes_opt (parser
);
12944 /* Or, it might be `typename'. */
12945 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
12948 /* Consume the `typename' token. */
12949 cp_lexer_consume_token (parser
->lexer
);
12950 /* Remember that it's a `typename' type. */
12951 tag_type
= typename_type
;
12953 /* Otherwise it must be a class-key. */
12956 tag_type
= cp_parser_class_key (parser
);
12957 if (tag_type
== none_type
)
12958 return error_mark_node
;
12959 /* Parse the attributes. */
12960 attributes
= cp_parser_attributes_opt (parser
);
12963 /* Look for the `::' operator. */
12964 globalscope
= cp_parser_global_scope_opt (parser
,
12965 /*current_scope_valid_p=*/false);
12966 /* Look for the nested-name-specifier. */
12967 if (tag_type
== typename_type
&& !globalscope
)
12969 if (!cp_parser_nested_name_specifier (parser
,
12970 /*typename_keyword_p=*/true,
12971 /*check_dependency_p=*/true,
12974 return error_mark_node
;
12977 /* Even though `typename' is not present, the proposed resolution
12978 to Core Issue 180 says that in `class A<T>::B', `B' should be
12979 considered a type-name, even if `A<T>' is dependent. */
12980 cp_parser_nested_name_specifier_opt (parser
,
12981 /*typename_keyword_p=*/true,
12982 /*check_dependency_p=*/true,
12985 /* For everything but enumeration types, consider a template-id.
12986 For an enumeration type, consider only a plain identifier. */
12987 if (tag_type
!= enum_type
)
12989 bool template_p
= false;
12992 /* Allow the `template' keyword. */
12993 template_p
= cp_parser_optional_template_keyword (parser
);
12994 /* If we didn't see `template', we don't know if there's a
12995 template-id or not. */
12997 cp_parser_parse_tentatively (parser
);
12998 /* Parse the template-id. */
12999 token
= cp_lexer_peek_token (parser
->lexer
);
13000 decl
= cp_parser_template_id (parser
, template_p
,
13001 /*check_dependency_p=*/true,
13003 /* If we didn't find a template-id, look for an ordinary
13005 if (!template_p
&& !cp_parser_parse_definitely (parser
))
13007 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13008 in effect, then we must assume that, upon instantiation, the
13009 template will correspond to a class. */
13010 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
13011 && tag_type
== typename_type
)
13012 type
= make_typename_type (parser
->scope
, decl
,
13014 /*complain=*/tf_error
);
13015 /* If the `typename' keyword is in effect and DECL is not a type
13016 decl. Then type is non existant. */
13017 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
13020 type
= TREE_TYPE (decl
);
13025 token
= cp_lexer_peek_token (parser
->lexer
);
13026 identifier
= cp_parser_identifier (parser
);
13028 if (identifier
== error_mark_node
)
13030 parser
->scope
= NULL_TREE
;
13031 return error_mark_node
;
13034 /* For a `typename', we needn't call xref_tag. */
13035 if (tag_type
== typename_type
13036 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
13037 return cp_parser_make_typename_type (parser
, parser
->scope
,
13040 /* Look up a qualified name in the usual way. */
13044 tree ambiguous_decls
;
13046 decl
= cp_parser_lookup_name (parser
, identifier
,
13048 /*is_template=*/false,
13049 /*is_namespace=*/false,
13050 /*check_dependency=*/true,
13054 /* If the lookup was ambiguous, an error will already have been
13056 if (ambiguous_decls
)
13057 return error_mark_node
;
13059 /* If we are parsing friend declaration, DECL may be a
13060 TEMPLATE_DECL tree node here. However, we need to check
13061 whether this TEMPLATE_DECL results in valid code. Consider
13062 the following example:
13065 template <class T> class C {};
13068 template <class T> friend class N::C; // #1, valid code
13070 template <class T> class Y {
13071 friend class N::C; // #2, invalid code
13074 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13075 name lookup of `N::C'. We see that friend declaration must
13076 be template for the code to be valid. Note that
13077 processing_template_decl does not work here since it is
13078 always 1 for the above two cases. */
13080 decl
= (cp_parser_maybe_treat_template_as_class
13081 (decl
, /*tag_name_p=*/is_friend
13082 && parser
->num_template_parameter_lists
));
13084 if (TREE_CODE (decl
) != TYPE_DECL
)
13086 cp_parser_diagnose_invalid_type_name (parser
,
13090 return error_mark_node
;
13093 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
13095 bool allow_template
= (parser
->num_template_parameter_lists
13096 || DECL_SELF_REFERENCE_P (decl
));
13097 type
= check_elaborated_type_specifier (tag_type
, decl
,
13100 if (type
== error_mark_node
)
13101 return error_mark_node
;
13104 /* Forward declarations of nested types, such as
13109 are invalid unless all components preceding the final '::'
13110 are complete. If all enclosing types are complete, these
13111 declarations become merely pointless.
13113 Invalid forward declarations of nested types are errors
13114 caught elsewhere in parsing. Those that are pointless arrive
13117 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
13118 && !is_friend
&& !processing_explicit_instantiation
)
13119 warning (0, "declaration %qD does not declare anything", decl
);
13121 type
= TREE_TYPE (decl
);
13125 /* An elaborated-type-specifier sometimes introduces a new type and
13126 sometimes names an existing type. Normally, the rule is that it
13127 introduces a new type only if there is not an existing type of
13128 the same name already in scope. For example, given:
13131 void f() { struct S s; }
13133 the `struct S' in the body of `f' is the same `struct S' as in
13134 the global scope; the existing definition is used. However, if
13135 there were no global declaration, this would introduce a new
13136 local class named `S'.
13138 An exception to this rule applies to the following code:
13140 namespace N { struct S; }
13142 Here, the elaborated-type-specifier names a new type
13143 unconditionally; even if there is already an `S' in the
13144 containing scope this declaration names a new type.
13145 This exception only applies if the elaborated-type-specifier
13146 forms the complete declaration:
13150 A declaration consisting solely of `class-key identifier ;' is
13151 either a redeclaration of the name in the current scope or a
13152 forward declaration of the identifier as a class name. It
13153 introduces the name into the current scope.
13155 We are in this situation precisely when the next token is a `;'.
13157 An exception to the exception is that a `friend' declaration does
13158 *not* name a new type; i.e., given:
13160 struct S { friend struct T; };
13162 `T' is not a new type in the scope of `S'.
13164 Also, `new struct S' or `sizeof (struct S)' never results in the
13165 definition of a new type; a new type can only be declared in a
13166 declaration context. */
13172 /* Friends have special name lookup rules. */
13173 ts
= ts_within_enclosing_non_class
;
13174 else if (is_declaration
13175 && cp_lexer_next_token_is (parser
->lexer
,
13177 /* This is a `class-key identifier ;' */
13183 (parser
->num_template_parameter_lists
13184 && (cp_parser_next_token_starts_class_definition_p (parser
)
13185 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
13186 /* An unqualified name was used to reference this type, so
13187 there were no qualifying templates. */
13188 if (!cp_parser_check_template_parameters (parser
,
13189 /*num_templates=*/0,
13191 /*declarator=*/NULL
))
13192 return error_mark_node
;
13193 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
13197 if (type
== error_mark_node
)
13198 return error_mark_node
;
13200 /* Allow attributes on forward declarations of classes. */
13203 if (TREE_CODE (type
) == TYPENAME_TYPE
)
13204 warning (OPT_Wattributes
,
13205 "attributes ignored on uninstantiated type");
13206 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
13207 && ! processing_explicit_instantiation
)
13208 warning (OPT_Wattributes
,
13209 "attributes ignored on template instantiation");
13210 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
13211 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
13213 warning (OPT_Wattributes
,
13214 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13217 if (tag_type
!= enum_type
)
13218 cp_parser_check_class_key (tag_type
, type
);
13220 /* A "<" cannot follow an elaborated type specifier. If that
13221 happens, the user was probably trying to form a template-id. */
13222 cp_parser_check_for_invalid_template_id (parser
, type
, token
->location
);
13227 /* Parse an enum-specifier.
13230 enum-head { enumerator-list [opt] }
13233 enum-key identifier [opt] enum-base [opt]
13234 enum-key nested-name-specifier identifier enum-base [opt]
13239 enum struct [C++0x]
13242 : type-specifier-seq
13244 opaque-enum-specifier:
13245 enum-key identifier enum-base [opt] ;
13248 enum-key attributes[opt] identifier [opt] enum-base [opt]
13249 { enumerator-list [opt] }attributes[opt]
13251 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13252 if the token stream isn't an enum-specifier after all. */
13255 cp_parser_enum_specifier (cp_parser
* parser
)
13258 tree type
= NULL_TREE
;
13260 tree nested_name_specifier
= NULL_TREE
;
13262 bool scoped_enum_p
= false;
13263 bool has_underlying_type
= false;
13264 bool nested_being_defined
= false;
13265 bool new_value_list
= false;
13266 bool is_new_type
= false;
13267 bool is_anonymous
= false;
13268 tree underlying_type
= NULL_TREE
;
13269 cp_token
*type_start_token
= NULL
;
13270 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
13272 parser
->colon_corrects_to_scope_p
= false;
13274 /* Parse tentatively so that we can back up if we don't find a
13276 cp_parser_parse_tentatively (parser
);
13278 /* Caller guarantees that the current token is 'enum', an identifier
13279 possibly follows, and the token after that is an opening brace.
13280 If we don't have an identifier, fabricate an anonymous name for
13281 the enumeration being defined. */
13282 cp_lexer_consume_token (parser
->lexer
);
13284 /* Parse the "class" or "struct", which indicates a scoped
13285 enumeration type in C++0x. */
13286 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
13287 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
13289 if (cxx_dialect
< cxx0x
)
13290 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
13292 /* Consume the `struct' or `class' token. */
13293 cp_lexer_consume_token (parser
->lexer
);
13295 scoped_enum_p
= true;
13298 attributes
= cp_parser_attributes_opt (parser
);
13300 /* Clear the qualification. */
13301 parser
->scope
= NULL_TREE
;
13302 parser
->qualifying_scope
= NULL_TREE
;
13303 parser
->object_scope
= NULL_TREE
;
13305 /* Figure out in what scope the declaration is being placed. */
13306 prev_scope
= current_scope ();
13308 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
13310 push_deferring_access_checks (dk_no_check
);
13311 nested_name_specifier
13312 = cp_parser_nested_name_specifier_opt (parser
,
13313 /*typename_keyword_p=*/true,
13314 /*check_dependency_p=*/false,
13316 /*is_declaration=*/false);
13318 if (nested_name_specifier
)
13322 identifier
= cp_parser_identifier (parser
);
13323 name
= cp_parser_lookup_name (parser
, identifier
,
13325 /*is_template=*/false,
13326 /*is_namespace=*/false,
13327 /*check_dependency=*/true,
13328 /*ambiguous_decls=*/NULL
,
13332 type
= TREE_TYPE (name
);
13333 if (TREE_CODE (type
) == TYPENAME_TYPE
)
13335 /* Are template enums allowed in ISO? */
13336 if (template_parm_scope_p ())
13337 pedwarn (type_start_token
->location
, OPT_pedantic
,
13338 "%qD is an enumeration template", name
);
13339 /* ignore a typename reference, for it will be solved by name
13345 error_at (type_start_token
->location
,
13346 "%qD is not an enumerator-name", identifier
);
13350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13351 identifier
= cp_parser_identifier (parser
);
13354 identifier
= make_anon_name ();
13355 is_anonymous
= true;
13358 pop_deferring_access_checks ();
13360 /* Check for the `:' that denotes a specified underlying type in C++0x.
13361 Note that a ':' could also indicate a bitfield width, however. */
13362 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13364 cp_decl_specifier_seq type_specifiers
;
13366 /* Consume the `:'. */
13367 cp_lexer_consume_token (parser
->lexer
);
13369 /* Parse the type-specifier-seq. */
13370 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
13371 /*is_trailing_return=*/false,
13374 /* At this point this is surely not elaborated type specifier. */
13375 if (!cp_parser_parse_definitely (parser
))
13378 if (cxx_dialect
< cxx0x
)
13379 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
13381 has_underlying_type
= true;
13383 /* If that didn't work, stop. */
13384 if (type_specifiers
.type
!= error_mark_node
)
13386 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
13387 /*initialized=*/0, NULL
);
13388 if (underlying_type
== error_mark_node
)
13389 underlying_type
= NULL_TREE
;
13393 /* Look for the `{' but don't consume it yet. */
13394 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13396 if (cxx_dialect
< cxx0x
|| (!scoped_enum_p
&& !underlying_type
))
13398 cp_parser_error (parser
, "expected %<{%>");
13399 if (has_underlying_type
)
13405 /* An opaque-enum-specifier must have a ';' here. */
13406 if ((scoped_enum_p
|| underlying_type
)
13407 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13409 cp_parser_error (parser
, "expected %<;%> or %<{%>");
13410 if (has_underlying_type
)
13418 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
13421 if (nested_name_specifier
)
13423 if (CLASS_TYPE_P (nested_name_specifier
))
13425 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
13426 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
13427 push_scope (nested_name_specifier
);
13429 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
13431 push_nested_namespace (nested_name_specifier
);
13435 /* Issue an error message if type-definitions are forbidden here. */
13436 if (!cp_parser_check_type_definition (parser
))
13437 type
= error_mark_node
;
13439 /* Create the new type. We do this before consuming the opening
13440 brace so the enum will be recorded as being on the line of its
13441 tag (or the 'enum' keyword, if there is no tag). */
13442 type
= start_enum (identifier
, type
, underlying_type
,
13443 scoped_enum_p
, &is_new_type
);
13445 /* If the next token is not '{' it is an opaque-enum-specifier or an
13446 elaborated-type-specifier. */
13447 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13449 if (nested_name_specifier
)
13451 /* The following catches invalid code such as:
13452 enum class S<int>::E { A, B, C }; */
13453 if (!processing_specialization
13454 && CLASS_TYPE_P (nested_name_specifier
)
13455 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
13456 error_at (type_start_token
->location
, "cannot add an enumerator "
13457 "list to a template instantiation");
13459 /* If that scope does not contain the scope in which the
13460 class was originally declared, the program is invalid. */
13461 if (prev_scope
&& !is_ancestor (prev_scope
, nested_name_specifier
))
13463 if (at_namespace_scope_p ())
13464 error_at (type_start_token
->location
,
13465 "declaration of %qD in namespace %qD which does not "
13467 type
, prev_scope
, nested_name_specifier
);
13469 error_at (type_start_token
->location
,
13470 "declaration of %qD in %qD which does not enclose %qD",
13471 type
, prev_scope
, nested_name_specifier
);
13472 type
= error_mark_node
;
13477 begin_scope (sk_scoped_enum
, type
);
13479 /* Consume the opening brace. */
13480 cp_lexer_consume_token (parser
->lexer
);
13482 if (type
== error_mark_node
)
13483 ; /* Nothing to add */
13484 else if (OPAQUE_ENUM_P (type
)
13485 || (cxx_dialect
> cxx98
&& processing_specialization
))
13487 new_value_list
= true;
13488 SET_OPAQUE_ENUM_P (type
, false);
13489 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
13493 error_at (type_start_token
->location
, "multiple definition of %q#T", type
);
13494 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
13495 "previous definition here");
13496 type
= error_mark_node
;
13499 if (type
== error_mark_node
)
13500 cp_parser_skip_to_end_of_block_or_statement (parser
);
13501 /* If the next token is not '}', then there are some enumerators. */
13502 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
13503 cp_parser_enumerator_list (parser
, type
);
13505 /* Consume the final '}'. */
13506 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
13513 /* If a ';' follows, then it is an opaque-enum-specifier
13514 and additional restrictions apply. */
13515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13518 error_at (type_start_token
->location
,
13519 "opaque-enum-specifier without name");
13520 else if (nested_name_specifier
)
13521 error_at (type_start_token
->location
,
13522 "opaque-enum-specifier must use a simple identifier");
13526 /* Look for trailing attributes to apply to this enumeration, and
13527 apply them if appropriate. */
13528 if (cp_parser_allow_gnu_extensions_p (parser
))
13530 tree trailing_attr
= cp_parser_attributes_opt (parser
);
13531 trailing_attr
= chainon (trailing_attr
, attributes
);
13532 cplus_decl_attributes (&type
,
13534 (int) ATTR_FLAG_TYPE_IN_PLACE
);
13537 /* Finish up the enumeration. */
13538 if (type
!= error_mark_node
)
13540 if (new_value_list
)
13541 finish_enum_value_list (type
);
13543 finish_enum (type
);
13546 if (nested_name_specifier
)
13548 if (CLASS_TYPE_P (nested_name_specifier
))
13550 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
13551 pop_scope (nested_name_specifier
);
13553 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
13555 pop_nested_namespace (nested_name_specifier
);
13559 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
13563 /* Parse an enumerator-list. The enumerators all have the indicated
13567 enumerator-definition
13568 enumerator-list , enumerator-definition */
13571 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
13575 /* Parse an enumerator-definition. */
13576 cp_parser_enumerator_definition (parser
, type
);
13578 /* If the next token is not a ',', we've reached the end of
13580 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13582 /* Otherwise, consume the `,' and keep going. */
13583 cp_lexer_consume_token (parser
->lexer
);
13584 /* If the next token is a `}', there is a trailing comma. */
13585 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
13587 if (!in_system_header
)
13588 pedwarn (input_location
, OPT_pedantic
, "comma at end of enumerator list");
13594 /* Parse an enumerator-definition. The enumerator has the indicated
13597 enumerator-definition:
13599 enumerator = constant-expression
13605 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
13611 /* Save the input location because we are interested in the location
13612 of the identifier and not the location of the explicit value. */
13613 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13615 /* Look for the identifier. */
13616 identifier
= cp_parser_identifier (parser
);
13617 if (identifier
== error_mark_node
)
13620 /* If the next token is an '=', then there is an explicit value. */
13621 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13623 /* Consume the `=' token. */
13624 cp_lexer_consume_token (parser
->lexer
);
13625 /* Parse the value. */
13626 value
= cp_parser_constant_expression (parser
,
13627 /*allow_non_constant_p=*/false,
13633 /* If we are processing a template, make sure the initializer of the
13634 enumerator doesn't contain any bare template parameter pack. */
13635 if (check_for_bare_parameter_packs (value
))
13636 value
= error_mark_node
;
13638 /* integral_constant_value will pull out this expression, so make sure
13639 it's folded as appropriate. */
13640 value
= fold_non_dependent_expr (value
);
13642 /* Create the enumerator. */
13643 build_enumerator (identifier
, value
, type
, loc
);
13646 /* Parse a namespace-name.
13649 original-namespace-name
13652 Returns the NAMESPACE_DECL for the namespace. */
13655 cp_parser_namespace_name (cp_parser
* parser
)
13658 tree namespace_decl
;
13660 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13662 /* Get the name of the namespace. */
13663 identifier
= cp_parser_identifier (parser
);
13664 if (identifier
== error_mark_node
)
13665 return error_mark_node
;
13667 /* Look up the identifier in the currently active scope. Look only
13668 for namespaces, due to:
13670 [basic.lookup.udir]
13672 When looking up a namespace-name in a using-directive or alias
13673 definition, only namespace names are considered.
13677 [basic.lookup.qual]
13679 During the lookup of a name preceding the :: scope resolution
13680 operator, object, function, and enumerator names are ignored.
13682 (Note that cp_parser_qualifying_entity only calls this
13683 function if the token after the name is the scope resolution
13685 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
13687 /*is_template=*/false,
13688 /*is_namespace=*/true,
13689 /*check_dependency=*/true,
13690 /*ambiguous_decls=*/NULL
,
13692 /* If it's not a namespace, issue an error. */
13693 if (namespace_decl
== error_mark_node
13694 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
13696 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
13697 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
13698 cp_parser_error (parser
, "expected namespace-name");
13699 namespace_decl
= error_mark_node
;
13702 return namespace_decl
;
13705 /* Parse a namespace-definition.
13707 namespace-definition:
13708 named-namespace-definition
13709 unnamed-namespace-definition
13711 named-namespace-definition:
13712 original-namespace-definition
13713 extension-namespace-definition
13715 original-namespace-definition:
13716 namespace identifier { namespace-body }
13718 extension-namespace-definition:
13719 namespace original-namespace-name { namespace-body }
13721 unnamed-namespace-definition:
13722 namespace { namespace-body } */
13725 cp_parser_namespace_definition (cp_parser
* parser
)
13727 tree identifier
, attribs
;
13728 bool has_visibility
;
13731 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
13733 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
13735 cp_lexer_consume_token (parser
->lexer
);
13740 /* Look for the `namespace' keyword. */
13741 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
13743 /* Get the name of the namespace. We do not attempt to distinguish
13744 between an original-namespace-definition and an
13745 extension-namespace-definition at this point. The semantic
13746 analysis routines are responsible for that. */
13747 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13748 identifier
= cp_parser_identifier (parser
);
13750 identifier
= NULL_TREE
;
13752 /* Parse any specified attributes. */
13753 attribs
= cp_parser_attributes_opt (parser
);
13755 /* Look for the `{' to start the namespace. */
13756 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
13757 /* Start the namespace. */
13758 push_namespace (identifier
);
13760 /* "inline namespace" is equivalent to a stub namespace definition
13761 followed by a strong using directive. */
13764 tree name_space
= current_namespace
;
13765 /* Set up namespace association. */
13766 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
13767 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
13768 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
13769 /* Import the contents of the inline namespace. */
13771 do_using_directive (name_space
);
13772 push_namespace (identifier
);
13775 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
13777 /* Parse the body of the namespace. */
13778 cp_parser_namespace_body (parser
);
13780 if (has_visibility
)
13781 pop_visibility (1);
13783 /* Finish the namespace. */
13785 /* Look for the final `}'. */
13786 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
13789 /* Parse a namespace-body.
13792 declaration-seq [opt] */
13795 cp_parser_namespace_body (cp_parser
* parser
)
13797 cp_parser_declaration_seq_opt (parser
);
13800 /* Parse a namespace-alias-definition.
13802 namespace-alias-definition:
13803 namespace identifier = qualified-namespace-specifier ; */
13806 cp_parser_namespace_alias_definition (cp_parser
* parser
)
13809 tree namespace_specifier
;
13811 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13813 /* Look for the `namespace' keyword. */
13814 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
13815 /* Look for the identifier. */
13816 identifier
= cp_parser_identifier (parser
);
13817 if (identifier
== error_mark_node
)
13819 /* Look for the `=' token. */
13820 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
13821 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13823 error_at (token
->location
, "%<namespace%> definition is not allowed here");
13824 /* Skip the definition. */
13825 cp_lexer_consume_token (parser
->lexer
);
13826 if (cp_parser_skip_to_closing_brace (parser
))
13827 cp_lexer_consume_token (parser
->lexer
);
13830 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
13831 /* Look for the qualified-namespace-specifier. */
13832 namespace_specifier
13833 = cp_parser_qualified_namespace_specifier (parser
);
13834 /* Look for the `;' token. */
13835 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13837 /* Register the alias in the symbol table. */
13838 do_namespace_alias (identifier
, namespace_specifier
);
13841 /* Parse a qualified-namespace-specifier.
13843 qualified-namespace-specifier:
13844 :: [opt] nested-name-specifier [opt] namespace-name
13846 Returns a NAMESPACE_DECL corresponding to the specified
13850 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
13852 /* Look for the optional `::'. */
13853 cp_parser_global_scope_opt (parser
,
13854 /*current_scope_valid_p=*/false);
13856 /* Look for the optional nested-name-specifier. */
13857 cp_parser_nested_name_specifier_opt (parser
,
13858 /*typename_keyword_p=*/false,
13859 /*check_dependency_p=*/true,
13861 /*is_declaration=*/true);
13863 return cp_parser_namespace_name (parser
);
13866 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13867 access declaration.
13870 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13871 using :: unqualified-id ;
13873 access-declaration:
13879 cp_parser_using_declaration (cp_parser
* parser
,
13880 bool access_declaration_p
)
13883 bool typename_p
= false;
13884 bool global_scope_p
;
13889 if (access_declaration_p
)
13890 cp_parser_parse_tentatively (parser
);
13893 /* Look for the `using' keyword. */
13894 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
13896 /* Peek at the next token. */
13897 token
= cp_lexer_peek_token (parser
->lexer
);
13898 /* See if it's `typename'. */
13899 if (token
->keyword
== RID_TYPENAME
)
13901 /* Remember that we've seen it. */
13903 /* Consume the `typename' token. */
13904 cp_lexer_consume_token (parser
->lexer
);
13908 /* Look for the optional global scope qualification. */
13910 = (cp_parser_global_scope_opt (parser
,
13911 /*current_scope_valid_p=*/false)
13914 /* If we saw `typename', or didn't see `::', then there must be a
13915 nested-name-specifier present. */
13916 if (typename_p
|| !global_scope_p
)
13917 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
13918 /*check_dependency_p=*/true,
13920 /*is_declaration=*/true);
13921 /* Otherwise, we could be in either of the two productions. In that
13922 case, treat the nested-name-specifier as optional. */
13924 qscope
= cp_parser_nested_name_specifier_opt (parser
,
13925 /*typename_keyword_p=*/false,
13926 /*check_dependency_p=*/true,
13928 /*is_declaration=*/true);
13930 qscope
= global_namespace
;
13932 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
13933 /* Something has already gone wrong; there's no need to parse
13934 further. Since an error has occurred, the return value of
13935 cp_parser_parse_definitely will be false, as required. */
13936 return cp_parser_parse_definitely (parser
);
13938 token
= cp_lexer_peek_token (parser
->lexer
);
13939 /* Parse the unqualified-id. */
13940 identifier
= cp_parser_unqualified_id (parser
,
13941 /*template_keyword_p=*/false,
13942 /*check_dependency_p=*/true,
13943 /*declarator_p=*/true,
13944 /*optional_p=*/false);
13946 if (access_declaration_p
)
13948 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13949 cp_parser_simulate_error (parser
);
13950 if (!cp_parser_parse_definitely (parser
))
13954 /* The function we call to handle a using-declaration is different
13955 depending on what scope we are in. */
13956 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
13958 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
13959 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
13960 /* [namespace.udecl]
13962 A using declaration shall not name a template-id. */
13963 error_at (token
->location
,
13964 "a template-id may not appear in a using-declaration");
13967 if (at_class_scope_p ())
13969 /* Create the USING_DECL. */
13970 decl
= do_class_using_decl (parser
->scope
, identifier
);
13972 if (check_for_bare_parameter_packs (decl
))
13975 /* Add it to the list of members in this class. */
13976 finish_member_declaration (decl
);
13980 decl
= cp_parser_lookup_name_simple (parser
,
13983 if (decl
== error_mark_node
)
13984 cp_parser_name_lookup_error (parser
, identifier
,
13987 else if (check_for_bare_parameter_packs (decl
))
13989 else if (!at_namespace_scope_p ())
13990 do_local_using_decl (decl
, qscope
, identifier
);
13992 do_toplevel_using_decl (decl
, qscope
, identifier
);
13996 /* Look for the final `;'. */
13997 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14002 /* Parse a using-directive.
14005 using namespace :: [opt] nested-name-specifier [opt]
14006 namespace-name ; */
14009 cp_parser_using_directive (cp_parser
* parser
)
14011 tree namespace_decl
;
14014 /* Look for the `using' keyword. */
14015 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
14016 /* And the `namespace' keyword. */
14017 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
14018 /* Look for the optional `::' operator. */
14019 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
14020 /* And the optional nested-name-specifier. */
14021 cp_parser_nested_name_specifier_opt (parser
,
14022 /*typename_keyword_p=*/false,
14023 /*check_dependency_p=*/true,
14025 /*is_declaration=*/true);
14026 /* Get the namespace being used. */
14027 namespace_decl
= cp_parser_namespace_name (parser
);
14028 /* And any specified attributes. */
14029 attribs
= cp_parser_attributes_opt (parser
);
14030 /* Update the symbol table. */
14031 parse_using_directive (namespace_decl
, attribs
);
14032 /* Look for the final `;'. */
14033 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14036 /* Parse an asm-definition.
14039 asm ( string-literal ) ;
14044 asm volatile [opt] ( string-literal ) ;
14045 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14046 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14047 : asm-operand-list [opt] ) ;
14048 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14049 : asm-operand-list [opt]
14050 : asm-clobber-list [opt] ) ;
14051 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14052 : asm-clobber-list [opt]
14053 : asm-goto-list ) ; */
14056 cp_parser_asm_definition (cp_parser
* parser
)
14059 tree outputs
= NULL_TREE
;
14060 tree inputs
= NULL_TREE
;
14061 tree clobbers
= NULL_TREE
;
14062 tree labels
= NULL_TREE
;
14064 bool volatile_p
= false;
14065 bool extended_p
= false;
14066 bool invalid_inputs_p
= false;
14067 bool invalid_outputs_p
= false;
14068 bool goto_p
= false;
14069 required_token missing
= RT_NONE
;
14071 /* Look for the `asm' keyword. */
14072 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
14073 /* See if the next token is `volatile'. */
14074 if (cp_parser_allow_gnu_extensions_p (parser
)
14075 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
14077 /* Remember that we saw the `volatile' keyword. */
14079 /* Consume the token. */
14080 cp_lexer_consume_token (parser
->lexer
);
14082 if (cp_parser_allow_gnu_extensions_p (parser
)
14083 && parser
->in_function_body
14084 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
14086 /* Remember that we saw the `goto' keyword. */
14088 /* Consume the token. */
14089 cp_lexer_consume_token (parser
->lexer
);
14091 /* Look for the opening `('. */
14092 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
14094 /* Look for the string. */
14095 string
= cp_parser_string_literal (parser
, false, false);
14096 if (string
== error_mark_node
)
14098 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14099 /*consume_paren=*/true);
14103 /* If we're allowing GNU extensions, check for the extended assembly
14104 syntax. Unfortunately, the `:' tokens need not be separated by
14105 a space in C, and so, for compatibility, we tolerate that here
14106 too. Doing that means that we have to treat the `::' operator as
14108 if (cp_parser_allow_gnu_extensions_p (parser
)
14109 && parser
->in_function_body
14110 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
14111 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
14113 bool inputs_p
= false;
14114 bool clobbers_p
= false;
14115 bool labels_p
= false;
14117 /* The extended syntax was used. */
14120 /* Look for outputs. */
14121 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
14123 /* Consume the `:'. */
14124 cp_lexer_consume_token (parser
->lexer
);
14125 /* Parse the output-operands. */
14126 if (cp_lexer_next_token_is_not (parser
->lexer
,
14128 && cp_lexer_next_token_is_not (parser
->lexer
,
14130 && cp_lexer_next_token_is_not (parser
->lexer
,
14133 outputs
= cp_parser_asm_operand_list (parser
);
14135 if (outputs
== error_mark_node
)
14136 invalid_outputs_p
= true;
14138 /* If the next token is `::', there are no outputs, and the
14139 next token is the beginning of the inputs. */
14140 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
14141 /* The inputs are coming next. */
14144 /* Look for inputs. */
14146 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
14148 /* Consume the `:' or `::'. */
14149 cp_lexer_consume_token (parser
->lexer
);
14150 /* Parse the output-operands. */
14151 if (cp_lexer_next_token_is_not (parser
->lexer
,
14153 && cp_lexer_next_token_is_not (parser
->lexer
,
14155 && cp_lexer_next_token_is_not (parser
->lexer
,
14157 inputs
= cp_parser_asm_operand_list (parser
);
14159 if (inputs
== error_mark_node
)
14160 invalid_inputs_p
= true;
14162 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
14163 /* The clobbers are coming next. */
14166 /* Look for clobbers. */
14168 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
14171 /* Consume the `:' or `::'. */
14172 cp_lexer_consume_token (parser
->lexer
);
14173 /* Parse the clobbers. */
14174 if (cp_lexer_next_token_is_not (parser
->lexer
,
14176 && cp_lexer_next_token_is_not (parser
->lexer
,
14178 clobbers
= cp_parser_asm_clobber_list (parser
);
14181 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
14182 /* The labels are coming next. */
14185 /* Look for labels. */
14187 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
14190 /* Consume the `:' or `::'. */
14191 cp_lexer_consume_token (parser
->lexer
);
14192 /* Parse the labels. */
14193 labels
= cp_parser_asm_label_list (parser
);
14196 if (goto_p
&& !labels_p
)
14197 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
14200 missing
= RT_COLON_SCOPE
;
14202 /* Look for the closing `)'. */
14203 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
14204 missing
? missing
: RT_CLOSE_PAREN
))
14205 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14206 /*consume_paren=*/true);
14207 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14209 if (!invalid_inputs_p
&& !invalid_outputs_p
)
14211 /* Create the ASM_EXPR. */
14212 if (parser
->in_function_body
)
14214 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
14215 inputs
, clobbers
, labels
);
14216 /* If the extended syntax was not used, mark the ASM_EXPR. */
14219 tree temp
= asm_stmt
;
14220 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
14221 temp
= TREE_OPERAND (temp
, 0);
14223 ASM_INPUT_P (temp
) = 1;
14227 cgraph_add_asm_node (string
);
14231 /* Declarators [gram.dcl.decl] */
14233 /* Parse an init-declarator.
14236 declarator initializer [opt]
14241 declarator asm-specification [opt] attributes [opt] initializer [opt]
14243 function-definition:
14244 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14246 decl-specifier-seq [opt] declarator function-try-block
14250 function-definition:
14251 __extension__ function-definition
14253 The DECL_SPECIFIERS apply to this declarator. Returns a
14254 representation of the entity declared. If MEMBER_P is TRUE, then
14255 this declarator appears in a class scope. The new DECL created by
14256 this declarator is returned.
14258 The CHECKS are access checks that should be performed once we know
14259 what entity is being declared (and, therefore, what classes have
14262 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14263 for a function-definition here as well. If the declarator is a
14264 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14265 be TRUE upon return. By that point, the function-definition will
14266 have been completely parsed.
14268 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14271 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14272 parsed declaration if it is an uninitialized single declarator not followed
14273 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14274 if present, will not be consumed. If returned, this declarator will be
14275 created with SD_INITIALIZED but will not call cp_finish_decl. */
14278 cp_parser_init_declarator (cp_parser
* parser
,
14279 cp_decl_specifier_seq
*decl_specifiers
,
14280 VEC (deferred_access_check
,gc
)* checks
,
14281 bool function_definition_allowed_p
,
14283 int declares_class_or_enum
,
14284 bool* function_definition_p
,
14285 tree
* maybe_range_for_decl
)
14287 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
14288 *attributes_start_token
= NULL
;
14289 cp_declarator
*declarator
;
14290 tree prefix_attributes
;
14292 tree asm_specification
;
14294 tree decl
= NULL_TREE
;
14296 int is_initialized
;
14297 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14298 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14300 enum cpp_ttype initialization_kind
;
14301 bool is_direct_init
= false;
14302 bool is_non_constant_init
;
14303 int ctor_dtor_or_conv_p
;
14305 tree pushed_scope
= NULL
;
14306 bool range_for_decl_p
= false;
14308 /* Gather the attributes that were provided with the
14309 decl-specifiers. */
14310 prefix_attributes
= decl_specifiers
->attributes
;
14312 /* Assume that this is not the declarator for a function
14314 if (function_definition_p
)
14315 *function_definition_p
= false;
14317 /* Defer access checks while parsing the declarator; we cannot know
14318 what names are accessible until we know what is being
14320 resume_deferring_access_checks ();
14322 /* Parse the declarator. */
14323 token
= cp_lexer_peek_token (parser
->lexer
);
14325 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14326 &ctor_dtor_or_conv_p
,
14327 /*parenthesized_p=*/NULL
,
14328 /*member_p=*/false);
14329 /* Gather up the deferred checks. */
14330 stop_deferring_access_checks ();
14332 /* If the DECLARATOR was erroneous, there's no need to go
14334 if (declarator
== cp_error_declarator
)
14335 return error_mark_node
;
14337 /* Check that the number of template-parameter-lists is OK. */
14338 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
14340 return error_mark_node
;
14342 if (declares_class_or_enum
& 2)
14343 cp_parser_check_for_definition_in_return_type (declarator
,
14344 decl_specifiers
->type
,
14345 decl_specifiers
->type_location
);
14347 /* Figure out what scope the entity declared by the DECLARATOR is
14348 located in. `grokdeclarator' sometimes changes the scope, so
14349 we compute it now. */
14350 scope
= get_scope_of_declarator (declarator
);
14352 /* Perform any lookups in the declared type which were thought to be
14353 dependent, but are not in the scope of the declarator. */
14354 decl_specifiers
->type
14355 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
14357 /* If we're allowing GNU extensions, look for an asm-specification
14359 if (cp_parser_allow_gnu_extensions_p (parser
))
14361 /* Look for an asm-specification. */
14362 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
14363 asm_specification
= cp_parser_asm_specification_opt (parser
);
14364 /* And attributes. */
14365 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
14366 attributes
= cp_parser_attributes_opt (parser
);
14370 asm_specification
= NULL_TREE
;
14371 attributes
= NULL_TREE
;
14374 /* Peek at the next token. */
14375 token
= cp_lexer_peek_token (parser
->lexer
);
14376 /* Check to see if the token indicates the start of a
14377 function-definition. */
14378 if (function_declarator_p (declarator
)
14379 && cp_parser_token_starts_function_definition_p (token
))
14381 if (!function_definition_allowed_p
)
14383 /* If a function-definition should not appear here, issue an
14385 cp_parser_error (parser
,
14386 "a function-definition is not allowed here");
14387 return error_mark_node
;
14391 location_t func_brace_location
14392 = cp_lexer_peek_token (parser
->lexer
)->location
;
14394 /* Neither attributes nor an asm-specification are allowed
14395 on a function-definition. */
14396 if (asm_specification
)
14397 error_at (asm_spec_start_token
->location
,
14398 "an asm-specification is not allowed "
14399 "on a function-definition");
14401 error_at (attributes_start_token
->location
,
14402 "attributes are not allowed on a function-definition");
14403 /* This is a function-definition. */
14404 *function_definition_p
= true;
14406 /* Parse the function definition. */
14408 decl
= cp_parser_save_member_function_body (parser
,
14411 prefix_attributes
);
14414 = (cp_parser_function_definition_from_specifiers_and_declarator
14415 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
14417 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
14419 /* This is where the prologue starts... */
14420 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
14421 = func_brace_location
;
14430 Only in function declarations for constructors, destructors, and
14431 type conversions can the decl-specifier-seq be omitted.
14433 We explicitly postpone this check past the point where we handle
14434 function-definitions because we tolerate function-definitions
14435 that are missing their return types in some modes. */
14436 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
14438 cp_parser_error (parser
,
14439 "expected constructor, destructor, or type conversion");
14440 return error_mark_node
;
14443 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14444 if (token
->type
== CPP_EQ
14445 || token
->type
== CPP_OPEN_PAREN
14446 || token
->type
== CPP_OPEN_BRACE
)
14448 is_initialized
= SD_INITIALIZED
;
14449 initialization_kind
= token
->type
;
14450 if (maybe_range_for_decl
)
14451 *maybe_range_for_decl
= error_mark_node
;
14453 if (token
->type
== CPP_EQ
14454 && function_declarator_p (declarator
))
14456 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
14457 if (t2
->keyword
== RID_DEFAULT
)
14458 is_initialized
= SD_DEFAULTED
;
14459 else if (t2
->keyword
== RID_DELETE
)
14460 is_initialized
= SD_DELETED
;
14465 /* If the init-declarator isn't initialized and isn't followed by a
14466 `,' or `;', it's not a valid init-declarator. */
14467 if (token
->type
!= CPP_COMMA
14468 && token
->type
!= CPP_SEMICOLON
)
14470 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
14471 range_for_decl_p
= true;
14474 cp_parser_error (parser
, "expected initializer");
14475 return error_mark_node
;
14478 is_initialized
= SD_UNINITIALIZED
;
14479 initialization_kind
= CPP_EOF
;
14482 /* Because start_decl has side-effects, we should only call it if we
14483 know we're going ahead. By this point, we know that we cannot
14484 possibly be looking at any other construct. */
14485 cp_parser_commit_to_tentative_parse (parser
);
14487 /* If the decl specifiers were bad, issue an error now that we're
14488 sure this was intended to be a declarator. Then continue
14489 declaring the variable(s), as int, to try to cut down on further
14491 if (decl_specifiers
->any_specifiers_p
14492 && decl_specifiers
->type
== error_mark_node
)
14494 cp_parser_error (parser
, "invalid type in declaration");
14495 decl_specifiers
->type
= integer_type_node
;
14498 /* Check to see whether or not this declaration is a friend. */
14499 friend_p
= cp_parser_friend_p (decl_specifiers
);
14501 /* Enter the newly declared entry in the symbol table. If we're
14502 processing a declaration in a class-specifier, we wait until
14503 after processing the initializer. */
14506 if (parser
->in_unbraced_linkage_specification_p
)
14507 decl_specifiers
->storage_class
= sc_extern
;
14508 decl
= start_decl (declarator
, decl_specifiers
,
14509 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
14510 attributes
, prefix_attributes
,
14512 /* Adjust location of decl if declarator->id_loc is more appropriate:
14513 set, and decl wasn't merged with another decl, in which case its
14514 location would be different from input_location, and more accurate. */
14516 && declarator
->id_loc
!= UNKNOWN_LOCATION
14517 && DECL_SOURCE_LOCATION (decl
) == input_location
)
14518 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
14521 /* Enter the SCOPE. That way unqualified names appearing in the
14522 initializer will be looked up in SCOPE. */
14523 pushed_scope
= push_scope (scope
);
14525 /* Perform deferred access control checks, now that we know in which
14526 SCOPE the declared entity resides. */
14527 if (!member_p
&& decl
)
14529 tree saved_current_function_decl
= NULL_TREE
;
14531 /* If the entity being declared is a function, pretend that we
14532 are in its scope. If it is a `friend', it may have access to
14533 things that would not otherwise be accessible. */
14534 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14536 saved_current_function_decl
= current_function_decl
;
14537 current_function_decl
= decl
;
14540 /* Perform access checks for template parameters. */
14541 cp_parser_perform_template_parameter_access_checks (checks
);
14543 /* Perform the access control checks for the declarator and the
14544 decl-specifiers. */
14545 perform_deferred_access_checks ();
14547 /* Restore the saved value. */
14548 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14549 current_function_decl
= saved_current_function_decl
;
14552 /* Parse the initializer. */
14553 initializer
= NULL_TREE
;
14554 is_direct_init
= false;
14555 is_non_constant_init
= true;
14556 if (is_initialized
)
14558 if (function_declarator_p (declarator
))
14560 cp_token
*initializer_start_token
= cp_lexer_peek_token (parser
->lexer
);
14561 if (initialization_kind
== CPP_EQ
)
14562 initializer
= cp_parser_pure_specifier (parser
);
14565 /* If the declaration was erroneous, we don't really
14566 know what the user intended, so just silently
14567 consume the initializer. */
14568 if (decl
!= error_mark_node
)
14569 error_at (initializer_start_token
->location
,
14570 "initializer provided for function");
14571 cp_parser_skip_to_closing_parenthesis (parser
,
14572 /*recovering=*/true,
14573 /*or_comma=*/false,
14574 /*consume_paren=*/true);
14579 /* We want to record the extra mangling scope for in-class
14580 initializers of class members and initializers of static data
14581 member templates. The former is a C++0x feature which isn't
14582 implemented yet, and I expect it will involve deferring
14583 parsing of the initializer until end of class as with default
14584 arguments. So right here we only handle the latter. */
14585 if (!member_p
&& processing_template_decl
)
14586 start_lambda_scope (decl
);
14587 initializer
= cp_parser_initializer (parser
,
14589 &is_non_constant_init
);
14590 if (!member_p
&& processing_template_decl
)
14591 finish_lambda_scope ();
14595 /* The old parser allows attributes to appear after a parenthesized
14596 initializer. Mark Mitchell proposed removing this functionality
14597 on the GCC mailing lists on 2002-08-13. This parser accepts the
14598 attributes -- but ignores them. */
14599 if (cp_parser_allow_gnu_extensions_p (parser
)
14600 && initialization_kind
== CPP_OPEN_PAREN
)
14601 if (cp_parser_attributes_opt (parser
))
14602 warning (OPT_Wattributes
,
14603 "attributes after parenthesized initializer ignored");
14605 /* For an in-class declaration, use `grokfield' to create the
14611 pop_scope (pushed_scope
);
14612 pushed_scope
= false;
14614 decl
= grokfield (declarator
, decl_specifiers
,
14615 initializer
, !is_non_constant_init
,
14616 /*asmspec=*/NULL_TREE
,
14617 prefix_attributes
);
14618 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
14619 cp_parser_save_default_args (parser
, decl
);
14622 /* Finish processing the declaration. But, skip member
14624 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
14626 cp_finish_decl (decl
,
14627 initializer
, !is_non_constant_init
,
14629 /* If the initializer is in parentheses, then this is
14630 a direct-initialization, which means that an
14631 `explicit' constructor is OK. Otherwise, an
14632 `explicit' constructor cannot be used. */
14633 ((is_direct_init
|| !is_initialized
)
14634 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
14636 else if ((cxx_dialect
!= cxx98
) && friend_p
14637 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
14638 /* Core issue #226 (C++0x only): A default template-argument
14639 shall not be specified in a friend class template
14641 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/1,
14642 /*is_partial=*/0, /*is_friend_decl=*/1);
14644 if (!friend_p
&& pushed_scope
)
14645 pop_scope (pushed_scope
);
14650 /* Parse a declarator.
14654 ptr-operator declarator
14656 abstract-declarator:
14657 ptr-operator abstract-declarator [opt]
14658 direct-abstract-declarator
14663 attributes [opt] direct-declarator
14664 attributes [opt] ptr-operator declarator
14666 abstract-declarator:
14667 attributes [opt] ptr-operator abstract-declarator [opt]
14668 attributes [opt] direct-abstract-declarator
14670 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14671 detect constructor, destructor or conversion operators. It is set
14672 to -1 if the declarator is a name, and +1 if it is a
14673 function. Otherwise it is set to zero. Usually you just want to
14674 test for >0, but internally the negative value is used.
14676 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14677 a decl-specifier-seq unless it declares a constructor, destructor,
14678 or conversion. It might seem that we could check this condition in
14679 semantic analysis, rather than parsing, but that makes it difficult
14680 to handle something like `f()'. We want to notice that there are
14681 no decl-specifiers, and therefore realize that this is an
14682 expression, not a declaration.)
14684 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14685 the declarator is a direct-declarator of the form "(...)".
14687 MEMBER_P is true iff this declarator is a member-declarator. */
14689 static cp_declarator
*
14690 cp_parser_declarator (cp_parser
* parser
,
14691 cp_parser_declarator_kind dcl_kind
,
14692 int* ctor_dtor_or_conv_p
,
14693 bool* parenthesized_p
,
14696 cp_declarator
*declarator
;
14697 enum tree_code code
;
14698 cp_cv_quals cv_quals
;
14700 tree attributes
= NULL_TREE
;
14702 /* Assume this is not a constructor, destructor, or type-conversion
14704 if (ctor_dtor_or_conv_p
)
14705 *ctor_dtor_or_conv_p
= 0;
14707 if (cp_parser_allow_gnu_extensions_p (parser
))
14708 attributes
= cp_parser_attributes_opt (parser
);
14710 /* Check for the ptr-operator production. */
14711 cp_parser_parse_tentatively (parser
);
14712 /* Parse the ptr-operator. */
14713 code
= cp_parser_ptr_operator (parser
,
14716 /* If that worked, then we have a ptr-operator. */
14717 if (cp_parser_parse_definitely (parser
))
14719 /* If a ptr-operator was found, then this declarator was not
14721 if (parenthesized_p
)
14722 *parenthesized_p
= true;
14723 /* The dependent declarator is optional if we are parsing an
14724 abstract-declarator. */
14725 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
14726 cp_parser_parse_tentatively (parser
);
14728 /* Parse the dependent declarator. */
14729 declarator
= cp_parser_declarator (parser
, dcl_kind
,
14730 /*ctor_dtor_or_conv_p=*/NULL
,
14731 /*parenthesized_p=*/NULL
,
14732 /*member_p=*/false);
14734 /* If we are parsing an abstract-declarator, we must handle the
14735 case where the dependent declarator is absent. */
14736 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
14737 && !cp_parser_parse_definitely (parser
))
14740 declarator
= cp_parser_make_indirect_declarator
14741 (code
, class_type
, cv_quals
, declarator
);
14743 /* Everything else is a direct-declarator. */
14746 if (parenthesized_p
)
14747 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
14749 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
14750 ctor_dtor_or_conv_p
,
14754 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
14755 declarator
->attributes
= attributes
;
14760 /* Parse a direct-declarator or direct-abstract-declarator.
14764 direct-declarator ( parameter-declaration-clause )
14765 cv-qualifier-seq [opt]
14766 exception-specification [opt]
14767 direct-declarator [ constant-expression [opt] ]
14770 direct-abstract-declarator:
14771 direct-abstract-declarator [opt]
14772 ( parameter-declaration-clause )
14773 cv-qualifier-seq [opt]
14774 exception-specification [opt]
14775 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14776 ( abstract-declarator )
14778 Returns a representation of the declarator. DCL_KIND is
14779 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14780 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14781 we are parsing a direct-declarator. It is
14782 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14783 of ambiguity we prefer an abstract declarator, as per
14784 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14785 cp_parser_declarator. */
14787 static cp_declarator
*
14788 cp_parser_direct_declarator (cp_parser
* parser
,
14789 cp_parser_declarator_kind dcl_kind
,
14790 int* ctor_dtor_or_conv_p
,
14794 cp_declarator
*declarator
= NULL
;
14795 tree scope
= NULL_TREE
;
14796 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
14797 bool saved_in_declarator_p
= parser
->in_declarator_p
;
14799 tree pushed_scope
= NULL_TREE
;
14803 /* Peek at the next token. */
14804 token
= cp_lexer_peek_token (parser
->lexer
);
14805 if (token
->type
== CPP_OPEN_PAREN
)
14807 /* This is either a parameter-declaration-clause, or a
14808 parenthesized declarator. When we know we are parsing a
14809 named declarator, it must be a parenthesized declarator
14810 if FIRST is true. For instance, `(int)' is a
14811 parameter-declaration-clause, with an omitted
14812 direct-abstract-declarator. But `((*))', is a
14813 parenthesized abstract declarator. Finally, when T is a
14814 template parameter `(T)' is a
14815 parameter-declaration-clause, and not a parenthesized
14818 We first try and parse a parameter-declaration-clause,
14819 and then try a nested declarator (if FIRST is true).
14821 It is not an error for it not to be a
14822 parameter-declaration-clause, even when FIRST is
14828 The first is the declaration of a function while the
14829 second is the definition of a variable, including its
14832 Having seen only the parenthesis, we cannot know which of
14833 these two alternatives should be selected. Even more
14834 complex are examples like:
14839 The former is a function-declaration; the latter is a
14840 variable initialization.
14842 Thus again, we try a parameter-declaration-clause, and if
14843 that fails, we back out and return. */
14845 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
14848 unsigned saved_num_template_parameter_lists
;
14849 bool is_declarator
= false;
14852 /* In a member-declarator, the only valid interpretation
14853 of a parenthesis is the start of a
14854 parameter-declaration-clause. (It is invalid to
14855 initialize a static data member with a parenthesized
14856 initializer; only the "=" form of initialization is
14859 cp_parser_parse_tentatively (parser
);
14861 /* Consume the `('. */
14862 cp_lexer_consume_token (parser
->lexer
);
14865 /* If this is going to be an abstract declarator, we're
14866 in a declarator and we can't have default args. */
14867 parser
->default_arg_ok_p
= false;
14868 parser
->in_declarator_p
= true;
14871 /* Inside the function parameter list, surrounding
14872 template-parameter-lists do not apply. */
14873 saved_num_template_parameter_lists
14874 = parser
->num_template_parameter_lists
;
14875 parser
->num_template_parameter_lists
= 0;
14877 begin_scope (sk_function_parms
, NULL_TREE
);
14879 /* Parse the parameter-declaration-clause. */
14880 params
= cp_parser_parameter_declaration_clause (parser
);
14882 parser
->num_template_parameter_lists
14883 = saved_num_template_parameter_lists
;
14885 /* If all went well, parse the cv-qualifier-seq and the
14886 exception-specification. */
14887 if (member_p
|| cp_parser_parse_definitely (parser
))
14889 cp_cv_quals cv_quals
;
14890 tree exception_specification
;
14893 is_declarator
= true;
14895 if (ctor_dtor_or_conv_p
)
14896 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
14898 /* Consume the `)'. */
14899 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
14901 /* Parse the cv-qualifier-seq. */
14902 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
14903 /* And the exception-specification. */
14904 exception_specification
14905 = cp_parser_exception_specification_opt (parser
);
14908 = cp_parser_late_return_type_opt (parser
);
14910 /* Create the function-declarator. */
14911 declarator
= make_call_declarator (declarator
,
14914 exception_specification
,
14916 /* Any subsequent parameter lists are to do with
14917 return type, so are not those of the declared
14919 parser
->default_arg_ok_p
= false;
14922 /* Remove the function parms from scope. */
14923 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
14924 pop_binding (DECL_NAME (t
), t
);
14928 /* Repeat the main loop. */
14932 /* If this is the first, we can try a parenthesized
14936 bool saved_in_type_id_in_expr_p
;
14938 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
14939 parser
->in_declarator_p
= saved_in_declarator_p
;
14941 /* Consume the `('. */
14942 cp_lexer_consume_token (parser
->lexer
);
14943 /* Parse the nested declarator. */
14944 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
14945 parser
->in_type_id_in_expr_p
= true;
14947 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
14948 /*parenthesized_p=*/NULL
,
14950 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
14952 /* Expect a `)'. */
14953 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
14954 declarator
= cp_error_declarator
;
14955 if (declarator
== cp_error_declarator
)
14958 goto handle_declarator
;
14960 /* Otherwise, we must be done. */
14964 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
14965 && token
->type
== CPP_OPEN_SQUARE
)
14967 /* Parse an array-declarator. */
14970 if (ctor_dtor_or_conv_p
)
14971 *ctor_dtor_or_conv_p
= 0;
14974 parser
->default_arg_ok_p
= false;
14975 parser
->in_declarator_p
= true;
14976 /* Consume the `['. */
14977 cp_lexer_consume_token (parser
->lexer
);
14978 /* Peek at the next token. */
14979 token
= cp_lexer_peek_token (parser
->lexer
);
14980 /* If the next token is `]', then there is no
14981 constant-expression. */
14982 if (token
->type
!= CPP_CLOSE_SQUARE
)
14984 bool non_constant_p
;
14987 = cp_parser_constant_expression (parser
,
14988 /*allow_non_constant=*/true,
14990 if (!non_constant_p
)
14992 /* Normally, the array bound must be an integral constant
14993 expression. However, as an extension, we allow VLAs
14994 in function scopes as long as they aren't part of a
14995 parameter declaration. */
14996 else if (!parser
->in_function_body
14997 || current_binding_level
->kind
== sk_function_parms
)
14999 cp_parser_error (parser
,
15000 "array bound is not an integer constant");
15001 bounds
= error_mark_node
;
15003 else if (processing_template_decl
&& !error_operand_p (bounds
))
15005 /* Remember this wasn't a constant-expression. */
15006 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
15007 TREE_SIDE_EFFECTS (bounds
) = 1;
15011 bounds
= NULL_TREE
;
15012 /* Look for the closing `]'. */
15013 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
15015 declarator
= cp_error_declarator
;
15019 declarator
= make_array_declarator (declarator
, bounds
);
15021 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
15024 tree qualifying_scope
;
15025 tree unqualified_name
;
15026 special_function_kind sfk
;
15028 bool pack_expansion_p
= false;
15029 cp_token
*declarator_id_start_token
;
15031 /* Parse a declarator-id */
15032 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
15035 cp_parser_parse_tentatively (parser
);
15037 /* If we see an ellipsis, we should be looking at a
15039 if (token
->type
== CPP_ELLIPSIS
)
15041 /* Consume the `...' */
15042 cp_lexer_consume_token (parser
->lexer
);
15044 pack_expansion_p
= true;
15048 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
15050 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
15051 qualifying_scope
= parser
->scope
;
15056 if (!unqualified_name
&& pack_expansion_p
)
15058 /* Check whether an error occurred. */
15059 okay
= !cp_parser_error_occurred (parser
);
15061 /* We already consumed the ellipsis to mark a
15062 parameter pack, but we have no way to report it,
15063 so abort the tentative parse. We will be exiting
15064 immediately anyway. */
15065 cp_parser_abort_tentative_parse (parser
);
15068 okay
= cp_parser_parse_definitely (parser
);
15071 unqualified_name
= error_mark_node
;
15072 else if (unqualified_name
15073 && (qualifying_scope
15074 || (TREE_CODE (unqualified_name
)
15075 != IDENTIFIER_NODE
)))
15077 cp_parser_error (parser
, "expected unqualified-id");
15078 unqualified_name
= error_mark_node
;
15082 if (!unqualified_name
)
15084 if (unqualified_name
== error_mark_node
)
15086 declarator
= cp_error_declarator
;
15087 pack_expansion_p
= false;
15088 declarator
->parameter_pack_p
= false;
15092 if (qualifying_scope
&& at_namespace_scope_p ()
15093 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
15095 /* In the declaration of a member of a template class
15096 outside of the class itself, the SCOPE will sometimes
15097 be a TYPENAME_TYPE. For example, given:
15099 template <typename T>
15100 int S<T>::R::i = 3;
15102 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15103 this context, we must resolve S<T>::R to an ordinary
15104 type, rather than a typename type.
15106 The reason we normally avoid resolving TYPENAME_TYPEs
15107 is that a specialization of `S' might render
15108 `S<T>::R' not a type. However, if `S' is
15109 specialized, then this `i' will not be used, so there
15110 is no harm in resolving the types here. */
15113 /* Resolve the TYPENAME_TYPE. */
15114 type
= resolve_typename_type (qualifying_scope
,
15115 /*only_current_p=*/false);
15116 /* If that failed, the declarator is invalid. */
15117 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15119 if (typedef_variant_p (type
))
15120 error_at (declarator_id_start_token
->location
,
15121 "cannot define member of dependent typedef "
15124 error_at (declarator_id_start_token
->location
,
15125 "%<%T::%E%> is not a type",
15126 TYPE_CONTEXT (qualifying_scope
),
15127 TYPE_IDENTIFIER (qualifying_scope
));
15129 qualifying_scope
= type
;
15134 if (unqualified_name
)
15138 if (qualifying_scope
15139 && CLASS_TYPE_P (qualifying_scope
))
15140 class_type
= qualifying_scope
;
15142 class_type
= current_class_type
;
15144 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
15146 tree name_type
= TREE_TYPE (unqualified_name
);
15147 if (class_type
&& same_type_p (name_type
, class_type
))
15149 if (qualifying_scope
15150 && CLASSTYPE_USE_TEMPLATE (name_type
))
15152 error_at (declarator_id_start_token
->location
,
15153 "invalid use of constructor as a template");
15154 inform (declarator_id_start_token
->location
,
15155 "use %<%T::%D%> instead of %<%T::%D%> to "
15156 "name the constructor in a qualified name",
15158 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
15159 class_type
, name_type
);
15160 declarator
= cp_error_declarator
;
15164 unqualified_name
= constructor_name (class_type
);
15168 /* We do not attempt to print the declarator
15169 here because we do not have enough
15170 information about its original syntactic
15172 cp_parser_error (parser
, "invalid declarator");
15173 declarator
= cp_error_declarator
;
15180 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
15181 sfk
= sfk_destructor
;
15182 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
15183 sfk
= sfk_conversion
;
15184 else if (/* There's no way to declare a constructor
15185 for an anonymous type, even if the type
15186 got a name for linkage purposes. */
15187 !TYPE_WAS_ANONYMOUS (class_type
)
15188 && constructor_name_p (unqualified_name
,
15191 unqualified_name
= constructor_name (class_type
);
15192 sfk
= sfk_constructor
;
15194 else if (is_overloaded_fn (unqualified_name
)
15195 && DECL_CONSTRUCTOR_P (get_first_fn
15196 (unqualified_name
)))
15197 sfk
= sfk_constructor
;
15199 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
15200 *ctor_dtor_or_conv_p
= -1;
15203 declarator
= make_id_declarator (qualifying_scope
,
15206 declarator
->id_loc
= token
->location
;
15207 declarator
->parameter_pack_p
= pack_expansion_p
;
15209 if (pack_expansion_p
)
15210 maybe_warn_variadic_templates ();
15213 handle_declarator
:;
15214 scope
= get_scope_of_declarator (declarator
);
15216 /* Any names that appear after the declarator-id for a
15217 member are looked up in the containing scope. */
15218 pushed_scope
= push_scope (scope
);
15219 parser
->in_declarator_p
= true;
15220 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
15221 || (declarator
&& declarator
->kind
== cdk_id
))
15222 /* Default args are only allowed on function
15224 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
15226 parser
->default_arg_ok_p
= false;
15235 /* For an abstract declarator, we might wind up with nothing at this
15236 point. That's an error; the declarator is not optional. */
15238 cp_parser_error (parser
, "expected declarator");
15240 /* If we entered a scope, we must exit it now. */
15242 pop_scope (pushed_scope
);
15244 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
15245 parser
->in_declarator_p
= saved_in_declarator_p
;
15250 /* Parse a ptr-operator.
15253 * cv-qualifier-seq [opt]
15255 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15260 & cv-qualifier-seq [opt]
15262 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15263 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15264 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15265 filled in with the TYPE containing the member. *CV_QUALS is
15266 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15267 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15268 Note that the tree codes returned by this function have nothing
15269 to do with the types of trees that will be eventually be created
15270 to represent the pointer or reference type being parsed. They are
15271 just constants with suggestive names. */
15272 static enum tree_code
15273 cp_parser_ptr_operator (cp_parser
* parser
,
15275 cp_cv_quals
*cv_quals
)
15277 enum tree_code code
= ERROR_MARK
;
15280 /* Assume that it's not a pointer-to-member. */
15282 /* And that there are no cv-qualifiers. */
15283 *cv_quals
= TYPE_UNQUALIFIED
;
15285 /* Peek at the next token. */
15286 token
= cp_lexer_peek_token (parser
->lexer
);
15288 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15289 if (token
->type
== CPP_MULT
)
15290 code
= INDIRECT_REF
;
15291 else if (token
->type
== CPP_AND
)
15293 else if ((cxx_dialect
!= cxx98
) &&
15294 token
->type
== CPP_AND_AND
) /* C++0x only */
15295 code
= NON_LVALUE_EXPR
;
15297 if (code
!= ERROR_MARK
)
15299 /* Consume the `*', `&' or `&&'. */
15300 cp_lexer_consume_token (parser
->lexer
);
15302 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15303 `&', if we are allowing GNU extensions. (The only qualifier
15304 that can legally appear after `&' is `restrict', but that is
15305 enforced during semantic analysis. */
15306 if (code
== INDIRECT_REF
15307 || cp_parser_allow_gnu_extensions_p (parser
))
15308 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
15312 /* Try the pointer-to-member case. */
15313 cp_parser_parse_tentatively (parser
);
15314 /* Look for the optional `::' operator. */
15315 cp_parser_global_scope_opt (parser
,
15316 /*current_scope_valid_p=*/false);
15317 /* Look for the nested-name specifier. */
15318 token
= cp_lexer_peek_token (parser
->lexer
);
15319 cp_parser_nested_name_specifier (parser
,
15320 /*typename_keyword_p=*/false,
15321 /*check_dependency_p=*/true,
15323 /*is_declaration=*/false);
15324 /* If we found it, and the next token is a `*', then we are
15325 indeed looking at a pointer-to-member operator. */
15326 if (!cp_parser_error_occurred (parser
)
15327 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
15329 /* Indicate that the `*' operator was used. */
15330 code
= INDIRECT_REF
;
15332 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
15333 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
15336 /* The type of which the member is a member is given by the
15338 *type
= parser
->scope
;
15339 /* The next name will not be qualified. */
15340 parser
->scope
= NULL_TREE
;
15341 parser
->qualifying_scope
= NULL_TREE
;
15342 parser
->object_scope
= NULL_TREE
;
15343 /* Look for the optional cv-qualifier-seq. */
15344 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
15347 /* If that didn't work we don't have a ptr-operator. */
15348 if (!cp_parser_parse_definitely (parser
))
15349 cp_parser_error (parser
, "expected ptr-operator");
15355 /* Parse an (optional) cv-qualifier-seq.
15358 cv-qualifier cv-qualifier-seq [opt]
15369 Returns a bitmask representing the cv-qualifiers. */
15372 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
15374 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
15379 cp_cv_quals cv_qualifier
;
15381 /* Peek at the next token. */
15382 token
= cp_lexer_peek_token (parser
->lexer
);
15383 /* See if it's a cv-qualifier. */
15384 switch (token
->keyword
)
15387 cv_qualifier
= TYPE_QUAL_CONST
;
15391 cv_qualifier
= TYPE_QUAL_VOLATILE
;
15395 cv_qualifier
= TYPE_QUAL_RESTRICT
;
15399 cv_qualifier
= TYPE_UNQUALIFIED
;
15406 if (cv_quals
& cv_qualifier
)
15408 error_at (token
->location
, "duplicate cv-qualifier");
15409 cp_lexer_purge_token (parser
->lexer
);
15413 cp_lexer_consume_token (parser
->lexer
);
15414 cv_quals
|= cv_qualifier
;
15421 /* Parse a late-specified return type, if any. This is not a separate
15422 non-terminal, but part of a function declarator, which looks like
15424 -> trailing-type-specifier-seq abstract-declarator(opt)
15426 Returns the type indicated by the type-id. */
15429 cp_parser_late_return_type_opt (cp_parser
* parser
)
15433 /* Peek at the next token. */
15434 token
= cp_lexer_peek_token (parser
->lexer
);
15435 /* A late-specified return type is indicated by an initial '->'. */
15436 if (token
->type
!= CPP_DEREF
)
15439 /* Consume the ->. */
15440 cp_lexer_consume_token (parser
->lexer
);
15442 return cp_parser_trailing_type_id (parser
);
15445 /* Parse a declarator-id.
15449 :: [opt] nested-name-specifier [opt] type-name
15451 In the `id-expression' case, the value returned is as for
15452 cp_parser_id_expression if the id-expression was an unqualified-id.
15453 If the id-expression was a qualified-id, then a SCOPE_REF is
15454 returned. The first operand is the scope (either a NAMESPACE_DECL
15455 or TREE_TYPE), but the second is still just a representation of an
15459 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
15462 /* The expression must be an id-expression. Assume that qualified
15463 names are the names of types so that:
15466 int S<T>::R::i = 3;
15468 will work; we must treat `S<T>::R' as the name of a type.
15469 Similarly, assume that qualified names are templates, where
15473 int S<T>::R<T>::i = 3;
15476 id
= cp_parser_id_expression (parser
,
15477 /*template_keyword_p=*/false,
15478 /*check_dependency_p=*/false,
15479 /*template_p=*/NULL
,
15480 /*declarator_p=*/true,
15482 if (id
&& BASELINK_P (id
))
15483 id
= BASELINK_FUNCTIONS (id
);
15487 /* Parse a type-id.
15490 type-specifier-seq abstract-declarator [opt]
15492 Returns the TYPE specified. */
15495 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
15496 bool is_trailing_return
)
15498 cp_decl_specifier_seq type_specifier_seq
;
15499 cp_declarator
*abstract_declarator
;
15501 /* Parse the type-specifier-seq. */
15502 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
15503 is_trailing_return
,
15504 &type_specifier_seq
);
15505 if (type_specifier_seq
.type
== error_mark_node
)
15506 return error_mark_node
;
15508 /* There might or might not be an abstract declarator. */
15509 cp_parser_parse_tentatively (parser
);
15510 /* Look for the declarator. */
15511 abstract_declarator
15512 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
15513 /*parenthesized_p=*/NULL
,
15514 /*member_p=*/false);
15515 /* Check to see if there really was a declarator. */
15516 if (!cp_parser_parse_definitely (parser
))
15517 abstract_declarator
= NULL
;
15519 if (type_specifier_seq
.type
15520 && type_uses_auto (type_specifier_seq
.type
))
15522 /* A type-id with type 'auto' is only ok if the abstract declarator
15523 is a function declarator with a late-specified return type. */
15524 if (abstract_declarator
15525 && abstract_declarator
->kind
== cdk_function
15526 && abstract_declarator
->u
.function
.late_return_type
)
15530 error ("invalid use of %<auto%>");
15531 return error_mark_node
;
15535 return groktypename (&type_specifier_seq
, abstract_declarator
,
15539 static tree
cp_parser_type_id (cp_parser
*parser
)
15541 return cp_parser_type_id_1 (parser
, false, false);
15544 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
15547 const char *saved_message
= parser
->type_definition_forbidden_message
;
15548 parser
->type_definition_forbidden_message
15549 = G_("types may not be defined in template arguments");
15550 r
= cp_parser_type_id_1 (parser
, true, false);
15551 parser
->type_definition_forbidden_message
= saved_message
;
15555 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
15557 return cp_parser_type_id_1 (parser
, false, true);
15560 /* Parse a type-specifier-seq.
15562 type-specifier-seq:
15563 type-specifier type-specifier-seq [opt]
15567 type-specifier-seq:
15568 attributes type-specifier-seq [opt]
15570 If IS_DECLARATION is true, we are at the start of a "condition" or
15571 exception-declaration, so we might be followed by a declarator-id.
15573 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15574 i.e. we've just seen "->".
15576 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15579 cp_parser_type_specifier_seq (cp_parser
* parser
,
15580 bool is_declaration
,
15581 bool is_trailing_return
,
15582 cp_decl_specifier_seq
*type_specifier_seq
)
15584 bool seen_type_specifier
= false;
15585 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
15586 cp_token
*start_token
= NULL
;
15588 /* Clear the TYPE_SPECIFIER_SEQ. */
15589 clear_decl_specs (type_specifier_seq
);
15591 /* In the context of a trailing return type, enum E { } is an
15592 elaborated-type-specifier followed by a function-body, not an
15594 if (is_trailing_return
)
15595 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
15597 /* Parse the type-specifiers and attributes. */
15600 tree type_specifier
;
15601 bool is_cv_qualifier
;
15603 /* Check for attributes first. */
15604 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
15606 type_specifier_seq
->attributes
=
15607 chainon (type_specifier_seq
->attributes
,
15608 cp_parser_attributes_opt (parser
));
15612 /* record the token of the beginning of the type specifier seq,
15613 for error reporting purposes*/
15615 start_token
= cp_lexer_peek_token (parser
->lexer
);
15617 /* Look for the type-specifier. */
15618 type_specifier
= cp_parser_type_specifier (parser
,
15620 type_specifier_seq
,
15621 /*is_declaration=*/false,
15624 if (!type_specifier
)
15626 /* If the first type-specifier could not be found, this is not a
15627 type-specifier-seq at all. */
15628 if (!seen_type_specifier
)
15630 cp_parser_error (parser
, "expected type-specifier");
15631 type_specifier_seq
->type
= error_mark_node
;
15634 /* If subsequent type-specifiers could not be found, the
15635 type-specifier-seq is complete. */
15639 seen_type_specifier
= true;
15640 /* The standard says that a condition can be:
15642 type-specifier-seq declarator = assignment-expression
15649 we should treat the "S" as a declarator, not as a
15650 type-specifier. The standard doesn't say that explicitly for
15651 type-specifier-seq, but it does say that for
15652 decl-specifier-seq in an ordinary declaration. Perhaps it
15653 would be clearer just to allow a decl-specifier-seq here, and
15654 then add a semantic restriction that if any decl-specifiers
15655 that are not type-specifiers appear, the program is invalid. */
15656 if (is_declaration
&& !is_cv_qualifier
)
15657 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
15660 cp_parser_check_decl_spec (type_specifier_seq
, start_token
->location
);
15663 /* Parse a parameter-declaration-clause.
15665 parameter-declaration-clause:
15666 parameter-declaration-list [opt] ... [opt]
15667 parameter-declaration-list , ...
15669 Returns a representation for the parameter declarations. A return
15670 value of NULL indicates a parameter-declaration-clause consisting
15671 only of an ellipsis. */
15674 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
15681 /* Peek at the next token. */
15682 token
= cp_lexer_peek_token (parser
->lexer
);
15683 /* Check for trivial parameter-declaration-clauses. */
15684 if (token
->type
== CPP_ELLIPSIS
)
15686 /* Consume the `...' token. */
15687 cp_lexer_consume_token (parser
->lexer
);
15690 else if (token
->type
== CPP_CLOSE_PAREN
)
15691 /* There are no parameters. */
15693 #ifndef NO_IMPLICIT_EXTERN_C
15694 if (in_system_header
&& current_class_type
== NULL
15695 && current_lang_name
== lang_name_c
)
15699 return void_list_node
;
15701 /* Check for `(void)', too, which is a special case. */
15702 else if (token
->keyword
== RID_VOID
15703 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
15704 == CPP_CLOSE_PAREN
))
15706 /* Consume the `void' token. */
15707 cp_lexer_consume_token (parser
->lexer
);
15708 /* There are no parameters. */
15709 return void_list_node
;
15712 /* Parse the parameter-declaration-list. */
15713 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
15714 /* If a parse error occurred while parsing the
15715 parameter-declaration-list, then the entire
15716 parameter-declaration-clause is erroneous. */
15720 /* Peek at the next token. */
15721 token
= cp_lexer_peek_token (parser
->lexer
);
15722 /* If it's a `,', the clause should terminate with an ellipsis. */
15723 if (token
->type
== CPP_COMMA
)
15725 /* Consume the `,'. */
15726 cp_lexer_consume_token (parser
->lexer
);
15727 /* Expect an ellipsis. */
15729 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
15731 /* It might also be `...' if the optional trailing `,' was
15733 else if (token
->type
== CPP_ELLIPSIS
)
15735 /* Consume the `...' token. */
15736 cp_lexer_consume_token (parser
->lexer
);
15737 /* And remember that we saw it. */
15741 ellipsis_p
= false;
15743 /* Finish the parameter list. */
15745 parameters
= chainon (parameters
, void_list_node
);
15750 /* Parse a parameter-declaration-list.
15752 parameter-declaration-list:
15753 parameter-declaration
15754 parameter-declaration-list , parameter-declaration
15756 Returns a representation of the parameter-declaration-list, as for
15757 cp_parser_parameter_declaration_clause. However, the
15758 `void_list_node' is never appended to the list. Upon return,
15759 *IS_ERROR will be true iff an error occurred. */
15762 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
15764 tree parameters
= NULL_TREE
;
15765 tree
*tail
= ¶meters
;
15766 bool saved_in_unbraced_linkage_specification_p
;
15769 /* Assume all will go well. */
15771 /* The special considerations that apply to a function within an
15772 unbraced linkage specifications do not apply to the parameters
15773 to the function. */
15774 saved_in_unbraced_linkage_specification_p
15775 = parser
->in_unbraced_linkage_specification_p
;
15776 parser
->in_unbraced_linkage_specification_p
= false;
15778 /* Look for more parameters. */
15781 cp_parameter_declarator
*parameter
;
15782 tree decl
= error_mark_node
;
15783 bool parenthesized_p
;
15784 /* Parse the parameter. */
15786 = cp_parser_parameter_declaration (parser
,
15787 /*template_parm_p=*/false,
15790 /* We don't know yet if the enclosing context is deprecated, so wait
15791 and warn in grokparms if appropriate. */
15792 deprecated_state
= DEPRECATED_SUPPRESS
;
15795 decl
= grokdeclarator (parameter
->declarator
,
15796 ¶meter
->decl_specifiers
,
15798 parameter
->default_argument
!= NULL_TREE
,
15799 ¶meter
->decl_specifiers
.attributes
);
15801 deprecated_state
= DEPRECATED_NORMAL
;
15803 /* If a parse error occurred parsing the parameter declaration,
15804 then the entire parameter-declaration-list is erroneous. */
15805 if (decl
== error_mark_node
)
15808 parameters
= error_mark_node
;
15812 if (parameter
->decl_specifiers
.attributes
)
15813 cplus_decl_attributes (&decl
,
15814 parameter
->decl_specifiers
.attributes
,
15816 if (DECL_NAME (decl
))
15817 decl
= pushdecl (decl
);
15819 if (decl
!= error_mark_node
)
15821 retrofit_lang_decl (decl
);
15822 DECL_PARM_INDEX (decl
) = ++index
;
15823 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
15826 /* Add the new parameter to the list. */
15827 *tail
= build_tree_list (parameter
->default_argument
, decl
);
15828 tail
= &TREE_CHAIN (*tail
);
15830 /* Peek at the next token. */
15831 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
15832 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
15833 /* These are for Objective-C++ */
15834 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15835 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15836 /* The parameter-declaration-list is complete. */
15838 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
15842 /* Peek at the next token. */
15843 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15844 /* If it's an ellipsis, then the list is complete. */
15845 if (token
->type
== CPP_ELLIPSIS
)
15847 /* Otherwise, there must be more parameters. Consume the
15849 cp_lexer_consume_token (parser
->lexer
);
15850 /* When parsing something like:
15852 int i(float f, double d)
15854 we can tell after seeing the declaration for "f" that we
15855 are not looking at an initialization of a variable "i",
15856 but rather at the declaration of a function "i".
15858 Due to the fact that the parsing of template arguments
15859 (as specified to a template-id) requires backtracking we
15860 cannot use this technique when inside a template argument
15862 if (!parser
->in_template_argument_list_p
15863 && !parser
->in_type_id_in_expr_p
15864 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
15865 /* However, a parameter-declaration of the form
15866 "foat(f)" (which is a valid declaration of a
15867 parameter "f") can also be interpreted as an
15868 expression (the conversion of "f" to "float"). */
15869 && !parenthesized_p
)
15870 cp_parser_commit_to_tentative_parse (parser
);
15874 cp_parser_error (parser
, "expected %<,%> or %<...%>");
15875 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
15876 cp_parser_skip_to_closing_parenthesis (parser
,
15877 /*recovering=*/true,
15878 /*or_comma=*/false,
15879 /*consume_paren=*/false);
15884 parser
->in_unbraced_linkage_specification_p
15885 = saved_in_unbraced_linkage_specification_p
;
15890 /* Parse a parameter declaration.
15892 parameter-declaration:
15893 decl-specifier-seq ... [opt] declarator
15894 decl-specifier-seq declarator = assignment-expression
15895 decl-specifier-seq ... [opt] abstract-declarator [opt]
15896 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15898 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15899 declares a template parameter. (In that case, a non-nested `>'
15900 token encountered during the parsing of the assignment-expression
15901 is not interpreted as a greater-than operator.)
15903 Returns a representation of the parameter, or NULL if an error
15904 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15905 true iff the declarator is of the form "(p)". */
15907 static cp_parameter_declarator
*
15908 cp_parser_parameter_declaration (cp_parser
*parser
,
15909 bool template_parm_p
,
15910 bool *parenthesized_p
)
15912 int declares_class_or_enum
;
15913 cp_decl_specifier_seq decl_specifiers
;
15914 cp_declarator
*declarator
;
15915 tree default_argument
;
15916 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
15917 const char *saved_message
;
15919 /* In a template parameter, `>' is not an operator.
15923 When parsing a default template-argument for a non-type
15924 template-parameter, the first non-nested `>' is taken as the end
15925 of the template parameter-list rather than a greater-than
15928 /* Type definitions may not appear in parameter types. */
15929 saved_message
= parser
->type_definition_forbidden_message
;
15930 parser
->type_definition_forbidden_message
15931 = G_("types may not be defined in parameter types");
15933 /* Parse the declaration-specifiers. */
15934 cp_parser_decl_specifier_seq (parser
,
15935 CP_PARSER_FLAGS_NONE
,
15937 &declares_class_or_enum
);
15939 /* Complain about missing 'typename' or other invalid type names. */
15940 if (!decl_specifiers
.any_type_specifiers_p
)
15941 cp_parser_parse_and_diagnose_invalid_type_name (parser
);
15943 /* If an error occurred, there's no reason to attempt to parse the
15944 rest of the declaration. */
15945 if (cp_parser_error_occurred (parser
))
15947 parser
->type_definition_forbidden_message
= saved_message
;
15951 /* Peek at the next token. */
15952 token
= cp_lexer_peek_token (parser
->lexer
);
15954 /* If the next token is a `)', `,', `=', `>', or `...', then there
15955 is no declarator. However, when variadic templates are enabled,
15956 there may be a declarator following `...'. */
15957 if (token
->type
== CPP_CLOSE_PAREN
15958 || token
->type
== CPP_COMMA
15959 || token
->type
== CPP_EQ
15960 || token
->type
== CPP_GREATER
)
15963 if (parenthesized_p
)
15964 *parenthesized_p
= false;
15966 /* Otherwise, there should be a declarator. */
15969 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
15970 parser
->default_arg_ok_p
= false;
15972 /* After seeing a decl-specifier-seq, if the next token is not a
15973 "(", there is no possibility that the code is a valid
15974 expression. Therefore, if parsing tentatively, we commit at
15976 if (!parser
->in_template_argument_list_p
15977 /* In an expression context, having seen:
15981 we cannot be sure whether we are looking at a
15982 function-type (taking a "char" as a parameter) or a cast
15983 of some object of type "char" to "int". */
15984 && !parser
->in_type_id_in_expr_p
15985 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
15986 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
15987 cp_parser_commit_to_tentative_parse (parser
);
15988 /* Parse the declarator. */
15989 declarator_token_start
= token
;
15990 declarator
= cp_parser_declarator (parser
,
15991 CP_PARSER_DECLARATOR_EITHER
,
15992 /*ctor_dtor_or_conv_p=*/NULL
,
15994 /*member_p=*/false);
15995 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
15996 /* After the declarator, allow more attributes. */
15997 decl_specifiers
.attributes
15998 = chainon (decl_specifiers
.attributes
,
15999 cp_parser_attributes_opt (parser
));
16002 /* If the next token is an ellipsis, and we have not seen a
16003 declarator name, and the type of the declarator contains parameter
16004 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16005 a parameter pack expansion expression. Otherwise, leave the
16006 ellipsis for a C-style variadic function. */
16007 token
= cp_lexer_peek_token (parser
->lexer
);
16008 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16010 tree type
= decl_specifiers
.type
;
16012 if (type
&& DECL_P (type
))
16013 type
= TREE_TYPE (type
);
16016 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
16017 && declarator_can_be_parameter_pack (declarator
)
16018 && (!declarator
|| !declarator
->parameter_pack_p
)
16019 && uses_parameter_packs (type
))
16021 /* Consume the `...'. */
16022 cp_lexer_consume_token (parser
->lexer
);
16023 maybe_warn_variadic_templates ();
16025 /* Build a pack expansion type */
16027 declarator
->parameter_pack_p
= true;
16029 decl_specifiers
.type
= make_pack_expansion (type
);
16033 /* The restriction on defining new types applies only to the type
16034 of the parameter, not to the default argument. */
16035 parser
->type_definition_forbidden_message
= saved_message
;
16037 /* If the next token is `=', then process a default argument. */
16038 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16040 /* Consume the `='. */
16041 cp_lexer_consume_token (parser
->lexer
);
16043 /* If we are defining a class, then the tokens that make up the
16044 default argument must be saved and processed later. */
16045 if (!template_parm_p
&& at_class_scope_p ()
16046 && TYPE_BEING_DEFINED (current_class_type
)
16047 && !LAMBDA_TYPE_P (current_class_type
))
16049 unsigned depth
= 0;
16050 int maybe_template_id
= 0;
16051 cp_token
*first_token
;
16054 /* Add tokens until we have processed the entire default
16055 argument. We add the range [first_token, token). */
16056 first_token
= cp_lexer_peek_token (parser
->lexer
);
16061 /* Peek at the next token. */
16062 token
= cp_lexer_peek_token (parser
->lexer
);
16063 /* What we do depends on what token we have. */
16064 switch (token
->type
)
16066 /* In valid code, a default argument must be
16067 immediately followed by a `,' `)', or `...'. */
16069 if (depth
== 0 && maybe_template_id
)
16071 /* If we've seen a '<', we might be in a
16072 template-argument-list. Until Core issue 325 is
16073 resolved, we don't know how this situation ought
16074 to be handled, so try to DTRT. We check whether
16075 what comes after the comma is a valid parameter
16076 declaration list. If it is, then the comma ends
16077 the default argument; otherwise the default
16078 argument continues. */
16079 bool error
= false;
16082 /* Set ITALP so cp_parser_parameter_declaration_list
16083 doesn't decide to commit to this parse. */
16084 bool saved_italp
= parser
->in_template_argument_list_p
;
16085 parser
->in_template_argument_list_p
= true;
16087 cp_parser_parse_tentatively (parser
);
16088 cp_lexer_consume_token (parser
->lexer
);
16089 begin_scope (sk_function_parms
, NULL_TREE
);
16090 cp_parser_parameter_declaration_list (parser
, &error
);
16091 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
16092 pop_binding (DECL_NAME (t
), t
);
16094 if (!cp_parser_error_occurred (parser
) && !error
)
16096 cp_parser_abort_tentative_parse (parser
);
16098 parser
->in_template_argument_list_p
= saved_italp
;
16101 case CPP_CLOSE_PAREN
:
16103 /* If we run into a non-nested `;', `}', or `]',
16104 then the code is invalid -- but the default
16105 argument is certainly over. */
16106 case CPP_SEMICOLON
:
16107 case CPP_CLOSE_BRACE
:
16108 case CPP_CLOSE_SQUARE
:
16111 /* Update DEPTH, if necessary. */
16112 else if (token
->type
== CPP_CLOSE_PAREN
16113 || token
->type
== CPP_CLOSE_BRACE
16114 || token
->type
== CPP_CLOSE_SQUARE
)
16118 case CPP_OPEN_PAREN
:
16119 case CPP_OPEN_SQUARE
:
16120 case CPP_OPEN_BRACE
:
16126 /* This might be the comparison operator, or it might
16127 start a template argument list. */
16128 ++maybe_template_id
;
16132 if (cxx_dialect
== cxx98
)
16134 /* Fall through for C++0x, which treats the `>>'
16135 operator like two `>' tokens in certain
16141 /* This might be an operator, or it might close a
16142 template argument list. But if a previous '<'
16143 started a template argument list, this will have
16144 closed it, so we can't be in one anymore. */
16145 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
16146 if (maybe_template_id
< 0)
16147 maybe_template_id
= 0;
16151 /* If we run out of tokens, issue an error message. */
16153 case CPP_PRAGMA_EOL
:
16154 error_at (token
->location
, "file ends in default argument");
16160 /* In these cases, we should look for template-ids.
16161 For example, if the default argument is
16162 `X<int, double>()', we need to do name lookup to
16163 figure out whether or not `X' is a template; if
16164 so, the `,' does not end the default argument.
16166 That is not yet done. */
16173 /* If we've reached the end, stop. */
16177 /* Add the token to the token block. */
16178 token
= cp_lexer_consume_token (parser
->lexer
);
16181 /* Create a DEFAULT_ARG to represent the unparsed default
16183 default_argument
= make_node (DEFAULT_ARG
);
16184 DEFARG_TOKENS (default_argument
)
16185 = cp_token_cache_new (first_token
, token
);
16186 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
16188 /* Outside of a class definition, we can just parse the
16189 assignment-expression. */
16192 token
= cp_lexer_peek_token (parser
->lexer
);
16194 = cp_parser_default_argument (parser
, template_parm_p
);
16197 if (!parser
->default_arg_ok_p
)
16199 if (flag_permissive
)
16200 warning (0, "deprecated use of default argument for parameter of non-function");
16203 error_at (token
->location
,
16204 "default arguments are only "
16205 "permitted for function parameters");
16206 default_argument
= NULL_TREE
;
16209 else if ((declarator
&& declarator
->parameter_pack_p
)
16210 || (decl_specifiers
.type
16211 && PACK_EXPANSION_P (decl_specifiers
.type
)))
16213 /* Find the name of the parameter pack. */
16214 cp_declarator
*id_declarator
= declarator
;
16215 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
16216 id_declarator
= id_declarator
->declarator
;
16218 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
16219 error_at (declarator_token_start
->location
,
16221 ? "template parameter pack %qD"
16222 " cannot have a default argument"
16223 : "parameter pack %qD cannot have a default argument",
16224 id_declarator
->u
.id
.unqualified_name
);
16226 error_at (declarator_token_start
->location
,
16228 ? "template parameter pack cannot have a default argument"
16229 : "parameter pack cannot have a default argument");
16231 default_argument
= NULL_TREE
;
16235 default_argument
= NULL_TREE
;
16237 return make_parameter_declarator (&decl_specifiers
,
16242 /* Parse a default argument and return it.
16244 TEMPLATE_PARM_P is true if this is a default argument for a
16245 non-type template parameter. */
16247 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
16249 tree default_argument
= NULL_TREE
;
16250 bool saved_greater_than_is_operator_p
;
16251 bool saved_local_variables_forbidden_p
;
16253 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16255 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
16256 parser
->greater_than_is_operator_p
= !template_parm_p
;
16257 /* Local variable names (and the `this' keyword) may not
16258 appear in a default argument. */
16259 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
16260 parser
->local_variables_forbidden_p
= true;
16261 /* Parse the assignment-expression. */
16262 if (template_parm_p
)
16263 push_deferring_access_checks (dk_no_deferred
);
16265 = cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
16266 if (template_parm_p
)
16267 pop_deferring_access_checks ();
16268 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
16269 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
16271 return default_argument
;
16274 /* Parse a function-body.
16277 compound_statement */
16280 cp_parser_function_body (cp_parser
*parser
)
16282 cp_parser_compound_statement (parser
, NULL
, false, true);
16285 /* Parse a ctor-initializer-opt followed by a function-body. Return
16286 true if a ctor-initializer was present. */
16289 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
16292 bool ctor_initializer_p
;
16293 const bool check_body_p
=
16294 DECL_CONSTRUCTOR_P (current_function_decl
)
16295 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
16298 /* Begin the function body. */
16299 body
= begin_function_body ();
16300 /* Parse the optional ctor-initializer. */
16301 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
16303 /* If we're parsing a constexpr constructor definition, we need
16304 to check that the constructor body is indeed empty. However,
16305 before we get to cp_parser_function_body lot of junk has been
16306 generated, so we can't just check that we have an empty block.
16307 Rather we take a snapshot of the outermost block, and check whether
16308 cp_parser_function_body changed its state. */
16312 if (TREE_CODE (list
) == BIND_EXPR
)
16313 list
= BIND_EXPR_BODY (list
);
16314 if (TREE_CODE (list
) == STATEMENT_LIST
16315 && STATEMENT_LIST_TAIL (list
) != NULL
)
16316 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
16318 /* Parse the function-body. */
16319 cp_parser_function_body (parser
);
16321 check_constexpr_ctor_body (last
, list
);
16322 /* Finish the function body. */
16323 finish_function_body (body
);
16325 return ctor_initializer_p
;
16328 /* Parse an initializer.
16331 = initializer-clause
16332 ( expression-list )
16334 Returns an expression representing the initializer. If no
16335 initializer is present, NULL_TREE is returned.
16337 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16338 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16339 set to TRUE if there is no initializer present. If there is an
16340 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16341 is set to true; otherwise it is set to false. */
16344 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
16345 bool* non_constant_p
)
16350 /* Peek at the next token. */
16351 token
= cp_lexer_peek_token (parser
->lexer
);
16353 /* Let our caller know whether or not this initializer was
16355 *is_direct_init
= (token
->type
!= CPP_EQ
);
16356 /* Assume that the initializer is constant. */
16357 *non_constant_p
= false;
16359 if (token
->type
== CPP_EQ
)
16361 /* Consume the `='. */
16362 cp_lexer_consume_token (parser
->lexer
);
16363 /* Parse the initializer-clause. */
16364 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
16366 else if (token
->type
== CPP_OPEN_PAREN
)
16369 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
16371 /*allow_expansion_p=*/true,
16374 return error_mark_node
;
16375 init
= build_tree_list_vec (vec
);
16376 release_tree_vector (vec
);
16378 else if (token
->type
== CPP_OPEN_BRACE
)
16380 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
16381 init
= cp_parser_braced_list (parser
, non_constant_p
);
16382 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
16386 /* Anything else is an error. */
16387 cp_parser_error (parser
, "expected initializer");
16388 init
= error_mark_node
;
16394 /* Parse an initializer-clause.
16396 initializer-clause:
16397 assignment-expression
16400 Returns an expression representing the initializer.
16402 If the `assignment-expression' production is used the value
16403 returned is simply a representation for the expression.
16405 Otherwise, calls cp_parser_braced_list. */
16408 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
16412 /* Assume the expression is constant. */
16413 *non_constant_p
= false;
16415 /* If it is not a `{', then we are looking at an
16416 assignment-expression. */
16417 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
16420 = cp_parser_constant_expression (parser
,
16421 /*allow_non_constant_p=*/true,
16423 if (!*non_constant_p
)
16425 /* We only want to fold if this is really a constant
16426 expression. FIXME Actually, we don't want to fold here, but in
16428 tree folded
= fold_non_dependent_expr (initializer
);
16429 folded
= maybe_constant_value (folded
);
16430 if (TREE_CONSTANT (folded
))
16431 initializer
= folded
;
16435 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
16437 return initializer
;
16440 /* Parse a brace-enclosed initializer list.
16443 { initializer-list , [opt] }
16446 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16447 the elements of the initializer-list (or NULL, if the last
16448 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16449 NULL_TREE. There is no way to detect whether or not the optional
16450 trailing `,' was provided. NON_CONSTANT_P is as for
16451 cp_parser_initializer. */
16454 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
16458 /* Consume the `{' token. */
16459 cp_lexer_consume_token (parser
->lexer
);
16460 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16461 initializer
= make_node (CONSTRUCTOR
);
16462 /* If it's not a `}', then there is a non-trivial initializer. */
16463 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
16465 /* Parse the initializer list. */
16466 CONSTRUCTOR_ELTS (initializer
)
16467 = cp_parser_initializer_list (parser
, non_constant_p
);
16468 /* A trailing `,' token is allowed. */
16469 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
16470 cp_lexer_consume_token (parser
->lexer
);
16472 /* Now, there should be a trailing `}'. */
16473 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
16474 TREE_TYPE (initializer
) = init_list_type_node
;
16475 return initializer
;
16478 /* Parse an initializer-list.
16481 initializer-clause ... [opt]
16482 initializer-list , initializer-clause ... [opt]
16487 identifier : initializer-clause
16488 initializer-list, identifier : initializer-clause
16490 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16491 for the initializer. If the INDEX of the elt is non-NULL, it is the
16492 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16493 as for cp_parser_initializer. */
16495 static VEC(constructor_elt
,gc
) *
16496 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
16498 VEC(constructor_elt
,gc
) *v
= NULL
;
16500 /* Assume all of the expressions are constant. */
16501 *non_constant_p
= false;
16503 /* Parse the rest of the list. */
16509 bool clause_non_constant_p
;
16511 /* If the next token is an identifier and the following one is a
16512 colon, we are looking at the GNU designated-initializer
16514 if (cp_parser_allow_gnu_extensions_p (parser
)
16515 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
16516 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
16518 /* Warn the user that they are using an extension. */
16519 pedwarn (input_location
, OPT_pedantic
,
16520 "ISO C++ does not allow designated initializers");
16521 /* Consume the identifier. */
16522 identifier
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16523 /* Consume the `:'. */
16524 cp_lexer_consume_token (parser
->lexer
);
16527 identifier
= NULL_TREE
;
16529 /* Parse the initializer. */
16530 initializer
= cp_parser_initializer_clause (parser
,
16531 &clause_non_constant_p
);
16532 /* If any clause is non-constant, so is the entire initializer. */
16533 if (clause_non_constant_p
)
16534 *non_constant_p
= true;
16536 /* If we have an ellipsis, this is an initializer pack
16538 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16540 /* Consume the `...'. */
16541 cp_lexer_consume_token (parser
->lexer
);
16543 /* Turn the initializer into an initializer expansion. */
16544 initializer
= make_pack_expansion (initializer
);
16547 /* Add it to the vector. */
16548 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
16550 /* If the next token is not a comma, we have reached the end of
16552 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16555 /* Peek at the next token. */
16556 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
16557 /* If the next token is a `}', then we're still done. An
16558 initializer-clause can have a trailing `,' after the
16559 initializer-list and before the closing `}'. */
16560 if (token
->type
== CPP_CLOSE_BRACE
)
16563 /* Consume the `,' token. */
16564 cp_lexer_consume_token (parser
->lexer
);
16570 /* Classes [gram.class] */
16572 /* Parse a class-name.
16578 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16579 to indicate that names looked up in dependent types should be
16580 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16581 keyword has been used to indicate that the name that appears next
16582 is a template. TAG_TYPE indicates the explicit tag given before
16583 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16584 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16585 is the class being defined in a class-head.
16587 Returns the TYPE_DECL representing the class. */
16590 cp_parser_class_name (cp_parser
*parser
,
16591 bool typename_keyword_p
,
16592 bool template_keyword_p
,
16593 enum tag_types tag_type
,
16594 bool check_dependency_p
,
16596 bool is_declaration
)
16602 tree identifier
= NULL_TREE
;
16604 /* All class-names start with an identifier. */
16605 token
= cp_lexer_peek_token (parser
->lexer
);
16606 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
16608 cp_parser_error (parser
, "expected class-name");
16609 return error_mark_node
;
16612 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16613 to a template-id, so we save it here. */
16614 scope
= parser
->scope
;
16615 if (scope
== error_mark_node
)
16616 return error_mark_node
;
16618 /* Any name names a type if we're following the `typename' keyword
16619 in a qualified name where the enclosing scope is type-dependent. */
16620 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
16621 && dependent_type_p (scope
));
16622 /* Handle the common case (an identifier, but not a template-id)
16624 if (token
->type
== CPP_NAME
16625 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
16627 cp_token
*identifier_token
;
16630 /* Look for the identifier. */
16631 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
16632 ambiguous_p
= identifier_token
->ambiguous_p
;
16633 identifier
= cp_parser_identifier (parser
);
16634 /* If the next token isn't an identifier, we are certainly not
16635 looking at a class-name. */
16636 if (identifier
== error_mark_node
)
16637 decl
= error_mark_node
;
16638 /* If we know this is a type-name, there's no need to look it
16640 else if (typename_p
)
16644 tree ambiguous_decls
;
16645 /* If we already know that this lookup is ambiguous, then
16646 we've already issued an error message; there's no reason
16650 cp_parser_simulate_error (parser
);
16651 return error_mark_node
;
16653 /* If the next token is a `::', then the name must be a type
16656 [basic.lookup.qual]
16658 During the lookup for a name preceding the :: scope
16659 resolution operator, object, function, and enumerator
16660 names are ignored. */
16661 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16662 tag_type
= typename_type
;
16663 /* Look up the name. */
16664 decl
= cp_parser_lookup_name (parser
, identifier
,
16666 /*is_template=*/false,
16667 /*is_namespace=*/false,
16668 check_dependency_p
,
16670 identifier_token
->location
);
16671 if (ambiguous_decls
)
16673 if (cp_parser_parsing_tentatively (parser
))
16674 cp_parser_simulate_error (parser
);
16675 return error_mark_node
;
16681 /* Try a template-id. */
16682 decl
= cp_parser_template_id (parser
, template_keyword_p
,
16683 check_dependency_p
,
16685 if (decl
== error_mark_node
)
16686 return error_mark_node
;
16689 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
16691 /* If this is a typename, create a TYPENAME_TYPE. */
16692 if (typename_p
&& decl
!= error_mark_node
)
16694 decl
= make_typename_type (scope
, decl
, typename_type
,
16695 /*complain=*/tf_error
);
16696 if (decl
!= error_mark_node
)
16697 decl
= TYPE_NAME (decl
);
16700 /* Check to see that it is really the name of a class. */
16701 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
16702 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
16703 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16704 /* Situations like this:
16706 template <typename T> struct A {
16707 typename T::template X<int>::I i;
16710 are problematic. Is `T::template X<int>' a class-name? The
16711 standard does not seem to be definitive, but there is no other
16712 valid interpretation of the following `::'. Therefore, those
16713 names are considered class-names. */
16715 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
16716 if (decl
!= error_mark_node
)
16717 decl
= TYPE_NAME (decl
);
16719 else if (TREE_CODE (decl
) != TYPE_DECL
16720 || TREE_TYPE (decl
) == error_mark_node
16721 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
16722 /* In Objective-C 2.0, a classname followed by '.' starts a
16723 dot-syntax expression, and it's not a type-name. */
16724 || (c_dialect_objc ()
16725 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
16726 && objc_is_class_name (decl
)))
16727 decl
= error_mark_node
;
16729 if (decl
== error_mark_node
)
16730 cp_parser_error (parser
, "expected class-name");
16731 else if (identifier
&& !parser
->scope
)
16732 maybe_note_name_used_in_class (identifier
, decl
);
16737 /* Parse a class-specifier.
16740 class-head { member-specification [opt] }
16742 Returns the TREE_TYPE representing the class. */
16745 cp_parser_class_specifier (cp_parser
* parser
)
16748 tree attributes
= NULL_TREE
;
16749 bool nested_name_specifier_p
;
16750 unsigned saved_num_template_parameter_lists
;
16751 bool saved_in_function_body
;
16752 bool saved_in_unbraced_linkage_specification_p
;
16753 tree old_scope
= NULL_TREE
;
16754 tree scope
= NULL_TREE
;
16756 cp_token
*closing_brace
;
16758 push_deferring_access_checks (dk_no_deferred
);
16760 /* Parse the class-head. */
16761 type
= cp_parser_class_head (parser
,
16762 &nested_name_specifier_p
,
16765 /* If the class-head was a semantic disaster, skip the entire body
16769 cp_parser_skip_to_end_of_block_or_statement (parser
);
16770 pop_deferring_access_checks ();
16771 return error_mark_node
;
16774 /* Look for the `{'. */
16775 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
16777 pop_deferring_access_checks ();
16778 return error_mark_node
;
16781 /* Process the base classes. If they're invalid, skip the
16782 entire class body. */
16783 if (!xref_basetypes (type
, bases
))
16785 /* Consuming the closing brace yields better error messages
16787 if (cp_parser_skip_to_closing_brace (parser
))
16788 cp_lexer_consume_token (parser
->lexer
);
16789 pop_deferring_access_checks ();
16790 return error_mark_node
;
16793 /* Issue an error message if type-definitions are forbidden here. */
16794 cp_parser_check_type_definition (parser
);
16795 /* Remember that we are defining one more class. */
16796 ++parser
->num_classes_being_defined
;
16797 /* Inside the class, surrounding template-parameter-lists do not
16799 saved_num_template_parameter_lists
16800 = parser
->num_template_parameter_lists
;
16801 parser
->num_template_parameter_lists
= 0;
16802 /* We are not in a function body. */
16803 saved_in_function_body
= parser
->in_function_body
;
16804 parser
->in_function_body
= false;
16805 /* We are not immediately inside an extern "lang" block. */
16806 saved_in_unbraced_linkage_specification_p
16807 = parser
->in_unbraced_linkage_specification_p
;
16808 parser
->in_unbraced_linkage_specification_p
= false;
16810 /* Start the class. */
16811 if (nested_name_specifier_p
)
16813 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
16814 old_scope
= push_inner_scope (scope
);
16816 type
= begin_class_definition (type
, attributes
);
16818 if (type
== error_mark_node
)
16819 /* If the type is erroneous, skip the entire body of the class. */
16820 cp_parser_skip_to_closing_brace (parser
);
16822 /* Parse the member-specification. */
16823 cp_parser_member_specification_opt (parser
);
16825 /* Look for the trailing `}'. */
16826 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
16827 /* Look for trailing attributes to apply to this class. */
16828 if (cp_parser_allow_gnu_extensions_p (parser
))
16829 attributes
= cp_parser_attributes_opt (parser
);
16830 if (type
!= error_mark_node
)
16831 type
= finish_struct (type
, attributes
);
16832 if (nested_name_specifier_p
)
16833 pop_inner_scope (old_scope
, scope
);
16835 /* We've finished a type definition. Check for the common syntax
16836 error of forgetting a semicolon after the definition. We need to
16837 be careful, as we can't just check for not-a-semicolon and be done
16838 with it; the user might have typed:
16840 class X { } c = ...;
16841 class X { } *p = ...;
16843 and so forth. Instead, enumerate all the possible tokens that
16844 might follow this production; if we don't see one of them, then
16845 complain and silently insert the semicolon. */
16847 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16848 bool want_semicolon
= true;
16850 switch (token
->type
)
16853 case CPP_SEMICOLON
:
16856 case CPP_OPEN_PAREN
:
16857 case CPP_CLOSE_PAREN
:
16859 want_semicolon
= false;
16862 /* While it's legal for type qualifiers and storage class
16863 specifiers to follow type definitions in the grammar, only
16864 compiler testsuites contain code like that. Assume that if
16865 we see such code, then what we're really seeing is a case
16869 const <type> var = ...;
16874 static <type> func (...) ...
16876 i.e. the qualifier or specifier applies to the next
16877 declaration. To do so, however, we need to look ahead one
16878 more token to see if *that* token is a type specifier.
16880 This code could be improved to handle:
16883 static const <type> var = ...; */
16885 if (keyword_is_decl_specifier (token
->keyword
))
16887 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
16889 /* Handling user-defined types here would be nice, but very
16892 = (lookahead
->type
== CPP_KEYWORD
16893 && keyword_begins_type_specifier (lookahead
->keyword
));
16900 /* If we don't have a type, then something is very wrong and we
16901 shouldn't try to do anything clever. Likewise for not seeing the
16903 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
16905 cp_token_position prev
16906 = cp_lexer_previous_token_position (parser
->lexer
);
16907 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
16908 location_t loc
= prev_token
->location
;
16910 if (CLASSTYPE_DECLARED_CLASS (type
))
16911 error_at (loc
, "expected %<;%> after class definition");
16912 else if (TREE_CODE (type
) == RECORD_TYPE
)
16913 error_at (loc
, "expected %<;%> after struct definition");
16914 else if (TREE_CODE (type
) == UNION_TYPE
)
16915 error_at (loc
, "expected %<;%> after union definition");
16917 gcc_unreachable ();
16919 /* Unget one token and smash it to look as though we encountered
16920 a semicolon in the input stream. */
16921 cp_lexer_set_token_position (parser
->lexer
, prev
);
16922 token
= cp_lexer_peek_token (parser
->lexer
);
16923 token
->type
= CPP_SEMICOLON
;
16924 token
->keyword
= RID_MAX
;
16928 /* If this class is not itself within the scope of another class,
16929 then we need to parse the bodies of all of the queued function
16930 definitions. Note that the queued functions defined in a class
16931 are not always processed immediately following the
16932 class-specifier for that class. Consider:
16935 struct B { void f() { sizeof (A); } };
16938 If `f' were processed before the processing of `A' were
16939 completed, there would be no way to compute the size of `A'.
16940 Note that the nesting we are interested in here is lexical --
16941 not the semantic nesting given by TYPE_CONTEXT. In particular,
16944 struct A { struct B; };
16945 struct A::B { void f() { } };
16947 there is no need to delay the parsing of `A::B::f'. */
16948 if (--parser
->num_classes_being_defined
== 0)
16951 tree class_type
= NULL_TREE
;
16952 tree pushed_scope
= NULL_TREE
;
16954 cp_default_arg_entry
*e
;
16956 /* In a first pass, parse default arguments to the functions.
16957 Then, in a second pass, parse the bodies of the functions.
16958 This two-phased approach handles cases like:
16966 FOR_EACH_VEC_ELT (cp_default_arg_entry
, unparsed_funs_with_default_args
,
16970 /* If there are default arguments that have not yet been processed,
16971 take care of them now. */
16972 if (class_type
!= e
->class_type
)
16975 pop_scope (pushed_scope
);
16976 class_type
= e
->class_type
;
16977 pushed_scope
= push_scope (class_type
);
16979 /* Make sure that any template parameters are in scope. */
16980 maybe_begin_member_template_processing (fn
);
16981 /* Parse the default argument expressions. */
16982 cp_parser_late_parsing_default_args (parser
, fn
);
16983 /* Remove any template parameters from the symbol table. */
16984 maybe_end_member_template_processing ();
16987 pop_scope (pushed_scope
);
16988 VEC_truncate (cp_default_arg_entry
, unparsed_funs_with_default_args
, 0);
16989 /* Now parse the body of the functions. */
16990 FOR_EACH_VEC_ELT (tree
, unparsed_funs_with_definitions
, ix
, fn
)
16991 cp_parser_late_parsing_for_member (parser
, fn
);
16992 VEC_truncate (tree
, unparsed_funs_with_definitions
, 0);
16995 /* Put back any saved access checks. */
16996 pop_deferring_access_checks ();
16998 /* Restore saved state. */
16999 parser
->in_function_body
= saved_in_function_body
;
17000 parser
->num_template_parameter_lists
17001 = saved_num_template_parameter_lists
;
17002 parser
->in_unbraced_linkage_specification_p
17003 = saved_in_unbraced_linkage_specification_p
;
17008 /* Parse a class-head.
17011 class-key identifier [opt] base-clause [opt]
17012 class-key nested-name-specifier identifier base-clause [opt]
17013 class-key nested-name-specifier [opt] template-id
17017 class-key attributes identifier [opt] base-clause [opt]
17018 class-key attributes nested-name-specifier identifier base-clause [opt]
17019 class-key attributes nested-name-specifier [opt] template-id
17022 Upon return BASES is initialized to the list of base classes (or
17023 NULL, if there are none) in the same form returned by
17024 cp_parser_base_clause.
17026 Returns the TYPE of the indicated class. Sets
17027 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17028 involving a nested-name-specifier was used, and FALSE otherwise.
17030 Returns error_mark_node if this is not a class-head.
17032 Returns NULL_TREE if the class-head is syntactically valid, but
17033 semantically invalid in a way that means we should skip the entire
17034 body of the class. */
17037 cp_parser_class_head (cp_parser
* parser
,
17038 bool* nested_name_specifier_p
,
17039 tree
*attributes_p
,
17042 tree nested_name_specifier
;
17043 enum tag_types class_key
;
17044 tree id
= NULL_TREE
;
17045 tree type
= NULL_TREE
;
17047 bool template_id_p
= false;
17048 bool qualified_p
= false;
17049 bool invalid_nested_name_p
= false;
17050 bool invalid_explicit_specialization_p
= false;
17051 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
17052 tree pushed_scope
= NULL_TREE
;
17053 unsigned num_templates
;
17054 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
17055 /* Assume no nested-name-specifier will be present. */
17056 *nested_name_specifier_p
= false;
17057 /* Assume no template parameter lists will be used in defining the
17060 parser
->colon_corrects_to_scope_p
= false;
17062 *bases
= NULL_TREE
;
17064 /* Look for the class-key. */
17065 class_key
= cp_parser_class_key (parser
);
17066 if (class_key
== none_type
)
17067 return error_mark_node
;
17069 /* Parse the attributes. */
17070 attributes
= cp_parser_attributes_opt (parser
);
17072 /* If the next token is `::', that is invalid -- but sometimes
17073 people do try to write:
17077 Handle this gracefully by accepting the extra qualifier, and then
17078 issuing an error about it later if this really is a
17079 class-head. If it turns out just to be an elaborated type
17080 specifier, remain silent. */
17081 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
17082 qualified_p
= true;
17084 push_deferring_access_checks (dk_no_check
);
17086 /* Determine the name of the class. Begin by looking for an
17087 optional nested-name-specifier. */
17088 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
17089 nested_name_specifier
17090 = cp_parser_nested_name_specifier_opt (parser
,
17091 /*typename_keyword_p=*/false,
17092 /*check_dependency_p=*/false,
17094 /*is_declaration=*/false);
17095 /* If there was a nested-name-specifier, then there *must* be an
17097 if (nested_name_specifier
)
17099 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
17100 /* Although the grammar says `identifier', it really means
17101 `class-name' or `template-name'. You are only allowed to
17102 define a class that has already been declared with this
17105 The proposed resolution for Core Issue 180 says that wherever
17106 you see `class T::X' you should treat `X' as a type-name.
17108 It is OK to define an inaccessible class; for example:
17110 class A { class B; };
17113 We do not know if we will see a class-name, or a
17114 template-name. We look for a class-name first, in case the
17115 class-name is a template-id; if we looked for the
17116 template-name first we would stop after the template-name. */
17117 cp_parser_parse_tentatively (parser
);
17118 type
= cp_parser_class_name (parser
,
17119 /*typename_keyword_p=*/false,
17120 /*template_keyword_p=*/false,
17122 /*check_dependency_p=*/false,
17123 /*class_head_p=*/true,
17124 /*is_declaration=*/false);
17125 /* If that didn't work, ignore the nested-name-specifier. */
17126 if (!cp_parser_parse_definitely (parser
))
17128 invalid_nested_name_p
= true;
17129 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
17130 id
= cp_parser_identifier (parser
);
17131 if (id
== error_mark_node
)
17134 /* If we could not find a corresponding TYPE, treat this
17135 declaration like an unqualified declaration. */
17136 if (type
== error_mark_node
)
17137 nested_name_specifier
= NULL_TREE
;
17138 /* Otherwise, count the number of templates used in TYPE and its
17139 containing scopes. */
17144 for (scope
= TREE_TYPE (type
);
17145 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
17146 scope
= (TYPE_P (scope
)
17147 ? TYPE_CONTEXT (scope
)
17148 : DECL_CONTEXT (scope
)))
17150 && CLASS_TYPE_P (scope
)
17151 && CLASSTYPE_TEMPLATE_INFO (scope
)
17152 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
17153 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
17157 /* Otherwise, the identifier is optional. */
17160 /* We don't know whether what comes next is a template-id,
17161 an identifier, or nothing at all. */
17162 cp_parser_parse_tentatively (parser
);
17163 /* Check for a template-id. */
17164 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
17165 id
= cp_parser_template_id (parser
,
17166 /*template_keyword_p=*/false,
17167 /*check_dependency_p=*/true,
17168 /*is_declaration=*/true);
17169 /* If that didn't work, it could still be an identifier. */
17170 if (!cp_parser_parse_definitely (parser
))
17172 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17174 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
17175 id
= cp_parser_identifier (parser
);
17182 template_id_p
= true;
17187 pop_deferring_access_checks ();
17190 cp_parser_check_for_invalid_template_id (parser
, id
,
17191 type_start_token
->location
);
17193 /* If it's not a `:' or a `{' then we can't really be looking at a
17194 class-head, since a class-head only appears as part of a
17195 class-specifier. We have to detect this situation before calling
17196 xref_tag, since that has irreversible side-effects. */
17197 if (!cp_parser_next_token_starts_class_definition_p (parser
))
17199 cp_parser_error (parser
, "expected %<{%> or %<:%>");
17200 type
= error_mark_node
;
17204 /* At this point, we're going ahead with the class-specifier, even
17205 if some other problem occurs. */
17206 cp_parser_commit_to_tentative_parse (parser
);
17207 /* Issue the error about the overly-qualified name now. */
17210 cp_parser_error (parser
,
17211 "global qualification of class name is invalid");
17212 type
= error_mark_node
;
17215 else if (invalid_nested_name_p
)
17217 cp_parser_error (parser
,
17218 "qualified name does not name a class");
17219 type
= error_mark_node
;
17222 else if (nested_name_specifier
)
17226 /* Reject typedef-names in class heads. */
17227 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
17229 error_at (type_start_token
->location
,
17230 "invalid class name in declaration of %qD",
17236 /* Figure out in what scope the declaration is being placed. */
17237 scope
= current_scope ();
17238 /* If that scope does not contain the scope in which the
17239 class was originally declared, the program is invalid. */
17240 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
17242 if (at_namespace_scope_p ())
17243 error_at (type_start_token
->location
,
17244 "declaration of %qD in namespace %qD which does not "
17246 type
, scope
, nested_name_specifier
);
17248 error_at (type_start_token
->location
,
17249 "declaration of %qD in %qD which does not enclose %qD",
17250 type
, scope
, nested_name_specifier
);
17256 A declarator-id shall not be qualified except for the
17257 definition of a ... nested class outside of its class
17258 ... [or] the definition or explicit instantiation of a
17259 class member of a namespace outside of its namespace. */
17260 if (scope
== nested_name_specifier
)
17262 permerror (nested_name_specifier_token_start
->location
,
17263 "extra qualification not allowed");
17264 nested_name_specifier
= NULL_TREE
;
17268 /* An explicit-specialization must be preceded by "template <>". If
17269 it is not, try to recover gracefully. */
17270 if (at_namespace_scope_p ()
17271 && parser
->num_template_parameter_lists
== 0
17274 error_at (type_start_token
->location
,
17275 "an explicit specialization must be preceded by %<template <>%>");
17276 invalid_explicit_specialization_p
= true;
17277 /* Take the same action that would have been taken by
17278 cp_parser_explicit_specialization. */
17279 ++parser
->num_template_parameter_lists
;
17280 begin_specialization ();
17282 /* There must be no "return" statements between this point and the
17283 end of this function; set "type "to the correct return value and
17284 use "goto done;" to return. */
17285 /* Make sure that the right number of template parameters were
17287 if (!cp_parser_check_template_parameters (parser
, num_templates
,
17288 type_start_token
->location
,
17289 /*declarator=*/NULL
))
17291 /* If something went wrong, there is no point in even trying to
17292 process the class-definition. */
17297 /* Look up the type. */
17300 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
17301 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
17302 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
17304 error_at (type_start_token
->location
,
17305 "function template %qD redeclared as a class template", id
);
17306 type
= error_mark_node
;
17310 type
= TREE_TYPE (id
);
17311 type
= maybe_process_partial_specialization (type
);
17313 if (nested_name_specifier
)
17314 pushed_scope
= push_scope (nested_name_specifier
);
17316 else if (nested_name_specifier
)
17322 template <typename T> struct S { struct T };
17323 template <typename T> struct S<T>::T { };
17325 we will get a TYPENAME_TYPE when processing the definition of
17326 `S::T'. We need to resolve it to the actual type before we
17327 try to define it. */
17328 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
17330 class_type
= resolve_typename_type (TREE_TYPE (type
),
17331 /*only_current_p=*/false);
17332 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
17333 type
= TYPE_NAME (class_type
);
17336 cp_parser_error (parser
, "could not resolve typename type");
17337 type
= error_mark_node
;
17341 if (maybe_process_partial_specialization (TREE_TYPE (type
))
17342 == error_mark_node
)
17348 class_type
= current_class_type
;
17349 /* Enter the scope indicated by the nested-name-specifier. */
17350 pushed_scope
= push_scope (nested_name_specifier
);
17351 /* Get the canonical version of this type. */
17352 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
17353 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17354 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
17356 type
= push_template_decl (type
);
17357 if (type
== error_mark_node
)
17364 type
= TREE_TYPE (type
);
17365 *nested_name_specifier_p
= true;
17367 else /* The name is not a nested name. */
17369 /* If the class was unnamed, create a dummy name. */
17371 id
= make_anon_name ();
17372 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
17373 parser
->num_template_parameter_lists
);
17376 /* Indicate whether this class was declared as a `class' or as a
17378 if (TREE_CODE (type
) == RECORD_TYPE
)
17379 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
17380 cp_parser_check_class_key (class_key
, type
);
17382 /* If this type was already complete, and we see another definition,
17383 that's an error. */
17384 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
17386 error_at (type_start_token
->location
, "redefinition of %q#T",
17388 error_at (type_start_token
->location
, "previous definition of %q+#T",
17393 else if (type
== error_mark_node
)
17396 /* We will have entered the scope containing the class; the names of
17397 base classes should be looked up in that context. For example:
17399 struct A { struct B {}; struct C; };
17400 struct A::C : B {};
17404 /* Get the list of base-classes, if there is one. */
17405 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
17406 *bases
= cp_parser_base_clause (parser
);
17409 /* Leave the scope given by the nested-name-specifier. We will
17410 enter the class scope itself while processing the members. */
17412 pop_scope (pushed_scope
);
17414 if (invalid_explicit_specialization_p
)
17416 end_specialization ();
17417 --parser
->num_template_parameter_lists
;
17421 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
17422 *attributes_p
= attributes
;
17424 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
17428 /* Parse a class-key.
17435 Returns the kind of class-key specified, or none_type to indicate
17438 static enum tag_types
17439 cp_parser_class_key (cp_parser
* parser
)
17442 enum tag_types tag_type
;
17444 /* Look for the class-key. */
17445 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
17449 /* Check to see if the TOKEN is a class-key. */
17450 tag_type
= cp_parser_token_is_class_key (token
);
17452 cp_parser_error (parser
, "expected class-key");
17456 /* Parse an (optional) member-specification.
17458 member-specification:
17459 member-declaration member-specification [opt]
17460 access-specifier : member-specification [opt] */
17463 cp_parser_member_specification_opt (cp_parser
* parser
)
17470 /* Peek at the next token. */
17471 token
= cp_lexer_peek_token (parser
->lexer
);
17472 /* If it's a `}', or EOF then we've seen all the members. */
17473 if (token
->type
== CPP_CLOSE_BRACE
17474 || token
->type
== CPP_EOF
17475 || token
->type
== CPP_PRAGMA_EOL
)
17478 /* See if this token is a keyword. */
17479 keyword
= token
->keyword
;
17483 case RID_PROTECTED
:
17485 /* Consume the access-specifier. */
17486 cp_lexer_consume_token (parser
->lexer
);
17487 /* Remember which access-specifier is active. */
17488 current_access_specifier
= token
->u
.value
;
17489 /* Look for the `:'. */
17490 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
17494 /* Accept #pragmas at class scope. */
17495 if (token
->type
== CPP_PRAGMA
)
17497 cp_parser_pragma (parser
, pragma_external
);
17501 /* Otherwise, the next construction must be a
17502 member-declaration. */
17503 cp_parser_member_declaration (parser
);
17508 /* Parse a member-declaration.
17510 member-declaration:
17511 decl-specifier-seq [opt] member-declarator-list [opt] ;
17512 function-definition ; [opt]
17513 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17515 template-declaration
17517 member-declarator-list:
17519 member-declarator-list , member-declarator
17522 declarator pure-specifier [opt]
17523 declarator constant-initializer [opt]
17524 identifier [opt] : constant-expression
17528 member-declaration:
17529 __extension__ member-declaration
17532 declarator attributes [opt] pure-specifier [opt]
17533 declarator attributes [opt] constant-initializer [opt]
17534 identifier [opt] attributes [opt] : constant-expression
17538 member-declaration:
17539 static_assert-declaration */
17542 cp_parser_member_declaration (cp_parser
* parser
)
17544 cp_decl_specifier_seq decl_specifiers
;
17545 tree prefix_attributes
;
17547 int declares_class_or_enum
;
17549 cp_token
*token
= NULL
;
17550 cp_token
*decl_spec_token_start
= NULL
;
17551 cp_token
*initializer_token_start
= NULL
;
17552 int saved_pedantic
;
17553 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
17555 /* Check for the `__extension__' keyword. */
17556 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
17559 cp_parser_member_declaration (parser
);
17560 /* Restore the old value of the PEDANTIC flag. */
17561 pedantic
= saved_pedantic
;
17566 /* Check for a template-declaration. */
17567 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17569 /* An explicit specialization here is an error condition, and we
17570 expect the specialization handler to detect and report this. */
17571 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
17572 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
17573 cp_parser_explicit_specialization (parser
);
17575 cp_parser_template_declaration (parser
, /*member_p=*/true);
17580 /* Check for a using-declaration. */
17581 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
17583 /* Parse the using-declaration. */
17584 cp_parser_using_declaration (parser
,
17585 /*access_declaration_p=*/false);
17589 /* Check for @defs. */
17590 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
17593 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
17594 ivar
= ivar_chains
;
17598 ivar
= TREE_CHAIN (member
);
17599 TREE_CHAIN (member
) = NULL_TREE
;
17600 finish_member_declaration (member
);
17605 /* If the next token is `static_assert' we have a static assertion. */
17606 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
17608 cp_parser_static_assert (parser
, /*member_p=*/true);
17612 parser
->colon_corrects_to_scope_p
= false;
17614 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
17617 /* Parse the decl-specifier-seq. */
17618 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
17619 cp_parser_decl_specifier_seq (parser
,
17620 CP_PARSER_FLAGS_OPTIONAL
,
17622 &declares_class_or_enum
);
17623 prefix_attributes
= decl_specifiers
.attributes
;
17624 decl_specifiers
.attributes
= NULL_TREE
;
17625 /* Check for an invalid type-name. */
17626 if (!decl_specifiers
.any_type_specifiers_p
17627 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
17629 /* If there is no declarator, then the decl-specifier-seq should
17631 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17633 /* If there was no decl-specifier-seq, and the next token is a
17634 `;', then we have something like:
17640 Each member-declaration shall declare at least one member
17641 name of the class. */
17642 if (!decl_specifiers
.any_specifiers_p
)
17644 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17645 if (!in_system_header_at (token
->location
))
17646 pedwarn (token
->location
, OPT_pedantic
, "extra %<;%>");
17652 /* See if this declaration is a friend. */
17653 friend_p
= cp_parser_friend_p (&decl_specifiers
);
17654 /* If there were decl-specifiers, check to see if there was
17655 a class-declaration. */
17656 type
= check_tag_decl (&decl_specifiers
);
17657 /* Nested classes have already been added to the class, but
17658 a `friend' needs to be explicitly registered. */
17661 /* If the `friend' keyword was present, the friend must
17662 be introduced with a class-key. */
17663 if (!declares_class_or_enum
)
17664 error_at (decl_spec_token_start
->location
,
17665 "a class-key must be used when declaring a friend");
17668 template <typename T> struct A {
17669 friend struct A<T>::B;
17672 A<T>::B will be represented by a TYPENAME_TYPE, and
17673 therefore not recognized by check_tag_decl. */
17675 && decl_specifiers
.type
17676 && TYPE_P (decl_specifiers
.type
))
17677 type
= decl_specifiers
.type
;
17678 if (!type
|| !TYPE_P (type
))
17679 error_at (decl_spec_token_start
->location
,
17680 "friend declaration does not name a class or "
17683 make_friend_class (current_class_type
, type
,
17684 /*complain=*/true);
17686 /* If there is no TYPE, an error message will already have
17688 else if (!type
|| type
== error_mark_node
)
17690 /* An anonymous aggregate has to be handled specially; such
17691 a declaration really declares a data member (with a
17692 particular type), as opposed to a nested class. */
17693 else if (ANON_AGGR_TYPE_P (type
))
17695 /* Remove constructors and such from TYPE, now that we
17696 know it is an anonymous aggregate. */
17697 fixup_anonymous_aggr (type
);
17698 /* And make the corresponding data member. */
17699 decl
= build_decl (decl_spec_token_start
->location
,
17700 FIELD_DECL
, NULL_TREE
, type
);
17701 /* Add it to the class. */
17702 finish_member_declaration (decl
);
17705 cp_parser_check_access_in_redeclaration
17707 decl_spec_token_start
->location
);
17712 bool assume_semicolon
= false;
17714 /* See if these declarations will be friends. */
17715 friend_p
= cp_parser_friend_p (&decl_specifiers
);
17717 /* Keep going until we hit the `;' at the end of the
17719 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17721 tree attributes
= NULL_TREE
;
17722 tree first_attribute
;
17724 /* Peek at the next token. */
17725 token
= cp_lexer_peek_token (parser
->lexer
);
17727 /* Check for a bitfield declaration. */
17728 if (token
->type
== CPP_COLON
17729 || (token
->type
== CPP_NAME
17730 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
17736 /* Get the name of the bitfield. Note that we cannot just
17737 check TOKEN here because it may have been invalidated by
17738 the call to cp_lexer_peek_nth_token above. */
17739 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
17740 identifier
= cp_parser_identifier (parser
);
17742 identifier
= NULL_TREE
;
17744 /* Consume the `:' token. */
17745 cp_lexer_consume_token (parser
->lexer
);
17746 /* Get the width of the bitfield. */
17748 = cp_parser_constant_expression (parser
,
17749 /*allow_non_constant=*/false,
17752 /* Look for attributes that apply to the bitfield. */
17753 attributes
= cp_parser_attributes_opt (parser
);
17754 /* Remember which attributes are prefix attributes and
17756 first_attribute
= attributes
;
17757 /* Combine the attributes. */
17758 attributes
= chainon (prefix_attributes
, attributes
);
17760 /* Create the bitfield declaration. */
17761 decl
= grokbitfield (identifier
17762 ? make_id_declarator (NULL_TREE
,
17772 cp_declarator
*declarator
;
17774 tree asm_specification
;
17775 int ctor_dtor_or_conv_p
;
17777 /* Parse the declarator. */
17779 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17780 &ctor_dtor_or_conv_p
,
17781 /*parenthesized_p=*/NULL
,
17782 /*member_p=*/true);
17784 /* If something went wrong parsing the declarator, make sure
17785 that we at least consume some tokens. */
17786 if (declarator
== cp_error_declarator
)
17788 /* Skip to the end of the statement. */
17789 cp_parser_skip_to_end_of_statement (parser
);
17790 /* If the next token is not a semicolon, that is
17791 probably because we just skipped over the body of
17792 a function. So, we consume a semicolon if
17793 present, but do not issue an error message if it
17795 if (cp_lexer_next_token_is (parser
->lexer
,
17797 cp_lexer_consume_token (parser
->lexer
);
17801 if (declares_class_or_enum
& 2)
17802 cp_parser_check_for_definition_in_return_type
17803 (declarator
, decl_specifiers
.type
,
17804 decl_specifiers
.type_location
);
17806 /* Look for an asm-specification. */
17807 asm_specification
= cp_parser_asm_specification_opt (parser
);
17808 /* Look for attributes that apply to the declaration. */
17809 attributes
= cp_parser_attributes_opt (parser
);
17810 /* Remember which attributes are prefix attributes and
17812 first_attribute
= attributes
;
17813 /* Combine the attributes. */
17814 attributes
= chainon (prefix_attributes
, attributes
);
17816 /* If it's an `=', then we have a constant-initializer or a
17817 pure-specifier. It is not correct to parse the
17818 initializer before registering the member declaration
17819 since the member declaration should be in scope while
17820 its initializer is processed. However, the rest of the
17821 front end does not yet provide an interface that allows
17822 us to handle this correctly. */
17823 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
17827 A pure-specifier shall be used only in the declaration of
17828 a virtual function.
17830 A member-declarator can contain a constant-initializer
17831 only if it declares a static member of integral or
17834 Therefore, if the DECLARATOR is for a function, we look
17835 for a pure-specifier; otherwise, we look for a
17836 constant-initializer. When we call `grokfield', it will
17837 perform more stringent semantics checks. */
17838 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
17839 if (function_declarator_p (declarator
))
17840 initializer
= cp_parser_pure_specifier (parser
);
17842 /* Parse the initializer. */
17843 initializer
= cp_parser_constant_initializer (parser
);
17845 /* Otherwise, there is no initializer. */
17847 initializer
= NULL_TREE
;
17849 /* See if we are probably looking at a function
17850 definition. We are certainly not looking at a
17851 member-declarator. Calling `grokfield' has
17852 side-effects, so we must not do it unless we are sure
17853 that we are looking at a member-declarator. */
17854 if (cp_parser_token_starts_function_definition_p
17855 (cp_lexer_peek_token (parser
->lexer
)))
17857 /* The grammar does not allow a pure-specifier to be
17858 used when a member function is defined. (It is
17859 possible that this fact is an oversight in the
17860 standard, since a pure function may be defined
17861 outside of the class-specifier. */
17863 error_at (initializer_token_start
->location
,
17864 "pure-specifier on function-definition");
17865 decl
= cp_parser_save_member_function_body (parser
,
17869 /* If the member was not a friend, declare it here. */
17871 finish_member_declaration (decl
);
17872 /* Peek at the next token. */
17873 token
= cp_lexer_peek_token (parser
->lexer
);
17874 /* If the next token is a semicolon, consume it. */
17875 if (token
->type
== CPP_SEMICOLON
)
17876 cp_lexer_consume_token (parser
->lexer
);
17880 if (declarator
->kind
== cdk_function
)
17881 declarator
->id_loc
= token
->location
;
17882 /* Create the declaration. */
17883 decl
= grokfield (declarator
, &decl_specifiers
,
17884 initializer
, /*init_const_expr_p=*/true,
17889 /* Reset PREFIX_ATTRIBUTES. */
17890 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
17891 attributes
= TREE_CHAIN (attributes
);
17893 TREE_CHAIN (attributes
) = NULL_TREE
;
17895 /* If there is any qualification still in effect, clear it
17896 now; we will be starting fresh with the next declarator. */
17897 parser
->scope
= NULL_TREE
;
17898 parser
->qualifying_scope
= NULL_TREE
;
17899 parser
->object_scope
= NULL_TREE
;
17900 /* If it's a `,', then there are more declarators. */
17901 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
17902 cp_lexer_consume_token (parser
->lexer
);
17903 /* If the next token isn't a `;', then we have a parse error. */
17904 else if (cp_lexer_next_token_is_not (parser
->lexer
,
17907 /* The next token might be a ways away from where the
17908 actual semicolon is missing. Find the previous token
17909 and use that for our error position. */
17910 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
17911 error_at (token
->location
,
17912 "expected %<;%> at end of member declaration");
17914 /* Assume that the user meant to provide a semicolon. If
17915 we were to cp_parser_skip_to_end_of_statement, we might
17916 skip to a semicolon inside a member function definition
17917 and issue nonsensical error messages. */
17918 assume_semicolon
= true;
17923 /* Add DECL to the list of members. */
17925 finish_member_declaration (decl
);
17927 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17928 cp_parser_save_default_args (parser
, decl
);
17931 if (assume_semicolon
)
17936 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17938 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
17941 /* Parse a pure-specifier.
17946 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17947 Otherwise, ERROR_MARK_NODE is returned. */
17950 cp_parser_pure_specifier (cp_parser
* parser
)
17954 /* Look for the `=' token. */
17955 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
17956 return error_mark_node
;
17957 /* Look for the `0' token. */
17958 token
= cp_lexer_peek_token (parser
->lexer
);
17960 if (token
->type
== CPP_EOF
17961 || token
->type
== CPP_PRAGMA_EOL
)
17962 return error_mark_node
;
17964 cp_lexer_consume_token (parser
->lexer
);
17966 /* Accept = default or = delete in c++0x mode. */
17967 if (token
->keyword
== RID_DEFAULT
17968 || token
->keyword
== RID_DELETE
)
17970 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
17971 return token
->u
.value
;
17974 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17975 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
17977 cp_parser_error (parser
,
17978 "invalid pure specifier (only %<= 0%> is allowed)");
17979 cp_parser_skip_to_end_of_statement (parser
);
17980 return error_mark_node
;
17982 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17984 error_at (token
->location
, "templates may not be %<virtual%>");
17985 return error_mark_node
;
17988 return integer_zero_node
;
17991 /* Parse a constant-initializer.
17993 constant-initializer:
17994 = constant-expression
17996 Returns a representation of the constant-expression. */
17999 cp_parser_constant_initializer (cp_parser
* parser
)
18001 /* Look for the `=' token. */
18002 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
18003 return error_mark_node
;
18005 /* It is invalid to write:
18007 struct S { static const int i = { 7 }; };
18010 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18012 cp_parser_error (parser
,
18013 "a brace-enclosed initializer is not allowed here");
18014 /* Consume the opening brace. */
18015 cp_lexer_consume_token (parser
->lexer
);
18016 /* Skip the initializer. */
18017 cp_parser_skip_to_closing_brace (parser
);
18018 /* Look for the trailing `}'. */
18019 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
18021 return error_mark_node
;
18024 return cp_parser_constant_expression (parser
,
18025 /*allow_non_constant=*/false,
18029 /* Derived classes [gram.class.derived] */
18031 /* Parse a base-clause.
18034 : base-specifier-list
18036 base-specifier-list:
18037 base-specifier ... [opt]
18038 base-specifier-list , base-specifier ... [opt]
18040 Returns a TREE_LIST representing the base-classes, in the order in
18041 which they were declared. The representation of each node is as
18042 described by cp_parser_base_specifier.
18044 In the case that no bases are specified, this function will return
18045 NULL_TREE, not ERROR_MARK_NODE. */
18048 cp_parser_base_clause (cp_parser
* parser
)
18050 tree bases
= NULL_TREE
;
18052 /* Look for the `:' that begins the list. */
18053 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
18055 /* Scan the base-specifier-list. */
18060 bool pack_expansion_p
= false;
18062 /* Look for the base-specifier. */
18063 base
= cp_parser_base_specifier (parser
);
18064 /* Look for the (optional) ellipsis. */
18065 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18067 /* Consume the `...'. */
18068 cp_lexer_consume_token (parser
->lexer
);
18070 pack_expansion_p
= true;
18073 /* Add BASE to the front of the list. */
18074 if (base
!= error_mark_node
)
18076 if (pack_expansion_p
)
18077 /* Make this a pack expansion type. */
18078 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
18081 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
18083 TREE_CHAIN (base
) = bases
;
18087 /* Peek at the next token. */
18088 token
= cp_lexer_peek_token (parser
->lexer
);
18089 /* If it's not a comma, then the list is complete. */
18090 if (token
->type
!= CPP_COMMA
)
18092 /* Consume the `,'. */
18093 cp_lexer_consume_token (parser
->lexer
);
18096 /* PARSER->SCOPE may still be non-NULL at this point, if the last
18097 base class had a qualified name. However, the next name that
18098 appears is certainly not qualified. */
18099 parser
->scope
= NULL_TREE
;
18100 parser
->qualifying_scope
= NULL_TREE
;
18101 parser
->object_scope
= NULL_TREE
;
18103 return nreverse (bases
);
18106 /* Parse a base-specifier.
18109 :: [opt] nested-name-specifier [opt] class-name
18110 virtual access-specifier [opt] :: [opt] nested-name-specifier
18112 access-specifier virtual [opt] :: [opt] nested-name-specifier
18115 Returns a TREE_LIST. The TREE_PURPOSE will be one of
18116 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18117 indicate the specifiers provided. The TREE_VALUE will be a TYPE
18118 (or the ERROR_MARK_NODE) indicating the type that was specified. */
18121 cp_parser_base_specifier (cp_parser
* parser
)
18125 bool virtual_p
= false;
18126 bool duplicate_virtual_error_issued_p
= false;
18127 bool duplicate_access_error_issued_p
= false;
18128 bool class_scope_p
, template_p
;
18129 tree access
= access_default_node
;
18132 /* Process the optional `virtual' and `access-specifier'. */
18135 /* Peek at the next token. */
18136 token
= cp_lexer_peek_token (parser
->lexer
);
18137 /* Process `virtual'. */
18138 switch (token
->keyword
)
18141 /* If `virtual' appears more than once, issue an error. */
18142 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
18144 cp_parser_error (parser
,
18145 "%<virtual%> specified more than once in base-specified");
18146 duplicate_virtual_error_issued_p
= true;
18151 /* Consume the `virtual' token. */
18152 cp_lexer_consume_token (parser
->lexer
);
18157 case RID_PROTECTED
:
18159 /* If more than one access specifier appears, issue an
18161 if (access
!= access_default_node
18162 && !duplicate_access_error_issued_p
)
18164 cp_parser_error (parser
,
18165 "more than one access specifier in base-specified");
18166 duplicate_access_error_issued_p
= true;
18169 access
= ridpointers
[(int) token
->keyword
];
18171 /* Consume the access-specifier. */
18172 cp_lexer_consume_token (parser
->lexer
);
18181 /* It is not uncommon to see programs mechanically, erroneously, use
18182 the 'typename' keyword to denote (dependent) qualified types
18183 as base classes. */
18184 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
18186 token
= cp_lexer_peek_token (parser
->lexer
);
18187 if (!processing_template_decl
)
18188 error_at (token
->location
,
18189 "keyword %<typename%> not allowed outside of templates");
18191 error_at (token
->location
,
18192 "keyword %<typename%> not allowed in this context "
18193 "(the base class is implicitly a type)");
18194 cp_lexer_consume_token (parser
->lexer
);
18197 /* Look for the optional `::' operator. */
18198 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
18199 /* Look for the nested-name-specifier. The simplest way to
18204 The keyword `typename' is not permitted in a base-specifier or
18205 mem-initializer; in these contexts a qualified name that
18206 depends on a template-parameter is implicitly assumed to be a
18209 is to pretend that we have seen the `typename' keyword at this
18211 cp_parser_nested_name_specifier_opt (parser
,
18212 /*typename_keyword_p=*/true,
18213 /*check_dependency_p=*/true,
18215 /*is_declaration=*/true);
18216 /* If the base class is given by a qualified name, assume that names
18217 we see are type names or templates, as appropriate. */
18218 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
18219 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
18221 /* Finally, look for the class-name. */
18222 type
= cp_parser_class_name (parser
,
18226 /*check_dependency_p=*/true,
18227 /*class_head_p=*/false,
18228 /*is_declaration=*/true);
18230 if (type
== error_mark_node
)
18231 return error_mark_node
;
18233 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
18236 /* Exception handling [gram.exception] */
18238 /* Parse an (optional) exception-specification.
18240 exception-specification:
18241 throw ( type-id-list [opt] )
18243 Returns a TREE_LIST representing the exception-specification. The
18244 TREE_VALUE of each node is a type. */
18247 cp_parser_exception_specification_opt (cp_parser
* parser
)
18251 const char *saved_message
;
18253 /* Peek at the next token. */
18254 token
= cp_lexer_peek_token (parser
->lexer
);
18256 /* Is it a noexcept-specification? */
18257 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
18260 cp_lexer_consume_token (parser
->lexer
);
18262 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
18264 cp_lexer_consume_token (parser
->lexer
);
18266 /* Types may not be defined in an exception-specification. */
18267 saved_message
= parser
->type_definition_forbidden_message
;
18268 parser
->type_definition_forbidden_message
18269 = G_("types may not be defined in an exception-specification");
18271 expr
= cp_parser_constant_expression (parser
, false, NULL
);
18273 /* Restore the saved message. */
18274 parser
->type_definition_forbidden_message
= saved_message
;
18276 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18279 expr
= boolean_true_node
;
18281 return build_noexcept_spec (expr
, tf_warning_or_error
);
18284 /* If it's not `throw', then there's no exception-specification. */
18285 if (!cp_parser_is_keyword (token
, RID_THROW
))
18289 /* Enable this once a lot of code has transitioned to noexcept? */
18290 if (cxx_dialect
== cxx0x
&& !in_system_header
)
18291 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
18292 "deprecated in C++0x; use %<noexcept%> instead");
18295 /* Consume the `throw'. */
18296 cp_lexer_consume_token (parser
->lexer
);
18298 /* Look for the `('. */
18299 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18301 /* Peek at the next token. */
18302 token
= cp_lexer_peek_token (parser
->lexer
);
18303 /* If it's not a `)', then there is a type-id-list. */
18304 if (token
->type
!= CPP_CLOSE_PAREN
)
18306 /* Types may not be defined in an exception-specification. */
18307 saved_message
= parser
->type_definition_forbidden_message
;
18308 parser
->type_definition_forbidden_message
18309 = G_("types may not be defined in an exception-specification");
18310 /* Parse the type-id-list. */
18311 type_id_list
= cp_parser_type_id_list (parser
);
18312 /* Restore the saved message. */
18313 parser
->type_definition_forbidden_message
= saved_message
;
18316 type_id_list
= empty_except_spec
;
18318 /* Look for the `)'. */
18319 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18321 return type_id_list
;
18324 /* Parse an (optional) type-id-list.
18328 type-id-list , type-id ... [opt]
18330 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18331 in the order that the types were presented. */
18334 cp_parser_type_id_list (cp_parser
* parser
)
18336 tree types
= NULL_TREE
;
18343 /* Get the next type-id. */
18344 type
= cp_parser_type_id (parser
);
18345 /* Parse the optional ellipsis. */
18346 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18348 /* Consume the `...'. */
18349 cp_lexer_consume_token (parser
->lexer
);
18351 /* Turn the type into a pack expansion expression. */
18352 type
= make_pack_expansion (type
);
18354 /* Add it to the list. */
18355 types
= add_exception_specifier (types
, type
, /*complain=*/1);
18356 /* Peek at the next token. */
18357 token
= cp_lexer_peek_token (parser
->lexer
);
18358 /* If it is not a `,', we are done. */
18359 if (token
->type
!= CPP_COMMA
)
18361 /* Consume the `,'. */
18362 cp_lexer_consume_token (parser
->lexer
);
18365 return nreverse (types
);
18368 /* Parse a try-block.
18371 try compound-statement handler-seq */
18374 cp_parser_try_block (cp_parser
* parser
)
18378 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
18379 try_block
= begin_try_block ();
18380 cp_parser_compound_statement (parser
, NULL
, true, false);
18381 finish_try_block (try_block
);
18382 cp_parser_handler_seq (parser
);
18383 finish_handler_sequence (try_block
);
18388 /* Parse a function-try-block.
18390 function-try-block:
18391 try ctor-initializer [opt] function-body handler-seq */
18394 cp_parser_function_try_block (cp_parser
* parser
)
18396 tree compound_stmt
;
18398 bool ctor_initializer_p
;
18400 /* Look for the `try' keyword. */
18401 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
18403 /* Let the rest of the front end know where we are. */
18404 try_block
= begin_function_try_block (&compound_stmt
);
18405 /* Parse the function-body. */
18407 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
18408 /* We're done with the `try' part. */
18409 finish_function_try_block (try_block
);
18410 /* Parse the handlers. */
18411 cp_parser_handler_seq (parser
);
18412 /* We're done with the handlers. */
18413 finish_function_handler_sequence (try_block
, compound_stmt
);
18415 return ctor_initializer_p
;
18418 /* Parse a handler-seq.
18421 handler handler-seq [opt] */
18424 cp_parser_handler_seq (cp_parser
* parser
)
18430 /* Parse the handler. */
18431 cp_parser_handler (parser
);
18432 /* Peek at the next token. */
18433 token
= cp_lexer_peek_token (parser
->lexer
);
18434 /* If it's not `catch' then there are no more handlers. */
18435 if (!cp_parser_is_keyword (token
, RID_CATCH
))
18440 /* Parse a handler.
18443 catch ( exception-declaration ) compound-statement */
18446 cp_parser_handler (cp_parser
* parser
)
18451 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
18452 handler
= begin_handler ();
18453 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18454 declaration
= cp_parser_exception_declaration (parser
);
18455 finish_handler_parms (declaration
, handler
);
18456 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18457 cp_parser_compound_statement (parser
, NULL
, false, false);
18458 finish_handler (handler
);
18461 /* Parse an exception-declaration.
18463 exception-declaration:
18464 type-specifier-seq declarator
18465 type-specifier-seq abstract-declarator
18469 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18470 ellipsis variant is used. */
18473 cp_parser_exception_declaration (cp_parser
* parser
)
18475 cp_decl_specifier_seq type_specifiers
;
18476 cp_declarator
*declarator
;
18477 const char *saved_message
;
18479 /* If it's an ellipsis, it's easy to handle. */
18480 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18482 /* Consume the `...' token. */
18483 cp_lexer_consume_token (parser
->lexer
);
18487 /* Types may not be defined in exception-declarations. */
18488 saved_message
= parser
->type_definition_forbidden_message
;
18489 parser
->type_definition_forbidden_message
18490 = G_("types may not be defined in exception-declarations");
18492 /* Parse the type-specifier-seq. */
18493 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
18494 /*is_trailing_return=*/false,
18496 /* If it's a `)', then there is no declarator. */
18497 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
18500 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
18501 /*ctor_dtor_or_conv_p=*/NULL
,
18502 /*parenthesized_p=*/NULL
,
18503 /*member_p=*/false);
18505 /* Restore the saved message. */
18506 parser
->type_definition_forbidden_message
= saved_message
;
18508 if (!type_specifiers
.any_specifiers_p
)
18509 return error_mark_node
;
18511 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
18514 /* Parse a throw-expression.
18517 throw assignment-expression [opt]
18519 Returns a THROW_EXPR representing the throw-expression. */
18522 cp_parser_throw_expression (cp_parser
* parser
)
18527 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
18528 token
= cp_lexer_peek_token (parser
->lexer
);
18529 /* Figure out whether or not there is an assignment-expression
18530 following the "throw" keyword. */
18531 if (token
->type
== CPP_COMMA
18532 || token
->type
== CPP_SEMICOLON
18533 || token
->type
== CPP_CLOSE_PAREN
18534 || token
->type
== CPP_CLOSE_SQUARE
18535 || token
->type
== CPP_CLOSE_BRACE
18536 || token
->type
== CPP_COLON
)
18537 expression
= NULL_TREE
;
18539 expression
= cp_parser_assignment_expression (parser
,
18540 /*cast_p=*/false, NULL
);
18542 return build_throw (expression
);
18545 /* GNU Extensions */
18547 /* Parse an (optional) asm-specification.
18550 asm ( string-literal )
18552 If the asm-specification is present, returns a STRING_CST
18553 corresponding to the string-literal. Otherwise, returns
18557 cp_parser_asm_specification_opt (cp_parser
* parser
)
18560 tree asm_specification
;
18562 /* Peek at the next token. */
18563 token
= cp_lexer_peek_token (parser
->lexer
);
18564 /* If the next token isn't the `asm' keyword, then there's no
18565 asm-specification. */
18566 if (!cp_parser_is_keyword (token
, RID_ASM
))
18569 /* Consume the `asm' token. */
18570 cp_lexer_consume_token (parser
->lexer
);
18571 /* Look for the `('. */
18572 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18574 /* Look for the string-literal. */
18575 asm_specification
= cp_parser_string_literal (parser
, false, false);
18577 /* Look for the `)'. */
18578 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18580 return asm_specification
;
18583 /* Parse an asm-operand-list.
18587 asm-operand-list , asm-operand
18590 string-literal ( expression )
18591 [ string-literal ] string-literal ( expression )
18593 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18594 each node is the expression. The TREE_PURPOSE is itself a
18595 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18596 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18597 is a STRING_CST for the string literal before the parenthesis. Returns
18598 ERROR_MARK_NODE if any of the operands are invalid. */
18601 cp_parser_asm_operand_list (cp_parser
* parser
)
18603 tree asm_operands
= NULL_TREE
;
18604 bool invalid_operands
= false;
18608 tree string_literal
;
18612 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
18614 /* Consume the `[' token. */
18615 cp_lexer_consume_token (parser
->lexer
);
18616 /* Read the operand name. */
18617 name
= cp_parser_identifier (parser
);
18618 if (name
!= error_mark_node
)
18619 name
= build_string (IDENTIFIER_LENGTH (name
),
18620 IDENTIFIER_POINTER (name
));
18621 /* Look for the closing `]'. */
18622 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
18626 /* Look for the string-literal. */
18627 string_literal
= cp_parser_string_literal (parser
, false, false);
18629 /* Look for the `('. */
18630 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18631 /* Parse the expression. */
18632 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
18633 /* Look for the `)'. */
18634 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18636 if (name
== error_mark_node
18637 || string_literal
== error_mark_node
18638 || expression
== error_mark_node
)
18639 invalid_operands
= true;
18641 /* Add this operand to the list. */
18642 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
18645 /* If the next token is not a `,', there are no more
18647 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18649 /* Consume the `,'. */
18650 cp_lexer_consume_token (parser
->lexer
);
18653 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
18656 /* Parse an asm-clobber-list.
18660 asm-clobber-list , string-literal
18662 Returns a TREE_LIST, indicating the clobbers in the order that they
18663 appeared. The TREE_VALUE of each node is a STRING_CST. */
18666 cp_parser_asm_clobber_list (cp_parser
* parser
)
18668 tree clobbers
= NULL_TREE
;
18672 tree string_literal
;
18674 /* Look for the string literal. */
18675 string_literal
= cp_parser_string_literal (parser
, false, false);
18676 /* Add it to the list. */
18677 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
18678 /* If the next token is not a `,', then the list is
18680 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18682 /* Consume the `,' token. */
18683 cp_lexer_consume_token (parser
->lexer
);
18689 /* Parse an asm-label-list.
18693 asm-label-list , identifier
18695 Returns a TREE_LIST, indicating the labels in the order that they
18696 appeared. The TREE_VALUE of each node is a label. */
18699 cp_parser_asm_label_list (cp_parser
* parser
)
18701 tree labels
= NULL_TREE
;
18705 tree identifier
, label
, name
;
18707 /* Look for the identifier. */
18708 identifier
= cp_parser_identifier (parser
);
18709 if (!error_operand_p (identifier
))
18711 label
= lookup_label (identifier
);
18712 if (TREE_CODE (label
) == LABEL_DECL
)
18714 TREE_USED (label
) = 1;
18715 check_goto (label
);
18716 name
= build_string (IDENTIFIER_LENGTH (identifier
),
18717 IDENTIFIER_POINTER (identifier
));
18718 labels
= tree_cons (name
, label
, labels
);
18721 /* If the next token is not a `,', then the list is
18723 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18725 /* Consume the `,' token. */
18726 cp_lexer_consume_token (parser
->lexer
);
18729 return nreverse (labels
);
18732 /* Parse an (optional) series of attributes.
18735 attributes attribute
18738 __attribute__ (( attribute-list [opt] ))
18740 The return value is as for cp_parser_attribute_list. */
18743 cp_parser_attributes_opt (cp_parser
* parser
)
18745 tree attributes
= NULL_TREE
;
18750 tree attribute_list
;
18752 /* Peek at the next token. */
18753 token
= cp_lexer_peek_token (parser
->lexer
);
18754 /* If it's not `__attribute__', then we're done. */
18755 if (token
->keyword
!= RID_ATTRIBUTE
)
18758 /* Consume the `__attribute__' keyword. */
18759 cp_lexer_consume_token (parser
->lexer
);
18760 /* Look for the two `(' tokens. */
18761 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18762 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18764 /* Peek at the next token. */
18765 token
= cp_lexer_peek_token (parser
->lexer
);
18766 if (token
->type
!= CPP_CLOSE_PAREN
)
18767 /* Parse the attribute-list. */
18768 attribute_list
= cp_parser_attribute_list (parser
);
18770 /* If the next token is a `)', then there is no attribute
18772 attribute_list
= NULL
;
18774 /* Look for the two `)' tokens. */
18775 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18776 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18778 /* Add these new attributes to the list. */
18779 attributes
= chainon (attributes
, attribute_list
);
18785 /* Parse an attribute-list.
18789 attribute-list , attribute
18793 identifier ( identifier )
18794 identifier ( identifier , expression-list )
18795 identifier ( expression-list )
18797 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18798 to an attribute. The TREE_PURPOSE of each node is the identifier
18799 indicating which attribute is in use. The TREE_VALUE represents
18800 the arguments, if any. */
18803 cp_parser_attribute_list (cp_parser
* parser
)
18805 tree attribute_list
= NULL_TREE
;
18806 bool save_translate_strings_p
= parser
->translate_strings_p
;
18808 parser
->translate_strings_p
= false;
18815 /* Look for the identifier. We also allow keywords here; for
18816 example `__attribute__ ((const))' is legal. */
18817 token
= cp_lexer_peek_token (parser
->lexer
);
18818 if (token
->type
== CPP_NAME
18819 || token
->type
== CPP_KEYWORD
)
18821 tree arguments
= NULL_TREE
;
18823 /* Consume the token. */
18824 token
= cp_lexer_consume_token (parser
->lexer
);
18826 /* Save away the identifier that indicates which attribute
18828 identifier
= (token
->type
== CPP_KEYWORD
)
18829 /* For keywords, use the canonical spelling, not the
18830 parsed identifier. */
18831 ? ridpointers
[(int) token
->keyword
]
18834 attribute
= build_tree_list (identifier
, NULL_TREE
);
18836 /* Peek at the next token. */
18837 token
= cp_lexer_peek_token (parser
->lexer
);
18838 /* If it's an `(', then parse the attribute arguments. */
18839 if (token
->type
== CPP_OPEN_PAREN
)
18842 int attr_flag
= (attribute_takes_identifier_p (identifier
)
18843 ? id_attr
: normal_attr
);
18844 vec
= cp_parser_parenthesized_expression_list
18845 (parser
, attr_flag
, /*cast_p=*/false,
18846 /*allow_expansion_p=*/false,
18847 /*non_constant_p=*/NULL
);
18849 arguments
= error_mark_node
;
18852 arguments
= build_tree_list_vec (vec
);
18853 release_tree_vector (vec
);
18855 /* Save the arguments away. */
18856 TREE_VALUE (attribute
) = arguments
;
18859 if (arguments
!= error_mark_node
)
18861 /* Add this attribute to the list. */
18862 TREE_CHAIN (attribute
) = attribute_list
;
18863 attribute_list
= attribute
;
18866 token
= cp_lexer_peek_token (parser
->lexer
);
18868 /* Now, look for more attributes. If the next token isn't a
18869 `,', we're done. */
18870 if (token
->type
!= CPP_COMMA
)
18873 /* Consume the comma and keep going. */
18874 cp_lexer_consume_token (parser
->lexer
);
18876 parser
->translate_strings_p
= save_translate_strings_p
;
18878 /* We built up the list in reverse order. */
18879 return nreverse (attribute_list
);
18882 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18883 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18884 current value of the PEDANTIC flag, regardless of whether or not
18885 the `__extension__' keyword is present. The caller is responsible
18886 for restoring the value of the PEDANTIC flag. */
18889 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
18891 /* Save the old value of the PEDANTIC flag. */
18892 *saved_pedantic
= pedantic
;
18894 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
18896 /* Consume the `__extension__' token. */
18897 cp_lexer_consume_token (parser
->lexer
);
18898 /* We're not being pedantic while the `__extension__' keyword is
18908 /* Parse a label declaration.
18911 __label__ label-declarator-seq ;
18913 label-declarator-seq:
18914 identifier , label-declarator-seq
18918 cp_parser_label_declaration (cp_parser
* parser
)
18920 /* Look for the `__label__' keyword. */
18921 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
18927 /* Look for an identifier. */
18928 identifier
= cp_parser_identifier (parser
);
18929 /* If we failed, stop. */
18930 if (identifier
== error_mark_node
)
18932 /* Declare it as a label. */
18933 finish_label_decl (identifier
);
18934 /* If the next token is a `;', stop. */
18935 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18937 /* Look for the `,' separating the label declarations. */
18938 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
18941 /* Look for the final `;'. */
18942 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18945 /* Support Functions */
18947 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18948 NAME should have one of the representations used for an
18949 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18950 is returned. If PARSER->SCOPE is a dependent type, then a
18951 SCOPE_REF is returned.
18953 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18954 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18955 was formed. Abstractly, such entities should not be passed to this
18956 function, because they do not need to be looked up, but it is
18957 simpler to check for this special case here, rather than at the
18960 In cases not explicitly covered above, this function returns a
18961 DECL, OVERLOAD, or baselink representing the result of the lookup.
18962 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18965 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18966 (e.g., "struct") that was used. In that case bindings that do not
18967 refer to types are ignored.
18969 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18972 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18975 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18978 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18979 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18980 NULL_TREE otherwise. */
18983 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
18984 enum tag_types tag_type
,
18987 bool check_dependency
,
18988 tree
*ambiguous_decls
,
18989 location_t name_location
)
18993 tree object_type
= parser
->context
->object_type
;
18995 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18996 flags
|= LOOKUP_COMPLAIN
;
18998 /* Assume that the lookup will be unambiguous. */
18999 if (ambiguous_decls
)
19000 *ambiguous_decls
= NULL_TREE
;
19002 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19003 no longer valid. Note that if we are parsing tentatively, and
19004 the parse fails, OBJECT_TYPE will be automatically restored. */
19005 parser
->context
->object_type
= NULL_TREE
;
19007 if (name
== error_mark_node
)
19008 return error_mark_node
;
19010 /* A template-id has already been resolved; there is no lookup to
19012 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
19014 if (BASELINK_P (name
))
19016 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
19017 == TEMPLATE_ID_EXPR
);
19021 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
19022 it should already have been checked to make sure that the name
19023 used matches the type being destroyed. */
19024 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
19028 /* Figure out to which type this destructor applies. */
19030 type
= parser
->scope
;
19031 else if (object_type
)
19032 type
= object_type
;
19034 type
= current_class_type
;
19035 /* If that's not a class type, there is no destructor. */
19036 if (!type
|| !CLASS_TYPE_P (type
))
19037 return error_mark_node
;
19038 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
19039 lazily_declare_fn (sfk_destructor
, type
);
19040 if (!CLASSTYPE_DESTRUCTORS (type
))
19041 return error_mark_node
;
19042 /* If it was a class type, return the destructor. */
19043 return CLASSTYPE_DESTRUCTORS (type
);
19046 /* By this point, the NAME should be an ordinary identifier. If
19047 the id-expression was a qualified name, the qualifying scope is
19048 stored in PARSER->SCOPE at this point. */
19049 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
19051 /* Perform the lookup. */
19056 if (parser
->scope
== error_mark_node
)
19057 return error_mark_node
;
19059 /* If the SCOPE is dependent, the lookup must be deferred until
19060 the template is instantiated -- unless we are explicitly
19061 looking up names in uninstantiated templates. Even then, we
19062 cannot look up the name if the scope is not a class type; it
19063 might, for example, be a template type parameter. */
19064 dependent_p
= (TYPE_P (parser
->scope
)
19065 && dependent_scope_p (parser
->scope
));
19066 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
19068 /* Defer lookup. */
19069 decl
= error_mark_node
;
19072 tree pushed_scope
= NULL_TREE
;
19074 /* If PARSER->SCOPE is a dependent type, then it must be a
19075 class type, and we must not be checking dependencies;
19076 otherwise, we would have processed this lookup above. So
19077 that PARSER->SCOPE is not considered a dependent base by
19078 lookup_member, we must enter the scope here. */
19080 pushed_scope
= push_scope (parser
->scope
);
19082 /* If the PARSER->SCOPE is a template specialization, it
19083 may be instantiated during name lookup. In that case,
19084 errors may be issued. Even if we rollback the current
19085 tentative parse, those errors are valid. */
19086 decl
= lookup_qualified_name (parser
->scope
, name
,
19087 tag_type
!= none_type
,
19088 /*complain=*/true);
19090 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19091 lookup result and the nested-name-specifier nominates a class C:
19092 * if the name specified after the nested-name-specifier, when
19093 looked up in C, is the injected-class-name of C (Clause 9), or
19094 * if the name specified after the nested-name-specifier is the
19095 same as the identifier or the simple-template-id's template-
19096 name in the last component of the nested-name-specifier,
19097 the name is instead considered to name the constructor of
19098 class C. [ Note: for example, the constructor is not an
19099 acceptable lookup result in an elaborated-type-specifier so
19100 the constructor would not be used in place of the
19101 injected-class-name. --end note ] Such a constructor name
19102 shall be used only in the declarator-id of a declaration that
19103 names a constructor or in a using-declaration. */
19104 if (tag_type
== none_type
19105 && DECL_SELF_REFERENCE_P (decl
)
19106 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
19107 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
19108 tag_type
!= none_type
,
19109 /*complain=*/true);
19111 /* If we have a single function from a using decl, pull it out. */
19112 if (TREE_CODE (decl
) == OVERLOAD
19113 && !really_overloaded_fn (decl
))
19114 decl
= OVL_FUNCTION (decl
);
19117 pop_scope (pushed_scope
);
19120 /* If the scope is a dependent type and either we deferred lookup or
19121 we did lookup but didn't find the name, rememeber the name. */
19122 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
19123 && dependent_type_p (parser
->scope
))
19129 /* The resolution to Core Issue 180 says that `struct
19130 A::B' should be considered a type-name, even if `A'
19132 type
= make_typename_type (parser
->scope
, name
, tag_type
,
19133 /*complain=*/tf_error
);
19134 decl
= TYPE_NAME (type
);
19136 else if (is_template
19137 && (cp_parser_next_token_ends_template_argument_p (parser
)
19138 || cp_lexer_next_token_is (parser
->lexer
,
19140 decl
= make_unbound_class_template (parser
->scope
,
19142 /*complain=*/tf_error
);
19144 decl
= build_qualified_name (/*type=*/NULL_TREE
,
19145 parser
->scope
, name
,
19148 parser
->qualifying_scope
= parser
->scope
;
19149 parser
->object_scope
= NULL_TREE
;
19151 else if (object_type
)
19153 tree object_decl
= NULL_TREE
;
19154 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19155 OBJECT_TYPE is not a class. */
19156 if (CLASS_TYPE_P (object_type
))
19157 /* If the OBJECT_TYPE is a template specialization, it may
19158 be instantiated during name lookup. In that case, errors
19159 may be issued. Even if we rollback the current tentative
19160 parse, those errors are valid. */
19161 object_decl
= lookup_member (object_type
,
19164 tag_type
!= none_type
);
19165 /* Look it up in the enclosing context, too. */
19166 decl
= lookup_name_real (name
, tag_type
!= none_type
,
19168 /*block_p=*/true, is_namespace
, flags
);
19169 parser
->object_scope
= object_type
;
19170 parser
->qualifying_scope
= NULL_TREE
;
19172 decl
= object_decl
;
19176 decl
= lookup_name_real (name
, tag_type
!= none_type
,
19178 /*block_p=*/true, is_namespace
, flags
);
19179 parser
->qualifying_scope
= NULL_TREE
;
19180 parser
->object_scope
= NULL_TREE
;
19183 /* If the lookup failed, let our caller know. */
19184 if (!decl
|| decl
== error_mark_node
)
19185 return error_mark_node
;
19187 /* Pull out the template from an injected-class-name (or multiple). */
19189 decl
= maybe_get_template_decl_from_type_decl (decl
);
19191 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19192 if (TREE_CODE (decl
) == TREE_LIST
)
19194 if (ambiguous_decls
)
19195 *ambiguous_decls
= decl
;
19196 /* The error message we have to print is too complicated for
19197 cp_parser_error, so we incorporate its actions directly. */
19198 if (!cp_parser_simulate_error (parser
))
19200 error_at (name_location
, "reference to %qD is ambiguous",
19202 print_candidates (decl
);
19204 return error_mark_node
;
19207 gcc_assert (DECL_P (decl
)
19208 || TREE_CODE (decl
) == OVERLOAD
19209 || TREE_CODE (decl
) == SCOPE_REF
19210 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
19211 || BASELINK_P (decl
));
19213 /* If we have resolved the name of a member declaration, check to
19214 see if the declaration is accessible. When the name resolves to
19215 set of overloaded functions, accessibility is checked when
19216 overload resolution is done.
19218 During an explicit instantiation, access is not checked at all,
19219 as per [temp.explicit]. */
19221 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
19226 /* Like cp_parser_lookup_name, but for use in the typical case where
19227 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19228 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19231 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
19233 return cp_parser_lookup_name (parser
, name
,
19235 /*is_template=*/false,
19236 /*is_namespace=*/false,
19237 /*check_dependency=*/true,
19238 /*ambiguous_decls=*/NULL
,
19242 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19243 the current context, return the TYPE_DECL. If TAG_NAME_P is
19244 true, the DECL indicates the class being defined in a class-head,
19245 or declared in an elaborated-type-specifier.
19247 Otherwise, return DECL. */
19250 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
19252 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19253 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19256 template <typename T> struct B;
19259 template <typename T> struct A::B {};
19261 Similarly, in an elaborated-type-specifier:
19263 namespace N { struct X{}; }
19266 template <typename T> friend struct N::X;
19269 However, if the DECL refers to a class type, and we are in
19270 the scope of the class, then the name lookup automatically
19271 finds the TYPE_DECL created by build_self_reference rather
19272 than a TEMPLATE_DECL. For example, in:
19274 template <class T> struct S {
19278 there is no need to handle such case. */
19280 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
19281 return DECL_TEMPLATE_RESULT (decl
);
19286 /* If too many, or too few, template-parameter lists apply to the
19287 declarator, issue an error message. Returns TRUE if all went well,
19288 and FALSE otherwise. */
19291 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
19292 cp_declarator
*declarator
,
19293 location_t declarator_location
)
19295 unsigned num_templates
;
19297 /* We haven't seen any classes that involve template parameters yet. */
19300 switch (declarator
->kind
)
19303 if (declarator
->u
.id
.qualifying_scope
)
19307 scope
= declarator
->u
.id
.qualifying_scope
;
19309 while (scope
&& CLASS_TYPE_P (scope
))
19311 /* You're supposed to have one `template <...>'
19312 for every template class, but you don't need one
19313 for a full specialization. For example:
19315 template <class T> struct S{};
19316 template <> struct S<int> { void f(); };
19317 void S<int>::f () {}
19319 is correct; there shouldn't be a `template <>' for
19320 the definition of `S<int>::f'. */
19321 if (!CLASSTYPE_TEMPLATE_INFO (scope
))
19322 /* If SCOPE does not have template information of any
19323 kind, then it is not a template, nor is it nested
19324 within a template. */
19326 if (explicit_class_specialization_p (scope
))
19328 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
19331 scope
= TYPE_CONTEXT (scope
);
19334 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
19335 == TEMPLATE_ID_EXPR
)
19336 /* If the DECLARATOR has the form `X<y>' then it uses one
19337 additional level of template parameters. */
19340 return cp_parser_check_template_parameters
19341 (parser
, num_templates
, declarator_location
, declarator
);
19347 case cdk_reference
:
19349 return (cp_parser_check_declarator_template_parameters
19350 (parser
, declarator
->declarator
, declarator_location
));
19356 gcc_unreachable ();
19361 /* NUM_TEMPLATES were used in the current declaration. If that is
19362 invalid, return FALSE and issue an error messages. Otherwise,
19363 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19364 declarator and we can print more accurate diagnostics. */
19367 cp_parser_check_template_parameters (cp_parser
* parser
,
19368 unsigned num_templates
,
19369 location_t location
,
19370 cp_declarator
*declarator
)
19372 /* If there are the same number of template classes and parameter
19373 lists, that's OK. */
19374 if (parser
->num_template_parameter_lists
== num_templates
)
19376 /* If there are more, but only one more, then we are referring to a
19377 member template. That's OK too. */
19378 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
19380 /* If there are more template classes than parameter lists, we have
19383 template <class T> void S<T>::R<T>::f (); */
19384 if (parser
->num_template_parameter_lists
< num_templates
)
19386 if (declarator
&& !current_function_decl
)
19387 error_at (location
, "specializing member %<%T::%E%> "
19388 "requires %<template<>%> syntax",
19389 declarator
->u
.id
.qualifying_scope
,
19390 declarator
->u
.id
.unqualified_name
);
19391 else if (declarator
)
19392 error_at (location
, "invalid declaration of %<%T::%E%>",
19393 declarator
->u
.id
.qualifying_scope
,
19394 declarator
->u
.id
.unqualified_name
);
19396 error_at (location
, "too few template-parameter-lists");
19399 /* Otherwise, there are too many template parameter lists. We have
19402 template <class T> template <class U> void S::f(); */
19403 error_at (location
, "too many template-parameter-lists");
19407 /* Parse an optional `::' token indicating that the following name is
19408 from the global namespace. If so, PARSER->SCOPE is set to the
19409 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19410 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19411 Returns the new value of PARSER->SCOPE, if the `::' token is
19412 present, and NULL_TREE otherwise. */
19415 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
19419 /* Peek at the next token. */
19420 token
= cp_lexer_peek_token (parser
->lexer
);
19421 /* If we're looking at a `::' token then we're starting from the
19422 global namespace, not our current location. */
19423 if (token
->type
== CPP_SCOPE
)
19425 /* Consume the `::' token. */
19426 cp_lexer_consume_token (parser
->lexer
);
19427 /* Set the SCOPE so that we know where to start the lookup. */
19428 parser
->scope
= global_namespace
;
19429 parser
->qualifying_scope
= global_namespace
;
19430 parser
->object_scope
= NULL_TREE
;
19432 return parser
->scope
;
19434 else if (!current_scope_valid_p
)
19436 parser
->scope
= NULL_TREE
;
19437 parser
->qualifying_scope
= NULL_TREE
;
19438 parser
->object_scope
= NULL_TREE
;
19444 /* Returns TRUE if the upcoming token sequence is the start of a
19445 constructor declarator. If FRIEND_P is true, the declarator is
19446 preceded by the `friend' specifier. */
19449 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
19451 bool constructor_p
;
19452 tree nested_name_specifier
;
19453 cp_token
*next_token
;
19455 /* The common case is that this is not a constructor declarator, so
19456 try to avoid doing lots of work if at all possible. It's not
19457 valid declare a constructor at function scope. */
19458 if (parser
->in_function_body
)
19460 /* And only certain tokens can begin a constructor declarator. */
19461 next_token
= cp_lexer_peek_token (parser
->lexer
);
19462 if (next_token
->type
!= CPP_NAME
19463 && next_token
->type
!= CPP_SCOPE
19464 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
19465 && next_token
->type
!= CPP_TEMPLATE_ID
)
19468 /* Parse tentatively; we are going to roll back all of the tokens
19470 cp_parser_parse_tentatively (parser
);
19471 /* Assume that we are looking at a constructor declarator. */
19472 constructor_p
= true;
19474 /* Look for the optional `::' operator. */
19475 cp_parser_global_scope_opt (parser
,
19476 /*current_scope_valid_p=*/false);
19477 /* Look for the nested-name-specifier. */
19478 nested_name_specifier
19479 = (cp_parser_nested_name_specifier_opt (parser
,
19480 /*typename_keyword_p=*/false,
19481 /*check_dependency_p=*/false,
19483 /*is_declaration=*/false));
19484 /* Outside of a class-specifier, there must be a
19485 nested-name-specifier. */
19486 if (!nested_name_specifier
&&
19487 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
19489 constructor_p
= false;
19490 else if (nested_name_specifier
== error_mark_node
)
19491 constructor_p
= false;
19493 /* If we have a class scope, this is easy; DR 147 says that S::S always
19494 names the constructor, and no other qualified name could. */
19495 if (constructor_p
&& nested_name_specifier
19496 && TYPE_P (nested_name_specifier
))
19498 tree id
= cp_parser_unqualified_id (parser
,
19499 /*template_keyword_p=*/false,
19500 /*check_dependency_p=*/false,
19501 /*declarator_p=*/true,
19502 /*optional_p=*/false);
19503 if (is_overloaded_fn (id
))
19504 id
= DECL_NAME (get_first_fn (id
));
19505 if (!constructor_name_p (id
, nested_name_specifier
))
19506 constructor_p
= false;
19508 /* If we still think that this might be a constructor-declarator,
19509 look for a class-name. */
19510 else if (constructor_p
)
19514 template <typename T> struct S {
19518 we must recognize that the nested `S' names a class. */
19520 type_decl
= cp_parser_class_name (parser
,
19521 /*typename_keyword_p=*/false,
19522 /*template_keyword_p=*/false,
19524 /*check_dependency_p=*/false,
19525 /*class_head_p=*/false,
19526 /*is_declaration=*/false);
19527 /* If there was no class-name, then this is not a constructor. */
19528 constructor_p
= !cp_parser_error_occurred (parser
);
19530 /* If we're still considering a constructor, we have to see a `(',
19531 to begin the parameter-declaration-clause, followed by either a
19532 `)', an `...', or a decl-specifier. We need to check for a
19533 type-specifier to avoid being fooled into thinking that:
19537 is a constructor. (It is actually a function named `f' that
19538 takes one parameter (of type `int') and returns a value of type
19541 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19542 constructor_p
= false;
19545 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
19546 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
19547 /* A parameter declaration begins with a decl-specifier,
19548 which is either the "attribute" keyword, a storage class
19549 specifier, or (usually) a type-specifier. */
19550 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
19553 tree pushed_scope
= NULL_TREE
;
19554 unsigned saved_num_template_parameter_lists
;
19556 /* Names appearing in the type-specifier should be looked up
19557 in the scope of the class. */
19558 if (current_class_type
)
19562 type
= TREE_TYPE (type_decl
);
19563 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19565 type
= resolve_typename_type (type
,
19566 /*only_current_p=*/false);
19567 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19569 cp_parser_abort_tentative_parse (parser
);
19573 pushed_scope
= push_scope (type
);
19576 /* Inside the constructor parameter list, surrounding
19577 template-parameter-lists do not apply. */
19578 saved_num_template_parameter_lists
19579 = parser
->num_template_parameter_lists
;
19580 parser
->num_template_parameter_lists
= 0;
19582 /* Look for the type-specifier. */
19583 cp_parser_type_specifier (parser
,
19584 CP_PARSER_FLAGS_NONE
,
19585 /*decl_specs=*/NULL
,
19586 /*is_declarator=*/true,
19587 /*declares_class_or_enum=*/NULL
,
19588 /*is_cv_qualifier=*/NULL
);
19590 parser
->num_template_parameter_lists
19591 = saved_num_template_parameter_lists
;
19593 /* Leave the scope of the class. */
19595 pop_scope (pushed_scope
);
19597 constructor_p
= !cp_parser_error_occurred (parser
);
19601 /* We did not really want to consume any tokens. */
19602 cp_parser_abort_tentative_parse (parser
);
19604 return constructor_p
;
19607 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19608 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19609 they must be performed once we are in the scope of the function.
19611 Returns the function defined. */
19614 cp_parser_function_definition_from_specifiers_and_declarator
19615 (cp_parser
* parser
,
19616 cp_decl_specifier_seq
*decl_specifiers
,
19618 const cp_declarator
*declarator
)
19623 /* Begin the function-definition. */
19624 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
19626 /* The things we're about to see are not directly qualified by any
19627 template headers we've seen thus far. */
19628 reset_specialization ();
19630 /* If there were names looked up in the decl-specifier-seq that we
19631 did not check, check them now. We must wait until we are in the
19632 scope of the function to perform the checks, since the function
19633 might be a friend. */
19634 perform_deferred_access_checks ();
19638 /* Skip the entire function. */
19639 cp_parser_skip_to_end_of_block_or_statement (parser
);
19640 fn
= error_mark_node
;
19642 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
19644 /* Seen already, skip it. An error message has already been output. */
19645 cp_parser_skip_to_end_of_block_or_statement (parser
);
19646 fn
= current_function_decl
;
19647 current_function_decl
= NULL_TREE
;
19648 /* If this is a function from a class, pop the nested class. */
19649 if (current_class_name
)
19650 pop_nested_class ();
19653 fn
= cp_parser_function_definition_after_declarator (parser
,
19654 /*inline_p=*/false);
19659 /* Parse the part of a function-definition that follows the
19660 declarator. INLINE_P is TRUE iff this function is an inline
19661 function defined within a class-specifier.
19663 Returns the function defined. */
19666 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
19670 bool ctor_initializer_p
= false;
19671 bool saved_in_unbraced_linkage_specification_p
;
19672 bool saved_in_function_body
;
19673 unsigned saved_num_template_parameter_lists
;
19676 saved_in_function_body
= parser
->in_function_body
;
19677 parser
->in_function_body
= true;
19678 /* If the next token is `return', then the code may be trying to
19679 make use of the "named return value" extension that G++ used to
19681 token
= cp_lexer_peek_token (parser
->lexer
);
19682 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
19684 /* Consume the `return' keyword. */
19685 cp_lexer_consume_token (parser
->lexer
);
19686 /* Look for the identifier that indicates what value is to be
19688 cp_parser_identifier (parser
);
19689 /* Issue an error message. */
19690 error_at (token
->location
,
19691 "named return values are no longer supported");
19692 /* Skip tokens until we reach the start of the function body. */
19695 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19696 if (token
->type
== CPP_OPEN_BRACE
19697 || token
->type
== CPP_EOF
19698 || token
->type
== CPP_PRAGMA_EOL
)
19700 cp_lexer_consume_token (parser
->lexer
);
19703 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19704 anything declared inside `f'. */
19705 saved_in_unbraced_linkage_specification_p
19706 = parser
->in_unbraced_linkage_specification_p
;
19707 parser
->in_unbraced_linkage_specification_p
= false;
19708 /* Inside the function, surrounding template-parameter-lists do not
19710 saved_num_template_parameter_lists
19711 = parser
->num_template_parameter_lists
;
19712 parser
->num_template_parameter_lists
= 0;
19714 start_lambda_scope (current_function_decl
);
19716 /* If the next token is `try', then we are looking at a
19717 function-try-block. */
19718 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
19719 ctor_initializer_p
= cp_parser_function_try_block (parser
);
19720 /* A function-try-block includes the function-body, so we only do
19721 this next part if we're not processing a function-try-block. */
19724 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
19726 finish_lambda_scope ();
19728 /* Finish the function. */
19729 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
19730 (inline_p
? 2 : 0));
19731 /* Generate code for it, if necessary. */
19732 expand_or_defer_fn (fn
);
19733 /* Restore the saved values. */
19734 parser
->in_unbraced_linkage_specification_p
19735 = saved_in_unbraced_linkage_specification_p
;
19736 parser
->num_template_parameter_lists
19737 = saved_num_template_parameter_lists
;
19738 parser
->in_function_body
= saved_in_function_body
;
19743 /* Parse a template-declaration, assuming that the `export' (and
19744 `extern') keywords, if present, has already been scanned. MEMBER_P
19745 is as for cp_parser_template_declaration. */
19748 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
19750 tree decl
= NULL_TREE
;
19751 VEC (deferred_access_check
,gc
) *checks
;
19752 tree parameter_list
;
19753 bool friend_p
= false;
19754 bool need_lang_pop
;
19757 /* Look for the `template' keyword. */
19758 token
= cp_lexer_peek_token (parser
->lexer
);
19759 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
))
19763 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
19765 if (at_class_scope_p () && current_function_decl
)
19767 /* 14.5.2.2 [temp.mem]
19769 A local class shall not have member templates. */
19770 error_at (token
->location
,
19771 "invalid declaration of member template in local class");
19772 cp_parser_skip_to_end_of_block_or_statement (parser
);
19777 A template ... shall not have C linkage. */
19778 if (current_lang_name
== lang_name_c
)
19780 error_at (token
->location
, "template with C linkage");
19781 /* Give it C++ linkage to avoid confusing other parts of the
19783 push_lang_context (lang_name_cplusplus
);
19784 need_lang_pop
= true;
19787 need_lang_pop
= false;
19789 /* We cannot perform access checks on the template parameter
19790 declarations until we know what is being declared, just as we
19791 cannot check the decl-specifier list. */
19792 push_deferring_access_checks (dk_deferred
);
19794 /* If the next token is `>', then we have an invalid
19795 specialization. Rather than complain about an invalid template
19796 parameter, issue an error message here. */
19797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
19799 cp_parser_error (parser
, "invalid explicit specialization");
19800 begin_specialization ();
19801 parameter_list
= NULL_TREE
;
19805 /* Parse the template parameters. */
19806 parameter_list
= cp_parser_template_parameter_list (parser
);
19807 fixup_template_parms ();
19810 /* Get the deferred access checks from the parameter list. These
19811 will be checked once we know what is being declared, as for a
19812 member template the checks must be performed in the scope of the
19813 class containing the member. */
19814 checks
= get_deferred_access_checks ();
19816 /* Look for the `>'. */
19817 cp_parser_skip_to_end_of_template_parameter_list (parser
);
19818 /* We just processed one more parameter list. */
19819 ++parser
->num_template_parameter_lists
;
19820 /* If the next token is `template', there are more template
19822 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
19824 cp_parser_template_declaration_after_export (parser
, member_p
);
19827 /* There are no access checks when parsing a template, as we do not
19828 know if a specialization will be a friend. */
19829 push_deferring_access_checks (dk_no_check
);
19830 token
= cp_lexer_peek_token (parser
->lexer
);
19831 decl
= cp_parser_single_declaration (parser
,
19834 /*explicit_specialization_p=*/false,
19836 pop_deferring_access_checks ();
19838 /* If this is a member template declaration, let the front
19840 if (member_p
&& !friend_p
&& decl
)
19842 if (TREE_CODE (decl
) == TYPE_DECL
)
19843 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
19845 decl
= finish_member_template_decl (decl
);
19847 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
19848 make_friend_class (current_class_type
, TREE_TYPE (decl
),
19849 /*complain=*/true);
19851 /* We are done with the current parameter list. */
19852 --parser
->num_template_parameter_lists
;
19854 pop_deferring_access_checks ();
19857 finish_template_decl (parameter_list
);
19859 /* Register member declarations. */
19860 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
19861 finish_member_declaration (decl
);
19862 /* For the erroneous case of a template with C linkage, we pushed an
19863 implicit C++ linkage scope; exit that scope now. */
19865 pop_lang_context ();
19866 /* If DECL is a function template, we must return to parse it later.
19867 (Even though there is no definition, there might be default
19868 arguments that need handling.) */
19869 if (member_p
&& decl
19870 && (TREE_CODE (decl
) == FUNCTION_DECL
19871 || DECL_FUNCTION_TEMPLATE_P (decl
)))
19872 VEC_safe_push (tree
, gc
, unparsed_funs_with_definitions
, decl
);
19875 /* Perform the deferred access checks from a template-parameter-list.
19876 CHECKS is a TREE_LIST of access checks, as returned by
19877 get_deferred_access_checks. */
19880 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check
,gc
)* checks
)
19882 ++processing_template_parmlist
;
19883 perform_access_checks (checks
);
19884 --processing_template_parmlist
;
19887 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19888 `function-definition' sequence. MEMBER_P is true, this declaration
19889 appears in a class scope.
19891 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19892 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19895 cp_parser_single_declaration (cp_parser
* parser
,
19896 VEC (deferred_access_check
,gc
)* checks
,
19898 bool explicit_specialization_p
,
19901 int declares_class_or_enum
;
19902 tree decl
= NULL_TREE
;
19903 cp_decl_specifier_seq decl_specifiers
;
19904 bool function_definition_p
= false;
19905 cp_token
*decl_spec_token_start
;
19907 /* This function is only used when processing a template
19909 gcc_assert (innermost_scope_kind () == sk_template_parms
19910 || innermost_scope_kind () == sk_template_spec
);
19912 /* Defer access checks until we know what is being declared. */
19913 push_deferring_access_checks (dk_deferred
);
19915 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19917 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
19918 cp_parser_decl_specifier_seq (parser
,
19919 CP_PARSER_FLAGS_OPTIONAL
,
19921 &declares_class_or_enum
);
19923 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
19925 /* There are no template typedefs. */
19926 if (decl_specifiers
.specs
[(int) ds_typedef
])
19928 error_at (decl_spec_token_start
->location
,
19929 "template declaration of %<typedef%>");
19930 decl
= error_mark_node
;
19933 /* Gather up the access checks that occurred the
19934 decl-specifier-seq. */
19935 stop_deferring_access_checks ();
19937 /* Check for the declaration of a template class. */
19938 if (declares_class_or_enum
)
19940 if (cp_parser_declares_only_class_p (parser
))
19942 decl
= shadow_tag (&decl_specifiers
);
19947 friend template <typename T> struct A<T>::B;
19950 A<T>::B will be represented by a TYPENAME_TYPE, and
19951 therefore not recognized by shadow_tag. */
19952 if (friend_p
&& *friend_p
19954 && decl_specifiers
.type
19955 && TYPE_P (decl_specifiers
.type
))
19956 decl
= decl_specifiers
.type
;
19958 if (decl
&& decl
!= error_mark_node
)
19959 decl
= TYPE_NAME (decl
);
19961 decl
= error_mark_node
;
19963 /* Perform access checks for template parameters. */
19964 cp_parser_perform_template_parameter_access_checks (checks
);
19968 /* Complain about missing 'typename' or other invalid type names. */
19969 if (!decl_specifiers
.any_type_specifiers_p
19970 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
19972 /* cp_parser_parse_and_diagnose_invalid_type_name calls
19973 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
19974 the rest of this declaration. */
19975 decl
= error_mark_node
;
19979 /* If it's not a template class, try for a template function. If
19980 the next token is a `;', then this declaration does not declare
19981 anything. But, if there were errors in the decl-specifiers, then
19982 the error might well have come from an attempted class-specifier.
19983 In that case, there's no need to warn about a missing declarator. */
19985 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
19986 || decl_specifiers
.type
!= error_mark_node
))
19988 decl
= cp_parser_init_declarator (parser
,
19991 /*function_definition_allowed_p=*/true,
19993 declares_class_or_enum
,
19994 &function_definition_p
,
19997 /* 7.1.1-1 [dcl.stc]
19999 A storage-class-specifier shall not be specified in an explicit
20000 specialization... */
20002 && explicit_specialization_p
20003 && decl_specifiers
.storage_class
!= sc_none
)
20005 error_at (decl_spec_token_start
->location
,
20006 "explicit template specialization cannot have a storage class");
20007 decl
= error_mark_node
;
20011 /* Look for a trailing `;' after the declaration. */
20012 if (!function_definition_p
20013 && (decl
== error_mark_node
20014 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
20015 cp_parser_skip_to_end_of_block_or_statement (parser
);
20018 pop_deferring_access_checks ();
20020 /* Clear any current qualification; whatever comes next is the start
20021 of something new. */
20022 parser
->scope
= NULL_TREE
;
20023 parser
->qualifying_scope
= NULL_TREE
;
20024 parser
->object_scope
= NULL_TREE
;
20029 /* Parse a cast-expression that is not the operand of a unary "&". */
20032 cp_parser_simple_cast_expression (cp_parser
*parser
)
20034 return cp_parser_cast_expression (parser
, /*address_p=*/false,
20035 /*cast_p=*/false, NULL
);
20038 /* Parse a functional cast to TYPE. Returns an expression
20039 representing the cast. */
20042 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
20045 tree expression_list
;
20049 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
20051 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
20052 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
20053 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
20054 if (TREE_CODE (type
) == TYPE_DECL
)
20055 type
= TREE_TYPE (type
);
20056 return finish_compound_literal (type
, expression_list
,
20057 tf_warning_or_error
);
20061 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
20063 /*allow_expansion_p=*/true,
20064 /*non_constant_p=*/NULL
);
20066 expression_list
= error_mark_node
;
20069 expression_list
= build_tree_list_vec (vec
);
20070 release_tree_vector (vec
);
20073 cast
= build_functional_cast (type
, expression_list
,
20074 tf_warning_or_error
);
20075 /* [expr.const]/1: In an integral constant expression "only type
20076 conversions to integral or enumeration type can be used". */
20077 if (TREE_CODE (type
) == TYPE_DECL
)
20078 type
= TREE_TYPE (type
);
20079 if (cast
!= error_mark_node
20080 && !cast_valid_in_integral_constant_expression_p (type
)
20081 && cp_parser_non_integral_constant_expression (parser
,
20083 return error_mark_node
;
20087 /* Save the tokens that make up the body of a member function defined
20088 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
20089 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
20090 specifiers applied to the declaration. Returns the FUNCTION_DECL
20091 for the member function. */
20094 cp_parser_save_member_function_body (cp_parser
* parser
,
20095 cp_decl_specifier_seq
*decl_specifiers
,
20096 cp_declarator
*declarator
,
20103 /* Create the FUNCTION_DECL. */
20104 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
20105 /* If something went badly wrong, bail out now. */
20106 if (fn
== error_mark_node
)
20108 /* If there's a function-body, skip it. */
20109 if (cp_parser_token_starts_function_definition_p
20110 (cp_lexer_peek_token (parser
->lexer
)))
20111 cp_parser_skip_to_end_of_block_or_statement (parser
);
20112 return error_mark_node
;
20115 /* Remember it, if there default args to post process. */
20116 cp_parser_save_default_args (parser
, fn
);
20118 /* Save away the tokens that make up the body of the
20120 first
= parser
->lexer
->next_token
;
20121 /* We can have braced-init-list mem-initializers before the fn body. */
20122 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20124 cp_lexer_consume_token (parser
->lexer
);
20125 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
20126 && cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_TRY
))
20128 /* cache_group will stop after an un-nested { } pair, too. */
20129 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
20132 /* variadic mem-inits have ... after the ')'. */
20133 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20134 cp_lexer_consume_token (parser
->lexer
);
20137 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
20138 /* Handle function try blocks. */
20139 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
20140 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
20141 last
= parser
->lexer
->next_token
;
20143 /* Save away the inline definition; we will process it when the
20144 class is complete. */
20145 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
20146 DECL_PENDING_INLINE_P (fn
) = 1;
20148 /* We need to know that this was defined in the class, so that
20149 friend templates are handled correctly. */
20150 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
20152 /* Add FN to the queue of functions to be parsed later. */
20153 VEC_safe_push (tree
, gc
, unparsed_funs_with_definitions
, fn
);
20158 /* Parse a template-argument-list, as well as the trailing ">" (but
20159 not the opening ">"). See cp_parser_template_argument_list for the
20163 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
20167 tree saved_qualifying_scope
;
20168 tree saved_object_scope
;
20169 bool saved_greater_than_is_operator_p
;
20170 int saved_unevaluated_operand
;
20171 int saved_inhibit_evaluation_warnings
;
20175 When parsing a template-id, the first non-nested `>' is taken as
20176 the end of the template-argument-list rather than a greater-than
20178 saved_greater_than_is_operator_p
20179 = parser
->greater_than_is_operator_p
;
20180 parser
->greater_than_is_operator_p
= false;
20181 /* Parsing the argument list may modify SCOPE, so we save it
20183 saved_scope
= parser
->scope
;
20184 saved_qualifying_scope
= parser
->qualifying_scope
;
20185 saved_object_scope
= parser
->object_scope
;
20186 /* We need to evaluate the template arguments, even though this
20187 template-id may be nested within a "sizeof". */
20188 saved_unevaluated_operand
= cp_unevaluated_operand
;
20189 cp_unevaluated_operand
= 0;
20190 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
20191 c_inhibit_evaluation_warnings
= 0;
20192 /* Parse the template-argument-list itself. */
20193 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
20194 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
20195 arguments
= NULL_TREE
;
20197 arguments
= cp_parser_template_argument_list (parser
);
20198 /* Look for the `>' that ends the template-argument-list. If we find
20199 a '>>' instead, it's probably just a typo. */
20200 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
20202 if (cxx_dialect
!= cxx98
)
20204 /* In C++0x, a `>>' in a template argument list or cast
20205 expression is considered to be two separate `>'
20206 tokens. So, change the current token to a `>', but don't
20207 consume it: it will be consumed later when the outer
20208 template argument list (or cast expression) is parsed.
20209 Note that this replacement of `>' for `>>' is necessary
20210 even if we are parsing tentatively: in the tentative
20211 case, after calling
20212 cp_parser_enclosed_template_argument_list we will always
20213 throw away all of the template arguments and the first
20214 closing `>', either because the template argument list
20215 was erroneous or because we are replacing those tokens
20216 with a CPP_TEMPLATE_ID token. The second `>' (which will
20217 not have been thrown away) is needed either to close an
20218 outer template argument list or to complete a new-style
20220 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20221 token
->type
= CPP_GREATER
;
20223 else if (!saved_greater_than_is_operator_p
)
20225 /* If we're in a nested template argument list, the '>>' has
20226 to be a typo for '> >'. We emit the error message, but we
20227 continue parsing and we push a '>' as next token, so that
20228 the argument list will be parsed correctly. Note that the
20229 global source location is still on the token before the
20230 '>>', so we need to say explicitly where we want it. */
20231 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20232 error_at (token
->location
, "%<>>%> should be %<> >%> "
20233 "within a nested template argument list");
20235 token
->type
= CPP_GREATER
;
20239 /* If this is not a nested template argument list, the '>>'
20240 is a typo for '>'. Emit an error message and continue.
20241 Same deal about the token location, but here we can get it
20242 right by consuming the '>>' before issuing the diagnostic. */
20243 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
20244 error_at (token
->location
,
20245 "spurious %<>>%>, use %<>%> to terminate "
20246 "a template argument list");
20250 cp_parser_skip_to_end_of_template_parameter_list (parser
);
20251 /* The `>' token might be a greater-than operator again now. */
20252 parser
->greater_than_is_operator_p
20253 = saved_greater_than_is_operator_p
;
20254 /* Restore the SAVED_SCOPE. */
20255 parser
->scope
= saved_scope
;
20256 parser
->qualifying_scope
= saved_qualifying_scope
;
20257 parser
->object_scope
= saved_object_scope
;
20258 cp_unevaluated_operand
= saved_unevaluated_operand
;
20259 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
20264 /* MEMBER_FUNCTION is a member function, or a friend. If default
20265 arguments, or the body of the function have not yet been parsed,
20269 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
20271 /* If this member is a template, get the underlying
20273 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
20274 member_function
= DECL_TEMPLATE_RESULT (member_function
);
20276 /* There should not be any class definitions in progress at this
20277 point; the bodies of members are only parsed outside of all class
20279 gcc_assert (parser
->num_classes_being_defined
== 0);
20280 /* While we're parsing the member functions we might encounter more
20281 classes. We want to handle them right away, but we don't want
20282 them getting mixed up with functions that are currently in the
20284 push_unparsed_function_queues (parser
);
20286 /* Make sure that any template parameters are in scope. */
20287 maybe_begin_member_template_processing (member_function
);
20289 /* If the body of the function has not yet been parsed, parse it
20291 if (DECL_PENDING_INLINE_P (member_function
))
20293 tree function_scope
;
20294 cp_token_cache
*tokens
;
20296 /* The function is no longer pending; we are processing it. */
20297 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
20298 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
20299 DECL_PENDING_INLINE_P (member_function
) = 0;
20301 /* If this is a local class, enter the scope of the containing
20303 function_scope
= current_function_decl
;
20304 if (function_scope
)
20305 push_function_context ();
20307 /* Push the body of the function onto the lexer stack. */
20308 cp_parser_push_lexer_for_tokens (parser
, tokens
);
20310 /* Let the front end know that we going to be defining this
20312 start_preparsed_function (member_function
, NULL_TREE
,
20313 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
20315 /* Don't do access checking if it is a templated function. */
20316 if (processing_template_decl
)
20317 push_deferring_access_checks (dk_no_check
);
20319 /* Now, parse the body of the function. */
20320 cp_parser_function_definition_after_declarator (parser
,
20321 /*inline_p=*/true);
20323 if (processing_template_decl
)
20324 pop_deferring_access_checks ();
20326 /* Leave the scope of the containing function. */
20327 if (function_scope
)
20328 pop_function_context ();
20329 cp_parser_pop_lexer (parser
);
20332 /* Remove any template parameters from the symbol table. */
20333 maybe_end_member_template_processing ();
20335 /* Restore the queue. */
20336 pop_unparsed_function_queues (parser
);
20339 /* If DECL contains any default args, remember it on the unparsed
20340 functions queue. */
20343 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
20347 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
20349 probe
= TREE_CHAIN (probe
))
20350 if (TREE_PURPOSE (probe
))
20352 cp_default_arg_entry
*entry
20353 = VEC_safe_push (cp_default_arg_entry
, gc
,
20354 unparsed_funs_with_default_args
, NULL
);
20355 entry
->class_type
= current_class_type
;
20356 entry
->decl
= decl
;
20361 /* FN is a FUNCTION_DECL which may contains a parameter with an
20362 unparsed DEFAULT_ARG. Parse the default args now. This function
20363 assumes that the current scope is the scope in which the default
20364 argument should be processed. */
20367 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
20369 bool saved_local_variables_forbidden_p
;
20370 tree parm
, parmdecl
;
20372 /* While we're parsing the default args, we might (due to the
20373 statement expression extension) encounter more classes. We want
20374 to handle them right away, but we don't want them getting mixed
20375 up with default args that are currently in the queue. */
20376 push_unparsed_function_queues (parser
);
20378 /* Local variable names (and the `this' keyword) may not appear
20379 in a default argument. */
20380 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
20381 parser
->local_variables_forbidden_p
= true;
20383 push_defarg_context (fn
);
20385 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
20386 parmdecl
= DECL_ARGUMENTS (fn
);
20387 parm
&& parm
!= void_list_node
;
20388 parm
= TREE_CHAIN (parm
),
20389 parmdecl
= DECL_CHAIN (parmdecl
))
20391 cp_token_cache
*tokens
;
20392 tree default_arg
= TREE_PURPOSE (parm
);
20394 VEC(tree
,gc
) *insts
;
20401 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
20402 /* This can happen for a friend declaration for a function
20403 already declared with default arguments. */
20406 /* Push the saved tokens for the default argument onto the parser's
20408 tokens
= DEFARG_TOKENS (default_arg
);
20409 cp_parser_push_lexer_for_tokens (parser
, tokens
);
20411 start_lambda_scope (parmdecl
);
20413 /* Parse the assignment-expression. */
20414 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
20415 if (parsed_arg
== error_mark_node
)
20417 cp_parser_pop_lexer (parser
);
20421 if (!processing_template_decl
)
20422 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
20424 TREE_PURPOSE (parm
) = parsed_arg
;
20426 /* Update any instantiations we've already created. */
20427 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
20428 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
20429 TREE_PURPOSE (copy
) = parsed_arg
;
20431 finish_lambda_scope ();
20433 /* If the token stream has not been completely used up, then
20434 there was extra junk after the end of the default
20436 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
20437 cp_parser_error (parser
, "expected %<,%>");
20439 /* Revert to the main lexer. */
20440 cp_parser_pop_lexer (parser
);
20443 pop_defarg_context ();
20445 /* Make sure no default arg is missing. */
20446 check_default_args (fn
);
20448 /* Restore the state of local_variables_forbidden_p. */
20449 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
20451 /* Restore the queue. */
20452 pop_unparsed_function_queues (parser
);
20455 /* Parse the operand of `sizeof' (or a similar operator). Returns
20456 either a TYPE or an expression, depending on the form of the
20457 input. The KEYWORD indicates which kind of expression we have
20461 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
20463 tree expr
= NULL_TREE
;
20464 const char *saved_message
;
20466 bool saved_integral_constant_expression_p
;
20467 bool saved_non_integral_constant_expression_p
;
20468 bool pack_expansion_p
= false;
20470 /* Types cannot be defined in a `sizeof' expression. Save away the
20472 saved_message
= parser
->type_definition_forbidden_message
;
20473 /* And create the new one. */
20474 tmp
= concat ("types may not be defined in %<",
20475 IDENTIFIER_POINTER (ridpointers
[keyword
]),
20476 "%> expressions", NULL
);
20477 parser
->type_definition_forbidden_message
= tmp
;
20479 /* The restrictions on constant-expressions do not apply inside
20480 sizeof expressions. */
20481 saved_integral_constant_expression_p
20482 = parser
->integral_constant_expression_p
;
20483 saved_non_integral_constant_expression_p
20484 = parser
->non_integral_constant_expression_p
;
20485 parser
->integral_constant_expression_p
= false;
20487 /* If it's a `...', then we are computing the length of a parameter
20489 if (keyword
== RID_SIZEOF
20490 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20492 /* Consume the `...'. */
20493 cp_lexer_consume_token (parser
->lexer
);
20494 maybe_warn_variadic_templates ();
20496 /* Note that this is an expansion. */
20497 pack_expansion_p
= true;
20500 /* Do not actually evaluate the expression. */
20501 ++cp_unevaluated_operand
;
20502 ++c_inhibit_evaluation_warnings
;
20503 /* If it's a `(', then we might be looking at the type-id
20505 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
20508 bool saved_in_type_id_in_expr_p
;
20510 /* We can't be sure yet whether we're looking at a type-id or an
20512 cp_parser_parse_tentatively (parser
);
20513 /* Consume the `('. */
20514 cp_lexer_consume_token (parser
->lexer
);
20515 /* Parse the type-id. */
20516 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20517 parser
->in_type_id_in_expr_p
= true;
20518 type
= cp_parser_type_id (parser
);
20519 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20520 /* Now, look for the trailing `)'. */
20521 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20522 /* If all went well, then we're done. */
20523 if (cp_parser_parse_definitely (parser
))
20525 cp_decl_specifier_seq decl_specs
;
20527 /* Build a trivial decl-specifier-seq. */
20528 clear_decl_specs (&decl_specs
);
20529 decl_specs
.type
= type
;
20531 /* Call grokdeclarator to figure out what type this is. */
20532 expr
= grokdeclarator (NULL
,
20536 /*attrlist=*/NULL
);
20540 /* If the type-id production did not work out, then we must be
20541 looking at the unary-expression production. */
20543 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
20544 /*cast_p=*/false, NULL
);
20546 if (pack_expansion_p
)
20547 /* Build a pack expansion. */
20548 expr
= make_pack_expansion (expr
);
20550 /* Go back to evaluating expressions. */
20551 --cp_unevaluated_operand
;
20552 --c_inhibit_evaluation_warnings
;
20554 /* Free the message we created. */
20556 /* And restore the old one. */
20557 parser
->type_definition_forbidden_message
= saved_message
;
20558 parser
->integral_constant_expression_p
20559 = saved_integral_constant_expression_p
;
20560 parser
->non_integral_constant_expression_p
20561 = saved_non_integral_constant_expression_p
;
20566 /* If the current declaration has no declarator, return true. */
20569 cp_parser_declares_only_class_p (cp_parser
*parser
)
20571 /* If the next token is a `;' or a `,' then there is no
20573 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
20574 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
20577 /* Update the DECL_SPECS to reflect the storage class indicated by
20581 cp_parser_set_storage_class (cp_parser
*parser
,
20582 cp_decl_specifier_seq
*decl_specs
,
20584 location_t location
)
20586 cp_storage_class storage_class
;
20588 if (parser
->in_unbraced_linkage_specification_p
)
20590 error_at (location
, "invalid use of %qD in linkage specification",
20591 ridpointers
[keyword
]);
20594 else if (decl_specs
->storage_class
!= sc_none
)
20596 decl_specs
->conflicting_specifiers_p
= true;
20600 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
20601 && decl_specs
->specs
[(int) ds_thread
])
20603 error_at (location
, "%<__thread%> before %qD", ridpointers
[keyword
]);
20604 decl_specs
->specs
[(int) ds_thread
] = 0;
20610 storage_class
= sc_auto
;
20613 storage_class
= sc_register
;
20616 storage_class
= sc_static
;
20619 storage_class
= sc_extern
;
20622 storage_class
= sc_mutable
;
20625 gcc_unreachable ();
20627 decl_specs
->storage_class
= storage_class
;
20629 /* A storage class specifier cannot be applied alongside a typedef
20630 specifier. If there is a typedef specifier present then set
20631 conflicting_specifiers_p which will trigger an error later
20632 on in grokdeclarator. */
20633 if (decl_specs
->specs
[(int)ds_typedef
])
20634 decl_specs
->conflicting_specifiers_p
= true;
20637 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20638 is true, the type is a user-defined type; otherwise it is a
20639 built-in type specified by a keyword. */
20642 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
20644 location_t location
,
20645 bool user_defined_p
)
20647 decl_specs
->any_specifiers_p
= true;
20649 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20650 (with, for example, in "typedef int wchar_t;") we remember that
20651 this is what happened. In system headers, we ignore these
20652 declarations so that G++ can work with system headers that are not
20654 if (decl_specs
->specs
[(int) ds_typedef
]
20656 && (type_spec
== boolean_type_node
20657 || type_spec
== char16_type_node
20658 || type_spec
== char32_type_node
20659 || type_spec
== wchar_type_node
)
20660 && (decl_specs
->type
20661 || decl_specs
->specs
[(int) ds_long
]
20662 || decl_specs
->specs
[(int) ds_short
]
20663 || decl_specs
->specs
[(int) ds_unsigned
]
20664 || decl_specs
->specs
[(int) ds_signed
]))
20666 decl_specs
->redefined_builtin_type
= type_spec
;
20667 if (!decl_specs
->type
)
20669 decl_specs
->type
= type_spec
;
20670 decl_specs
->user_defined_type_p
= false;
20671 decl_specs
->type_location
= location
;
20674 else if (decl_specs
->type
)
20675 decl_specs
->multiple_types_p
= true;
20678 decl_specs
->type
= type_spec
;
20679 decl_specs
->user_defined_type_p
= user_defined_p
;
20680 decl_specs
->redefined_builtin_type
= NULL_TREE
;
20681 decl_specs
->type_location
= location
;
20685 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20686 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20689 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
20691 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
20694 /* Issue an error message indicating that TOKEN_DESC was expected.
20695 If KEYWORD is true, it indicated this function is called by
20696 cp_parser_require_keword and the required token can only be
20697 a indicated keyword. */
20700 cp_parser_required_error (cp_parser
*parser
,
20701 required_token token_desc
,
20704 switch (token_desc
)
20707 cp_parser_error (parser
, "expected %<new%>");
20710 cp_parser_error (parser
, "expected %<delete%>");
20713 cp_parser_error (parser
, "expected %<return%>");
20716 cp_parser_error (parser
, "expected %<while%>");
20719 cp_parser_error (parser
, "expected %<extern%>");
20721 case RT_STATIC_ASSERT
:
20722 cp_parser_error (parser
, "expected %<static_assert%>");
20725 cp_parser_error (parser
, "expected %<decltype%>");
20728 cp_parser_error (parser
, "expected %<operator%>");
20731 cp_parser_error (parser
, "expected %<class%>");
20734 cp_parser_error (parser
, "expected %<template%>");
20737 cp_parser_error (parser
, "expected %<namespace%>");
20740 cp_parser_error (parser
, "expected %<using%>");
20743 cp_parser_error (parser
, "expected %<asm%>");
20746 cp_parser_error (parser
, "expected %<try%>");
20749 cp_parser_error (parser
, "expected %<catch%>");
20752 cp_parser_error (parser
, "expected %<throw%>");
20755 cp_parser_error (parser
, "expected %<__label__%>");
20758 cp_parser_error (parser
, "expected %<@try%>");
20760 case RT_AT_SYNCHRONIZED
:
20761 cp_parser_error (parser
, "expected %<@synchronized%>");
20764 cp_parser_error (parser
, "expected %<@throw%>");
20771 switch (token_desc
)
20774 cp_parser_error (parser
, "expected %<;%>");
20776 case RT_OPEN_PAREN
:
20777 cp_parser_error (parser
, "expected %<(%>");
20779 case RT_CLOSE_BRACE
:
20780 cp_parser_error (parser
, "expected %<}%>");
20782 case RT_OPEN_BRACE
:
20783 cp_parser_error (parser
, "expected %<{%>");
20785 case RT_CLOSE_SQUARE
:
20786 cp_parser_error (parser
, "expected %<]%>");
20788 case RT_OPEN_SQUARE
:
20789 cp_parser_error (parser
, "expected %<[%>");
20792 cp_parser_error (parser
, "expected %<,%>");
20795 cp_parser_error (parser
, "expected %<::%>");
20798 cp_parser_error (parser
, "expected %<<%>");
20801 cp_parser_error (parser
, "expected %<>%>");
20804 cp_parser_error (parser
, "expected %<=%>");
20807 cp_parser_error (parser
, "expected %<...%>");
20810 cp_parser_error (parser
, "expected %<*%>");
20813 cp_parser_error (parser
, "expected %<~%>");
20816 cp_parser_error (parser
, "expected %<:%>");
20818 case RT_COLON_SCOPE
:
20819 cp_parser_error (parser
, "expected %<:%> or %<::%>");
20821 case RT_CLOSE_PAREN
:
20822 cp_parser_error (parser
, "expected %<)%>");
20824 case RT_COMMA_CLOSE_PAREN
:
20825 cp_parser_error (parser
, "expected %<,%> or %<)%>");
20827 case RT_PRAGMA_EOL
:
20828 cp_parser_error (parser
, "expected end of line");
20831 cp_parser_error (parser
, "expected identifier");
20834 cp_parser_error (parser
, "expected selection-statement");
20836 case RT_INTERATION
:
20837 cp_parser_error (parser
, "expected iteration-statement");
20840 cp_parser_error (parser
, "expected jump-statement");
20843 cp_parser_error (parser
, "expected class-key");
20845 case RT_CLASS_TYPENAME_TEMPLATE
:
20846 cp_parser_error (parser
,
20847 "expected %<class%>, %<typename%>, or %<template%>");
20850 gcc_unreachable ();
20854 gcc_unreachable ();
20859 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20860 issue an error message indicating that TOKEN_DESC was expected.
20862 Returns the token consumed, if the token had the appropriate type.
20863 Otherwise, returns NULL. */
20866 cp_parser_require (cp_parser
* parser
,
20867 enum cpp_ttype type
,
20868 required_token token_desc
)
20870 if (cp_lexer_next_token_is (parser
->lexer
, type
))
20871 return cp_lexer_consume_token (parser
->lexer
);
20874 /* Output the MESSAGE -- unless we're parsing tentatively. */
20875 if (!cp_parser_simulate_error (parser
))
20876 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
20881 /* An error message is produced if the next token is not '>'.
20882 All further tokens are skipped until the desired token is
20883 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20886 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
20888 /* Current level of '< ... >'. */
20889 unsigned level
= 0;
20890 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20891 unsigned nesting_depth
= 0;
20893 /* Are we ready, yet? If not, issue error message. */
20894 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
20897 /* Skip tokens until the desired token is found. */
20900 /* Peek at the next token. */
20901 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
20904 if (!nesting_depth
)
20909 if (cxx_dialect
== cxx98
)
20910 /* C++0x views the `>>' operator as two `>' tokens, but
20913 else if (!nesting_depth
&& level
-- == 0)
20915 /* We've hit a `>>' where the first `>' closes the
20916 template argument list, and the second `>' is
20917 spurious. Just consume the `>>' and stop; we've
20918 already produced at least one error. */
20919 cp_lexer_consume_token (parser
->lexer
);
20922 /* Fall through for C++0x, so we handle the second `>' in
20926 if (!nesting_depth
&& level
-- == 0)
20928 /* We've reached the token we want, consume it and stop. */
20929 cp_lexer_consume_token (parser
->lexer
);
20934 case CPP_OPEN_PAREN
:
20935 case CPP_OPEN_SQUARE
:
20939 case CPP_CLOSE_PAREN
:
20940 case CPP_CLOSE_SQUARE
:
20941 if (nesting_depth
-- == 0)
20946 case CPP_PRAGMA_EOL
:
20947 case CPP_SEMICOLON
:
20948 case CPP_OPEN_BRACE
:
20949 case CPP_CLOSE_BRACE
:
20950 /* The '>' was probably forgotten, don't look further. */
20957 /* Consume this token. */
20958 cp_lexer_consume_token (parser
->lexer
);
20962 /* If the next token is the indicated keyword, consume it. Otherwise,
20963 issue an error message indicating that TOKEN_DESC was expected.
20965 Returns the token consumed, if the token had the appropriate type.
20966 Otherwise, returns NULL. */
20969 cp_parser_require_keyword (cp_parser
* parser
,
20971 required_token token_desc
)
20973 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
20975 if (token
&& token
->keyword
!= keyword
)
20977 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
20984 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20985 function-definition. */
20988 cp_parser_token_starts_function_definition_p (cp_token
* token
)
20990 return (/* An ordinary function-body begins with an `{'. */
20991 token
->type
== CPP_OPEN_BRACE
20992 /* A ctor-initializer begins with a `:'. */
20993 || token
->type
== CPP_COLON
20994 /* A function-try-block begins with `try'. */
20995 || token
->keyword
== RID_TRY
20996 /* The named return value extension begins with `return'. */
20997 || token
->keyword
== RID_RETURN
);
21000 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21004 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
21008 token
= cp_lexer_peek_token (parser
->lexer
);
21009 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
21012 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21013 C++0x) ending a template-argument. */
21016 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
21020 token
= cp_lexer_peek_token (parser
->lexer
);
21021 return (token
->type
== CPP_COMMA
21022 || token
->type
== CPP_GREATER
21023 || token
->type
== CPP_ELLIPSIS
21024 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
21027 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21028 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
21031 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
21036 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
21037 if (token
->type
== CPP_LESS
)
21039 /* Check for the sequence `<::' in the original code. It would be lexed as
21040 `[:', where `[' is a digraph, and there is no whitespace before
21042 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
21045 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
21046 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
21052 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21053 or none_type otherwise. */
21055 static enum tag_types
21056 cp_parser_token_is_class_key (cp_token
* token
)
21058 switch (token
->keyword
)
21063 return record_type
;
21072 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
21075 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
21077 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
21078 permerror (input_location
, "%qs tag used in naming %q#T",
21079 class_key
== union_type
? "union"
21080 : class_key
== record_type
? "struct" : "class",
21084 /* Issue an error message if DECL is redeclared with different
21085 access than its original declaration [class.access.spec/3].
21086 This applies to nested classes and nested class templates.
21090 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
21092 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
21095 if ((TREE_PRIVATE (decl
)
21096 != (current_access_specifier
== access_private_node
))
21097 || (TREE_PROTECTED (decl
)
21098 != (current_access_specifier
== access_protected_node
)))
21099 error_at (location
, "%qD redeclared with different access", decl
);
21102 /* Look for the `template' keyword, as a syntactic disambiguator.
21103 Return TRUE iff it is present, in which case it will be
21107 cp_parser_optional_template_keyword (cp_parser
*parser
)
21109 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
21111 /* The `template' keyword can only be used within templates;
21112 outside templates the parser can always figure out what is a
21113 template and what is not. */
21114 if (!processing_template_decl
)
21116 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21117 error_at (token
->location
,
21118 "%<template%> (as a disambiguator) is only allowed "
21119 "within templates");
21120 /* If this part of the token stream is rescanned, the same
21121 error message would be generated. So, we purge the token
21122 from the stream. */
21123 cp_lexer_purge_token (parser
->lexer
);
21128 /* Consume the `template' keyword. */
21129 cp_lexer_consume_token (parser
->lexer
);
21137 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
21138 set PARSER->SCOPE, and perform other related actions. */
21141 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
21144 struct tree_check
*check_value
;
21145 deferred_access_check
*chk
;
21146 VEC (deferred_access_check
,gc
) *checks
;
21148 /* Get the stored value. */
21149 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
21150 /* Perform any access checks that were deferred. */
21151 checks
= check_value
->checks
;
21154 FOR_EACH_VEC_ELT (deferred_access_check
, checks
, i
, chk
)
21155 perform_or_defer_access_check (chk
->binfo
,
21159 /* Set the scope from the stored value. */
21160 parser
->scope
= check_value
->value
;
21161 parser
->qualifying_scope
= check_value
->qualifying_scope
;
21162 parser
->object_scope
= NULL_TREE
;
21165 /* Consume tokens up through a non-nested END token. Returns TRUE if we
21166 encounter the end of a block before what we were looking for. */
21169 cp_parser_cache_group (cp_parser
*parser
,
21170 enum cpp_ttype end
,
21175 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21177 /* Abort a parenthesized expression if we encounter a semicolon. */
21178 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
21179 && token
->type
== CPP_SEMICOLON
)
21181 /* If we've reached the end of the file, stop. */
21182 if (token
->type
== CPP_EOF
21183 || (end
!= CPP_PRAGMA_EOL
21184 && token
->type
== CPP_PRAGMA_EOL
))
21186 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
21187 /* We've hit the end of an enclosing block, so there's been some
21188 kind of syntax error. */
21191 /* Consume the token. */
21192 cp_lexer_consume_token (parser
->lexer
);
21193 /* See if it starts a new group. */
21194 if (token
->type
== CPP_OPEN_BRACE
)
21196 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
21197 /* In theory this should probably check end == '}', but
21198 cp_parser_save_member_function_body needs it to exit
21199 after either '}' or ')' when called with ')'. */
21203 else if (token
->type
== CPP_OPEN_PAREN
)
21205 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
21206 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
21209 else if (token
->type
== CPP_PRAGMA
)
21210 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
21211 else if (token
->type
== end
)
21216 /* Begin parsing tentatively. We always save tokens while parsing
21217 tentatively so that if the tentative parsing fails we can restore the
21221 cp_parser_parse_tentatively (cp_parser
* parser
)
21223 /* Enter a new parsing context. */
21224 parser
->context
= cp_parser_context_new (parser
->context
);
21225 /* Begin saving tokens. */
21226 cp_lexer_save_tokens (parser
->lexer
);
21227 /* In order to avoid repetitive access control error messages,
21228 access checks are queued up until we are no longer parsing
21230 push_deferring_access_checks (dk_deferred
);
21233 /* Commit to the currently active tentative parse. */
21236 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
21238 cp_parser_context
*context
;
21241 /* Mark all of the levels as committed. */
21242 lexer
= parser
->lexer
;
21243 for (context
= parser
->context
; context
->next
; context
= context
->next
)
21245 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
21247 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
21248 while (!cp_lexer_saving_tokens (lexer
))
21249 lexer
= lexer
->next
;
21250 cp_lexer_commit_tokens (lexer
);
21254 /* Abort the currently active tentative parse. All consumed tokens
21255 will be rolled back, and no diagnostics will be issued. */
21258 cp_parser_abort_tentative_parse (cp_parser
* parser
)
21260 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
21261 || errorcount
> 0);
21262 cp_parser_simulate_error (parser
);
21263 /* Now, pretend that we want to see if the construct was
21264 successfully parsed. */
21265 cp_parser_parse_definitely (parser
);
21268 /* Stop parsing tentatively. If a parse error has occurred, restore the
21269 token stream. Otherwise, commit to the tokens we have consumed.
21270 Returns true if no error occurred; false otherwise. */
21273 cp_parser_parse_definitely (cp_parser
* parser
)
21275 bool error_occurred
;
21276 cp_parser_context
*context
;
21278 /* Remember whether or not an error occurred, since we are about to
21279 destroy that information. */
21280 error_occurred
= cp_parser_error_occurred (parser
);
21281 /* Remove the topmost context from the stack. */
21282 context
= parser
->context
;
21283 parser
->context
= context
->next
;
21284 /* If no parse errors occurred, commit to the tentative parse. */
21285 if (!error_occurred
)
21287 /* Commit to the tokens read tentatively, unless that was
21289 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
21290 cp_lexer_commit_tokens (parser
->lexer
);
21292 pop_to_parent_deferring_access_checks ();
21294 /* Otherwise, if errors occurred, roll back our state so that things
21295 are just as they were before we began the tentative parse. */
21298 cp_lexer_rollback_tokens (parser
->lexer
);
21299 pop_deferring_access_checks ();
21301 /* Add the context to the front of the free list. */
21302 context
->next
= cp_parser_context_free_list
;
21303 cp_parser_context_free_list
= context
;
21305 return !error_occurred
;
21308 /* Returns true if we are parsing tentatively and are not committed to
21309 this tentative parse. */
21312 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
21314 return (cp_parser_parsing_tentatively (parser
)
21315 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
21318 /* Returns nonzero iff an error has occurred during the most recent
21319 tentative parse. */
21322 cp_parser_error_occurred (cp_parser
* parser
)
21324 return (cp_parser_parsing_tentatively (parser
)
21325 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
21328 /* Returns nonzero if GNU extensions are allowed. */
21331 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
21333 return parser
->allow_gnu_extensions_p
;
21336 /* Objective-C++ Productions */
21339 /* Parse an Objective-C expression, which feeds into a primary-expression
21343 objc-message-expression
21344 objc-string-literal
21345 objc-encode-expression
21346 objc-protocol-expression
21347 objc-selector-expression
21349 Returns a tree representation of the expression. */
21352 cp_parser_objc_expression (cp_parser
* parser
)
21354 /* Try to figure out what kind of declaration is present. */
21355 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
21359 case CPP_OPEN_SQUARE
:
21360 return cp_parser_objc_message_expression (parser
);
21362 case CPP_OBJC_STRING
:
21363 kwd
= cp_lexer_consume_token (parser
->lexer
);
21364 return objc_build_string_object (kwd
->u
.value
);
21367 switch (kwd
->keyword
)
21369 case RID_AT_ENCODE
:
21370 return cp_parser_objc_encode_expression (parser
);
21372 case RID_AT_PROTOCOL
:
21373 return cp_parser_objc_protocol_expression (parser
);
21375 case RID_AT_SELECTOR
:
21376 return cp_parser_objc_selector_expression (parser
);
21382 error_at (kwd
->location
,
21383 "misplaced %<@%D%> Objective-C++ construct",
21385 cp_parser_skip_to_end_of_block_or_statement (parser
);
21388 return error_mark_node
;
21391 /* Parse an Objective-C message expression.
21393 objc-message-expression:
21394 [ objc-message-receiver objc-message-args ]
21396 Returns a representation of an Objective-C message. */
21399 cp_parser_objc_message_expression (cp_parser
* parser
)
21401 tree receiver
, messageargs
;
21403 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
21404 receiver
= cp_parser_objc_message_receiver (parser
);
21405 messageargs
= cp_parser_objc_message_args (parser
);
21406 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21408 return objc_build_message_expr (receiver
, messageargs
);
21411 /* Parse an objc-message-receiver.
21413 objc-message-receiver:
21415 simple-type-specifier
21417 Returns a representation of the type or expression. */
21420 cp_parser_objc_message_receiver (cp_parser
* parser
)
21424 /* An Objective-C message receiver may be either (1) a type
21425 or (2) an expression. */
21426 cp_parser_parse_tentatively (parser
);
21427 rcv
= cp_parser_expression (parser
, false, NULL
);
21429 if (cp_parser_parse_definitely (parser
))
21432 rcv
= cp_parser_simple_type_specifier (parser
,
21433 /*decl_specs=*/NULL
,
21434 CP_PARSER_FLAGS_NONE
);
21436 return objc_get_class_reference (rcv
);
21439 /* Parse the arguments and selectors comprising an Objective-C message.
21444 objc-selector-args , objc-comma-args
21446 objc-selector-args:
21447 objc-selector [opt] : assignment-expression
21448 objc-selector-args objc-selector [opt] : assignment-expression
21451 assignment-expression
21452 objc-comma-args , assignment-expression
21454 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21455 selector arguments and TREE_VALUE containing a list of comma
21459 cp_parser_objc_message_args (cp_parser
* parser
)
21461 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
21462 bool maybe_unary_selector_p
= true;
21463 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21465 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
21467 tree selector
= NULL_TREE
, arg
;
21469 if (token
->type
!= CPP_COLON
)
21470 selector
= cp_parser_objc_selector (parser
);
21472 /* Detect if we have a unary selector. */
21473 if (maybe_unary_selector_p
21474 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
21475 return build_tree_list (selector
, NULL_TREE
);
21477 maybe_unary_selector_p
= false;
21478 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
21479 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
21482 = chainon (sel_args
,
21483 build_tree_list (selector
, arg
));
21485 token
= cp_lexer_peek_token (parser
->lexer
);
21488 /* Handle non-selector arguments, if any. */
21489 while (token
->type
== CPP_COMMA
)
21493 cp_lexer_consume_token (parser
->lexer
);
21494 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
21497 = chainon (addl_args
,
21498 build_tree_list (NULL_TREE
, arg
));
21500 token
= cp_lexer_peek_token (parser
->lexer
);
21503 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
21505 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
21506 return build_tree_list (error_mark_node
, error_mark_node
);
21509 return build_tree_list (sel_args
, addl_args
);
21512 /* Parse an Objective-C encode expression.
21514 objc-encode-expression:
21515 @encode objc-typename
21517 Returns an encoded representation of the type argument. */
21520 cp_parser_objc_encode_expression (cp_parser
* parser
)
21525 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
21526 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21527 token
= cp_lexer_peek_token (parser
->lexer
);
21528 type
= complete_type (cp_parser_type_id (parser
));
21529 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21533 error_at (token
->location
,
21534 "%<@encode%> must specify a type as an argument");
21535 return error_mark_node
;
21538 /* This happens if we find @encode(T) (where T is a template
21539 typename or something dependent on a template typename) when
21540 parsing a template. In that case, we can't compile it
21541 immediately, but we rather create an AT_ENCODE_EXPR which will
21542 need to be instantiated when the template is used.
21544 if (dependent_type_p (type
))
21546 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
21547 TREE_READONLY (value
) = 1;
21551 return objc_build_encode_expr (type
);
21554 /* Parse an Objective-C @defs expression. */
21557 cp_parser_objc_defs_expression (cp_parser
*parser
)
21561 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
21562 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21563 name
= cp_parser_identifier (parser
);
21564 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21566 return objc_get_class_ivars (name
);
21569 /* Parse an Objective-C protocol expression.
21571 objc-protocol-expression:
21572 @protocol ( identifier )
21574 Returns a representation of the protocol expression. */
21577 cp_parser_objc_protocol_expression (cp_parser
* parser
)
21581 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
21582 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21583 proto
= cp_parser_identifier (parser
);
21584 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21586 return objc_build_protocol_expr (proto
);
21589 /* Parse an Objective-C selector expression.
21591 objc-selector-expression:
21592 @selector ( objc-method-signature )
21594 objc-method-signature:
21600 objc-selector-seq objc-selector :
21602 Returns a representation of the method selector. */
21605 cp_parser_objc_selector_expression (cp_parser
* parser
)
21607 tree sel_seq
= NULL_TREE
;
21608 bool maybe_unary_selector_p
= true;
21610 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21612 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
21613 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21614 token
= cp_lexer_peek_token (parser
->lexer
);
21616 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
21617 || token
->type
== CPP_SCOPE
)
21619 tree selector
= NULL_TREE
;
21621 if (token
->type
!= CPP_COLON
21622 || token
->type
== CPP_SCOPE
)
21623 selector
= cp_parser_objc_selector (parser
);
21625 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
21626 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
21628 /* Detect if we have a unary selector. */
21629 if (maybe_unary_selector_p
)
21631 sel_seq
= selector
;
21632 goto finish_selector
;
21636 cp_parser_error (parser
, "expected %<:%>");
21639 maybe_unary_selector_p
= false;
21640 token
= cp_lexer_consume_token (parser
->lexer
);
21642 if (token
->type
== CPP_SCOPE
)
21645 = chainon (sel_seq
,
21646 build_tree_list (selector
, NULL_TREE
));
21648 = chainon (sel_seq
,
21649 build_tree_list (NULL_TREE
, NULL_TREE
));
21653 = chainon (sel_seq
,
21654 build_tree_list (selector
, NULL_TREE
));
21656 token
= cp_lexer_peek_token (parser
->lexer
);
21660 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21662 return objc_build_selector_expr (loc
, sel_seq
);
21665 /* Parse a list of identifiers.
21667 objc-identifier-list:
21669 objc-identifier-list , identifier
21671 Returns a TREE_LIST of identifier nodes. */
21674 cp_parser_objc_identifier_list (cp_parser
* parser
)
21680 identifier
= cp_parser_identifier (parser
);
21681 if (identifier
== error_mark_node
)
21682 return error_mark_node
;
21684 list
= build_tree_list (NULL_TREE
, identifier
);
21685 sep
= cp_lexer_peek_token (parser
->lexer
);
21687 while (sep
->type
== CPP_COMMA
)
21689 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
21690 identifier
= cp_parser_identifier (parser
);
21691 if (identifier
== error_mark_node
)
21694 list
= chainon (list
, build_tree_list (NULL_TREE
,
21696 sep
= cp_lexer_peek_token (parser
->lexer
);
21702 /* Parse an Objective-C alias declaration.
21704 objc-alias-declaration:
21705 @compatibility_alias identifier identifier ;
21707 This function registers the alias mapping with the Objective-C front end.
21708 It returns nothing. */
21711 cp_parser_objc_alias_declaration (cp_parser
* parser
)
21715 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
21716 alias
= cp_parser_identifier (parser
);
21717 orig
= cp_parser_identifier (parser
);
21718 objc_declare_alias (alias
, orig
);
21719 cp_parser_consume_semicolon_at_end_of_statement (parser
);
21722 /* Parse an Objective-C class forward-declaration.
21724 objc-class-declaration:
21725 @class objc-identifier-list ;
21727 The function registers the forward declarations with the Objective-C
21728 front end. It returns nothing. */
21731 cp_parser_objc_class_declaration (cp_parser
* parser
)
21733 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
21738 id
= cp_parser_identifier (parser
);
21739 if (id
== error_mark_node
)
21742 objc_declare_class (id
);
21744 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21745 cp_lexer_consume_token (parser
->lexer
);
21749 cp_parser_consume_semicolon_at_end_of_statement (parser
);
21752 /* Parse a list of Objective-C protocol references.
21754 objc-protocol-refs-opt:
21755 objc-protocol-refs [opt]
21757 objc-protocol-refs:
21758 < objc-identifier-list >
21760 Returns a TREE_LIST of identifiers, if any. */
21763 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
21765 tree protorefs
= NULL_TREE
;
21767 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
21769 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
21770 protorefs
= cp_parser_objc_identifier_list (parser
);
21771 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
21777 /* Parse a Objective-C visibility specification. */
21780 cp_parser_objc_visibility_spec (cp_parser
* parser
)
21782 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
21784 switch (vis
->keyword
)
21786 case RID_AT_PRIVATE
:
21787 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
21789 case RID_AT_PROTECTED
:
21790 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
21792 case RID_AT_PUBLIC
:
21793 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
21795 case RID_AT_PACKAGE
:
21796 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
21802 /* Eat '@private'/'@protected'/'@public'. */
21803 cp_lexer_consume_token (parser
->lexer
);
21806 /* Parse an Objective-C method type. Return 'true' if it is a class
21807 (+) method, and 'false' if it is an instance (-) method. */
21810 cp_parser_objc_method_type (cp_parser
* parser
)
21812 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
21818 /* Parse an Objective-C protocol qualifier. */
21821 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
21823 tree quals
= NULL_TREE
, node
;
21824 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21826 node
= token
->u
.value
;
21828 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
21829 && (node
== ridpointers
[(int) RID_IN
]
21830 || node
== ridpointers
[(int) RID_OUT
]
21831 || node
== ridpointers
[(int) RID_INOUT
]
21832 || node
== ridpointers
[(int) RID_BYCOPY
]
21833 || node
== ridpointers
[(int) RID_BYREF
]
21834 || node
== ridpointers
[(int) RID_ONEWAY
]))
21836 quals
= tree_cons (NULL_TREE
, node
, quals
);
21837 cp_lexer_consume_token (parser
->lexer
);
21838 token
= cp_lexer_peek_token (parser
->lexer
);
21839 node
= token
->u
.value
;
21845 /* Parse an Objective-C typename. */
21848 cp_parser_objc_typename (cp_parser
* parser
)
21850 tree type_name
= NULL_TREE
;
21852 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
21854 tree proto_quals
, cp_type
= NULL_TREE
;
21856 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
21857 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
21859 /* An ObjC type name may consist of just protocol qualifiers, in which
21860 case the type shall default to 'id'. */
21861 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
21863 cp_type
= cp_parser_type_id (parser
);
21865 /* If the type could not be parsed, an error has already
21866 been produced. For error recovery, behave as if it had
21867 not been specified, which will use the default type
21869 if (cp_type
== error_mark_node
)
21871 cp_type
= NULL_TREE
;
21872 /* We need to skip to the closing parenthesis as
21873 cp_parser_type_id() does not seem to do it for
21875 cp_parser_skip_to_closing_parenthesis (parser
,
21876 /*recovering=*/true,
21877 /*or_comma=*/false,
21878 /*consume_paren=*/false);
21882 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21883 type_name
= build_tree_list (proto_quals
, cp_type
);
21889 /* Check to see if TYPE refers to an Objective-C selector name. */
21892 cp_parser_objc_selector_p (enum cpp_ttype type
)
21894 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
21895 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
21896 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
21897 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
21898 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
21901 /* Parse an Objective-C selector. */
21904 cp_parser_objc_selector (cp_parser
* parser
)
21906 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
21908 if (!cp_parser_objc_selector_p (token
->type
))
21910 error_at (token
->location
, "invalid Objective-C++ selector name");
21911 return error_mark_node
;
21914 /* C++ operator names are allowed to appear in ObjC selectors. */
21915 switch (token
->type
)
21917 case CPP_AND_AND
: return get_identifier ("and");
21918 case CPP_AND_EQ
: return get_identifier ("and_eq");
21919 case CPP_AND
: return get_identifier ("bitand");
21920 case CPP_OR
: return get_identifier ("bitor");
21921 case CPP_COMPL
: return get_identifier ("compl");
21922 case CPP_NOT
: return get_identifier ("not");
21923 case CPP_NOT_EQ
: return get_identifier ("not_eq");
21924 case CPP_OR_OR
: return get_identifier ("or");
21925 case CPP_OR_EQ
: return get_identifier ("or_eq");
21926 case CPP_XOR
: return get_identifier ("xor");
21927 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
21928 default: return token
->u
.value
;
21932 /* Parse an Objective-C params list. */
21935 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
21937 tree params
= NULL_TREE
;
21938 bool maybe_unary_selector_p
= true;
21939 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21941 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
21943 tree selector
= NULL_TREE
, type_name
, identifier
;
21944 tree parm_attr
= NULL_TREE
;
21946 if (token
->keyword
== RID_ATTRIBUTE
)
21949 if (token
->type
!= CPP_COLON
)
21950 selector
= cp_parser_objc_selector (parser
);
21952 /* Detect if we have a unary selector. */
21953 if (maybe_unary_selector_p
21954 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
21956 params
= selector
; /* Might be followed by attributes. */
21960 maybe_unary_selector_p
= false;
21961 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
21963 /* Something went quite wrong. There should be a colon
21964 here, but there is not. Stop parsing parameters. */
21967 type_name
= cp_parser_objc_typename (parser
);
21968 /* New ObjC allows attributes on parameters too. */
21969 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
21970 parm_attr
= cp_parser_attributes_opt (parser
);
21971 identifier
= cp_parser_identifier (parser
);
21975 objc_build_keyword_decl (selector
,
21980 token
= cp_lexer_peek_token (parser
->lexer
);
21983 if (params
== NULL_TREE
)
21985 cp_parser_error (parser
, "objective-c++ method declaration is expected");
21986 return error_mark_node
;
21989 /* We allow tail attributes for the method. */
21990 if (token
->keyword
== RID_ATTRIBUTE
)
21992 *attributes
= cp_parser_attributes_opt (parser
);
21993 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21994 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21996 cp_parser_error (parser
,
21997 "method attributes must be specified at the end");
21998 return error_mark_node
;
22001 if (params
== NULL_TREE
)
22003 cp_parser_error (parser
, "objective-c++ method declaration is expected");
22004 return error_mark_node
;
22009 /* Parse the non-keyword Objective-C params. */
22012 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
22015 tree params
= make_node (TREE_LIST
);
22016 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22017 *ellipsisp
= false; /* Initially, assume no ellipsis. */
22019 while (token
->type
== CPP_COMMA
)
22021 cp_parameter_declarator
*parmdecl
;
22024 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
22025 token
= cp_lexer_peek_token (parser
->lexer
);
22027 if (token
->type
== CPP_ELLIPSIS
)
22029 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
22031 token
= cp_lexer_peek_token (parser
->lexer
);
22035 /* TODO: parse attributes for tail parameters. */
22036 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
22037 parm
= grokdeclarator (parmdecl
->declarator
,
22038 &parmdecl
->decl_specifiers
,
22039 PARM
, /*initialized=*/0,
22040 /*attrlist=*/NULL
);
22042 chainon (params
, build_tree_list (NULL_TREE
, parm
));
22043 token
= cp_lexer_peek_token (parser
->lexer
);
22046 /* We allow tail attributes for the method. */
22047 if (token
->keyword
== RID_ATTRIBUTE
)
22049 if (*attributes
== NULL_TREE
)
22051 *attributes
= cp_parser_attributes_opt (parser
);
22052 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
22053 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22057 /* We have an error, but parse the attributes, so that we can
22059 *attributes
= cp_parser_attributes_opt (parser
);
22061 cp_parser_error (parser
,
22062 "method attributes must be specified at the end");
22063 return error_mark_node
;
22069 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
22072 cp_parser_objc_interstitial_code (cp_parser
* parser
)
22074 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22076 /* If the next token is `extern' and the following token is a string
22077 literal, then we have a linkage specification. */
22078 if (token
->keyword
== RID_EXTERN
22079 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
22080 cp_parser_linkage_specification (parser
);
22081 /* Handle #pragma, if any. */
22082 else if (token
->type
== CPP_PRAGMA
)
22083 cp_parser_pragma (parser
, pragma_external
);
22084 /* Allow stray semicolons. */
22085 else if (token
->type
== CPP_SEMICOLON
)
22086 cp_lexer_consume_token (parser
->lexer
);
22087 /* Mark methods as optional or required, when building protocols. */
22088 else if (token
->keyword
== RID_AT_OPTIONAL
)
22090 cp_lexer_consume_token (parser
->lexer
);
22091 objc_set_method_opt (true);
22093 else if (token
->keyword
== RID_AT_REQUIRED
)
22095 cp_lexer_consume_token (parser
->lexer
);
22096 objc_set_method_opt (false);
22098 else if (token
->keyword
== RID_NAMESPACE
)
22099 cp_parser_namespace_definition (parser
);
22100 /* Other stray characters must generate errors. */
22101 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
22103 cp_lexer_consume_token (parser
->lexer
);
22104 error ("stray %qs between Objective-C++ methods",
22105 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
22107 /* Finally, try to parse a block-declaration, or a function-definition. */
22109 cp_parser_block_declaration (parser
, /*statement_p=*/false);
22112 /* Parse a method signature. */
22115 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
22117 tree rettype
, kwdparms
, optparms
;
22118 bool ellipsis
= false;
22119 bool is_class_method
;
22121 is_class_method
= cp_parser_objc_method_type (parser
);
22122 rettype
= cp_parser_objc_typename (parser
);
22123 *attributes
= NULL_TREE
;
22124 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
22125 if (kwdparms
== error_mark_node
)
22126 return error_mark_node
;
22127 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
22128 if (optparms
== error_mark_node
)
22129 return error_mark_node
;
22131 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
22135 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
22138 cp_lexer_save_tokens (parser
->lexer
);
22139 tattr
= cp_parser_attributes_opt (parser
);
22140 gcc_assert (tattr
) ;
22142 /* If the attributes are followed by a method introducer, this is not allowed.
22143 Dump the attributes and flag the situation. */
22144 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
22145 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
22148 /* Otherwise, the attributes introduce some interstitial code, possibly so
22149 rewind to allow that check. */
22150 cp_lexer_rollback_tokens (parser
->lexer
);
22154 /* Parse an Objective-C method prototype list. */
22157 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
22159 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22161 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
22163 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
22165 tree attributes
, sig
;
22166 bool is_class_method
;
22167 if (token
->type
== CPP_PLUS
)
22168 is_class_method
= true;
22170 is_class_method
= false;
22171 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
22172 if (sig
== error_mark_node
)
22174 cp_parser_skip_to_end_of_block_or_statement (parser
);
22175 token
= cp_lexer_peek_token (parser
->lexer
);
22178 objc_add_method_declaration (is_class_method
, sig
, attributes
);
22179 cp_parser_consume_semicolon_at_end_of_statement (parser
);
22181 else if (token
->keyword
== RID_AT_PROPERTY
)
22182 cp_parser_objc_at_property_declaration (parser
);
22183 else if (token
->keyword
== RID_ATTRIBUTE
22184 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
22185 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
22187 "prefix attributes are ignored for methods");
22189 /* Allow for interspersed non-ObjC++ code. */
22190 cp_parser_objc_interstitial_code (parser
);
22192 token
= cp_lexer_peek_token (parser
->lexer
);
22195 if (token
->type
!= CPP_EOF
)
22196 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
22198 cp_parser_error (parser
, "expected %<@end%>");
22200 objc_finish_interface ();
22203 /* Parse an Objective-C method definition list. */
22206 cp_parser_objc_method_definition_list (cp_parser
* parser
)
22208 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22210 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
22214 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
22217 tree sig
, attribute
;
22218 bool is_class_method
;
22219 if (token
->type
== CPP_PLUS
)
22220 is_class_method
= true;
22222 is_class_method
= false;
22223 push_deferring_access_checks (dk_deferred
);
22224 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
22225 if (sig
== error_mark_node
)
22227 cp_parser_skip_to_end_of_block_or_statement (parser
);
22228 token
= cp_lexer_peek_token (parser
->lexer
);
22231 objc_start_method_definition (is_class_method
, sig
, attribute
);
22233 /* For historical reasons, we accept an optional semicolon. */
22234 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22235 cp_lexer_consume_token (parser
->lexer
);
22237 ptk
= cp_lexer_peek_token (parser
->lexer
);
22238 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
22239 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
22241 perform_deferred_access_checks ();
22242 stop_deferring_access_checks ();
22243 meth
= cp_parser_function_definition_after_declarator (parser
,
22245 pop_deferring_access_checks ();
22246 objc_finish_method_definition (meth
);
22249 /* The following case will be removed once @synthesize is
22250 completely implemented. */
22251 else if (token
->keyword
== RID_AT_PROPERTY
)
22252 cp_parser_objc_at_property_declaration (parser
);
22253 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
22254 cp_parser_objc_at_synthesize_declaration (parser
);
22255 else if (token
->keyword
== RID_AT_DYNAMIC
)
22256 cp_parser_objc_at_dynamic_declaration (parser
);
22257 else if (token
->keyword
== RID_ATTRIBUTE
22258 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
22259 warning_at (token
->location
, OPT_Wattributes
,
22260 "prefix attributes are ignored for methods");
22262 /* Allow for interspersed non-ObjC++ code. */
22263 cp_parser_objc_interstitial_code (parser
);
22265 token
= cp_lexer_peek_token (parser
->lexer
);
22268 if (token
->type
!= CPP_EOF
)
22269 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
22271 cp_parser_error (parser
, "expected %<@end%>");
22273 objc_finish_implementation ();
22276 /* Parse Objective-C ivars. */
22279 cp_parser_objc_class_ivars (cp_parser
* parser
)
22281 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22283 if (token
->type
!= CPP_OPEN_BRACE
)
22284 return; /* No ivars specified. */
22286 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
22287 token
= cp_lexer_peek_token (parser
->lexer
);
22289 while (token
->type
!= CPP_CLOSE_BRACE
22290 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
22292 cp_decl_specifier_seq declspecs
;
22293 int decl_class_or_enum_p
;
22294 tree prefix_attributes
;
22296 cp_parser_objc_visibility_spec (parser
);
22298 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
22301 cp_parser_decl_specifier_seq (parser
,
22302 CP_PARSER_FLAGS_OPTIONAL
,
22304 &decl_class_or_enum_p
);
22306 /* auto, register, static, extern, mutable. */
22307 if (declspecs
.storage_class
!= sc_none
)
22309 cp_parser_error (parser
, "invalid type for instance variable");
22310 declspecs
.storage_class
= sc_none
;
22314 if (declspecs
.specs
[(int) ds_thread
])
22316 cp_parser_error (parser
, "invalid type for instance variable");
22317 declspecs
.specs
[(int) ds_thread
] = 0;
22321 if (declspecs
.specs
[(int) ds_typedef
])
22323 cp_parser_error (parser
, "invalid type for instance variable");
22324 declspecs
.specs
[(int) ds_typedef
] = 0;
22327 prefix_attributes
= declspecs
.attributes
;
22328 declspecs
.attributes
= NULL_TREE
;
22330 /* Keep going until we hit the `;' at the end of the
22332 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
22334 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
22335 cp_declarator
*declarator
= NULL
;
22336 int ctor_dtor_or_conv_p
;
22338 /* Check for a (possibly unnamed) bitfield declaration. */
22339 token
= cp_lexer_peek_token (parser
->lexer
);
22340 if (token
->type
== CPP_COLON
)
22343 if (token
->type
== CPP_NAME
22344 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22347 /* Get the name of the bitfield. */
22348 declarator
= make_id_declarator (NULL_TREE
,
22349 cp_parser_identifier (parser
),
22353 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
22354 /* Get the width of the bitfield. */
22356 = cp_parser_constant_expression (parser
,
22357 /*allow_non_constant=*/false,
22362 /* Parse the declarator. */
22364 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
22365 &ctor_dtor_or_conv_p
,
22366 /*parenthesized_p=*/NULL
,
22367 /*member_p=*/false);
22370 /* Look for attributes that apply to the ivar. */
22371 attributes
= cp_parser_attributes_opt (parser
);
22372 /* Remember which attributes are prefix attributes and
22374 first_attribute
= attributes
;
22375 /* Combine the attributes. */
22376 attributes
= chainon (prefix_attributes
, attributes
);
22379 /* Create the bitfield declaration. */
22380 decl
= grokbitfield (declarator
, &declspecs
,
22384 decl
= grokfield (declarator
, &declspecs
,
22385 NULL_TREE
, /*init_const_expr_p=*/false,
22386 NULL_TREE
, attributes
);
22388 /* Add the instance variable. */
22389 objc_add_instance_variable (decl
);
22391 /* Reset PREFIX_ATTRIBUTES. */
22392 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
22393 attributes
= TREE_CHAIN (attributes
);
22395 TREE_CHAIN (attributes
) = NULL_TREE
;
22397 token
= cp_lexer_peek_token (parser
->lexer
);
22399 if (token
->type
== CPP_COMMA
)
22401 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
22407 cp_parser_consume_semicolon_at_end_of_statement (parser
);
22408 token
= cp_lexer_peek_token (parser
->lexer
);
22411 if (token
->keyword
== RID_AT_END
)
22412 cp_parser_error (parser
, "expected %<}%>");
22414 /* Do not consume the RID_AT_END, so it will be read again as terminating
22415 the @interface of @implementation. */
22416 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
22417 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
22419 /* For historical reasons, we accept an optional semicolon. */
22420 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22421 cp_lexer_consume_token (parser
->lexer
);
22424 /* Parse an Objective-C protocol declaration. */
22427 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
22429 tree proto
, protorefs
;
22432 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
22433 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
22435 tok
= cp_lexer_peek_token (parser
->lexer
);
22436 error_at (tok
->location
, "identifier expected after %<@protocol%>");
22437 cp_parser_consume_semicolon_at_end_of_statement (parser
);
22441 /* See if we have a forward declaration or a definition. */
22442 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22444 /* Try a forward declaration first. */
22445 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
22451 id
= cp_parser_identifier (parser
);
22452 if (id
== error_mark_node
)
22455 objc_declare_protocol (id
, attributes
);
22457 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22458 cp_lexer_consume_token (parser
->lexer
);
22462 cp_parser_consume_semicolon_at_end_of_statement (parser
);
22465 /* Ok, we got a full-fledged definition (or at least should). */
22468 proto
= cp_parser_identifier (parser
);
22469 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
22470 objc_start_protocol (proto
, protorefs
, attributes
);
22471 cp_parser_objc_method_prototype_list (parser
);
22475 /* Parse an Objective-C superclass or category. */
22478 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
22481 tree
*categ
, bool *is_class_extension
)
22483 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
22485 *super
= *categ
= NULL_TREE
;
22486 *is_class_extension
= false;
22487 if (next
->type
== CPP_COLON
)
22489 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
22490 *super
= cp_parser_identifier (parser
);
22492 else if (next
->type
== CPP_OPEN_PAREN
)
22494 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
22496 /* If there is no category name, and this is an @interface, we
22497 have a class extension. */
22498 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
22500 *categ
= NULL_TREE
;
22501 *is_class_extension
= true;
22504 *categ
= cp_parser_identifier (parser
);
22506 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22510 /* Parse an Objective-C class interface. */
22513 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
22515 tree name
, super
, categ
, protos
;
22516 bool is_class_extension
;
22518 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
22519 name
= cp_parser_identifier (parser
);
22520 if (name
== error_mark_node
)
22522 /* It's hard to recover because even if valid @interface stuff
22523 is to follow, we can't compile it (or validate it) if we
22524 don't even know which class it refers to. Let's assume this
22525 was a stray '@interface' token in the stream and skip it.
22529 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
22530 &is_class_extension
);
22531 protos
= cp_parser_objc_protocol_refs_opt (parser
);
22533 /* We have either a class or a category on our hands. */
22534 if (categ
|| is_class_extension
)
22535 objc_start_category_interface (name
, categ
, protos
, attributes
);
22538 objc_start_class_interface (name
, super
, protos
, attributes
);
22539 /* Handle instance variable declarations, if any. */
22540 cp_parser_objc_class_ivars (parser
);
22541 objc_continue_interface ();
22544 cp_parser_objc_method_prototype_list (parser
);
22547 /* Parse an Objective-C class implementation. */
22550 cp_parser_objc_class_implementation (cp_parser
* parser
)
22552 tree name
, super
, categ
;
22553 bool is_class_extension
;
22555 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
22556 name
= cp_parser_identifier (parser
);
22557 if (name
== error_mark_node
)
22559 /* It's hard to recover because even if valid @implementation
22560 stuff is to follow, we can't compile it (or validate it) if
22561 we don't even know which class it refers to. Let's assume
22562 this was a stray '@implementation' token in the stream and
22567 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
22568 &is_class_extension
);
22570 /* We have either a class or a category on our hands. */
22572 objc_start_category_implementation (name
, categ
);
22575 objc_start_class_implementation (name
, super
);
22576 /* Handle instance variable declarations, if any. */
22577 cp_parser_objc_class_ivars (parser
);
22578 objc_continue_implementation ();
22581 cp_parser_objc_method_definition_list (parser
);
22584 /* Consume the @end token and finish off the implementation. */
22587 cp_parser_objc_end_implementation (cp_parser
* parser
)
22589 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
22590 objc_finish_implementation ();
22593 /* Parse an Objective-C declaration. */
22596 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
22598 /* Try to figure out what kind of declaration is present. */
22599 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
22602 switch (kwd
->keyword
)
22607 error_at (kwd
->location
, "attributes may not be specified before"
22608 " the %<@%D%> Objective-C++ keyword",
22612 case RID_AT_IMPLEMENTATION
:
22613 warning_at (kwd
->location
, OPT_Wattributes
,
22614 "prefix attributes are ignored before %<@%D%>",
22621 switch (kwd
->keyword
)
22624 cp_parser_objc_alias_declaration (parser
);
22627 cp_parser_objc_class_declaration (parser
);
22629 case RID_AT_PROTOCOL
:
22630 cp_parser_objc_protocol_declaration (parser
, attributes
);
22632 case RID_AT_INTERFACE
:
22633 cp_parser_objc_class_interface (parser
, attributes
);
22635 case RID_AT_IMPLEMENTATION
:
22636 cp_parser_objc_class_implementation (parser
);
22639 cp_parser_objc_end_implementation (parser
);
22642 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
22644 cp_parser_skip_to_end_of_block_or_statement (parser
);
22648 /* Parse an Objective-C try-catch-finally statement.
22650 objc-try-catch-finally-stmt:
22651 @try compound-statement objc-catch-clause-seq [opt]
22652 objc-finally-clause [opt]
22654 objc-catch-clause-seq:
22655 objc-catch-clause objc-catch-clause-seq [opt]
22658 @catch ( objc-exception-declaration ) compound-statement
22660 objc-finally-clause:
22661 @finally compound-statement
22663 objc-exception-declaration:
22664 parameter-declaration
22667 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22671 PS: This function is identical to c_parser_objc_try_catch_finally_statement
22672 for C. Keep them in sync. */
22675 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
22677 location_t location
;
22680 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
22681 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
22682 objc_maybe_warn_exceptions (location
);
22683 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22684 node, lest it get absorbed into the surrounding block. */
22685 stmt
= push_stmt_list ();
22686 cp_parser_compound_statement (parser
, NULL
, false, false);
22687 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
22689 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
22691 cp_parameter_declarator
*parm
;
22692 tree parameter_declaration
= error_mark_node
;
22693 bool seen_open_paren
= false;
22695 cp_lexer_consume_token (parser
->lexer
);
22696 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22697 seen_open_paren
= true;
22698 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22700 /* We have "@catch (...)" (where the '...' are literally
22701 what is in the code). Skip the '...'.
22702 parameter_declaration is set to NULL_TREE, and
22703 objc_being_catch_clauses() knows that that means
22705 cp_lexer_consume_token (parser
->lexer
);
22706 parameter_declaration
= NULL_TREE
;
22710 /* We have "@catch (NSException *exception)" or something
22711 like that. Parse the parameter declaration. */
22712 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
22714 parameter_declaration
= error_mark_node
;
22716 parameter_declaration
= grokdeclarator (parm
->declarator
,
22717 &parm
->decl_specifiers
,
22718 PARM
, /*initialized=*/0,
22719 /*attrlist=*/NULL
);
22721 if (seen_open_paren
)
22722 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22725 /* If there was no open parenthesis, we are recovering from
22726 an error, and we are trying to figure out what mistake
22727 the user has made. */
22729 /* If there is an immediate closing parenthesis, the user
22730 probably forgot the opening one (ie, they typed "@catch
22731 NSException *e)". Parse the closing parenthesis and keep
22733 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
22734 cp_lexer_consume_token (parser
->lexer
);
22736 /* If these is no immediate closing parenthesis, the user
22737 probably doesn't know that parenthesis are required at
22738 all (ie, they typed "@catch NSException *e"). So, just
22739 forget about the closing parenthesis and keep going. */
22741 objc_begin_catch_clause (parameter_declaration
);
22742 cp_parser_compound_statement (parser
, NULL
, false, false);
22743 objc_finish_catch_clause ();
22745 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
22747 cp_lexer_consume_token (parser
->lexer
);
22748 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
22749 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22750 node, lest it get absorbed into the surrounding block. */
22751 stmt
= push_stmt_list ();
22752 cp_parser_compound_statement (parser
, NULL
, false, false);
22753 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
22756 return objc_finish_try_stmt ();
22759 /* Parse an Objective-C synchronized statement.
22761 objc-synchronized-stmt:
22762 @synchronized ( expression ) compound-statement
22764 Returns NULL_TREE. */
22767 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
22769 location_t location
;
22772 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
22774 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
22775 objc_maybe_warn_exceptions (location
);
22776 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22777 lock
= cp_parser_expression (parser
, false, NULL
);
22778 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22780 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22781 node, lest it get absorbed into the surrounding block. */
22782 stmt
= push_stmt_list ();
22783 cp_parser_compound_statement (parser
, NULL
, false, false);
22785 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
22788 /* Parse an Objective-C throw statement.
22791 @throw assignment-expression [opt] ;
22793 Returns a constructed '@throw' statement. */
22796 cp_parser_objc_throw_statement (cp_parser
*parser
)
22798 tree expr
= NULL_TREE
;
22799 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22801 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
22803 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
22804 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
22806 cp_parser_consume_semicolon_at_end_of_statement (parser
);
22808 return objc_build_throw_stmt (loc
, expr
);
22811 /* Parse an Objective-C statement. */
22814 cp_parser_objc_statement (cp_parser
* parser
)
22816 /* Try to figure out what kind of declaration is present. */
22817 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
22819 switch (kwd
->keyword
)
22822 return cp_parser_objc_try_catch_finally_statement (parser
);
22823 case RID_AT_SYNCHRONIZED
:
22824 return cp_parser_objc_synchronized_statement (parser
);
22826 return cp_parser_objc_throw_statement (parser
);
22828 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
22830 cp_parser_skip_to_end_of_block_or_statement (parser
);
22833 return error_mark_node
;
22836 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22837 look ahead to see if an objc keyword follows the attributes. This
22838 is to detect the use of prefix attributes on ObjC @interface and
22842 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
22844 cp_lexer_save_tokens (parser
->lexer
);
22845 *attrib
= cp_parser_attributes_opt (parser
);
22846 gcc_assert (*attrib
);
22847 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
22849 cp_lexer_commit_tokens (parser
->lexer
);
22852 cp_lexer_rollback_tokens (parser
->lexer
);
22856 /* This routine is a minimal replacement for
22857 c_parser_struct_declaration () used when parsing the list of
22858 types/names or ObjC++ properties. For example, when parsing the
22861 @property (readonly) int a, b, c;
22863 this function is responsible for parsing "int a, int b, int c" and
22864 returning the declarations as CHAIN of DECLs.
22866 TODO: Share this code with cp_parser_objc_class_ivars. It's very
22867 similar parsing. */
22869 cp_parser_objc_struct_declaration (cp_parser
*parser
)
22871 tree decls
= NULL_TREE
;
22872 cp_decl_specifier_seq declspecs
;
22873 int decl_class_or_enum_p
;
22874 tree prefix_attributes
;
22876 cp_parser_decl_specifier_seq (parser
,
22877 CP_PARSER_FLAGS_NONE
,
22879 &decl_class_or_enum_p
);
22881 if (declspecs
.type
== error_mark_node
)
22882 return error_mark_node
;
22884 /* auto, register, static, extern, mutable. */
22885 if (declspecs
.storage_class
!= sc_none
)
22887 cp_parser_error (parser
, "invalid type for property");
22888 declspecs
.storage_class
= sc_none
;
22892 if (declspecs
.specs
[(int) ds_thread
])
22894 cp_parser_error (parser
, "invalid type for property");
22895 declspecs
.specs
[(int) ds_thread
] = 0;
22899 if (declspecs
.specs
[(int) ds_typedef
])
22901 cp_parser_error (parser
, "invalid type for property");
22902 declspecs
.specs
[(int) ds_typedef
] = 0;
22905 prefix_attributes
= declspecs
.attributes
;
22906 declspecs
.attributes
= NULL_TREE
;
22908 /* Keep going until we hit the `;' at the end of the declaration. */
22909 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
22911 tree attributes
, first_attribute
, decl
;
22912 cp_declarator
*declarator
;
22915 /* Parse the declarator. */
22916 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
22917 NULL
, NULL
, false);
22919 /* Look for attributes that apply to the ivar. */
22920 attributes
= cp_parser_attributes_opt (parser
);
22921 /* Remember which attributes are prefix attributes and
22923 first_attribute
= attributes
;
22924 /* Combine the attributes. */
22925 attributes
= chainon (prefix_attributes
, attributes
);
22927 decl
= grokfield (declarator
, &declspecs
,
22928 NULL_TREE
, /*init_const_expr_p=*/false,
22929 NULL_TREE
, attributes
);
22931 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
22932 return error_mark_node
;
22934 /* Reset PREFIX_ATTRIBUTES. */
22935 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
22936 attributes
= TREE_CHAIN (attributes
);
22938 TREE_CHAIN (attributes
) = NULL_TREE
;
22940 DECL_CHAIN (decl
) = decls
;
22943 token
= cp_lexer_peek_token (parser
->lexer
);
22944 if (token
->type
== CPP_COMMA
)
22946 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
22955 /* Parse an Objective-C @property declaration. The syntax is:
22957 objc-property-declaration:
22958 '@property' objc-property-attributes[opt] struct-declaration ;
22960 objc-property-attributes:
22961 '(' objc-property-attribute-list ')'
22963 objc-property-attribute-list:
22964 objc-property-attribute
22965 objc-property-attribute-list, objc-property-attribute
22967 objc-property-attribute
22968 'getter' = identifier
22969 'setter' = identifier
22978 @property NSString *name;
22979 @property (readonly) id object;
22980 @property (retain, nonatomic, getter=getTheName) id name;
22981 @property int a, b, c;
22983 PS: This function is identical to
22984 c_parser_objc_at_property_declaration for C. Keep them in sync. */
22986 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
22988 /* The following variables hold the attributes of the properties as
22989 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
22990 seen. When we see an attribute, we set them to 'true' (if they
22991 are boolean properties) or to the identifier (if they have an
22992 argument, ie, for getter and setter). Note that here we only
22993 parse the list of attributes, check the syntax and accumulate the
22994 attributes that we find. objc_add_property_declaration() will
22995 then process the information. */
22996 bool property_assign
= false;
22997 bool property_copy
= false;
22998 tree property_getter_ident
= NULL_TREE
;
22999 bool property_nonatomic
= false;
23000 bool property_readonly
= false;
23001 bool property_readwrite
= false;
23002 bool property_retain
= false;
23003 tree property_setter_ident
= NULL_TREE
;
23005 /* 'properties' is the list of properties that we read. Usually a
23006 single one, but maybe more (eg, in "@property int a, b, c;" there
23011 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23013 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
23015 /* Parse the optional attribute list... */
23016 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23019 cp_lexer_consume_token (parser
->lexer
);
23023 bool syntax_error
= false;
23024 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23027 if (token
->type
!= CPP_NAME
)
23029 cp_parser_error (parser
, "expected identifier");
23032 keyword
= C_RID_CODE (token
->u
.value
);
23033 cp_lexer_consume_token (parser
->lexer
);
23036 case RID_ASSIGN
: property_assign
= true; break;
23037 case RID_COPY
: property_copy
= true; break;
23038 case RID_NONATOMIC
: property_nonatomic
= true; break;
23039 case RID_READONLY
: property_readonly
= true; break;
23040 case RID_READWRITE
: property_readwrite
= true; break;
23041 case RID_RETAIN
: property_retain
= true; break;
23045 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
23047 if (keyword
== RID_GETTER
)
23048 cp_parser_error (parser
,
23049 "missing %<=%> (after %<getter%> attribute)");
23051 cp_parser_error (parser
,
23052 "missing %<=%> (after %<setter%> attribute)");
23053 syntax_error
= true;
23056 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
23057 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
23059 cp_parser_error (parser
, "expected identifier");
23060 syntax_error
= true;
23063 if (keyword
== RID_SETTER
)
23065 if (property_setter_ident
!= NULL_TREE
)
23066 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
23068 property_setter_ident
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
23069 cp_lexer_consume_token (parser
->lexer
);
23070 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
23071 cp_parser_error (parser
, "setter name must terminate with %<:%>");
23073 cp_lexer_consume_token (parser
->lexer
);
23077 if (property_getter_ident
!= NULL_TREE
)
23078 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
23080 property_getter_ident
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
23081 cp_lexer_consume_token (parser
->lexer
);
23085 cp_parser_error (parser
, "unknown property attribute");
23086 syntax_error
= true;
23093 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23094 cp_lexer_consume_token (parser
->lexer
);
23099 /* FIXME: "@property (setter, assign);" will generate a spurious
23100 "error: expected ‘)’ before ‘,’ token". This is because
23101 cp_parser_require, unlike the C counterpart, will produce an
23102 error even if we are in error recovery. */
23103 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23105 cp_parser_skip_to_closing_parenthesis (parser
,
23106 /*recovering=*/true,
23107 /*or_comma=*/false,
23108 /*consume_paren=*/true);
23112 /* ... and the property declaration(s). */
23113 properties
= cp_parser_objc_struct_declaration (parser
);
23115 if (properties
== error_mark_node
)
23117 cp_parser_skip_to_end_of_statement (parser
);
23118 /* If the next token is now a `;', consume it. */
23119 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23120 cp_lexer_consume_token (parser
->lexer
);
23124 if (properties
== NULL_TREE
)
23125 cp_parser_error (parser
, "expected identifier");
23128 /* Comma-separated properties are chained together in
23129 reverse order; add them one by one. */
23130 properties
= nreverse (properties
);
23132 for (; properties
; properties
= TREE_CHAIN (properties
))
23133 objc_add_property_declaration (loc
, copy_node (properties
),
23134 property_readonly
, property_readwrite
,
23135 property_assign
, property_retain
,
23136 property_copy
, property_nonatomic
,
23137 property_getter_ident
, property_setter_ident
);
23140 cp_parser_consume_semicolon_at_end_of_statement (parser
);
23143 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
23145 objc-synthesize-declaration:
23146 @synthesize objc-synthesize-identifier-list ;
23148 objc-synthesize-identifier-list:
23149 objc-synthesize-identifier
23150 objc-synthesize-identifier-list, objc-synthesize-identifier
23152 objc-synthesize-identifier
23154 identifier = identifier
23157 @synthesize MyProperty;
23158 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23160 PS: This function is identical to c_parser_objc_at_synthesize_declaration
23161 for C. Keep them in sync.
23164 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
23166 tree list
= NULL_TREE
;
23168 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23170 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
23173 tree property
, ivar
;
23174 property
= cp_parser_identifier (parser
);
23175 if (property
== error_mark_node
)
23177 cp_parser_consume_semicolon_at_end_of_statement (parser
);
23180 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23182 cp_lexer_consume_token (parser
->lexer
);
23183 ivar
= cp_parser_identifier (parser
);
23184 if (ivar
== error_mark_node
)
23186 cp_parser_consume_semicolon_at_end_of_statement (parser
);
23192 list
= chainon (list
, build_tree_list (ivar
, property
));
23193 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23194 cp_lexer_consume_token (parser
->lexer
);
23198 cp_parser_consume_semicolon_at_end_of_statement (parser
);
23199 objc_add_synthesize_declaration (loc
, list
);
23202 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
23204 objc-dynamic-declaration:
23205 @dynamic identifier-list ;
23208 @dynamic MyProperty;
23209 @dynamic MyProperty, AnotherProperty;
23211 PS: This function is identical to c_parser_objc_at_dynamic_declaration
23212 for C. Keep them in sync.
23215 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
23217 tree list
= NULL_TREE
;
23219 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23221 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
23225 property
= cp_parser_identifier (parser
);
23226 if (property
== error_mark_node
)
23228 cp_parser_consume_semicolon_at_end_of_statement (parser
);
23231 list
= chainon (list
, build_tree_list (NULL
, property
));
23232 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23233 cp_lexer_consume_token (parser
->lexer
);
23237 cp_parser_consume_semicolon_at_end_of_statement (parser
);
23238 objc_add_dynamic_declaration (loc
, list
);
23242 /* OpenMP 2.5 parsing routines. */
23244 /* Returns name of the next clause.
23245 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23246 the token is not consumed. Otherwise appropriate pragma_omp_clause is
23247 returned and the token is consumed. */
23249 static pragma_omp_clause
23250 cp_parser_omp_clause_name (cp_parser
*parser
)
23252 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
23254 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
23255 result
= PRAGMA_OMP_CLAUSE_IF
;
23256 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
23257 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
23258 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
23259 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
23260 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23262 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
23263 const char *p
= IDENTIFIER_POINTER (id
);
23268 if (!strcmp ("collapse", p
))
23269 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
23270 else if (!strcmp ("copyin", p
))
23271 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
23272 else if (!strcmp ("copyprivate", p
))
23273 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
23276 if (!strcmp ("firstprivate", p
))
23277 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
23280 if (!strcmp ("lastprivate", p
))
23281 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
23284 if (!strcmp ("nowait", p
))
23285 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
23286 else if (!strcmp ("num_threads", p
))
23287 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
23290 if (!strcmp ("ordered", p
))
23291 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
23294 if (!strcmp ("reduction", p
))
23295 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
23298 if (!strcmp ("schedule", p
))
23299 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
23300 else if (!strcmp ("shared", p
))
23301 result
= PRAGMA_OMP_CLAUSE_SHARED
;
23304 if (!strcmp ("untied", p
))
23305 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
23310 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
23311 cp_lexer_consume_token (parser
->lexer
);
23316 /* Validate that a clause of the given type does not already exist. */
23319 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
23320 const char *name
, location_t location
)
23324 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
23325 if (OMP_CLAUSE_CODE (c
) == code
)
23327 error_at (location
, "too many %qs clauses", name
);
23335 variable-list , identifier
23337 In addition, we match a closing parenthesis. An opening parenthesis
23338 will have been consumed by the caller.
23340 If KIND is nonzero, create the appropriate node and install the decl
23341 in OMP_CLAUSE_DECL and add the node to the head of the list.
23343 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23344 return the list created. */
23347 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
23355 token
= cp_lexer_peek_token (parser
->lexer
);
23356 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
23357 /*check_dependency_p=*/true,
23358 /*template_p=*/NULL
,
23359 /*declarator_p=*/false,
23360 /*optional_p=*/false);
23361 if (name
== error_mark_node
)
23364 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
23365 if (decl
== error_mark_node
)
23366 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
23368 else if (kind
!= 0)
23370 tree u
= build_omp_clause (token
->location
, kind
);
23371 OMP_CLAUSE_DECL (u
) = decl
;
23372 OMP_CLAUSE_CHAIN (u
) = list
;
23376 list
= tree_cons (decl
, NULL_TREE
, list
);
23379 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
23381 cp_lexer_consume_token (parser
->lexer
);
23384 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23388 /* Try to resync to an unnested comma. Copied from
23389 cp_parser_parenthesized_expression_list. */
23391 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
23392 /*recovering=*/true,
23394 /*consume_paren=*/true);
23402 /* Similarly, but expect leading and trailing parenthesis. This is a very
23403 common case for omp clauses. */
23406 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
23408 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23409 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
23414 collapse ( constant-expression ) */
23417 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
23423 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23424 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23427 num
= cp_parser_constant_expression (parser
, false, NULL
);
23429 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23430 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
23431 /*or_comma=*/false,
23432 /*consume_paren=*/true);
23434 if (num
== error_mark_node
)
23436 num
= fold_non_dependent_expr (num
);
23437 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
23438 || !host_integerp (num
, 0)
23439 || (n
= tree_low_cst (num
, 0)) <= 0
23442 error_at (loc
, "collapse argument needs positive constant integer expression");
23446 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
23447 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
23448 OMP_CLAUSE_CHAIN (c
) = list
;
23449 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
23455 default ( shared | none ) */
23458 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
23460 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
23463 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23465 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23467 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
23468 const char *p
= IDENTIFIER_POINTER (id
);
23473 if (strcmp ("none", p
) != 0)
23475 kind
= OMP_CLAUSE_DEFAULT_NONE
;
23479 if (strcmp ("shared", p
) != 0)
23481 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
23488 cp_lexer_consume_token (parser
->lexer
);
23493 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
23496 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23497 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
23498 /*or_comma=*/false,
23499 /*consume_paren=*/true);
23501 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
23504 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
23505 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
23506 OMP_CLAUSE_CHAIN (c
) = list
;
23507 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
23513 if ( expression ) */
23516 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
23520 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23523 t
= cp_parser_condition (parser
);
23525 if (t
== error_mark_node
23526 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23527 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
23528 /*or_comma=*/false,
23529 /*consume_paren=*/true);
23531 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
23533 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
23534 OMP_CLAUSE_IF_EXPR (c
) = t
;
23535 OMP_CLAUSE_CHAIN (c
) = list
;
23544 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
,
23545 tree list
, location_t location
)
23549 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
23551 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
23552 OMP_CLAUSE_CHAIN (c
) = list
;
23557 num_threads ( expression ) */
23560 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
23561 location_t location
)
23565 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23568 t
= cp_parser_expression (parser
, false, NULL
);
23570 if (t
== error_mark_node
23571 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23572 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
23573 /*or_comma=*/false,
23574 /*consume_paren=*/true);
23576 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
23577 "num_threads", location
);
23579 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
23580 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
23581 OMP_CLAUSE_CHAIN (c
) = list
;
23590 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
,
23591 tree list
, location_t location
)
23595 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
23596 "ordered", location
);
23598 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
23599 OMP_CLAUSE_CHAIN (c
) = list
;
23604 reduction ( reduction-operator : variable-list )
23606 reduction-operator:
23607 One of: + * - & ^ | && || */
23610 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
23612 enum tree_code code
;
23615 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23618 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
23630 code
= BIT_AND_EXPR
;
23633 code
= BIT_XOR_EXPR
;
23636 code
= BIT_IOR_EXPR
;
23639 code
= TRUTH_ANDIF_EXPR
;
23642 code
= TRUTH_ORIF_EXPR
;
23645 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23646 "%<|%>, %<&&%>, or %<||%>");
23648 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
23649 /*or_comma=*/false,
23650 /*consume_paren=*/true);
23653 cp_lexer_consume_token (parser
->lexer
);
23655 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
23658 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
23659 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
23660 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
23666 schedule ( schedule-kind )
23667 schedule ( schedule-kind , expression )
23670 static | dynamic | guided | runtime | auto */
23673 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
23677 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23680 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
23682 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23684 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
23685 const char *p
= IDENTIFIER_POINTER (id
);
23690 if (strcmp ("dynamic", p
) != 0)
23692 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
23696 if (strcmp ("guided", p
) != 0)
23698 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
23702 if (strcmp ("runtime", p
) != 0)
23704 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
23711 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
23712 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
23713 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
23714 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
23717 cp_lexer_consume_token (parser
->lexer
);
23719 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23722 cp_lexer_consume_token (parser
->lexer
);
23724 token
= cp_lexer_peek_token (parser
->lexer
);
23725 t
= cp_parser_assignment_expression (parser
, false, NULL
);
23727 if (t
== error_mark_node
)
23729 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
23730 error_at (token
->location
, "schedule %<runtime%> does not take "
23731 "a %<chunk_size%> parameter");
23732 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
23733 error_at (token
->location
, "schedule %<auto%> does not take "
23734 "a %<chunk_size%> parameter");
23736 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
23738 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23741 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
23744 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
23745 OMP_CLAUSE_CHAIN (c
) = list
;
23749 cp_parser_error (parser
, "invalid schedule kind");
23751 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
23752 /*or_comma=*/false,
23753 /*consume_paren=*/true);
23761 cp_parser_omp_clause_untied (cp_parser
*parser ATTRIBUTE_UNUSED
,
23762 tree list
, location_t location
)
23766 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
23768 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
23769 OMP_CLAUSE_CHAIN (c
) = list
;
23773 /* Parse all OpenMP clauses. The set clauses allowed by the directive
23774 is a bitmask in MASK. Return the list of clauses found; the result
23775 of clause default goes in *pdefault. */
23778 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
23779 const char *where
, cp_token
*pragma_tok
)
23781 tree clauses
= NULL
;
23783 cp_token
*token
= NULL
;
23785 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
23787 pragma_omp_clause c_kind
;
23788 const char *c_name
;
23789 tree prev
= clauses
;
23791 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23792 cp_lexer_consume_token (parser
->lexer
);
23794 token
= cp_lexer_peek_token (parser
->lexer
);
23795 c_kind
= cp_parser_omp_clause_name (parser
);
23800 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
23801 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
23803 c_name
= "collapse";
23805 case PRAGMA_OMP_CLAUSE_COPYIN
:
23806 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
23809 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
23810 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
23812 c_name
= "copyprivate";
23814 case PRAGMA_OMP_CLAUSE_DEFAULT
:
23815 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
23817 c_name
= "default";
23819 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
23820 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
23822 c_name
= "firstprivate";
23824 case PRAGMA_OMP_CLAUSE_IF
:
23825 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
23828 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
23829 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
23831 c_name
= "lastprivate";
23833 case PRAGMA_OMP_CLAUSE_NOWAIT
:
23834 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
23837 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
23838 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
23840 c_name
= "num_threads";
23842 case PRAGMA_OMP_CLAUSE_ORDERED
:
23843 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
23845 c_name
= "ordered";
23847 case PRAGMA_OMP_CLAUSE_PRIVATE
:
23848 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
23850 c_name
= "private";
23852 case PRAGMA_OMP_CLAUSE_REDUCTION
:
23853 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
23854 c_name
= "reduction";
23856 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
23857 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
23859 c_name
= "schedule";
23861 case PRAGMA_OMP_CLAUSE_SHARED
:
23862 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
23866 case PRAGMA_OMP_CLAUSE_UNTIED
:
23867 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
23872 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
23876 if (((mask
>> c_kind
) & 1) == 0)
23878 /* Remove the invalid clause(s) from the list to avoid
23879 confusing the rest of the compiler. */
23881 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
23885 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
23886 return finish_omp_clauses (clauses
);
23893 In practice, we're also interested in adding the statement to an
23894 outer node. So it is convenient if we work around the fact that
23895 cp_parser_statement calls add_stmt. */
23898 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
23900 unsigned save
= parser
->in_statement
;
23902 /* Only move the values to IN_OMP_BLOCK if they weren't false.
23903 This preserves the "not within loop or switch" style error messages
23904 for nonsense cases like
23910 if (parser
->in_statement
)
23911 parser
->in_statement
= IN_OMP_BLOCK
;
23917 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
23919 parser
->in_statement
= save
;
23923 cp_parser_omp_structured_block (cp_parser
*parser
)
23925 tree stmt
= begin_omp_structured_block ();
23926 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
23928 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
23930 cp_parser_end_omp_structured_block (parser
, save
);
23931 return finish_omp_structured_block (stmt
);
23935 # pragma omp atomic new-line
23939 x binop= expr | x++ | ++x | x-- | --x
23941 +, *, -, /, &, ^, |, <<, >>
23943 where x is an lvalue expression with scalar type. */
23946 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
23949 enum tree_code code
;
23951 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23953 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
23954 /*cast_p=*/false, NULL
);
23955 switch (TREE_CODE (lhs
))
23960 case PREINCREMENT_EXPR
:
23961 case POSTINCREMENT_EXPR
:
23962 lhs
= TREE_OPERAND (lhs
, 0);
23964 rhs
= integer_one_node
;
23967 case PREDECREMENT_EXPR
:
23968 case POSTDECREMENT_EXPR
:
23969 lhs
= TREE_OPERAND (lhs
, 0);
23971 rhs
= integer_one_node
;
23974 case COMPOUND_EXPR
:
23975 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
23976 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
23977 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
23978 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
23979 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23980 (TREE_OPERAND (lhs
, 1), 0), 0)))
23982 /* Undo effects of boolean_increment for post {in,de}crement. */
23983 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
23986 if (TREE_CODE (lhs
) == MODIFY_EXPR
23987 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
23989 /* Undo effects of boolean_increment. */
23990 if (integer_onep (TREE_OPERAND (lhs
, 1)))
23992 /* This is pre or post increment. */
23993 rhs
= TREE_OPERAND (lhs
, 1);
23994 lhs
= TREE_OPERAND (lhs
, 0);
24001 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
24007 code
= TRUNC_DIV_EXPR
;
24015 case CPP_LSHIFT_EQ
:
24016 code
= LSHIFT_EXPR
;
24018 case CPP_RSHIFT_EQ
:
24019 code
= RSHIFT_EXPR
;
24022 code
= BIT_AND_EXPR
;
24025 code
= BIT_IOR_EXPR
;
24028 code
= BIT_XOR_EXPR
;
24031 cp_parser_error (parser
,
24032 "invalid operator for %<#pragma omp atomic%>");
24035 cp_lexer_consume_token (parser
->lexer
);
24037 rhs
= cp_parser_expression (parser
, false, NULL
);
24038 if (rhs
== error_mark_node
)
24042 finish_omp_atomic (code
, lhs
, rhs
);
24043 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24047 cp_parser_skip_to_end_of_block_or_statement (parser
);
24052 # pragma omp barrier new-line */
24055 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
24057 cp_parser_require_pragma_eol (parser
, pragma_tok
);
24058 finish_omp_barrier ();
24062 # pragma omp critical [(name)] new-line
24063 structured-block */
24066 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
24068 tree stmt
, name
= NULL
;
24070 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
24072 cp_lexer_consume_token (parser
->lexer
);
24074 name
= cp_parser_identifier (parser
);
24076 if (name
== error_mark_node
24077 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
24078 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
24079 /*or_comma=*/false,
24080 /*consume_paren=*/true);
24081 if (name
== error_mark_node
)
24084 cp_parser_require_pragma_eol (parser
, pragma_tok
);
24086 stmt
= cp_parser_omp_structured_block (parser
);
24087 return c_finish_omp_critical (input_location
, stmt
, name
);
24091 # pragma omp flush flush-vars[opt] new-line
24094 ( variable-list ) */
24097 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
24099 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
24100 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
24101 cp_parser_require_pragma_eol (parser
, pragma_tok
);
24103 finish_omp_flush ();
24106 /* Helper function, to parse omp for increment expression. */
24109 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
24111 tree cond
= cp_parser_binary_expression (parser
, false, true,
24112 PREC_NOT_OPERATOR
, NULL
);
24115 if (cond
== error_mark_node
24116 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24118 cp_parser_skip_to_end_of_statement (parser
);
24119 return error_mark_node
;
24122 switch (TREE_CODE (cond
))
24130 return error_mark_node
;
24133 /* If decl is an iterator, preserve LHS and RHS of the relational
24134 expr until finish_omp_for. */
24136 && (type_dependent_expression_p (decl
)
24137 || CLASS_TYPE_P (TREE_TYPE (decl
))))
24140 return build_x_binary_op (TREE_CODE (cond
),
24141 TREE_OPERAND (cond
, 0), ERROR_MARK
,
24142 TREE_OPERAND (cond
, 1), ERROR_MARK
,
24143 &overloaded_p
, tf_warning_or_error
);
24146 /* Helper function, to parse omp for increment expression. */
24149 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
24151 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24157 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
24159 op
= (token
->type
== CPP_PLUS_PLUS
24160 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
24161 cp_lexer_consume_token (parser
->lexer
);
24162 lhs
= cp_parser_cast_expression (parser
, false, false, NULL
);
24164 return error_mark_node
;
24165 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
24168 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
24170 return error_mark_node
;
24172 token
= cp_lexer_peek_token (parser
->lexer
);
24173 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
24175 op
= (token
->type
== CPP_PLUS_PLUS
24176 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
24177 cp_lexer_consume_token (parser
->lexer
);
24178 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
24181 op
= cp_parser_assignment_operator_opt (parser
);
24182 if (op
== ERROR_MARK
)
24183 return error_mark_node
;
24185 if (op
!= NOP_EXPR
)
24187 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
24188 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
24189 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
24192 lhs
= cp_parser_binary_expression (parser
, false, false,
24193 PREC_ADDITIVE_EXPRESSION
, NULL
);
24194 token
= cp_lexer_peek_token (parser
->lexer
);
24195 decl_first
= lhs
== decl
;
24198 if (token
->type
!= CPP_PLUS
24199 && token
->type
!= CPP_MINUS
)
24200 return error_mark_node
;
24204 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
24205 cp_lexer_consume_token (parser
->lexer
);
24206 rhs
= cp_parser_binary_expression (parser
, false, false,
24207 PREC_ADDITIVE_EXPRESSION
, NULL
);
24208 token
= cp_lexer_peek_token (parser
->lexer
);
24209 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
24211 if (lhs
== NULL_TREE
)
24213 if (op
== PLUS_EXPR
)
24216 lhs
= build_x_unary_op (NEGATE_EXPR
, rhs
, tf_warning_or_error
);
24219 lhs
= build_x_binary_op (op
, lhs
, ERROR_MARK
, rhs
, ERROR_MARK
,
24220 NULL
, tf_warning_or_error
);
24223 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
24227 if (rhs
!= decl
|| op
== MINUS_EXPR
)
24228 return error_mark_node
;
24229 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
24232 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
24234 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
24237 /* Parse the restricted form of the for statement allowed by OpenMP. */
24240 cp_parser_omp_for_loop (cp_parser
*parser
, tree clauses
, tree
*par_clauses
)
24242 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
24243 tree real_decl
, initv
, condv
, incrv
, declv
;
24244 tree this_pre_body
, cl
;
24245 location_t loc_first
;
24246 bool collapse_err
= false;
24247 int i
, collapse
= 1, nbraces
= 0;
24248 VEC(tree
,gc
) *for_block
= make_tree_vector ();
24250 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
24251 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
24252 collapse
= tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl
), 0);
24254 gcc_assert (collapse
>= 1);
24256 declv
= make_tree_vec (collapse
);
24257 initv
= make_tree_vec (collapse
);
24258 condv
= make_tree_vec (collapse
);
24259 incrv
= make_tree_vec (collapse
);
24261 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
24263 for (i
= 0; i
< collapse
; i
++)
24265 int bracecount
= 0;
24266 bool add_private_clause
= false;
24269 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
24271 cp_parser_error (parser
, "for statement expected");
24274 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
24276 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
24279 init
= decl
= real_decl
= NULL
;
24280 this_pre_body
= push_stmt_list ();
24281 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24283 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24287 integer-type var = lb
24288 random-access-iterator-type var = lb
24289 pointer-type var = lb
24291 cp_decl_specifier_seq type_specifiers
;
24293 /* First, try to parse as an initialized declaration. See
24294 cp_parser_condition, from whence the bulk of this is copied. */
24296 cp_parser_parse_tentatively (parser
);
24297 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24298 /*is_trailing_return=*/false,
24300 if (cp_parser_parse_definitely (parser
))
24302 /* If parsing a type specifier seq succeeded, then this
24303 MUST be a initialized declaration. */
24304 tree asm_specification
, attributes
;
24305 cp_declarator
*declarator
;
24307 declarator
= cp_parser_declarator (parser
,
24308 CP_PARSER_DECLARATOR_NAMED
,
24309 /*ctor_dtor_or_conv_p=*/NULL
,
24310 /*parenthesized_p=*/NULL
,
24311 /*member_p=*/false);
24312 attributes
= cp_parser_attributes_opt (parser
);
24313 asm_specification
= cp_parser_asm_specification_opt (parser
);
24315 if (declarator
== cp_error_declarator
)
24316 cp_parser_skip_to_end_of_statement (parser
);
24320 tree pushed_scope
, auto_node
;
24322 decl
= start_decl (declarator
, &type_specifiers
,
24323 SD_INITIALIZED
, attributes
,
24324 /*prefix_attributes=*/NULL_TREE
,
24327 auto_node
= type_uses_auto (TREE_TYPE (decl
));
24328 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
24330 if (cp_lexer_next_token_is (parser
->lexer
,
24332 error ("parenthesized initialization is not allowed in "
24333 "OpenMP %<for%> loop");
24335 /* Trigger an error. */
24336 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
24338 init
= error_mark_node
;
24339 cp_parser_skip_to_end_of_statement (parser
);
24341 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
24342 || type_dependent_expression_p (decl
)
24345 bool is_direct_init
, is_non_constant_init
;
24347 init
= cp_parser_initializer (parser
,
24349 &is_non_constant_init
);
24351 if (auto_node
&& describable_type (init
))
24354 = do_auto_deduction (TREE_TYPE (decl
), init
,
24357 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
24358 && !type_dependent_expression_p (decl
))
24362 cp_finish_decl (decl
, init
, !is_non_constant_init
,
24364 LOOKUP_ONLYCONVERTING
);
24365 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
24367 VEC_safe_push (tree
, gc
, for_block
, this_pre_body
);
24371 init
= pop_stmt_list (this_pre_body
);
24372 this_pre_body
= NULL_TREE
;
24377 cp_lexer_consume_token (parser
->lexer
);
24378 init
= cp_parser_assignment_expression (parser
, false, NULL
);
24381 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
24382 init
= error_mark_node
;
24384 cp_finish_decl (decl
, NULL_TREE
,
24385 /*init_const_expr_p=*/false,
24387 LOOKUP_ONLYCONVERTING
);
24391 pop_scope (pushed_scope
);
24397 /* If parsing a type specifier sequence failed, then
24398 this MUST be a simple expression. */
24399 cp_parser_parse_tentatively (parser
);
24400 decl
= cp_parser_primary_expression (parser
, false, false,
24402 if (!cp_parser_error_occurred (parser
)
24405 && CLASS_TYPE_P (TREE_TYPE (decl
)))
24409 cp_parser_parse_definitely (parser
);
24410 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
24411 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
24412 finish_expr_stmt (build_x_modify_expr (decl
, NOP_EXPR
,
24414 tf_warning_or_error
));
24415 add_private_clause
= true;
24420 cp_parser_abort_tentative_parse (parser
);
24421 init
= cp_parser_expression (parser
, false, NULL
);
24424 if (TREE_CODE (init
) == MODIFY_EXPR
24425 || TREE_CODE (init
) == MODOP_EXPR
)
24426 real_decl
= TREE_OPERAND (init
, 0);
24431 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24434 this_pre_body
= pop_stmt_list (this_pre_body
);
24438 pre_body
= push_stmt_list ();
24440 add_stmt (this_pre_body
);
24441 pre_body
= pop_stmt_list (pre_body
);
24444 pre_body
= this_pre_body
;
24449 if (par_clauses
!= NULL
&& real_decl
!= NULL_TREE
)
24452 for (c
= par_clauses
; *c
; )
24453 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
24454 && OMP_CLAUSE_DECL (*c
) == real_decl
)
24456 error_at (loc
, "iteration variable %qD"
24457 " should not be firstprivate", real_decl
);
24458 *c
= OMP_CLAUSE_CHAIN (*c
);
24460 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
24461 && OMP_CLAUSE_DECL (*c
) == real_decl
)
24463 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24464 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
24465 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
24466 OMP_CLAUSE_DECL (l
) = real_decl
;
24467 OMP_CLAUSE_CHAIN (l
) = clauses
;
24468 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
24470 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
24471 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
24472 add_private_clause
= false;
24476 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
24477 && OMP_CLAUSE_DECL (*c
) == real_decl
)
24478 add_private_clause
= false;
24479 c
= &OMP_CLAUSE_CHAIN (*c
);
24483 if (add_private_clause
)
24486 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
24488 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
24489 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
24490 && OMP_CLAUSE_DECL (c
) == decl
)
24492 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
24493 && OMP_CLAUSE_DECL (c
) == decl
)
24494 error_at (loc
, "iteration variable %qD "
24495 "should not be firstprivate",
24497 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
24498 && OMP_CLAUSE_DECL (c
) == decl
)
24499 error_at (loc
, "iteration variable %qD should not be reduction",
24504 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
24505 OMP_CLAUSE_DECL (c
) = decl
;
24506 c
= finish_omp_clauses (c
);
24509 OMP_CLAUSE_CHAIN (c
) = clauses
;
24516 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24517 cond
= cp_parser_omp_for_cond (parser
, decl
);
24518 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24521 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
24523 /* If decl is an iterator, preserve the operator on decl
24524 until finish_omp_for. */
24526 && ((type_dependent_expression_p (decl
)
24527 && !POINTER_TYPE_P (TREE_TYPE (decl
)))
24528 || CLASS_TYPE_P (TREE_TYPE (decl
))))
24529 incr
= cp_parser_omp_for_incr (parser
, decl
);
24531 incr
= cp_parser_expression (parser
, false, NULL
);
24534 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
24535 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
24536 /*or_comma=*/false,
24537 /*consume_paren=*/true);
24539 TREE_VEC_ELT (declv
, i
) = decl
;
24540 TREE_VEC_ELT (initv
, i
) = init
;
24541 TREE_VEC_ELT (condv
, i
) = cond
;
24542 TREE_VEC_ELT (incrv
, i
) = incr
;
24544 if (i
== collapse
- 1)
24547 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24548 in between the collapsed for loops to be still considered perfectly
24549 nested. Hopefully the final version clarifies this.
24550 For now handle (multiple) {'s and empty statements. */
24551 cp_parser_parse_tentatively (parser
);
24554 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
24556 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24558 cp_lexer_consume_token (parser
->lexer
);
24561 else if (bracecount
24562 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24563 cp_lexer_consume_token (parser
->lexer
);
24566 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
24567 error_at (loc
, "not enough collapsed for loops");
24568 collapse_err
= true;
24569 cp_parser_abort_tentative_parse (parser
);
24578 cp_parser_parse_definitely (parser
);
24579 nbraces
+= bracecount
;
24583 /* Note that we saved the original contents of this flag when we entered
24584 the structured block, and so we don't need to re-save it here. */
24585 parser
->in_statement
= IN_OMP_FOR
;
24587 /* Note that the grammar doesn't call for a structured block here,
24588 though the loop as a whole is a structured block. */
24589 body
= push_stmt_list ();
24590 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
24591 body
= pop_stmt_list (body
);
24593 if (declv
== NULL_TREE
)
24596 ret
= finish_omp_for (loc_first
, declv
, initv
, condv
, incrv
, body
,
24597 pre_body
, clauses
);
24601 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
24603 cp_lexer_consume_token (parser
->lexer
);
24606 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24607 cp_lexer_consume_token (parser
->lexer
);
24612 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
24613 "collapsed loops not perfectly nested");
24615 collapse_err
= true;
24616 cp_parser_statement_seq_opt (parser
, NULL
);
24617 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
24622 while (!VEC_empty (tree
, for_block
))
24623 add_stmt (pop_stmt_list (VEC_pop (tree
, for_block
)));
24624 release_tree_vector (for_block
);
24630 #pragma omp for for-clause[optseq] new-line
24633 #define OMP_FOR_CLAUSE_MASK \
24634 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24635 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24636 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24637 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24638 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
24639 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
24640 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
24641 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24644 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
24646 tree clauses
, sb
, ret
;
24649 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
24650 "#pragma omp for", pragma_tok
);
24652 sb
= begin_omp_structured_block ();
24653 save
= cp_parser_begin_omp_structured_block (parser
);
24655 ret
= cp_parser_omp_for_loop (parser
, clauses
, NULL
);
24657 cp_parser_end_omp_structured_block (parser
, save
);
24658 add_stmt (finish_omp_structured_block (sb
));
24664 # pragma omp master new-line
24665 structured-block */
24668 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
24670 cp_parser_require_pragma_eol (parser
, pragma_tok
);
24671 return c_finish_omp_master (input_location
,
24672 cp_parser_omp_structured_block (parser
));
24676 # pragma omp ordered new-line
24677 structured-block */
24680 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
24682 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
24683 cp_parser_require_pragma_eol (parser
, pragma_tok
);
24684 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
24690 { section-sequence }
24693 section-directive[opt] structured-block
24694 section-sequence section-directive structured-block */
24697 cp_parser_omp_sections_scope (cp_parser
*parser
)
24699 tree stmt
, substmt
;
24700 bool error_suppress
= false;
24703 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
24706 stmt
= push_stmt_list ();
24708 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
24712 substmt
= begin_omp_structured_block ();
24713 save
= cp_parser_begin_omp_structured_block (parser
);
24717 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
24719 tok
= cp_lexer_peek_token (parser
->lexer
);
24720 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
24722 if (tok
->type
== CPP_CLOSE_BRACE
)
24724 if (tok
->type
== CPP_EOF
)
24728 cp_parser_end_omp_structured_block (parser
, save
);
24729 substmt
= finish_omp_structured_block (substmt
);
24730 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
24731 add_stmt (substmt
);
24736 tok
= cp_lexer_peek_token (parser
->lexer
);
24737 if (tok
->type
== CPP_CLOSE_BRACE
)
24739 if (tok
->type
== CPP_EOF
)
24742 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
24744 cp_lexer_consume_token (parser
->lexer
);
24745 cp_parser_require_pragma_eol (parser
, tok
);
24746 error_suppress
= false;
24748 else if (!error_suppress
)
24750 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
24751 error_suppress
= true;
24754 substmt
= cp_parser_omp_structured_block (parser
);
24755 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
24756 add_stmt (substmt
);
24758 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
24760 substmt
= pop_stmt_list (stmt
);
24762 stmt
= make_node (OMP_SECTIONS
);
24763 TREE_TYPE (stmt
) = void_type_node
;
24764 OMP_SECTIONS_BODY (stmt
) = substmt
;
24771 # pragma omp sections sections-clause[optseq] newline
24774 #define OMP_SECTIONS_CLAUSE_MASK \
24775 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24776 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24777 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24778 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24779 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24782 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
24786 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
24787 "#pragma omp sections", pragma_tok
);
24789 ret
= cp_parser_omp_sections_scope (parser
);
24791 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
24797 # pragma parallel parallel-clause new-line
24798 # pragma parallel for parallel-for-clause new-line
24799 # pragma parallel sections parallel-sections-clause new-line */
24801 #define OMP_PARALLEL_CLAUSE_MASK \
24802 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24803 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24804 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24805 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24806 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
24807 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
24808 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24809 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24812 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
24814 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
24815 const char *p_name
= "#pragma omp parallel";
24816 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
24817 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
24819 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
24821 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
24823 cp_lexer_consume_token (parser
->lexer
);
24824 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
24825 p_name
= "#pragma omp parallel for";
24826 mask
|= OMP_FOR_CLAUSE_MASK
;
24827 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
24829 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
24831 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
24832 const char *p
= IDENTIFIER_POINTER (id
);
24833 if (strcmp (p
, "sections") == 0)
24835 cp_lexer_consume_token (parser
->lexer
);
24836 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
24837 p_name
= "#pragma omp parallel sections";
24838 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
24839 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
24843 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
24844 block
= begin_omp_parallel ();
24845 save
= cp_parser_begin_omp_structured_block (parser
);
24849 case PRAGMA_OMP_PARALLEL
:
24850 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
24851 par_clause
= clauses
;
24854 case PRAGMA_OMP_PARALLEL_FOR
:
24855 c_split_parallel_clauses (loc
, clauses
, &par_clause
, &ws_clause
);
24856 cp_parser_omp_for_loop (parser
, ws_clause
, &par_clause
);
24859 case PRAGMA_OMP_PARALLEL_SECTIONS
:
24860 c_split_parallel_clauses (loc
, clauses
, &par_clause
, &ws_clause
);
24861 stmt
= cp_parser_omp_sections_scope (parser
);
24863 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
24867 gcc_unreachable ();
24870 cp_parser_end_omp_structured_block (parser
, save
);
24871 stmt
= finish_omp_parallel (par_clause
, block
);
24872 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
24873 OMP_PARALLEL_COMBINED (stmt
) = 1;
24878 # pragma omp single single-clause[optseq] new-line
24879 structured-block */
24881 #define OMP_SINGLE_CLAUSE_MASK \
24882 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24883 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24884 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24885 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24888 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
24890 tree stmt
= make_node (OMP_SINGLE
);
24891 TREE_TYPE (stmt
) = void_type_node
;
24893 OMP_SINGLE_CLAUSES (stmt
)
24894 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
24895 "#pragma omp single", pragma_tok
);
24896 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
24898 return add_stmt (stmt
);
24902 # pragma omp task task-clause[optseq] new-line
24903 structured-block */
24905 #define OMP_TASK_CLAUSE_MASK \
24906 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24907 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
24908 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24909 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24910 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24911 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24914 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
24916 tree clauses
, block
;
24919 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
24920 "#pragma omp task", pragma_tok
);
24921 block
= begin_omp_task ();
24922 save
= cp_parser_begin_omp_structured_block (parser
);
24923 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
24924 cp_parser_end_omp_structured_block (parser
, save
);
24925 return finish_omp_task (clauses
, block
);
24929 # pragma omp taskwait new-line */
24932 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
24934 cp_parser_require_pragma_eol (parser
, pragma_tok
);
24935 finish_omp_taskwait ();
24939 # pragma omp threadprivate (variable-list) */
24942 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
24946 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
24947 cp_parser_require_pragma_eol (parser
, pragma_tok
);
24949 finish_omp_threadprivate (vars
);
24952 /* Main entry point to OpenMP statement pragmas. */
24955 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
24959 switch (pragma_tok
->pragma_kind
)
24961 case PRAGMA_OMP_ATOMIC
:
24962 cp_parser_omp_atomic (parser
, pragma_tok
);
24964 case PRAGMA_OMP_CRITICAL
:
24965 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
24967 case PRAGMA_OMP_FOR
:
24968 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
24970 case PRAGMA_OMP_MASTER
:
24971 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
24973 case PRAGMA_OMP_ORDERED
:
24974 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
24976 case PRAGMA_OMP_PARALLEL
:
24977 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
24979 case PRAGMA_OMP_SECTIONS
:
24980 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
24982 case PRAGMA_OMP_SINGLE
:
24983 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
24985 case PRAGMA_OMP_TASK
:
24986 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
24989 gcc_unreachable ();
24993 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
24998 static GTY (()) cp_parser
*the_parser
;
25001 /* Special handling for the first token or line in the file. The first
25002 thing in the file might be #pragma GCC pch_preprocess, which loads a
25003 PCH file, which is a GC collection point. So we need to handle this
25004 first pragma without benefit of an existing lexer structure.
25006 Always returns one token to the caller in *FIRST_TOKEN. This is
25007 either the true first token of the file, or the first token after
25008 the initial pragma. */
25011 cp_parser_initial_pragma (cp_token
*first_token
)
25015 cp_lexer_get_preprocessor_token (NULL
, first_token
);
25016 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
25019 cp_lexer_get_preprocessor_token (NULL
, first_token
);
25020 if (first_token
->type
== CPP_STRING
)
25022 name
= first_token
->u
.value
;
25024 cp_lexer_get_preprocessor_token (NULL
, first_token
);
25025 if (first_token
->type
!= CPP_PRAGMA_EOL
)
25026 error_at (first_token
->location
,
25027 "junk at end of %<#pragma GCC pch_preprocess%>");
25030 error_at (first_token
->location
, "expected string literal");
25032 /* Skip to the end of the pragma. */
25033 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
25034 cp_lexer_get_preprocessor_token (NULL
, first_token
);
25036 /* Now actually load the PCH file. */
25038 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
25040 /* Read one more token to return to our caller. We have to do this
25041 after reading the PCH file in, since its pointers have to be
25043 cp_lexer_get_preprocessor_token (NULL
, first_token
);
25046 /* Normal parsing of a pragma token. Here we can (and must) use the
25050 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
25052 cp_token
*pragma_tok
;
25055 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
25056 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
25057 parser
->lexer
->in_pragma
= true;
25059 id
= pragma_tok
->pragma_kind
;
25062 case PRAGMA_GCC_PCH_PREPROCESS
:
25063 error_at (pragma_tok
->location
,
25064 "%<#pragma GCC pch_preprocess%> must be first");
25067 case PRAGMA_OMP_BARRIER
:
25070 case pragma_compound
:
25071 cp_parser_omp_barrier (parser
, pragma_tok
);
25074 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
25075 "used in compound statements");
25082 case PRAGMA_OMP_FLUSH
:
25085 case pragma_compound
:
25086 cp_parser_omp_flush (parser
, pragma_tok
);
25089 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
25090 "used in compound statements");
25097 case PRAGMA_OMP_TASKWAIT
:
25100 case pragma_compound
:
25101 cp_parser_omp_taskwait (parser
, pragma_tok
);
25104 error_at (pragma_tok
->location
,
25105 "%<#pragma omp taskwait%> may only be "
25106 "used in compound statements");
25113 case PRAGMA_OMP_THREADPRIVATE
:
25114 cp_parser_omp_threadprivate (parser
, pragma_tok
);
25117 case PRAGMA_OMP_ATOMIC
:
25118 case PRAGMA_OMP_CRITICAL
:
25119 case PRAGMA_OMP_FOR
:
25120 case PRAGMA_OMP_MASTER
:
25121 case PRAGMA_OMP_ORDERED
:
25122 case PRAGMA_OMP_PARALLEL
:
25123 case PRAGMA_OMP_SECTIONS
:
25124 case PRAGMA_OMP_SINGLE
:
25125 case PRAGMA_OMP_TASK
:
25126 if (context
== pragma_external
)
25128 cp_parser_omp_construct (parser
, pragma_tok
);
25131 case PRAGMA_OMP_SECTION
:
25132 error_at (pragma_tok
->location
,
25133 "%<#pragma omp section%> may only be used in "
25134 "%<#pragma omp sections%> construct");
25138 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
25139 c_invoke_pragma_handler (id
);
25143 cp_parser_error (parser
, "expected declaration specifiers");
25147 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
25151 /* The interface the pragma parsers have to the lexer. */
25154 pragma_lex (tree
*value
)
25157 enum cpp_ttype ret
;
25159 tok
= cp_lexer_peek_token (the_parser
->lexer
);
25162 *value
= tok
->u
.value
;
25164 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
25166 else if (ret
== CPP_STRING
)
25167 *value
= cp_parser_string_literal (the_parser
, false, false);
25170 cp_lexer_consume_token (the_parser
->lexer
);
25171 if (ret
== CPP_KEYWORD
)
25179 /* External interface. */
25181 /* Parse one entire translation unit. */
25184 c_parse_file (void)
25186 static bool already_called
= false;
25188 if (already_called
)
25190 sorry ("inter-module optimizations not implemented for C++");
25193 already_called
= true;
25195 the_parser
= cp_parser_new ();
25196 push_deferring_access_checks (flag_access_control
25197 ? dk_no_deferred
: dk_no_check
);
25198 cp_parser_translation_unit (the_parser
);
25202 #include "gt-cp-parser.h"