2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 /* A token's value and its associated deferred access checks and
51 struct GTY(()) tree_check
{
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check
, gc
)* checks
;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope
;
63 typedef struct GTY (()) cp_token
{
64 /* The kind of token. */
65 ENUM_BITFIELD (cpp_ttype
) type
: 8;
66 /* If this token is a keyword, this value indicates which keyword.
67 Otherwise, this value is RID_MAX. */
68 ENUM_BITFIELD (rid
) keyword
: 8;
71 /* Identifier for the pragma. */
72 ENUM_BITFIELD (pragma_kind
) pragma_kind
: 6;
73 /* True if this token is from a context where it is implicitly extern "C" */
74 BOOL_BITFIELD implicit_extern_c
: 1;
75 /* True for a CPP_NAME token that is not a keyword (i.e., for which
76 KEYWORD is RID_MAX) iff this name was looked up and found to be
77 ambiguous. An error has already been reported. */
78 BOOL_BITFIELD ambiguous_p
: 1;
79 /* The location at which this token was found. */
81 /* The value associated with this token, if any. */
82 union cp_token_value
{
83 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
84 struct tree_check
* GTY((tag ("1"))) tree_check_value
;
85 /* Use for all other tokens. */
86 tree
GTY((tag ("0"))) value
;
87 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u
;
90 /* We use a stack of token pointer for saving token sets. */
91 typedef struct cp_token
*cp_token_position
;
92 DEF_VEC_P (cp_token_position
);
93 DEF_VEC_ALLOC_P (cp_token_position
,heap
);
95 static cp_token eof_token
=
97 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, false, 0, 0, { NULL
}
100 /* The cp_lexer structure represents the C++ lexer. It is responsible
101 for managing the token stream from the preprocessor and supplying
102 it to the parser. Tokens are never added to the cp_lexer after
105 typedef struct GTY (()) cp_lexer
{
106 /* The memory allocated for the buffer. NULL if this lexer does not
107 own the token buffer. */
108 cp_token
* GTY ((length ("%h.buffer_length"))) buffer
;
109 /* If the lexer owns the buffer, this is the number of tokens in the
111 size_t buffer_length
;
113 /* A pointer just past the last available token. The tokens
114 in this lexer are [buffer, last_token). */
115 cp_token_position
GTY ((skip
)) last_token
;
117 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
118 no more available tokens. */
119 cp_token_position
GTY ((skip
)) next_token
;
121 /* A stack indicating positions at which cp_lexer_save_tokens was
122 called. The top entry is the most recent position at which we
123 began saving tokens. If the stack is non-empty, we are saving
125 VEC(cp_token_position
,heap
) *GTY ((skip
)) saved_tokens
;
127 /* The next lexer in a linked list of lexers. */
128 struct cp_lexer
*next
;
130 /* True if we should output debugging information. */
133 /* True if we're in the context of parsing a pragma, and should not
134 increment past the end-of-line marker. */
138 /* cp_token_cache is a range of tokens. There is no need to represent
139 allocate heap memory for it, since tokens are never removed from the
140 lexer's array. There is also no need for the GC to walk through
141 a cp_token_cache, since everything in here is referenced through
144 typedef struct GTY(()) cp_token_cache
{
145 /* The beginning of the token range. */
146 cp_token
* GTY((skip
)) first
;
148 /* Points immediately after the last token in the range. */
149 cp_token
* GTY ((skip
)) last
;
154 static cp_lexer
*cp_lexer_new_main
156 static cp_lexer
*cp_lexer_new_from_tokens
157 (cp_token_cache
*tokens
);
158 static void cp_lexer_destroy
160 static int cp_lexer_saving_tokens
162 static cp_token_position cp_lexer_token_position
164 static cp_token
*cp_lexer_token_at
165 (cp_lexer
*, cp_token_position
);
166 static void cp_lexer_get_preprocessor_token
167 (cp_lexer
*, cp_token
*);
168 static inline cp_token
*cp_lexer_peek_token
170 static cp_token
*cp_lexer_peek_nth_token
171 (cp_lexer
*, size_t);
172 static inline bool cp_lexer_next_token_is
173 (cp_lexer
*, enum cpp_ttype
);
174 static bool cp_lexer_next_token_is_not
175 (cp_lexer
*, enum cpp_ttype
);
176 static bool cp_lexer_next_token_is_keyword
177 (cp_lexer
*, enum rid
);
178 static cp_token
*cp_lexer_consume_token
180 static void cp_lexer_purge_token
182 static void cp_lexer_purge_tokens_after
183 (cp_lexer
*, cp_token_position
);
184 static void cp_lexer_save_tokens
186 static void cp_lexer_commit_tokens
188 static void cp_lexer_rollback_tokens
190 #ifdef ENABLE_CHECKING
191 static void cp_lexer_print_token
192 (FILE *, cp_token
*);
193 static inline bool cp_lexer_debugging_p
195 static void cp_lexer_start_debugging
196 (cp_lexer
*) ATTRIBUTE_UNUSED
;
197 static void cp_lexer_stop_debugging
198 (cp_lexer
*) ATTRIBUTE_UNUSED
;
200 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201 about passing NULL to functions that require non-NULL arguments
202 (fputs, fprintf). It will never be used, so all we need is a value
203 of the right type that's guaranteed not to be NULL. */
204 #define cp_lexer_debug_stream stdout
205 #define cp_lexer_print_token(str, tok) (void) 0
206 #define cp_lexer_debugging_p(lexer) 0
207 #endif /* ENABLE_CHECKING */
209 static cp_token_cache
*cp_token_cache_new
210 (cp_token
*, cp_token
*);
212 static void cp_parser_initial_pragma
215 /* Manifest constants. */
216 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
217 #define CP_SAVED_TOKEN_STACK 5
219 /* A token type for keywords, as opposed to ordinary identifiers. */
220 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
222 /* A token type for template-ids. If a template-id is processed while
223 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224 the value of the CPP_TEMPLATE_ID is whatever was returned by
225 cp_parser_template_id. */
226 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
228 /* A token type for nested-name-specifiers. If a
229 nested-name-specifier is processed while parsing tentatively, it is
230 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232 cp_parser_nested_name_specifier_opt. */
233 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
235 /* A token type for tokens that are not tokens at all; these are used
236 to represent slots in the array where there used to be a token
237 that has now been deleted. */
238 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
240 /* The number of token types, including C++-specific ones. */
241 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
245 #ifdef ENABLE_CHECKING
246 /* The stream to which debugging output should be written. */
247 static FILE *cp_lexer_debug_stream
;
248 #endif /* ENABLE_CHECKING */
250 /* Nonzero if we are parsing an unevaluated operand: an operand to
251 sizeof, typeof, or alignof. */
252 int cp_unevaluated_operand
;
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
258 cp_lexer_new_main (void)
260 cp_token first_token
;
267 /* It's possible that parsing the first pragma will load a PCH file,
268 which is a GC collection point. So we have to do that before
269 allocating any memory. */
270 cp_parser_initial_pragma (&first_token
);
272 c_common_no_more_pch ();
274 /* Allocate the memory. */
275 lexer
= GGC_CNEW (cp_lexer
);
277 #ifdef ENABLE_CHECKING
278 /* Initially we are not debugging. */
279 lexer
->debugging_p
= false;
280 #endif /* ENABLE_CHECKING */
281 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
282 CP_SAVED_TOKEN_STACK
);
284 /* Create the buffer. */
285 alloc
= CP_LEXER_BUFFER_SIZE
;
286 buffer
= GGC_NEWVEC (cp_token
, alloc
);
288 /* Put the first token in the buffer. */
293 /* Get the remaining tokens from the preprocessor. */
294 while (pos
->type
!= CPP_EOF
)
301 buffer
= GGC_RESIZEVEC (cp_token
, buffer
, alloc
);
302 pos
= buffer
+ space
;
304 cp_lexer_get_preprocessor_token (lexer
, pos
);
306 lexer
->buffer
= buffer
;
307 lexer
->buffer_length
= alloc
- space
;
308 lexer
->last_token
= pos
;
309 lexer
->next_token
= lexer
->buffer_length
? buffer
: &eof_token
;
311 /* Subsequent preprocessor diagnostics should use compiler
312 diagnostic functions to get the compiler source location. */
315 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
319 /* Create a new lexer whose token stream is primed with the tokens in
320 CACHE. When these tokens are exhausted, no new tokens will be read. */
323 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
325 cp_token
*first
= cache
->first
;
326 cp_token
*last
= cache
->last
;
327 cp_lexer
*lexer
= GGC_CNEW (cp_lexer
);
329 /* We do not own the buffer. */
330 lexer
->buffer
= NULL
;
331 lexer
->buffer_length
= 0;
332 lexer
->next_token
= first
== last
? &eof_token
: first
;
333 lexer
->last_token
= last
;
335 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
336 CP_SAVED_TOKEN_STACK
);
338 #ifdef ENABLE_CHECKING
339 /* Initially we are not debugging. */
340 lexer
->debugging_p
= false;
343 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
347 /* Frees all resources associated with LEXER. */
350 cp_lexer_destroy (cp_lexer
*lexer
)
353 ggc_free (lexer
->buffer
);
354 VEC_free (cp_token_position
, heap
, lexer
->saved_tokens
);
358 /* Returns nonzero if debugging information should be output. */
360 #ifdef ENABLE_CHECKING
363 cp_lexer_debugging_p (cp_lexer
*lexer
)
365 return lexer
->debugging_p
;
368 #endif /* ENABLE_CHECKING */
370 static inline cp_token_position
371 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
373 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
375 return lexer
->next_token
- previous_p
;
378 static inline cp_token
*
379 cp_lexer_token_at (cp_lexer
*lexer ATTRIBUTE_UNUSED
, cp_token_position pos
)
384 /* nonzero if we are presently saving tokens. */
387 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
389 return VEC_length (cp_token_position
, lexer
->saved_tokens
) != 0;
392 /* Store the next token from the preprocessor in *TOKEN. Return true
393 if we reach EOF. If LEXER is NULL, assume we are handling an
394 initial #pragma pch_preprocess, and thus want the lexer to return
395 processed strings. */
398 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
400 static int is_extern_c
= 0;
402 /* Get a new token from the preprocessor. */
404 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
405 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
406 token
->keyword
= RID_MAX
;
407 token
->pragma_kind
= PRAGMA_NONE
;
409 /* On some systems, some header files are surrounded by an
410 implicit extern "C" block. Set a flag in the token if it
411 comes from such a header. */
412 is_extern_c
+= pending_lang_change
;
413 pending_lang_change
= 0;
414 token
->implicit_extern_c
= is_extern_c
> 0;
416 /* Check to see if this token is a keyword. */
417 if (token
->type
== CPP_NAME
)
419 if (C_IS_RESERVED_WORD (token
->u
.value
))
421 /* Mark this token as a keyword. */
422 token
->type
= CPP_KEYWORD
;
423 /* Record which keyword. */
424 token
->keyword
= C_RID_CODE (token
->u
.value
);
428 if (warn_cxx0x_compat
429 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
430 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
432 /* Warn about the C++0x keyword (but still treat it as
434 warning (OPT_Wc__0x_compat
,
435 "identifier %qE will become a keyword in C++0x",
438 /* Clear out the C_RID_CODE so we don't warn about this
439 particular identifier-turned-keyword again. */
440 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
443 token
->ambiguous_p
= false;
444 token
->keyword
= RID_MAX
;
447 /* Handle Objective-C++ keywords. */
448 else if (token
->type
== CPP_AT_NAME
)
450 token
->type
= CPP_KEYWORD
;
451 switch (C_RID_CODE (token
->u
.value
))
453 /* Map 'class' to '@class', 'private' to '@private', etc. */
454 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
455 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
456 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
457 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
458 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
459 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
460 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
461 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
464 else if (token
->type
== CPP_PRAGMA
)
466 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
467 token
->pragma_kind
= ((enum pragma_kind
)
468 TREE_INT_CST_LOW (token
->u
.value
));
469 token
->u
.value
= NULL_TREE
;
473 /* Update the globals input_location and the input file stack from TOKEN. */
475 cp_lexer_set_source_position_from_token (cp_token
*token
)
477 if (token
->type
!= CPP_EOF
)
479 input_location
= token
->location
;
483 /* Return a pointer to the next token in the token stream, but do not
486 static inline cp_token
*
487 cp_lexer_peek_token (cp_lexer
*lexer
)
489 if (cp_lexer_debugging_p (lexer
))
491 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
492 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
493 putc ('\n', cp_lexer_debug_stream
);
495 return lexer
->next_token
;
498 /* Return true if the next token has the indicated TYPE. */
501 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
503 return cp_lexer_peek_token (lexer
)->type
== type
;
506 /* Return true if the next token does not have the indicated TYPE. */
509 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
511 return !cp_lexer_next_token_is (lexer
, type
);
514 /* Return true if the next token is the indicated KEYWORD. */
517 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
519 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
522 /* Return true if the next token is not the indicated KEYWORD. */
525 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
527 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
530 /* Return true if the next token is a keyword for a decl-specifier. */
533 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
537 token
= cp_lexer_peek_token (lexer
);
538 switch (token
->keyword
)
540 /* auto specifier: storage-class-specifier in C++,
541 simple-type-specifier in C++0x. */
543 /* Storage classes. */
549 /* Elaborated type specifiers. */
555 /* Simple type specifiers. */
569 /* GNU extensions. */
572 /* C++0x extensions. */
581 /* Return a pointer to the Nth token in the token stream. If N is 1,
582 then this is precisely equivalent to cp_lexer_peek_token (except
583 that it is not inline). One would like to disallow that case, but
584 there is one case (cp_parser_nth_token_starts_template_id) where
585 the caller passes a variable for N and it might be 1. */
588 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
592 /* N is 1-based, not zero-based. */
595 if (cp_lexer_debugging_p (lexer
))
596 fprintf (cp_lexer_debug_stream
,
597 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
600 token
= lexer
->next_token
;
601 gcc_assert (!n
|| token
!= &eof_token
);
605 if (token
== lexer
->last_token
)
611 if (token
->type
!= CPP_PURGED
)
615 if (cp_lexer_debugging_p (lexer
))
617 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
618 putc ('\n', cp_lexer_debug_stream
);
624 /* Return the next token, and advance the lexer's next_token pointer
625 to point to the next non-purged token. */
628 cp_lexer_consume_token (cp_lexer
* lexer
)
630 cp_token
*token
= lexer
->next_token
;
632 gcc_assert (token
!= &eof_token
);
633 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
638 if (lexer
->next_token
== lexer
->last_token
)
640 lexer
->next_token
= &eof_token
;
645 while (lexer
->next_token
->type
== CPP_PURGED
);
647 cp_lexer_set_source_position_from_token (token
);
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer
))
652 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
653 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
654 putc ('\n', cp_lexer_debug_stream
);
660 /* Permanently remove the next token from the token stream, and
661 advance the next_token pointer to refer to the next non-purged
665 cp_lexer_purge_token (cp_lexer
*lexer
)
667 cp_token
*tok
= lexer
->next_token
;
669 gcc_assert (tok
!= &eof_token
);
670 tok
->type
= CPP_PURGED
;
671 tok
->location
= UNKNOWN_LOCATION
;
672 tok
->u
.value
= NULL_TREE
;
673 tok
->keyword
= RID_MAX
;
678 if (tok
== lexer
->last_token
)
684 while (tok
->type
== CPP_PURGED
);
685 lexer
->next_token
= tok
;
688 /* Permanently remove all tokens after TOK, up to, but not
689 including, the token that will be returned next by
690 cp_lexer_peek_token. */
693 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
695 cp_token
*peek
= lexer
->next_token
;
697 if (peek
== &eof_token
)
698 peek
= lexer
->last_token
;
700 gcc_assert (tok
< peek
);
702 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
704 tok
->type
= CPP_PURGED
;
705 tok
->location
= UNKNOWN_LOCATION
;
706 tok
->u
.value
= NULL_TREE
;
707 tok
->keyword
= RID_MAX
;
711 /* Begin saving tokens. All tokens consumed after this point will be
715 cp_lexer_save_tokens (cp_lexer
* lexer
)
717 /* Provide debugging output. */
718 if (cp_lexer_debugging_p (lexer
))
719 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
721 VEC_safe_push (cp_token_position
, heap
,
722 lexer
->saved_tokens
, lexer
->next_token
);
725 /* Commit to the portion of the token stream most recently saved. */
728 cp_lexer_commit_tokens (cp_lexer
* lexer
)
730 /* Provide debugging output. */
731 if (cp_lexer_debugging_p (lexer
))
732 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
734 VEC_pop (cp_token_position
, lexer
->saved_tokens
);
737 /* Return all tokens saved since the last call to cp_lexer_save_tokens
738 to the token stream. Stop saving tokens. */
741 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
743 /* Provide debugging output. */
744 if (cp_lexer_debugging_p (lexer
))
745 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
747 lexer
->next_token
= VEC_pop (cp_token_position
, lexer
->saved_tokens
);
750 /* Print a representation of the TOKEN on the STREAM. */
752 #ifdef ENABLE_CHECKING
755 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
757 /* We don't use cpp_type2name here because the parser defines
758 a few tokens of its own. */
759 static const char *const token_names
[] = {
760 /* cpplib-defined token types */
766 /* C++ parser token types - see "Manifest constants", above. */
769 "NESTED_NAME_SPECIFIER",
773 /* If we have a name for the token, print it out. Otherwise, we
774 simply give the numeric code. */
775 gcc_assert (token
->type
< ARRAY_SIZE(token_names
));
776 fputs (token_names
[token
->type
], stream
);
778 /* For some tokens, print the associated data. */
782 /* Some keywords have a value that is not an IDENTIFIER_NODE.
783 For example, `struct' is mapped to an INTEGER_CST. */
784 if (TREE_CODE (token
->u
.value
) != IDENTIFIER_NODE
)
786 /* else fall through */
788 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
796 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
804 /* Start emitting debugging information. */
807 cp_lexer_start_debugging (cp_lexer
* lexer
)
809 lexer
->debugging_p
= true;
812 /* Stop emitting debugging information. */
815 cp_lexer_stop_debugging (cp_lexer
* lexer
)
817 lexer
->debugging_p
= false;
820 #endif /* ENABLE_CHECKING */
822 /* Create a new cp_token_cache, representing a range of tokens. */
824 static cp_token_cache
*
825 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
827 cp_token_cache
*cache
= GGC_NEW (cp_token_cache
);
828 cache
->first
= first
;
834 /* Decl-specifiers. */
836 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
839 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
841 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
846 /* Nothing other than the parser should be creating declarators;
847 declarators are a semi-syntactic representation of C++ entities.
848 Other parts of the front end that need to create entities (like
849 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
851 static cp_declarator
*make_call_declarator
852 (cp_declarator
*, tree
, cp_cv_quals
, tree
, tree
);
853 static cp_declarator
*make_array_declarator
854 (cp_declarator
*, tree
);
855 static cp_declarator
*make_pointer_declarator
856 (cp_cv_quals
, cp_declarator
*);
857 static cp_declarator
*make_reference_declarator
858 (cp_cv_quals
, cp_declarator
*, bool);
859 static cp_parameter_declarator
*make_parameter_declarator
860 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
861 static cp_declarator
*make_ptrmem_declarator
862 (cp_cv_quals
, tree
, cp_declarator
*);
864 /* An erroneous declarator. */
865 static cp_declarator
*cp_error_declarator
;
867 /* The obstack on which declarators and related data structures are
869 static struct obstack declarator_obstack
;
871 /* Alloc BYTES from the declarator memory pool. */
874 alloc_declarator (size_t bytes
)
876 return obstack_alloc (&declarator_obstack
, bytes
);
879 /* Allocate a declarator of the indicated KIND. Clear fields that are
880 common to all declarators. */
882 static cp_declarator
*
883 make_declarator (cp_declarator_kind kind
)
885 cp_declarator
*declarator
;
887 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
888 declarator
->kind
= kind
;
889 declarator
->attributes
= NULL_TREE
;
890 declarator
->declarator
= NULL
;
891 declarator
->parameter_pack_p
= false;
892 declarator
->id_loc
= UNKNOWN_LOCATION
;
897 /* Make a declarator for a generalized identifier. If
898 QUALIFYING_SCOPE is non-NULL, the identifier is
899 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
900 UNQUALIFIED_NAME. SFK indicates the kind of special function this
903 static cp_declarator
*
904 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
905 special_function_kind sfk
)
907 cp_declarator
*declarator
;
909 /* It is valid to write:
911 class C { void f(); };
915 The standard is not clear about whether `typedef const C D' is
916 legal; as of 2002-09-15 the committee is considering that
917 question. EDG 3.0 allows that syntax. Therefore, we do as
919 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
920 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
922 gcc_assert (TREE_CODE (unqualified_name
) == IDENTIFIER_NODE
923 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
924 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
926 declarator
= make_declarator (cdk_id
);
927 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
928 declarator
->u
.id
.unqualified_name
= unqualified_name
;
929 declarator
->u
.id
.sfk
= sfk
;
934 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
935 of modifiers such as const or volatile to apply to the pointer
936 type, represented as identifiers. */
939 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
941 cp_declarator
*declarator
;
943 declarator
= make_declarator (cdk_pointer
);
944 declarator
->declarator
= target
;
945 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
946 declarator
->u
.pointer
.class_type
= NULL_TREE
;
949 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
950 target
->parameter_pack_p
= false;
953 declarator
->parameter_pack_p
= false;
958 /* Like make_pointer_declarator -- but for references. */
961 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
964 cp_declarator
*declarator
;
966 declarator
= make_declarator (cdk_reference
);
967 declarator
->declarator
= target
;
968 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
969 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
972 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
973 target
->parameter_pack_p
= false;
976 declarator
->parameter_pack_p
= false;
981 /* Like make_pointer_declarator -- but for a pointer to a non-static
982 member of CLASS_TYPE. */
985 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
986 cp_declarator
*pointee
)
988 cp_declarator
*declarator
;
990 declarator
= make_declarator (cdk_ptrmem
);
991 declarator
->declarator
= pointee
;
992 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
993 declarator
->u
.pointer
.class_type
= class_type
;
997 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
998 pointee
->parameter_pack_p
= false;
1001 declarator
->parameter_pack_p
= false;
1006 /* Make a declarator for the function given by TARGET, with the
1007 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1008 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1009 indicates what exceptions can be thrown. */
1012 make_call_declarator (cp_declarator
*target
,
1014 cp_cv_quals cv_qualifiers
,
1015 tree exception_specification
,
1016 tree late_return_type
)
1018 cp_declarator
*declarator
;
1020 declarator
= make_declarator (cdk_function
);
1021 declarator
->declarator
= target
;
1022 declarator
->u
.function
.parameters
= parms
;
1023 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1024 declarator
->u
.function
.exception_specification
= exception_specification
;
1025 declarator
->u
.function
.late_return_type
= late_return_type
;
1028 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1029 target
->parameter_pack_p
= false;
1032 declarator
->parameter_pack_p
= false;
1037 /* Make a declarator for an array of BOUNDS elements, each of which is
1038 defined by ELEMENT. */
1041 make_array_declarator (cp_declarator
*element
, tree bounds
)
1043 cp_declarator
*declarator
;
1045 declarator
= make_declarator (cdk_array
);
1046 declarator
->declarator
= element
;
1047 declarator
->u
.array
.bounds
= bounds
;
1050 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1051 element
->parameter_pack_p
= false;
1054 declarator
->parameter_pack_p
= false;
1059 /* Determine whether the declarator we've seen so far can be a
1060 parameter pack, when followed by an ellipsis. */
1062 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1064 /* Search for a declarator name, or any other declarator that goes
1065 after the point where the ellipsis could appear in a parameter
1066 pack. If we find any of these, then this declarator can not be
1067 made into a parameter pack. */
1069 while (declarator
&& !found
)
1071 switch ((int)declarator
->kind
)
1082 declarator
= declarator
->declarator
;
1090 cp_parameter_declarator
*no_parameters
;
1092 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1093 DECLARATOR and DEFAULT_ARGUMENT. */
1095 cp_parameter_declarator
*
1096 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1097 cp_declarator
*declarator
,
1098 tree default_argument
)
1100 cp_parameter_declarator
*parameter
;
1102 parameter
= ((cp_parameter_declarator
*)
1103 alloc_declarator (sizeof (cp_parameter_declarator
)));
1104 parameter
->next
= NULL
;
1105 if (decl_specifiers
)
1106 parameter
->decl_specifiers
= *decl_specifiers
;
1108 clear_decl_specs (¶meter
->decl_specifiers
);
1109 parameter
->declarator
= declarator
;
1110 parameter
->default_argument
= default_argument
;
1111 parameter
->ellipsis_p
= false;
1116 /* Returns true iff DECLARATOR is a declaration for a function. */
1119 function_declarator_p (const cp_declarator
*declarator
)
1123 if (declarator
->kind
== cdk_function
1124 && declarator
->declarator
->kind
== cdk_id
)
1126 if (declarator
->kind
== cdk_id
1127 || declarator
->kind
== cdk_error
)
1129 declarator
= declarator
->declarator
;
1139 A cp_parser parses the token stream as specified by the C++
1140 grammar. Its job is purely parsing, not semantic analysis. For
1141 example, the parser breaks the token stream into declarators,
1142 expressions, statements, and other similar syntactic constructs.
1143 It does not check that the types of the expressions on either side
1144 of an assignment-statement are compatible, or that a function is
1145 not declared with a parameter of type `void'.
1147 The parser invokes routines elsewhere in the compiler to perform
1148 semantic analysis and to build up the abstract syntax tree for the
1151 The parser (and the template instantiation code, which is, in a
1152 way, a close relative of parsing) are the only parts of the
1153 compiler that should be calling push_scope and pop_scope, or
1154 related functions. The parser (and template instantiation code)
1155 keeps track of what scope is presently active; everything else
1156 should simply honor that. (The code that generates static
1157 initializers may also need to set the scope, in order to check
1158 access control correctly when emitting the initializers.)
1163 The parser is of the standard recursive-descent variety. Upcoming
1164 tokens in the token stream are examined in order to determine which
1165 production to use when parsing a non-terminal. Some C++ constructs
1166 require arbitrary look ahead to disambiguate. For example, it is
1167 impossible, in the general case, to tell whether a statement is an
1168 expression or declaration without scanning the entire statement.
1169 Therefore, the parser is capable of "parsing tentatively." When the
1170 parser is not sure what construct comes next, it enters this mode.
1171 Then, while we attempt to parse the construct, the parser queues up
1172 error messages, rather than issuing them immediately, and saves the
1173 tokens it consumes. If the construct is parsed successfully, the
1174 parser "commits", i.e., it issues any queued error messages and
1175 the tokens that were being preserved are permanently discarded.
1176 If, however, the construct is not parsed successfully, the parser
1177 rolls back its state completely so that it can resume parsing using
1178 a different alternative.
1183 The performance of the parser could probably be improved substantially.
1184 We could often eliminate the need to parse tentatively by looking ahead
1185 a little bit. In some places, this approach might not entirely eliminate
1186 the need to parse tentatively, but it might still speed up the average
1189 /* Flags that are passed to some parsing functions. These values can
1190 be bitwise-ored together. */
1195 CP_PARSER_FLAGS_NONE
= 0x0,
1196 /* The construct is optional. If it is not present, then no error
1197 should be issued. */
1198 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1199 /* When parsing a type-specifier, treat user-defined type-names
1200 as non-type identifiers. */
1201 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1202 /* When parsing a type-specifier, do not try to parse a class-specifier
1203 or enum-specifier. */
1204 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4
1207 /* This type is used for parameters and variables which hold
1208 combinations of the above flags. */
1209 typedef int cp_parser_flags
;
1211 /* The different kinds of declarators we want to parse. */
1213 typedef enum cp_parser_declarator_kind
1215 /* We want an abstract declarator. */
1216 CP_PARSER_DECLARATOR_ABSTRACT
,
1217 /* We want a named declarator. */
1218 CP_PARSER_DECLARATOR_NAMED
,
1219 /* We don't mind, but the name must be an unqualified-id. */
1220 CP_PARSER_DECLARATOR_EITHER
1221 } cp_parser_declarator_kind
;
1223 /* The precedence values used to parse binary expressions. The minimum value
1224 of PREC must be 1, because zero is reserved to quickly discriminate
1225 binary operators from other tokens. */
1230 PREC_LOGICAL_OR_EXPRESSION
,
1231 PREC_LOGICAL_AND_EXPRESSION
,
1232 PREC_INCLUSIVE_OR_EXPRESSION
,
1233 PREC_EXCLUSIVE_OR_EXPRESSION
,
1234 PREC_AND_EXPRESSION
,
1235 PREC_EQUALITY_EXPRESSION
,
1236 PREC_RELATIONAL_EXPRESSION
,
1237 PREC_SHIFT_EXPRESSION
,
1238 PREC_ADDITIVE_EXPRESSION
,
1239 PREC_MULTIPLICATIVE_EXPRESSION
,
1241 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1244 /* A mapping from a token type to a corresponding tree node type, with a
1245 precedence value. */
1247 typedef struct cp_parser_binary_operations_map_node
1249 /* The token type. */
1250 enum cpp_ttype token_type
;
1251 /* The corresponding tree code. */
1252 enum tree_code tree_type
;
1253 /* The precedence of this operator. */
1254 enum cp_parser_prec prec
;
1255 } cp_parser_binary_operations_map_node
;
1257 /* The status of a tentative parse. */
1259 typedef enum cp_parser_status_kind
1261 /* No errors have occurred. */
1262 CP_PARSER_STATUS_KIND_NO_ERROR
,
1263 /* An error has occurred. */
1264 CP_PARSER_STATUS_KIND_ERROR
,
1265 /* We are committed to this tentative parse, whether or not an error
1267 CP_PARSER_STATUS_KIND_COMMITTED
1268 } cp_parser_status_kind
;
1270 typedef struct cp_parser_expression_stack_entry
1272 /* Left hand side of the binary operation we are currently
1275 /* Original tree code for left hand side, if it was a binary
1276 expression itself (used for -Wparentheses). */
1277 enum tree_code lhs_type
;
1278 /* Tree code for the binary operation we are parsing. */
1279 enum tree_code tree_type
;
1280 /* Precedence of the binary operation we are parsing. */
1281 enum cp_parser_prec prec
;
1282 } cp_parser_expression_stack_entry
;
1284 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1285 entries because precedence levels on the stack are monotonically
1287 typedef struct cp_parser_expression_stack_entry
1288 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1290 /* Context that is saved and restored when parsing tentatively. */
1291 typedef struct GTY (()) cp_parser_context
{
1292 /* If this is a tentative parsing context, the status of the
1294 enum cp_parser_status_kind status
;
1295 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1296 that are looked up in this context must be looked up both in the
1297 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1298 the context of the containing expression. */
1301 /* The next parsing context in the stack. */
1302 struct cp_parser_context
*next
;
1303 } cp_parser_context
;
1307 /* Constructors and destructors. */
1309 static cp_parser_context
*cp_parser_context_new
1310 (cp_parser_context
*);
1312 /* Class variables. */
1314 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1316 /* The operator-precedence table used by cp_parser_binary_expression.
1317 Transformed into an associative array (binops_by_token) by
1320 static const cp_parser_binary_operations_map_node binops
[] = {
1321 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1322 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1324 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1325 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1326 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1328 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1329 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1331 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1332 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1334 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1335 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1336 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1337 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1339 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1340 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1342 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1344 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1346 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1348 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1350 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1353 /* The same as binops, but initialized by cp_parser_new so that
1354 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1356 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1358 /* Constructors and destructors. */
1360 /* Construct a new context. The context below this one on the stack
1361 is given by NEXT. */
1363 static cp_parser_context
*
1364 cp_parser_context_new (cp_parser_context
* next
)
1366 cp_parser_context
*context
;
1368 /* Allocate the storage. */
1369 if (cp_parser_context_free_list
!= NULL
)
1371 /* Pull the first entry from the free list. */
1372 context
= cp_parser_context_free_list
;
1373 cp_parser_context_free_list
= context
->next
;
1374 memset (context
, 0, sizeof (*context
));
1377 context
= GGC_CNEW (cp_parser_context
);
1379 /* No errors have occurred yet in this context. */
1380 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1381 /* If this is not the bottommost context, copy information that we
1382 need from the previous context. */
1385 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1386 expression, then we are parsing one in this context, too. */
1387 context
->object_type
= next
->object_type
;
1388 /* Thread the stack. */
1389 context
->next
= next
;
1395 /* The cp_parser structure represents the C++ parser. */
1397 typedef struct GTY(()) cp_parser
{
1398 /* The lexer from which we are obtaining tokens. */
1401 /* The scope in which names should be looked up. If NULL_TREE, then
1402 we look up names in the scope that is currently open in the
1403 source program. If non-NULL, this is either a TYPE or
1404 NAMESPACE_DECL for the scope in which we should look. It can
1405 also be ERROR_MARK, when we've parsed a bogus scope.
1407 This value is not cleared automatically after a name is looked
1408 up, so we must be careful to clear it before starting a new look
1409 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1410 will look up `Z' in the scope of `X', rather than the current
1411 scope.) Unfortunately, it is difficult to tell when name lookup
1412 is complete, because we sometimes peek at a token, look it up,
1413 and then decide not to consume it. */
1416 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1417 last lookup took place. OBJECT_SCOPE is used if an expression
1418 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1419 respectively. QUALIFYING_SCOPE is used for an expression of the
1420 form "X::Y"; it refers to X. */
1422 tree qualifying_scope
;
1424 /* A stack of parsing contexts. All but the bottom entry on the
1425 stack will be tentative contexts.
1427 We parse tentatively in order to determine which construct is in
1428 use in some situations. For example, in order to determine
1429 whether a statement is an expression-statement or a
1430 declaration-statement we parse it tentatively as a
1431 declaration-statement. If that fails, we then reparse the same
1432 token stream as an expression-statement. */
1433 cp_parser_context
*context
;
1435 /* True if we are parsing GNU C++. If this flag is not set, then
1436 GNU extensions are not recognized. */
1437 bool allow_gnu_extensions_p
;
1439 /* TRUE if the `>' token should be interpreted as the greater-than
1440 operator. FALSE if it is the end of a template-id or
1441 template-parameter-list. In C++0x mode, this flag also applies to
1442 `>>' tokens, which are viewed as two consecutive `>' tokens when
1443 this flag is FALSE. */
1444 bool greater_than_is_operator_p
;
1446 /* TRUE if default arguments are allowed within a parameter list
1447 that starts at this point. FALSE if only a gnu extension makes
1448 them permissible. */
1449 bool default_arg_ok_p
;
1451 /* TRUE if we are parsing an integral constant-expression. See
1452 [expr.const] for a precise definition. */
1453 bool integral_constant_expression_p
;
1455 /* TRUE if we are parsing an integral constant-expression -- but a
1456 non-constant expression should be permitted as well. This flag
1457 is used when parsing an array bound so that GNU variable-length
1458 arrays are tolerated. */
1459 bool allow_non_integral_constant_expression_p
;
1461 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1462 been seen that makes the expression non-constant. */
1463 bool non_integral_constant_expression_p
;
1465 /* TRUE if local variable names and `this' are forbidden in the
1467 bool local_variables_forbidden_p
;
1469 /* TRUE if the declaration we are parsing is part of a
1470 linkage-specification of the form `extern string-literal
1472 bool in_unbraced_linkage_specification_p
;
1474 /* TRUE if we are presently parsing a declarator, after the
1475 direct-declarator. */
1476 bool in_declarator_p
;
1478 /* TRUE if we are presently parsing a template-argument-list. */
1479 bool in_template_argument_list_p
;
1481 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1482 to IN_OMP_BLOCK if parsing OpenMP structured block and
1483 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1484 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1485 iteration-statement, OpenMP block or loop within that switch. */
1486 #define IN_SWITCH_STMT 1
1487 #define IN_ITERATION_STMT 2
1488 #define IN_OMP_BLOCK 4
1489 #define IN_OMP_FOR 8
1490 #define IN_IF_STMT 16
1491 unsigned char in_statement
;
1493 /* TRUE if we are presently parsing the body of a switch statement.
1494 Note that this doesn't quite overlap with in_statement above.
1495 The difference relates to giving the right sets of error messages:
1496 "case not in switch" vs "break statement used with OpenMP...". */
1497 bool in_switch_statement_p
;
1499 /* TRUE if we are parsing a type-id in an expression context. In
1500 such a situation, both "type (expr)" and "type (type)" are valid
1502 bool in_type_id_in_expr_p
;
1504 /* TRUE if we are currently in a header file where declarations are
1505 implicitly extern "C". */
1506 bool implicit_extern_c
;
1508 /* TRUE if strings in expressions should be translated to the execution
1510 bool translate_strings_p
;
1512 /* TRUE if we are presently parsing the body of a function, but not
1514 bool in_function_body
;
1516 /* If non-NULL, then we are parsing a construct where new type
1517 definitions are not permitted. The string stored here will be
1518 issued as an error message if a type is defined. */
1519 const char *type_definition_forbidden_message
;
1521 /* A list of lists. The outer list is a stack, used for member
1522 functions of local classes. At each level there are two sub-list,
1523 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1524 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1525 TREE_VALUE's. The functions are chained in reverse declaration
1528 The TREE_PURPOSE sublist contains those functions with default
1529 arguments that need post processing, and the TREE_VALUE sublist
1530 contains those functions with definitions that need post
1533 These lists can only be processed once the outermost class being
1534 defined is complete. */
1535 tree unparsed_functions_queues
;
1537 /* The number of classes whose definitions are currently in
1539 unsigned num_classes_being_defined
;
1541 /* The number of template parameter lists that apply directly to the
1542 current declaration. */
1543 unsigned num_template_parameter_lists
;
1548 /* Constructors and destructors. */
1550 static cp_parser
*cp_parser_new
1553 /* Routines to parse various constructs.
1555 Those that return `tree' will return the error_mark_node (rather
1556 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1557 Sometimes, they will return an ordinary node if error-recovery was
1558 attempted, even though a parse error occurred. So, to check
1559 whether or not a parse error occurred, you should always use
1560 cp_parser_error_occurred. If the construct is optional (indicated
1561 either by an `_opt' in the name of the function that does the
1562 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1563 the construct is not present. */
1565 /* Lexical conventions [gram.lex] */
1567 static tree cp_parser_identifier
1569 static tree cp_parser_string_literal
1570 (cp_parser
*, bool, bool);
1572 /* Basic concepts [gram.basic] */
1574 static bool cp_parser_translation_unit
1577 /* Expressions [gram.expr] */
1579 static tree cp_parser_primary_expression
1580 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1581 static tree cp_parser_id_expression
1582 (cp_parser
*, bool, bool, bool *, bool, bool);
1583 static tree cp_parser_unqualified_id
1584 (cp_parser
*, bool, bool, bool, bool);
1585 static tree cp_parser_nested_name_specifier_opt
1586 (cp_parser
*, bool, bool, bool, bool);
1587 static tree cp_parser_nested_name_specifier
1588 (cp_parser
*, bool, bool, bool, bool);
1589 static tree cp_parser_qualifying_entity
1590 (cp_parser
*, bool, bool, bool, bool, bool);
1591 static tree cp_parser_postfix_expression
1592 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1593 static tree cp_parser_postfix_open_square_expression
1594 (cp_parser
*, tree
, bool);
1595 static tree cp_parser_postfix_dot_deref_expression
1596 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1597 static VEC(tree
,gc
) *cp_parser_parenthesized_expression_list
1598 (cp_parser
*, int, bool, bool, bool *);
1599 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1600 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1601 static void cp_parser_pseudo_destructor_name
1602 (cp_parser
*, tree
*, tree
*);
1603 static tree cp_parser_unary_expression
1604 (cp_parser
*, bool, bool, cp_id_kind
*);
1605 static enum tree_code cp_parser_unary_operator
1607 static tree cp_parser_new_expression
1609 static VEC(tree
,gc
) *cp_parser_new_placement
1611 static tree cp_parser_new_type_id
1612 (cp_parser
*, tree
*);
1613 static cp_declarator
*cp_parser_new_declarator_opt
1615 static cp_declarator
*cp_parser_direct_new_declarator
1617 static VEC(tree
,gc
) *cp_parser_new_initializer
1619 static tree cp_parser_delete_expression
1621 static tree cp_parser_cast_expression
1622 (cp_parser
*, bool, bool, cp_id_kind
*);
1623 static tree cp_parser_binary_expression
1624 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
1625 static tree cp_parser_question_colon_clause
1626 (cp_parser
*, tree
);
1627 static tree cp_parser_assignment_expression
1628 (cp_parser
*, bool, cp_id_kind
*);
1629 static enum tree_code cp_parser_assignment_operator_opt
1631 static tree cp_parser_expression
1632 (cp_parser
*, bool, cp_id_kind
*);
1633 static tree cp_parser_constant_expression
1634 (cp_parser
*, bool, bool *);
1635 static tree cp_parser_builtin_offsetof
1637 static tree cp_parser_lambda_expression
1639 static void cp_parser_lambda_introducer
1640 (cp_parser
*, tree
);
1641 static void cp_parser_lambda_declarator_opt
1642 (cp_parser
*, tree
);
1643 static void cp_parser_lambda_body
1644 (cp_parser
*, tree
);
1646 /* Statements [gram.stmt.stmt] */
1648 static void cp_parser_statement
1649 (cp_parser
*, tree
, bool, bool *);
1650 static void cp_parser_label_for_labeled_statement
1652 static tree cp_parser_expression_statement
1653 (cp_parser
*, tree
);
1654 static tree cp_parser_compound_statement
1655 (cp_parser
*, tree
, bool);
1656 static void cp_parser_statement_seq_opt
1657 (cp_parser
*, tree
);
1658 static tree cp_parser_selection_statement
1659 (cp_parser
*, bool *);
1660 static tree cp_parser_condition
1662 static tree cp_parser_iteration_statement
1664 static void cp_parser_for_init_statement
1666 static tree cp_parser_jump_statement
1668 static void cp_parser_declaration_statement
1671 static tree cp_parser_implicitly_scoped_statement
1672 (cp_parser
*, bool *);
1673 static void cp_parser_already_scoped_statement
1676 /* Declarations [gram.dcl.dcl] */
1678 static void cp_parser_declaration_seq_opt
1680 static void cp_parser_declaration
1682 static void cp_parser_block_declaration
1683 (cp_parser
*, bool);
1684 static void cp_parser_simple_declaration
1685 (cp_parser
*, bool);
1686 static void cp_parser_decl_specifier_seq
1687 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1688 static tree cp_parser_storage_class_specifier_opt
1690 static tree cp_parser_function_specifier_opt
1691 (cp_parser
*, cp_decl_specifier_seq
*);
1692 static tree cp_parser_type_specifier
1693 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1695 static tree cp_parser_simple_type_specifier
1696 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1697 static tree cp_parser_type_name
1699 static tree cp_parser_nonclass_name
1700 (cp_parser
* parser
);
1701 static tree cp_parser_elaborated_type_specifier
1702 (cp_parser
*, bool, bool);
1703 static tree cp_parser_enum_specifier
1705 static void cp_parser_enumerator_list
1706 (cp_parser
*, tree
);
1707 static void cp_parser_enumerator_definition
1708 (cp_parser
*, tree
);
1709 static tree cp_parser_namespace_name
1711 static void cp_parser_namespace_definition
1713 static void cp_parser_namespace_body
1715 static tree cp_parser_qualified_namespace_specifier
1717 static void cp_parser_namespace_alias_definition
1719 static bool cp_parser_using_declaration
1720 (cp_parser
*, bool);
1721 static void cp_parser_using_directive
1723 static void cp_parser_asm_definition
1725 static void cp_parser_linkage_specification
1727 static void cp_parser_static_assert
1728 (cp_parser
*, bool);
1729 static tree cp_parser_decltype
1732 /* Declarators [gram.dcl.decl] */
1734 static tree cp_parser_init_declarator
1735 (cp_parser
*, cp_decl_specifier_seq
*, VEC (deferred_access_check
,gc
)*, bool, bool, int, bool *);
1736 static cp_declarator
*cp_parser_declarator
1737 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1738 static cp_declarator
*cp_parser_direct_declarator
1739 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1740 static enum tree_code cp_parser_ptr_operator
1741 (cp_parser
*, tree
*, cp_cv_quals
*);
1742 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1744 static tree cp_parser_late_return_type_opt
1746 static tree cp_parser_declarator_id
1747 (cp_parser
*, bool);
1748 static tree cp_parser_type_id
1750 static tree cp_parser_template_type_arg
1752 static tree
cp_parser_trailing_type_id (cp_parser
*);
1753 static tree cp_parser_type_id_1
1754 (cp_parser
*, bool, bool);
1755 static void cp_parser_type_specifier_seq
1756 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
1757 static tree cp_parser_parameter_declaration_clause
1759 static tree cp_parser_parameter_declaration_list
1760 (cp_parser
*, bool *);
1761 static cp_parameter_declarator
*cp_parser_parameter_declaration
1762 (cp_parser
*, bool, bool *);
1763 static tree cp_parser_default_argument
1764 (cp_parser
*, bool);
1765 static void cp_parser_function_body
1767 static tree cp_parser_initializer
1768 (cp_parser
*, bool *, bool *);
1769 static tree cp_parser_initializer_clause
1770 (cp_parser
*, bool *);
1771 static tree cp_parser_braced_list
1772 (cp_parser
*, bool*);
1773 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1774 (cp_parser
*, bool *);
1776 static bool cp_parser_ctor_initializer_opt_and_function_body
1779 /* Classes [gram.class] */
1781 static tree cp_parser_class_name
1782 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1783 static tree cp_parser_class_specifier
1785 static tree cp_parser_class_head
1786 (cp_parser
*, bool *, tree
*, tree
*);
1787 static enum tag_types cp_parser_class_key
1789 static void cp_parser_member_specification_opt
1791 static void cp_parser_member_declaration
1793 static tree cp_parser_pure_specifier
1795 static tree cp_parser_constant_initializer
1798 /* Derived classes [gram.class.derived] */
1800 static tree cp_parser_base_clause
1802 static tree cp_parser_base_specifier
1805 /* Special member functions [gram.special] */
1807 static tree cp_parser_conversion_function_id
1809 static tree cp_parser_conversion_type_id
1811 static cp_declarator
*cp_parser_conversion_declarator_opt
1813 static bool cp_parser_ctor_initializer_opt
1815 static void cp_parser_mem_initializer_list
1817 static tree cp_parser_mem_initializer
1819 static tree cp_parser_mem_initializer_id
1822 /* Overloading [gram.over] */
1824 static tree cp_parser_operator_function_id
1826 static tree cp_parser_operator
1829 /* Templates [gram.temp] */
1831 static void cp_parser_template_declaration
1832 (cp_parser
*, bool);
1833 static tree cp_parser_template_parameter_list
1835 static tree cp_parser_template_parameter
1836 (cp_parser
*, bool *, bool *);
1837 static tree cp_parser_type_parameter
1838 (cp_parser
*, bool *);
1839 static tree cp_parser_template_id
1840 (cp_parser
*, bool, bool, bool);
1841 static tree cp_parser_template_name
1842 (cp_parser
*, bool, bool, bool, bool *);
1843 static tree cp_parser_template_argument_list
1845 static tree cp_parser_template_argument
1847 static void cp_parser_explicit_instantiation
1849 static void cp_parser_explicit_specialization
1852 /* Exception handling [gram.exception] */
1854 static tree cp_parser_try_block
1856 static bool cp_parser_function_try_block
1858 static void cp_parser_handler_seq
1860 static void cp_parser_handler
1862 static tree cp_parser_exception_declaration
1864 static tree cp_parser_throw_expression
1866 static tree cp_parser_exception_specification_opt
1868 static tree cp_parser_type_id_list
1871 /* GNU Extensions */
1873 static tree cp_parser_asm_specification_opt
1875 static tree cp_parser_asm_operand_list
1877 static tree cp_parser_asm_clobber_list
1879 static tree cp_parser_asm_label_list
1881 static tree cp_parser_attributes_opt
1883 static tree cp_parser_attribute_list
1885 static bool cp_parser_extension_opt
1886 (cp_parser
*, int *);
1887 static void cp_parser_label_declaration
1890 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
1891 static bool cp_parser_pragma
1892 (cp_parser
*, enum pragma_context
);
1894 /* Objective-C++ Productions */
1896 static tree cp_parser_objc_message_receiver
1898 static tree cp_parser_objc_message_args
1900 static tree cp_parser_objc_message_expression
1902 static tree cp_parser_objc_encode_expression
1904 static tree cp_parser_objc_defs_expression
1906 static tree cp_parser_objc_protocol_expression
1908 static tree cp_parser_objc_selector_expression
1910 static tree cp_parser_objc_expression
1912 static bool cp_parser_objc_selector_p
1914 static tree cp_parser_objc_selector
1916 static tree cp_parser_objc_protocol_refs_opt
1918 static void cp_parser_objc_declaration
1920 static tree cp_parser_objc_statement
1923 /* Utility Routines */
1925 static tree cp_parser_lookup_name
1926 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
1927 static tree cp_parser_lookup_name_simple
1928 (cp_parser
*, tree
, location_t
);
1929 static tree cp_parser_maybe_treat_template_as_class
1931 static bool cp_parser_check_declarator_template_parameters
1932 (cp_parser
*, cp_declarator
*, location_t
);
1933 static bool cp_parser_check_template_parameters
1934 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
1935 static tree cp_parser_simple_cast_expression
1937 static tree cp_parser_global_scope_opt
1938 (cp_parser
*, bool);
1939 static bool cp_parser_constructor_declarator_p
1940 (cp_parser
*, bool);
1941 static tree cp_parser_function_definition_from_specifiers_and_declarator
1942 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1943 static tree cp_parser_function_definition_after_declarator
1944 (cp_parser
*, bool);
1945 static void cp_parser_template_declaration_after_export
1946 (cp_parser
*, bool);
1947 static void cp_parser_perform_template_parameter_access_checks
1948 (VEC (deferred_access_check
,gc
)*);
1949 static tree cp_parser_single_declaration
1950 (cp_parser
*, VEC (deferred_access_check
,gc
)*, bool, bool, bool *);
1951 static tree cp_parser_functional_cast
1952 (cp_parser
*, tree
);
1953 static tree cp_parser_save_member_function_body
1954 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1955 static tree cp_parser_enclosed_template_argument_list
1957 static void cp_parser_save_default_args
1958 (cp_parser
*, tree
);
1959 static void cp_parser_late_parsing_for_member
1960 (cp_parser
*, tree
);
1961 static void cp_parser_late_parsing_default_args
1962 (cp_parser
*, tree
);
1963 static tree cp_parser_sizeof_operand
1964 (cp_parser
*, enum rid
);
1965 static tree cp_parser_trait_expr
1966 (cp_parser
*, enum rid
);
1967 static bool cp_parser_declares_only_class_p
1969 static void cp_parser_set_storage_class
1970 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, location_t
);
1971 static void cp_parser_set_decl_spec_type
1972 (cp_decl_specifier_seq
*, tree
, location_t
, bool);
1973 static bool cp_parser_friend_p
1974 (const cp_decl_specifier_seq
*);
1975 static cp_token
*cp_parser_require
1976 (cp_parser
*, enum cpp_ttype
, const char *);
1977 static cp_token
*cp_parser_require_keyword
1978 (cp_parser
*, enum rid
, const char *);
1979 static bool cp_parser_token_starts_function_definition_p
1981 static bool cp_parser_next_token_starts_class_definition_p
1983 static bool cp_parser_next_token_ends_template_argument_p
1985 static bool cp_parser_nth_token_starts_template_argument_list_p
1986 (cp_parser
*, size_t);
1987 static enum tag_types cp_parser_token_is_class_key
1989 static void cp_parser_check_class_key
1990 (enum tag_types
, tree type
);
1991 static void cp_parser_check_access_in_redeclaration
1992 (tree type
, location_t location
);
1993 static bool cp_parser_optional_template_keyword
1995 static void cp_parser_pre_parsed_nested_name_specifier
1997 static bool cp_parser_cache_group
1998 (cp_parser
*, enum cpp_ttype
, unsigned);
1999 static void cp_parser_parse_tentatively
2001 static void cp_parser_commit_to_tentative_parse
2003 static void cp_parser_abort_tentative_parse
2005 static bool cp_parser_parse_definitely
2007 static inline bool cp_parser_parsing_tentatively
2009 static bool cp_parser_uncommitted_to_tentative_parse_p
2011 static void cp_parser_error
2012 (cp_parser
*, const char *);
2013 static void cp_parser_name_lookup_error
2014 (cp_parser
*, tree
, tree
, const char *, location_t
);
2015 static bool cp_parser_simulate_error
2017 static bool cp_parser_check_type_definition
2019 static void cp_parser_check_for_definition_in_return_type
2020 (cp_declarator
*, tree
, location_t type_location
);
2021 static void cp_parser_check_for_invalid_template_id
2022 (cp_parser
*, tree
, location_t location
);
2023 static bool cp_parser_non_integral_constant_expression
2024 (cp_parser
*, const char *);
2025 static void cp_parser_diagnose_invalid_type_name
2026 (cp_parser
*, tree
, tree
, location_t
);
2027 static bool cp_parser_parse_and_diagnose_invalid_type_name
2029 static int cp_parser_skip_to_closing_parenthesis
2030 (cp_parser
*, bool, bool, bool);
2031 static void cp_parser_skip_to_end_of_statement
2033 static void cp_parser_consume_semicolon_at_end_of_statement
2035 static void cp_parser_skip_to_end_of_block_or_statement
2037 static bool cp_parser_skip_to_closing_brace
2039 static void cp_parser_skip_to_end_of_template_parameter_list
2041 static void cp_parser_skip_to_pragma_eol
2042 (cp_parser
*, cp_token
*);
2043 static bool cp_parser_error_occurred
2045 static bool cp_parser_allow_gnu_extensions_p
2047 static bool cp_parser_is_string_literal
2049 static bool cp_parser_is_keyword
2050 (cp_token
*, enum rid
);
2051 static tree cp_parser_make_typename_type
2052 (cp_parser
*, tree
, tree
, location_t location
);
2053 static cp_declarator
* cp_parser_make_indirect_declarator
2054 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*);
2056 /* Returns nonzero if we are parsing tentatively. */
2059 cp_parser_parsing_tentatively (cp_parser
* parser
)
2061 return parser
->context
->next
!= NULL
;
2064 /* Returns nonzero if TOKEN is a string literal. */
2067 cp_parser_is_string_literal (cp_token
* token
)
2069 return (token
->type
== CPP_STRING
||
2070 token
->type
== CPP_STRING16
||
2071 token
->type
== CPP_STRING32
||
2072 token
->type
== CPP_WSTRING
||
2073 token
->type
== CPP_UTF8STRING
);
2076 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2079 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2081 return token
->keyword
== keyword
;
2084 /* If not parsing tentatively, issue a diagnostic of the form
2085 FILE:LINE: MESSAGE before TOKEN
2086 where TOKEN is the next token in the input stream. MESSAGE
2087 (specified by the caller) is usually of the form "expected
2091 cp_parser_error (cp_parser
* parser
, const char* message
)
2093 if (!cp_parser_simulate_error (parser
))
2095 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2096 /* This diagnostic makes more sense if it is tagged to the line
2097 of the token we just peeked at. */
2098 cp_lexer_set_source_position_from_token (token
);
2100 if (token
->type
== CPP_PRAGMA
)
2102 error_at (token
->location
,
2103 "%<#pragma%> is not allowed here");
2104 cp_parser_skip_to_pragma_eol (parser
, token
);
2108 c_parse_error (message
,
2109 /* Because c_parser_error does not understand
2110 CPP_KEYWORD, keywords are treated like
2112 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2113 token
->u
.value
, token
->flags
);
2117 /* Issue an error about name-lookup failing. NAME is the
2118 IDENTIFIER_NODE DECL is the result of
2119 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2120 the thing that we hoped to find. */
2123 cp_parser_name_lookup_error (cp_parser
* parser
,
2126 const char* desired
,
2127 location_t location
)
2129 /* If name lookup completely failed, tell the user that NAME was not
2131 if (decl
== error_mark_node
)
2133 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2134 error_at (location
, "%<%E::%E%> has not been declared",
2135 parser
->scope
, name
);
2136 else if (parser
->scope
== global_namespace
)
2137 error_at (location
, "%<::%E%> has not been declared", name
);
2138 else if (parser
->object_scope
2139 && !CLASS_TYPE_P (parser
->object_scope
))
2140 error_at (location
, "request for member %qE in non-class type %qT",
2141 name
, parser
->object_scope
);
2142 else if (parser
->object_scope
)
2143 error_at (location
, "%<%T::%E%> has not been declared",
2144 parser
->object_scope
, name
);
2146 error_at (location
, "%qE has not been declared", name
);
2148 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2149 error_at (location
, "%<%E::%E%> %s", parser
->scope
, name
, desired
);
2150 else if (parser
->scope
== global_namespace
)
2151 error_at (location
, "%<::%E%> %s", name
, desired
);
2153 error_at (location
, "%qE %s", name
, desired
);
2156 /* If we are parsing tentatively, remember that an error has occurred
2157 during this tentative parse. Returns true if the error was
2158 simulated; false if a message should be issued by the caller. */
2161 cp_parser_simulate_error (cp_parser
* parser
)
2163 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2165 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2171 /* Check for repeated decl-specifiers. */
2174 cp_parser_check_decl_spec (cp_decl_specifier_seq
*decl_specs
,
2175 location_t location
)
2179 for (ds
= ds_first
; ds
!= ds_last
; ++ds
)
2181 unsigned count
= decl_specs
->specs
[ds
];
2184 /* The "long" specifier is a special case because of "long long". */
2188 error_at (location
, "%<long long long%> is too long for GCC");
2190 pedwarn_cxx98 (location
, OPT_Wlong_long
,
2191 "ISO C++ 1998 does not support %<long long%>");
2195 static const char *const decl_spec_names
[] = {
2212 error_at (location
, "duplicate %qs", decl_spec_names
[ds
]);
2217 /* This function is called when a type is defined. If type
2218 definitions are forbidden at this point, an error message is
2222 cp_parser_check_type_definition (cp_parser
* parser
)
2224 /* If types are forbidden here, issue a message. */
2225 if (parser
->type_definition_forbidden_message
)
2227 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2228 in the message need to be interpreted. */
2229 error (parser
->type_definition_forbidden_message
);
2235 /* This function is called when the DECLARATOR is processed. The TYPE
2236 was a type defined in the decl-specifiers. If it is invalid to
2237 define a type in the decl-specifiers for DECLARATOR, an error is
2238 issued. TYPE_LOCATION is the location of TYPE and is used
2239 for error reporting. */
2242 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2243 tree type
, location_t type_location
)
2245 /* [dcl.fct] forbids type definitions in return types.
2246 Unfortunately, it's not easy to know whether or not we are
2247 processing a return type until after the fact. */
2249 && (declarator
->kind
== cdk_pointer
2250 || declarator
->kind
== cdk_reference
2251 || declarator
->kind
== cdk_ptrmem
))
2252 declarator
= declarator
->declarator
;
2254 && declarator
->kind
== cdk_function
)
2256 error_at (type_location
,
2257 "new types may not be defined in a return type");
2258 inform (type_location
,
2259 "(perhaps a semicolon is missing after the definition of %qT)",
2264 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2265 "<" in any valid C++ program. If the next token is indeed "<",
2266 issue a message warning the user about what appears to be an
2267 invalid attempt to form a template-id. LOCATION is the location
2268 of the type-specifier (TYPE) */
2271 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2272 tree type
, location_t location
)
2274 cp_token_position start
= 0;
2276 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2279 error_at (location
, "%qT is not a template", type
);
2280 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2281 error_at (location
, "%qE is not a template", type
);
2283 error_at (location
, "invalid template-id");
2284 /* Remember the location of the invalid "<". */
2285 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2286 start
= cp_lexer_token_position (parser
->lexer
, true);
2287 /* Consume the "<". */
2288 cp_lexer_consume_token (parser
->lexer
);
2289 /* Parse the template arguments. */
2290 cp_parser_enclosed_template_argument_list (parser
);
2291 /* Permanently remove the invalid template arguments so that
2292 this error message is not issued again. */
2294 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2298 /* If parsing an integral constant-expression, issue an error message
2299 about the fact that THING appeared and return true. Otherwise,
2300 return false. In either case, set
2301 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2304 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2307 parser
->non_integral_constant_expression_p
= true;
2308 if (parser
->integral_constant_expression_p
)
2310 if (!parser
->allow_non_integral_constant_expression_p
)
2312 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2313 in the message need to be interpreted. */
2314 char *message
= concat (thing
,
2315 " cannot appear in a constant-expression",
2325 /* Emit a diagnostic for an invalid type name. SCOPE is the
2326 qualifying scope (or NULL, if none) for ID. This function commits
2327 to the current active tentative parse, if any. (Otherwise, the
2328 problematic construct might be encountered again later, resulting
2329 in duplicate error messages.) LOCATION is the location of ID. */
2332 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
,
2333 tree scope
, tree id
,
2334 location_t location
)
2336 tree decl
, old_scope
;
2337 /* Try to lookup the identifier. */
2338 old_scope
= parser
->scope
;
2339 parser
->scope
= scope
;
2340 decl
= cp_parser_lookup_name_simple (parser
, id
, location
);
2341 parser
->scope
= old_scope
;
2342 /* If the lookup found a template-name, it means that the user forgot
2343 to specify an argument list. Emit a useful error message. */
2344 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2346 "invalid use of template-name %qE without an argument list",
2348 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2349 error_at (location
, "invalid use of destructor %qD as a type", id
);
2350 else if (TREE_CODE (decl
) == TYPE_DECL
)
2351 /* Something like 'unsigned A a;' */
2352 error_at (location
, "invalid combination of multiple type-specifiers");
2353 else if (!parser
->scope
)
2355 /* Issue an error message. */
2356 error_at (location
, "%qE does not name a type", id
);
2357 /* If we're in a template class, it's possible that the user was
2358 referring to a type from a base class. For example:
2360 template <typename T> struct A { typedef T X; };
2361 template <typename T> struct B : public A<T> { X x; };
2363 The user should have said "typename A<T>::X". */
2364 if (processing_template_decl
&& current_class_type
2365 && TYPE_BINFO (current_class_type
))
2369 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2373 tree base_type
= BINFO_TYPE (b
);
2374 if (CLASS_TYPE_P (base_type
)
2375 && dependent_type_p (base_type
))
2378 /* Go from a particular instantiation of the
2379 template (which will have an empty TYPE_FIELDs),
2380 to the main version. */
2381 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2382 for (field
= TYPE_FIELDS (base_type
);
2384 field
= TREE_CHAIN (field
))
2385 if (TREE_CODE (field
) == TYPE_DECL
2386 && DECL_NAME (field
) == id
)
2389 "(perhaps %<typename %T::%E%> was intended)",
2390 BINFO_TYPE (b
), id
);
2399 /* Here we diagnose qualified-ids where the scope is actually correct,
2400 but the identifier does not resolve to a valid type name. */
2401 else if (parser
->scope
!= error_mark_node
)
2403 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2404 error_at (location
, "%qE in namespace %qE does not name a type",
2406 else if (CLASS_TYPE_P (parser
->scope
)
2407 && constructor_name_p (id
, parser
->scope
))
2410 error_at (location
, "%<%T::%E%> names the constructor, not"
2411 " the type", parser
->scope
, id
);
2412 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2413 error_at (location
, "and %qT has no template constructors",
2416 else if (TYPE_P (parser
->scope
)
2417 && dependent_scope_p (parser
->scope
))
2418 error_at (location
, "need %<typename%> before %<%T::%E%> because "
2419 "%qT is a dependent scope",
2420 parser
->scope
, id
, parser
->scope
);
2421 else if (TYPE_P (parser
->scope
))
2422 error_at (location
, "%qE in class %qT does not name a type",
2427 cp_parser_commit_to_tentative_parse (parser
);
2430 /* Check for a common situation where a type-name should be present,
2431 but is not, and issue a sensible error message. Returns true if an
2432 invalid type-name was detected.
2434 The situation handled by this function are variable declarations of the
2435 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2436 Usually, `ID' should name a type, but if we got here it means that it
2437 does not. We try to emit the best possible error message depending on
2438 how exactly the id-expression looks like. */
2441 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2444 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2446 /* Avoid duplicate error about ambiguous lookup. */
2447 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
2449 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
2450 if (next
->type
== CPP_NAME
&& next
->ambiguous_p
)
2454 cp_parser_parse_tentatively (parser
);
2455 id
= cp_parser_id_expression (parser
,
2456 /*template_keyword_p=*/false,
2457 /*check_dependency_p=*/true,
2458 /*template_p=*/NULL
,
2459 /*declarator_p=*/true,
2460 /*optional_p=*/false);
2461 /* If the next token is a (, this is a function with no explicit return
2462 type, i.e. constructor, destructor or conversion op. */
2463 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
2464 || TREE_CODE (id
) == TYPE_DECL
)
2466 cp_parser_abort_tentative_parse (parser
);
2469 if (!cp_parser_parse_definitely (parser
))
2472 /* Emit a diagnostic for the invalid type. */
2473 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
,
2474 id
, token
->location
);
2476 /* If we aren't in the middle of a declarator (i.e. in a
2477 parameter-declaration-clause), skip to the end of the declaration;
2478 there's no point in trying to process it. */
2479 if (!parser
->in_declarator_p
)
2480 cp_parser_skip_to_end_of_block_or_statement (parser
);
2484 /* Consume tokens up to, and including, the next non-nested closing `)'.
2485 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2486 are doing error recovery. Returns -1 if OR_COMMA is true and we
2487 found an unnested comma. */
2490 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2495 unsigned paren_depth
= 0;
2496 unsigned brace_depth
= 0;
2497 unsigned square_depth
= 0;
2499 if (recovering
&& !or_comma
2500 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2505 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2507 switch (token
->type
)
2510 case CPP_PRAGMA_EOL
:
2511 /* If we've run out of tokens, then there is no closing `)'. */
2514 /* This is good for lambda expression capture-lists. */
2515 case CPP_OPEN_SQUARE
:
2518 case CPP_CLOSE_SQUARE
:
2519 if (!square_depth
--)
2524 /* This matches the processing in skip_to_end_of_statement. */
2529 case CPP_OPEN_BRACE
:
2532 case CPP_CLOSE_BRACE
:
2538 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
2543 case CPP_OPEN_PAREN
:
2548 case CPP_CLOSE_PAREN
:
2549 if (!brace_depth
&& !paren_depth
--)
2552 cp_lexer_consume_token (parser
->lexer
);
2561 /* Consume the token. */
2562 cp_lexer_consume_token (parser
->lexer
);
2566 /* Consume tokens until we reach the end of the current statement.
2567 Normally, that will be just before consuming a `;'. However, if a
2568 non-nested `}' comes first, then we stop before consuming that. */
2571 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2573 unsigned nesting_depth
= 0;
2577 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2579 switch (token
->type
)
2582 case CPP_PRAGMA_EOL
:
2583 /* If we've run out of tokens, stop. */
2587 /* If the next token is a `;', we have reached the end of the
2593 case CPP_CLOSE_BRACE
:
2594 /* If this is a non-nested '}', stop before consuming it.
2595 That way, when confronted with something like:
2599 we stop before consuming the closing '}', even though we
2600 have not yet reached a `;'. */
2601 if (nesting_depth
== 0)
2604 /* If it is the closing '}' for a block that we have
2605 scanned, stop -- but only after consuming the token.
2611 we will stop after the body of the erroneously declared
2612 function, but before consuming the following `typedef'
2614 if (--nesting_depth
== 0)
2616 cp_lexer_consume_token (parser
->lexer
);
2620 case CPP_OPEN_BRACE
:
2628 /* Consume the token. */
2629 cp_lexer_consume_token (parser
->lexer
);
2633 /* This function is called at the end of a statement or declaration.
2634 If the next token is a semicolon, it is consumed; otherwise, error
2635 recovery is attempted. */
2638 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2640 /* Look for the trailing `;'. */
2641 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>"))
2643 /* If there is additional (erroneous) input, skip to the end of
2645 cp_parser_skip_to_end_of_statement (parser
);
2646 /* If the next token is now a `;', consume it. */
2647 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2648 cp_lexer_consume_token (parser
->lexer
);
2652 /* Skip tokens until we have consumed an entire block, or until we
2653 have consumed a non-nested `;'. */
2656 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2658 int nesting_depth
= 0;
2660 while (nesting_depth
>= 0)
2662 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2664 switch (token
->type
)
2667 case CPP_PRAGMA_EOL
:
2668 /* If we've run out of tokens, stop. */
2672 /* Stop if this is an unnested ';'. */
2677 case CPP_CLOSE_BRACE
:
2678 /* Stop if this is an unnested '}', or closes the outermost
2681 if (nesting_depth
< 0)
2687 case CPP_OPEN_BRACE
:
2696 /* Consume the token. */
2697 cp_lexer_consume_token (parser
->lexer
);
2701 /* Skip tokens until a non-nested closing curly brace is the next
2702 token, or there are no more tokens. Return true in the first case,
2706 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2708 unsigned nesting_depth
= 0;
2712 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2714 switch (token
->type
)
2717 case CPP_PRAGMA_EOL
:
2718 /* If we've run out of tokens, stop. */
2721 case CPP_CLOSE_BRACE
:
2722 /* If the next token is a non-nested `}', then we have reached
2723 the end of the current block. */
2724 if (nesting_depth
-- == 0)
2728 case CPP_OPEN_BRACE
:
2729 /* If it the next token is a `{', then we are entering a new
2730 block. Consume the entire block. */
2738 /* Consume the token. */
2739 cp_lexer_consume_token (parser
->lexer
);
2743 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2744 parameter is the PRAGMA token, allowing us to purge the entire pragma
2748 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
2752 parser
->lexer
->in_pragma
= false;
2755 token
= cp_lexer_consume_token (parser
->lexer
);
2756 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
2758 /* Ensure that the pragma is not parsed again. */
2759 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
2762 /* Require pragma end of line, resyncing with it as necessary. The
2763 arguments are as for cp_parser_skip_to_pragma_eol. */
2766 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
2768 parser
->lexer
->in_pragma
= false;
2769 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, "end of line"))
2770 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
2773 /* This is a simple wrapper around make_typename_type. When the id is
2774 an unresolved identifier node, we can provide a superior diagnostic
2775 using cp_parser_diagnose_invalid_type_name. */
2778 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
,
2779 tree id
, location_t id_location
)
2782 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2784 result
= make_typename_type (scope
, id
, typename_type
,
2785 /*complain=*/tf_none
);
2786 if (result
== error_mark_node
)
2787 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
, id_location
);
2790 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2793 /* This is a wrapper around the
2794 make_{pointer,ptrmem,reference}_declarator functions that decides
2795 which one to call based on the CODE and CLASS_TYPE arguments. The
2796 CODE argument should be one of the values returned by
2797 cp_parser_ptr_operator. */
2798 static cp_declarator
*
2799 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
2800 cp_cv_quals cv_qualifiers
,
2801 cp_declarator
*target
)
2803 if (code
== ERROR_MARK
)
2804 return cp_error_declarator
;
2806 if (code
== INDIRECT_REF
)
2807 if (class_type
== NULL_TREE
)
2808 return make_pointer_declarator (cv_qualifiers
, target
);
2810 return make_ptrmem_declarator (cv_qualifiers
, class_type
, target
);
2811 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
2812 return make_reference_declarator (cv_qualifiers
, target
, false);
2813 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
2814 return make_reference_declarator (cv_qualifiers
, target
, true);
2818 /* Create a new C++ parser. */
2821 cp_parser_new (void)
2827 /* cp_lexer_new_main is called before calling ggc_alloc because
2828 cp_lexer_new_main might load a PCH file. */
2829 lexer
= cp_lexer_new_main ();
2831 /* Initialize the binops_by_token so that we can get the tree
2832 directly from the token. */
2833 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2834 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2836 parser
= GGC_CNEW (cp_parser
);
2837 parser
->lexer
= lexer
;
2838 parser
->context
= cp_parser_context_new (NULL
);
2840 /* For now, we always accept GNU extensions. */
2841 parser
->allow_gnu_extensions_p
= 1;
2843 /* The `>' token is a greater-than operator, not the end of a
2845 parser
->greater_than_is_operator_p
= true;
2847 parser
->default_arg_ok_p
= true;
2849 /* We are not parsing a constant-expression. */
2850 parser
->integral_constant_expression_p
= false;
2851 parser
->allow_non_integral_constant_expression_p
= false;
2852 parser
->non_integral_constant_expression_p
= false;
2854 /* Local variable names are not forbidden. */
2855 parser
->local_variables_forbidden_p
= false;
2857 /* We are not processing an `extern "C"' declaration. */
2858 parser
->in_unbraced_linkage_specification_p
= false;
2860 /* We are not processing a declarator. */
2861 parser
->in_declarator_p
= false;
2863 /* We are not processing a template-argument-list. */
2864 parser
->in_template_argument_list_p
= false;
2866 /* We are not in an iteration statement. */
2867 parser
->in_statement
= 0;
2869 /* We are not in a switch statement. */
2870 parser
->in_switch_statement_p
= false;
2872 /* We are not parsing a type-id inside an expression. */
2873 parser
->in_type_id_in_expr_p
= false;
2875 /* Declarations aren't implicitly extern "C". */
2876 parser
->implicit_extern_c
= false;
2878 /* String literals should be translated to the execution character set. */
2879 parser
->translate_strings_p
= true;
2881 /* We are not parsing a function body. */
2882 parser
->in_function_body
= false;
2884 /* The unparsed function queue is empty. */
2885 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2887 /* There are no classes being defined. */
2888 parser
->num_classes_being_defined
= 0;
2890 /* No template parameters apply. */
2891 parser
->num_template_parameter_lists
= 0;
2896 /* Create a cp_lexer structure which will emit the tokens in CACHE
2897 and push it onto the parser's lexer stack. This is used for delayed
2898 parsing of in-class method bodies and default arguments, and should
2899 not be confused with tentative parsing. */
2901 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2903 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2904 lexer
->next
= parser
->lexer
;
2905 parser
->lexer
= lexer
;
2907 /* Move the current source position to that of the first token in the
2909 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2912 /* Pop the top lexer off the parser stack. This is never used for the
2913 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2915 cp_parser_pop_lexer (cp_parser
*parser
)
2917 cp_lexer
*lexer
= parser
->lexer
;
2918 parser
->lexer
= lexer
->next
;
2919 cp_lexer_destroy (lexer
);
2921 /* Put the current source position back where it was before this
2922 lexer was pushed. */
2923 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2926 /* Lexical conventions [gram.lex] */
2928 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2932 cp_parser_identifier (cp_parser
* parser
)
2936 /* Look for the identifier. */
2937 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2938 /* Return the value. */
2939 return token
? token
->u
.value
: error_mark_node
;
2942 /* Parse a sequence of adjacent string constants. Returns a
2943 TREE_STRING representing the combined, nul-terminated string
2944 constant. If TRANSLATE is true, translate the string to the
2945 execution character set. If WIDE_OK is true, a wide string is
2948 C++98 [lex.string] says that if a narrow string literal token is
2949 adjacent to a wide string literal token, the behavior is undefined.
2950 However, C99 6.4.5p4 says that this results in a wide string literal.
2951 We follow C99 here, for consistency with the C front end.
2953 This code is largely lifted from lex_string() in c-lex.c.
2955 FUTURE: ObjC++ will need to handle @-strings here. */
2957 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2961 struct obstack str_ob
;
2962 cpp_string str
, istr
, *strs
;
2964 enum cpp_ttype type
;
2966 tok
= cp_lexer_peek_token (parser
->lexer
);
2967 if (!cp_parser_is_string_literal (tok
))
2969 cp_parser_error (parser
, "expected string-literal");
2970 return error_mark_node
;
2975 /* Try to avoid the overhead of creating and destroying an obstack
2976 for the common case of just one string. */
2977 if (!cp_parser_is_string_literal
2978 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2980 cp_lexer_consume_token (parser
->lexer
);
2982 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
2983 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
2990 gcc_obstack_init (&str_ob
);
2995 cp_lexer_consume_token (parser
->lexer
);
2997 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
2998 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
3000 if (type
!= tok
->type
)
3002 if (type
== CPP_STRING
)
3004 else if (tok
->type
!= CPP_STRING
)
3005 error_at (tok
->location
,
3006 "unsupported non-standard concatenation "
3007 "of string literals");
3010 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3012 tok
= cp_lexer_peek_token (parser
->lexer
);
3014 while (cp_parser_is_string_literal (tok
));
3016 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3019 if (type
!= CPP_STRING
&& !wide_ok
)
3021 cp_parser_error (parser
, "a wide string is invalid in this context");
3025 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3026 (parse_in
, strs
, count
, &istr
, type
))
3028 value
= build_string (istr
.len
, (const char *)istr
.text
);
3029 free (CONST_CAST (unsigned char *, istr
.text
));
3035 case CPP_UTF8STRING
:
3036 TREE_TYPE (value
) = char_array_type_node
;
3039 TREE_TYPE (value
) = char16_array_type_node
;
3042 TREE_TYPE (value
) = char32_array_type_node
;
3045 TREE_TYPE (value
) = wchar_array_type_node
;
3049 value
= fix_string_type (value
);
3052 /* cpp_interpret_string has issued an error. */
3053 value
= error_mark_node
;
3056 obstack_free (&str_ob
, 0);
3062 /* Basic concepts [gram.basic] */
3064 /* Parse a translation-unit.
3067 declaration-seq [opt]
3069 Returns TRUE if all went well. */
3072 cp_parser_translation_unit (cp_parser
* parser
)
3074 /* The address of the first non-permanent object on the declarator
3076 static void *declarator_obstack_base
;
3080 /* Create the declarator obstack, if necessary. */
3081 if (!cp_error_declarator
)
3083 gcc_obstack_init (&declarator_obstack
);
3084 /* Create the error declarator. */
3085 cp_error_declarator
= make_declarator (cdk_error
);
3086 /* Create the empty parameter list. */
3087 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
3088 /* Remember where the base of the declarator obstack lies. */
3089 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
3092 cp_parser_declaration_seq_opt (parser
);
3094 /* If there are no tokens left then all went well. */
3095 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
3097 /* Get rid of the token array; we don't need it any more. */
3098 cp_lexer_destroy (parser
->lexer
);
3099 parser
->lexer
= NULL
;
3101 /* This file might have been a context that's implicitly extern
3102 "C". If so, pop the lang context. (Only relevant for PCH.) */
3103 if (parser
->implicit_extern_c
)
3105 pop_lang_context ();
3106 parser
->implicit_extern_c
= false;
3110 finish_translation_unit ();
3116 cp_parser_error (parser
, "expected declaration");
3120 /* Make sure the declarator obstack was fully cleaned up. */
3121 gcc_assert (obstack_next_free (&declarator_obstack
)
3122 == declarator_obstack_base
);
3124 /* All went well. */
3128 /* Expressions [gram.expr] */
3130 /* Parse a primary-expression.
3141 ( compound-statement )
3142 __builtin_va_arg ( assignment-expression , type-id )
3143 __builtin_offsetof ( type-id , offsetof-expression )
3146 __has_nothrow_assign ( type-id )
3147 __has_nothrow_constructor ( type-id )
3148 __has_nothrow_copy ( type-id )
3149 __has_trivial_assign ( type-id )
3150 __has_trivial_constructor ( type-id )
3151 __has_trivial_copy ( type-id )
3152 __has_trivial_destructor ( type-id )
3153 __has_virtual_destructor ( type-id )
3154 __is_abstract ( type-id )
3155 __is_base_of ( type-id , type-id )
3156 __is_class ( type-id )
3157 __is_convertible_to ( type-id , type-id )
3158 __is_empty ( type-id )
3159 __is_enum ( type-id )
3160 __is_pod ( type-id )
3161 __is_polymorphic ( type-id )
3162 __is_union ( type-id )
3164 Objective-C++ Extension:
3172 ADDRESS_P is true iff this expression was immediately preceded by
3173 "&" and therefore might denote a pointer-to-member. CAST_P is true
3174 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3175 true iff this expression is a template argument.
3177 Returns a representation of the expression. Upon return, *IDK
3178 indicates what kind of id-expression (if any) was present. */
3181 cp_parser_primary_expression (cp_parser
*parser
,
3184 bool template_arg_p
,
3187 cp_token
*token
= NULL
;
3189 /* Assume the primary expression is not an id-expression. */
3190 *idk
= CP_ID_KIND_NONE
;
3192 /* Peek at the next token. */
3193 token
= cp_lexer_peek_token (parser
->lexer
);
3194 switch (token
->type
)
3207 token
= cp_lexer_consume_token (parser
->lexer
);
3208 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
3210 error_at (token
->location
,
3211 "fixed-point types not supported in C++");
3212 return error_mark_node
;
3214 /* Floating-point literals are only allowed in an integral
3215 constant expression if they are cast to an integral or
3216 enumeration type. */
3217 if (TREE_CODE (token
->u
.value
) == REAL_CST
3218 && parser
->integral_constant_expression_p
3221 /* CAST_P will be set even in invalid code like "int(2.7 +
3222 ...)". Therefore, we have to check that the next token
3223 is sure to end the cast. */
3226 cp_token
*next_token
;
3228 next_token
= cp_lexer_peek_token (parser
->lexer
);
3229 if (/* The comma at the end of an
3230 enumerator-definition. */
3231 next_token
->type
!= CPP_COMMA
3232 /* The curly brace at the end of an enum-specifier. */
3233 && next_token
->type
!= CPP_CLOSE_BRACE
3234 /* The end of a statement. */
3235 && next_token
->type
!= CPP_SEMICOLON
3236 /* The end of the cast-expression. */
3237 && next_token
->type
!= CPP_CLOSE_PAREN
3238 /* The end of an array bound. */
3239 && next_token
->type
!= CPP_CLOSE_SQUARE
3240 /* The closing ">" in a template-argument-list. */
3241 && (next_token
->type
!= CPP_GREATER
3242 || parser
->greater_than_is_operator_p
)
3243 /* C++0x only: A ">>" treated like two ">" tokens,
3244 in a template-argument-list. */
3245 && (next_token
->type
!= CPP_RSHIFT
3246 || (cxx_dialect
== cxx98
)
3247 || parser
->greater_than_is_operator_p
))
3251 /* If we are within a cast, then the constraint that the
3252 cast is to an integral or enumeration type will be
3253 checked at that point. If we are not within a cast, then
3254 this code is invalid. */
3256 cp_parser_non_integral_constant_expression
3257 (parser
, "floating-point literal");
3259 return token
->u
.value
;
3265 case CPP_UTF8STRING
:
3266 /* ??? Should wide strings be allowed when parser->translate_strings_p
3267 is false (i.e. in attributes)? If not, we can kill the third
3268 argument to cp_parser_string_literal. */
3269 return cp_parser_string_literal (parser
,
3270 parser
->translate_strings_p
,
3273 case CPP_OPEN_PAREN
:
3276 bool saved_greater_than_is_operator_p
;
3278 /* Consume the `('. */
3279 cp_lexer_consume_token (parser
->lexer
);
3280 /* Within a parenthesized expression, a `>' token is always
3281 the greater-than operator. */
3282 saved_greater_than_is_operator_p
3283 = parser
->greater_than_is_operator_p
;
3284 parser
->greater_than_is_operator_p
= true;
3285 /* If we see `( { ' then we are looking at the beginning of
3286 a GNU statement-expression. */
3287 if (cp_parser_allow_gnu_extensions_p (parser
)
3288 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
3290 /* Statement-expressions are not allowed by the standard. */
3291 pedwarn (token
->location
, OPT_pedantic
,
3292 "ISO C++ forbids braced-groups within expressions");
3294 /* And they're not allowed outside of a function-body; you
3295 cannot, for example, write:
3297 int i = ({ int j = 3; j + 1; });
3299 at class or namespace scope. */
3300 if (!parser
->in_function_body
3301 || parser
->in_template_argument_list_p
)
3303 error_at (token
->location
,
3304 "statement-expressions are not allowed outside "
3305 "functions nor in template-argument lists");
3306 cp_parser_skip_to_end_of_block_or_statement (parser
);
3307 expr
= error_mark_node
;
3311 /* Start the statement-expression. */
3312 expr
= begin_stmt_expr ();
3313 /* Parse the compound-statement. */
3314 cp_parser_compound_statement (parser
, expr
, false);
3316 expr
= finish_stmt_expr (expr
, false);
3321 /* Parse the parenthesized expression. */
3322 expr
= cp_parser_expression (parser
, cast_p
, idk
);
3323 /* Let the front end know that this expression was
3324 enclosed in parentheses. This matters in case, for
3325 example, the expression is of the form `A::B', since
3326 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3328 finish_parenthesized_expr (expr
);
3330 /* The `>' token might be the end of a template-id or
3331 template-parameter-list now. */
3332 parser
->greater_than_is_operator_p
3333 = saved_greater_than_is_operator_p
;
3334 /* Consume the `)'. */
3335 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
3336 cp_parser_skip_to_end_of_statement (parser
);
3341 case CPP_OPEN_SQUARE
:
3342 if (c_dialect_objc ())
3343 /* We have an Objective-C++ message. */
3344 return cp_parser_objc_expression (parser
);
3345 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
3346 return cp_parser_lambda_expression (parser
);
3348 case CPP_OBJC_STRING
:
3349 if (c_dialect_objc ())
3350 /* We have an Objective-C++ string literal. */
3351 return cp_parser_objc_expression (parser
);
3352 cp_parser_error (parser
, "expected primary-expression");
3353 return error_mark_node
;
3356 switch (token
->keyword
)
3358 /* These two are the boolean literals. */
3360 cp_lexer_consume_token (parser
->lexer
);
3361 return boolean_true_node
;
3363 cp_lexer_consume_token (parser
->lexer
);
3364 return boolean_false_node
;
3366 /* The `__null' literal. */
3368 cp_lexer_consume_token (parser
->lexer
);
3371 /* The `nullptr' literal. */
3373 cp_lexer_consume_token (parser
->lexer
);
3374 return nullptr_node
;
3376 /* Recognize the `this' keyword. */
3378 cp_lexer_consume_token (parser
->lexer
);
3379 if (parser
->local_variables_forbidden_p
)
3381 error_at (token
->location
,
3382 "%<this%> may not be used in this context");
3383 return error_mark_node
;
3385 /* Pointers cannot appear in constant-expressions. */
3386 if (cp_parser_non_integral_constant_expression (parser
, "%<this%>"))
3387 return error_mark_node
;
3388 return finish_this_expr ();
3390 /* The `operator' keyword can be the beginning of an
3395 case RID_FUNCTION_NAME
:
3396 case RID_PRETTY_FUNCTION_NAME
:
3397 case RID_C99_FUNCTION_NAME
:
3401 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3402 __func__ are the names of variables -- but they are
3403 treated specially. Therefore, they are handled here,
3404 rather than relying on the generic id-expression logic
3405 below. Grammatically, these names are id-expressions.
3407 Consume the token. */
3408 token
= cp_lexer_consume_token (parser
->lexer
);
3410 switch (token
->keyword
)
3412 case RID_FUNCTION_NAME
:
3413 name
= "%<__FUNCTION__%>";
3415 case RID_PRETTY_FUNCTION_NAME
:
3416 name
= "%<__PRETTY_FUNCTION__%>";
3418 case RID_C99_FUNCTION_NAME
:
3419 name
= "%<__func__%>";
3425 if (cp_parser_non_integral_constant_expression (parser
, name
))
3426 return error_mark_node
;
3428 /* Look up the name. */
3429 return finish_fname (token
->u
.value
);
3437 /* The `__builtin_va_arg' construct is used to handle
3438 `va_arg'. Consume the `__builtin_va_arg' token. */
3439 cp_lexer_consume_token (parser
->lexer
);
3440 /* Look for the opening `('. */
3441 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
3442 /* Now, parse the assignment-expression. */
3443 expression
= cp_parser_assignment_expression (parser
,
3444 /*cast_p=*/false, NULL
);
3445 /* Look for the `,'. */
3446 cp_parser_require (parser
, CPP_COMMA
, "%<,%>");
3447 /* Parse the type-id. */
3448 type
= cp_parser_type_id (parser
);
3449 /* Look for the closing `)'. */
3450 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
3451 /* Using `va_arg' in a constant-expression is not
3453 if (cp_parser_non_integral_constant_expression (parser
,
3455 return error_mark_node
;
3456 return build_x_va_arg (expression
, type
);
3460 return cp_parser_builtin_offsetof (parser
);
3462 case RID_HAS_NOTHROW_ASSIGN
:
3463 case RID_HAS_NOTHROW_CONSTRUCTOR
:
3464 case RID_HAS_NOTHROW_COPY
:
3465 case RID_HAS_TRIVIAL_ASSIGN
:
3466 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
3467 case RID_HAS_TRIVIAL_COPY
:
3468 case RID_HAS_TRIVIAL_DESTRUCTOR
:
3469 case RID_HAS_VIRTUAL_DESTRUCTOR
:
3470 case RID_IS_ABSTRACT
:
3471 case RID_IS_BASE_OF
:
3473 case RID_IS_CONVERTIBLE_TO
:
3477 case RID_IS_POLYMORPHIC
:
3478 case RID_IS_STD_LAYOUT
:
3479 case RID_IS_TRIVIAL
:
3481 return cp_parser_trait_expr (parser
, token
->keyword
);
3483 /* Objective-C++ expressions. */
3485 case RID_AT_PROTOCOL
:
3486 case RID_AT_SELECTOR
:
3487 return cp_parser_objc_expression (parser
);
3490 cp_parser_error (parser
, "expected primary-expression");
3491 return error_mark_node
;
3494 /* An id-expression can start with either an identifier, a
3495 `::' as the beginning of a qualified-id, or the "operator"
3499 case CPP_TEMPLATE_ID
:
3500 case CPP_NESTED_NAME_SPECIFIER
:
3504 const char *error_msg
;
3507 cp_token
*id_expr_token
;
3510 /* Parse the id-expression. */
3512 = cp_parser_id_expression (parser
,
3513 /*template_keyword_p=*/false,
3514 /*check_dependency_p=*/true,
3516 /*declarator_p=*/false,
3517 /*optional_p=*/false);
3518 if (id_expression
== error_mark_node
)
3519 return error_mark_node
;
3520 id_expr_token
= token
;
3521 token
= cp_lexer_peek_token (parser
->lexer
);
3522 done
= (token
->type
!= CPP_OPEN_SQUARE
3523 && token
->type
!= CPP_OPEN_PAREN
3524 && token
->type
!= CPP_DOT
3525 && token
->type
!= CPP_DEREF
3526 && token
->type
!= CPP_PLUS_PLUS
3527 && token
->type
!= CPP_MINUS_MINUS
);
3528 /* If we have a template-id, then no further lookup is
3529 required. If the template-id was for a template-class, we
3530 will sometimes have a TYPE_DECL at this point. */
3531 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
3532 || TREE_CODE (id_expression
) == TYPE_DECL
)
3533 decl
= id_expression
;
3534 /* Look up the name. */
3537 tree ambiguous_decls
;
3539 /* If we already know that this lookup is ambiguous, then
3540 we've already issued an error message; there's no reason
3542 if (id_expr_token
->type
== CPP_NAME
3543 && id_expr_token
->ambiguous_p
)
3545 cp_parser_simulate_error (parser
);
3546 return error_mark_node
;
3549 decl
= cp_parser_lookup_name (parser
, id_expression
,
3552 /*is_namespace=*/false,
3553 /*check_dependency=*/true,
3555 id_expr_token
->location
);
3556 /* If the lookup was ambiguous, an error will already have
3558 if (ambiguous_decls
)
3559 return error_mark_node
;
3561 /* In Objective-C++, an instance variable (ivar) may be preferred
3562 to whatever cp_parser_lookup_name() found. */
3563 decl
= objc_lookup_ivar (decl
, id_expression
);
3565 /* If name lookup gives us a SCOPE_REF, then the
3566 qualifying scope was dependent. */
3567 if (TREE_CODE (decl
) == SCOPE_REF
)
3569 /* At this point, we do not know if DECL is a valid
3570 integral constant expression. We assume that it is
3571 in fact such an expression, so that code like:
3573 template <int N> struct A {
3577 is accepted. At template-instantiation time, we
3578 will check that B<N>::i is actually a constant. */
3581 /* Check to see if DECL is a local variable in a context
3582 where that is forbidden. */
3583 if (parser
->local_variables_forbidden_p
3584 && local_variable_p (decl
))
3586 /* It might be that we only found DECL because we are
3587 trying to be generous with pre-ISO scoping rules.
3588 For example, consider:
3592 for (int i = 0; i < 10; ++i) {}
3593 extern void f(int j = i);
3596 Here, name look up will originally find the out
3597 of scope `i'. We need to issue a warning message,
3598 but then use the global `i'. */
3599 decl
= check_for_out_of_scope_variable (decl
);
3600 if (local_variable_p (decl
))
3602 error_at (id_expr_token
->location
,
3603 "local variable %qD may not appear in this context",
3605 return error_mark_node
;
3610 decl
= (finish_id_expression
3611 (id_expression
, decl
, parser
->scope
,
3613 parser
->integral_constant_expression_p
,
3614 parser
->allow_non_integral_constant_expression_p
,
3615 &parser
->non_integral_constant_expression_p
,
3616 template_p
, done
, address_p
,
3619 id_expr_token
->location
));
3621 cp_parser_error (parser
, error_msg
);
3625 /* Anything else is an error. */
3627 cp_parser_error (parser
, "expected primary-expression");
3628 return error_mark_node
;
3632 /* Parse an id-expression.
3639 :: [opt] nested-name-specifier template [opt] unqualified-id
3641 :: operator-function-id
3644 Return a representation of the unqualified portion of the
3645 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3646 a `::' or nested-name-specifier.
3648 Often, if the id-expression was a qualified-id, the caller will
3649 want to make a SCOPE_REF to represent the qualified-id. This
3650 function does not do this in order to avoid wastefully creating
3651 SCOPE_REFs when they are not required.
3653 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3656 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3657 uninstantiated templates.
3659 If *TEMPLATE_P is non-NULL, it is set to true iff the
3660 `template' keyword is used to explicitly indicate that the entity
3661 named is a template.
3663 If DECLARATOR_P is true, the id-expression is appearing as part of
3664 a declarator, rather than as part of an expression. */
3667 cp_parser_id_expression (cp_parser
*parser
,
3668 bool template_keyword_p
,
3669 bool check_dependency_p
,
3674 bool global_scope_p
;
3675 bool nested_name_specifier_p
;
3677 /* Assume the `template' keyword was not used. */
3679 *template_p
= template_keyword_p
;
3681 /* Look for the optional `::' operator. */
3683 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3685 /* Look for the optional nested-name-specifier. */
3686 nested_name_specifier_p
3687 = (cp_parser_nested_name_specifier_opt (parser
,
3688 /*typename_keyword_p=*/false,
3693 /* If there is a nested-name-specifier, then we are looking at
3694 the first qualified-id production. */
3695 if (nested_name_specifier_p
)
3698 tree saved_object_scope
;
3699 tree saved_qualifying_scope
;
3700 tree unqualified_id
;
3703 /* See if the next token is the `template' keyword. */
3705 template_p
= &is_template
;
3706 *template_p
= cp_parser_optional_template_keyword (parser
);
3707 /* Name lookup we do during the processing of the
3708 unqualified-id might obliterate SCOPE. */
3709 saved_scope
= parser
->scope
;
3710 saved_object_scope
= parser
->object_scope
;
3711 saved_qualifying_scope
= parser
->qualifying_scope
;
3712 /* Process the final unqualified-id. */
3713 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3716 /*optional_p=*/false);
3717 /* Restore the SAVED_SCOPE for our caller. */
3718 parser
->scope
= saved_scope
;
3719 parser
->object_scope
= saved_object_scope
;
3720 parser
->qualifying_scope
= saved_qualifying_scope
;
3722 return unqualified_id
;
3724 /* Otherwise, if we are in global scope, then we are looking at one
3725 of the other qualified-id productions. */
3726 else if (global_scope_p
)
3731 /* Peek at the next token. */
3732 token
= cp_lexer_peek_token (parser
->lexer
);
3734 /* If it's an identifier, and the next token is not a "<", then
3735 we can avoid the template-id case. This is an optimization
3736 for this common case. */
3737 if (token
->type
== CPP_NAME
3738 && !cp_parser_nth_token_starts_template_argument_list_p
3740 return cp_parser_identifier (parser
);
3742 cp_parser_parse_tentatively (parser
);
3743 /* Try a template-id. */
3744 id
= cp_parser_template_id (parser
,
3745 /*template_keyword_p=*/false,
3746 /*check_dependency_p=*/true,
3748 /* If that worked, we're done. */
3749 if (cp_parser_parse_definitely (parser
))
3752 /* Peek at the next token. (Changes in the token buffer may
3753 have invalidated the pointer obtained above.) */
3754 token
= cp_lexer_peek_token (parser
->lexer
);
3756 switch (token
->type
)
3759 return cp_parser_identifier (parser
);
3762 if (token
->keyword
== RID_OPERATOR
)
3763 return cp_parser_operator_function_id (parser
);
3767 cp_parser_error (parser
, "expected id-expression");
3768 return error_mark_node
;
3772 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3773 /*check_dependency_p=*/true,
3778 /* Parse an unqualified-id.
3782 operator-function-id
3783 conversion-function-id
3787 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3788 keyword, in a construct like `A::template ...'.
3790 Returns a representation of unqualified-id. For the `identifier'
3791 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3792 production a BIT_NOT_EXPR is returned; the operand of the
3793 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3794 other productions, see the documentation accompanying the
3795 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3796 names are looked up in uninstantiated templates. If DECLARATOR_P
3797 is true, the unqualified-id is appearing as part of a declarator,
3798 rather than as part of an expression. */
3801 cp_parser_unqualified_id (cp_parser
* parser
,
3802 bool template_keyword_p
,
3803 bool check_dependency_p
,
3809 /* Peek at the next token. */
3810 token
= cp_lexer_peek_token (parser
->lexer
);
3812 switch (token
->type
)
3818 /* We don't know yet whether or not this will be a
3820 cp_parser_parse_tentatively (parser
);
3821 /* Try a template-id. */
3822 id
= cp_parser_template_id (parser
, template_keyword_p
,
3825 /* If it worked, we're done. */
3826 if (cp_parser_parse_definitely (parser
))
3828 /* Otherwise, it's an ordinary identifier. */
3829 return cp_parser_identifier (parser
);
3832 case CPP_TEMPLATE_ID
:
3833 return cp_parser_template_id (parser
, template_keyword_p
,
3840 tree qualifying_scope
;
3845 /* Consume the `~' token. */
3846 cp_lexer_consume_token (parser
->lexer
);
3847 /* Parse the class-name. The standard, as written, seems to
3850 template <typename T> struct S { ~S (); };
3851 template <typename T> S<T>::~S() {}
3853 is invalid, since `~' must be followed by a class-name, but
3854 `S<T>' is dependent, and so not known to be a class.
3855 That's not right; we need to look in uninstantiated
3856 templates. A further complication arises from:
3858 template <typename T> void f(T t) {
3862 Here, it is not possible to look up `T' in the scope of `T'
3863 itself. We must look in both the current scope, and the
3864 scope of the containing complete expression.
3866 Yet another issue is:
3875 The standard does not seem to say that the `S' in `~S'
3876 should refer to the type `S' and not the data member
3879 /* DR 244 says that we look up the name after the "~" in the
3880 same scope as we looked up the qualifying name. That idea
3881 isn't fully worked out; it's more complicated than that. */
3882 scope
= parser
->scope
;
3883 object_scope
= parser
->object_scope
;
3884 qualifying_scope
= parser
->qualifying_scope
;
3886 /* Check for invalid scopes. */
3887 if (scope
== error_mark_node
)
3889 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3890 cp_lexer_consume_token (parser
->lexer
);
3891 return error_mark_node
;
3893 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
3895 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3896 error_at (token
->location
,
3897 "scope %qT before %<~%> is not a class-name",
3899 cp_parser_simulate_error (parser
);
3900 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3901 cp_lexer_consume_token (parser
->lexer
);
3902 return error_mark_node
;
3904 gcc_assert (!scope
|| TYPE_P (scope
));
3906 /* If the name is of the form "X::~X" it's OK even if X is a
3908 token
= cp_lexer_peek_token (parser
->lexer
);
3910 && token
->type
== CPP_NAME
3911 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3913 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
3914 || constructor_name_p (token
->u
.value
, scope
)))
3916 cp_lexer_consume_token (parser
->lexer
);
3917 return build_nt (BIT_NOT_EXPR
, scope
);
3920 /* If there was an explicit qualification (S::~T), first look
3921 in the scope given by the qualification (i.e., S).
3923 Note: in the calls to cp_parser_class_name below we pass
3924 typename_type so that lookup finds the injected-class-name
3925 rather than the constructor. */
3927 type_decl
= NULL_TREE
;
3930 cp_parser_parse_tentatively (parser
);
3931 type_decl
= cp_parser_class_name (parser
,
3932 /*typename_keyword_p=*/false,
3933 /*template_keyword_p=*/false,
3935 /*check_dependency=*/false,
3936 /*class_head_p=*/false,
3938 if (cp_parser_parse_definitely (parser
))
3941 /* In "N::S::~S", look in "N" as well. */
3942 if (!done
&& scope
&& qualifying_scope
)
3944 cp_parser_parse_tentatively (parser
);
3945 parser
->scope
= qualifying_scope
;
3946 parser
->object_scope
= NULL_TREE
;
3947 parser
->qualifying_scope
= NULL_TREE
;
3949 = cp_parser_class_name (parser
,
3950 /*typename_keyword_p=*/false,
3951 /*template_keyword_p=*/false,
3953 /*check_dependency=*/false,
3954 /*class_head_p=*/false,
3956 if (cp_parser_parse_definitely (parser
))
3959 /* In "p->S::~T", look in the scope given by "*p" as well. */
3960 else if (!done
&& object_scope
)
3962 cp_parser_parse_tentatively (parser
);
3963 parser
->scope
= object_scope
;
3964 parser
->object_scope
= NULL_TREE
;
3965 parser
->qualifying_scope
= NULL_TREE
;
3967 = cp_parser_class_name (parser
,
3968 /*typename_keyword_p=*/false,
3969 /*template_keyword_p=*/false,
3971 /*check_dependency=*/false,
3972 /*class_head_p=*/false,
3974 if (cp_parser_parse_definitely (parser
))
3977 /* Look in the surrounding context. */
3980 parser
->scope
= NULL_TREE
;
3981 parser
->object_scope
= NULL_TREE
;
3982 parser
->qualifying_scope
= NULL_TREE
;
3983 if (processing_template_decl
)
3984 cp_parser_parse_tentatively (parser
);
3986 = cp_parser_class_name (parser
,
3987 /*typename_keyword_p=*/false,
3988 /*template_keyword_p=*/false,
3990 /*check_dependency=*/false,
3991 /*class_head_p=*/false,
3993 if (processing_template_decl
3994 && ! cp_parser_parse_definitely (parser
))
3996 /* We couldn't find a type with this name, so just accept
3997 it and check for a match at instantiation time. */
3998 type_decl
= cp_parser_identifier (parser
);
3999 if (type_decl
!= error_mark_node
)
4000 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
4004 /* If an error occurred, assume that the name of the
4005 destructor is the same as the name of the qualifying
4006 class. That allows us to keep parsing after running
4007 into ill-formed destructor names. */
4008 if (type_decl
== error_mark_node
&& scope
)
4009 return build_nt (BIT_NOT_EXPR
, scope
);
4010 else if (type_decl
== error_mark_node
)
4011 return error_mark_node
;
4013 /* Check that destructor name and scope match. */
4014 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
4016 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4017 error_at (token
->location
,
4018 "declaration of %<~%T%> as member of %qT",
4020 cp_parser_simulate_error (parser
);
4021 return error_mark_node
;
4026 A typedef-name that names a class shall not be used as the
4027 identifier in the declarator for a destructor declaration. */
4029 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
4030 && !DECL_SELF_REFERENCE_P (type_decl
)
4031 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
4032 error_at (token
->location
,
4033 "typedef-name %qD used as destructor declarator",
4036 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
4040 if (token
->keyword
== RID_OPERATOR
)
4044 /* This could be a template-id, so we try that first. */
4045 cp_parser_parse_tentatively (parser
);
4046 /* Try a template-id. */
4047 id
= cp_parser_template_id (parser
, template_keyword_p
,
4048 /*check_dependency_p=*/true,
4050 /* If that worked, we're done. */
4051 if (cp_parser_parse_definitely (parser
))
4053 /* We still don't know whether we're looking at an
4054 operator-function-id or a conversion-function-id. */
4055 cp_parser_parse_tentatively (parser
);
4056 /* Try an operator-function-id. */
4057 id
= cp_parser_operator_function_id (parser
);
4058 /* If that didn't work, try a conversion-function-id. */
4059 if (!cp_parser_parse_definitely (parser
))
4060 id
= cp_parser_conversion_function_id (parser
);
4069 cp_parser_error (parser
, "expected unqualified-id");
4070 return error_mark_node
;
4074 /* Parse an (optional) nested-name-specifier.
4076 nested-name-specifier: [C++98]
4077 class-or-namespace-name :: nested-name-specifier [opt]
4078 class-or-namespace-name :: template nested-name-specifier [opt]
4080 nested-name-specifier: [C++0x]
4083 nested-name-specifier identifier ::
4084 nested-name-specifier template [opt] simple-template-id ::
4086 PARSER->SCOPE should be set appropriately before this function is
4087 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4088 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4091 Sets PARSER->SCOPE to the class (TYPE) or namespace
4092 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4093 it unchanged if there is no nested-name-specifier. Returns the new
4094 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4096 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4097 part of a declaration and/or decl-specifier. */
4100 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
4101 bool typename_keyword_p
,
4102 bool check_dependency_p
,
4104 bool is_declaration
)
4106 bool success
= false;
4107 cp_token_position start
= 0;
4110 /* Remember where the nested-name-specifier starts. */
4111 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4113 start
= cp_lexer_token_position (parser
->lexer
, false);
4114 push_deferring_access_checks (dk_deferred
);
4121 tree saved_qualifying_scope
;
4122 bool template_keyword_p
;
4124 /* Spot cases that cannot be the beginning of a
4125 nested-name-specifier. */
4126 token
= cp_lexer_peek_token (parser
->lexer
);
4128 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4129 the already parsed nested-name-specifier. */
4130 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
4132 /* Grab the nested-name-specifier and continue the loop. */
4133 cp_parser_pre_parsed_nested_name_specifier (parser
);
4134 /* If we originally encountered this nested-name-specifier
4135 with IS_DECLARATION set to false, we will not have
4136 resolved TYPENAME_TYPEs, so we must do so here. */
4138 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
4140 new_scope
= resolve_typename_type (parser
->scope
,
4141 /*only_current_p=*/false);
4142 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
4143 parser
->scope
= new_scope
;
4149 /* Spot cases that cannot be the beginning of a
4150 nested-name-specifier. On the second and subsequent times
4151 through the loop, we look for the `template' keyword. */
4152 if (success
&& token
->keyword
== RID_TEMPLATE
)
4154 /* A template-id can start a nested-name-specifier. */
4155 else if (token
->type
== CPP_TEMPLATE_ID
)
4159 /* If the next token is not an identifier, then it is
4160 definitely not a type-name or namespace-name. */
4161 if (token
->type
!= CPP_NAME
)
4163 /* If the following token is neither a `<' (to begin a
4164 template-id), nor a `::', then we are not looking at a
4165 nested-name-specifier. */
4166 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
4167 if (token
->type
!= CPP_SCOPE
4168 && !cp_parser_nth_token_starts_template_argument_list_p
4173 /* The nested-name-specifier is optional, so we parse
4175 cp_parser_parse_tentatively (parser
);
4177 /* Look for the optional `template' keyword, if this isn't the
4178 first time through the loop. */
4180 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
4182 template_keyword_p
= false;
4184 /* Save the old scope since the name lookup we are about to do
4185 might destroy it. */
4186 old_scope
= parser
->scope
;
4187 saved_qualifying_scope
= parser
->qualifying_scope
;
4188 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4189 look up names in "X<T>::I" in order to determine that "Y" is
4190 a template. So, if we have a typename at this point, we make
4191 an effort to look through it. */
4193 && !typename_keyword_p
4195 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
4196 parser
->scope
= resolve_typename_type (parser
->scope
,
4197 /*only_current_p=*/false);
4198 /* Parse the qualifying entity. */
4200 = cp_parser_qualifying_entity (parser
,
4206 /* Look for the `::' token. */
4207 cp_parser_require (parser
, CPP_SCOPE
, "%<::%>");
4209 /* If we found what we wanted, we keep going; otherwise, we're
4211 if (!cp_parser_parse_definitely (parser
))
4213 bool error_p
= false;
4215 /* Restore the OLD_SCOPE since it was valid before the
4216 failed attempt at finding the last
4217 class-or-namespace-name. */
4218 parser
->scope
= old_scope
;
4219 parser
->qualifying_scope
= saved_qualifying_scope
;
4220 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4222 /* If the next token is an identifier, and the one after
4223 that is a `::', then any valid interpretation would have
4224 found a class-or-namespace-name. */
4225 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
4226 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4228 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
4231 token
= cp_lexer_consume_token (parser
->lexer
);
4234 if (!token
->ambiguous_p
)
4237 tree ambiguous_decls
;
4239 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
4241 /*is_template=*/false,
4242 /*is_namespace=*/false,
4243 /*check_dependency=*/true,
4246 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4247 error_at (token
->location
,
4248 "%qD used without template parameters",
4250 else if (ambiguous_decls
)
4252 error_at (token
->location
,
4253 "reference to %qD is ambiguous",
4255 print_candidates (ambiguous_decls
);
4256 decl
= error_mark_node
;
4260 const char* msg
= "is not a class or namespace";
4261 if (cxx_dialect
!= cxx98
)
4262 msg
= "is not a class, namespace, or enumeration";
4263 cp_parser_name_lookup_error
4264 (parser
, token
->u
.value
, decl
, msg
,
4268 parser
->scope
= error_mark_node
;
4270 /* Treat this as a successful nested-name-specifier
4275 If the name found is not a class-name (clause
4276 _class_) or namespace-name (_namespace.def_), the
4277 program is ill-formed. */
4280 cp_lexer_consume_token (parser
->lexer
);
4284 /* We've found one valid nested-name-specifier. */
4286 /* Name lookup always gives us a DECL. */
4287 if (TREE_CODE (new_scope
) == TYPE_DECL
)
4288 new_scope
= TREE_TYPE (new_scope
);
4289 /* Uses of "template" must be followed by actual templates. */
4290 if (template_keyword_p
4291 && !(CLASS_TYPE_P (new_scope
)
4292 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
4293 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
4294 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
4295 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
4296 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
4297 == TEMPLATE_ID_EXPR
)))
4298 permerror (input_location
, TYPE_P (new_scope
)
4299 ? "%qT is not a template"
4300 : "%qD is not a template",
4302 /* If it is a class scope, try to complete it; we are about to
4303 be looking up names inside the class. */
4304 if (TYPE_P (new_scope
)
4305 /* Since checking types for dependency can be expensive,
4306 avoid doing it if the type is already complete. */
4307 && !COMPLETE_TYPE_P (new_scope
)
4308 /* Do not try to complete dependent types. */
4309 && !dependent_type_p (new_scope
))
4311 new_scope
= complete_type (new_scope
);
4312 /* If it is a typedef to current class, use the current
4313 class instead, as the typedef won't have any names inside
4315 if (!COMPLETE_TYPE_P (new_scope
)
4316 && currently_open_class (new_scope
))
4317 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
4319 /* Make sure we look in the right scope the next time through
4321 parser
->scope
= new_scope
;
4324 /* If parsing tentatively, replace the sequence of tokens that makes
4325 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4326 token. That way, should we re-parse the token stream, we will
4327 not have to repeat the effort required to do the parse, nor will
4328 we issue duplicate error messages. */
4329 if (success
&& start
)
4333 token
= cp_lexer_token_at (parser
->lexer
, start
);
4334 /* Reset the contents of the START token. */
4335 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
4336 /* Retrieve any deferred checks. Do not pop this access checks yet
4337 so the memory will not be reclaimed during token replacing below. */
4338 token
->u
.tree_check_value
= GGC_CNEW (struct tree_check
);
4339 token
->u
.tree_check_value
->value
= parser
->scope
;
4340 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
4341 token
->u
.tree_check_value
->qualifying_scope
=
4342 parser
->qualifying_scope
;
4343 token
->keyword
= RID_MAX
;
4345 /* Purge all subsequent tokens. */
4346 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4350 pop_to_parent_deferring_access_checks ();
4352 return success
? parser
->scope
: NULL_TREE
;
4355 /* Parse a nested-name-specifier. See
4356 cp_parser_nested_name_specifier_opt for details. This function
4357 behaves identically, except that it will an issue an error if no
4358 nested-name-specifier is present. */
4361 cp_parser_nested_name_specifier (cp_parser
*parser
,
4362 bool typename_keyword_p
,
4363 bool check_dependency_p
,
4365 bool is_declaration
)
4369 /* Look for the nested-name-specifier. */
4370 scope
= cp_parser_nested_name_specifier_opt (parser
,
4375 /* If it was not present, issue an error message. */
4378 cp_parser_error (parser
, "expected nested-name-specifier");
4379 parser
->scope
= NULL_TREE
;
4385 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4386 this is either a class-name or a namespace-name (which corresponds
4387 to the class-or-namespace-name production in the grammar). For
4388 C++0x, it can also be a type-name that refers to an enumeration
4391 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4392 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4393 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4394 TYPE_P is TRUE iff the next name should be taken as a class-name,
4395 even the same name is declared to be another entity in the same
4398 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4399 specified by the class-or-namespace-name. If neither is found the
4400 ERROR_MARK_NODE is returned. */
4403 cp_parser_qualifying_entity (cp_parser
*parser
,
4404 bool typename_keyword_p
,
4405 bool template_keyword_p
,
4406 bool check_dependency_p
,
4408 bool is_declaration
)
4411 tree saved_qualifying_scope
;
4412 tree saved_object_scope
;
4415 bool successful_parse_p
;
4417 /* Before we try to parse the class-name, we must save away the
4418 current PARSER->SCOPE since cp_parser_class_name will destroy
4420 saved_scope
= parser
->scope
;
4421 saved_qualifying_scope
= parser
->qualifying_scope
;
4422 saved_object_scope
= parser
->object_scope
;
4423 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4424 there is no need to look for a namespace-name. */
4425 only_class_p
= template_keyword_p
4426 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
4428 cp_parser_parse_tentatively (parser
);
4429 scope
= cp_parser_class_name (parser
,
4432 type_p
? class_type
: none_type
,
4434 /*class_head_p=*/false,
4436 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
4437 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4439 && cxx_dialect
!= cxx98
4440 && !successful_parse_p
)
4442 /* Restore the saved scope. */
4443 parser
->scope
= saved_scope
;
4444 parser
->qualifying_scope
= saved_qualifying_scope
;
4445 parser
->object_scope
= saved_object_scope
;
4447 /* Parse tentatively. */
4448 cp_parser_parse_tentatively (parser
);
4450 /* Parse a typedef-name or enum-name. */
4451 scope
= cp_parser_nonclass_name (parser
);
4453 /* "If the name found does not designate a namespace or a class,
4454 enumeration, or dependent type, the program is ill-formed."
4456 We cover classes and dependent types above and namespaces below,
4457 so this code is only looking for enums. */
4458 if (!scope
|| TREE_CODE (scope
) != TYPE_DECL
4459 || TREE_CODE (TREE_TYPE (scope
)) != ENUMERAL_TYPE
)
4460 cp_parser_simulate_error (parser
);
4462 successful_parse_p
= cp_parser_parse_definitely (parser
);
4464 /* If that didn't work, try for a namespace-name. */
4465 if (!only_class_p
&& !successful_parse_p
)
4467 /* Restore the saved scope. */
4468 parser
->scope
= saved_scope
;
4469 parser
->qualifying_scope
= saved_qualifying_scope
;
4470 parser
->object_scope
= saved_object_scope
;
4471 /* If we are not looking at an identifier followed by the scope
4472 resolution operator, then this is not part of a
4473 nested-name-specifier. (Note that this function is only used
4474 to parse the components of a nested-name-specifier.) */
4475 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
4476 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
4477 return error_mark_node
;
4478 scope
= cp_parser_namespace_name (parser
);
4484 /* Parse a postfix-expression.
4488 postfix-expression [ expression ]
4489 postfix-expression ( expression-list [opt] )
4490 simple-type-specifier ( expression-list [opt] )
4491 typename :: [opt] nested-name-specifier identifier
4492 ( expression-list [opt] )
4493 typename :: [opt] nested-name-specifier template [opt] template-id
4494 ( expression-list [opt] )
4495 postfix-expression . template [opt] id-expression
4496 postfix-expression -> template [opt] id-expression
4497 postfix-expression . pseudo-destructor-name
4498 postfix-expression -> pseudo-destructor-name
4499 postfix-expression ++
4500 postfix-expression --
4501 dynamic_cast < type-id > ( expression )
4502 static_cast < type-id > ( expression )
4503 reinterpret_cast < type-id > ( expression )
4504 const_cast < type-id > ( expression )
4505 typeid ( expression )
4511 ( type-id ) { initializer-list , [opt] }
4513 This extension is a GNU version of the C99 compound-literal
4514 construct. (The C99 grammar uses `type-name' instead of `type-id',
4515 but they are essentially the same concept.)
4517 If ADDRESS_P is true, the postfix expression is the operand of the
4518 `&' operator. CAST_P is true if this expression is the target of a
4521 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4522 class member access expressions [expr.ref].
4524 Returns a representation of the expression. */
4527 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
4528 bool member_access_only_p
,
4529 cp_id_kind
* pidk_return
)
4533 cp_id_kind idk
= CP_ID_KIND_NONE
;
4534 tree postfix_expression
= NULL_TREE
;
4535 bool is_member_access
= false;
4537 /* Peek at the next token. */
4538 token
= cp_lexer_peek_token (parser
->lexer
);
4539 /* Some of the productions are determined by keywords. */
4540 keyword
= token
->keyword
;
4550 const char *saved_message
;
4552 /* All of these can be handled in the same way from the point
4553 of view of parsing. Begin by consuming the token
4554 identifying the cast. */
4555 cp_lexer_consume_token (parser
->lexer
);
4557 /* New types cannot be defined in the cast. */
4558 saved_message
= parser
->type_definition_forbidden_message
;
4559 parser
->type_definition_forbidden_message
4560 = G_("types may not be defined in casts");
4562 /* Look for the opening `<'. */
4563 cp_parser_require (parser
, CPP_LESS
, "%<<%>");
4564 /* Parse the type to which we are casting. */
4565 type
= cp_parser_type_id (parser
);
4566 /* Look for the closing `>'. */
4567 cp_parser_require (parser
, CPP_GREATER
, "%<>%>");
4568 /* Restore the old message. */
4569 parser
->type_definition_forbidden_message
= saved_message
;
4571 /* And the expression which is being cast. */
4572 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
4573 expression
= cp_parser_expression (parser
, /*cast_p=*/true, & idk
);
4574 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
4576 /* Only type conversions to integral or enumeration types
4577 can be used in constant-expressions. */
4578 if (!cast_valid_in_integral_constant_expression_p (type
)
4579 && (cp_parser_non_integral_constant_expression
4581 "a cast to a type other than an integral or "
4582 "enumeration type")))
4583 return error_mark_node
;
4589 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
4593 = build_static_cast (type
, expression
, tf_warning_or_error
);
4597 = build_reinterpret_cast (type
, expression
,
4598 tf_warning_or_error
);
4602 = build_const_cast (type
, expression
, tf_warning_or_error
);
4613 const char *saved_message
;
4614 bool saved_in_type_id_in_expr_p
;
4616 /* Consume the `typeid' token. */
4617 cp_lexer_consume_token (parser
->lexer
);
4618 /* Look for the `(' token. */
4619 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
4620 /* Types cannot be defined in a `typeid' expression. */
4621 saved_message
= parser
->type_definition_forbidden_message
;
4622 parser
->type_definition_forbidden_message
4623 = G_("types may not be defined in a %<typeid%> expression");
4624 /* We can't be sure yet whether we're looking at a type-id or an
4626 cp_parser_parse_tentatively (parser
);
4627 /* Try a type-id first. */
4628 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4629 parser
->in_type_id_in_expr_p
= true;
4630 type
= cp_parser_type_id (parser
);
4631 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4632 /* Look for the `)' token. Otherwise, we can't be sure that
4633 we're not looking at an expression: consider `typeid (int
4634 (3))', for example. */
4635 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
4636 /* If all went well, simply lookup the type-id. */
4637 if (cp_parser_parse_definitely (parser
))
4638 postfix_expression
= get_typeid (type
);
4639 /* Otherwise, fall back to the expression variant. */
4644 /* Look for an expression. */
4645 expression
= cp_parser_expression (parser
, /*cast_p=*/false, & idk
);
4646 /* Compute its typeid. */
4647 postfix_expression
= build_typeid (expression
);
4648 /* Look for the `)' token. */
4649 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
4651 /* Restore the saved message. */
4652 parser
->type_definition_forbidden_message
= saved_message
;
4653 /* `typeid' may not appear in an integral constant expression. */
4654 if (cp_parser_non_integral_constant_expression(parser
,
4655 "%<typeid%> operator"))
4656 return error_mark_node
;
4663 /* The syntax permitted here is the same permitted for an
4664 elaborated-type-specifier. */
4665 type
= cp_parser_elaborated_type_specifier (parser
,
4666 /*is_friend=*/false,
4667 /*is_declaration=*/false);
4668 postfix_expression
= cp_parser_functional_cast (parser
, type
);
4676 /* If the next thing is a simple-type-specifier, we may be
4677 looking at a functional cast. We could also be looking at
4678 an id-expression. So, we try the functional cast, and if
4679 that doesn't work we fall back to the primary-expression. */
4680 cp_parser_parse_tentatively (parser
);
4681 /* Look for the simple-type-specifier. */
4682 type
= cp_parser_simple_type_specifier (parser
,
4683 /*decl_specs=*/NULL
,
4684 CP_PARSER_FLAGS_NONE
);
4685 /* Parse the cast itself. */
4686 if (!cp_parser_error_occurred (parser
))
4688 = cp_parser_functional_cast (parser
, type
);
4689 /* If that worked, we're done. */
4690 if (cp_parser_parse_definitely (parser
))
4693 /* If the functional-cast didn't work out, try a
4694 compound-literal. */
4695 if (cp_parser_allow_gnu_extensions_p (parser
)
4696 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4698 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
4699 bool saved_in_type_id_in_expr_p
;
4701 cp_parser_parse_tentatively (parser
);
4702 /* Consume the `('. */
4703 cp_lexer_consume_token (parser
->lexer
);
4704 /* Parse the type. */
4705 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4706 parser
->in_type_id_in_expr_p
= true;
4707 type
= cp_parser_type_id (parser
);
4708 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4709 /* Look for the `)'. */
4710 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
4711 /* Look for the `{'. */
4712 cp_parser_require (parser
, CPP_OPEN_BRACE
, "%<{%>");
4713 /* If things aren't going well, there's no need to
4715 if (!cp_parser_error_occurred (parser
))
4717 bool non_constant_p
;
4718 /* Parse the initializer-list. */
4720 = cp_parser_initializer_list (parser
, &non_constant_p
);
4721 /* Allow a trailing `,'. */
4722 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
4723 cp_lexer_consume_token (parser
->lexer
);
4724 /* Look for the final `}'. */
4725 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
4727 /* If that worked, we're definitely looking at a
4728 compound-literal expression. */
4729 if (cp_parser_parse_definitely (parser
))
4731 /* Warn the user that a compound literal is not
4732 allowed in standard C++. */
4733 pedwarn (input_location
, OPT_pedantic
, "ISO C++ forbids compound-literals");
4734 /* For simplicity, we disallow compound literals in
4735 constant-expressions. We could
4736 allow compound literals of integer type, whose
4737 initializer was a constant, in constant
4738 expressions. Permitting that usage, as a further
4739 extension, would not change the meaning of any
4740 currently accepted programs. (Of course, as
4741 compound literals are not part of ISO C++, the
4742 standard has nothing to say.) */
4743 if (cp_parser_non_integral_constant_expression
4744 (parser
, "non-constant compound literals"))
4746 postfix_expression
= error_mark_node
;
4749 /* Form the representation of the compound-literal. */
4751 = (finish_compound_literal
4752 (type
, build_constructor (init_list_type_node
,
4753 initializer_list
)));
4758 /* It must be a primary-expression. */
4760 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
4761 /*template_arg_p=*/false,
4767 /* Keep looping until the postfix-expression is complete. */
4770 if (idk
== CP_ID_KIND_UNQUALIFIED
4771 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4772 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4773 /* It is not a Koenig lookup function call. */
4775 = unqualified_name_lookup_error (postfix_expression
);
4777 /* Peek at the next token. */
4778 token
= cp_lexer_peek_token (parser
->lexer
);
4780 switch (token
->type
)
4782 case CPP_OPEN_SQUARE
:
4784 = cp_parser_postfix_open_square_expression (parser
,
4787 idk
= CP_ID_KIND_NONE
;
4788 is_member_access
= false;
4791 case CPP_OPEN_PAREN
:
4792 /* postfix-expression ( expression-list [opt] ) */
4795 bool is_builtin_constant_p
;
4796 bool saved_integral_constant_expression_p
= false;
4797 bool saved_non_integral_constant_expression_p
= false;
4800 is_member_access
= false;
4802 is_builtin_constant_p
4803 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
4804 if (is_builtin_constant_p
)
4806 /* The whole point of __builtin_constant_p is to allow
4807 non-constant expressions to appear as arguments. */
4808 saved_integral_constant_expression_p
4809 = parser
->integral_constant_expression_p
;
4810 saved_non_integral_constant_expression_p
4811 = parser
->non_integral_constant_expression_p
;
4812 parser
->integral_constant_expression_p
= false;
4814 args
= (cp_parser_parenthesized_expression_list
4816 /*cast_p=*/false, /*allow_expansion_p=*/true,
4817 /*non_constant_p=*/NULL
));
4818 if (is_builtin_constant_p
)
4820 parser
->integral_constant_expression_p
4821 = saved_integral_constant_expression_p
;
4822 parser
->non_integral_constant_expression_p
4823 = saved_non_integral_constant_expression_p
;
4828 postfix_expression
= error_mark_node
;
4832 /* Function calls are not permitted in
4833 constant-expressions. */
4834 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
4835 && cp_parser_non_integral_constant_expression (parser
,
4838 postfix_expression
= error_mark_node
;
4839 release_tree_vector (args
);
4844 if (idk
== CP_ID_KIND_UNQUALIFIED
4845 || idk
== CP_ID_KIND_TEMPLATE_ID
)
4847 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
4849 if (!VEC_empty (tree
, args
))
4852 if (!any_type_dependent_arguments_p (args
))
4854 = perform_koenig_lookup (postfix_expression
, args
);
4858 = unqualified_fn_lookup_error (postfix_expression
);
4860 /* We do not perform argument-dependent lookup if
4861 normal lookup finds a non-function, in accordance
4862 with the expected resolution of DR 218. */
4863 else if (!VEC_empty (tree
, args
)
4864 && is_overloaded_fn (postfix_expression
))
4866 tree fn
= get_first_fn (postfix_expression
);
4867 fn
= STRIP_TEMPLATE (fn
);
4869 /* Do not do argument dependent lookup if regular
4870 lookup finds a member function or a block-scope
4871 function declaration. [basic.lookup.argdep]/3 */
4872 if (!DECL_FUNCTION_MEMBER_P (fn
)
4873 && !DECL_LOCAL_FUNCTION_P (fn
))
4876 if (!any_type_dependent_arguments_p (args
))
4878 = perform_koenig_lookup (postfix_expression
, args
);
4883 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4885 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4886 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4888 if (processing_template_decl
4889 && (type_dependent_expression_p (instance
)
4890 || (!BASELINK_P (fn
)
4891 && TREE_CODE (fn
) != FIELD_DECL
)
4892 || type_dependent_expression_p (fn
)
4893 || any_type_dependent_arguments_p (args
)))
4896 = build_nt_call_vec (postfix_expression
, args
);
4897 release_tree_vector (args
);
4901 if (BASELINK_P (fn
))
4904 = (build_new_method_call
4905 (instance
, fn
, &args
, NULL_TREE
,
4906 (idk
== CP_ID_KIND_QUALIFIED
4907 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
),
4909 tf_warning_or_error
));
4913 = finish_call_expr (postfix_expression
, &args
,
4914 /*disallow_virtual=*/false,
4916 tf_warning_or_error
);
4918 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4919 || TREE_CODE (postfix_expression
) == MEMBER_REF
4920 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4921 postfix_expression
= (build_offset_ref_call_from_tree
4922 (postfix_expression
, &args
));
4923 else if (idk
== CP_ID_KIND_QUALIFIED
)
4924 /* A call to a static class member, or a namespace-scope
4927 = finish_call_expr (postfix_expression
, &args
,
4928 /*disallow_virtual=*/true,
4930 tf_warning_or_error
);
4932 /* All other function calls. */
4934 = finish_call_expr (postfix_expression
, &args
,
4935 /*disallow_virtual=*/false,
4937 tf_warning_or_error
);
4939 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4940 idk
= CP_ID_KIND_NONE
;
4942 release_tree_vector (args
);
4948 /* postfix-expression . template [opt] id-expression
4949 postfix-expression . pseudo-destructor-name
4950 postfix-expression -> template [opt] id-expression
4951 postfix-expression -> pseudo-destructor-name */
4953 /* Consume the `.' or `->' operator. */
4954 cp_lexer_consume_token (parser
->lexer
);
4957 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4962 is_member_access
= true;
4966 /* postfix-expression ++ */
4967 /* Consume the `++' token. */
4968 cp_lexer_consume_token (parser
->lexer
);
4969 /* Generate a representation for the complete expression. */
4971 = finish_increment_expr (postfix_expression
,
4972 POSTINCREMENT_EXPR
);
4973 /* Increments may not appear in constant-expressions. */
4974 if (cp_parser_non_integral_constant_expression (parser
,
4976 postfix_expression
= error_mark_node
;
4977 idk
= CP_ID_KIND_NONE
;
4978 is_member_access
= false;
4981 case CPP_MINUS_MINUS
:
4982 /* postfix-expression -- */
4983 /* Consume the `--' token. */
4984 cp_lexer_consume_token (parser
->lexer
);
4985 /* Generate a representation for the complete expression. */
4987 = finish_increment_expr (postfix_expression
,
4988 POSTDECREMENT_EXPR
);
4989 /* Decrements may not appear in constant-expressions. */
4990 if (cp_parser_non_integral_constant_expression (parser
,
4992 postfix_expression
= error_mark_node
;
4993 idk
= CP_ID_KIND_NONE
;
4994 is_member_access
= false;
4998 if (pidk_return
!= NULL
)
4999 * pidk_return
= idk
;
5000 if (member_access_only_p
)
5001 return is_member_access
? postfix_expression
: error_mark_node
;
5003 return postfix_expression
;
5007 /* We should never get here. */
5009 return error_mark_node
;
5012 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5013 by cp_parser_builtin_offsetof. We're looking for
5015 postfix-expression [ expression ]
5017 FOR_OFFSETOF is set if we're being called in that context, which
5018 changes how we deal with integer constant expressions. */
5021 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
5022 tree postfix_expression
,
5027 /* Consume the `[' token. */
5028 cp_lexer_consume_token (parser
->lexer
);
5030 /* Parse the index expression. */
5031 /* ??? For offsetof, there is a question of what to allow here. If
5032 offsetof is not being used in an integral constant expression context,
5033 then we *could* get the right answer by computing the value at runtime.
5034 If we are in an integral constant expression context, then we might
5035 could accept any constant expression; hard to say without analysis.
5036 Rather than open the barn door too wide right away, allow only integer
5037 constant expressions here. */
5039 index
= cp_parser_constant_expression (parser
, false, NULL
);
5041 index
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
5043 /* Look for the closing `]'. */
5044 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "%<]%>");
5046 /* Build the ARRAY_REF. */
5047 postfix_expression
= grok_array_decl (postfix_expression
, index
);
5049 /* When not doing offsetof, array references are not permitted in
5050 constant-expressions. */
5052 && (cp_parser_non_integral_constant_expression
5053 (parser
, "an array reference")))
5054 postfix_expression
= error_mark_node
;
5056 return postfix_expression
;
5059 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5060 by cp_parser_builtin_offsetof. We're looking for
5062 postfix-expression . template [opt] id-expression
5063 postfix-expression . pseudo-destructor-name
5064 postfix-expression -> template [opt] id-expression
5065 postfix-expression -> pseudo-destructor-name
5067 FOR_OFFSETOF is set if we're being called in that context. That sorta
5068 limits what of the above we'll actually accept, but nevermind.
5069 TOKEN_TYPE is the "." or "->" token, which will already have been
5070 removed from the stream. */
5073 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
5074 enum cpp_ttype token_type
,
5075 tree postfix_expression
,
5076 bool for_offsetof
, cp_id_kind
*idk
,
5077 location_t location
)
5081 bool pseudo_destructor_p
;
5082 tree scope
= NULL_TREE
;
5084 /* If this is a `->' operator, dereference the pointer. */
5085 if (token_type
== CPP_DEREF
)
5086 postfix_expression
= build_x_arrow (postfix_expression
);
5087 /* Check to see whether or not the expression is type-dependent. */
5088 dependent_p
= type_dependent_expression_p (postfix_expression
);
5089 /* The identifier following the `->' or `.' is not qualified. */
5090 parser
->scope
= NULL_TREE
;
5091 parser
->qualifying_scope
= NULL_TREE
;
5092 parser
->object_scope
= NULL_TREE
;
5093 *idk
= CP_ID_KIND_NONE
;
5095 /* Enter the scope corresponding to the type of the object
5096 given by the POSTFIX_EXPRESSION. */
5097 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
5099 scope
= TREE_TYPE (postfix_expression
);
5100 /* According to the standard, no expression should ever have
5101 reference type. Unfortunately, we do not currently match
5102 the standard in this respect in that our internal representation
5103 of an expression may have reference type even when the standard
5104 says it does not. Therefore, we have to manually obtain the
5105 underlying type here. */
5106 scope
= non_reference (scope
);
5107 /* The type of the POSTFIX_EXPRESSION must be complete. */
5108 if (scope
== unknown_type_node
)
5110 error_at (location
, "%qE does not have class type",
5111 postfix_expression
);
5115 scope
= complete_type_or_else (scope
, NULL_TREE
);
5116 /* Let the name lookup machinery know that we are processing a
5117 class member access expression. */
5118 parser
->context
->object_type
= scope
;
5119 /* If something went wrong, we want to be able to discern that case,
5120 as opposed to the case where there was no SCOPE due to the type
5121 of expression being dependent. */
5123 scope
= error_mark_node
;
5124 /* If the SCOPE was erroneous, make the various semantic analysis
5125 functions exit quickly -- and without issuing additional error
5127 if (scope
== error_mark_node
)
5128 postfix_expression
= error_mark_node
;
5131 /* Assume this expression is not a pseudo-destructor access. */
5132 pseudo_destructor_p
= false;
5134 /* If the SCOPE is a scalar type, then, if this is a valid program,
5135 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5136 is type dependent, it can be pseudo-destructor-name or something else.
5137 Try to parse it as pseudo-destructor-name first. */
5138 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
5143 cp_parser_parse_tentatively (parser
);
5144 /* Parse the pseudo-destructor-name. */
5146 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
5148 && (cp_parser_error_occurred (parser
)
5149 || TREE_CODE (type
) != TYPE_DECL
5150 || !SCALAR_TYPE_P (TREE_TYPE (type
))))
5151 cp_parser_abort_tentative_parse (parser
);
5152 else if (cp_parser_parse_definitely (parser
))
5154 pseudo_destructor_p
= true;
5156 = finish_pseudo_destructor_expr (postfix_expression
,
5157 s
, TREE_TYPE (type
));
5161 if (!pseudo_destructor_p
)
5163 /* If the SCOPE is not a scalar type, we are looking at an
5164 ordinary class member access expression, rather than a
5165 pseudo-destructor-name. */
5167 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
5168 /* Parse the id-expression. */
5169 name
= (cp_parser_id_expression
5171 cp_parser_optional_template_keyword (parser
),
5172 /*check_dependency_p=*/true,
5174 /*declarator_p=*/false,
5175 /*optional_p=*/false));
5176 /* In general, build a SCOPE_REF if the member name is qualified.
5177 However, if the name was not dependent and has already been
5178 resolved; there is no need to build the SCOPE_REF. For example;
5180 struct X { void f(); };
5181 template <typename T> void f(T* t) { t->X::f(); }
5183 Even though "t" is dependent, "X::f" is not and has been resolved
5184 to a BASELINK; there is no need to include scope information. */
5186 /* But we do need to remember that there was an explicit scope for
5187 virtual function calls. */
5189 *idk
= CP_ID_KIND_QUALIFIED
;
5191 /* If the name is a template-id that names a type, we will get a
5192 TYPE_DECL here. That is invalid code. */
5193 if (TREE_CODE (name
) == TYPE_DECL
)
5195 error_at (token
->location
, "invalid use of %qD", name
);
5196 postfix_expression
= error_mark_node
;
5200 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
5202 name
= build_qualified_name (/*type=*/NULL_TREE
,
5206 parser
->scope
= NULL_TREE
;
5207 parser
->qualifying_scope
= NULL_TREE
;
5208 parser
->object_scope
= NULL_TREE
;
5210 if (scope
&& name
&& BASELINK_P (name
))
5211 adjust_result_of_qualified_name_lookup
5212 (name
, BINFO_TYPE (BASELINK_ACCESS_BINFO (name
)), scope
);
5214 = finish_class_member_access_expr (postfix_expression
, name
,
5216 tf_warning_or_error
);
5220 /* We no longer need to look up names in the scope of the object on
5221 the left-hand side of the `.' or `->' operator. */
5222 parser
->context
->object_type
= NULL_TREE
;
5224 /* Outside of offsetof, these operators may not appear in
5225 constant-expressions. */
5227 && (cp_parser_non_integral_constant_expression
5228 (parser
, token_type
== CPP_DEREF
? "%<->%>" : "%<.%>")))
5229 postfix_expression
= error_mark_node
;
5231 return postfix_expression
;
5234 /* Parse a parenthesized expression-list.
5237 assignment-expression
5238 expression-list, assignment-expression
5243 identifier, expression-list
5245 CAST_P is true if this expression is the target of a cast.
5247 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5250 Returns a vector of trees. Each element is a representation of an
5251 assignment-expression. NULL is returned if the ( and or ) are
5252 missing. An empty, but allocated, vector is returned on no
5253 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5254 if we are parsing an attribute list for an attribute that wants a
5255 plain identifier argument, normal_attr for an attribute that wants
5256 an expression, or non_attr if we aren't parsing an attribute list. If
5257 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5258 not all of the expressions in the list were constant. */
5260 static VEC(tree
,gc
) *
5261 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
5262 int is_attribute_list
,
5264 bool allow_expansion_p
,
5265 bool *non_constant_p
)
5267 VEC(tree
,gc
) *expression_list
;
5268 bool fold_expr_p
= is_attribute_list
!= non_attr
;
5269 tree identifier
= NULL_TREE
;
5270 bool saved_greater_than_is_operator_p
;
5272 /* Assume all the expressions will be constant. */
5274 *non_constant_p
= false;
5276 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
5279 expression_list
= make_tree_vector ();
5281 /* Within a parenthesized expression, a `>' token is always
5282 the greater-than operator. */
5283 saved_greater_than_is_operator_p
5284 = parser
->greater_than_is_operator_p
;
5285 parser
->greater_than_is_operator_p
= true;
5287 /* Consume expressions until there are no more. */
5288 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
5293 /* At the beginning of attribute lists, check to see if the
5294 next token is an identifier. */
5295 if (is_attribute_list
== id_attr
5296 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
5300 /* Consume the identifier. */
5301 token
= cp_lexer_consume_token (parser
->lexer
);
5302 /* Save the identifier. */
5303 identifier
= token
->u
.value
;
5307 bool expr_non_constant_p
;
5309 /* Parse the next assignment-expression. */
5310 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
5312 /* A braced-init-list. */
5313 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
5314 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
5315 if (non_constant_p
&& expr_non_constant_p
)
5316 *non_constant_p
= true;
5318 else if (non_constant_p
)
5320 expr
= (cp_parser_constant_expression
5321 (parser
, /*allow_non_constant_p=*/true,
5322 &expr_non_constant_p
));
5323 if (expr_non_constant_p
)
5324 *non_constant_p
= true;
5327 expr
= cp_parser_assignment_expression (parser
, cast_p
, NULL
);
5330 expr
= fold_non_dependent_expr (expr
);
5332 /* If we have an ellipsis, then this is an expression
5334 if (allow_expansion_p
5335 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5337 /* Consume the `...'. */
5338 cp_lexer_consume_token (parser
->lexer
);
5340 /* Build the argument pack. */
5341 expr
= make_pack_expansion (expr
);
5344 /* Add it to the list. We add error_mark_node
5345 expressions to the list, so that we can still tell if
5346 the correct form for a parenthesized expression-list
5347 is found. That gives better errors. */
5348 VEC_safe_push (tree
, gc
, expression_list
, expr
);
5350 if (expr
== error_mark_node
)
5354 /* After the first item, attribute lists look the same as
5355 expression lists. */
5356 is_attribute_list
= non_attr
;
5359 /* If the next token isn't a `,', then we are done. */
5360 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5363 /* Otherwise, consume the `,' and keep going. */
5364 cp_lexer_consume_token (parser
->lexer
);
5367 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
5372 /* We try and resync to an unnested comma, as that will give the
5373 user better diagnostics. */
5374 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
5375 /*recovering=*/true,
5377 /*consume_paren=*/true);
5382 parser
->greater_than_is_operator_p
5383 = saved_greater_than_is_operator_p
;
5388 parser
->greater_than_is_operator_p
5389 = saved_greater_than_is_operator_p
;
5392 VEC_safe_insert (tree
, gc
, expression_list
, 0, identifier
);
5394 return expression_list
;
5397 /* Parse a pseudo-destructor-name.
5399 pseudo-destructor-name:
5400 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5401 :: [opt] nested-name-specifier template template-id :: ~ type-name
5402 :: [opt] nested-name-specifier [opt] ~ type-name
5404 If either of the first two productions is used, sets *SCOPE to the
5405 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5406 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5407 or ERROR_MARK_NODE if the parse fails. */
5410 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
5414 bool nested_name_specifier_p
;
5416 /* Assume that things will not work out. */
5417 *type
= error_mark_node
;
5419 /* Look for the optional `::' operator. */
5420 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
5421 /* Look for the optional nested-name-specifier. */
5422 nested_name_specifier_p
5423 = (cp_parser_nested_name_specifier_opt (parser
,
5424 /*typename_keyword_p=*/false,
5425 /*check_dependency_p=*/true,
5427 /*is_declaration=*/false)
5429 /* Now, if we saw a nested-name-specifier, we might be doing the
5430 second production. */
5431 if (nested_name_specifier_p
5432 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
5434 /* Consume the `template' keyword. */
5435 cp_lexer_consume_token (parser
->lexer
);
5436 /* Parse the template-id. */
5437 cp_parser_template_id (parser
,
5438 /*template_keyword_p=*/true,
5439 /*check_dependency_p=*/false,
5440 /*is_declaration=*/true);
5441 /* Look for the `::' token. */
5442 cp_parser_require (parser
, CPP_SCOPE
, "%<::%>");
5444 /* If the next token is not a `~', then there might be some
5445 additional qualification. */
5446 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
5448 /* At this point, we're looking for "type-name :: ~". The type-name
5449 must not be a class-name, since this is a pseudo-destructor. So,
5450 it must be either an enum-name, or a typedef-name -- both of which
5451 are just identifiers. So, we peek ahead to check that the "::"
5452 and "~" tokens are present; if they are not, then we can avoid
5453 calling type_name. */
5454 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
5455 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
5456 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
5458 cp_parser_error (parser
, "non-scalar type");
5462 /* Look for the type-name. */
5463 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
5464 if (*scope
== error_mark_node
)
5467 /* Look for the `::' token. */
5468 cp_parser_require (parser
, CPP_SCOPE
, "%<::%>");
5473 /* Look for the `~'. */
5474 cp_parser_require (parser
, CPP_COMPL
, "%<~%>");
5475 /* Look for the type-name again. We are not responsible for
5476 checking that it matches the first type-name. */
5477 *type
= cp_parser_nonclass_name (parser
);
5480 /* Parse a unary-expression.
5486 unary-operator cast-expression
5487 sizeof unary-expression
5495 __extension__ cast-expression
5496 __alignof__ unary-expression
5497 __alignof__ ( type-id )
5498 __real__ cast-expression
5499 __imag__ cast-expression
5502 ADDRESS_P is true iff the unary-expression is appearing as the
5503 operand of the `&' operator. CAST_P is true if this expression is
5504 the target of a cast.
5506 Returns a representation of the expression. */
5509 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5513 enum tree_code unary_operator
;
5515 /* Peek at the next token. */
5516 token
= cp_lexer_peek_token (parser
->lexer
);
5517 /* Some keywords give away the kind of expression. */
5518 if (token
->type
== CPP_KEYWORD
)
5520 enum rid keyword
= token
->keyword
;
5530 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
5531 /* Consume the token. */
5532 cp_lexer_consume_token (parser
->lexer
);
5533 /* Parse the operand. */
5534 operand
= cp_parser_sizeof_operand (parser
, keyword
);
5536 if (TYPE_P (operand
))
5537 return cxx_sizeof_or_alignof_type (operand
, op
, true);
5539 return cxx_sizeof_or_alignof_expr (operand
, op
, true);
5543 return cp_parser_new_expression (parser
);
5546 return cp_parser_delete_expression (parser
);
5550 /* The saved value of the PEDANTIC flag. */
5554 /* Save away the PEDANTIC flag. */
5555 cp_parser_extension_opt (parser
, &saved_pedantic
);
5556 /* Parse the cast-expression. */
5557 expr
= cp_parser_simple_cast_expression (parser
);
5558 /* Restore the PEDANTIC flag. */
5559 pedantic
= saved_pedantic
;
5569 /* Consume the `__real__' or `__imag__' token. */
5570 cp_lexer_consume_token (parser
->lexer
);
5571 /* Parse the cast-expression. */
5572 expression
= cp_parser_simple_cast_expression (parser
);
5573 /* Create the complete representation. */
5574 return build_x_unary_op ((keyword
== RID_REALPART
5575 ? REALPART_EXPR
: IMAGPART_EXPR
),
5577 tf_warning_or_error
);
5586 /* Look for the `:: new' and `:: delete', which also signal the
5587 beginning of a new-expression, or delete-expression,
5588 respectively. If the next token is `::', then it might be one of
5590 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
5594 /* See if the token after the `::' is one of the keywords in
5595 which we're interested. */
5596 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
5597 /* If it's `new', we have a new-expression. */
5598 if (keyword
== RID_NEW
)
5599 return cp_parser_new_expression (parser
);
5600 /* Similarly, for `delete'. */
5601 else if (keyword
== RID_DELETE
)
5602 return cp_parser_delete_expression (parser
);
5605 /* Look for a unary operator. */
5606 unary_operator
= cp_parser_unary_operator (token
);
5607 /* The `++' and `--' operators can be handled similarly, even though
5608 they are not technically unary-operators in the grammar. */
5609 if (unary_operator
== ERROR_MARK
)
5611 if (token
->type
== CPP_PLUS_PLUS
)
5612 unary_operator
= PREINCREMENT_EXPR
;
5613 else if (token
->type
== CPP_MINUS_MINUS
)
5614 unary_operator
= PREDECREMENT_EXPR
;
5615 /* Handle the GNU address-of-label extension. */
5616 else if (cp_parser_allow_gnu_extensions_p (parser
)
5617 && token
->type
== CPP_AND_AND
)
5621 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
5623 /* Consume the '&&' token. */
5624 cp_lexer_consume_token (parser
->lexer
);
5625 /* Look for the identifier. */
5626 identifier
= cp_parser_identifier (parser
);
5627 /* Create an expression representing the address. */
5628 expression
= finish_label_address_expr (identifier
, loc
);
5629 if (cp_parser_non_integral_constant_expression (parser
,
5630 "the address of a label"))
5631 expression
= error_mark_node
;
5635 if (unary_operator
!= ERROR_MARK
)
5637 tree cast_expression
;
5638 tree expression
= error_mark_node
;
5639 const char *non_constant_p
= NULL
;
5641 /* Consume the operator token. */
5642 token
= cp_lexer_consume_token (parser
->lexer
);
5643 /* Parse the cast-expression. */
5645 = cp_parser_cast_expression (parser
,
5646 unary_operator
== ADDR_EXPR
,
5647 /*cast_p=*/false, pidk
);
5648 /* Now, build an appropriate representation. */
5649 switch (unary_operator
)
5652 non_constant_p
= "%<*%>";
5653 expression
= build_x_indirect_ref (cast_expression
, RO_UNARY_STAR
,
5654 tf_warning_or_error
);
5658 non_constant_p
= "%<&%>";
5661 expression
= build_x_unary_op (unary_operator
, cast_expression
,
5662 tf_warning_or_error
);
5665 case PREINCREMENT_EXPR
:
5666 case PREDECREMENT_EXPR
:
5667 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
5668 ? "%<++%>" : "%<--%>");
5670 case UNARY_PLUS_EXPR
:
5672 case TRUTH_NOT_EXPR
:
5673 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
5681 && cp_parser_non_integral_constant_expression (parser
,
5683 expression
= error_mark_node
;
5688 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
5689 /*member_access_only_p=*/false,
5693 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5694 unary-operator, the corresponding tree code is returned. */
5696 static enum tree_code
5697 cp_parser_unary_operator (cp_token
* token
)
5699 switch (token
->type
)
5702 return INDIRECT_REF
;
5708 return UNARY_PLUS_EXPR
;
5714 return TRUTH_NOT_EXPR
;
5717 return BIT_NOT_EXPR
;
5724 /* Parse a new-expression.
5727 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5728 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5730 Returns a representation of the expression. */
5733 cp_parser_new_expression (cp_parser
* parser
)
5735 bool global_scope_p
;
5736 VEC(tree
,gc
) *placement
;
5738 VEC(tree
,gc
) *initializer
;
5742 /* Look for the optional `::' operator. */
5744 = (cp_parser_global_scope_opt (parser
,
5745 /*current_scope_valid_p=*/false)
5747 /* Look for the `new' operator. */
5748 cp_parser_require_keyword (parser
, RID_NEW
, "%<new%>");
5749 /* There's no easy way to tell a new-placement from the
5750 `( type-id )' construct. */
5751 cp_parser_parse_tentatively (parser
);
5752 /* Look for a new-placement. */
5753 placement
= cp_parser_new_placement (parser
);
5754 /* If that didn't work out, there's no new-placement. */
5755 if (!cp_parser_parse_definitely (parser
))
5757 if (placement
!= NULL
)
5758 release_tree_vector (placement
);
5762 /* If the next token is a `(', then we have a parenthesized
5764 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5767 /* Consume the `('. */
5768 cp_lexer_consume_token (parser
->lexer
);
5769 /* Parse the type-id. */
5770 type
= cp_parser_type_id (parser
);
5771 /* Look for the closing `)'. */
5772 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
5773 token
= cp_lexer_peek_token (parser
->lexer
);
5774 /* There should not be a direct-new-declarator in this production,
5775 but GCC used to allowed this, so we check and emit a sensible error
5776 message for this case. */
5777 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5779 error_at (token
->location
,
5780 "array bound forbidden after parenthesized type-id");
5781 inform (token
->location
,
5782 "try removing the parentheses around the type-id");
5783 cp_parser_direct_new_declarator (parser
);
5787 /* Otherwise, there must be a new-type-id. */
5789 type
= cp_parser_new_type_id (parser
, &nelts
);
5791 /* If the next token is a `(' or '{', then we have a new-initializer. */
5792 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
5793 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
5794 initializer
= cp_parser_new_initializer (parser
);
5798 /* A new-expression may not appear in an integral constant
5800 if (cp_parser_non_integral_constant_expression (parser
, "%<new%>"))
5801 ret
= error_mark_node
;
5804 /* Create a representation of the new-expression. */
5805 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
5806 tf_warning_or_error
);
5809 if (placement
!= NULL
)
5810 release_tree_vector (placement
);
5811 if (initializer
!= NULL
)
5812 release_tree_vector (initializer
);
5817 /* Parse a new-placement.
5822 Returns the same representation as for an expression-list. */
5824 static VEC(tree
,gc
) *
5825 cp_parser_new_placement (cp_parser
* parser
)
5827 VEC(tree
,gc
) *expression_list
;
5829 /* Parse the expression-list. */
5830 expression_list
= (cp_parser_parenthesized_expression_list
5831 (parser
, non_attr
, /*cast_p=*/false,
5832 /*allow_expansion_p=*/true,
5833 /*non_constant_p=*/NULL
));
5835 return expression_list
;
5838 /* Parse a new-type-id.
5841 type-specifier-seq new-declarator [opt]
5843 Returns the TYPE allocated. If the new-type-id indicates an array
5844 type, *NELTS is set to the number of elements in the last array
5845 bound; the TYPE will not include the last array bound. */
5848 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
5850 cp_decl_specifier_seq type_specifier_seq
;
5851 cp_declarator
*new_declarator
;
5852 cp_declarator
*declarator
;
5853 cp_declarator
*outer_declarator
;
5854 const char *saved_message
;
5857 /* The type-specifier sequence must not contain type definitions.
5858 (It cannot contain declarations of new types either, but if they
5859 are not definitions we will catch that because they are not
5861 saved_message
= parser
->type_definition_forbidden_message
;
5862 parser
->type_definition_forbidden_message
5863 = G_("types may not be defined in a new-type-id");
5864 /* Parse the type-specifier-seq. */
5865 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
5866 /*is_trailing_return=*/false,
5867 &type_specifier_seq
);
5868 /* Restore the old message. */
5869 parser
->type_definition_forbidden_message
= saved_message
;
5870 /* Parse the new-declarator. */
5871 new_declarator
= cp_parser_new_declarator_opt (parser
);
5873 /* Determine the number of elements in the last array dimension, if
5876 /* Skip down to the last array dimension. */
5877 declarator
= new_declarator
;
5878 outer_declarator
= NULL
;
5879 while (declarator
&& (declarator
->kind
== cdk_pointer
5880 || declarator
->kind
== cdk_ptrmem
))
5882 outer_declarator
= declarator
;
5883 declarator
= declarator
->declarator
;
5886 && declarator
->kind
== cdk_array
5887 && declarator
->declarator
5888 && declarator
->declarator
->kind
== cdk_array
)
5890 outer_declarator
= declarator
;
5891 declarator
= declarator
->declarator
;
5894 if (declarator
&& declarator
->kind
== cdk_array
)
5896 *nelts
= declarator
->u
.array
.bounds
;
5897 if (*nelts
== error_mark_node
)
5898 *nelts
= integer_one_node
;
5900 if (outer_declarator
)
5901 outer_declarator
->declarator
= declarator
->declarator
;
5903 new_declarator
= NULL
;
5906 type
= groktypename (&type_specifier_seq
, new_declarator
, false);
5910 /* Parse an (optional) new-declarator.
5913 ptr-operator new-declarator [opt]
5914 direct-new-declarator
5916 Returns the declarator. */
5918 static cp_declarator
*
5919 cp_parser_new_declarator_opt (cp_parser
* parser
)
5921 enum tree_code code
;
5923 cp_cv_quals cv_quals
;
5925 /* We don't know if there's a ptr-operator next, or not. */
5926 cp_parser_parse_tentatively (parser
);
5927 /* Look for a ptr-operator. */
5928 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
5929 /* If that worked, look for more new-declarators. */
5930 if (cp_parser_parse_definitely (parser
))
5932 cp_declarator
*declarator
;
5934 /* Parse another optional declarator. */
5935 declarator
= cp_parser_new_declarator_opt (parser
);
5937 return cp_parser_make_indirect_declarator
5938 (code
, type
, cv_quals
, declarator
);
5941 /* If the next token is a `[', there is a direct-new-declarator. */
5942 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5943 return cp_parser_direct_new_declarator (parser
);
5948 /* Parse a direct-new-declarator.
5950 direct-new-declarator:
5952 direct-new-declarator [constant-expression]
5956 static cp_declarator
*
5957 cp_parser_direct_new_declarator (cp_parser
* parser
)
5959 cp_declarator
*declarator
= NULL
;
5965 /* Look for the opening `['. */
5966 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "%<[%>");
5967 /* The first expression is not required to be constant. */
5970 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
5971 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
5972 /* The standard requires that the expression have integral
5973 type. DR 74 adds enumeration types. We believe that the
5974 real intent is that these expressions be handled like the
5975 expression in a `switch' condition, which also allows
5976 classes with a single conversion to integral or
5977 enumeration type. */
5978 if (!processing_template_decl
)
5981 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5986 error_at (token
->location
,
5987 "expression in new-declarator must have integral "
5988 "or enumeration type");
5989 expression
= error_mark_node
;
5993 /* But all the other expressions must be. */
5996 = cp_parser_constant_expression (parser
,
5997 /*allow_non_constant=*/false,
5999 /* Look for the closing `]'. */
6000 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "%<]%>");
6002 /* Add this bound to the declarator. */
6003 declarator
= make_array_declarator (declarator
, expression
);
6005 /* If the next token is not a `[', then there are no more
6007 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
6014 /* Parse a new-initializer.
6017 ( expression-list [opt] )
6020 Returns a representation of the expression-list. */
6022 static VEC(tree
,gc
) *
6023 cp_parser_new_initializer (cp_parser
* parser
)
6025 VEC(tree
,gc
) *expression_list
;
6027 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6030 bool expr_non_constant_p
;
6031 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6032 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6033 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
6034 expression_list
= make_tree_vector_single (t
);
6037 expression_list
= (cp_parser_parenthesized_expression_list
6038 (parser
, non_attr
, /*cast_p=*/false,
6039 /*allow_expansion_p=*/true,
6040 /*non_constant_p=*/NULL
));
6042 return expression_list
;
6045 /* Parse a delete-expression.
6048 :: [opt] delete cast-expression
6049 :: [opt] delete [ ] cast-expression
6051 Returns a representation of the expression. */
6054 cp_parser_delete_expression (cp_parser
* parser
)
6056 bool global_scope_p
;
6060 /* Look for the optional `::' operator. */
6062 = (cp_parser_global_scope_opt (parser
,
6063 /*current_scope_valid_p=*/false)
6065 /* Look for the `delete' keyword. */
6066 cp_parser_require_keyword (parser
, RID_DELETE
, "%<delete%>");
6067 /* See if the array syntax is in use. */
6068 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6070 /* Consume the `[' token. */
6071 cp_lexer_consume_token (parser
->lexer
);
6072 /* Look for the `]' token. */
6073 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "%<]%>");
6074 /* Remember that this is the `[]' construct. */
6080 /* Parse the cast-expression. */
6081 expression
= cp_parser_simple_cast_expression (parser
);
6083 /* A delete-expression may not appear in an integral constant
6085 if (cp_parser_non_integral_constant_expression (parser
, "%<delete%>"))
6086 return error_mark_node
;
6088 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
6091 /* Returns true if TOKEN may start a cast-expression and false
6095 cp_parser_token_starts_cast_expression (cp_token
*token
)
6097 switch (token
->type
)
6103 case CPP_CLOSE_SQUARE
:
6104 case CPP_CLOSE_PAREN
:
6105 case CPP_CLOSE_BRACE
:
6109 case CPP_DEREF_STAR
:
6117 case CPP_GREATER_EQ
:
6137 /* '[' may start a primary-expression in obj-c++. */
6138 case CPP_OPEN_SQUARE
:
6139 return c_dialect_objc ();
6146 /* Parse a cast-expression.
6150 ( type-id ) cast-expression
6152 ADDRESS_P is true iff the unary-expression is appearing as the
6153 operand of the `&' operator. CAST_P is true if this expression is
6154 the target of a cast.
6156 Returns a representation of the expression. */
6159 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6162 /* If it's a `(', then we might be looking at a cast. */
6163 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6165 tree type
= NULL_TREE
;
6166 tree expr
= NULL_TREE
;
6167 bool compound_literal_p
;
6168 const char *saved_message
;
6170 /* There's no way to know yet whether or not this is a cast.
6171 For example, `(int (3))' is a unary-expression, while `(int)
6172 3' is a cast. So, we resort to parsing tentatively. */
6173 cp_parser_parse_tentatively (parser
);
6174 /* Types may not be defined in a cast. */
6175 saved_message
= parser
->type_definition_forbidden_message
;
6176 parser
->type_definition_forbidden_message
6177 = G_("types may not be defined in casts");
6178 /* Consume the `('. */
6179 cp_lexer_consume_token (parser
->lexer
);
6180 /* A very tricky bit is that `(struct S) { 3 }' is a
6181 compound-literal (which we permit in C++ as an extension).
6182 But, that construct is not a cast-expression -- it is a
6183 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6184 is legal; if the compound-literal were a cast-expression,
6185 you'd need an extra set of parentheses.) But, if we parse
6186 the type-id, and it happens to be a class-specifier, then we
6187 will commit to the parse at that point, because we cannot
6188 undo the action that is done when creating a new class. So,
6189 then we cannot back up and do a postfix-expression.
6191 Therefore, we scan ahead to the closing `)', and check to see
6192 if the token after the `)' is a `{'. If so, we are not
6193 looking at a cast-expression.
6195 Save tokens so that we can put them back. */
6196 cp_lexer_save_tokens (parser
->lexer
);
6197 /* Skip tokens until the next token is a closing parenthesis.
6198 If we find the closing `)', and the next token is a `{', then
6199 we are looking at a compound-literal. */
6201 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6202 /*consume_paren=*/true)
6203 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6204 /* Roll back the tokens we skipped. */
6205 cp_lexer_rollback_tokens (parser
->lexer
);
6206 /* If we were looking at a compound-literal, simulate an error
6207 so that the call to cp_parser_parse_definitely below will
6209 if (compound_literal_p
)
6210 cp_parser_simulate_error (parser
);
6213 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6214 parser
->in_type_id_in_expr_p
= true;
6215 /* Look for the type-id. */
6216 type
= cp_parser_type_id (parser
);
6217 /* Look for the closing `)'. */
6218 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
6219 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6222 /* Restore the saved message. */
6223 parser
->type_definition_forbidden_message
= saved_message
;
6225 /* At this point this can only be either a cast or a
6226 parenthesized ctor such as `(T ())' that looks like a cast to
6227 function returning T. */
6228 if (!cp_parser_error_occurred (parser
)
6229 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6232 cp_parser_parse_definitely (parser
);
6233 expr
= cp_parser_cast_expression (parser
,
6234 /*address_p=*/false,
6235 /*cast_p=*/true, pidk
);
6237 /* Warn about old-style casts, if so requested. */
6238 if (warn_old_style_cast
6239 && !in_system_header
6240 && !VOID_TYPE_P (type
)
6241 && current_lang_name
!= lang_name_c
)
6242 warning (OPT_Wold_style_cast
, "use of old-style cast");
6244 /* Only type conversions to integral or enumeration types
6245 can be used in constant-expressions. */
6246 if (!cast_valid_in_integral_constant_expression_p (type
)
6247 && (cp_parser_non_integral_constant_expression
6249 "a cast to a type other than an integral or "
6250 "enumeration type")))
6251 return error_mark_node
;
6253 /* Perform the cast. */
6254 expr
= build_c_cast (input_location
, type
, expr
);
6258 cp_parser_abort_tentative_parse (parser
);
6261 /* If we get here, then it's not a cast, so it must be a
6262 unary-expression. */
6263 return cp_parser_unary_expression (parser
, address_p
, cast_p
, pidk
);
6266 /* Parse a binary expression of the general form:
6270 pm-expression .* cast-expression
6271 pm-expression ->* cast-expression
6273 multiplicative-expression:
6275 multiplicative-expression * pm-expression
6276 multiplicative-expression / pm-expression
6277 multiplicative-expression % pm-expression
6279 additive-expression:
6280 multiplicative-expression
6281 additive-expression + multiplicative-expression
6282 additive-expression - multiplicative-expression
6286 shift-expression << additive-expression
6287 shift-expression >> additive-expression
6289 relational-expression:
6291 relational-expression < shift-expression
6292 relational-expression > shift-expression
6293 relational-expression <= shift-expression
6294 relational-expression >= shift-expression
6298 relational-expression:
6299 relational-expression <? shift-expression
6300 relational-expression >? shift-expression
6302 equality-expression:
6303 relational-expression
6304 equality-expression == relational-expression
6305 equality-expression != relational-expression
6309 and-expression & equality-expression
6311 exclusive-or-expression:
6313 exclusive-or-expression ^ and-expression
6315 inclusive-or-expression:
6316 exclusive-or-expression
6317 inclusive-or-expression | exclusive-or-expression
6319 logical-and-expression:
6320 inclusive-or-expression
6321 logical-and-expression && inclusive-or-expression
6323 logical-or-expression:
6324 logical-and-expression
6325 logical-or-expression || logical-and-expression
6327 All these are implemented with a single function like:
6330 simple-cast-expression
6331 binary-expression <token> binary-expression
6333 CAST_P is true if this expression is the target of a cast.
6335 The binops_by_token map is used to get the tree codes for each <token> type.
6336 binary-expressions are associated according to a precedence table. */
6338 #define TOKEN_PRECEDENCE(token) \
6339 (((token->type == CPP_GREATER \
6340 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6341 && !parser->greater_than_is_operator_p) \
6342 ? PREC_NOT_OPERATOR \
6343 : binops_by_token[token->type].prec)
6346 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
6347 bool no_toplevel_fold_p
,
6348 enum cp_parser_prec prec
,
6351 cp_parser_expression_stack stack
;
6352 cp_parser_expression_stack_entry
*sp
= &stack
[0];
6355 enum tree_code tree_type
, lhs_type
, rhs_type
;
6356 enum cp_parser_prec new_prec
, lookahead_prec
;
6359 /* Parse the first expression. */
6360 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
, pidk
);
6361 lhs_type
= ERROR_MARK
;
6365 /* Get an operator token. */
6366 token
= cp_lexer_peek_token (parser
->lexer
);
6368 if (warn_cxx0x_compat
6369 && token
->type
== CPP_RSHIFT
6370 && !parser
->greater_than_is_operator_p
)
6372 if (warning_at (token
->location
, OPT_Wc__0x_compat
,
6373 "%<>>%> operator will be treated as"
6374 " two right angle brackets in C++0x"))
6375 inform (token
->location
,
6376 "suggest parentheses around %<>>%> expression");
6379 new_prec
= TOKEN_PRECEDENCE (token
);
6381 /* Popping an entry off the stack means we completed a subexpression:
6382 - either we found a token which is not an operator (`>' where it is not
6383 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6384 will happen repeatedly;
6385 - or, we found an operator which has lower priority. This is the case
6386 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6388 if (new_prec
<= prec
)
6397 tree_type
= binops_by_token
[token
->type
].tree_type
;
6399 /* We used the operator token. */
6400 cp_lexer_consume_token (parser
->lexer
);
6402 /* For "false && x" or "true || x", x will never be executed;
6403 disable warnings while evaluating it. */
6404 if (tree_type
== TRUTH_ANDIF_EXPR
)
6405 c_inhibit_evaluation_warnings
+= lhs
== truthvalue_false_node
;
6406 else if (tree_type
== TRUTH_ORIF_EXPR
)
6407 c_inhibit_evaluation_warnings
+= lhs
== truthvalue_true_node
;
6409 /* Extract another operand. It may be the RHS of this expression
6410 or the LHS of a new, higher priority expression. */
6411 rhs
= cp_parser_simple_cast_expression (parser
);
6412 rhs_type
= ERROR_MARK
;
6414 /* Get another operator token. Look up its precedence to avoid
6415 building a useless (immediately popped) stack entry for common
6416 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6417 token
= cp_lexer_peek_token (parser
->lexer
);
6418 lookahead_prec
= TOKEN_PRECEDENCE (token
);
6419 if (lookahead_prec
> new_prec
)
6421 /* ... and prepare to parse the RHS of the new, higher priority
6422 expression. Since precedence levels on the stack are
6423 monotonically increasing, we do not have to care about
6426 sp
->tree_type
= tree_type
;
6428 sp
->lhs_type
= lhs_type
;
6431 lhs_type
= rhs_type
;
6433 new_prec
= lookahead_prec
;
6437 lookahead_prec
= new_prec
;
6438 /* If the stack is not empty, we have parsed into LHS the right side
6439 (`4' in the example above) of an expression we had suspended.
6440 We can use the information on the stack to recover the LHS (`3')
6441 from the stack together with the tree code (`MULT_EXPR'), and
6442 the precedence of the higher level subexpression
6443 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6444 which will be used to actually build the additive expression. */
6447 tree_type
= sp
->tree_type
;
6449 rhs_type
= lhs_type
;
6451 lhs_type
= sp
->lhs_type
;
6454 /* Undo the disabling of warnings done above. */
6455 if (tree_type
== TRUTH_ANDIF_EXPR
)
6456 c_inhibit_evaluation_warnings
-= lhs
== truthvalue_false_node
;
6457 else if (tree_type
== TRUTH_ORIF_EXPR
)
6458 c_inhibit_evaluation_warnings
-= lhs
== truthvalue_true_node
;
6460 overloaded_p
= false;
6461 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6462 ERROR_MARK for everything that is not a binary expression.
6463 This makes warn_about_parentheses miss some warnings that
6464 involve unary operators. For unary expressions we should
6465 pass the correct tree_code unless the unary expression was
6466 surrounded by parentheses.
6468 if (no_toplevel_fold_p
6469 && lookahead_prec
<= prec
6471 && TREE_CODE_CLASS (tree_type
) == tcc_comparison
)
6472 lhs
= build2 (tree_type
, boolean_type_node
, lhs
, rhs
);
6474 lhs
= build_x_binary_op (tree_type
, lhs
, lhs_type
, rhs
, rhs_type
,
6475 &overloaded_p
, tf_warning_or_error
);
6476 lhs_type
= tree_type
;
6478 /* If the binary operator required the use of an overloaded operator,
6479 then this expression cannot be an integral constant-expression.
6480 An overloaded operator can be used even if both operands are
6481 otherwise permissible in an integral constant-expression if at
6482 least one of the operands is of enumeration type. */
6485 && (cp_parser_non_integral_constant_expression
6486 (parser
, "calls to overloaded operators")))
6487 return error_mark_node
;
6494 /* Parse the `? expression : assignment-expression' part of a
6495 conditional-expression. The LOGICAL_OR_EXPR is the
6496 logical-or-expression that started the conditional-expression.
6497 Returns a representation of the entire conditional-expression.
6499 This routine is used by cp_parser_assignment_expression.
6501 ? expression : assignment-expression
6505 ? : assignment-expression */
6508 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
6511 tree assignment_expr
;
6513 /* Consume the `?' token. */
6514 cp_lexer_consume_token (parser
->lexer
);
6515 if (cp_parser_allow_gnu_extensions_p (parser
)
6516 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
6518 /* Implicit true clause. */
6520 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
6524 /* Parse the expression. */
6525 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
6526 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
6527 c_inhibit_evaluation_warnings
+=
6528 ((logical_or_expr
== truthvalue_true_node
)
6529 - (logical_or_expr
== truthvalue_false_node
));
6532 /* The next token should be a `:'. */
6533 cp_parser_require (parser
, CPP_COLON
, "%<:%>");
6534 /* Parse the assignment-expression. */
6535 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
6536 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
6538 /* Build the conditional-expression. */
6539 return build_x_conditional_expr (logical_or_expr
,
6542 tf_warning_or_error
);
6545 /* Parse an assignment-expression.
6547 assignment-expression:
6548 conditional-expression
6549 logical-or-expression assignment-operator assignment_expression
6552 CAST_P is true if this expression is the target of a cast.
6554 Returns a representation for the expression. */
6557 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
6562 /* If the next token is the `throw' keyword, then we're looking at
6563 a throw-expression. */
6564 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
6565 expr
= cp_parser_throw_expression (parser
);
6566 /* Otherwise, it must be that we are looking at a
6567 logical-or-expression. */
6570 /* Parse the binary expressions (logical-or-expression). */
6571 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
6572 PREC_NOT_OPERATOR
, pidk
);
6573 /* If the next token is a `?' then we're actually looking at a
6574 conditional-expression. */
6575 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
6576 return cp_parser_question_colon_clause (parser
, expr
);
6579 enum tree_code assignment_operator
;
6581 /* If it's an assignment-operator, we're using the second
6584 = cp_parser_assignment_operator_opt (parser
);
6585 if (assignment_operator
!= ERROR_MARK
)
6587 bool non_constant_p
;
6589 /* Parse the right-hand side of the assignment. */
6590 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
6592 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
6593 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6595 /* An assignment may not appear in a
6596 constant-expression. */
6597 if (cp_parser_non_integral_constant_expression (parser
,
6599 return error_mark_node
;
6600 /* Build the assignment expression. */
6601 expr
= build_x_modify_expr (expr
,
6602 assignment_operator
,
6604 tf_warning_or_error
);
6612 /* Parse an (optional) assignment-operator.
6614 assignment-operator: one of
6615 = *= /= %= += -= >>= <<= &= ^= |=
6619 assignment-operator: one of
6622 If the next token is an assignment operator, the corresponding tree
6623 code is returned, and the token is consumed. For example, for
6624 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6625 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6626 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6627 operator, ERROR_MARK is returned. */
6629 static enum tree_code
6630 cp_parser_assignment_operator_opt (cp_parser
* parser
)
6635 /* Peek at the next token. */
6636 token
= cp_lexer_peek_token (parser
->lexer
);
6638 switch (token
->type
)
6649 op
= TRUNC_DIV_EXPR
;
6653 op
= TRUNC_MOD_EXPR
;
6685 /* Nothing else is an assignment operator. */
6689 /* If it was an assignment operator, consume it. */
6690 if (op
!= ERROR_MARK
)
6691 cp_lexer_consume_token (parser
->lexer
);
6696 /* Parse an expression.
6699 assignment-expression
6700 expression , assignment-expression
6702 CAST_P is true if this expression is the target of a cast.
6704 Returns a representation of the expression. */
6707 cp_parser_expression (cp_parser
* parser
, bool cast_p
, cp_id_kind
* pidk
)
6709 tree expression
= NULL_TREE
;
6713 tree assignment_expression
;
6715 /* Parse the next assignment-expression. */
6716 assignment_expression
6717 = cp_parser_assignment_expression (parser
, cast_p
, pidk
);
6718 /* If this is the first assignment-expression, we can just
6721 expression
= assignment_expression
;
6723 expression
= build_x_compound_expr (expression
,
6724 assignment_expression
,
6725 tf_warning_or_error
);
6726 /* If the next token is not a comma, then we are done with the
6728 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6730 /* Consume the `,'. */
6731 cp_lexer_consume_token (parser
->lexer
);
6732 /* A comma operator cannot appear in a constant-expression. */
6733 if (cp_parser_non_integral_constant_expression (parser
,
6734 "a comma operator"))
6735 expression
= error_mark_node
;
6741 /* Parse a constant-expression.
6743 constant-expression:
6744 conditional-expression
6746 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6747 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6748 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6749 is false, NON_CONSTANT_P should be NULL. */
6752 cp_parser_constant_expression (cp_parser
* parser
,
6753 bool allow_non_constant_p
,
6754 bool *non_constant_p
)
6756 bool saved_integral_constant_expression_p
;
6757 bool saved_allow_non_integral_constant_expression_p
;
6758 bool saved_non_integral_constant_expression_p
;
6761 /* It might seem that we could simply parse the
6762 conditional-expression, and then check to see if it were
6763 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6764 one that the compiler can figure out is constant, possibly after
6765 doing some simplifications or optimizations. The standard has a
6766 precise definition of constant-expression, and we must honor
6767 that, even though it is somewhat more restrictive.
6773 is not a legal declaration, because `(2, 3)' is not a
6774 constant-expression. The `,' operator is forbidden in a
6775 constant-expression. However, GCC's constant-folding machinery
6776 will fold this operation to an INTEGER_CST for `3'. */
6778 /* Save the old settings. */
6779 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
6780 saved_allow_non_integral_constant_expression_p
6781 = parser
->allow_non_integral_constant_expression_p
;
6782 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
6783 /* We are now parsing a constant-expression. */
6784 parser
->integral_constant_expression_p
= true;
6785 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
6786 parser
->non_integral_constant_expression_p
= false;
6787 /* Although the grammar says "conditional-expression", we parse an
6788 "assignment-expression", which also permits "throw-expression"
6789 and the use of assignment operators. In the case that
6790 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6791 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6792 actually essential that we look for an assignment-expression.
6793 For example, cp_parser_initializer_clauses uses this function to
6794 determine whether a particular assignment-expression is in fact
6796 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
6797 /* Restore the old settings. */
6798 parser
->integral_constant_expression_p
6799 = saved_integral_constant_expression_p
;
6800 parser
->allow_non_integral_constant_expression_p
6801 = saved_allow_non_integral_constant_expression_p
;
6802 if (allow_non_constant_p
)
6803 *non_constant_p
= parser
->non_integral_constant_expression_p
;
6804 else if (parser
->non_integral_constant_expression_p
)
6805 expression
= error_mark_node
;
6806 parser
->non_integral_constant_expression_p
6807 = saved_non_integral_constant_expression_p
;
6812 /* Parse __builtin_offsetof.
6814 offsetof-expression:
6815 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6817 offsetof-member-designator:
6819 | offsetof-member-designator "." id-expression
6820 | offsetof-member-designator "[" expression "]"
6821 | offsetof-member-designator "->" id-expression */
6824 cp_parser_builtin_offsetof (cp_parser
*parser
)
6826 int save_ice_p
, save_non_ice_p
;
6831 /* We're about to accept non-integral-constant things, but will
6832 definitely yield an integral constant expression. Save and
6833 restore these values around our local parsing. */
6834 save_ice_p
= parser
->integral_constant_expression_p
;
6835 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
6837 /* Consume the "__builtin_offsetof" token. */
6838 cp_lexer_consume_token (parser
->lexer
);
6839 /* Consume the opening `('. */
6840 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
6841 /* Parse the type-id. */
6842 type
= cp_parser_type_id (parser
);
6843 /* Look for the `,'. */
6844 cp_parser_require (parser
, CPP_COMMA
, "%<,%>");
6845 token
= cp_lexer_peek_token (parser
->lexer
);
6847 /* Build the (type *)null that begins the traditional offsetof macro. */
6848 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
6849 tf_warning_or_error
);
6851 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6852 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
6853 true, &dummy
, token
->location
);
6856 token
= cp_lexer_peek_token (parser
->lexer
);
6857 switch (token
->type
)
6859 case CPP_OPEN_SQUARE
:
6860 /* offsetof-member-designator "[" expression "]" */
6861 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
6865 /* offsetof-member-designator "->" identifier */
6866 expr
= grok_array_decl (expr
, integer_zero_node
);
6870 /* offsetof-member-designator "." identifier */
6871 cp_lexer_consume_token (parser
->lexer
);
6872 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
6877 case CPP_CLOSE_PAREN
:
6878 /* Consume the ")" token. */
6879 cp_lexer_consume_token (parser
->lexer
);
6883 /* Error. We know the following require will fail, but
6884 that gives the proper error message. */
6885 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
6886 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
6887 expr
= error_mark_node
;
6893 /* If we're processing a template, we can't finish the semantics yet.
6894 Otherwise we can fold the entire expression now. */
6895 if (processing_template_decl
)
6896 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
6898 expr
= finish_offsetof (expr
);
6901 parser
->integral_constant_expression_p
= save_ice_p
;
6902 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
6907 /* Parse a trait expression. */
6910 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
6913 tree type1
, type2
= NULL_TREE
;
6914 bool binary
= false;
6915 cp_decl_specifier_seq decl_specs
;
6919 case RID_HAS_NOTHROW_ASSIGN
:
6920 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
6922 case RID_HAS_NOTHROW_CONSTRUCTOR
:
6923 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
6925 case RID_HAS_NOTHROW_COPY
:
6926 kind
= CPTK_HAS_NOTHROW_COPY
;
6928 case RID_HAS_TRIVIAL_ASSIGN
:
6929 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
6931 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
6932 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
6934 case RID_HAS_TRIVIAL_COPY
:
6935 kind
= CPTK_HAS_TRIVIAL_COPY
;
6937 case RID_HAS_TRIVIAL_DESTRUCTOR
:
6938 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
6940 case RID_HAS_VIRTUAL_DESTRUCTOR
:
6941 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
6943 case RID_IS_ABSTRACT
:
6944 kind
= CPTK_IS_ABSTRACT
;
6946 case RID_IS_BASE_OF
:
6947 kind
= CPTK_IS_BASE_OF
;
6951 kind
= CPTK_IS_CLASS
;
6953 case RID_IS_CONVERTIBLE_TO
:
6954 kind
= CPTK_IS_CONVERTIBLE_TO
;
6958 kind
= CPTK_IS_EMPTY
;
6961 kind
= CPTK_IS_ENUM
;
6966 case RID_IS_POLYMORPHIC
:
6967 kind
= CPTK_IS_POLYMORPHIC
;
6969 case RID_IS_STD_LAYOUT
:
6970 kind
= CPTK_IS_STD_LAYOUT
;
6972 case RID_IS_TRIVIAL
:
6973 kind
= CPTK_IS_TRIVIAL
;
6976 kind
= CPTK_IS_UNION
;
6982 /* Consume the token. */
6983 cp_lexer_consume_token (parser
->lexer
);
6985 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
6987 type1
= cp_parser_type_id (parser
);
6989 if (type1
== error_mark_node
)
6990 return error_mark_node
;
6992 /* Build a trivial decl-specifier-seq. */
6993 clear_decl_specs (&decl_specs
);
6994 decl_specs
.type
= type1
;
6996 /* Call grokdeclarator to figure out what type this is. */
6997 type1
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
6998 /*initialized=*/0, /*attrlist=*/NULL
);
7002 cp_parser_require (parser
, CPP_COMMA
, "%<,%>");
7004 type2
= cp_parser_type_id (parser
);
7006 if (type2
== error_mark_node
)
7007 return error_mark_node
;
7009 /* Build a trivial decl-specifier-seq. */
7010 clear_decl_specs (&decl_specs
);
7011 decl_specs
.type
= type2
;
7013 /* Call grokdeclarator to figure out what type this is. */
7014 type2
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
7015 /*initialized=*/0, /*attrlist=*/NULL
);
7018 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
7020 /* Complete the trait expression, which may mean either processing
7021 the trait expr now or saving it for template instantiation. */
7022 return finish_trait_expr (kind
, type1
, type2
);
7025 /* Lambdas that appear in variable initializer or default argument scope
7026 get that in their mangling, so we need to record it. We might as well
7027 use the count for function and namespace scopes as well. */
7028 static GTY(()) tree lambda_scope
;
7029 static GTY(()) int lambda_count
;
7030 typedef struct GTY(()) tree_int
7035 DEF_VEC_O(tree_int
);
7036 DEF_VEC_ALLOC_O(tree_int
,gc
);
7037 static GTY(()) VEC(tree_int
,gc
) *lambda_scope_stack
;
7040 start_lambda_scope (tree decl
)
7044 /* Once we're inside a function, we ignore other scopes and just push
7045 the function again so that popping works properly. */
7046 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
7047 decl
= current_function_decl
;
7048 ti
.t
= lambda_scope
;
7049 ti
.i
= lambda_count
;
7050 VEC_safe_push (tree_int
, gc
, lambda_scope_stack
, &ti
);
7051 if (lambda_scope
!= decl
)
7053 /* Don't reset the count if we're still in the same function. */
7054 lambda_scope
= decl
;
7060 record_lambda_scope (tree lambda
)
7062 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
7063 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
7067 finish_lambda_scope (void)
7069 tree_int
*p
= VEC_last (tree_int
, lambda_scope_stack
);
7070 if (lambda_scope
!= p
->t
)
7072 lambda_scope
= p
->t
;
7073 lambda_count
= p
->i
;
7075 VEC_pop (tree_int
, lambda_scope_stack
);
7078 /* Parse a lambda expression.
7081 lambda-introducer lambda-declarator [opt] compound-statement
7083 Returns a representation of the expression. */
7086 cp_parser_lambda_expression (cp_parser
* parser
)
7088 tree lambda_expr
= build_lambda_expr ();
7091 LAMBDA_EXPR_LOCATION (lambda_expr
)
7092 = cp_lexer_peek_token (parser
->lexer
)->location
;
7094 if (cp_unevaluated_operand
)
7095 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
7096 "lambda-expression in unevaluated context");
7098 /* We may be in the middle of deferred access check. Disable
7100 push_deferring_access_checks (dk_no_deferred
);
7102 cp_parser_lambda_introducer (parser
, lambda_expr
);
7104 type
= begin_lambda_type (lambda_expr
);
7106 record_lambda_scope (lambda_expr
);
7108 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7109 determine_visibility (TYPE_NAME (type
));
7111 /* Now that we've started the type, add the capture fields for any
7112 explicit captures. */
7113 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
7116 /* Inside the class, surrounding template-parameter-lists do not apply. */
7117 unsigned int saved_num_template_parameter_lists
7118 = parser
->num_template_parameter_lists
;
7120 parser
->num_template_parameter_lists
= 0;
7122 /* By virtue of defining a local class, a lambda expression has access to
7123 the private variables of enclosing classes. */
7125 cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
7127 cp_parser_lambda_body (parser
, lambda_expr
);
7129 /* The capture list was built up in reverse order; fix that now. */
7131 tree newlist
= NULL_TREE
;
7134 for (elt
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
);
7137 tree field
= TREE_PURPOSE (elt
);
7140 next
= TREE_CHAIN (elt
);
7141 TREE_CHAIN (elt
) = newlist
;
7144 /* Also add __ to the beginning of the field name so that code
7145 outside the lambda body can't see the captured name. We could
7146 just remove the name entirely, but this is more useful for
7148 if (field
== LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
7149 /* The 'this' capture already starts with __. */
7152 buf
= (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field
)) + 3);
7153 buf
[1] = buf
[0] = '_';
7154 memcpy (buf
+ 2, IDENTIFIER_POINTER (DECL_NAME (field
)),
7155 IDENTIFIER_LENGTH (DECL_NAME (field
)) + 1);
7156 DECL_NAME (field
) = get_identifier (buf
);
7158 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
) = newlist
;
7161 maybe_add_lambda_conv_op (type
);
7163 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
7165 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
7168 pop_deferring_access_checks ();
7170 return build_lambda_object (lambda_expr
);
7173 /* Parse the beginning of a lambda expression.
7176 [ lambda-capture [opt] ]
7178 LAMBDA_EXPR is the current representation of the lambda expression. */
7181 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
7183 /* Need commas after the first capture. */
7186 /* Eat the leading `['. */
7187 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "%<[%>");
7189 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7190 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
7191 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
7192 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
7193 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7194 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
7196 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
7198 cp_lexer_consume_token (parser
->lexer
);
7202 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
7204 cp_token
* capture_token
;
7206 tree capture_init_expr
;
7207 cp_id_kind idk
= CP_ID_KIND_NONE
;
7208 bool explicit_init_p
= false;
7210 enum capture_kind_type
7215 enum capture_kind_type capture_kind
= BY_COPY
;
7217 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
7219 error ("expected end of capture-list");
7226 cp_parser_require (parser
, CPP_COMMA
, "%<,%>");
7228 /* Possibly capture `this'. */
7229 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
7231 cp_lexer_consume_token (parser
->lexer
);
7232 add_capture (lambda_expr
,
7233 /*id=*/get_identifier ("__this"),
7234 /*initializer=*/finish_this_expr(),
7235 /*by_reference_p=*/false,
7240 /* Remember whether we want to capture as a reference or not. */
7241 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
7243 capture_kind
= BY_REFERENCE
;
7244 cp_lexer_consume_token (parser
->lexer
);
7247 /* Get the identifier. */
7248 capture_token
= cp_lexer_peek_token (parser
->lexer
);
7249 capture_id
= cp_parser_identifier (parser
);
7251 if (capture_id
== error_mark_node
)
7252 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7253 delimiters, but I modified this to stop on unnested ']' as well. It
7254 was already changed to stop on unnested '}', so the
7255 "closing_parenthesis" name is no more misleading with my change. */
7257 cp_parser_skip_to_closing_parenthesis (parser
,
7258 /*recovering=*/true,
7260 /*consume_paren=*/true);
7264 /* Find the initializer for this capture. */
7265 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7267 /* An explicit expression exists. */
7268 cp_lexer_consume_token (parser
->lexer
);
7269 pedwarn (input_location
, OPT_pedantic
,
7270 "ISO C++ does not allow initializers "
7271 "in lambda expression capture lists");
7272 capture_init_expr
= cp_parser_assignment_expression (parser
,
7275 explicit_init_p
= true;
7279 const char* error_msg
;
7281 /* Turn the identifier into an id-expression. */
7283 = cp_parser_lookup_name
7287 /*is_template=*/false,
7288 /*is_namespace=*/false,
7289 /*check_dependency=*/true,
7290 /*ambiguous_decls=*/NULL
,
7291 capture_token
->location
);
7294 = finish_id_expression
7299 /*integral_constant_expression_p=*/false,
7300 /*allow_non_integral_constant_expression_p=*/false,
7301 /*non_integral_constant_expression_p=*/NULL
,
7302 /*template_p=*/false,
7304 /*address_p=*/false,
7305 /*template_arg_p=*/false,
7307 capture_token
->location
);
7310 if (TREE_CODE (capture_init_expr
) == IDENTIFIER_NODE
)
7312 = unqualified_name_lookup_error (capture_init_expr
);
7314 add_capture (lambda_expr
,
7317 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
7321 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "%<]%>");
7324 /* Parse the (optional) middle of a lambda expression.
7327 ( parameter-declaration-clause [opt] )
7328 attribute-specifier [opt]
7330 exception-specification [opt]
7331 lambda-return-type-clause [opt]
7333 LAMBDA_EXPR is the current representation of the lambda expression. */
7336 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
7338 /* 5.1.1.4 of the standard says:
7339 If a lambda-expression does not include a lambda-declarator, it is as if
7340 the lambda-declarator were ().
7341 This means an empty parameter list, no attributes, and no exception
7343 tree param_list
= void_list_node
;
7344 tree attributes
= NULL_TREE
;
7345 tree exception_spec
= NULL_TREE
;
7348 /* The lambda-declarator is optional, but must begin with an opening
7349 parenthesis if present. */
7350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7352 cp_lexer_consume_token (parser
->lexer
);
7354 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
7356 /* Parse parameters. */
7357 param_list
= cp_parser_parameter_declaration_clause (parser
);
7359 /* Default arguments shall not be specified in the
7360 parameter-declaration-clause of a lambda-declarator. */
7361 for (t
= param_list
; t
; t
= TREE_CHAIN (t
))
7362 if (TREE_PURPOSE (t
))
7363 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_pedantic
,
7364 "default argument specified for lambda parameter");
7366 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
7368 attributes
= cp_parser_attributes_opt (parser
);
7370 /* Parse optional `mutable' keyword. */
7371 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
7373 cp_lexer_consume_token (parser
->lexer
);
7374 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
7377 /* Parse optional exception specification. */
7378 exception_spec
= cp_parser_exception_specification_opt (parser
);
7380 /* Parse optional trailing return type. */
7381 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
7383 cp_lexer_consume_token (parser
->lexer
);
7384 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
) = cp_parser_type_id (parser
);
7387 /* The function parameters must be in scope all the way until after the
7388 trailing-return-type in case of decltype. */
7389 for (t
= current_binding_level
->names
; t
; t
= TREE_CHAIN (t
))
7390 pop_binding (DECL_NAME (t
), t
);
7395 /* Create the function call operator.
7397 Messing with declarators like this is no uglier than building up the
7398 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7401 cp_decl_specifier_seq return_type_specs
;
7402 cp_declarator
* declarator
;
7407 clear_decl_specs (&return_type_specs
);
7408 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
7409 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
7411 /* Maybe we will deduce the return type later, but we can use void
7412 as a placeholder return type anyways. */
7413 return_type_specs
.type
= void_type_node
;
7415 p
= obstack_alloc (&declarator_obstack
, 0);
7417 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
7420 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
7421 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
7422 declarator
= make_call_declarator (declarator
, param_list
, quals
,
7424 /*late_return_type=*/NULL_TREE
);
7425 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
7427 fco
= grokmethod (&return_type_specs
,
7430 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
7431 DECL_ARTIFICIAL (fco
) = 1;
7433 finish_member_declaration (fco
);
7435 obstack_free (&declarator_obstack
, p
);
7439 /* Parse the body of a lambda expression, which is simply
7443 but which requires special handling.
7444 LAMBDA_EXPR is the current representation of the lambda expression. */
7447 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
7449 bool nested
= (current_function_decl
!= NULL_TREE
);
7451 push_function_context ();
7453 /* Finish the function call operator
7455 + late_parsing_for_member
7456 + function_definition_after_declarator
7457 + ctor_initializer_opt_and_function_body */
7459 tree fco
= lambda_function (lambda_expr
);
7463 /* Let the front end know that we are going to be defining this
7465 start_preparsed_function (fco
,
7467 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
7469 start_lambda_scope (fco
);
7470 body
= begin_function_body ();
7472 /* 5.1.1.4 of the standard says:
7473 If a lambda-expression does not include a trailing-return-type, it
7474 is as if the trailing-return-type denotes the following type:
7475 * if the compound-statement is of the form
7476 { return attribute-specifier [opt] expression ; }
7477 the type of the returned expression after lvalue-to-rvalue
7478 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7479 (_conv.array_ 4.2), and function-to-pointer conversion
7481 * otherwise, void. */
7483 /* In a lambda that has neither a lambda-return-type-clause
7484 nor a deducible form, errors should be reported for return statements
7485 in the body. Since we used void as the placeholder return type, parsing
7486 the body as usual will give such desired behavior. */
7487 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
7488 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
7489 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
== RID_RETURN
7490 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_SEMICOLON
)
7493 tree expr
= NULL_TREE
;
7494 cp_id_kind idk
= CP_ID_KIND_NONE
;
7496 /* Parse tentatively in case there's more after the initial return
7498 cp_parser_parse_tentatively (parser
);
7500 cp_parser_require (parser
, CPP_OPEN_BRACE
, "%<{%>");
7501 cp_parser_require_keyword (parser
, RID_RETURN
, "%<return%>");
7503 expr
= cp_parser_expression (parser
, /*cast_p=*/false, &idk
);
7505 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7506 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
7508 if (cp_parser_parse_definitely (parser
))
7510 apply_lambda_return_type (lambda_expr
, lambda_return_type (expr
));
7512 compound_stmt
= begin_compound_stmt (0);
7513 /* Will get error here if type not deduced yet. */
7514 finish_return_stmt (expr
);
7515 finish_compound_stmt (compound_stmt
);
7523 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
7524 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr
) = true;
7525 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7526 cp_parser_compound_stmt does not pass it. */
7527 cp_parser_function_body (parser
);
7528 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr
) = false;
7531 finish_function_body (body
);
7532 finish_lambda_scope ();
7534 /* Finish the function and generate code for it if necessary. */
7535 expand_or_defer_fn (finish_function (/*inline*/2));
7539 pop_function_context();
7542 /* Statements [gram.stmt.stmt] */
7544 /* Parse a statement.
7548 expression-statement
7553 declaration-statement
7556 IN_COMPOUND is true when the statement is nested inside a
7557 cp_parser_compound_statement; this matters for certain pragmas.
7559 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7560 is a (possibly labeled) if statement which is not enclosed in braces
7561 and has an else clause. This is used to implement -Wparentheses. */
7564 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
7565 bool in_compound
, bool *if_p
)
7569 location_t statement_location
;
7574 /* There is no statement yet. */
7575 statement
= NULL_TREE
;
7576 /* Peek at the next token. */
7577 token
= cp_lexer_peek_token (parser
->lexer
);
7578 /* Remember the location of the first token in the statement. */
7579 statement_location
= token
->location
;
7580 /* If this is a keyword, then that will often determine what kind of
7581 statement we have. */
7582 if (token
->type
== CPP_KEYWORD
)
7584 enum rid keyword
= token
->keyword
;
7590 /* Looks like a labeled-statement with a case label.
7591 Parse the label, and then use tail recursion to parse
7593 cp_parser_label_for_labeled_statement (parser
);
7598 statement
= cp_parser_selection_statement (parser
, if_p
);
7604 statement
= cp_parser_iteration_statement (parser
);
7611 statement
= cp_parser_jump_statement (parser
);
7614 /* Objective-C++ exception-handling constructs. */
7617 case RID_AT_FINALLY
:
7618 case RID_AT_SYNCHRONIZED
:
7620 statement
= cp_parser_objc_statement (parser
);
7624 statement
= cp_parser_try_block (parser
);
7628 /* This must be a namespace alias definition. */
7629 cp_parser_declaration_statement (parser
);
7633 /* It might be a keyword like `int' that can start a
7634 declaration-statement. */
7638 else if (token
->type
== CPP_NAME
)
7640 /* If the next token is a `:', then we are looking at a
7641 labeled-statement. */
7642 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7643 if (token
->type
== CPP_COLON
)
7645 /* Looks like a labeled-statement with an ordinary label.
7646 Parse the label, and then use tail recursion to parse
7648 cp_parser_label_for_labeled_statement (parser
);
7652 /* Anything that starts with a `{' must be a compound-statement. */
7653 else if (token
->type
== CPP_OPEN_BRACE
)
7654 statement
= cp_parser_compound_statement (parser
, NULL
, false);
7655 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7656 a statement all its own. */
7657 else if (token
->type
== CPP_PRAGMA
)
7659 /* Only certain OpenMP pragmas are attached to statements, and thus
7660 are considered statements themselves. All others are not. In
7661 the context of a compound, accept the pragma as a "statement" and
7662 return so that we can check for a close brace. Otherwise we
7663 require a real statement and must go back and read one. */
7665 cp_parser_pragma (parser
, pragma_compound
);
7666 else if (!cp_parser_pragma (parser
, pragma_stmt
))
7670 else if (token
->type
== CPP_EOF
)
7672 cp_parser_error (parser
, "expected statement");
7676 /* Everything else must be a declaration-statement or an
7677 expression-statement. Try for the declaration-statement
7678 first, unless we are looking at a `;', in which case we know that
7679 we have an expression-statement. */
7682 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7684 cp_parser_parse_tentatively (parser
);
7685 /* Try to parse the declaration-statement. */
7686 cp_parser_declaration_statement (parser
);
7687 /* If that worked, we're done. */
7688 if (cp_parser_parse_definitely (parser
))
7691 /* Look for an expression-statement instead. */
7692 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
7695 /* Set the line number for the statement. */
7696 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
7697 SET_EXPR_LOCATION (statement
, statement_location
);
7700 /* Parse the label for a labeled-statement, i.e.
7703 case constant-expression :
7707 case constant-expression ... constant-expression : statement
7709 When a label is parsed without errors, the label is added to the
7710 parse tree by the finish_* functions, so this function doesn't
7711 have to return the label. */
7714 cp_parser_label_for_labeled_statement (cp_parser
* parser
)
7717 tree label
= NULL_TREE
;
7719 /* The next token should be an identifier. */
7720 token
= cp_lexer_peek_token (parser
->lexer
);
7721 if (token
->type
!= CPP_NAME
7722 && token
->type
!= CPP_KEYWORD
)
7724 cp_parser_error (parser
, "expected labeled-statement");
7728 switch (token
->keyword
)
7735 /* Consume the `case' token. */
7736 cp_lexer_consume_token (parser
->lexer
);
7737 /* Parse the constant-expression. */
7738 expr
= cp_parser_constant_expression (parser
,
7739 /*allow_non_constant_p=*/false,
7742 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
7743 if (ellipsis
->type
== CPP_ELLIPSIS
)
7745 /* Consume the `...' token. */
7746 cp_lexer_consume_token (parser
->lexer
);
7748 cp_parser_constant_expression (parser
,
7749 /*allow_non_constant_p=*/false,
7751 /* We don't need to emit warnings here, as the common code
7752 will do this for us. */
7755 expr_hi
= NULL_TREE
;
7757 if (parser
->in_switch_statement_p
)
7758 finish_case_label (token
->location
, expr
, expr_hi
);
7760 error_at (token
->location
,
7761 "case label %qE not within a switch statement",
7767 /* Consume the `default' token. */
7768 cp_lexer_consume_token (parser
->lexer
);
7770 if (parser
->in_switch_statement_p
)
7771 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
7773 error_at (token
->location
, "case label not within a switch statement");
7777 /* Anything else must be an ordinary label. */
7778 label
= finish_label_stmt (cp_parser_identifier (parser
));
7782 /* Require the `:' token. */
7783 cp_parser_require (parser
, CPP_COLON
, "%<:%>");
7785 /* An ordinary label may optionally be followed by attributes.
7786 However, this is only permitted if the attributes are then
7787 followed by a semicolon. This is because, for backward
7788 compatibility, when parsing
7789 lab: __attribute__ ((unused)) int i;
7790 we want the attribute to attach to "i", not "lab". */
7791 if (label
!= NULL_TREE
7792 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
7796 cp_parser_parse_tentatively (parser
);
7797 attrs
= cp_parser_attributes_opt (parser
);
7798 if (attrs
== NULL_TREE
7799 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7800 cp_parser_abort_tentative_parse (parser
);
7801 else if (!cp_parser_parse_definitely (parser
))
7804 cplus_decl_attributes (&label
, attrs
, 0);
7808 /* Parse an expression-statement.
7810 expression-statement:
7813 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7814 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7815 indicates whether this expression-statement is part of an
7816 expression statement. */
7819 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
7821 tree statement
= NULL_TREE
;
7822 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7824 /* If the next token is a ';', then there is no expression
7826 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7827 statement
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
7829 /* Give a helpful message for "A<T>::type t;" and the like. */
7830 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
7831 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
7833 if (TREE_CODE (statement
) == SCOPE_REF
)
7834 error_at (token
->location
, "need %<typename%> before %qE because "
7835 "%qT is a dependent scope",
7836 statement
, TREE_OPERAND (statement
, 0));
7837 else if (is_overloaded_fn (statement
)
7838 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
7841 tree fn
= get_first_fn (statement
);
7842 error_at (token
->location
,
7843 "%<%T::%D%> names the constructor, not the type",
7844 DECL_CONTEXT (fn
), DECL_NAME (fn
));
7848 /* Consume the final `;'. */
7849 cp_parser_consume_semicolon_at_end_of_statement (parser
);
7851 if (in_statement_expr
7852 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
7853 /* This is the final expression statement of a statement
7855 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
7857 statement
= finish_expr_stmt (statement
);
7864 /* Parse a compound-statement.
7867 { statement-seq [opt] }
7872 { label-declaration-seq [opt] statement-seq [opt] }
7874 label-declaration-seq:
7876 label-declaration-seq label-declaration
7878 Returns a tree representing the statement. */
7881 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
7886 /* Consume the `{'. */
7887 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "%<{%>"))
7888 return error_mark_node
;
7889 /* Begin the compound-statement. */
7890 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
7891 /* If the next keyword is `__label__' we have a label declaration. */
7892 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
7893 cp_parser_label_declaration (parser
);
7894 /* Parse an (optional) statement-seq. */
7895 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
7896 /* Finish the compound-statement. */
7897 finish_compound_stmt (compound_stmt
);
7898 /* Consume the `}'. */
7899 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
7901 return compound_stmt
;
7904 /* Parse an (optional) statement-seq.
7908 statement-seq [opt] statement */
7911 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
7913 /* Scan statements until there aren't any more. */
7916 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7918 /* If we're looking at a `}', then we've run out of statements. */
7919 if (token
->type
== CPP_CLOSE_BRACE
7920 || token
->type
== CPP_EOF
7921 || token
->type
== CPP_PRAGMA_EOL
)
7924 /* If we are in a compound statement and find 'else' then
7925 something went wrong. */
7926 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
7928 if (parser
->in_statement
& IN_IF_STMT
)
7932 token
= cp_lexer_consume_token (parser
->lexer
);
7933 error_at (token
->location
, "%<else%> without a previous %<if%>");
7937 /* Parse the statement. */
7938 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
7942 /* Parse a selection-statement.
7944 selection-statement:
7945 if ( condition ) statement
7946 if ( condition ) statement else statement
7947 switch ( condition ) statement
7949 Returns the new IF_STMT or SWITCH_STMT.
7951 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7952 is a (possibly labeled) if statement which is not enclosed in
7953 braces and has an else clause. This is used to implement
7957 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
7965 /* Peek at the next token. */
7966 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
7968 /* See what kind of keyword it is. */
7969 keyword
= token
->keyword
;
7978 /* Look for the `('. */
7979 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
7981 cp_parser_skip_to_end_of_statement (parser
);
7982 return error_mark_node
;
7985 /* Begin the selection-statement. */
7986 if (keyword
== RID_IF
)
7987 statement
= begin_if_stmt ();
7989 statement
= begin_switch_stmt ();
7991 /* Parse the condition. */
7992 condition
= cp_parser_condition (parser
);
7993 /* Look for the `)'. */
7994 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
7995 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
7996 /*consume_paren=*/true);
7998 if (keyword
== RID_IF
)
8001 unsigned char in_statement
;
8003 /* Add the condition. */
8004 finish_if_stmt_cond (condition
, statement
);
8006 /* Parse the then-clause. */
8007 in_statement
= parser
->in_statement
;
8008 parser
->in_statement
|= IN_IF_STMT
;
8009 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
8011 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8012 add_stmt (build_empty_stmt (loc
));
8013 cp_lexer_consume_token (parser
->lexer
);
8014 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
8015 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
8016 "empty body in an %<if%> statement");
8020 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
8021 parser
->in_statement
= in_statement
;
8023 finish_then_clause (statement
);
8025 /* If the next token is `else', parse the else-clause. */
8026 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
8029 /* Consume the `else' keyword. */
8030 cp_lexer_consume_token (parser
->lexer
);
8031 begin_else_clause (statement
);
8032 /* Parse the else-clause. */
8033 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
8036 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8038 OPT_Wempty_body
, "suggest braces around "
8039 "empty body in an %<else%> statement");
8040 add_stmt (build_empty_stmt (loc
));
8041 cp_lexer_consume_token (parser
->lexer
);
8044 cp_parser_implicitly_scoped_statement (parser
, NULL
);
8046 finish_else_clause (statement
);
8048 /* If we are currently parsing a then-clause, then
8049 IF_P will not be NULL. We set it to true to
8050 indicate that this if statement has an else clause.
8051 This may trigger the Wparentheses warning below
8052 when we get back up to the parent if statement. */
8058 /* This if statement does not have an else clause. If
8059 NESTED_IF is true, then the then-clause is an if
8060 statement which does have an else clause. We warn
8061 about the potential ambiguity. */
8063 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
8064 "suggest explicit braces to avoid ambiguous"
8068 /* Now we're all done with the if-statement. */
8069 finish_if_stmt (statement
);
8073 bool in_switch_statement_p
;
8074 unsigned char in_statement
;
8076 /* Add the condition. */
8077 finish_switch_cond (condition
, statement
);
8079 /* Parse the body of the switch-statement. */
8080 in_switch_statement_p
= parser
->in_switch_statement_p
;
8081 in_statement
= parser
->in_statement
;
8082 parser
->in_switch_statement_p
= true;
8083 parser
->in_statement
|= IN_SWITCH_STMT
;
8084 cp_parser_implicitly_scoped_statement (parser
, NULL
);
8085 parser
->in_switch_statement_p
= in_switch_statement_p
;
8086 parser
->in_statement
= in_statement
;
8088 /* Now we're all done with the switch-statement. */
8089 finish_switch_stmt (statement
);
8097 cp_parser_error (parser
, "expected selection-statement");
8098 return error_mark_node
;
8102 /* Parse a condition.
8106 type-specifier-seq declarator = initializer-clause
8107 type-specifier-seq declarator braced-init-list
8112 type-specifier-seq declarator asm-specification [opt]
8113 attributes [opt] = assignment-expression
8115 Returns the expression that should be tested. */
8118 cp_parser_condition (cp_parser
* parser
)
8120 cp_decl_specifier_seq type_specifiers
;
8121 const char *saved_message
;
8123 /* Try the declaration first. */
8124 cp_parser_parse_tentatively (parser
);
8125 /* New types are not allowed in the type-specifier-seq for a
8127 saved_message
= parser
->type_definition_forbidden_message
;
8128 parser
->type_definition_forbidden_message
8129 = G_("types may not be defined in conditions");
8130 /* Parse the type-specifier-seq. */
8131 cp_parser_type_specifier_seq (parser
, /*is_declaration==*/true,
8132 /*is_trailing_return=*/false,
8134 /* Restore the saved message. */
8135 parser
->type_definition_forbidden_message
= saved_message
;
8136 /* If all is well, we might be looking at a declaration. */
8137 if (!cp_parser_error_occurred (parser
))
8140 tree asm_specification
;
8142 cp_declarator
*declarator
;
8143 tree initializer
= NULL_TREE
;
8145 /* Parse the declarator. */
8146 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
8147 /*ctor_dtor_or_conv_p=*/NULL
,
8148 /*parenthesized_p=*/NULL
,
8149 /*member_p=*/false);
8150 /* Parse the attributes. */
8151 attributes
= cp_parser_attributes_opt (parser
);
8152 /* Parse the asm-specification. */
8153 asm_specification
= cp_parser_asm_specification_opt (parser
);
8154 /* If the next token is not an `=' or '{', then we might still be
8155 looking at an expression. For example:
8159 looks like a decl-specifier-seq and a declarator -- but then
8160 there is no `=', so this is an expression. */
8161 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
8162 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
8163 cp_parser_simulate_error (parser
);
8165 /* If we did see an `=' or '{', then we are looking at a declaration
8167 if (cp_parser_parse_definitely (parser
))
8170 bool non_constant_p
;
8171 bool flags
= LOOKUP_ONLYCONVERTING
;
8173 /* Create the declaration. */
8174 decl
= start_decl (declarator
, &type_specifiers
,
8175 /*initialized_p=*/true,
8176 attributes
, /*prefix_attributes=*/NULL_TREE
,
8179 /* Parse the initializer. */
8180 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8182 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
8183 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
8188 /* Consume the `='. */
8189 cp_parser_require (parser
, CPP_EQ
, "%<=%>");
8190 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
8192 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
8193 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8195 if (!non_constant_p
)
8196 initializer
= fold_non_dependent_expr (initializer
);
8198 /* Process the initializer. */
8199 cp_finish_decl (decl
,
8200 initializer
, !non_constant_p
,
8205 pop_scope (pushed_scope
);
8207 return convert_from_reference (decl
);
8210 /* If we didn't even get past the declarator successfully, we are
8211 definitely not looking at a declaration. */
8213 cp_parser_abort_tentative_parse (parser
);
8215 /* Otherwise, we are looking at an expression. */
8216 return cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8219 /* Parse an iteration-statement.
8221 iteration-statement:
8222 while ( condition ) statement
8223 do statement while ( expression ) ;
8224 for ( for-init-statement condition [opt] ; expression [opt] )
8227 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8230 cp_parser_iteration_statement (cp_parser
* parser
)
8235 unsigned char in_statement
;
8237 /* Peek at the next token. */
8238 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
8240 return error_mark_node
;
8242 /* Remember whether or not we are already within an iteration
8244 in_statement
= parser
->in_statement
;
8246 /* See what kind of keyword it is. */
8247 keyword
= token
->keyword
;
8254 /* Begin the while-statement. */
8255 statement
= begin_while_stmt ();
8256 /* Look for the `('. */
8257 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
8258 /* Parse the condition. */
8259 condition
= cp_parser_condition (parser
);
8260 finish_while_stmt_cond (condition
, statement
);
8261 /* Look for the `)'. */
8262 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
8263 /* Parse the dependent statement. */
8264 parser
->in_statement
= IN_ITERATION_STMT
;
8265 cp_parser_already_scoped_statement (parser
);
8266 parser
->in_statement
= in_statement
;
8267 /* We're done with the while-statement. */
8268 finish_while_stmt (statement
);
8276 /* Begin the do-statement. */
8277 statement
= begin_do_stmt ();
8278 /* Parse the body of the do-statement. */
8279 parser
->in_statement
= IN_ITERATION_STMT
;
8280 cp_parser_implicitly_scoped_statement (parser
, NULL
);
8281 parser
->in_statement
= in_statement
;
8282 finish_do_body (statement
);
8283 /* Look for the `while' keyword. */
8284 cp_parser_require_keyword (parser
, RID_WHILE
, "%<while%>");
8285 /* Look for the `('. */
8286 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
8287 /* Parse the expression. */
8288 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8289 /* We're done with the do-statement. */
8290 finish_do_stmt (expression
, statement
);
8291 /* Look for the `)'. */
8292 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
8293 /* Look for the `;'. */
8294 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
8300 tree condition
= NULL_TREE
;
8301 tree expression
= NULL_TREE
;
8303 /* Begin the for-statement. */
8304 statement
= begin_for_stmt ();
8305 /* Look for the `('. */
8306 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
8307 /* Parse the initialization. */
8308 cp_parser_for_init_statement (parser
);
8309 finish_for_init_stmt (statement
);
8311 /* If there's a condition, process it. */
8312 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8313 condition
= cp_parser_condition (parser
);
8314 finish_for_cond (condition
, statement
);
8315 /* Look for the `;'. */
8316 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
8318 /* If there's an expression, process it. */
8319 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
8320 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8321 finish_for_expr (expression
, statement
);
8322 /* Look for the `)'. */
8323 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
8325 /* Parse the body of the for-statement. */
8326 parser
->in_statement
= IN_ITERATION_STMT
;
8327 cp_parser_already_scoped_statement (parser
);
8328 parser
->in_statement
= in_statement
;
8330 /* We're done with the for-statement. */
8331 finish_for_stmt (statement
);
8336 cp_parser_error (parser
, "expected iteration-statement");
8337 statement
= error_mark_node
;
8344 /* Parse a for-init-statement.
8347 expression-statement
8348 simple-declaration */
8351 cp_parser_for_init_statement (cp_parser
* parser
)
8353 /* If the next token is a `;', then we have an empty
8354 expression-statement. Grammatically, this is also a
8355 simple-declaration, but an invalid one, because it does not
8356 declare anything. Therefore, if we did not handle this case
8357 specially, we would issue an error message about an invalid
8359 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8361 /* We're going to speculatively look for a declaration, falling back
8362 to an expression, if necessary. */
8363 cp_parser_parse_tentatively (parser
);
8364 /* Parse the declaration. */
8365 cp_parser_simple_declaration (parser
,
8366 /*function_definition_allowed_p=*/false);
8367 /* If the tentative parse failed, then we shall need to look for an
8368 expression-statement. */
8369 if (cp_parser_parse_definitely (parser
))
8373 cp_parser_expression_statement (parser
, NULL_TREE
);
8376 /* Parse a jump-statement.
8381 return expression [opt] ;
8382 return braced-init-list ;
8390 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8393 cp_parser_jump_statement (cp_parser
* parser
)
8395 tree statement
= error_mark_node
;
8398 unsigned char in_statement
;
8400 /* Peek at the next token. */
8401 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
8403 return error_mark_node
;
8405 /* See what kind of keyword it is. */
8406 keyword
= token
->keyword
;
8410 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
8411 switch (in_statement
)
8414 error_at (token
->location
, "break statement not within loop or switch");
8417 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
8418 || in_statement
== IN_ITERATION_STMT
);
8419 statement
= finish_break_stmt ();
8422 error_at (token
->location
, "invalid exit from OpenMP structured block");
8425 error_at (token
->location
, "break statement used with OpenMP for loop");
8428 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
8432 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
8435 error_at (token
->location
, "continue statement not within a loop");
8437 case IN_ITERATION_STMT
:
8439 statement
= finish_continue_stmt ();
8442 error_at (token
->location
, "invalid exit from OpenMP structured block");
8447 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
8453 bool expr_non_constant_p
;
8455 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8457 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8458 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8460 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8461 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8463 /* If the next token is a `;', then there is no
8466 /* Build the return-statement. */
8467 statement
= finish_return_stmt (expr
);
8468 /* Look for the final `;'. */
8469 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
8474 /* Create the goto-statement. */
8475 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
8477 /* Issue a warning about this use of a GNU extension. */
8478 pedwarn (token
->location
, OPT_pedantic
, "ISO C++ forbids computed gotos");
8479 /* Consume the '*' token. */
8480 cp_lexer_consume_token (parser
->lexer
);
8481 /* Parse the dependent expression. */
8482 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false, NULL
));
8485 finish_goto_stmt (cp_parser_identifier (parser
));
8486 /* Look for the final `;'. */
8487 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
8491 cp_parser_error (parser
, "expected jump-statement");
8498 /* Parse a declaration-statement.
8500 declaration-statement:
8501 block-declaration */
8504 cp_parser_declaration_statement (cp_parser
* parser
)
8508 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8509 p
= obstack_alloc (&declarator_obstack
, 0);
8511 /* Parse the block-declaration. */
8512 cp_parser_block_declaration (parser
, /*statement_p=*/true);
8514 /* Free any declarators allocated. */
8515 obstack_free (&declarator_obstack
, p
);
8517 /* Finish off the statement. */
8521 /* Some dependent statements (like `if (cond) statement'), are
8522 implicitly in their own scope. In other words, if the statement is
8523 a single statement (as opposed to a compound-statement), it is
8524 none-the-less treated as if it were enclosed in braces. Any
8525 declarations appearing in the dependent statement are out of scope
8526 after control passes that point. This function parses a statement,
8527 but ensures that is in its own scope, even if it is not a
8530 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8531 is a (possibly labeled) if statement which is not enclosed in
8532 braces and has an else clause. This is used to implement
8535 Returns the new statement. */
8538 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
8545 /* Mark if () ; with a special NOP_EXPR. */
8546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
8548 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8549 cp_lexer_consume_token (parser
->lexer
);
8550 statement
= add_stmt (build_empty_stmt (loc
));
8552 /* if a compound is opened, we simply parse the statement directly. */
8553 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8554 statement
= cp_parser_compound_statement (parser
, NULL
, false);
8555 /* If the token is not a `{', then we must take special action. */
8558 /* Create a compound-statement. */
8559 statement
= begin_compound_stmt (0);
8560 /* Parse the dependent-statement. */
8561 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
8562 /* Finish the dummy compound-statement. */
8563 finish_compound_stmt (statement
);
8566 /* Return the statement. */
8570 /* For some dependent statements (like `while (cond) statement'), we
8571 have already created a scope. Therefore, even if the dependent
8572 statement is a compound-statement, we do not want to create another
8576 cp_parser_already_scoped_statement (cp_parser
* parser
)
8578 /* If the token is a `{', then we must take special action. */
8579 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
8580 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
8583 /* Avoid calling cp_parser_compound_statement, so that we
8584 don't create a new scope. Do everything else by hand. */
8585 cp_parser_require (parser
, CPP_OPEN_BRACE
, "%<{%>");
8586 /* If the next keyword is `__label__' we have a label declaration. */
8587 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
8588 cp_parser_label_declaration (parser
);
8589 /* Parse an (optional) statement-seq. */
8590 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
8591 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
8595 /* Declarations [gram.dcl.dcl] */
8597 /* Parse an optional declaration-sequence.
8601 declaration-seq declaration */
8604 cp_parser_declaration_seq_opt (cp_parser
* parser
)
8610 token
= cp_lexer_peek_token (parser
->lexer
);
8612 if (token
->type
== CPP_CLOSE_BRACE
8613 || token
->type
== CPP_EOF
8614 || token
->type
== CPP_PRAGMA_EOL
)
8617 if (token
->type
== CPP_SEMICOLON
)
8619 /* A declaration consisting of a single semicolon is
8620 invalid. Allow it unless we're being pedantic. */
8621 cp_lexer_consume_token (parser
->lexer
);
8622 if (!in_system_header
)
8623 pedwarn (input_location
, OPT_pedantic
, "extra %<;%>");
8627 /* If we're entering or exiting a region that's implicitly
8628 extern "C", modify the lang context appropriately. */
8629 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
8631 push_lang_context (lang_name_c
);
8632 parser
->implicit_extern_c
= true;
8634 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
8636 pop_lang_context ();
8637 parser
->implicit_extern_c
= false;
8640 if (token
->type
== CPP_PRAGMA
)
8642 /* A top-level declaration can consist solely of a #pragma.
8643 A nested declaration cannot, so this is done here and not
8644 in cp_parser_declaration. (A #pragma at block scope is
8645 handled in cp_parser_statement.) */
8646 cp_parser_pragma (parser
, pragma_external
);
8650 /* Parse the declaration itself. */
8651 cp_parser_declaration (parser
);
8655 /* Parse a declaration.
8660 template-declaration
8661 explicit-instantiation
8662 explicit-specialization
8663 linkage-specification
8664 namespace-definition
8669 __extension__ declaration */
8672 cp_parser_declaration (cp_parser
* parser
)
8679 /* Check for the `__extension__' keyword. */
8680 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
8682 /* Parse the qualified declaration. */
8683 cp_parser_declaration (parser
);
8684 /* Restore the PEDANTIC flag. */
8685 pedantic
= saved_pedantic
;
8690 /* Try to figure out what kind of declaration is present. */
8691 token1
= *cp_lexer_peek_token (parser
->lexer
);
8693 if (token1
.type
!= CPP_EOF
)
8694 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
8697 token2
.type
= CPP_EOF
;
8698 token2
.keyword
= RID_MAX
;
8701 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8702 p
= obstack_alloc (&declarator_obstack
, 0);
8704 /* If the next token is `extern' and the following token is a string
8705 literal, then we have a linkage specification. */
8706 if (token1
.keyword
== RID_EXTERN
8707 && cp_parser_is_string_literal (&token2
))
8708 cp_parser_linkage_specification (parser
);
8709 /* If the next token is `template', then we have either a template
8710 declaration, an explicit instantiation, or an explicit
8712 else if (token1
.keyword
== RID_TEMPLATE
)
8714 /* `template <>' indicates a template specialization. */
8715 if (token2
.type
== CPP_LESS
8716 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
8717 cp_parser_explicit_specialization (parser
);
8718 /* `template <' indicates a template declaration. */
8719 else if (token2
.type
== CPP_LESS
)
8720 cp_parser_template_declaration (parser
, /*member_p=*/false);
8721 /* Anything else must be an explicit instantiation. */
8723 cp_parser_explicit_instantiation (parser
);
8725 /* If the next token is `export', then we have a template
8727 else if (token1
.keyword
== RID_EXPORT
)
8728 cp_parser_template_declaration (parser
, /*member_p=*/false);
8729 /* If the next token is `extern', 'static' or 'inline' and the one
8730 after that is `template', we have a GNU extended explicit
8731 instantiation directive. */
8732 else if (cp_parser_allow_gnu_extensions_p (parser
)
8733 && (token1
.keyword
== RID_EXTERN
8734 || token1
.keyword
== RID_STATIC
8735 || token1
.keyword
== RID_INLINE
)
8736 && token2
.keyword
== RID_TEMPLATE
)
8737 cp_parser_explicit_instantiation (parser
);
8738 /* If the next token is `namespace', check for a named or unnamed
8739 namespace definition. */
8740 else if (token1
.keyword
== RID_NAMESPACE
8741 && (/* A named namespace definition. */
8742 (token2
.type
== CPP_NAME
8743 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
8745 /* An unnamed namespace definition. */
8746 || token2
.type
== CPP_OPEN_BRACE
8747 || token2
.keyword
== RID_ATTRIBUTE
))
8748 cp_parser_namespace_definition (parser
);
8749 /* An inline (associated) namespace definition. */
8750 else if (token1
.keyword
== RID_INLINE
8751 && token2
.keyword
== RID_NAMESPACE
)
8752 cp_parser_namespace_definition (parser
);
8753 /* Objective-C++ declaration/definition. */
8754 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
8755 cp_parser_objc_declaration (parser
);
8756 /* We must have either a block declaration or a function
8759 /* Try to parse a block-declaration, or a function-definition. */
8760 cp_parser_block_declaration (parser
, /*statement_p=*/false);
8762 /* Free any declarators allocated. */
8763 obstack_free (&declarator_obstack
, p
);
8766 /* Parse a block-declaration.
8771 namespace-alias-definition
8778 __extension__ block-declaration
8783 static_assert-declaration
8785 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8786 part of a declaration-statement. */
8789 cp_parser_block_declaration (cp_parser
*parser
,
8795 /* Check for the `__extension__' keyword. */
8796 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
8798 /* Parse the qualified declaration. */
8799 cp_parser_block_declaration (parser
, statement_p
);
8800 /* Restore the PEDANTIC flag. */
8801 pedantic
= saved_pedantic
;
8806 /* Peek at the next token to figure out which kind of declaration is
8808 token1
= cp_lexer_peek_token (parser
->lexer
);
8810 /* If the next keyword is `asm', we have an asm-definition. */
8811 if (token1
->keyword
== RID_ASM
)
8814 cp_parser_commit_to_tentative_parse (parser
);
8815 cp_parser_asm_definition (parser
);
8817 /* If the next keyword is `namespace', we have a
8818 namespace-alias-definition. */
8819 else if (token1
->keyword
== RID_NAMESPACE
)
8820 cp_parser_namespace_alias_definition (parser
);
8821 /* If the next keyword is `using', we have either a
8822 using-declaration or a using-directive. */
8823 else if (token1
->keyword
== RID_USING
)
8828 cp_parser_commit_to_tentative_parse (parser
);
8829 /* If the token after `using' is `namespace', then we have a
8831 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8832 if (token2
->keyword
== RID_NAMESPACE
)
8833 cp_parser_using_directive (parser
);
8834 /* Otherwise, it's a using-declaration. */
8836 cp_parser_using_declaration (parser
,
8837 /*access_declaration_p=*/false);
8839 /* If the next keyword is `__label__' we have a misplaced label
8841 else if (token1
->keyword
== RID_LABEL
)
8843 cp_lexer_consume_token (parser
->lexer
);
8844 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
8845 cp_parser_skip_to_end_of_statement (parser
);
8846 /* If the next token is now a `;', consume it. */
8847 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
8848 cp_lexer_consume_token (parser
->lexer
);
8850 /* If the next token is `static_assert' we have a static assertion. */
8851 else if (token1
->keyword
== RID_STATIC_ASSERT
)
8852 cp_parser_static_assert (parser
, /*member_p=*/false);
8853 /* Anything else must be a simple-declaration. */
8855 cp_parser_simple_declaration (parser
, !statement_p
);
8858 /* Parse a simple-declaration.
8861 decl-specifier-seq [opt] init-declarator-list [opt] ;
8863 init-declarator-list:
8865 init-declarator-list , init-declarator
8867 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8868 function-definition as a simple-declaration. */
8871 cp_parser_simple_declaration (cp_parser
* parser
,
8872 bool function_definition_allowed_p
)
8874 cp_decl_specifier_seq decl_specifiers
;
8875 int declares_class_or_enum
;
8876 bool saw_declarator
;
8878 /* Defer access checks until we know what is being declared; the
8879 checks for names appearing in the decl-specifier-seq should be
8880 done as if we were in the scope of the thing being declared. */
8881 push_deferring_access_checks (dk_deferred
);
8883 /* Parse the decl-specifier-seq. We have to keep track of whether
8884 or not the decl-specifier-seq declares a named class or
8885 enumeration type, since that is the only case in which the
8886 init-declarator-list is allowed to be empty.
8890 In a simple-declaration, the optional init-declarator-list can be
8891 omitted only when declaring a class or enumeration, that is when
8892 the decl-specifier-seq contains either a class-specifier, an
8893 elaborated-type-specifier, or an enum-specifier. */
8894 cp_parser_decl_specifier_seq (parser
,
8895 CP_PARSER_FLAGS_OPTIONAL
,
8897 &declares_class_or_enum
);
8898 /* We no longer need to defer access checks. */
8899 stop_deferring_access_checks ();
8901 /* In a block scope, a valid declaration must always have a
8902 decl-specifier-seq. By not trying to parse declarators, we can
8903 resolve the declaration/expression ambiguity more quickly. */
8904 if (!function_definition_allowed_p
8905 && !decl_specifiers
.any_specifiers_p
)
8907 cp_parser_error (parser
, "expected declaration");
8911 /* If the next two tokens are both identifiers, the code is
8912 erroneous. The usual cause of this situation is code like:
8916 where "T" should name a type -- but does not. */
8917 if (!decl_specifiers
.any_type_specifiers_p
8918 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
8920 /* If parsing tentatively, we should commit; we really are
8921 looking at a declaration. */
8922 cp_parser_commit_to_tentative_parse (parser
);
8927 /* If we have seen at least one decl-specifier, and the next token
8928 is not a parenthesis, then we must be looking at a declaration.
8929 (After "int (" we might be looking at a functional cast.) */
8930 if (decl_specifiers
.any_specifiers_p
8931 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
8932 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
8933 && !cp_parser_error_occurred (parser
))
8934 cp_parser_commit_to_tentative_parse (parser
);
8936 /* Keep going until we hit the `;' at the end of the simple
8938 saw_declarator
= false;
8939 while (cp_lexer_next_token_is_not (parser
->lexer
,
8943 bool function_definition_p
;
8948 /* If we are processing next declarator, coma is expected */
8949 token
= cp_lexer_peek_token (parser
->lexer
);
8950 gcc_assert (token
->type
== CPP_COMMA
);
8951 cp_lexer_consume_token (parser
->lexer
);
8954 saw_declarator
= true;
8956 /* Parse the init-declarator. */
8957 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
8959 function_definition_allowed_p
,
8961 declares_class_or_enum
,
8962 &function_definition_p
);
8963 /* If an error occurred while parsing tentatively, exit quickly.
8964 (That usually happens when in the body of a function; each
8965 statement is treated as a declaration-statement until proven
8967 if (cp_parser_error_occurred (parser
))
8969 /* Handle function definitions specially. */
8970 if (function_definition_p
)
8972 /* If the next token is a `,', then we are probably
8973 processing something like:
8977 which is erroneous. */
8978 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8980 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8981 error_at (token
->location
,
8983 " declarations and function-definitions is forbidden");
8985 /* Otherwise, we're done with the list of declarators. */
8988 pop_deferring_access_checks ();
8992 /* The next token should be either a `,' or a `;'. */
8993 token
= cp_lexer_peek_token (parser
->lexer
);
8994 /* If it's a `,', there are more declarators to come. */
8995 if (token
->type
== CPP_COMMA
)
8996 /* will be consumed next time around */;
8997 /* If it's a `;', we are done. */
8998 else if (token
->type
== CPP_SEMICOLON
)
9000 /* Anything else is an error. */
9003 /* If we have already issued an error message we don't need
9004 to issue another one. */
9005 if (decl
!= error_mark_node
9006 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
9007 cp_parser_error (parser
, "expected %<,%> or %<;%>");
9008 /* Skip tokens until we reach the end of the statement. */
9009 cp_parser_skip_to_end_of_statement (parser
);
9010 /* If the next token is now a `;', consume it. */
9011 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9012 cp_lexer_consume_token (parser
->lexer
);
9015 /* After the first time around, a function-definition is not
9016 allowed -- even if it was OK at first. For example:
9021 function_definition_allowed_p
= false;
9024 /* Issue an error message if no declarators are present, and the
9025 decl-specifier-seq does not itself declare a class or
9027 if (!saw_declarator
)
9029 if (cp_parser_declares_only_class_p (parser
))
9030 shadow_tag (&decl_specifiers
);
9031 /* Perform any deferred access checks. */
9032 perform_deferred_access_checks ();
9035 /* Consume the `;'. */
9036 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
9039 pop_deferring_access_checks ();
9042 /* Parse a decl-specifier-seq.
9045 decl-specifier-seq [opt] decl-specifier
9048 storage-class-specifier
9059 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9061 The parser flags FLAGS is used to control type-specifier parsing.
9063 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9066 1: one of the decl-specifiers is an elaborated-type-specifier
9067 (i.e., a type declaration)
9068 2: one of the decl-specifiers is an enum-specifier or a
9069 class-specifier (i.e., a type definition)
9074 cp_parser_decl_specifier_seq (cp_parser
* parser
,
9075 cp_parser_flags flags
,
9076 cp_decl_specifier_seq
*decl_specs
,
9077 int* declares_class_or_enum
)
9079 bool constructor_possible_p
= !parser
->in_declarator_p
;
9080 cp_token
*start_token
= NULL
;
9082 /* Clear DECL_SPECS. */
9083 clear_decl_specs (decl_specs
);
9085 /* Assume no class or enumeration type is declared. */
9086 *declares_class_or_enum
= 0;
9088 /* Keep reading specifiers until there are no more to read. */
9092 bool found_decl_spec
;
9095 /* Peek at the next token. */
9096 token
= cp_lexer_peek_token (parser
->lexer
);
9098 /* Save the first token of the decl spec list for error
9101 start_token
= token
;
9102 /* Handle attributes. */
9103 if (token
->keyword
== RID_ATTRIBUTE
)
9105 /* Parse the attributes. */
9106 decl_specs
->attributes
9107 = chainon (decl_specs
->attributes
,
9108 cp_parser_attributes_opt (parser
));
9111 /* Assume we will find a decl-specifier keyword. */
9112 found_decl_spec
= true;
9113 /* If the next token is an appropriate keyword, we can simply
9114 add it to the list. */
9115 switch (token
->keyword
)
9121 if (!at_class_scope_p ())
9123 error_at (token
->location
, "%<friend%> used outside of class");
9124 cp_lexer_purge_token (parser
->lexer
);
9128 ++decl_specs
->specs
[(int) ds_friend
];
9129 /* Consume the token. */
9130 cp_lexer_consume_token (parser
->lexer
);
9135 ++decl_specs
->specs
[(int) ds_constexpr
];
9136 cp_lexer_consume_token (parser
->lexer
);
9139 /* function-specifier:
9146 cp_parser_function_specifier_opt (parser
, decl_specs
);
9152 ++decl_specs
->specs
[(int) ds_typedef
];
9153 /* Consume the token. */
9154 cp_lexer_consume_token (parser
->lexer
);
9155 /* A constructor declarator cannot appear in a typedef. */
9156 constructor_possible_p
= false;
9157 /* The "typedef" keyword can only occur in a declaration; we
9158 may as well commit at this point. */
9159 cp_parser_commit_to_tentative_parse (parser
);
9161 if (decl_specs
->storage_class
!= sc_none
)
9162 decl_specs
->conflicting_specifiers_p
= true;
9165 /* storage-class-specifier:
9175 if (cxx_dialect
== cxx98
)
9177 /* Consume the token. */
9178 cp_lexer_consume_token (parser
->lexer
);
9180 /* Complain about `auto' as a storage specifier, if
9181 we're complaining about C++0x compatibility. */
9182 warning_at (token
->location
, OPT_Wc__0x_compat
, "%<auto%>"
9183 " will change meaning in C++0x; please remove it");
9185 /* Set the storage class anyway. */
9186 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
9190 /* C++0x auto type-specifier. */
9191 found_decl_spec
= false;
9198 /* Consume the token. */
9199 cp_lexer_consume_token (parser
->lexer
);
9200 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
9204 /* Consume the token. */
9205 cp_lexer_consume_token (parser
->lexer
);
9206 ++decl_specs
->specs
[(int) ds_thread
];
9210 /* We did not yet find a decl-specifier yet. */
9211 found_decl_spec
= false;
9215 /* Constructors are a special case. The `S' in `S()' is not a
9216 decl-specifier; it is the beginning of the declarator. */
9219 && constructor_possible_p
9220 && (cp_parser_constructor_declarator_p
9221 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
9223 /* If we don't have a DECL_SPEC yet, then we must be looking at
9224 a type-specifier. */
9225 if (!found_decl_spec
&& !constructor_p
)
9227 int decl_spec_declares_class_or_enum
;
9228 bool is_cv_qualifier
;
9232 = cp_parser_type_specifier (parser
, flags
,
9234 /*is_declaration=*/true,
9235 &decl_spec_declares_class_or_enum
,
9237 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
9239 /* If this type-specifier referenced a user-defined type
9240 (a typedef, class-name, etc.), then we can't allow any
9241 more such type-specifiers henceforth.
9245 The longest sequence of decl-specifiers that could
9246 possibly be a type name is taken as the
9247 decl-specifier-seq of a declaration. The sequence shall
9248 be self-consistent as described below.
9252 As a general rule, at most one type-specifier is allowed
9253 in the complete decl-specifier-seq of a declaration. The
9254 only exceptions are the following:
9256 -- const or volatile can be combined with any other
9259 -- signed or unsigned can be combined with char, long,
9267 void g (const int Pc);
9269 Here, Pc is *not* part of the decl-specifier seq; it's
9270 the declarator. Therefore, once we see a type-specifier
9271 (other than a cv-qualifier), we forbid any additional
9272 user-defined types. We *do* still allow things like `int
9273 int' to be considered a decl-specifier-seq, and issue the
9274 error message later. */
9275 if (type_spec
&& !is_cv_qualifier
)
9276 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
9277 /* A constructor declarator cannot follow a type-specifier. */
9280 constructor_possible_p
= false;
9281 found_decl_spec
= true;
9282 if (!is_cv_qualifier
)
9283 decl_specs
->any_type_specifiers_p
= true;
9287 /* If we still do not have a DECL_SPEC, then there are no more
9289 if (!found_decl_spec
)
9292 decl_specs
->any_specifiers_p
= true;
9293 /* After we see one decl-specifier, further decl-specifiers are
9295 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
9298 cp_parser_check_decl_spec (decl_specs
, start_token
->location
);
9300 /* Don't allow a friend specifier with a class definition. */
9301 if (decl_specs
->specs
[(int) ds_friend
] != 0
9302 && (*declares_class_or_enum
& 2))
9303 error_at (start_token
->location
,
9304 "class definition may not be declared a friend");
9307 /* Parse an (optional) storage-class-specifier.
9309 storage-class-specifier:
9318 storage-class-specifier:
9321 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9324 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
9326 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
9329 if (cxx_dialect
!= cxx98
)
9331 /* Fall through for C++98. */
9338 /* Consume the token. */
9339 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
9346 /* Parse an (optional) function-specifier.
9353 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9354 Updates DECL_SPECS, if it is non-NULL. */
9357 cp_parser_function_specifier_opt (cp_parser
* parser
,
9358 cp_decl_specifier_seq
*decl_specs
)
9360 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9361 switch (token
->keyword
)
9365 ++decl_specs
->specs
[(int) ds_inline
];
9369 /* 14.5.2.3 [temp.mem]
9371 A member function template shall not be virtual. */
9372 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9373 error_at (token
->location
, "templates may not be %<virtual%>");
9374 else if (decl_specs
)
9375 ++decl_specs
->specs
[(int) ds_virtual
];
9380 ++decl_specs
->specs
[(int) ds_explicit
];
9387 /* Consume the token. */
9388 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
9391 /* Parse a linkage-specification.
9393 linkage-specification:
9394 extern string-literal { declaration-seq [opt] }
9395 extern string-literal declaration */
9398 cp_parser_linkage_specification (cp_parser
* parser
)
9402 /* Look for the `extern' keyword. */
9403 cp_parser_require_keyword (parser
, RID_EXTERN
, "%<extern%>");
9405 /* Look for the string-literal. */
9406 linkage
= cp_parser_string_literal (parser
, false, false);
9408 /* Transform the literal into an identifier. If the literal is a
9409 wide-character string, or contains embedded NULs, then we can't
9410 handle it as the user wants. */
9411 if (strlen (TREE_STRING_POINTER (linkage
))
9412 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
9414 cp_parser_error (parser
, "invalid linkage-specification");
9415 /* Assume C++ linkage. */
9416 linkage
= lang_name_cplusplus
;
9419 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
9421 /* We're now using the new linkage. */
9422 push_lang_context (linkage
);
9424 /* If the next token is a `{', then we're using the first
9426 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9428 /* Consume the `{' token. */
9429 cp_lexer_consume_token (parser
->lexer
);
9430 /* Parse the declarations. */
9431 cp_parser_declaration_seq_opt (parser
);
9432 /* Look for the closing `}'. */
9433 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
9435 /* Otherwise, there's just one declaration. */
9438 bool saved_in_unbraced_linkage_specification_p
;
9440 saved_in_unbraced_linkage_specification_p
9441 = parser
->in_unbraced_linkage_specification_p
;
9442 parser
->in_unbraced_linkage_specification_p
= true;
9443 cp_parser_declaration (parser
);
9444 parser
->in_unbraced_linkage_specification_p
9445 = saved_in_unbraced_linkage_specification_p
;
9448 /* We're done with the linkage-specification. */
9449 pop_lang_context ();
9452 /* Parse a static_assert-declaration.
9454 static_assert-declaration:
9455 static_assert ( constant-expression , string-literal ) ;
9457 If MEMBER_P, this static_assert is a class member. */
9460 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
9465 location_t saved_loc
;
9467 /* Peek at the `static_assert' token so we can keep track of exactly
9468 where the static assertion started. */
9469 token
= cp_lexer_peek_token (parser
->lexer
);
9470 saved_loc
= token
->location
;
9472 /* Look for the `static_assert' keyword. */
9473 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
9474 "%<static_assert%>"))
9477 /* We know we are in a static assertion; commit to any tentative
9479 if (cp_parser_parsing_tentatively (parser
))
9480 cp_parser_commit_to_tentative_parse (parser
);
9482 /* Parse the `(' starting the static assertion condition. */
9483 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
9485 /* Parse the constant-expression. */
9487 cp_parser_constant_expression (parser
,
9488 /*allow_non_constant_p=*/false,
9489 /*non_constant_p=*/NULL
);
9491 /* Parse the separating `,'. */
9492 cp_parser_require (parser
, CPP_COMMA
, "%<,%>");
9494 /* Parse the string-literal message. */
9495 message
= cp_parser_string_literal (parser
,
9496 /*translate=*/false,
9499 /* A `)' completes the static assertion. */
9500 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
9501 cp_parser_skip_to_closing_parenthesis (parser
,
9502 /*recovering=*/true,
9504 /*consume_paren=*/true);
9506 /* A semicolon terminates the declaration. */
9507 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
9509 /* Complete the static assertion, which may mean either processing
9510 the static assert now or saving it for template instantiation. */
9511 finish_static_assert (condition
, message
, saved_loc
, member_p
);
9514 /* Parse a `decltype' type. Returns the type.
9516 simple-type-specifier:
9517 decltype ( expression ) */
9520 cp_parser_decltype (cp_parser
*parser
)
9523 bool id_expression_or_member_access_p
= false;
9524 const char *saved_message
;
9525 bool saved_integral_constant_expression_p
;
9526 bool saved_non_integral_constant_expression_p
;
9527 cp_token
*id_expr_start_token
;
9529 /* Look for the `decltype' token. */
9530 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, "%<decltype%>"))
9531 return error_mark_node
;
9533 /* Types cannot be defined in a `decltype' expression. Save away the
9535 saved_message
= parser
->type_definition_forbidden_message
;
9537 /* And create the new one. */
9538 parser
->type_definition_forbidden_message
9539 = G_("types may not be defined in %<decltype%> expressions");
9541 /* The restrictions on constant-expressions do not apply inside
9542 decltype expressions. */
9543 saved_integral_constant_expression_p
9544 = parser
->integral_constant_expression_p
;
9545 saved_non_integral_constant_expression_p
9546 = parser
->non_integral_constant_expression_p
;
9547 parser
->integral_constant_expression_p
= false;
9549 /* Do not actually evaluate the expression. */
9550 ++cp_unevaluated_operand
;
9552 /* Do not warn about problems with the expression. */
9553 ++c_inhibit_evaluation_warnings
;
9555 /* Parse the opening `('. */
9556 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
9557 return error_mark_node
;
9559 /* First, try parsing an id-expression. */
9560 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
9561 cp_parser_parse_tentatively (parser
);
9562 expr
= cp_parser_id_expression (parser
,
9563 /*template_keyword_p=*/false,
9564 /*check_dependency_p=*/true,
9565 /*template_p=*/NULL
,
9566 /*declarator_p=*/false,
9567 /*optional_p=*/false);
9569 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
9571 bool non_integral_constant_expression_p
= false;
9572 tree id_expression
= expr
;
9574 const char *error_msg
;
9576 if (TREE_CODE (expr
) == IDENTIFIER_NODE
)
9577 /* Lookup the name we got back from the id-expression. */
9578 expr
= cp_parser_lookup_name (parser
, expr
,
9580 /*is_template=*/false,
9581 /*is_namespace=*/false,
9582 /*check_dependency=*/true,
9583 /*ambiguous_decls=*/NULL
,
9584 id_expr_start_token
->location
);
9587 && expr
!= error_mark_node
9588 && TREE_CODE (expr
) != TEMPLATE_ID_EXPR
9589 && TREE_CODE (expr
) != TYPE_DECL
9590 && (TREE_CODE (expr
) != BIT_NOT_EXPR
9591 || !TYPE_P (TREE_OPERAND (expr
, 0)))
9592 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
9594 /* Complete lookup of the id-expression. */
9595 expr
= (finish_id_expression
9596 (id_expression
, expr
, parser
->scope
, &idk
,
9597 /*integral_constant_expression_p=*/false,
9598 /*allow_non_integral_constant_expression_p=*/true,
9599 &non_integral_constant_expression_p
,
9600 /*template_p=*/false,
9602 /*address_p=*/false,
9603 /*template_arg_p=*/false,
9605 id_expr_start_token
->location
));
9607 if (expr
== error_mark_node
)
9608 /* We found an id-expression, but it was something that we
9609 should not have found. This is an error, not something
9610 we can recover from, so note that we found an
9611 id-expression and we'll recover as gracefully as
9613 id_expression_or_member_access_p
= true;
9617 && expr
!= error_mark_node
9618 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
9619 /* We have an id-expression. */
9620 id_expression_or_member_access_p
= true;
9623 if (!id_expression_or_member_access_p
)
9625 /* Abort the id-expression parse. */
9626 cp_parser_abort_tentative_parse (parser
);
9628 /* Parsing tentatively, again. */
9629 cp_parser_parse_tentatively (parser
);
9631 /* Parse a class member access. */
9632 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
9634 /*member_access_only_p=*/true, NULL
);
9637 && expr
!= error_mark_node
9638 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
9639 /* We have an id-expression. */
9640 id_expression_or_member_access_p
= true;
9643 if (id_expression_or_member_access_p
)
9644 /* We have parsed the complete id-expression or member access. */
9645 cp_parser_parse_definitely (parser
);
9648 bool saved_greater_than_is_operator_p
;
9650 /* Abort our attempt to parse an id-expression or member access
9652 cp_parser_abort_tentative_parse (parser
);
9654 /* Within a parenthesized expression, a `>' token is always
9655 the greater-than operator. */
9656 saved_greater_than_is_operator_p
9657 = parser
->greater_than_is_operator_p
;
9658 parser
->greater_than_is_operator_p
= true;
9660 /* Parse a full expression. */
9661 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9663 /* The `>' token might be the end of a template-id or
9664 template-parameter-list now. */
9665 parser
->greater_than_is_operator_p
9666 = saved_greater_than_is_operator_p
;
9669 /* Go back to evaluating expressions. */
9670 --cp_unevaluated_operand
;
9671 --c_inhibit_evaluation_warnings
;
9673 /* Restore the old message and the integral constant expression
9675 parser
->type_definition_forbidden_message
= saved_message
;
9676 parser
->integral_constant_expression_p
9677 = saved_integral_constant_expression_p
;
9678 parser
->non_integral_constant_expression_p
9679 = saved_non_integral_constant_expression_p
;
9681 if (expr
== error_mark_node
)
9683 /* Skip everything up to the closing `)'. */
9684 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
9685 /*consume_paren=*/true);
9686 return error_mark_node
;
9689 /* Parse to the closing `)'. */
9690 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
9692 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
9693 /*consume_paren=*/true);
9694 return error_mark_node
;
9697 return finish_decltype_type (expr
, id_expression_or_member_access_p
);
9700 /* Special member functions [gram.special] */
9702 /* Parse a conversion-function-id.
9704 conversion-function-id:
9705 operator conversion-type-id
9707 Returns an IDENTIFIER_NODE representing the operator. */
9710 cp_parser_conversion_function_id (cp_parser
* parser
)
9714 tree saved_qualifying_scope
;
9715 tree saved_object_scope
;
9716 tree pushed_scope
= NULL_TREE
;
9718 /* Look for the `operator' token. */
9719 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "%<operator%>"))
9720 return error_mark_node
;
9721 /* When we parse the conversion-type-id, the current scope will be
9722 reset. However, we need that information in able to look up the
9723 conversion function later, so we save it here. */
9724 saved_scope
= parser
->scope
;
9725 saved_qualifying_scope
= parser
->qualifying_scope
;
9726 saved_object_scope
= parser
->object_scope
;
9727 /* We must enter the scope of the class so that the names of
9728 entities declared within the class are available in the
9729 conversion-type-id. For example, consider:
9736 S::operator I() { ... }
9738 In order to see that `I' is a type-name in the definition, we
9739 must be in the scope of `S'. */
9741 pushed_scope
= push_scope (saved_scope
);
9742 /* Parse the conversion-type-id. */
9743 type
= cp_parser_conversion_type_id (parser
);
9744 /* Leave the scope of the class, if any. */
9746 pop_scope (pushed_scope
);
9747 /* Restore the saved scope. */
9748 parser
->scope
= saved_scope
;
9749 parser
->qualifying_scope
= saved_qualifying_scope
;
9750 parser
->object_scope
= saved_object_scope
;
9751 /* If the TYPE is invalid, indicate failure. */
9752 if (type
== error_mark_node
)
9753 return error_mark_node
;
9754 return mangle_conv_op_name_for_type (type
);
9757 /* Parse a conversion-type-id:
9760 type-specifier-seq conversion-declarator [opt]
9762 Returns the TYPE specified. */
9765 cp_parser_conversion_type_id (cp_parser
* parser
)
9768 cp_decl_specifier_seq type_specifiers
;
9769 cp_declarator
*declarator
;
9770 tree type_specified
;
9772 /* Parse the attributes. */
9773 attributes
= cp_parser_attributes_opt (parser
);
9774 /* Parse the type-specifiers. */
9775 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
9776 /*is_trailing_return=*/false,
9778 /* If that didn't work, stop. */
9779 if (type_specifiers
.type
== error_mark_node
)
9780 return error_mark_node
;
9781 /* Parse the conversion-declarator. */
9782 declarator
= cp_parser_conversion_declarator_opt (parser
);
9784 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
9785 /*initialized=*/0, &attributes
);
9787 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
9789 /* Don't give this error when parsing tentatively. This happens to
9790 work because we always parse this definitively once. */
9791 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
9792 && type_uses_auto (type_specified
))
9794 error ("invalid use of %<auto%> in conversion operator");
9795 return error_mark_node
;
9798 return type_specified
;
9801 /* Parse an (optional) conversion-declarator.
9803 conversion-declarator:
9804 ptr-operator conversion-declarator [opt]
9808 static cp_declarator
*
9809 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
9811 enum tree_code code
;
9813 cp_cv_quals cv_quals
;
9815 /* We don't know if there's a ptr-operator next, or not. */
9816 cp_parser_parse_tentatively (parser
);
9817 /* Try the ptr-operator. */
9818 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
9819 /* If it worked, look for more conversion-declarators. */
9820 if (cp_parser_parse_definitely (parser
))
9822 cp_declarator
*declarator
;
9824 /* Parse another optional declarator. */
9825 declarator
= cp_parser_conversion_declarator_opt (parser
);
9827 return cp_parser_make_indirect_declarator
9828 (code
, class_type
, cv_quals
, declarator
);
9834 /* Parse an (optional) ctor-initializer.
9837 : mem-initializer-list
9839 Returns TRUE iff the ctor-initializer was actually present. */
9842 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
9844 /* If the next token is not a `:', then there is no
9845 ctor-initializer. */
9846 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
9848 /* Do default initialization of any bases and members. */
9849 if (DECL_CONSTRUCTOR_P (current_function_decl
))
9850 finish_mem_initializers (NULL_TREE
);
9855 /* Consume the `:' token. */
9856 cp_lexer_consume_token (parser
->lexer
);
9857 /* And the mem-initializer-list. */
9858 cp_parser_mem_initializer_list (parser
);
9863 /* Parse a mem-initializer-list.
9865 mem-initializer-list:
9866 mem-initializer ... [opt]
9867 mem-initializer ... [opt] , mem-initializer-list */
9870 cp_parser_mem_initializer_list (cp_parser
* parser
)
9872 tree mem_initializer_list
= NULL_TREE
;
9873 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9875 /* Let the semantic analysis code know that we are starting the
9876 mem-initializer-list. */
9877 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
9878 error_at (token
->location
,
9879 "only constructors take base initializers");
9881 /* Loop through the list. */
9884 tree mem_initializer
;
9886 token
= cp_lexer_peek_token (parser
->lexer
);
9887 /* Parse the mem-initializer. */
9888 mem_initializer
= cp_parser_mem_initializer (parser
);
9889 /* If the next token is a `...', we're expanding member initializers. */
9890 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9892 /* Consume the `...'. */
9893 cp_lexer_consume_token (parser
->lexer
);
9895 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9896 can be expanded but members cannot. */
9897 if (mem_initializer
!= error_mark_node
9898 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
9900 error_at (token
->location
,
9901 "cannot expand initializer for member %<%D%>",
9902 TREE_PURPOSE (mem_initializer
));
9903 mem_initializer
= error_mark_node
;
9906 /* Construct the pack expansion type. */
9907 if (mem_initializer
!= error_mark_node
)
9908 mem_initializer
= make_pack_expansion (mem_initializer
);
9910 /* Add it to the list, unless it was erroneous. */
9911 if (mem_initializer
!= error_mark_node
)
9913 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
9914 mem_initializer_list
= mem_initializer
;
9916 /* If the next token is not a `,', we're done. */
9917 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
9919 /* Consume the `,' token. */
9920 cp_lexer_consume_token (parser
->lexer
);
9923 /* Perform semantic analysis. */
9924 if (DECL_CONSTRUCTOR_P (current_function_decl
))
9925 finish_mem_initializers (mem_initializer_list
);
9928 /* Parse a mem-initializer.
9931 mem-initializer-id ( expression-list [opt] )
9932 mem-initializer-id braced-init-list
9937 ( expression-list [opt] )
9939 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9940 class) or FIELD_DECL (for a non-static data member) to initialize;
9941 the TREE_VALUE is the expression-list. An empty initialization
9942 list is represented by void_list_node. */
9945 cp_parser_mem_initializer (cp_parser
* parser
)
9947 tree mem_initializer_id
;
9948 tree expression_list
;
9950 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9952 /* Find out what is being initialized. */
9953 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9955 permerror (token
->location
,
9956 "anachronistic old-style base class initializer");
9957 mem_initializer_id
= NULL_TREE
;
9961 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
9962 if (mem_initializer_id
== error_mark_node
)
9963 return mem_initializer_id
;
9965 member
= expand_member_init (mem_initializer_id
);
9966 if (member
&& !DECL_P (member
))
9967 in_base_initializer
= 1;
9969 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9971 bool expr_non_constant_p
;
9972 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9973 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
9974 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
9975 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
9980 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
9982 /*allow_expansion_p=*/true,
9983 /*non_constant_p=*/NULL
);
9985 return error_mark_node
;
9986 expression_list
= build_tree_list_vec (vec
);
9987 release_tree_vector (vec
);
9990 if (expression_list
== error_mark_node
)
9991 return error_mark_node
;
9992 if (!expression_list
)
9993 expression_list
= void_type_node
;
9995 in_base_initializer
= 0;
9997 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
10000 /* Parse a mem-initializer-id.
10002 mem-initializer-id:
10003 :: [opt] nested-name-specifier [opt] class-name
10006 Returns a TYPE indicating the class to be initializer for the first
10007 production. Returns an IDENTIFIER_NODE indicating the data member
10008 to be initialized for the second production. */
10011 cp_parser_mem_initializer_id (cp_parser
* parser
)
10013 bool global_scope_p
;
10014 bool nested_name_specifier_p
;
10015 bool template_p
= false;
10018 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10020 /* `typename' is not allowed in this context ([temp.res]). */
10021 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
10023 error_at (token
->location
,
10024 "keyword %<typename%> not allowed in this context (a qualified "
10025 "member initializer is implicitly a type)");
10026 cp_lexer_consume_token (parser
->lexer
);
10028 /* Look for the optional `::' operator. */
10030 = (cp_parser_global_scope_opt (parser
,
10031 /*current_scope_valid_p=*/false)
10033 /* Look for the optional nested-name-specifier. The simplest way to
10038 The keyword `typename' is not permitted in a base-specifier or
10039 mem-initializer; in these contexts a qualified name that
10040 depends on a template-parameter is implicitly assumed to be a
10043 is to assume that we have seen the `typename' keyword at this
10045 nested_name_specifier_p
10046 = (cp_parser_nested_name_specifier_opt (parser
,
10047 /*typename_keyword_p=*/true,
10048 /*check_dependency_p=*/true,
10050 /*is_declaration=*/true)
10052 if (nested_name_specifier_p
)
10053 template_p
= cp_parser_optional_template_keyword (parser
);
10054 /* If there is a `::' operator or a nested-name-specifier, then we
10055 are definitely looking for a class-name. */
10056 if (global_scope_p
|| nested_name_specifier_p
)
10057 return cp_parser_class_name (parser
,
10058 /*typename_keyword_p=*/true,
10059 /*template_keyword_p=*/template_p
,
10061 /*check_dependency_p=*/true,
10062 /*class_head_p=*/false,
10063 /*is_declaration=*/true);
10064 /* Otherwise, we could also be looking for an ordinary identifier. */
10065 cp_parser_parse_tentatively (parser
);
10066 /* Try a class-name. */
10067 id
= cp_parser_class_name (parser
,
10068 /*typename_keyword_p=*/true,
10069 /*template_keyword_p=*/false,
10071 /*check_dependency_p=*/true,
10072 /*class_head_p=*/false,
10073 /*is_declaration=*/true);
10074 /* If we found one, we're done. */
10075 if (cp_parser_parse_definitely (parser
))
10077 /* Otherwise, look for an ordinary identifier. */
10078 return cp_parser_identifier (parser
);
10081 /* Overloading [gram.over] */
10083 /* Parse an operator-function-id.
10085 operator-function-id:
10088 Returns an IDENTIFIER_NODE for the operator which is a
10089 human-readable spelling of the identifier, e.g., `operator +'. */
10092 cp_parser_operator_function_id (cp_parser
* parser
)
10094 /* Look for the `operator' keyword. */
10095 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "%<operator%>"))
10096 return error_mark_node
;
10097 /* And then the name of the operator itself. */
10098 return cp_parser_operator (parser
);
10101 /* Parse an operator.
10104 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10105 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10106 || ++ -- , ->* -> () []
10113 Returns an IDENTIFIER_NODE for the operator which is a
10114 human-readable spelling of the identifier, e.g., `operator +'. */
10117 cp_parser_operator (cp_parser
* parser
)
10119 tree id
= NULL_TREE
;
10122 /* Peek at the next token. */
10123 token
= cp_lexer_peek_token (parser
->lexer
);
10124 /* Figure out which operator we have. */
10125 switch (token
->type
)
10131 /* The keyword should be either `new' or `delete'. */
10132 if (token
->keyword
== RID_NEW
)
10134 else if (token
->keyword
== RID_DELETE
)
10139 /* Consume the `new' or `delete' token. */
10140 cp_lexer_consume_token (parser
->lexer
);
10142 /* Peek at the next token. */
10143 token
= cp_lexer_peek_token (parser
->lexer
);
10144 /* If it's a `[' token then this is the array variant of the
10146 if (token
->type
== CPP_OPEN_SQUARE
)
10148 /* Consume the `[' token. */
10149 cp_lexer_consume_token (parser
->lexer
);
10150 /* Look for the `]' token. */
10151 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "%<]%>");
10152 id
= ansi_opname (op
== NEW_EXPR
10153 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
10155 /* Otherwise, we have the non-array variant. */
10157 id
= ansi_opname (op
);
10163 id
= ansi_opname (PLUS_EXPR
);
10167 id
= ansi_opname (MINUS_EXPR
);
10171 id
= ansi_opname (MULT_EXPR
);
10175 id
= ansi_opname (TRUNC_DIV_EXPR
);
10179 id
= ansi_opname (TRUNC_MOD_EXPR
);
10183 id
= ansi_opname (BIT_XOR_EXPR
);
10187 id
= ansi_opname (BIT_AND_EXPR
);
10191 id
= ansi_opname (BIT_IOR_EXPR
);
10195 id
= ansi_opname (BIT_NOT_EXPR
);
10199 id
= ansi_opname (TRUTH_NOT_EXPR
);
10203 id
= ansi_assopname (NOP_EXPR
);
10207 id
= ansi_opname (LT_EXPR
);
10211 id
= ansi_opname (GT_EXPR
);
10215 id
= ansi_assopname (PLUS_EXPR
);
10219 id
= ansi_assopname (MINUS_EXPR
);
10223 id
= ansi_assopname (MULT_EXPR
);
10227 id
= ansi_assopname (TRUNC_DIV_EXPR
);
10231 id
= ansi_assopname (TRUNC_MOD_EXPR
);
10235 id
= ansi_assopname (BIT_XOR_EXPR
);
10239 id
= ansi_assopname (BIT_AND_EXPR
);
10243 id
= ansi_assopname (BIT_IOR_EXPR
);
10247 id
= ansi_opname (LSHIFT_EXPR
);
10251 id
= ansi_opname (RSHIFT_EXPR
);
10254 case CPP_LSHIFT_EQ
:
10255 id
= ansi_assopname (LSHIFT_EXPR
);
10258 case CPP_RSHIFT_EQ
:
10259 id
= ansi_assopname (RSHIFT_EXPR
);
10263 id
= ansi_opname (EQ_EXPR
);
10267 id
= ansi_opname (NE_EXPR
);
10271 id
= ansi_opname (LE_EXPR
);
10274 case CPP_GREATER_EQ
:
10275 id
= ansi_opname (GE_EXPR
);
10279 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
10283 id
= ansi_opname (TRUTH_ORIF_EXPR
);
10286 case CPP_PLUS_PLUS
:
10287 id
= ansi_opname (POSTINCREMENT_EXPR
);
10290 case CPP_MINUS_MINUS
:
10291 id
= ansi_opname (PREDECREMENT_EXPR
);
10295 id
= ansi_opname (COMPOUND_EXPR
);
10298 case CPP_DEREF_STAR
:
10299 id
= ansi_opname (MEMBER_REF
);
10303 id
= ansi_opname (COMPONENT_REF
);
10306 case CPP_OPEN_PAREN
:
10307 /* Consume the `('. */
10308 cp_lexer_consume_token (parser
->lexer
);
10309 /* Look for the matching `)'. */
10310 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
10311 return ansi_opname (CALL_EXPR
);
10313 case CPP_OPEN_SQUARE
:
10314 /* Consume the `['. */
10315 cp_lexer_consume_token (parser
->lexer
);
10316 /* Look for the matching `]'. */
10317 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "%<]%>");
10318 return ansi_opname (ARRAY_REF
);
10321 /* Anything else is an error. */
10325 /* If we have selected an identifier, we need to consume the
10328 cp_lexer_consume_token (parser
->lexer
);
10329 /* Otherwise, no valid operator name was present. */
10332 cp_parser_error (parser
, "expected operator");
10333 id
= error_mark_node
;
10339 /* Parse a template-declaration.
10341 template-declaration:
10342 export [opt] template < template-parameter-list > declaration
10344 If MEMBER_P is TRUE, this template-declaration occurs within a
10347 The grammar rule given by the standard isn't correct. What
10348 is really meant is:
10350 template-declaration:
10351 export [opt] template-parameter-list-seq
10352 decl-specifier-seq [opt] init-declarator [opt] ;
10353 export [opt] template-parameter-list-seq
10354 function-definition
10356 template-parameter-list-seq:
10357 template-parameter-list-seq [opt]
10358 template < template-parameter-list > */
10361 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
10363 /* Check for `export'. */
10364 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
10366 /* Consume the `export' token. */
10367 cp_lexer_consume_token (parser
->lexer
);
10368 /* Warn that we do not support `export'. */
10369 warning (0, "keyword %<export%> not implemented, and will be ignored");
10372 cp_parser_template_declaration_after_export (parser
, member_p
);
10375 /* Parse a template-parameter-list.
10377 template-parameter-list:
10379 template-parameter-list , template-parameter
10381 Returns a TREE_LIST. Each node represents a template parameter.
10382 The nodes are connected via their TREE_CHAINs. */
10385 cp_parser_template_parameter_list (cp_parser
* parser
)
10387 tree parameter_list
= NULL_TREE
;
10389 begin_template_parm_list ();
10394 bool is_parameter_pack
;
10395 location_t parm_loc
;
10397 /* Parse the template-parameter. */
10398 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10399 parameter
= cp_parser_template_parameter (parser
,
10401 &is_parameter_pack
);
10402 /* Add it to the list. */
10403 if (parameter
!= error_mark_node
)
10404 parameter_list
= process_template_parm (parameter_list
,
10408 is_parameter_pack
);
10411 tree err_parm
= build_tree_list (parameter
, parameter
);
10412 TREE_VALUE (err_parm
) = error_mark_node
;
10413 parameter_list
= chainon (parameter_list
, err_parm
);
10416 /* If the next token is not a `,', we're done. */
10417 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
10419 /* Otherwise, consume the `,' token. */
10420 cp_lexer_consume_token (parser
->lexer
);
10423 return end_template_parm_list (parameter_list
);
10426 /* Parse a template-parameter.
10428 template-parameter:
10430 parameter-declaration
10432 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10433 the parameter. The TREE_PURPOSE is the default value, if any.
10434 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10435 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10436 set to true iff this parameter is a parameter pack. */
10439 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
10440 bool *is_parameter_pack
)
10443 cp_parameter_declarator
*parameter_declarator
;
10444 cp_declarator
*id_declarator
;
10447 /* Assume it is a type parameter or a template parameter. */
10448 *is_non_type
= false;
10449 /* Assume it not a parameter pack. */
10450 *is_parameter_pack
= false;
10451 /* Peek at the next token. */
10452 token
= cp_lexer_peek_token (parser
->lexer
);
10453 /* If it is `class' or `template', we have a type-parameter. */
10454 if (token
->keyword
== RID_TEMPLATE
)
10455 return cp_parser_type_parameter (parser
, is_parameter_pack
);
10456 /* If it is `class' or `typename' we do not know yet whether it is a
10457 type parameter or a non-type parameter. Consider:
10459 template <typename T, typename T::X X> ...
10463 template <class C, class D*> ...
10465 Here, the first parameter is a type parameter, and the second is
10466 a non-type parameter. We can tell by looking at the token after
10467 the identifier -- if it is a `,', `=', or `>' then we have a type
10469 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
10471 /* Peek at the token after `class' or `typename'. */
10472 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10473 /* If it's an ellipsis, we have a template type parameter
10475 if (token
->type
== CPP_ELLIPSIS
)
10476 return cp_parser_type_parameter (parser
, is_parameter_pack
);
10477 /* If it's an identifier, skip it. */
10478 if (token
->type
== CPP_NAME
)
10479 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
10480 /* Now, see if the token looks like the end of a template
10482 if (token
->type
== CPP_COMMA
10483 || token
->type
== CPP_EQ
10484 || token
->type
== CPP_GREATER
)
10485 return cp_parser_type_parameter (parser
, is_parameter_pack
);
10488 /* Otherwise, it is a non-type parameter.
10492 When parsing a default template-argument for a non-type
10493 template-parameter, the first non-nested `>' is taken as the end
10494 of the template parameter-list rather than a greater-than
10496 *is_non_type
= true;
10497 parameter_declarator
10498 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
10499 /*parenthesized_p=*/NULL
);
10501 /* If the parameter declaration is marked as a parameter pack, set
10502 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10503 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10505 if (parameter_declarator
10506 && parameter_declarator
->declarator
10507 && parameter_declarator
->declarator
->parameter_pack_p
)
10509 *is_parameter_pack
= true;
10510 parameter_declarator
->declarator
->parameter_pack_p
= false;
10513 /* If the next token is an ellipsis, and we don't already have it
10514 marked as a parameter pack, then we have a parameter pack (that
10515 has no declarator). */
10516 if (!*is_parameter_pack
10517 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
10518 && declarator_can_be_parameter_pack (parameter_declarator
->declarator
))
10520 /* Consume the `...'. */
10521 cp_lexer_consume_token (parser
->lexer
);
10522 maybe_warn_variadic_templates ();
10524 *is_parameter_pack
= true;
10526 /* We might end up with a pack expansion as the type of the non-type
10527 template parameter, in which case this is a non-type template
10529 else if (parameter_declarator
10530 && parameter_declarator
->decl_specifiers
.type
10531 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
10533 *is_parameter_pack
= true;
10534 parameter_declarator
->decl_specifiers
.type
=
10535 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
10538 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10540 /* Parameter packs cannot have default arguments. However, a
10541 user may try to do so, so we'll parse them and give an
10542 appropriate diagnostic here. */
10544 /* Consume the `='. */
10545 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
10546 cp_lexer_consume_token (parser
->lexer
);
10548 /* Find the name of the parameter pack. */
10549 id_declarator
= parameter_declarator
->declarator
;
10550 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
10551 id_declarator
= id_declarator
->declarator
;
10553 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
10554 error_at (start_token
->location
,
10555 "template parameter pack %qD cannot have a default argument",
10556 id_declarator
->u
.id
.unqualified_name
);
10558 error_at (start_token
->location
,
10559 "template parameter pack cannot have a default argument");
10561 /* Parse the default argument, but throw away the result. */
10562 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
10565 parm
= grokdeclarator (parameter_declarator
->declarator
,
10566 ¶meter_declarator
->decl_specifiers
,
10567 TPARM
, /*initialized=*/0,
10568 /*attrlist=*/NULL
);
10569 if (parm
== error_mark_node
)
10570 return error_mark_node
;
10572 return build_tree_list (parameter_declarator
->default_argument
, parm
);
10575 /* Parse a type-parameter.
10578 class identifier [opt]
10579 class identifier [opt] = type-id
10580 typename identifier [opt]
10581 typename identifier [opt] = type-id
10582 template < template-parameter-list > class identifier [opt]
10583 template < template-parameter-list > class identifier [opt]
10586 GNU Extension (variadic templates):
10589 class ... identifier [opt]
10590 typename ... identifier [opt]
10592 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10593 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10594 the declaration of the parameter.
10596 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10599 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
10604 /* Look for a keyword to tell us what kind of parameter this is. */
10605 token
= cp_parser_require (parser
, CPP_KEYWORD
,
10606 "%<class%>, %<typename%>, or %<template%>");
10608 return error_mark_node
;
10610 switch (token
->keyword
)
10616 tree default_argument
;
10618 /* If the next token is an ellipsis, we have a template
10620 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10622 /* Consume the `...' token. */
10623 cp_lexer_consume_token (parser
->lexer
);
10624 maybe_warn_variadic_templates ();
10626 *is_parameter_pack
= true;
10629 /* If the next token is an identifier, then it names the
10631 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10632 identifier
= cp_parser_identifier (parser
);
10634 identifier
= NULL_TREE
;
10636 /* Create the parameter. */
10637 parameter
= finish_template_type_parm (class_type_node
, identifier
);
10639 /* If the next token is an `=', we have a default argument. */
10640 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10642 /* Consume the `=' token. */
10643 cp_lexer_consume_token (parser
->lexer
);
10644 /* Parse the default-argument. */
10645 push_deferring_access_checks (dk_no_deferred
);
10646 default_argument
= cp_parser_type_id (parser
);
10648 /* Template parameter packs cannot have default
10650 if (*is_parameter_pack
)
10653 error_at (token
->location
,
10654 "template parameter pack %qD cannot have a "
10655 "default argument", identifier
);
10657 error_at (token
->location
,
10658 "template parameter packs cannot have "
10659 "default arguments");
10660 default_argument
= NULL_TREE
;
10662 pop_deferring_access_checks ();
10665 default_argument
= NULL_TREE
;
10667 /* Create the combined representation of the parameter and the
10668 default argument. */
10669 parameter
= build_tree_list (default_argument
, parameter
);
10676 tree default_argument
;
10678 /* Look for the `<'. */
10679 cp_parser_require (parser
, CPP_LESS
, "%<<%>");
10680 /* Parse the template-parameter-list. */
10681 cp_parser_template_parameter_list (parser
);
10682 /* Look for the `>'. */
10683 cp_parser_require (parser
, CPP_GREATER
, "%<>%>");
10684 /* Look for the `class' keyword. */
10685 cp_parser_require_keyword (parser
, RID_CLASS
, "%<class%>");
10686 /* If the next token is an ellipsis, we have a template
10688 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10690 /* Consume the `...' token. */
10691 cp_lexer_consume_token (parser
->lexer
);
10692 maybe_warn_variadic_templates ();
10694 *is_parameter_pack
= true;
10696 /* If the next token is an `=', then there is a
10697 default-argument. If the next token is a `>', we are at
10698 the end of the parameter-list. If the next token is a `,',
10699 then we are at the end of this parameter. */
10700 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
10701 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
10702 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
10704 identifier
= cp_parser_identifier (parser
);
10705 /* Treat invalid names as if the parameter were nameless. */
10706 if (identifier
== error_mark_node
)
10707 identifier
= NULL_TREE
;
10710 identifier
= NULL_TREE
;
10712 /* Create the template parameter. */
10713 parameter
= finish_template_template_parm (class_type_node
,
10716 /* If the next token is an `=', then there is a
10717 default-argument. */
10718 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10722 /* Consume the `='. */
10723 cp_lexer_consume_token (parser
->lexer
);
10724 /* Parse the id-expression. */
10725 push_deferring_access_checks (dk_no_deferred
);
10726 /* save token before parsing the id-expression, for error
10728 token
= cp_lexer_peek_token (parser
->lexer
);
10730 = cp_parser_id_expression (parser
,
10731 /*template_keyword_p=*/false,
10732 /*check_dependency_p=*/true,
10733 /*template_p=*/&is_template
,
10734 /*declarator_p=*/false,
10735 /*optional_p=*/false);
10736 if (TREE_CODE (default_argument
) == TYPE_DECL
)
10737 /* If the id-expression was a template-id that refers to
10738 a template-class, we already have the declaration here,
10739 so no further lookup is needed. */
10742 /* Look up the name. */
10744 = cp_parser_lookup_name (parser
, default_argument
,
10746 /*is_template=*/is_template
,
10747 /*is_namespace=*/false,
10748 /*check_dependency=*/true,
10749 /*ambiguous_decls=*/NULL
,
10751 /* See if the default argument is valid. */
10753 = check_template_template_default_arg (default_argument
);
10755 /* Template parameter packs cannot have default
10757 if (*is_parameter_pack
)
10760 error_at (token
->location
,
10761 "template parameter pack %qD cannot "
10762 "have a default argument",
10765 error_at (token
->location
, "template parameter packs cannot "
10766 "have default arguments");
10767 default_argument
= NULL_TREE
;
10769 pop_deferring_access_checks ();
10772 default_argument
= NULL_TREE
;
10774 /* Create the combined representation of the parameter and the
10775 default argument. */
10776 parameter
= build_tree_list (default_argument
, parameter
);
10781 gcc_unreachable ();
10788 /* Parse a template-id.
10791 template-name < template-argument-list [opt] >
10793 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10794 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10795 returned. Otherwise, if the template-name names a function, or set
10796 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10797 names a class, returns a TYPE_DECL for the specialization.
10799 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10800 uninstantiated templates. */
10803 cp_parser_template_id (cp_parser
*parser
,
10804 bool template_keyword_p
,
10805 bool check_dependency_p
,
10806 bool is_declaration
)
10812 cp_token_position start_of_id
= 0;
10813 deferred_access_check
*chk
;
10814 VEC (deferred_access_check
,gc
) *access_check
;
10815 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
10816 bool is_identifier
;
10818 /* If the next token corresponds to a template-id, there is no need
10820 next_token
= cp_lexer_peek_token (parser
->lexer
);
10821 if (next_token
->type
== CPP_TEMPLATE_ID
)
10823 struct tree_check
*check_value
;
10825 /* Get the stored value. */
10826 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
10827 /* Perform any access checks that were deferred. */
10828 access_check
= check_value
->checks
;
10832 VEC_iterate (deferred_access_check
, access_check
, i
, chk
) ;
10835 perform_or_defer_access_check (chk
->binfo
,
10840 /* Return the stored value. */
10841 return check_value
->value
;
10844 /* Avoid performing name lookup if there is no possibility of
10845 finding a template-id. */
10846 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
10847 || (next_token
->type
== CPP_NAME
10848 && !cp_parser_nth_token_starts_template_argument_list_p
10851 cp_parser_error (parser
, "expected template-id");
10852 return error_mark_node
;
10855 /* Remember where the template-id starts. */
10856 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
10857 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
10859 push_deferring_access_checks (dk_deferred
);
10861 /* Parse the template-name. */
10862 is_identifier
= false;
10863 templ
= cp_parser_template_name (parser
, template_keyword_p
,
10864 check_dependency_p
,
10867 if (templ
== error_mark_node
|| is_identifier
)
10869 pop_deferring_access_checks ();
10873 /* If we find the sequence `[:' after a template-name, it's probably
10874 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10875 parse correctly the argument list. */
10876 next_token
= cp_lexer_peek_token (parser
->lexer
);
10877 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10878 if (next_token
->type
== CPP_OPEN_SQUARE
10879 && next_token
->flags
& DIGRAPH
10880 && next_token_2
->type
== CPP_COLON
10881 && !(next_token_2
->flags
& PREV_WHITE
))
10883 cp_parser_parse_tentatively (parser
);
10884 /* Change `:' into `::'. */
10885 next_token_2
->type
= CPP_SCOPE
;
10886 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10888 cp_lexer_consume_token (parser
->lexer
);
10890 /* Parse the arguments. */
10891 arguments
= cp_parser_enclosed_template_argument_list (parser
);
10892 if (!cp_parser_parse_definitely (parser
))
10894 /* If we couldn't parse an argument list, then we revert our changes
10895 and return simply an error. Maybe this is not a template-id
10897 next_token_2
->type
= CPP_COLON
;
10898 cp_parser_error (parser
, "expected %<<%>");
10899 pop_deferring_access_checks ();
10900 return error_mark_node
;
10902 /* Otherwise, emit an error about the invalid digraph, but continue
10903 parsing because we got our argument list. */
10904 if (permerror (next_token
->location
,
10905 "%<<::%> cannot begin a template-argument list"))
10907 static bool hint
= false;
10908 inform (next_token
->location
,
10909 "%<<:%> is an alternate spelling for %<[%>."
10910 " Insert whitespace between %<<%> and %<::%>");
10911 if (!hint
&& !flag_permissive
)
10913 inform (next_token
->location
, "(if you use %<-fpermissive%>"
10914 " G++ will accept your code)");
10921 /* Look for the `<' that starts the template-argument-list. */
10922 if (!cp_parser_require (parser
, CPP_LESS
, "%<<%>"))
10924 pop_deferring_access_checks ();
10925 return error_mark_node
;
10927 /* Parse the arguments. */
10928 arguments
= cp_parser_enclosed_template_argument_list (parser
);
10931 /* Build a representation of the specialization. */
10932 if (TREE_CODE (templ
) == IDENTIFIER_NODE
)
10933 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, templ
, arguments
);
10934 else if (DECL_CLASS_TEMPLATE_P (templ
)
10935 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
10937 bool entering_scope
;
10938 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10939 template (rather than some instantiation thereof) only if
10940 is not nested within some other construct. For example, in
10941 "template <typename T> void f(T) { A<T>::", A<T> is just an
10942 instantiation of A. */
10943 entering_scope
= (template_parm_scope_p ()
10944 && cp_lexer_next_token_is (parser
->lexer
,
10947 = finish_template_type (templ
, arguments
, entering_scope
);
10951 /* If it's not a class-template or a template-template, it should be
10952 a function-template. */
10953 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
10954 || TREE_CODE (templ
) == OVERLOAD
10955 || BASELINK_P (templ
)));
10957 template_id
= lookup_template_function (templ
, arguments
);
10960 /* If parsing tentatively, replace the sequence of tokens that makes
10961 up the template-id with a CPP_TEMPLATE_ID token. That way,
10962 should we re-parse the token stream, we will not have to repeat
10963 the effort required to do the parse, nor will we issue duplicate
10964 error messages about problems during instantiation of the
10968 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
10970 /* Reset the contents of the START_OF_ID token. */
10971 token
->type
= CPP_TEMPLATE_ID
;
10972 /* Retrieve any deferred checks. Do not pop this access checks yet
10973 so the memory will not be reclaimed during token replacing below. */
10974 token
->u
.tree_check_value
= GGC_CNEW (struct tree_check
);
10975 token
->u
.tree_check_value
->value
= template_id
;
10976 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
10977 token
->keyword
= RID_MAX
;
10979 /* Purge all subsequent tokens. */
10980 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
10982 /* ??? Can we actually assume that, if template_id ==
10983 error_mark_node, we will have issued a diagnostic to the
10984 user, as opposed to simply marking the tentative parse as
10986 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
10987 error_at (token
->location
, "parse error in template argument list");
10990 pop_deferring_access_checks ();
10991 return template_id
;
10994 /* Parse a template-name.
10999 The standard should actually say:
11003 operator-function-id
11005 A defect report has been filed about this issue.
11007 A conversion-function-id cannot be a template name because they cannot
11008 be part of a template-id. In fact, looking at this code:
11010 a.operator K<int>()
11012 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11013 It is impossible to call a templated conversion-function-id with an
11014 explicit argument list, since the only allowed template parameter is
11015 the type to which it is converting.
11017 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11018 `template' keyword, in a construction like:
11022 In that case `f' is taken to be a template-name, even though there
11023 is no way of knowing for sure.
11025 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11026 name refers to a set of overloaded functions, at least one of which
11027 is a template, or an IDENTIFIER_NODE with the name of the template,
11028 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11029 names are looked up inside uninstantiated templates. */
11032 cp_parser_template_name (cp_parser
* parser
,
11033 bool template_keyword_p
,
11034 bool check_dependency_p
,
11035 bool is_declaration
,
11036 bool *is_identifier
)
11041 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11043 /* If the next token is `operator', then we have either an
11044 operator-function-id or a conversion-function-id. */
11045 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
11047 /* We don't know whether we're looking at an
11048 operator-function-id or a conversion-function-id. */
11049 cp_parser_parse_tentatively (parser
);
11050 /* Try an operator-function-id. */
11051 identifier
= cp_parser_operator_function_id (parser
);
11052 /* If that didn't work, try a conversion-function-id. */
11053 if (!cp_parser_parse_definitely (parser
))
11055 cp_parser_error (parser
, "expected template-name");
11056 return error_mark_node
;
11059 /* Look for the identifier. */
11061 identifier
= cp_parser_identifier (parser
);
11063 /* If we didn't find an identifier, we don't have a template-id. */
11064 if (identifier
== error_mark_node
)
11065 return error_mark_node
;
11067 /* If the name immediately followed the `template' keyword, then it
11068 is a template-name. However, if the next token is not `<', then
11069 we do not treat it as a template-name, since it is not being used
11070 as part of a template-id. This enables us to handle constructs
11073 template <typename T> struct S { S(); };
11074 template <typename T> S<T>::S();
11076 correctly. We would treat `S' as a template -- if it were `S<T>'
11077 -- but we do not if there is no `<'. */
11079 if (processing_template_decl
11080 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
11082 /* In a declaration, in a dependent context, we pretend that the
11083 "template" keyword was present in order to improve error
11084 recovery. For example, given:
11086 template <typename T> void f(T::X<int>);
11088 we want to treat "X<int>" as a template-id. */
11090 && !template_keyword_p
11091 && parser
->scope
&& TYPE_P (parser
->scope
)
11092 && check_dependency_p
11093 && dependent_scope_p (parser
->scope
)
11094 /* Do not do this for dtors (or ctors), since they never
11095 need the template keyword before their name. */
11096 && !constructor_name_p (identifier
, parser
->scope
))
11098 cp_token_position start
= 0;
11100 /* Explain what went wrong. */
11101 error_at (token
->location
, "non-template %qD used as template",
11103 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
11104 parser
->scope
, identifier
);
11105 /* If parsing tentatively, find the location of the "<" token. */
11106 if (cp_parser_simulate_error (parser
))
11107 start
= cp_lexer_token_position (parser
->lexer
, true);
11108 /* Parse the template arguments so that we can issue error
11109 messages about them. */
11110 cp_lexer_consume_token (parser
->lexer
);
11111 cp_parser_enclosed_template_argument_list (parser
);
11112 /* Skip tokens until we find a good place from which to
11113 continue parsing. */
11114 cp_parser_skip_to_closing_parenthesis (parser
,
11115 /*recovering=*/true,
11117 /*consume_paren=*/false);
11118 /* If parsing tentatively, permanently remove the
11119 template argument list. That will prevent duplicate
11120 error messages from being issued about the missing
11121 "template" keyword. */
11123 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
11125 *is_identifier
= true;
11129 /* If the "template" keyword is present, then there is generally
11130 no point in doing name-lookup, so we just return IDENTIFIER.
11131 But, if the qualifying scope is non-dependent then we can
11132 (and must) do name-lookup normally. */
11133 if (template_keyword_p
11135 || (TYPE_P (parser
->scope
)
11136 && dependent_type_p (parser
->scope
))))
11140 /* Look up the name. */
11141 decl
= cp_parser_lookup_name (parser
, identifier
,
11143 /*is_template=*/true,
11144 /*is_namespace=*/false,
11145 check_dependency_p
,
11146 /*ambiguous_decls=*/NULL
,
11149 /* If DECL is a template, then the name was a template-name. */
11150 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
11154 tree fn
= NULL_TREE
;
11156 /* The standard does not explicitly indicate whether a name that
11157 names a set of overloaded declarations, some of which are
11158 templates, is a template-name. However, such a name should
11159 be a template-name; otherwise, there is no way to form a
11160 template-id for the overloaded templates. */
11161 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
11162 if (TREE_CODE (fns
) == OVERLOAD
)
11163 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
11164 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
11169 /* The name does not name a template. */
11170 cp_parser_error (parser
, "expected template-name");
11171 return error_mark_node
;
11175 /* If DECL is dependent, and refers to a function, then just return
11176 its name; we will look it up again during template instantiation. */
11177 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
11179 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
11180 if (TYPE_P (scope
) && dependent_type_p (scope
))
11187 /* Parse a template-argument-list.
11189 template-argument-list:
11190 template-argument ... [opt]
11191 template-argument-list , template-argument ... [opt]
11193 Returns a TREE_VEC containing the arguments. */
11196 cp_parser_template_argument_list (cp_parser
* parser
)
11198 tree fixed_args
[10];
11199 unsigned n_args
= 0;
11200 unsigned alloced
= 10;
11201 tree
*arg_ary
= fixed_args
;
11203 bool saved_in_template_argument_list_p
;
11205 bool saved_non_ice_p
;
11207 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
11208 parser
->in_template_argument_list_p
= true;
11209 /* Even if the template-id appears in an integral
11210 constant-expression, the contents of the argument list do
11212 saved_ice_p
= parser
->integral_constant_expression_p
;
11213 parser
->integral_constant_expression_p
= false;
11214 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
11215 parser
->non_integral_constant_expression_p
= false;
11216 /* Parse the arguments. */
11222 /* Consume the comma. */
11223 cp_lexer_consume_token (parser
->lexer
);
11225 /* Parse the template-argument. */
11226 argument
= cp_parser_template_argument (parser
);
11228 /* If the next token is an ellipsis, we're expanding a template
11230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11232 if (argument
== error_mark_node
)
11234 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11235 error_at (token
->location
,
11236 "expected parameter pack before %<...%>");
11238 /* Consume the `...' token. */
11239 cp_lexer_consume_token (parser
->lexer
);
11241 /* Make the argument into a TYPE_PACK_EXPANSION or
11242 EXPR_PACK_EXPANSION. */
11243 argument
= make_pack_expansion (argument
);
11246 if (n_args
== alloced
)
11250 if (arg_ary
== fixed_args
)
11252 arg_ary
= XNEWVEC (tree
, alloced
);
11253 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
11256 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
11258 arg_ary
[n_args
++] = argument
;
11260 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
11262 vec
= make_tree_vec (n_args
);
11265 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
11267 if (arg_ary
!= fixed_args
)
11269 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
11270 parser
->integral_constant_expression_p
= saved_ice_p
;
11271 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
11272 #ifdef ENABLE_CHECKING
11273 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
11278 /* Parse a template-argument.
11281 assignment-expression
11285 The representation is that of an assignment-expression, type-id, or
11286 id-expression -- except that the qualified id-expression is
11287 evaluated, so that the value returned is either a DECL or an
11290 Although the standard says "assignment-expression", it forbids
11291 throw-expressions or assignments in the template argument.
11292 Therefore, we use "conditional-expression" instead. */
11295 cp_parser_template_argument (cp_parser
* parser
)
11300 bool maybe_type_id
= false;
11301 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
11304 /* There's really no way to know what we're looking at, so we just
11305 try each alternative in order.
11309 In a template-argument, an ambiguity between a type-id and an
11310 expression is resolved to a type-id, regardless of the form of
11311 the corresponding template-parameter.
11313 Therefore, we try a type-id first. */
11314 cp_parser_parse_tentatively (parser
);
11315 argument
= cp_parser_template_type_arg (parser
);
11316 /* If there was no error parsing the type-id but the next token is a
11317 '>>', our behavior depends on which dialect of C++ we're
11318 parsing. In C++98, we probably found a typo for '> >'. But there
11319 are type-id which are also valid expressions. For instance:
11321 struct X { int operator >> (int); };
11322 template <int V> struct Foo {};
11325 Here 'X()' is a valid type-id of a function type, but the user just
11326 wanted to write the expression "X() >> 5". Thus, we remember that we
11327 found a valid type-id, but we still try to parse the argument as an
11328 expression to see what happens.
11330 In C++0x, the '>>' will be considered two separate '>'
11332 if (!cp_parser_error_occurred (parser
)
11333 && cxx_dialect
== cxx98
11334 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
11336 maybe_type_id
= true;
11337 cp_parser_abort_tentative_parse (parser
);
11341 /* If the next token isn't a `,' or a `>', then this argument wasn't
11342 really finished. This means that the argument is not a valid
11344 if (!cp_parser_next_token_ends_template_argument_p (parser
))
11345 cp_parser_error (parser
, "expected template-argument");
11346 /* If that worked, we're done. */
11347 if (cp_parser_parse_definitely (parser
))
11350 /* We're still not sure what the argument will be. */
11351 cp_parser_parse_tentatively (parser
);
11352 /* Try a template. */
11353 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
11354 argument
= cp_parser_id_expression (parser
,
11355 /*template_keyword_p=*/false,
11356 /*check_dependency_p=*/true,
11358 /*declarator_p=*/false,
11359 /*optional_p=*/false);
11360 /* If the next token isn't a `,' or a `>', then this argument wasn't
11361 really finished. */
11362 if (!cp_parser_next_token_ends_template_argument_p (parser
))
11363 cp_parser_error (parser
, "expected template-argument");
11364 if (!cp_parser_error_occurred (parser
))
11366 /* Figure out what is being referred to. If the id-expression
11367 was for a class template specialization, then we will have a
11368 TYPE_DECL at this point. There is no need to do name lookup
11369 at this point in that case. */
11370 if (TREE_CODE (argument
) != TYPE_DECL
)
11371 argument
= cp_parser_lookup_name (parser
, argument
,
11373 /*is_template=*/template_p
,
11374 /*is_namespace=*/false,
11375 /*check_dependency=*/true,
11376 /*ambiguous_decls=*/NULL
,
11377 argument_start_token
->location
);
11378 if (TREE_CODE (argument
) != TEMPLATE_DECL
11379 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
11380 cp_parser_error (parser
, "expected template-name");
11382 if (cp_parser_parse_definitely (parser
))
11384 /* It must be a non-type argument. There permitted cases are given
11385 in [temp.arg.nontype]:
11387 -- an integral constant-expression of integral or enumeration
11390 -- the name of a non-type template-parameter; or
11392 -- the name of an object or function with external linkage...
11394 -- the address of an object or function with external linkage...
11396 -- a pointer to member... */
11397 /* Look for a non-type template parameter. */
11398 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11400 cp_parser_parse_tentatively (parser
);
11401 argument
= cp_parser_primary_expression (parser
,
11402 /*address_p=*/false,
11404 /*template_arg_p=*/true,
11406 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
11407 || !cp_parser_next_token_ends_template_argument_p (parser
))
11408 cp_parser_simulate_error (parser
);
11409 if (cp_parser_parse_definitely (parser
))
11413 /* If the next token is "&", the argument must be the address of an
11414 object or function with external linkage. */
11415 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
11417 cp_lexer_consume_token (parser
->lexer
);
11418 /* See if we might have an id-expression. */
11419 token
= cp_lexer_peek_token (parser
->lexer
);
11420 if (token
->type
== CPP_NAME
11421 || token
->keyword
== RID_OPERATOR
11422 || token
->type
== CPP_SCOPE
11423 || token
->type
== CPP_TEMPLATE_ID
11424 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
11426 cp_parser_parse_tentatively (parser
);
11427 argument
= cp_parser_primary_expression (parser
,
11430 /*template_arg_p=*/true,
11432 if (cp_parser_error_occurred (parser
)
11433 || !cp_parser_next_token_ends_template_argument_p (parser
))
11434 cp_parser_abort_tentative_parse (parser
);
11439 if (TREE_CODE (argument
) == INDIRECT_REF
)
11441 gcc_assert (REFERENCE_REF_P (argument
));
11442 argument
= TREE_OPERAND (argument
, 0);
11445 /* If we're in a template, we represent a qualified-id referring
11446 to a static data member as a SCOPE_REF even if the scope isn't
11447 dependent so that we can check access control later. */
11449 if (TREE_CODE (probe
) == SCOPE_REF
)
11450 probe
= TREE_OPERAND (probe
, 1);
11451 if (TREE_CODE (probe
) == VAR_DECL
)
11453 /* A variable without external linkage might still be a
11454 valid constant-expression, so no error is issued here
11455 if the external-linkage check fails. */
11456 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
11457 cp_parser_simulate_error (parser
);
11459 else if (is_overloaded_fn (argument
))
11460 /* All overloaded functions are allowed; if the external
11461 linkage test does not pass, an error will be issued
11465 && (TREE_CODE (argument
) == OFFSET_REF
11466 || TREE_CODE (argument
) == SCOPE_REF
))
11467 /* A pointer-to-member. */
11469 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
11472 cp_parser_simulate_error (parser
);
11474 if (cp_parser_parse_definitely (parser
))
11477 argument
= build_x_unary_op (ADDR_EXPR
, argument
,
11478 tf_warning_or_error
);
11483 /* If the argument started with "&", there are no other valid
11484 alternatives at this point. */
11487 cp_parser_error (parser
, "invalid non-type template argument");
11488 return error_mark_node
;
11491 /* If the argument wasn't successfully parsed as a type-id followed
11492 by '>>', the argument can only be a constant expression now.
11493 Otherwise, we try parsing the constant-expression tentatively,
11494 because the argument could really be a type-id. */
11496 cp_parser_parse_tentatively (parser
);
11497 argument
= cp_parser_constant_expression (parser
,
11498 /*allow_non_constant_p=*/false,
11499 /*non_constant_p=*/NULL
);
11500 argument
= fold_non_dependent_expr (argument
);
11501 if (!maybe_type_id
)
11503 if (!cp_parser_next_token_ends_template_argument_p (parser
))
11504 cp_parser_error (parser
, "expected template-argument");
11505 if (cp_parser_parse_definitely (parser
))
11507 /* We did our best to parse the argument as a non type-id, but that
11508 was the only alternative that matched (albeit with a '>' after
11509 it). We can assume it's just a typo from the user, and a
11510 diagnostic will then be issued. */
11511 return cp_parser_template_type_arg (parser
);
11514 /* Parse an explicit-instantiation.
11516 explicit-instantiation:
11517 template declaration
11519 Although the standard says `declaration', what it really means is:
11521 explicit-instantiation:
11522 template decl-specifier-seq [opt] declarator [opt] ;
11524 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11525 supposed to be allowed. A defect report has been filed about this
11530 explicit-instantiation:
11531 storage-class-specifier template
11532 decl-specifier-seq [opt] declarator [opt] ;
11533 function-specifier template
11534 decl-specifier-seq [opt] declarator [opt] ; */
11537 cp_parser_explicit_instantiation (cp_parser
* parser
)
11539 int declares_class_or_enum
;
11540 cp_decl_specifier_seq decl_specifiers
;
11541 tree extension_specifier
= NULL_TREE
;
11543 /* Look for an (optional) storage-class-specifier or
11544 function-specifier. */
11545 if (cp_parser_allow_gnu_extensions_p (parser
))
11547 extension_specifier
11548 = cp_parser_storage_class_specifier_opt (parser
);
11549 if (!extension_specifier
)
11550 extension_specifier
11551 = cp_parser_function_specifier_opt (parser
,
11552 /*decl_specs=*/NULL
);
11555 /* Look for the `template' keyword. */
11556 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "%<template%>");
11557 /* Let the front end know that we are processing an explicit
11559 begin_explicit_instantiation ();
11560 /* [temp.explicit] says that we are supposed to ignore access
11561 control while processing explicit instantiation directives. */
11562 push_deferring_access_checks (dk_no_check
);
11563 /* Parse a decl-specifier-seq. */
11564 cp_parser_decl_specifier_seq (parser
,
11565 CP_PARSER_FLAGS_OPTIONAL
,
11567 &declares_class_or_enum
);
11568 /* If there was exactly one decl-specifier, and it declared a class,
11569 and there's no declarator, then we have an explicit type
11571 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
11575 type
= check_tag_decl (&decl_specifiers
);
11576 /* Turn access control back on for names used during
11577 template instantiation. */
11578 pop_deferring_access_checks ();
11580 do_type_instantiation (type
, extension_specifier
,
11581 /*complain=*/tf_error
);
11585 cp_declarator
*declarator
;
11588 /* Parse the declarator. */
11590 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11591 /*ctor_dtor_or_conv_p=*/NULL
,
11592 /*parenthesized_p=*/NULL
,
11593 /*member_p=*/false);
11594 if (declares_class_or_enum
& 2)
11595 cp_parser_check_for_definition_in_return_type (declarator
,
11596 decl_specifiers
.type
,
11597 decl_specifiers
.type_location
);
11598 if (declarator
!= cp_error_declarator
)
11600 decl
= grokdeclarator (declarator
, &decl_specifiers
,
11601 NORMAL
, 0, &decl_specifiers
.attributes
);
11602 /* Turn access control back on for names used during
11603 template instantiation. */
11604 pop_deferring_access_checks ();
11605 /* Do the explicit instantiation. */
11606 do_decl_instantiation (decl
, extension_specifier
);
11610 pop_deferring_access_checks ();
11611 /* Skip the body of the explicit instantiation. */
11612 cp_parser_skip_to_end_of_statement (parser
);
11615 /* We're done with the instantiation. */
11616 end_explicit_instantiation ();
11618 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11621 /* Parse an explicit-specialization.
11623 explicit-specialization:
11624 template < > declaration
11626 Although the standard says `declaration', what it really means is:
11628 explicit-specialization:
11629 template <> decl-specifier [opt] init-declarator [opt] ;
11630 template <> function-definition
11631 template <> explicit-specialization
11632 template <> template-declaration */
11635 cp_parser_explicit_specialization (cp_parser
* parser
)
11637 bool need_lang_pop
;
11638 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11640 /* Look for the `template' keyword. */
11641 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "%<template%>");
11642 /* Look for the `<'. */
11643 cp_parser_require (parser
, CPP_LESS
, "%<<%>");
11644 /* Look for the `>'. */
11645 cp_parser_require (parser
, CPP_GREATER
, "%<>%>");
11646 /* We have processed another parameter list. */
11647 ++parser
->num_template_parameter_lists
;
11650 A template ... explicit specialization ... shall not have C
11652 if (current_lang_name
== lang_name_c
)
11654 error_at (token
->location
, "template specialization with C linkage");
11655 /* Give it C++ linkage to avoid confusing other parts of the
11657 push_lang_context (lang_name_cplusplus
);
11658 need_lang_pop
= true;
11661 need_lang_pop
= false;
11662 /* Let the front end know that we are beginning a specialization. */
11663 if (!begin_specialization ())
11665 end_specialization ();
11669 /* If the next keyword is `template', we need to figure out whether
11670 or not we're looking a template-declaration. */
11671 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
11673 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
11674 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
11675 cp_parser_template_declaration_after_export (parser
,
11676 /*member_p=*/false);
11678 cp_parser_explicit_specialization (parser
);
11681 /* Parse the dependent declaration. */
11682 cp_parser_single_declaration (parser
,
11684 /*member_p=*/false,
11685 /*explicit_specialization_p=*/true,
11686 /*friend_p=*/NULL
);
11687 /* We're done with the specialization. */
11688 end_specialization ();
11689 /* For the erroneous case of a template with C linkage, we pushed an
11690 implicit C++ linkage scope; exit that scope now. */
11692 pop_lang_context ();
11693 /* We're done with this parameter list. */
11694 --parser
->num_template_parameter_lists
;
11697 /* Parse a type-specifier.
11700 simple-type-specifier
11703 elaborated-type-specifier
11711 Returns a representation of the type-specifier. For a
11712 class-specifier, enum-specifier, or elaborated-type-specifier, a
11713 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11715 The parser flags FLAGS is used to control type-specifier parsing.
11717 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11718 in a decl-specifier-seq.
11720 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11721 class-specifier, enum-specifier, or elaborated-type-specifier, then
11722 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11723 if a type is declared; 2 if it is defined. Otherwise, it is set to
11726 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11727 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11728 is set to FALSE. */
11731 cp_parser_type_specifier (cp_parser
* parser
,
11732 cp_parser_flags flags
,
11733 cp_decl_specifier_seq
*decl_specs
,
11734 bool is_declaration
,
11735 int* declares_class_or_enum
,
11736 bool* is_cv_qualifier
)
11738 tree type_spec
= NULL_TREE
;
11741 cp_decl_spec ds
= ds_last
;
11743 /* Assume this type-specifier does not declare a new type. */
11744 if (declares_class_or_enum
)
11745 *declares_class_or_enum
= 0;
11746 /* And that it does not specify a cv-qualifier. */
11747 if (is_cv_qualifier
)
11748 *is_cv_qualifier
= false;
11749 /* Peek at the next token. */
11750 token
= cp_lexer_peek_token (parser
->lexer
);
11752 /* If we're looking at a keyword, we can use that to guide the
11753 production we choose. */
11754 keyword
= token
->keyword
;
11758 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
11759 goto elaborated_type_specifier
;
11761 /* Look for the enum-specifier. */
11762 type_spec
= cp_parser_enum_specifier (parser
);
11763 /* If that worked, we're done. */
11766 if (declares_class_or_enum
)
11767 *declares_class_or_enum
= 2;
11769 cp_parser_set_decl_spec_type (decl_specs
,
11772 /*user_defined_p=*/true);
11776 goto elaborated_type_specifier
;
11778 /* Any of these indicate either a class-specifier, or an
11779 elaborated-type-specifier. */
11783 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
11784 goto elaborated_type_specifier
;
11786 /* Parse tentatively so that we can back up if we don't find a
11787 class-specifier. */
11788 cp_parser_parse_tentatively (parser
);
11789 /* Look for the class-specifier. */
11790 type_spec
= cp_parser_class_specifier (parser
);
11791 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
11792 /* If that worked, we're done. */
11793 if (cp_parser_parse_definitely (parser
))
11795 if (declares_class_or_enum
)
11796 *declares_class_or_enum
= 2;
11798 cp_parser_set_decl_spec_type (decl_specs
,
11801 /*user_defined_p=*/true);
11805 /* Fall through. */
11806 elaborated_type_specifier
:
11807 /* We're declaring (not defining) a class or enum. */
11808 if (declares_class_or_enum
)
11809 *declares_class_or_enum
= 1;
11811 /* Fall through. */
11813 /* Look for an elaborated-type-specifier. */
11815 = (cp_parser_elaborated_type_specifier
11817 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
11820 cp_parser_set_decl_spec_type (decl_specs
,
11823 /*user_defined_p=*/true);
11828 if (is_cv_qualifier
)
11829 *is_cv_qualifier
= true;
11834 if (is_cv_qualifier
)
11835 *is_cv_qualifier
= true;
11840 if (is_cv_qualifier
)
11841 *is_cv_qualifier
= true;
11845 /* The `__complex__' keyword is a GNU extension. */
11853 /* Handle simple keywords. */
11858 ++decl_specs
->specs
[(int)ds
];
11859 decl_specs
->any_specifiers_p
= true;
11861 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11864 /* If we do not already have a type-specifier, assume we are looking
11865 at a simple-type-specifier. */
11866 type_spec
= cp_parser_simple_type_specifier (parser
,
11870 /* If we didn't find a type-specifier, and a type-specifier was not
11871 optional in this context, issue an error message. */
11872 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
11874 cp_parser_error (parser
, "expected type specifier");
11875 return error_mark_node
;
11881 /* Parse a simple-type-specifier.
11883 simple-type-specifier:
11884 :: [opt] nested-name-specifier [opt] type-name
11885 :: [opt] nested-name-specifier template template-id
11900 simple-type-specifier:
11902 decltype ( expression )
11908 simple-type-specifier:
11909 __typeof__ unary-expression
11910 __typeof__ ( type-id )
11912 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11913 appropriately updated. */
11916 cp_parser_simple_type_specifier (cp_parser
* parser
,
11917 cp_decl_specifier_seq
*decl_specs
,
11918 cp_parser_flags flags
)
11920 tree type
= NULL_TREE
;
11923 /* Peek at the next token. */
11924 token
= cp_lexer_peek_token (parser
->lexer
);
11926 /* If we're looking at a keyword, things are easy. */
11927 switch (token
->keyword
)
11931 decl_specs
->explicit_char_p
= true;
11932 type
= char_type_node
;
11935 type
= char16_type_node
;
11938 type
= char32_type_node
;
11941 type
= wchar_type_node
;
11944 type
= boolean_type_node
;
11948 ++decl_specs
->specs
[(int) ds_short
];
11949 type
= short_integer_type_node
;
11953 decl_specs
->explicit_int_p
= true;
11954 type
= integer_type_node
;
11958 ++decl_specs
->specs
[(int) ds_long
];
11959 type
= long_integer_type_node
;
11963 ++decl_specs
->specs
[(int) ds_signed
];
11964 type
= integer_type_node
;
11968 ++decl_specs
->specs
[(int) ds_unsigned
];
11969 type
= unsigned_type_node
;
11972 type
= float_type_node
;
11975 type
= double_type_node
;
11978 type
= void_type_node
;
11982 maybe_warn_cpp0x (CPP0X_AUTO
);
11983 type
= make_auto ();
11987 /* Parse the `decltype' type. */
11988 type
= cp_parser_decltype (parser
);
11991 cp_parser_set_decl_spec_type (decl_specs
, type
,
11993 /*user_defined_p=*/true);
11998 /* Consume the `typeof' token. */
11999 cp_lexer_consume_token (parser
->lexer
);
12000 /* Parse the operand to `typeof'. */
12001 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
12002 /* If it is not already a TYPE, take its type. */
12003 if (!TYPE_P (type
))
12004 type
= finish_typeof (type
);
12007 cp_parser_set_decl_spec_type (decl_specs
, type
,
12009 /*user_defined_p=*/true);
12017 /* If the type-specifier was for a built-in type, we're done. */
12020 /* Record the type. */
12022 && (token
->keyword
!= RID_SIGNED
12023 && token
->keyword
!= RID_UNSIGNED
12024 && token
->keyword
!= RID_SHORT
12025 && token
->keyword
!= RID_LONG
))
12026 cp_parser_set_decl_spec_type (decl_specs
,
12029 /*user_defined=*/false);
12031 decl_specs
->any_specifiers_p
= true;
12033 /* Consume the token. */
12034 cp_lexer_consume_token (parser
->lexer
);
12036 /* There is no valid C++ program where a non-template type is
12037 followed by a "<". That usually indicates that the user thought
12038 that the type was a template. */
12039 cp_parser_check_for_invalid_template_id (parser
, type
, token
->location
);
12041 return TYPE_NAME (type
);
12044 /* The type-specifier must be a user-defined type. */
12045 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
12050 /* Don't gobble tokens or issue error messages if this is an
12051 optional type-specifier. */
12052 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
12053 cp_parser_parse_tentatively (parser
);
12055 /* Look for the optional `::' operator. */
12057 = (cp_parser_global_scope_opt (parser
,
12058 /*current_scope_valid_p=*/false)
12060 /* Look for the nested-name specifier. */
12062 = (cp_parser_nested_name_specifier_opt (parser
,
12063 /*typename_keyword_p=*/false,
12064 /*check_dependency_p=*/true,
12066 /*is_declaration=*/false)
12068 token
= cp_lexer_peek_token (parser
->lexer
);
12069 /* If we have seen a nested-name-specifier, and the next token
12070 is `template', then we are using the template-id production. */
12072 && cp_parser_optional_template_keyword (parser
))
12074 /* Look for the template-id. */
12075 type
= cp_parser_template_id (parser
,
12076 /*template_keyword_p=*/true,
12077 /*check_dependency_p=*/true,
12078 /*is_declaration=*/false);
12079 /* If the template-id did not name a type, we are out of
12081 if (TREE_CODE (type
) != TYPE_DECL
)
12083 cp_parser_error (parser
, "expected template-id for type");
12087 /* Otherwise, look for a type-name. */
12089 type
= cp_parser_type_name (parser
);
12090 /* Keep track of all name-lookups performed in class scopes. */
12094 && TREE_CODE (type
) == TYPE_DECL
12095 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
12096 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
12097 /* If it didn't work out, we don't have a TYPE. */
12098 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
12099 && !cp_parser_parse_definitely (parser
))
12101 if (type
&& decl_specs
)
12102 cp_parser_set_decl_spec_type (decl_specs
, type
,
12104 /*user_defined=*/true);
12107 /* If we didn't get a type-name, issue an error message. */
12108 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
12110 cp_parser_error (parser
, "expected type-name");
12111 return error_mark_node
;
12114 /* There is no valid C++ program where a non-template type is
12115 followed by a "<". That usually indicates that the user thought
12116 that the type was a template. */
12117 if (type
&& type
!= error_mark_node
)
12119 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12120 If it is, then the '<'...'>' enclose protocol names rather than
12121 template arguments, and so everything is fine. */
12122 if (c_dialect_objc ()
12123 && (objc_is_id (type
) || objc_is_class_name (type
)))
12125 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
12126 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
12128 /* Clobber the "unqualified" type previously entered into
12129 DECL_SPECS with the new, improved protocol-qualified version. */
12131 decl_specs
->type
= qual_type
;
12136 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
12143 /* Parse a type-name.
12156 Returns a TYPE_DECL for the type. */
12159 cp_parser_type_name (cp_parser
* parser
)
12163 /* We can't know yet whether it is a class-name or not. */
12164 cp_parser_parse_tentatively (parser
);
12165 /* Try a class-name. */
12166 type_decl
= cp_parser_class_name (parser
,
12167 /*typename_keyword_p=*/false,
12168 /*template_keyword_p=*/false,
12170 /*check_dependency_p=*/true,
12171 /*class_head_p=*/false,
12172 /*is_declaration=*/false);
12173 /* If it's not a class-name, keep looking. */
12174 if (!cp_parser_parse_definitely (parser
))
12176 /* It must be a typedef-name or an enum-name. */
12177 return cp_parser_nonclass_name (parser
);
12183 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12191 Returns a TYPE_DECL for the type. */
12194 cp_parser_nonclass_name (cp_parser
* parser
)
12199 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12200 identifier
= cp_parser_identifier (parser
);
12201 if (identifier
== error_mark_node
)
12202 return error_mark_node
;
12204 /* Look up the type-name. */
12205 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
12207 if (TREE_CODE (type_decl
) != TYPE_DECL
12208 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
12210 /* See if this is an Objective-C type. */
12211 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
12212 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
12214 type_decl
= TYPE_NAME (type
);
12217 /* Issue an error if we did not find a type-name. */
12218 if (TREE_CODE (type_decl
) != TYPE_DECL
)
12220 if (!cp_parser_simulate_error (parser
))
12221 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
12222 "is not a type", token
->location
);
12223 return error_mark_node
;
12225 /* Remember that the name was used in the definition of the
12226 current class so that we can check later to see if the
12227 meaning would have been different after the class was
12228 entirely defined. */
12229 else if (type_decl
!= error_mark_node
12231 maybe_note_name_used_in_class (identifier
, type_decl
);
12236 /* Parse an elaborated-type-specifier. Note that the grammar given
12237 here incorporates the resolution to DR68.
12239 elaborated-type-specifier:
12240 class-key :: [opt] nested-name-specifier [opt] identifier
12241 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12242 enum-key :: [opt] nested-name-specifier [opt] identifier
12243 typename :: [opt] nested-name-specifier identifier
12244 typename :: [opt] nested-name-specifier template [opt]
12249 elaborated-type-specifier:
12250 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12251 class-key attributes :: [opt] nested-name-specifier [opt]
12252 template [opt] template-id
12253 enum attributes :: [opt] nested-name-specifier [opt] identifier
12255 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12256 declared `friend'. If IS_DECLARATION is TRUE, then this
12257 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12258 something is being declared.
12260 Returns the TYPE specified. */
12263 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
12265 bool is_declaration
)
12267 enum tag_types tag_type
;
12269 tree type
= NULL_TREE
;
12270 tree attributes
= NULL_TREE
;
12272 cp_token
*token
= NULL
;
12274 /* See if we're looking at the `enum' keyword. */
12275 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
12277 /* Consume the `enum' token. */
12278 cp_lexer_consume_token (parser
->lexer
);
12279 /* Remember that it's an enumeration type. */
12280 tag_type
= enum_type
;
12281 /* Parse the optional `struct' or `class' key (for C++0x scoped
12283 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
12284 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
12286 if (cxx_dialect
== cxx98
)
12287 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
12289 /* Consume the `struct' or `class'. */
12290 cp_lexer_consume_token (parser
->lexer
);
12292 /* Parse the attributes. */
12293 attributes
= cp_parser_attributes_opt (parser
);
12295 /* Or, it might be `typename'. */
12296 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
12299 /* Consume the `typename' token. */
12300 cp_lexer_consume_token (parser
->lexer
);
12301 /* Remember that it's a `typename' type. */
12302 tag_type
= typename_type
;
12304 /* Otherwise it must be a class-key. */
12307 tag_type
= cp_parser_class_key (parser
);
12308 if (tag_type
== none_type
)
12309 return error_mark_node
;
12310 /* Parse the attributes. */
12311 attributes
= cp_parser_attributes_opt (parser
);
12314 /* Look for the `::' operator. */
12315 globalscope
= cp_parser_global_scope_opt (parser
,
12316 /*current_scope_valid_p=*/false);
12317 /* Look for the nested-name-specifier. */
12318 if (tag_type
== typename_type
&& !globalscope
)
12320 if (!cp_parser_nested_name_specifier (parser
,
12321 /*typename_keyword_p=*/true,
12322 /*check_dependency_p=*/true,
12325 return error_mark_node
;
12328 /* Even though `typename' is not present, the proposed resolution
12329 to Core Issue 180 says that in `class A<T>::B', `B' should be
12330 considered a type-name, even if `A<T>' is dependent. */
12331 cp_parser_nested_name_specifier_opt (parser
,
12332 /*typename_keyword_p=*/true,
12333 /*check_dependency_p=*/true,
12336 /* For everything but enumeration types, consider a template-id.
12337 For an enumeration type, consider only a plain identifier. */
12338 if (tag_type
!= enum_type
)
12340 bool template_p
= false;
12343 /* Allow the `template' keyword. */
12344 template_p
= cp_parser_optional_template_keyword (parser
);
12345 /* If we didn't see `template', we don't know if there's a
12346 template-id or not. */
12348 cp_parser_parse_tentatively (parser
);
12349 /* Parse the template-id. */
12350 token
= cp_lexer_peek_token (parser
->lexer
);
12351 decl
= cp_parser_template_id (parser
, template_p
,
12352 /*check_dependency_p=*/true,
12354 /* If we didn't find a template-id, look for an ordinary
12356 if (!template_p
&& !cp_parser_parse_definitely (parser
))
12358 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12359 in effect, then we must assume that, upon instantiation, the
12360 template will correspond to a class. */
12361 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
12362 && tag_type
== typename_type
)
12363 type
= make_typename_type (parser
->scope
, decl
,
12365 /*complain=*/tf_error
);
12366 /* If the `typename' keyword is in effect and DECL is not a type
12367 decl. Then type is non existant. */
12368 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
12371 type
= TREE_TYPE (decl
);
12376 token
= cp_lexer_peek_token (parser
->lexer
);
12377 identifier
= cp_parser_identifier (parser
);
12379 if (identifier
== error_mark_node
)
12381 parser
->scope
= NULL_TREE
;
12382 return error_mark_node
;
12385 /* For a `typename', we needn't call xref_tag. */
12386 if (tag_type
== typename_type
12387 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
12388 return cp_parser_make_typename_type (parser
, parser
->scope
,
12391 /* Look up a qualified name in the usual way. */
12395 tree ambiguous_decls
;
12397 decl
= cp_parser_lookup_name (parser
, identifier
,
12399 /*is_template=*/false,
12400 /*is_namespace=*/false,
12401 /*check_dependency=*/true,
12405 /* If the lookup was ambiguous, an error will already have been
12407 if (ambiguous_decls
)
12408 return error_mark_node
;
12410 /* If we are parsing friend declaration, DECL may be a
12411 TEMPLATE_DECL tree node here. However, we need to check
12412 whether this TEMPLATE_DECL results in valid code. Consider
12413 the following example:
12416 template <class T> class C {};
12419 template <class T> friend class N::C; // #1, valid code
12421 template <class T> class Y {
12422 friend class N::C; // #2, invalid code
12425 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12426 name lookup of `N::C'. We see that friend declaration must
12427 be template for the code to be valid. Note that
12428 processing_template_decl does not work here since it is
12429 always 1 for the above two cases. */
12431 decl
= (cp_parser_maybe_treat_template_as_class
12432 (decl
, /*tag_name_p=*/is_friend
12433 && parser
->num_template_parameter_lists
));
12435 if (TREE_CODE (decl
) != TYPE_DECL
)
12437 cp_parser_diagnose_invalid_type_name (parser
,
12441 return error_mark_node
;
12444 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
12446 bool allow_template
= (parser
->num_template_parameter_lists
12447 || DECL_SELF_REFERENCE_P (decl
));
12448 type
= check_elaborated_type_specifier (tag_type
, decl
,
12451 if (type
== error_mark_node
)
12452 return error_mark_node
;
12455 /* Forward declarations of nested types, such as
12460 are invalid unless all components preceding the final '::'
12461 are complete. If all enclosing types are complete, these
12462 declarations become merely pointless.
12464 Invalid forward declarations of nested types are errors
12465 caught elsewhere in parsing. Those that are pointless arrive
12468 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
12469 && !is_friend
&& !processing_explicit_instantiation
)
12470 warning (0, "declaration %qD does not declare anything", decl
);
12472 type
= TREE_TYPE (decl
);
12476 /* An elaborated-type-specifier sometimes introduces a new type and
12477 sometimes names an existing type. Normally, the rule is that it
12478 introduces a new type only if there is not an existing type of
12479 the same name already in scope. For example, given:
12482 void f() { struct S s; }
12484 the `struct S' in the body of `f' is the same `struct S' as in
12485 the global scope; the existing definition is used. However, if
12486 there were no global declaration, this would introduce a new
12487 local class named `S'.
12489 An exception to this rule applies to the following code:
12491 namespace N { struct S; }
12493 Here, the elaborated-type-specifier names a new type
12494 unconditionally; even if there is already an `S' in the
12495 containing scope this declaration names a new type.
12496 This exception only applies if the elaborated-type-specifier
12497 forms the complete declaration:
12501 A declaration consisting solely of `class-key identifier ;' is
12502 either a redeclaration of the name in the current scope or a
12503 forward declaration of the identifier as a class name. It
12504 introduces the name into the current scope.
12506 We are in this situation precisely when the next token is a `;'.
12508 An exception to the exception is that a `friend' declaration does
12509 *not* name a new type; i.e., given:
12511 struct S { friend struct T; };
12513 `T' is not a new type in the scope of `S'.
12515 Also, `new struct S' or `sizeof (struct S)' never results in the
12516 definition of a new type; a new type can only be declared in a
12517 declaration context. */
12523 /* Friends have special name lookup rules. */
12524 ts
= ts_within_enclosing_non_class
;
12525 else if (is_declaration
12526 && cp_lexer_next_token_is (parser
->lexer
,
12528 /* This is a `class-key identifier ;' */
12534 (parser
->num_template_parameter_lists
12535 && (cp_parser_next_token_starts_class_definition_p (parser
)
12536 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
12537 /* An unqualified name was used to reference this type, so
12538 there were no qualifying templates. */
12539 if (!cp_parser_check_template_parameters (parser
,
12540 /*num_templates=*/0,
12542 /*declarator=*/NULL
))
12543 return error_mark_node
;
12544 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
12548 if (type
== error_mark_node
)
12549 return error_mark_node
;
12551 /* Allow attributes on forward declarations of classes. */
12554 if (TREE_CODE (type
) == TYPENAME_TYPE
)
12555 warning (OPT_Wattributes
,
12556 "attributes ignored on uninstantiated type");
12557 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
12558 && ! processing_explicit_instantiation
)
12559 warning (OPT_Wattributes
,
12560 "attributes ignored on template instantiation");
12561 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
12562 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
12564 warning (OPT_Wattributes
,
12565 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12568 if (tag_type
!= enum_type
)
12569 cp_parser_check_class_key (tag_type
, type
);
12571 /* A "<" cannot follow an elaborated type specifier. If that
12572 happens, the user was probably trying to form a template-id. */
12573 cp_parser_check_for_invalid_template_id (parser
, type
, token
->location
);
12578 /* Parse an enum-specifier.
12581 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12586 enum struct [C++0x]
12589 : type-specifier-seq
12592 enum-key attributes[opt] identifier [opt] enum-base [opt]
12593 { enumerator-list [opt] }attributes[opt]
12595 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12596 if the token stream isn't an enum-specifier after all. */
12599 cp_parser_enum_specifier (cp_parser
* parser
)
12604 bool scoped_enum_p
= false;
12605 bool has_underlying_type
= false;
12606 tree underlying_type
= NULL_TREE
;
12608 /* Parse tentatively so that we can back up if we don't find a
12610 cp_parser_parse_tentatively (parser
);
12612 /* Caller guarantees that the current token is 'enum', an identifier
12613 possibly follows, and the token after that is an opening brace.
12614 If we don't have an identifier, fabricate an anonymous name for
12615 the enumeration being defined. */
12616 cp_lexer_consume_token (parser
->lexer
);
12618 /* Parse the "class" or "struct", which indicates a scoped
12619 enumeration type in C++0x. */
12620 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
12621 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
12623 if (cxx_dialect
== cxx98
)
12624 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
12626 /* Consume the `struct' or `class' token. */
12627 cp_lexer_consume_token (parser
->lexer
);
12629 scoped_enum_p
= true;
12632 attributes
= cp_parser_attributes_opt (parser
);
12634 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12635 identifier
= cp_parser_identifier (parser
);
12637 identifier
= make_anon_name ();
12639 /* Check for the `:' that denotes a specified underlying type in C++0x.
12640 Note that a ':' could also indicate a bitfield width, however. */
12641 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12643 cp_decl_specifier_seq type_specifiers
;
12645 /* Consume the `:'. */
12646 cp_lexer_consume_token (parser
->lexer
);
12648 /* Parse the type-specifier-seq. */
12649 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
12650 /*is_trailing_return=*/false,
12653 /* At this point this is surely not elaborated type specifier. */
12654 if (!cp_parser_parse_definitely (parser
))
12657 if (cxx_dialect
== cxx98
)
12658 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
12660 has_underlying_type
= true;
12662 /* If that didn't work, stop. */
12663 if (type_specifiers
.type
!= error_mark_node
)
12665 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
12666 /*initialized=*/0, NULL
);
12667 if (underlying_type
== error_mark_node
)
12668 underlying_type
= NULL_TREE
;
12672 /* Look for the `{' but don't consume it yet. */
12673 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12675 cp_parser_error (parser
, "expected %<{%>");
12676 if (has_underlying_type
)
12680 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
12683 /* Issue an error message if type-definitions are forbidden here. */
12684 if (!cp_parser_check_type_definition (parser
))
12685 type
= error_mark_node
;
12687 /* Create the new type. We do this before consuming the opening
12688 brace so the enum will be recorded as being on the line of its
12689 tag (or the 'enum' keyword, if there is no tag). */
12690 type
= start_enum (identifier
, underlying_type
, scoped_enum_p
);
12692 /* Consume the opening brace. */
12693 cp_lexer_consume_token (parser
->lexer
);
12695 if (type
== error_mark_node
)
12697 cp_parser_skip_to_end_of_block_or_statement (parser
);
12698 return error_mark_node
;
12701 /* If the next token is not '}', then there are some enumerators. */
12702 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
12703 cp_parser_enumerator_list (parser
, type
);
12705 /* Consume the final '}'. */
12706 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
12708 /* Look for trailing attributes to apply to this enumeration, and
12709 apply them if appropriate. */
12710 if (cp_parser_allow_gnu_extensions_p (parser
))
12712 tree trailing_attr
= cp_parser_attributes_opt (parser
);
12713 trailing_attr
= chainon (trailing_attr
, attributes
);
12714 cplus_decl_attributes (&type
,
12716 (int) ATTR_FLAG_TYPE_IN_PLACE
);
12719 /* Finish up the enumeration. */
12720 finish_enum (type
);
12725 /* Parse an enumerator-list. The enumerators all have the indicated
12729 enumerator-definition
12730 enumerator-list , enumerator-definition */
12733 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
12737 /* Parse an enumerator-definition. */
12738 cp_parser_enumerator_definition (parser
, type
);
12740 /* If the next token is not a ',', we've reached the end of
12742 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12744 /* Otherwise, consume the `,' and keep going. */
12745 cp_lexer_consume_token (parser
->lexer
);
12746 /* If the next token is a `}', there is a trailing comma. */
12747 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
12749 if (!in_system_header
)
12750 pedwarn (input_location
, OPT_pedantic
, "comma at end of enumerator list");
12756 /* Parse an enumerator-definition. The enumerator has the indicated
12759 enumerator-definition:
12761 enumerator = constant-expression
12767 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
12772 /* Look for the identifier. */
12773 identifier
= cp_parser_identifier (parser
);
12774 if (identifier
== error_mark_node
)
12777 /* If the next token is an '=', then there is an explicit value. */
12778 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12780 /* Consume the `=' token. */
12781 cp_lexer_consume_token (parser
->lexer
);
12782 /* Parse the value. */
12783 value
= cp_parser_constant_expression (parser
,
12784 /*allow_non_constant_p=*/false,
12790 /* If we are processing a template, make sure the initializer of the
12791 enumerator doesn't contain any bare template parameter pack. */
12792 if (check_for_bare_parameter_packs (value
))
12793 value
= error_mark_node
;
12795 /* Create the enumerator. */
12796 build_enumerator (identifier
, value
, type
);
12799 /* Parse a namespace-name.
12802 original-namespace-name
12805 Returns the NAMESPACE_DECL for the namespace. */
12808 cp_parser_namespace_name (cp_parser
* parser
)
12811 tree namespace_decl
;
12813 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12815 /* Get the name of the namespace. */
12816 identifier
= cp_parser_identifier (parser
);
12817 if (identifier
== error_mark_node
)
12818 return error_mark_node
;
12820 /* Look up the identifier in the currently active scope. Look only
12821 for namespaces, due to:
12823 [basic.lookup.udir]
12825 When looking up a namespace-name in a using-directive or alias
12826 definition, only namespace names are considered.
12830 [basic.lookup.qual]
12832 During the lookup of a name preceding the :: scope resolution
12833 operator, object, function, and enumerator names are ignored.
12835 (Note that cp_parser_qualifying_entity only calls this
12836 function if the token after the name is the scope resolution
12838 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
12840 /*is_template=*/false,
12841 /*is_namespace=*/true,
12842 /*check_dependency=*/true,
12843 /*ambiguous_decls=*/NULL
,
12845 /* If it's not a namespace, issue an error. */
12846 if (namespace_decl
== error_mark_node
12847 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
12849 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
12850 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
12851 cp_parser_error (parser
, "expected namespace-name");
12852 namespace_decl
= error_mark_node
;
12855 return namespace_decl
;
12858 /* Parse a namespace-definition.
12860 namespace-definition:
12861 named-namespace-definition
12862 unnamed-namespace-definition
12864 named-namespace-definition:
12865 original-namespace-definition
12866 extension-namespace-definition
12868 original-namespace-definition:
12869 namespace identifier { namespace-body }
12871 extension-namespace-definition:
12872 namespace original-namespace-name { namespace-body }
12874 unnamed-namespace-definition:
12875 namespace { namespace-body } */
12878 cp_parser_namespace_definition (cp_parser
* parser
)
12880 tree identifier
, attribs
;
12881 bool has_visibility
;
12884 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
12887 cp_lexer_consume_token (parser
->lexer
);
12892 /* Look for the `namespace' keyword. */
12893 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "%<namespace%>");
12895 /* Get the name of the namespace. We do not attempt to distinguish
12896 between an original-namespace-definition and an
12897 extension-namespace-definition at this point. The semantic
12898 analysis routines are responsible for that. */
12899 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12900 identifier
= cp_parser_identifier (parser
);
12902 identifier
= NULL_TREE
;
12904 /* Parse any specified attributes. */
12905 attribs
= cp_parser_attributes_opt (parser
);
12907 /* Look for the `{' to start the namespace. */
12908 cp_parser_require (parser
, CPP_OPEN_BRACE
, "%<{%>");
12909 /* Start the namespace. */
12910 push_namespace (identifier
);
12912 /* "inline namespace" is equivalent to a stub namespace definition
12913 followed by a strong using directive. */
12916 tree name_space
= current_namespace
;
12917 /* Set up namespace association. */
12918 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
12919 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
12920 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
12921 /* Import the contents of the inline namespace. */
12923 do_using_directive (name_space
);
12924 push_namespace (identifier
);
12927 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
12929 /* Parse the body of the namespace. */
12930 cp_parser_namespace_body (parser
);
12932 #ifdef HANDLE_PRAGMA_VISIBILITY
12933 if (has_visibility
)
12934 pop_visibility (1);
12937 /* Finish the namespace. */
12939 /* Look for the final `}'. */
12940 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
12943 /* Parse a namespace-body.
12946 declaration-seq [opt] */
12949 cp_parser_namespace_body (cp_parser
* parser
)
12951 cp_parser_declaration_seq_opt (parser
);
12954 /* Parse a namespace-alias-definition.
12956 namespace-alias-definition:
12957 namespace identifier = qualified-namespace-specifier ; */
12960 cp_parser_namespace_alias_definition (cp_parser
* parser
)
12963 tree namespace_specifier
;
12965 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12967 /* Look for the `namespace' keyword. */
12968 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "%<namespace%>");
12969 /* Look for the identifier. */
12970 identifier
= cp_parser_identifier (parser
);
12971 if (identifier
== error_mark_node
)
12973 /* Look for the `=' token. */
12974 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
12975 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12977 error_at (token
->location
, "%<namespace%> definition is not allowed here");
12978 /* Skip the definition. */
12979 cp_lexer_consume_token (parser
->lexer
);
12980 if (cp_parser_skip_to_closing_brace (parser
))
12981 cp_lexer_consume_token (parser
->lexer
);
12984 cp_parser_require (parser
, CPP_EQ
, "%<=%>");
12985 /* Look for the qualified-namespace-specifier. */
12986 namespace_specifier
12987 = cp_parser_qualified_namespace_specifier (parser
);
12988 /* Look for the `;' token. */
12989 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
12991 /* Register the alias in the symbol table. */
12992 do_namespace_alias (identifier
, namespace_specifier
);
12995 /* Parse a qualified-namespace-specifier.
12997 qualified-namespace-specifier:
12998 :: [opt] nested-name-specifier [opt] namespace-name
13000 Returns a NAMESPACE_DECL corresponding to the specified
13004 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
13006 /* Look for the optional `::'. */
13007 cp_parser_global_scope_opt (parser
,
13008 /*current_scope_valid_p=*/false);
13010 /* Look for the optional nested-name-specifier. */
13011 cp_parser_nested_name_specifier_opt (parser
,
13012 /*typename_keyword_p=*/false,
13013 /*check_dependency_p=*/true,
13015 /*is_declaration=*/true);
13017 return cp_parser_namespace_name (parser
);
13020 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13021 access declaration.
13024 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13025 using :: unqualified-id ;
13027 access-declaration:
13033 cp_parser_using_declaration (cp_parser
* parser
,
13034 bool access_declaration_p
)
13037 bool typename_p
= false;
13038 bool global_scope_p
;
13043 if (access_declaration_p
)
13044 cp_parser_parse_tentatively (parser
);
13047 /* Look for the `using' keyword. */
13048 cp_parser_require_keyword (parser
, RID_USING
, "%<using%>");
13050 /* Peek at the next token. */
13051 token
= cp_lexer_peek_token (parser
->lexer
);
13052 /* See if it's `typename'. */
13053 if (token
->keyword
== RID_TYPENAME
)
13055 /* Remember that we've seen it. */
13057 /* Consume the `typename' token. */
13058 cp_lexer_consume_token (parser
->lexer
);
13062 /* Look for the optional global scope qualification. */
13064 = (cp_parser_global_scope_opt (parser
,
13065 /*current_scope_valid_p=*/false)
13068 /* If we saw `typename', or didn't see `::', then there must be a
13069 nested-name-specifier present. */
13070 if (typename_p
|| !global_scope_p
)
13071 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
13072 /*check_dependency_p=*/true,
13074 /*is_declaration=*/true);
13075 /* Otherwise, we could be in either of the two productions. In that
13076 case, treat the nested-name-specifier as optional. */
13078 qscope
= cp_parser_nested_name_specifier_opt (parser
,
13079 /*typename_keyword_p=*/false,
13080 /*check_dependency_p=*/true,
13082 /*is_declaration=*/true);
13084 qscope
= global_namespace
;
13086 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
13087 /* Something has already gone wrong; there's no need to parse
13088 further. Since an error has occurred, the return value of
13089 cp_parser_parse_definitely will be false, as required. */
13090 return cp_parser_parse_definitely (parser
);
13092 token
= cp_lexer_peek_token (parser
->lexer
);
13093 /* Parse the unqualified-id. */
13094 identifier
= cp_parser_unqualified_id (parser
,
13095 /*template_keyword_p=*/false,
13096 /*check_dependency_p=*/true,
13097 /*declarator_p=*/true,
13098 /*optional_p=*/false);
13100 if (access_declaration_p
)
13102 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13103 cp_parser_simulate_error (parser
);
13104 if (!cp_parser_parse_definitely (parser
))
13108 /* The function we call to handle a using-declaration is different
13109 depending on what scope we are in. */
13110 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
13112 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
13113 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
13114 /* [namespace.udecl]
13116 A using declaration shall not name a template-id. */
13117 error_at (token
->location
,
13118 "a template-id may not appear in a using-declaration");
13121 if (at_class_scope_p ())
13123 /* Create the USING_DECL. */
13124 decl
= do_class_using_decl (parser
->scope
, identifier
);
13126 if (check_for_bare_parameter_packs (decl
))
13129 /* Add it to the list of members in this class. */
13130 finish_member_declaration (decl
);
13134 decl
= cp_parser_lookup_name_simple (parser
,
13137 if (decl
== error_mark_node
)
13138 cp_parser_name_lookup_error (parser
, identifier
,
13141 else if (check_for_bare_parameter_packs (decl
))
13143 else if (!at_namespace_scope_p ())
13144 do_local_using_decl (decl
, qscope
, identifier
);
13146 do_toplevel_using_decl (decl
, qscope
, identifier
);
13150 /* Look for the final `;'. */
13151 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
13156 /* Parse a using-directive.
13159 using namespace :: [opt] nested-name-specifier [opt]
13160 namespace-name ; */
13163 cp_parser_using_directive (cp_parser
* parser
)
13165 tree namespace_decl
;
13168 /* Look for the `using' keyword. */
13169 cp_parser_require_keyword (parser
, RID_USING
, "%<using%>");
13170 /* And the `namespace' keyword. */
13171 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "%<namespace%>");
13172 /* Look for the optional `::' operator. */
13173 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
13174 /* And the optional nested-name-specifier. */
13175 cp_parser_nested_name_specifier_opt (parser
,
13176 /*typename_keyword_p=*/false,
13177 /*check_dependency_p=*/true,
13179 /*is_declaration=*/true);
13180 /* Get the namespace being used. */
13181 namespace_decl
= cp_parser_namespace_name (parser
);
13182 /* And any specified attributes. */
13183 attribs
= cp_parser_attributes_opt (parser
);
13184 /* Update the symbol table. */
13185 parse_using_directive (namespace_decl
, attribs
);
13186 /* Look for the final `;'. */
13187 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
13190 /* Parse an asm-definition.
13193 asm ( string-literal ) ;
13198 asm volatile [opt] ( string-literal ) ;
13199 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13200 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13201 : asm-operand-list [opt] ) ;
13202 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13203 : asm-operand-list [opt]
13204 : asm-clobber-list [opt] ) ;
13205 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13206 : asm-clobber-list [opt]
13207 : asm-goto-list ) ; */
13210 cp_parser_asm_definition (cp_parser
* parser
)
13213 tree outputs
= NULL_TREE
;
13214 tree inputs
= NULL_TREE
;
13215 tree clobbers
= NULL_TREE
;
13216 tree labels
= NULL_TREE
;
13218 bool volatile_p
= false;
13219 bool extended_p
= false;
13220 bool invalid_inputs_p
= false;
13221 bool invalid_outputs_p
= false;
13222 bool goto_p
= false;
13223 const char *missing
= NULL
;
13225 /* Look for the `asm' keyword. */
13226 cp_parser_require_keyword (parser
, RID_ASM
, "%<asm%>");
13227 /* See if the next token is `volatile'. */
13228 if (cp_parser_allow_gnu_extensions_p (parser
)
13229 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
13231 /* Remember that we saw the `volatile' keyword. */
13233 /* Consume the token. */
13234 cp_lexer_consume_token (parser
->lexer
);
13236 if (cp_parser_allow_gnu_extensions_p (parser
)
13237 && parser
->in_function_body
13238 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
13240 /* Remember that we saw the `goto' keyword. */
13242 /* Consume the token. */
13243 cp_lexer_consume_token (parser
->lexer
);
13245 /* Look for the opening `('. */
13246 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
13248 /* Look for the string. */
13249 string
= cp_parser_string_literal (parser
, false, false);
13250 if (string
== error_mark_node
)
13252 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
13253 /*consume_paren=*/true);
13257 /* If we're allowing GNU extensions, check for the extended assembly
13258 syntax. Unfortunately, the `:' tokens need not be separated by
13259 a space in C, and so, for compatibility, we tolerate that here
13260 too. Doing that means that we have to treat the `::' operator as
13262 if (cp_parser_allow_gnu_extensions_p (parser
)
13263 && parser
->in_function_body
13264 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
13265 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
13267 bool inputs_p
= false;
13268 bool clobbers_p
= false;
13269 bool labels_p
= false;
13271 /* The extended syntax was used. */
13274 /* Look for outputs. */
13275 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13277 /* Consume the `:'. */
13278 cp_lexer_consume_token (parser
->lexer
);
13279 /* Parse the output-operands. */
13280 if (cp_lexer_next_token_is_not (parser
->lexer
,
13282 && cp_lexer_next_token_is_not (parser
->lexer
,
13284 && cp_lexer_next_token_is_not (parser
->lexer
,
13287 outputs
= cp_parser_asm_operand_list (parser
);
13289 if (outputs
== error_mark_node
)
13290 invalid_outputs_p
= true;
13292 /* If the next token is `::', there are no outputs, and the
13293 next token is the beginning of the inputs. */
13294 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13295 /* The inputs are coming next. */
13298 /* Look for inputs. */
13300 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13302 /* Consume the `:' or `::'. */
13303 cp_lexer_consume_token (parser
->lexer
);
13304 /* Parse the output-operands. */
13305 if (cp_lexer_next_token_is_not (parser
->lexer
,
13307 && cp_lexer_next_token_is_not (parser
->lexer
,
13309 && cp_lexer_next_token_is_not (parser
->lexer
,
13311 inputs
= cp_parser_asm_operand_list (parser
);
13313 if (inputs
== error_mark_node
)
13314 invalid_inputs_p
= true;
13316 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13317 /* The clobbers are coming next. */
13320 /* Look for clobbers. */
13322 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13325 /* Consume the `:' or `::'. */
13326 cp_lexer_consume_token (parser
->lexer
);
13327 /* Parse the clobbers. */
13328 if (cp_lexer_next_token_is_not (parser
->lexer
,
13330 && cp_lexer_next_token_is_not (parser
->lexer
,
13332 clobbers
= cp_parser_asm_clobber_list (parser
);
13335 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13336 /* The labels are coming next. */
13339 /* Look for labels. */
13341 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
13344 /* Consume the `:' or `::'. */
13345 cp_lexer_consume_token (parser
->lexer
);
13346 /* Parse the labels. */
13347 labels
= cp_parser_asm_label_list (parser
);
13350 if (goto_p
&& !labels_p
)
13351 missing
= clobbers_p
? "%<:%>" : "%<:%> or %<::%>";
13354 missing
= "%<:%> or %<::%>";
13356 /* Look for the closing `)'. */
13357 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
13358 missing
? missing
: "%<)%>"))
13359 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
13360 /*consume_paren=*/true);
13361 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
13363 if (!invalid_inputs_p
&& !invalid_outputs_p
)
13365 /* Create the ASM_EXPR. */
13366 if (parser
->in_function_body
)
13368 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
13369 inputs
, clobbers
, labels
);
13370 /* If the extended syntax was not used, mark the ASM_EXPR. */
13373 tree temp
= asm_stmt
;
13374 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
13375 temp
= TREE_OPERAND (temp
, 0);
13377 ASM_INPUT_P (temp
) = 1;
13381 cgraph_add_asm_node (string
);
13385 /* Declarators [gram.dcl.decl] */
13387 /* Parse an init-declarator.
13390 declarator initializer [opt]
13395 declarator asm-specification [opt] attributes [opt] initializer [opt]
13397 function-definition:
13398 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13400 decl-specifier-seq [opt] declarator function-try-block
13404 function-definition:
13405 __extension__ function-definition
13407 The DECL_SPECIFIERS apply to this declarator. Returns a
13408 representation of the entity declared. If MEMBER_P is TRUE, then
13409 this declarator appears in a class scope. The new DECL created by
13410 this declarator is returned.
13412 The CHECKS are access checks that should be performed once we know
13413 what entity is being declared (and, therefore, what classes have
13416 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13417 for a function-definition here as well. If the declarator is a
13418 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13419 be TRUE upon return. By that point, the function-definition will
13420 have been completely parsed.
13422 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13426 cp_parser_init_declarator (cp_parser
* parser
,
13427 cp_decl_specifier_seq
*decl_specifiers
,
13428 VEC (deferred_access_check
,gc
)* checks
,
13429 bool function_definition_allowed_p
,
13431 int declares_class_or_enum
,
13432 bool* function_definition_p
)
13434 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
13435 *attributes_start_token
= NULL
;
13436 cp_declarator
*declarator
;
13437 tree prefix_attributes
;
13439 tree asm_specification
;
13441 tree decl
= NULL_TREE
;
13443 int is_initialized
;
13444 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13445 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13447 enum cpp_ttype initialization_kind
;
13448 bool is_direct_init
= false;
13449 bool is_non_constant_init
;
13450 int ctor_dtor_or_conv_p
;
13452 tree pushed_scope
= NULL
;
13454 /* Gather the attributes that were provided with the
13455 decl-specifiers. */
13456 prefix_attributes
= decl_specifiers
->attributes
;
13458 /* Assume that this is not the declarator for a function
13460 if (function_definition_p
)
13461 *function_definition_p
= false;
13463 /* Defer access checks while parsing the declarator; we cannot know
13464 what names are accessible until we know what is being
13466 resume_deferring_access_checks ();
13468 /* Parse the declarator. */
13469 token
= cp_lexer_peek_token (parser
->lexer
);
13471 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13472 &ctor_dtor_or_conv_p
,
13473 /*parenthesized_p=*/NULL
,
13474 /*member_p=*/false);
13475 /* Gather up the deferred checks. */
13476 stop_deferring_access_checks ();
13478 /* If the DECLARATOR was erroneous, there's no need to go
13480 if (declarator
== cp_error_declarator
)
13481 return error_mark_node
;
13483 /* Check that the number of template-parameter-lists is OK. */
13484 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
13486 return error_mark_node
;
13488 if (declares_class_or_enum
& 2)
13489 cp_parser_check_for_definition_in_return_type (declarator
,
13490 decl_specifiers
->type
,
13491 decl_specifiers
->type_location
);
13493 /* Figure out what scope the entity declared by the DECLARATOR is
13494 located in. `grokdeclarator' sometimes changes the scope, so
13495 we compute it now. */
13496 scope
= get_scope_of_declarator (declarator
);
13498 /* Perform any lookups in the declared type which were thought to be
13499 dependent, but are not in the scope of the declarator. */
13500 decl_specifiers
->type
13501 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
13503 /* If we're allowing GNU extensions, look for an asm-specification
13505 if (cp_parser_allow_gnu_extensions_p (parser
))
13507 /* Look for an asm-specification. */
13508 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
13509 asm_specification
= cp_parser_asm_specification_opt (parser
);
13510 /* And attributes. */
13511 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
13512 attributes
= cp_parser_attributes_opt (parser
);
13516 asm_specification
= NULL_TREE
;
13517 attributes
= NULL_TREE
;
13520 /* Peek at the next token. */
13521 token
= cp_lexer_peek_token (parser
->lexer
);
13522 /* Check to see if the token indicates the start of a
13523 function-definition. */
13524 if (function_declarator_p (declarator
)
13525 && cp_parser_token_starts_function_definition_p (token
))
13527 if (!function_definition_allowed_p
)
13529 /* If a function-definition should not appear here, issue an
13531 cp_parser_error (parser
,
13532 "a function-definition is not allowed here");
13533 return error_mark_node
;
13537 location_t func_brace_location
13538 = cp_lexer_peek_token (parser
->lexer
)->location
;
13540 /* Neither attributes nor an asm-specification are allowed
13541 on a function-definition. */
13542 if (asm_specification
)
13543 error_at (asm_spec_start_token
->location
,
13544 "an asm-specification is not allowed "
13545 "on a function-definition");
13547 error_at (attributes_start_token
->location
,
13548 "attributes are not allowed on a function-definition");
13549 /* This is a function-definition. */
13550 *function_definition_p
= true;
13552 /* Parse the function definition. */
13554 decl
= cp_parser_save_member_function_body (parser
,
13557 prefix_attributes
);
13560 = (cp_parser_function_definition_from_specifiers_and_declarator
13561 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
13563 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
13565 /* This is where the prologue starts... */
13566 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
13567 = func_brace_location
;
13576 Only in function declarations for constructors, destructors, and
13577 type conversions can the decl-specifier-seq be omitted.
13579 We explicitly postpone this check past the point where we handle
13580 function-definitions because we tolerate function-definitions
13581 that are missing their return types in some modes. */
13582 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
13584 cp_parser_error (parser
,
13585 "expected constructor, destructor, or type conversion");
13586 return error_mark_node
;
13589 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13590 if (token
->type
== CPP_EQ
13591 || token
->type
== CPP_OPEN_PAREN
13592 || token
->type
== CPP_OPEN_BRACE
)
13594 is_initialized
= SD_INITIALIZED
;
13595 initialization_kind
= token
->type
;
13597 if (token
->type
== CPP_EQ
13598 && function_declarator_p (declarator
))
13600 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13601 if (t2
->keyword
== RID_DEFAULT
)
13602 is_initialized
= SD_DEFAULTED
;
13603 else if (t2
->keyword
== RID_DELETE
)
13604 is_initialized
= SD_DELETED
;
13609 /* If the init-declarator isn't initialized and isn't followed by a
13610 `,' or `;', it's not a valid init-declarator. */
13611 if (token
->type
!= CPP_COMMA
13612 && token
->type
!= CPP_SEMICOLON
)
13614 cp_parser_error (parser
, "expected initializer");
13615 return error_mark_node
;
13617 is_initialized
= SD_UNINITIALIZED
;
13618 initialization_kind
= CPP_EOF
;
13621 /* Because start_decl has side-effects, we should only call it if we
13622 know we're going ahead. By this point, we know that we cannot
13623 possibly be looking at any other construct. */
13624 cp_parser_commit_to_tentative_parse (parser
);
13626 /* If the decl specifiers were bad, issue an error now that we're
13627 sure this was intended to be a declarator. Then continue
13628 declaring the variable(s), as int, to try to cut down on further
13630 if (decl_specifiers
->any_specifiers_p
13631 && decl_specifiers
->type
== error_mark_node
)
13633 cp_parser_error (parser
, "invalid type in declaration");
13634 decl_specifiers
->type
= integer_type_node
;
13637 /* Check to see whether or not this declaration is a friend. */
13638 friend_p
= cp_parser_friend_p (decl_specifiers
);
13640 /* Enter the newly declared entry in the symbol table. If we're
13641 processing a declaration in a class-specifier, we wait until
13642 after processing the initializer. */
13645 if (parser
->in_unbraced_linkage_specification_p
)
13646 decl_specifiers
->storage_class
= sc_extern
;
13647 decl
= start_decl (declarator
, decl_specifiers
,
13648 is_initialized
, attributes
, prefix_attributes
,
13652 /* Enter the SCOPE. That way unqualified names appearing in the
13653 initializer will be looked up in SCOPE. */
13654 pushed_scope
= push_scope (scope
);
13656 /* Perform deferred access control checks, now that we know in which
13657 SCOPE the declared entity resides. */
13658 if (!member_p
&& decl
)
13660 tree saved_current_function_decl
= NULL_TREE
;
13662 /* If the entity being declared is a function, pretend that we
13663 are in its scope. If it is a `friend', it may have access to
13664 things that would not otherwise be accessible. */
13665 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13667 saved_current_function_decl
= current_function_decl
;
13668 current_function_decl
= decl
;
13671 /* Perform access checks for template parameters. */
13672 cp_parser_perform_template_parameter_access_checks (checks
);
13674 /* Perform the access control checks for the declarator and the
13675 decl-specifiers. */
13676 perform_deferred_access_checks ();
13678 /* Restore the saved value. */
13679 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13680 current_function_decl
= saved_current_function_decl
;
13683 /* Parse the initializer. */
13684 initializer
= NULL_TREE
;
13685 is_direct_init
= false;
13686 is_non_constant_init
= true;
13687 if (is_initialized
)
13689 if (function_declarator_p (declarator
))
13691 cp_token
*initializer_start_token
= cp_lexer_peek_token (parser
->lexer
);
13692 if (initialization_kind
== CPP_EQ
)
13693 initializer
= cp_parser_pure_specifier (parser
);
13696 /* If the declaration was erroneous, we don't really
13697 know what the user intended, so just silently
13698 consume the initializer. */
13699 if (decl
!= error_mark_node
)
13700 error_at (initializer_start_token
->location
,
13701 "initializer provided for function");
13702 cp_parser_skip_to_closing_parenthesis (parser
,
13703 /*recovering=*/true,
13704 /*or_comma=*/false,
13705 /*consume_paren=*/true);
13710 /* We want to record the extra mangling scope for in-class
13711 initializers of class members and initializers of static data
13712 member templates. The former is a C++0x feature which isn't
13713 implemented yet, and I expect it will involve deferring
13714 parsing of the initializer until end of class as with default
13715 arguments. So right here we only handle the latter. */
13716 if (!member_p
&& processing_template_decl
)
13717 start_lambda_scope (decl
);
13718 initializer
= cp_parser_initializer (parser
,
13720 &is_non_constant_init
);
13721 if (!member_p
&& processing_template_decl
)
13722 finish_lambda_scope ();
13726 /* The old parser allows attributes to appear after a parenthesized
13727 initializer. Mark Mitchell proposed removing this functionality
13728 on the GCC mailing lists on 2002-08-13. This parser accepts the
13729 attributes -- but ignores them. */
13730 if (cp_parser_allow_gnu_extensions_p (parser
)
13731 && initialization_kind
== CPP_OPEN_PAREN
)
13732 if (cp_parser_attributes_opt (parser
))
13733 warning (OPT_Wattributes
,
13734 "attributes after parenthesized initializer ignored");
13736 /* For an in-class declaration, use `grokfield' to create the
13742 pop_scope (pushed_scope
);
13743 pushed_scope
= false;
13745 decl
= grokfield (declarator
, decl_specifiers
,
13746 initializer
, !is_non_constant_init
,
13747 /*asmspec=*/NULL_TREE
,
13748 prefix_attributes
);
13749 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
13750 cp_parser_save_default_args (parser
, decl
);
13753 /* Finish processing the declaration. But, skip friend
13755 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
13757 cp_finish_decl (decl
,
13758 initializer
, !is_non_constant_init
,
13760 /* If the initializer is in parentheses, then this is
13761 a direct-initialization, which means that an
13762 `explicit' constructor is OK. Otherwise, an
13763 `explicit' constructor cannot be used. */
13764 ((is_direct_init
|| !is_initialized
)
13765 ? 0 : LOOKUP_ONLYCONVERTING
));
13767 else if ((cxx_dialect
!= cxx98
) && friend_p
13768 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
13769 /* Core issue #226 (C++0x only): A default template-argument
13770 shall not be specified in a friend class template
13772 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/1,
13773 /*is_partial=*/0, /*is_friend_decl=*/1);
13775 if (!friend_p
&& pushed_scope
)
13776 pop_scope (pushed_scope
);
13781 /* Parse a declarator.
13785 ptr-operator declarator
13787 abstract-declarator:
13788 ptr-operator abstract-declarator [opt]
13789 direct-abstract-declarator
13794 attributes [opt] direct-declarator
13795 attributes [opt] ptr-operator declarator
13797 abstract-declarator:
13798 attributes [opt] ptr-operator abstract-declarator [opt]
13799 attributes [opt] direct-abstract-declarator
13801 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13802 detect constructor, destructor or conversion operators. It is set
13803 to -1 if the declarator is a name, and +1 if it is a
13804 function. Otherwise it is set to zero. Usually you just want to
13805 test for >0, but internally the negative value is used.
13807 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13808 a decl-specifier-seq unless it declares a constructor, destructor,
13809 or conversion. It might seem that we could check this condition in
13810 semantic analysis, rather than parsing, but that makes it difficult
13811 to handle something like `f()'. We want to notice that there are
13812 no decl-specifiers, and therefore realize that this is an
13813 expression, not a declaration.)
13815 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13816 the declarator is a direct-declarator of the form "(...)".
13818 MEMBER_P is true iff this declarator is a member-declarator. */
13820 static cp_declarator
*
13821 cp_parser_declarator (cp_parser
* parser
,
13822 cp_parser_declarator_kind dcl_kind
,
13823 int* ctor_dtor_or_conv_p
,
13824 bool* parenthesized_p
,
13827 cp_declarator
*declarator
;
13828 enum tree_code code
;
13829 cp_cv_quals cv_quals
;
13831 tree attributes
= NULL_TREE
;
13833 /* Assume this is not a constructor, destructor, or type-conversion
13835 if (ctor_dtor_or_conv_p
)
13836 *ctor_dtor_or_conv_p
= 0;
13838 if (cp_parser_allow_gnu_extensions_p (parser
))
13839 attributes
= cp_parser_attributes_opt (parser
);
13841 /* Check for the ptr-operator production. */
13842 cp_parser_parse_tentatively (parser
);
13843 /* Parse the ptr-operator. */
13844 code
= cp_parser_ptr_operator (parser
,
13847 /* If that worked, then we have a ptr-operator. */
13848 if (cp_parser_parse_definitely (parser
))
13850 /* If a ptr-operator was found, then this declarator was not
13852 if (parenthesized_p
)
13853 *parenthesized_p
= true;
13854 /* The dependent declarator is optional if we are parsing an
13855 abstract-declarator. */
13856 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
13857 cp_parser_parse_tentatively (parser
);
13859 /* Parse the dependent declarator. */
13860 declarator
= cp_parser_declarator (parser
, dcl_kind
,
13861 /*ctor_dtor_or_conv_p=*/NULL
,
13862 /*parenthesized_p=*/NULL
,
13863 /*member_p=*/false);
13865 /* If we are parsing an abstract-declarator, we must handle the
13866 case where the dependent declarator is absent. */
13867 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
13868 && !cp_parser_parse_definitely (parser
))
13871 declarator
= cp_parser_make_indirect_declarator
13872 (code
, class_type
, cv_quals
, declarator
);
13874 /* Everything else is a direct-declarator. */
13877 if (parenthesized_p
)
13878 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
13880 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
13881 ctor_dtor_or_conv_p
,
13885 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
13886 declarator
->attributes
= attributes
;
13891 /* Parse a direct-declarator or direct-abstract-declarator.
13895 direct-declarator ( parameter-declaration-clause )
13896 cv-qualifier-seq [opt]
13897 exception-specification [opt]
13898 direct-declarator [ constant-expression [opt] ]
13901 direct-abstract-declarator:
13902 direct-abstract-declarator [opt]
13903 ( parameter-declaration-clause )
13904 cv-qualifier-seq [opt]
13905 exception-specification [opt]
13906 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13907 ( abstract-declarator )
13909 Returns a representation of the declarator. DCL_KIND is
13910 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13911 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13912 we are parsing a direct-declarator. It is
13913 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13914 of ambiguity we prefer an abstract declarator, as per
13915 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13916 cp_parser_declarator. */
13918 static cp_declarator
*
13919 cp_parser_direct_declarator (cp_parser
* parser
,
13920 cp_parser_declarator_kind dcl_kind
,
13921 int* ctor_dtor_or_conv_p
,
13925 cp_declarator
*declarator
= NULL
;
13926 tree scope
= NULL_TREE
;
13927 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
13928 bool saved_in_declarator_p
= parser
->in_declarator_p
;
13930 tree pushed_scope
= NULL_TREE
;
13934 /* Peek at the next token. */
13935 token
= cp_lexer_peek_token (parser
->lexer
);
13936 if (token
->type
== CPP_OPEN_PAREN
)
13938 /* This is either a parameter-declaration-clause, or a
13939 parenthesized declarator. When we know we are parsing a
13940 named declarator, it must be a parenthesized declarator
13941 if FIRST is true. For instance, `(int)' is a
13942 parameter-declaration-clause, with an omitted
13943 direct-abstract-declarator. But `((*))', is a
13944 parenthesized abstract declarator. Finally, when T is a
13945 template parameter `(T)' is a
13946 parameter-declaration-clause, and not a parenthesized
13949 We first try and parse a parameter-declaration-clause,
13950 and then try a nested declarator (if FIRST is true).
13952 It is not an error for it not to be a
13953 parameter-declaration-clause, even when FIRST is
13959 The first is the declaration of a function while the
13960 second is the definition of a variable, including its
13963 Having seen only the parenthesis, we cannot know which of
13964 these two alternatives should be selected. Even more
13965 complex are examples like:
13970 The former is a function-declaration; the latter is a
13971 variable initialization.
13973 Thus again, we try a parameter-declaration-clause, and if
13974 that fails, we back out and return. */
13976 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
13979 unsigned saved_num_template_parameter_lists
;
13980 bool is_declarator
= false;
13983 /* In a member-declarator, the only valid interpretation
13984 of a parenthesis is the start of a
13985 parameter-declaration-clause. (It is invalid to
13986 initialize a static data member with a parenthesized
13987 initializer; only the "=" form of initialization is
13990 cp_parser_parse_tentatively (parser
);
13992 /* Consume the `('. */
13993 cp_lexer_consume_token (parser
->lexer
);
13996 /* If this is going to be an abstract declarator, we're
13997 in a declarator and we can't have default args. */
13998 parser
->default_arg_ok_p
= false;
13999 parser
->in_declarator_p
= true;
14002 /* Inside the function parameter list, surrounding
14003 template-parameter-lists do not apply. */
14004 saved_num_template_parameter_lists
14005 = parser
->num_template_parameter_lists
;
14006 parser
->num_template_parameter_lists
= 0;
14008 begin_scope (sk_function_parms
, NULL_TREE
);
14010 /* Parse the parameter-declaration-clause. */
14011 params
= cp_parser_parameter_declaration_clause (parser
);
14013 parser
->num_template_parameter_lists
14014 = saved_num_template_parameter_lists
;
14016 /* If all went well, parse the cv-qualifier-seq and the
14017 exception-specification. */
14018 if (member_p
|| cp_parser_parse_definitely (parser
))
14020 cp_cv_quals cv_quals
;
14021 tree exception_specification
;
14024 is_declarator
= true;
14026 if (ctor_dtor_or_conv_p
)
14027 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
14029 /* Consume the `)'. */
14030 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
14032 /* Parse the cv-qualifier-seq. */
14033 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
14034 /* And the exception-specification. */
14035 exception_specification
14036 = cp_parser_exception_specification_opt (parser
);
14039 = cp_parser_late_return_type_opt (parser
);
14041 /* Create the function-declarator. */
14042 declarator
= make_call_declarator (declarator
,
14045 exception_specification
,
14047 /* Any subsequent parameter lists are to do with
14048 return type, so are not those of the declared
14050 parser
->default_arg_ok_p
= false;
14053 /* Remove the function parms from scope. */
14054 for (t
= current_binding_level
->names
; t
; t
= TREE_CHAIN (t
))
14055 pop_binding (DECL_NAME (t
), t
);
14059 /* Repeat the main loop. */
14063 /* If this is the first, we can try a parenthesized
14067 bool saved_in_type_id_in_expr_p
;
14069 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
14070 parser
->in_declarator_p
= saved_in_declarator_p
;
14072 /* Consume the `('. */
14073 cp_lexer_consume_token (parser
->lexer
);
14074 /* Parse the nested declarator. */
14075 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
14076 parser
->in_type_id_in_expr_p
= true;
14078 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
14079 /*parenthesized_p=*/NULL
,
14081 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
14083 /* Expect a `)'. */
14084 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
14085 declarator
= cp_error_declarator
;
14086 if (declarator
== cp_error_declarator
)
14089 goto handle_declarator
;
14091 /* Otherwise, we must be done. */
14095 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
14096 && token
->type
== CPP_OPEN_SQUARE
)
14098 /* Parse an array-declarator. */
14101 if (ctor_dtor_or_conv_p
)
14102 *ctor_dtor_or_conv_p
= 0;
14105 parser
->default_arg_ok_p
= false;
14106 parser
->in_declarator_p
= true;
14107 /* Consume the `['. */
14108 cp_lexer_consume_token (parser
->lexer
);
14109 /* Peek at the next token. */
14110 token
= cp_lexer_peek_token (parser
->lexer
);
14111 /* If the next token is `]', then there is no
14112 constant-expression. */
14113 if (token
->type
!= CPP_CLOSE_SQUARE
)
14115 bool non_constant_p
;
14118 = cp_parser_constant_expression (parser
,
14119 /*allow_non_constant=*/true,
14121 if (!non_constant_p
)
14122 bounds
= fold_non_dependent_expr (bounds
);
14123 /* Normally, the array bound must be an integral constant
14124 expression. However, as an extension, we allow VLAs
14125 in function scopes as long as they aren't part of a
14126 parameter declaration. */
14127 else if (!parser
->in_function_body
14128 || current_binding_level
->kind
== sk_function_parms
)
14130 cp_parser_error (parser
,
14131 "array bound is not an integer constant");
14132 bounds
= error_mark_node
;
14134 else if (processing_template_decl
&& !error_operand_p (bounds
))
14136 /* Remember this wasn't a constant-expression. */
14137 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
14138 TREE_SIDE_EFFECTS (bounds
) = 1;
14142 bounds
= NULL_TREE
;
14143 /* Look for the closing `]'. */
14144 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "%<]%>"))
14146 declarator
= cp_error_declarator
;
14150 declarator
= make_array_declarator (declarator
, bounds
);
14152 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
14155 tree qualifying_scope
;
14156 tree unqualified_name
;
14157 special_function_kind sfk
;
14159 bool pack_expansion_p
= false;
14160 cp_token
*declarator_id_start_token
;
14162 /* Parse a declarator-id */
14163 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
14166 cp_parser_parse_tentatively (parser
);
14168 /* If we see an ellipsis, we should be looking at a
14170 if (token
->type
== CPP_ELLIPSIS
)
14172 /* Consume the `...' */
14173 cp_lexer_consume_token (parser
->lexer
);
14175 pack_expansion_p
= true;
14179 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
14181 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
14182 qualifying_scope
= parser
->scope
;
14187 if (!unqualified_name
&& pack_expansion_p
)
14189 /* Check whether an error occurred. */
14190 okay
= !cp_parser_error_occurred (parser
);
14192 /* We already consumed the ellipsis to mark a
14193 parameter pack, but we have no way to report it,
14194 so abort the tentative parse. We will be exiting
14195 immediately anyway. */
14196 cp_parser_abort_tentative_parse (parser
);
14199 okay
= cp_parser_parse_definitely (parser
);
14202 unqualified_name
= error_mark_node
;
14203 else if (unqualified_name
14204 && (qualifying_scope
14205 || (TREE_CODE (unqualified_name
)
14206 != IDENTIFIER_NODE
)))
14208 cp_parser_error (parser
, "expected unqualified-id");
14209 unqualified_name
= error_mark_node
;
14213 if (!unqualified_name
)
14215 if (unqualified_name
== error_mark_node
)
14217 declarator
= cp_error_declarator
;
14218 pack_expansion_p
= false;
14219 declarator
->parameter_pack_p
= false;
14223 if (qualifying_scope
&& at_namespace_scope_p ()
14224 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
14226 /* In the declaration of a member of a template class
14227 outside of the class itself, the SCOPE will sometimes
14228 be a TYPENAME_TYPE. For example, given:
14230 template <typename T>
14231 int S<T>::R::i = 3;
14233 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14234 this context, we must resolve S<T>::R to an ordinary
14235 type, rather than a typename type.
14237 The reason we normally avoid resolving TYPENAME_TYPEs
14238 is that a specialization of `S' might render
14239 `S<T>::R' not a type. However, if `S' is
14240 specialized, then this `i' will not be used, so there
14241 is no harm in resolving the types here. */
14244 /* Resolve the TYPENAME_TYPE. */
14245 type
= resolve_typename_type (qualifying_scope
,
14246 /*only_current_p=*/false);
14247 /* If that failed, the declarator is invalid. */
14248 if (TREE_CODE (type
) == TYPENAME_TYPE
)
14250 if (typedef_variant_p (type
))
14251 error_at (declarator_id_start_token
->location
,
14252 "cannot define member of dependent typedef "
14255 error_at (declarator_id_start_token
->location
,
14256 "%<%T::%E%> is not a type",
14257 TYPE_CONTEXT (qualifying_scope
),
14258 TYPE_IDENTIFIER (qualifying_scope
));
14260 qualifying_scope
= type
;
14265 if (unqualified_name
)
14269 if (qualifying_scope
14270 && CLASS_TYPE_P (qualifying_scope
))
14271 class_type
= qualifying_scope
;
14273 class_type
= current_class_type
;
14275 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
14277 tree name_type
= TREE_TYPE (unqualified_name
);
14278 if (class_type
&& same_type_p (name_type
, class_type
))
14280 if (qualifying_scope
14281 && CLASSTYPE_USE_TEMPLATE (name_type
))
14283 error_at (declarator_id_start_token
->location
,
14284 "invalid use of constructor as a template");
14285 inform (declarator_id_start_token
->location
,
14286 "use %<%T::%D%> instead of %<%T::%D%> to "
14287 "name the constructor in a qualified name",
14289 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
14290 class_type
, name_type
);
14291 declarator
= cp_error_declarator
;
14295 unqualified_name
= constructor_name (class_type
);
14299 /* We do not attempt to print the declarator
14300 here because we do not have enough
14301 information about its original syntactic
14303 cp_parser_error (parser
, "invalid declarator");
14304 declarator
= cp_error_declarator
;
14311 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
14312 sfk
= sfk_destructor
;
14313 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
14314 sfk
= sfk_conversion
;
14315 else if (/* There's no way to declare a constructor
14316 for an anonymous type, even if the type
14317 got a name for linkage purposes. */
14318 !TYPE_WAS_ANONYMOUS (class_type
)
14319 && constructor_name_p (unqualified_name
,
14322 unqualified_name
= constructor_name (class_type
);
14323 sfk
= sfk_constructor
;
14325 else if (is_overloaded_fn (unqualified_name
)
14326 && DECL_CONSTRUCTOR_P (get_first_fn
14327 (unqualified_name
)))
14328 sfk
= sfk_constructor
;
14330 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
14331 *ctor_dtor_or_conv_p
= -1;
14334 declarator
= make_id_declarator (qualifying_scope
,
14337 declarator
->id_loc
= token
->location
;
14338 declarator
->parameter_pack_p
= pack_expansion_p
;
14340 if (pack_expansion_p
)
14341 maybe_warn_variadic_templates ();
14344 handle_declarator
:;
14345 scope
= get_scope_of_declarator (declarator
);
14347 /* Any names that appear after the declarator-id for a
14348 member are looked up in the containing scope. */
14349 pushed_scope
= push_scope (scope
);
14350 parser
->in_declarator_p
= true;
14351 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
14352 || (declarator
&& declarator
->kind
== cdk_id
))
14353 /* Default args are only allowed on function
14355 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
14357 parser
->default_arg_ok_p
= false;
14366 /* For an abstract declarator, we might wind up with nothing at this
14367 point. That's an error; the declarator is not optional. */
14369 cp_parser_error (parser
, "expected declarator");
14371 /* If we entered a scope, we must exit it now. */
14373 pop_scope (pushed_scope
);
14375 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
14376 parser
->in_declarator_p
= saved_in_declarator_p
;
14381 /* Parse a ptr-operator.
14384 * cv-qualifier-seq [opt]
14386 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14391 & cv-qualifier-seq [opt]
14393 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14394 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14395 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14396 filled in with the TYPE containing the member. *CV_QUALS is
14397 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14398 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14399 Note that the tree codes returned by this function have nothing
14400 to do with the types of trees that will be eventually be created
14401 to represent the pointer or reference type being parsed. They are
14402 just constants with suggestive names. */
14403 static enum tree_code
14404 cp_parser_ptr_operator (cp_parser
* parser
,
14406 cp_cv_quals
*cv_quals
)
14408 enum tree_code code
= ERROR_MARK
;
14411 /* Assume that it's not a pointer-to-member. */
14413 /* And that there are no cv-qualifiers. */
14414 *cv_quals
= TYPE_UNQUALIFIED
;
14416 /* Peek at the next token. */
14417 token
= cp_lexer_peek_token (parser
->lexer
);
14419 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14420 if (token
->type
== CPP_MULT
)
14421 code
= INDIRECT_REF
;
14422 else if (token
->type
== CPP_AND
)
14424 else if ((cxx_dialect
!= cxx98
) &&
14425 token
->type
== CPP_AND_AND
) /* C++0x only */
14426 code
= NON_LVALUE_EXPR
;
14428 if (code
!= ERROR_MARK
)
14430 /* Consume the `*', `&' or `&&'. */
14431 cp_lexer_consume_token (parser
->lexer
);
14433 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14434 `&', if we are allowing GNU extensions. (The only qualifier
14435 that can legally appear after `&' is `restrict', but that is
14436 enforced during semantic analysis. */
14437 if (code
== INDIRECT_REF
14438 || cp_parser_allow_gnu_extensions_p (parser
))
14439 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
14443 /* Try the pointer-to-member case. */
14444 cp_parser_parse_tentatively (parser
);
14445 /* Look for the optional `::' operator. */
14446 cp_parser_global_scope_opt (parser
,
14447 /*current_scope_valid_p=*/false);
14448 /* Look for the nested-name specifier. */
14449 token
= cp_lexer_peek_token (parser
->lexer
);
14450 cp_parser_nested_name_specifier (parser
,
14451 /*typename_keyword_p=*/false,
14452 /*check_dependency_p=*/true,
14454 /*is_declaration=*/false);
14455 /* If we found it, and the next token is a `*', then we are
14456 indeed looking at a pointer-to-member operator. */
14457 if (!cp_parser_error_occurred (parser
)
14458 && cp_parser_require (parser
, CPP_MULT
, "%<*%>"))
14460 /* Indicate that the `*' operator was used. */
14461 code
= INDIRECT_REF
;
14463 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
14464 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
14467 /* The type of which the member is a member is given by the
14469 *type
= parser
->scope
;
14470 /* The next name will not be qualified. */
14471 parser
->scope
= NULL_TREE
;
14472 parser
->qualifying_scope
= NULL_TREE
;
14473 parser
->object_scope
= NULL_TREE
;
14474 /* Look for the optional cv-qualifier-seq. */
14475 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
14478 /* If that didn't work we don't have a ptr-operator. */
14479 if (!cp_parser_parse_definitely (parser
))
14480 cp_parser_error (parser
, "expected ptr-operator");
14486 /* Parse an (optional) cv-qualifier-seq.
14489 cv-qualifier cv-qualifier-seq [opt]
14500 Returns a bitmask representing the cv-qualifiers. */
14503 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
14505 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
14510 cp_cv_quals cv_qualifier
;
14512 /* Peek at the next token. */
14513 token
= cp_lexer_peek_token (parser
->lexer
);
14514 /* See if it's a cv-qualifier. */
14515 switch (token
->keyword
)
14518 cv_qualifier
= TYPE_QUAL_CONST
;
14522 cv_qualifier
= TYPE_QUAL_VOLATILE
;
14526 cv_qualifier
= TYPE_QUAL_RESTRICT
;
14530 cv_qualifier
= TYPE_UNQUALIFIED
;
14537 if (cv_quals
& cv_qualifier
)
14539 error_at (token
->location
, "duplicate cv-qualifier");
14540 cp_lexer_purge_token (parser
->lexer
);
14544 cp_lexer_consume_token (parser
->lexer
);
14545 cv_quals
|= cv_qualifier
;
14552 /* Parse a late-specified return type, if any. This is not a separate
14553 non-terminal, but part of a function declarator, which looks like
14555 -> trailing-type-specifier-seq abstract-declarator(opt)
14557 Returns the type indicated by the type-id. */
14560 cp_parser_late_return_type_opt (cp_parser
* parser
)
14564 /* Peek at the next token. */
14565 token
= cp_lexer_peek_token (parser
->lexer
);
14566 /* A late-specified return type is indicated by an initial '->'. */
14567 if (token
->type
!= CPP_DEREF
)
14570 /* Consume the ->. */
14571 cp_lexer_consume_token (parser
->lexer
);
14573 return cp_parser_trailing_type_id (parser
);
14576 /* Parse a declarator-id.
14580 :: [opt] nested-name-specifier [opt] type-name
14582 In the `id-expression' case, the value returned is as for
14583 cp_parser_id_expression if the id-expression was an unqualified-id.
14584 If the id-expression was a qualified-id, then a SCOPE_REF is
14585 returned. The first operand is the scope (either a NAMESPACE_DECL
14586 or TREE_TYPE), but the second is still just a representation of an
14590 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
14593 /* The expression must be an id-expression. Assume that qualified
14594 names are the names of types so that:
14597 int S<T>::R::i = 3;
14599 will work; we must treat `S<T>::R' as the name of a type.
14600 Similarly, assume that qualified names are templates, where
14604 int S<T>::R<T>::i = 3;
14607 id
= cp_parser_id_expression (parser
,
14608 /*template_keyword_p=*/false,
14609 /*check_dependency_p=*/false,
14610 /*template_p=*/NULL
,
14611 /*declarator_p=*/true,
14613 if (id
&& BASELINK_P (id
))
14614 id
= BASELINK_FUNCTIONS (id
);
14618 /* Parse a type-id.
14621 type-specifier-seq abstract-declarator [opt]
14623 Returns the TYPE specified. */
14626 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
14627 bool is_trailing_return
)
14629 cp_decl_specifier_seq type_specifier_seq
;
14630 cp_declarator
*abstract_declarator
;
14632 /* Parse the type-specifier-seq. */
14633 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14634 is_trailing_return
,
14635 &type_specifier_seq
);
14636 if (type_specifier_seq
.type
== error_mark_node
)
14637 return error_mark_node
;
14639 /* There might or might not be an abstract declarator. */
14640 cp_parser_parse_tentatively (parser
);
14641 /* Look for the declarator. */
14642 abstract_declarator
14643 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
14644 /*parenthesized_p=*/NULL
,
14645 /*member_p=*/false);
14646 /* Check to see if there really was a declarator. */
14647 if (!cp_parser_parse_definitely (parser
))
14648 abstract_declarator
= NULL
;
14650 if (type_specifier_seq
.type
14651 && type_uses_auto (type_specifier_seq
.type
))
14653 /* A type-id with type 'auto' is only ok if the abstract declarator
14654 is a function declarator with a late-specified return type. */
14655 if (abstract_declarator
14656 && abstract_declarator
->kind
== cdk_function
14657 && abstract_declarator
->u
.function
.late_return_type
)
14661 error ("invalid use of %<auto%>");
14662 return error_mark_node
;
14666 return groktypename (&type_specifier_seq
, abstract_declarator
,
14670 static tree
cp_parser_type_id (cp_parser
*parser
)
14672 return cp_parser_type_id_1 (parser
, false, false);
14675 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
14677 return cp_parser_type_id_1 (parser
, true, false);
14680 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
14682 return cp_parser_type_id_1 (parser
, false, true);
14685 /* Parse a type-specifier-seq.
14687 type-specifier-seq:
14688 type-specifier type-specifier-seq [opt]
14692 type-specifier-seq:
14693 attributes type-specifier-seq [opt]
14695 If IS_DECLARATION is true, we are at the start of a "condition" or
14696 exception-declaration, so we might be followed by a declarator-id.
14698 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14699 i.e. we've just seen "->".
14701 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14704 cp_parser_type_specifier_seq (cp_parser
* parser
,
14705 bool is_declaration
,
14706 bool is_trailing_return
,
14707 cp_decl_specifier_seq
*type_specifier_seq
)
14709 bool seen_type_specifier
= false;
14710 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
14711 cp_token
*start_token
= NULL
;
14713 /* Clear the TYPE_SPECIFIER_SEQ. */
14714 clear_decl_specs (type_specifier_seq
);
14716 /* In the context of a trailing return type, enum E { } is an
14717 elaborated-type-specifier followed by a function-body, not an
14719 if (is_trailing_return
)
14720 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
14722 /* Parse the type-specifiers and attributes. */
14725 tree type_specifier
;
14726 bool is_cv_qualifier
;
14728 /* Check for attributes first. */
14729 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
14731 type_specifier_seq
->attributes
=
14732 chainon (type_specifier_seq
->attributes
,
14733 cp_parser_attributes_opt (parser
));
14737 /* record the token of the beginning of the type specifier seq,
14738 for error reporting purposes*/
14740 start_token
= cp_lexer_peek_token (parser
->lexer
);
14742 /* Look for the type-specifier. */
14743 type_specifier
= cp_parser_type_specifier (parser
,
14745 type_specifier_seq
,
14746 /*is_declaration=*/false,
14749 if (!type_specifier
)
14751 /* If the first type-specifier could not be found, this is not a
14752 type-specifier-seq at all. */
14753 if (!seen_type_specifier
)
14755 cp_parser_error (parser
, "expected type-specifier");
14756 type_specifier_seq
->type
= error_mark_node
;
14759 /* If subsequent type-specifiers could not be found, the
14760 type-specifier-seq is complete. */
14764 seen_type_specifier
= true;
14765 /* The standard says that a condition can be:
14767 type-specifier-seq declarator = assignment-expression
14774 we should treat the "S" as a declarator, not as a
14775 type-specifier. The standard doesn't say that explicitly for
14776 type-specifier-seq, but it does say that for
14777 decl-specifier-seq in an ordinary declaration. Perhaps it
14778 would be clearer just to allow a decl-specifier-seq here, and
14779 then add a semantic restriction that if any decl-specifiers
14780 that are not type-specifiers appear, the program is invalid. */
14781 if (is_declaration
&& !is_cv_qualifier
)
14782 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
14785 cp_parser_check_decl_spec (type_specifier_seq
, start_token
->location
);
14788 /* Parse a parameter-declaration-clause.
14790 parameter-declaration-clause:
14791 parameter-declaration-list [opt] ... [opt]
14792 parameter-declaration-list , ...
14794 Returns a representation for the parameter declarations. A return
14795 value of NULL indicates a parameter-declaration-clause consisting
14796 only of an ellipsis. */
14799 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
14806 /* Peek at the next token. */
14807 token
= cp_lexer_peek_token (parser
->lexer
);
14808 /* Check for trivial parameter-declaration-clauses. */
14809 if (token
->type
== CPP_ELLIPSIS
)
14811 /* Consume the `...' token. */
14812 cp_lexer_consume_token (parser
->lexer
);
14815 else if (token
->type
== CPP_CLOSE_PAREN
)
14816 /* There are no parameters. */
14818 #ifndef NO_IMPLICIT_EXTERN_C
14819 if (in_system_header
&& current_class_type
== NULL
14820 && current_lang_name
== lang_name_c
)
14824 return void_list_node
;
14826 /* Check for `(void)', too, which is a special case. */
14827 else if (token
->keyword
== RID_VOID
14828 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
14829 == CPP_CLOSE_PAREN
))
14831 /* Consume the `void' token. */
14832 cp_lexer_consume_token (parser
->lexer
);
14833 /* There are no parameters. */
14834 return void_list_node
;
14837 /* Parse the parameter-declaration-list. */
14838 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
14839 /* If a parse error occurred while parsing the
14840 parameter-declaration-list, then the entire
14841 parameter-declaration-clause is erroneous. */
14845 /* Peek at the next token. */
14846 token
= cp_lexer_peek_token (parser
->lexer
);
14847 /* If it's a `,', the clause should terminate with an ellipsis. */
14848 if (token
->type
== CPP_COMMA
)
14850 /* Consume the `,'. */
14851 cp_lexer_consume_token (parser
->lexer
);
14852 /* Expect an ellipsis. */
14854 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "%<...%>") != NULL
);
14856 /* It might also be `...' if the optional trailing `,' was
14858 else if (token
->type
== CPP_ELLIPSIS
)
14860 /* Consume the `...' token. */
14861 cp_lexer_consume_token (parser
->lexer
);
14862 /* And remember that we saw it. */
14866 ellipsis_p
= false;
14868 /* Finish the parameter list. */
14870 parameters
= chainon (parameters
, void_list_node
);
14875 /* Parse a parameter-declaration-list.
14877 parameter-declaration-list:
14878 parameter-declaration
14879 parameter-declaration-list , parameter-declaration
14881 Returns a representation of the parameter-declaration-list, as for
14882 cp_parser_parameter_declaration_clause. However, the
14883 `void_list_node' is never appended to the list. Upon return,
14884 *IS_ERROR will be true iff an error occurred. */
14887 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
14889 tree parameters
= NULL_TREE
;
14890 tree
*tail
= ¶meters
;
14891 bool saved_in_unbraced_linkage_specification_p
;
14894 /* Assume all will go well. */
14896 /* The special considerations that apply to a function within an
14897 unbraced linkage specifications do not apply to the parameters
14898 to the function. */
14899 saved_in_unbraced_linkage_specification_p
14900 = parser
->in_unbraced_linkage_specification_p
;
14901 parser
->in_unbraced_linkage_specification_p
= false;
14903 /* Look for more parameters. */
14906 cp_parameter_declarator
*parameter
;
14907 tree decl
= error_mark_node
;
14908 bool parenthesized_p
;
14909 /* Parse the parameter. */
14911 = cp_parser_parameter_declaration (parser
,
14912 /*template_parm_p=*/false,
14915 /* We don't know yet if the enclosing context is deprecated, so wait
14916 and warn in grokparms if appropriate. */
14917 deprecated_state
= DEPRECATED_SUPPRESS
;
14920 decl
= grokdeclarator (parameter
->declarator
,
14921 ¶meter
->decl_specifiers
,
14923 parameter
->default_argument
!= NULL_TREE
,
14924 ¶meter
->decl_specifiers
.attributes
);
14926 deprecated_state
= DEPRECATED_NORMAL
;
14928 /* If a parse error occurred parsing the parameter declaration,
14929 then the entire parameter-declaration-list is erroneous. */
14930 if (decl
== error_mark_node
)
14933 parameters
= error_mark_node
;
14937 if (parameter
->decl_specifiers
.attributes
)
14938 cplus_decl_attributes (&decl
,
14939 parameter
->decl_specifiers
.attributes
,
14941 if (DECL_NAME (decl
))
14942 decl
= pushdecl (decl
);
14944 if (decl
!= error_mark_node
)
14946 retrofit_lang_decl (decl
);
14947 DECL_PARM_INDEX (decl
) = ++index
;
14950 /* Add the new parameter to the list. */
14951 *tail
= build_tree_list (parameter
->default_argument
, decl
);
14952 tail
= &TREE_CHAIN (*tail
);
14954 /* Peek at the next token. */
14955 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
14956 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
14957 /* These are for Objective-C++ */
14958 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
14959 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14960 /* The parameter-declaration-list is complete. */
14962 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
14966 /* Peek at the next token. */
14967 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
14968 /* If it's an ellipsis, then the list is complete. */
14969 if (token
->type
== CPP_ELLIPSIS
)
14971 /* Otherwise, there must be more parameters. Consume the
14973 cp_lexer_consume_token (parser
->lexer
);
14974 /* When parsing something like:
14976 int i(float f, double d)
14978 we can tell after seeing the declaration for "f" that we
14979 are not looking at an initialization of a variable "i",
14980 but rather at the declaration of a function "i".
14982 Due to the fact that the parsing of template arguments
14983 (as specified to a template-id) requires backtracking we
14984 cannot use this technique when inside a template argument
14986 if (!parser
->in_template_argument_list_p
14987 && !parser
->in_type_id_in_expr_p
14988 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
14989 /* However, a parameter-declaration of the form
14990 "foat(f)" (which is a valid declaration of a
14991 parameter "f") can also be interpreted as an
14992 expression (the conversion of "f" to "float"). */
14993 && !parenthesized_p
)
14994 cp_parser_commit_to_tentative_parse (parser
);
14998 cp_parser_error (parser
, "expected %<,%> or %<...%>");
14999 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
15000 cp_parser_skip_to_closing_parenthesis (parser
,
15001 /*recovering=*/true,
15002 /*or_comma=*/false,
15003 /*consume_paren=*/false);
15008 parser
->in_unbraced_linkage_specification_p
15009 = saved_in_unbraced_linkage_specification_p
;
15014 /* Parse a parameter declaration.
15016 parameter-declaration:
15017 decl-specifier-seq ... [opt] declarator
15018 decl-specifier-seq declarator = assignment-expression
15019 decl-specifier-seq ... [opt] abstract-declarator [opt]
15020 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15022 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15023 declares a template parameter. (In that case, a non-nested `>'
15024 token encountered during the parsing of the assignment-expression
15025 is not interpreted as a greater-than operator.)
15027 Returns a representation of the parameter, or NULL if an error
15028 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15029 true iff the declarator is of the form "(p)". */
15031 static cp_parameter_declarator
*
15032 cp_parser_parameter_declaration (cp_parser
*parser
,
15033 bool template_parm_p
,
15034 bool *parenthesized_p
)
15036 int declares_class_or_enum
;
15037 cp_decl_specifier_seq decl_specifiers
;
15038 cp_declarator
*declarator
;
15039 tree default_argument
;
15040 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
15041 const char *saved_message
;
15043 /* In a template parameter, `>' is not an operator.
15047 When parsing a default template-argument for a non-type
15048 template-parameter, the first non-nested `>' is taken as the end
15049 of the template parameter-list rather than a greater-than
15052 /* Type definitions may not appear in parameter types. */
15053 saved_message
= parser
->type_definition_forbidden_message
;
15054 parser
->type_definition_forbidden_message
15055 = G_("types may not be defined in parameter types");
15057 /* Parse the declaration-specifiers. */
15058 cp_parser_decl_specifier_seq (parser
,
15059 CP_PARSER_FLAGS_NONE
,
15061 &declares_class_or_enum
);
15063 /* Complain about missing 'typename' or other invalid type names. */
15064 if (!decl_specifiers
.any_type_specifiers_p
)
15065 cp_parser_parse_and_diagnose_invalid_type_name (parser
);
15067 /* If an error occurred, there's no reason to attempt to parse the
15068 rest of the declaration. */
15069 if (cp_parser_error_occurred (parser
))
15071 parser
->type_definition_forbidden_message
= saved_message
;
15075 /* Peek at the next token. */
15076 token
= cp_lexer_peek_token (parser
->lexer
);
15078 /* If the next token is a `)', `,', `=', `>', or `...', then there
15079 is no declarator. However, when variadic templates are enabled,
15080 there may be a declarator following `...'. */
15081 if (token
->type
== CPP_CLOSE_PAREN
15082 || token
->type
== CPP_COMMA
15083 || token
->type
== CPP_EQ
15084 || token
->type
== CPP_GREATER
)
15087 if (parenthesized_p
)
15088 *parenthesized_p
= false;
15090 /* Otherwise, there should be a declarator. */
15093 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
15094 parser
->default_arg_ok_p
= false;
15096 /* After seeing a decl-specifier-seq, if the next token is not a
15097 "(", there is no possibility that the code is a valid
15098 expression. Therefore, if parsing tentatively, we commit at
15100 if (!parser
->in_template_argument_list_p
15101 /* In an expression context, having seen:
15105 we cannot be sure whether we are looking at a
15106 function-type (taking a "char" as a parameter) or a cast
15107 of some object of type "char" to "int". */
15108 && !parser
->in_type_id_in_expr_p
15109 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
15110 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
15111 cp_parser_commit_to_tentative_parse (parser
);
15112 /* Parse the declarator. */
15113 declarator_token_start
= token
;
15114 declarator
= cp_parser_declarator (parser
,
15115 CP_PARSER_DECLARATOR_EITHER
,
15116 /*ctor_dtor_or_conv_p=*/NULL
,
15118 /*member_p=*/false);
15119 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
15120 /* After the declarator, allow more attributes. */
15121 decl_specifiers
.attributes
15122 = chainon (decl_specifiers
.attributes
,
15123 cp_parser_attributes_opt (parser
));
15126 /* If the next token is an ellipsis, and we have not seen a
15127 declarator name, and the type of the declarator contains parameter
15128 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15129 a parameter pack expansion expression. Otherwise, leave the
15130 ellipsis for a C-style variadic function. */
15131 token
= cp_lexer_peek_token (parser
->lexer
);
15132 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15134 tree type
= decl_specifiers
.type
;
15136 if (type
&& DECL_P (type
))
15137 type
= TREE_TYPE (type
);
15140 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
15141 && declarator_can_be_parameter_pack (declarator
)
15142 && (!declarator
|| !declarator
->parameter_pack_p
)
15143 && uses_parameter_packs (type
))
15145 /* Consume the `...'. */
15146 cp_lexer_consume_token (parser
->lexer
);
15147 maybe_warn_variadic_templates ();
15149 /* Build a pack expansion type */
15151 declarator
->parameter_pack_p
= true;
15153 decl_specifiers
.type
= make_pack_expansion (type
);
15157 /* The restriction on defining new types applies only to the type
15158 of the parameter, not to the default argument. */
15159 parser
->type_definition_forbidden_message
= saved_message
;
15161 /* If the next token is `=', then process a default argument. */
15162 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15164 /* Consume the `='. */
15165 cp_lexer_consume_token (parser
->lexer
);
15167 /* If we are defining a class, then the tokens that make up the
15168 default argument must be saved and processed later. */
15169 if (!template_parm_p
&& at_class_scope_p ()
15170 && TYPE_BEING_DEFINED (current_class_type
)
15171 && !LAMBDA_TYPE_P (current_class_type
))
15173 unsigned depth
= 0;
15174 int maybe_template_id
= 0;
15175 cp_token
*first_token
;
15178 /* Add tokens until we have processed the entire default
15179 argument. We add the range [first_token, token). */
15180 first_token
= cp_lexer_peek_token (parser
->lexer
);
15185 /* Peek at the next token. */
15186 token
= cp_lexer_peek_token (parser
->lexer
);
15187 /* What we do depends on what token we have. */
15188 switch (token
->type
)
15190 /* In valid code, a default argument must be
15191 immediately followed by a `,' `)', or `...'. */
15193 if (depth
== 0 && maybe_template_id
)
15195 /* If we've seen a '<', we might be in a
15196 template-argument-list. Until Core issue 325 is
15197 resolved, we don't know how this situation ought
15198 to be handled, so try to DTRT. We check whether
15199 what comes after the comma is a valid parameter
15200 declaration list. If it is, then the comma ends
15201 the default argument; otherwise the default
15202 argument continues. */
15203 bool error
= false;
15205 /* Set ITALP so cp_parser_parameter_declaration_list
15206 doesn't decide to commit to this parse. */
15207 bool saved_italp
= parser
->in_template_argument_list_p
;
15208 parser
->in_template_argument_list_p
= true;
15210 cp_parser_parse_tentatively (parser
);
15211 cp_lexer_consume_token (parser
->lexer
);
15212 cp_parser_parameter_declaration_list (parser
, &error
);
15213 if (!cp_parser_error_occurred (parser
) && !error
)
15215 cp_parser_abort_tentative_parse (parser
);
15217 parser
->in_template_argument_list_p
= saved_italp
;
15220 case CPP_CLOSE_PAREN
:
15222 /* If we run into a non-nested `;', `}', or `]',
15223 then the code is invalid -- but the default
15224 argument is certainly over. */
15225 case CPP_SEMICOLON
:
15226 case CPP_CLOSE_BRACE
:
15227 case CPP_CLOSE_SQUARE
:
15230 /* Update DEPTH, if necessary. */
15231 else if (token
->type
== CPP_CLOSE_PAREN
15232 || token
->type
== CPP_CLOSE_BRACE
15233 || token
->type
== CPP_CLOSE_SQUARE
)
15237 case CPP_OPEN_PAREN
:
15238 case CPP_OPEN_SQUARE
:
15239 case CPP_OPEN_BRACE
:
15245 /* This might be the comparison operator, or it might
15246 start a template argument list. */
15247 ++maybe_template_id
;
15251 if (cxx_dialect
== cxx98
)
15253 /* Fall through for C++0x, which treats the `>>'
15254 operator like two `>' tokens in certain
15260 /* This might be an operator, or it might close a
15261 template argument list. But if a previous '<'
15262 started a template argument list, this will have
15263 closed it, so we can't be in one anymore. */
15264 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
15265 if (maybe_template_id
< 0)
15266 maybe_template_id
= 0;
15270 /* If we run out of tokens, issue an error message. */
15272 case CPP_PRAGMA_EOL
:
15273 error_at (token
->location
, "file ends in default argument");
15279 /* In these cases, we should look for template-ids.
15280 For example, if the default argument is
15281 `X<int, double>()', we need to do name lookup to
15282 figure out whether or not `X' is a template; if
15283 so, the `,' does not end the default argument.
15285 That is not yet done. */
15292 /* If we've reached the end, stop. */
15296 /* Add the token to the token block. */
15297 token
= cp_lexer_consume_token (parser
->lexer
);
15300 /* Create a DEFAULT_ARG to represent the unparsed default
15302 default_argument
= make_node (DEFAULT_ARG
);
15303 DEFARG_TOKENS (default_argument
)
15304 = cp_token_cache_new (first_token
, token
);
15305 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
15307 /* Outside of a class definition, we can just parse the
15308 assignment-expression. */
15311 token
= cp_lexer_peek_token (parser
->lexer
);
15313 = cp_parser_default_argument (parser
, template_parm_p
);
15316 if (!parser
->default_arg_ok_p
)
15318 if (flag_permissive
)
15319 warning (0, "deprecated use of default argument for parameter of non-function");
15322 error_at (token
->location
,
15323 "default arguments are only "
15324 "permitted for function parameters");
15325 default_argument
= NULL_TREE
;
15328 else if ((declarator
&& declarator
->parameter_pack_p
)
15329 || (decl_specifiers
.type
15330 && PACK_EXPANSION_P (decl_specifiers
.type
)))
15332 /* Find the name of the parameter pack. */
15333 cp_declarator
*id_declarator
= declarator
;
15334 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
15335 id_declarator
= id_declarator
->declarator
;
15337 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
15338 error_at (declarator_token_start
->location
,
15340 ? "template parameter pack %qD"
15341 " cannot have a default argument"
15342 : "parameter pack %qD cannot have a default argument",
15343 id_declarator
->u
.id
.unqualified_name
);
15345 error_at (declarator_token_start
->location
,
15347 ? "template parameter pack cannot have a default argument"
15348 : "parameter pack cannot have a default argument");
15350 default_argument
= NULL_TREE
;
15354 default_argument
= NULL_TREE
;
15356 return make_parameter_declarator (&decl_specifiers
,
15361 /* Parse a default argument and return it.
15363 TEMPLATE_PARM_P is true if this is a default argument for a
15364 non-type template parameter. */
15366 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
15368 tree default_argument
= NULL_TREE
;
15369 bool saved_greater_than_is_operator_p
;
15370 bool saved_local_variables_forbidden_p
;
15372 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15374 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
15375 parser
->greater_than_is_operator_p
= !template_parm_p
;
15376 /* Local variable names (and the `this' keyword) may not
15377 appear in a default argument. */
15378 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
15379 parser
->local_variables_forbidden_p
= true;
15380 /* Parse the assignment-expression. */
15381 if (template_parm_p
)
15382 push_deferring_access_checks (dk_no_deferred
);
15384 = cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
15385 if (template_parm_p
)
15386 pop_deferring_access_checks ();
15387 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
15388 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
15390 return default_argument
;
15393 /* Parse a function-body.
15396 compound_statement */
15399 cp_parser_function_body (cp_parser
*parser
)
15401 cp_parser_compound_statement (parser
, NULL
, false);
15404 /* Parse a ctor-initializer-opt followed by a function-body. Return
15405 true if a ctor-initializer was present. */
15408 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
15411 bool ctor_initializer_p
;
15413 /* Begin the function body. */
15414 body
= begin_function_body ();
15415 /* Parse the optional ctor-initializer. */
15416 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
15417 /* Parse the function-body. */
15418 cp_parser_function_body (parser
);
15419 /* Finish the function body. */
15420 finish_function_body (body
);
15422 return ctor_initializer_p
;
15425 /* Parse an initializer.
15428 = initializer-clause
15429 ( expression-list )
15431 Returns an expression representing the initializer. If no
15432 initializer is present, NULL_TREE is returned.
15434 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15435 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15436 set to TRUE if there is no initializer present. If there is an
15437 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15438 is set to true; otherwise it is set to false. */
15441 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
15442 bool* non_constant_p
)
15447 /* Peek at the next token. */
15448 token
= cp_lexer_peek_token (parser
->lexer
);
15450 /* Let our caller know whether or not this initializer was
15452 *is_direct_init
= (token
->type
!= CPP_EQ
);
15453 /* Assume that the initializer is constant. */
15454 *non_constant_p
= false;
15456 if (token
->type
== CPP_EQ
)
15458 /* Consume the `='. */
15459 cp_lexer_consume_token (parser
->lexer
);
15460 /* Parse the initializer-clause. */
15461 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
15463 else if (token
->type
== CPP_OPEN_PAREN
)
15466 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
15468 /*allow_expansion_p=*/true,
15471 return error_mark_node
;
15472 init
= build_tree_list_vec (vec
);
15473 release_tree_vector (vec
);
15475 else if (token
->type
== CPP_OPEN_BRACE
)
15477 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
15478 init
= cp_parser_braced_list (parser
, non_constant_p
);
15479 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
15483 /* Anything else is an error. */
15484 cp_parser_error (parser
, "expected initializer");
15485 init
= error_mark_node
;
15491 /* Parse an initializer-clause.
15493 initializer-clause:
15494 assignment-expression
15497 Returns an expression representing the initializer.
15499 If the `assignment-expression' production is used the value
15500 returned is simply a representation for the expression.
15502 Otherwise, calls cp_parser_braced_list. */
15505 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
15509 /* Assume the expression is constant. */
15510 *non_constant_p
= false;
15512 /* If it is not a `{', then we are looking at an
15513 assignment-expression. */
15514 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
15517 = cp_parser_constant_expression (parser
,
15518 /*allow_non_constant_p=*/true,
15520 if (!*non_constant_p
)
15521 initializer
= fold_non_dependent_expr (initializer
);
15524 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
15526 return initializer
;
15529 /* Parse a brace-enclosed initializer list.
15532 { initializer-list , [opt] }
15535 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15536 the elements of the initializer-list (or NULL, if the last
15537 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15538 NULL_TREE. There is no way to detect whether or not the optional
15539 trailing `,' was provided. NON_CONSTANT_P is as for
15540 cp_parser_initializer. */
15543 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
15547 /* Consume the `{' token. */
15548 cp_lexer_consume_token (parser
->lexer
);
15549 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15550 initializer
= make_node (CONSTRUCTOR
);
15551 /* If it's not a `}', then there is a non-trivial initializer. */
15552 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
15554 /* Parse the initializer list. */
15555 CONSTRUCTOR_ELTS (initializer
)
15556 = cp_parser_initializer_list (parser
, non_constant_p
);
15557 /* A trailing `,' token is allowed. */
15558 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
15559 cp_lexer_consume_token (parser
->lexer
);
15561 /* Now, there should be a trailing `}'. */
15562 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
15563 TREE_TYPE (initializer
) = init_list_type_node
;
15564 return initializer
;
15567 /* Parse an initializer-list.
15570 initializer-clause ... [opt]
15571 initializer-list , initializer-clause ... [opt]
15576 identifier : initializer-clause
15577 initializer-list, identifier : initializer-clause
15579 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15580 for the initializer. If the INDEX of the elt is non-NULL, it is the
15581 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15582 as for cp_parser_initializer. */
15584 static VEC(constructor_elt
,gc
) *
15585 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
15587 VEC(constructor_elt
,gc
) *v
= NULL
;
15589 /* Assume all of the expressions are constant. */
15590 *non_constant_p
= false;
15592 /* Parse the rest of the list. */
15598 bool clause_non_constant_p
;
15600 /* If the next token is an identifier and the following one is a
15601 colon, we are looking at the GNU designated-initializer
15603 if (cp_parser_allow_gnu_extensions_p (parser
)
15604 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
15605 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
15607 /* Warn the user that they are using an extension. */
15608 pedwarn (input_location
, OPT_pedantic
,
15609 "ISO C++ does not allow designated initializers");
15610 /* Consume the identifier. */
15611 identifier
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
15612 /* Consume the `:'. */
15613 cp_lexer_consume_token (parser
->lexer
);
15616 identifier
= NULL_TREE
;
15618 /* Parse the initializer. */
15619 initializer
= cp_parser_initializer_clause (parser
,
15620 &clause_non_constant_p
);
15621 /* If any clause is non-constant, so is the entire initializer. */
15622 if (clause_non_constant_p
)
15623 *non_constant_p
= true;
15625 /* If we have an ellipsis, this is an initializer pack
15627 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15629 /* Consume the `...'. */
15630 cp_lexer_consume_token (parser
->lexer
);
15632 /* Turn the initializer into an initializer expansion. */
15633 initializer
= make_pack_expansion (initializer
);
15636 /* Add it to the vector. */
15637 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
15639 /* If the next token is not a comma, we have reached the end of
15641 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15644 /* Peek at the next token. */
15645 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15646 /* If the next token is a `}', then we're still done. An
15647 initializer-clause can have a trailing `,' after the
15648 initializer-list and before the closing `}'. */
15649 if (token
->type
== CPP_CLOSE_BRACE
)
15652 /* Consume the `,' token. */
15653 cp_lexer_consume_token (parser
->lexer
);
15659 /* Classes [gram.class] */
15661 /* Parse a class-name.
15667 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15668 to indicate that names looked up in dependent types should be
15669 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15670 keyword has been used to indicate that the name that appears next
15671 is a template. TAG_TYPE indicates the explicit tag given before
15672 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15673 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15674 is the class being defined in a class-head.
15676 Returns the TYPE_DECL representing the class. */
15679 cp_parser_class_name (cp_parser
*parser
,
15680 bool typename_keyword_p
,
15681 bool template_keyword_p
,
15682 enum tag_types tag_type
,
15683 bool check_dependency_p
,
15685 bool is_declaration
)
15691 tree identifier
= NULL_TREE
;
15693 /* All class-names start with an identifier. */
15694 token
= cp_lexer_peek_token (parser
->lexer
);
15695 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
15697 cp_parser_error (parser
, "expected class-name");
15698 return error_mark_node
;
15701 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15702 to a template-id, so we save it here. */
15703 scope
= parser
->scope
;
15704 if (scope
== error_mark_node
)
15705 return error_mark_node
;
15707 /* Any name names a type if we're following the `typename' keyword
15708 in a qualified name where the enclosing scope is type-dependent. */
15709 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
15710 && dependent_type_p (scope
));
15711 /* Handle the common case (an identifier, but not a template-id)
15713 if (token
->type
== CPP_NAME
15714 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
15716 cp_token
*identifier_token
;
15719 /* Look for the identifier. */
15720 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
15721 ambiguous_p
= identifier_token
->ambiguous_p
;
15722 identifier
= cp_parser_identifier (parser
);
15723 /* If the next token isn't an identifier, we are certainly not
15724 looking at a class-name. */
15725 if (identifier
== error_mark_node
)
15726 decl
= error_mark_node
;
15727 /* If we know this is a type-name, there's no need to look it
15729 else if (typename_p
)
15733 tree ambiguous_decls
;
15734 /* If we already know that this lookup is ambiguous, then
15735 we've already issued an error message; there's no reason
15739 cp_parser_simulate_error (parser
);
15740 return error_mark_node
;
15742 /* If the next token is a `::', then the name must be a type
15745 [basic.lookup.qual]
15747 During the lookup for a name preceding the :: scope
15748 resolution operator, object, function, and enumerator
15749 names are ignored. */
15750 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
15751 tag_type
= typename_type
;
15752 /* Look up the name. */
15753 decl
= cp_parser_lookup_name (parser
, identifier
,
15755 /*is_template=*/false,
15756 /*is_namespace=*/false,
15757 check_dependency_p
,
15759 identifier_token
->location
);
15760 if (ambiguous_decls
)
15762 if (cp_parser_parsing_tentatively (parser
))
15763 cp_parser_simulate_error (parser
);
15764 return error_mark_node
;
15770 /* Try a template-id. */
15771 decl
= cp_parser_template_id (parser
, template_keyword_p
,
15772 check_dependency_p
,
15774 if (decl
== error_mark_node
)
15775 return error_mark_node
;
15778 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
15780 /* If this is a typename, create a TYPENAME_TYPE. */
15781 if (typename_p
&& decl
!= error_mark_node
)
15783 decl
= make_typename_type (scope
, decl
, typename_type
,
15784 /*complain=*/tf_error
);
15785 if (decl
!= error_mark_node
)
15786 decl
= TYPE_NAME (decl
);
15789 /* Check to see that it is really the name of a class. */
15790 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
15791 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
15792 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
15793 /* Situations like this:
15795 template <typename T> struct A {
15796 typename T::template X<int>::I i;
15799 are problematic. Is `T::template X<int>' a class-name? The
15800 standard does not seem to be definitive, but there is no other
15801 valid interpretation of the following `::'. Therefore, those
15802 names are considered class-names. */
15804 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
15805 if (decl
!= error_mark_node
)
15806 decl
= TYPE_NAME (decl
);
15808 else if (TREE_CODE (decl
) != TYPE_DECL
15809 || TREE_TYPE (decl
) == error_mark_node
15810 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
)))
15811 decl
= error_mark_node
;
15813 if (decl
== error_mark_node
)
15814 cp_parser_error (parser
, "expected class-name");
15815 else if (identifier
&& !parser
->scope
)
15816 maybe_note_name_used_in_class (identifier
, decl
);
15821 /* Parse a class-specifier.
15824 class-head { member-specification [opt] }
15826 Returns the TREE_TYPE representing the class. */
15829 cp_parser_class_specifier (cp_parser
* parser
)
15832 tree attributes
= NULL_TREE
;
15833 bool nested_name_specifier_p
;
15834 unsigned saved_num_template_parameter_lists
;
15835 bool saved_in_function_body
;
15836 bool saved_in_unbraced_linkage_specification_p
;
15837 tree old_scope
= NULL_TREE
;
15838 tree scope
= NULL_TREE
;
15841 push_deferring_access_checks (dk_no_deferred
);
15843 /* Parse the class-head. */
15844 type
= cp_parser_class_head (parser
,
15845 &nested_name_specifier_p
,
15848 /* If the class-head was a semantic disaster, skip the entire body
15852 cp_parser_skip_to_end_of_block_or_statement (parser
);
15853 pop_deferring_access_checks ();
15854 return error_mark_node
;
15857 /* Look for the `{'. */
15858 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "%<{%>"))
15860 pop_deferring_access_checks ();
15861 return error_mark_node
;
15864 /* Process the base classes. If they're invalid, skip the
15865 entire class body. */
15866 if (!xref_basetypes (type
, bases
))
15868 /* Consuming the closing brace yields better error messages
15870 if (cp_parser_skip_to_closing_brace (parser
))
15871 cp_lexer_consume_token (parser
->lexer
);
15872 pop_deferring_access_checks ();
15873 return error_mark_node
;
15876 /* Issue an error message if type-definitions are forbidden here. */
15877 cp_parser_check_type_definition (parser
);
15878 /* Remember that we are defining one more class. */
15879 ++parser
->num_classes_being_defined
;
15880 /* Inside the class, surrounding template-parameter-lists do not
15882 saved_num_template_parameter_lists
15883 = parser
->num_template_parameter_lists
;
15884 parser
->num_template_parameter_lists
= 0;
15885 /* We are not in a function body. */
15886 saved_in_function_body
= parser
->in_function_body
;
15887 parser
->in_function_body
= false;
15888 /* We are not immediately inside an extern "lang" block. */
15889 saved_in_unbraced_linkage_specification_p
15890 = parser
->in_unbraced_linkage_specification_p
;
15891 parser
->in_unbraced_linkage_specification_p
= false;
15893 /* Start the class. */
15894 if (nested_name_specifier_p
)
15896 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
15897 old_scope
= push_inner_scope (scope
);
15899 type
= begin_class_definition (type
, attributes
);
15901 if (type
== error_mark_node
)
15902 /* If the type is erroneous, skip the entire body of the class. */
15903 cp_parser_skip_to_closing_brace (parser
);
15905 /* Parse the member-specification. */
15906 cp_parser_member_specification_opt (parser
);
15908 /* Look for the trailing `}'. */
15909 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
15910 /* Look for trailing attributes to apply to this class. */
15911 if (cp_parser_allow_gnu_extensions_p (parser
))
15912 attributes
= cp_parser_attributes_opt (parser
);
15913 if (type
!= error_mark_node
)
15914 type
= finish_struct (type
, attributes
);
15915 if (nested_name_specifier_p
)
15916 pop_inner_scope (old_scope
, scope
);
15917 /* If this class is not itself within the scope of another class,
15918 then we need to parse the bodies of all of the queued function
15919 definitions. Note that the queued functions defined in a class
15920 are not always processed immediately following the
15921 class-specifier for that class. Consider:
15924 struct B { void f() { sizeof (A); } };
15927 If `f' were processed before the processing of `A' were
15928 completed, there would be no way to compute the size of `A'.
15929 Note that the nesting we are interested in here is lexical --
15930 not the semantic nesting given by TYPE_CONTEXT. In particular,
15933 struct A { struct B; };
15934 struct A::B { void f() { } };
15936 there is no need to delay the parsing of `A::B::f'. */
15937 if (--parser
->num_classes_being_defined
== 0)
15941 tree class_type
= NULL_TREE
;
15942 tree pushed_scope
= NULL_TREE
;
15944 /* In a first pass, parse default arguments to the functions.
15945 Then, in a second pass, parse the bodies of the functions.
15946 This two-phased approach handles cases like:
15954 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
15955 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
15956 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
15957 TREE_PURPOSE (parser
->unparsed_functions_queues
)
15958 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
15960 fn
= TREE_VALUE (queue_entry
);
15961 /* If there are default arguments that have not yet been processed,
15962 take care of them now. */
15963 if (class_type
!= TREE_PURPOSE (queue_entry
))
15966 pop_scope (pushed_scope
);
15967 class_type
= TREE_PURPOSE (queue_entry
);
15968 pushed_scope
= push_scope (class_type
);
15970 /* Make sure that any template parameters are in scope. */
15971 maybe_begin_member_template_processing (fn
);
15972 /* Parse the default argument expressions. */
15973 cp_parser_late_parsing_default_args (parser
, fn
);
15974 /* Remove any template parameters from the symbol table. */
15975 maybe_end_member_template_processing ();
15978 pop_scope (pushed_scope
);
15979 /* Now parse the body of the functions. */
15980 for (TREE_VALUE (parser
->unparsed_functions_queues
)
15981 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
15982 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
15983 TREE_VALUE (parser
->unparsed_functions_queues
)
15984 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
15986 /* Figure out which function we need to process. */
15987 fn
= TREE_VALUE (queue_entry
);
15988 /* Parse the function. */
15989 cp_parser_late_parsing_for_member (parser
, fn
);
15993 /* Put back any saved access checks. */
15994 pop_deferring_access_checks ();
15996 /* Restore saved state. */
15997 parser
->in_function_body
= saved_in_function_body
;
15998 parser
->num_template_parameter_lists
15999 = saved_num_template_parameter_lists
;
16000 parser
->in_unbraced_linkage_specification_p
16001 = saved_in_unbraced_linkage_specification_p
;
16006 /* Parse a class-head.
16009 class-key identifier [opt] base-clause [opt]
16010 class-key nested-name-specifier identifier base-clause [opt]
16011 class-key nested-name-specifier [opt] template-id
16015 class-key attributes identifier [opt] base-clause [opt]
16016 class-key attributes nested-name-specifier identifier base-clause [opt]
16017 class-key attributes nested-name-specifier [opt] template-id
16020 Upon return BASES is initialized to the list of base classes (or
16021 NULL, if there are none) in the same form returned by
16022 cp_parser_base_clause.
16024 Returns the TYPE of the indicated class. Sets
16025 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16026 involving a nested-name-specifier was used, and FALSE otherwise.
16028 Returns error_mark_node if this is not a class-head.
16030 Returns NULL_TREE if the class-head is syntactically valid, but
16031 semantically invalid in a way that means we should skip the entire
16032 body of the class. */
16035 cp_parser_class_head (cp_parser
* parser
,
16036 bool* nested_name_specifier_p
,
16037 tree
*attributes_p
,
16040 tree nested_name_specifier
;
16041 enum tag_types class_key
;
16042 tree id
= NULL_TREE
;
16043 tree type
= NULL_TREE
;
16045 bool template_id_p
= false;
16046 bool qualified_p
= false;
16047 bool invalid_nested_name_p
= false;
16048 bool invalid_explicit_specialization_p
= false;
16049 tree pushed_scope
= NULL_TREE
;
16050 unsigned num_templates
;
16051 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
16052 /* Assume no nested-name-specifier will be present. */
16053 *nested_name_specifier_p
= false;
16054 /* Assume no template parameter lists will be used in defining the
16058 *bases
= NULL_TREE
;
16060 /* Look for the class-key. */
16061 class_key
= cp_parser_class_key (parser
);
16062 if (class_key
== none_type
)
16063 return error_mark_node
;
16065 /* Parse the attributes. */
16066 attributes
= cp_parser_attributes_opt (parser
);
16068 /* If the next token is `::', that is invalid -- but sometimes
16069 people do try to write:
16073 Handle this gracefully by accepting the extra qualifier, and then
16074 issuing an error about it later if this really is a
16075 class-head. If it turns out just to be an elaborated type
16076 specifier, remain silent. */
16077 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
16078 qualified_p
= true;
16080 push_deferring_access_checks (dk_no_check
);
16082 /* Determine the name of the class. Begin by looking for an
16083 optional nested-name-specifier. */
16084 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
16085 nested_name_specifier
16086 = cp_parser_nested_name_specifier_opt (parser
,
16087 /*typename_keyword_p=*/false,
16088 /*check_dependency_p=*/false,
16090 /*is_declaration=*/false);
16091 /* If there was a nested-name-specifier, then there *must* be an
16093 if (nested_name_specifier
)
16095 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
16096 /* Although the grammar says `identifier', it really means
16097 `class-name' or `template-name'. You are only allowed to
16098 define a class that has already been declared with this
16101 The proposed resolution for Core Issue 180 says that wherever
16102 you see `class T::X' you should treat `X' as a type-name.
16104 It is OK to define an inaccessible class; for example:
16106 class A { class B; };
16109 We do not know if we will see a class-name, or a
16110 template-name. We look for a class-name first, in case the
16111 class-name is a template-id; if we looked for the
16112 template-name first we would stop after the template-name. */
16113 cp_parser_parse_tentatively (parser
);
16114 type
= cp_parser_class_name (parser
,
16115 /*typename_keyword_p=*/false,
16116 /*template_keyword_p=*/false,
16118 /*check_dependency_p=*/false,
16119 /*class_head_p=*/true,
16120 /*is_declaration=*/false);
16121 /* If that didn't work, ignore the nested-name-specifier. */
16122 if (!cp_parser_parse_definitely (parser
))
16124 invalid_nested_name_p
= true;
16125 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
16126 id
= cp_parser_identifier (parser
);
16127 if (id
== error_mark_node
)
16130 /* If we could not find a corresponding TYPE, treat this
16131 declaration like an unqualified declaration. */
16132 if (type
== error_mark_node
)
16133 nested_name_specifier
= NULL_TREE
;
16134 /* Otherwise, count the number of templates used in TYPE and its
16135 containing scopes. */
16140 for (scope
= TREE_TYPE (type
);
16141 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
16142 scope
= (TYPE_P (scope
)
16143 ? TYPE_CONTEXT (scope
)
16144 : DECL_CONTEXT (scope
)))
16146 && CLASS_TYPE_P (scope
)
16147 && CLASSTYPE_TEMPLATE_INFO (scope
)
16148 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
16149 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
16153 /* Otherwise, the identifier is optional. */
16156 /* We don't know whether what comes next is a template-id,
16157 an identifier, or nothing at all. */
16158 cp_parser_parse_tentatively (parser
);
16159 /* Check for a template-id. */
16160 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
16161 id
= cp_parser_template_id (parser
,
16162 /*template_keyword_p=*/false,
16163 /*check_dependency_p=*/true,
16164 /*is_declaration=*/true);
16165 /* If that didn't work, it could still be an identifier. */
16166 if (!cp_parser_parse_definitely (parser
))
16168 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16170 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
16171 id
= cp_parser_identifier (parser
);
16178 template_id_p
= true;
16183 pop_deferring_access_checks ();
16186 cp_parser_check_for_invalid_template_id (parser
, id
,
16187 type_start_token
->location
);
16189 /* If it's not a `:' or a `{' then we can't really be looking at a
16190 class-head, since a class-head only appears as part of a
16191 class-specifier. We have to detect this situation before calling
16192 xref_tag, since that has irreversible side-effects. */
16193 if (!cp_parser_next_token_starts_class_definition_p (parser
))
16195 cp_parser_error (parser
, "expected %<{%> or %<:%>");
16196 return error_mark_node
;
16199 /* At this point, we're going ahead with the class-specifier, even
16200 if some other problem occurs. */
16201 cp_parser_commit_to_tentative_parse (parser
);
16202 /* Issue the error about the overly-qualified name now. */
16205 cp_parser_error (parser
,
16206 "global qualification of class name is invalid");
16207 return error_mark_node
;
16209 else if (invalid_nested_name_p
)
16211 cp_parser_error (parser
,
16212 "qualified name does not name a class");
16213 return error_mark_node
;
16215 else if (nested_name_specifier
)
16219 /* Reject typedef-names in class heads. */
16220 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
16222 error_at (type_start_token
->location
,
16223 "invalid class name in declaration of %qD",
16229 /* Figure out in what scope the declaration is being placed. */
16230 scope
= current_scope ();
16231 /* If that scope does not contain the scope in which the
16232 class was originally declared, the program is invalid. */
16233 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
16235 if (at_namespace_scope_p ())
16236 error_at (type_start_token
->location
,
16237 "declaration of %qD in namespace %qD which does not "
16239 type
, scope
, nested_name_specifier
);
16241 error_at (type_start_token
->location
,
16242 "declaration of %qD in %qD which does not enclose %qD",
16243 type
, scope
, nested_name_specifier
);
16249 A declarator-id shall not be qualified except for the
16250 definition of a ... nested class outside of its class
16251 ... [or] the definition or explicit instantiation of a
16252 class member of a namespace outside of its namespace. */
16253 if (scope
== nested_name_specifier
)
16255 permerror (nested_name_specifier_token_start
->location
,
16256 "extra qualification not allowed");
16257 nested_name_specifier
= NULL_TREE
;
16261 /* An explicit-specialization must be preceded by "template <>". If
16262 it is not, try to recover gracefully. */
16263 if (at_namespace_scope_p ()
16264 && parser
->num_template_parameter_lists
== 0
16267 error_at (type_start_token
->location
,
16268 "an explicit specialization must be preceded by %<template <>%>");
16269 invalid_explicit_specialization_p
= true;
16270 /* Take the same action that would have been taken by
16271 cp_parser_explicit_specialization. */
16272 ++parser
->num_template_parameter_lists
;
16273 begin_specialization ();
16275 /* There must be no "return" statements between this point and the
16276 end of this function; set "type "to the correct return value and
16277 use "goto done;" to return. */
16278 /* Make sure that the right number of template parameters were
16280 if (!cp_parser_check_template_parameters (parser
, num_templates
,
16281 type_start_token
->location
,
16282 /*declarator=*/NULL
))
16284 /* If something went wrong, there is no point in even trying to
16285 process the class-definition. */
16290 /* Look up the type. */
16293 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
16294 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
16295 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
16297 error_at (type_start_token
->location
,
16298 "function template %qD redeclared as a class template", id
);
16299 type
= error_mark_node
;
16303 type
= TREE_TYPE (id
);
16304 type
= maybe_process_partial_specialization (type
);
16306 if (nested_name_specifier
)
16307 pushed_scope
= push_scope (nested_name_specifier
);
16309 else if (nested_name_specifier
)
16315 template <typename T> struct S { struct T };
16316 template <typename T> struct S<T>::T { };
16318 we will get a TYPENAME_TYPE when processing the definition of
16319 `S::T'. We need to resolve it to the actual type before we
16320 try to define it. */
16321 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
16323 class_type
= resolve_typename_type (TREE_TYPE (type
),
16324 /*only_current_p=*/false);
16325 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
16326 type
= TYPE_NAME (class_type
);
16329 cp_parser_error (parser
, "could not resolve typename type");
16330 type
= error_mark_node
;
16334 if (maybe_process_partial_specialization (TREE_TYPE (type
))
16335 == error_mark_node
)
16341 class_type
= current_class_type
;
16342 /* Enter the scope indicated by the nested-name-specifier. */
16343 pushed_scope
= push_scope (nested_name_specifier
);
16344 /* Get the canonical version of this type. */
16345 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
16346 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16347 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
16349 type
= push_template_decl (type
);
16350 if (type
== error_mark_node
)
16357 type
= TREE_TYPE (type
);
16358 *nested_name_specifier_p
= true;
16360 else /* The name is not a nested name. */
16362 /* If the class was unnamed, create a dummy name. */
16364 id
= make_anon_name ();
16365 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
16366 parser
->num_template_parameter_lists
);
16369 /* Indicate whether this class was declared as a `class' or as a
16371 if (TREE_CODE (type
) == RECORD_TYPE
)
16372 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
16373 cp_parser_check_class_key (class_key
, type
);
16375 /* If this type was already complete, and we see another definition,
16376 that's an error. */
16377 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
16379 error_at (type_start_token
->location
, "redefinition of %q#T",
16381 error_at (type_start_token
->location
, "previous definition of %q+#T",
16386 else if (type
== error_mark_node
)
16389 /* We will have entered the scope containing the class; the names of
16390 base classes should be looked up in that context. For example:
16392 struct A { struct B {}; struct C; };
16393 struct A::C : B {};
16397 /* Get the list of base-classes, if there is one. */
16398 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16399 *bases
= cp_parser_base_clause (parser
);
16402 /* Leave the scope given by the nested-name-specifier. We will
16403 enter the class scope itself while processing the members. */
16405 pop_scope (pushed_scope
);
16407 if (invalid_explicit_specialization_p
)
16409 end_specialization ();
16410 --parser
->num_template_parameter_lists
;
16414 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
16415 *attributes_p
= attributes
;
16419 /* Parse a class-key.
16426 Returns the kind of class-key specified, or none_type to indicate
16429 static enum tag_types
16430 cp_parser_class_key (cp_parser
* parser
)
16433 enum tag_types tag_type
;
16435 /* Look for the class-key. */
16436 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
16440 /* Check to see if the TOKEN is a class-key. */
16441 tag_type
= cp_parser_token_is_class_key (token
);
16443 cp_parser_error (parser
, "expected class-key");
16447 /* Parse an (optional) member-specification.
16449 member-specification:
16450 member-declaration member-specification [opt]
16451 access-specifier : member-specification [opt] */
16454 cp_parser_member_specification_opt (cp_parser
* parser
)
16461 /* Peek at the next token. */
16462 token
= cp_lexer_peek_token (parser
->lexer
);
16463 /* If it's a `}', or EOF then we've seen all the members. */
16464 if (token
->type
== CPP_CLOSE_BRACE
16465 || token
->type
== CPP_EOF
16466 || token
->type
== CPP_PRAGMA_EOL
)
16469 /* See if this token is a keyword. */
16470 keyword
= token
->keyword
;
16474 case RID_PROTECTED
:
16476 /* Consume the access-specifier. */
16477 cp_lexer_consume_token (parser
->lexer
);
16478 /* Remember which access-specifier is active. */
16479 current_access_specifier
= token
->u
.value
;
16480 /* Look for the `:'. */
16481 cp_parser_require (parser
, CPP_COLON
, "%<:%>");
16485 /* Accept #pragmas at class scope. */
16486 if (token
->type
== CPP_PRAGMA
)
16488 cp_parser_pragma (parser
, pragma_external
);
16492 /* Otherwise, the next construction must be a
16493 member-declaration. */
16494 cp_parser_member_declaration (parser
);
16499 /* Parse a member-declaration.
16501 member-declaration:
16502 decl-specifier-seq [opt] member-declarator-list [opt] ;
16503 function-definition ; [opt]
16504 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16506 template-declaration
16508 member-declarator-list:
16510 member-declarator-list , member-declarator
16513 declarator pure-specifier [opt]
16514 declarator constant-initializer [opt]
16515 identifier [opt] : constant-expression
16519 member-declaration:
16520 __extension__ member-declaration
16523 declarator attributes [opt] pure-specifier [opt]
16524 declarator attributes [opt] constant-initializer [opt]
16525 identifier [opt] attributes [opt] : constant-expression
16529 member-declaration:
16530 static_assert-declaration */
16533 cp_parser_member_declaration (cp_parser
* parser
)
16535 cp_decl_specifier_seq decl_specifiers
;
16536 tree prefix_attributes
;
16538 int declares_class_or_enum
;
16540 cp_token
*token
= NULL
;
16541 cp_token
*decl_spec_token_start
= NULL
;
16542 cp_token
*initializer_token_start
= NULL
;
16543 int saved_pedantic
;
16545 /* Check for the `__extension__' keyword. */
16546 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
16549 cp_parser_member_declaration (parser
);
16550 /* Restore the old value of the PEDANTIC flag. */
16551 pedantic
= saved_pedantic
;
16556 /* Check for a template-declaration. */
16557 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16559 /* An explicit specialization here is an error condition, and we
16560 expect the specialization handler to detect and report this. */
16561 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16562 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
16563 cp_parser_explicit_specialization (parser
);
16565 cp_parser_template_declaration (parser
, /*member_p=*/true);
16570 /* Check for a using-declaration. */
16571 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
16573 /* Parse the using-declaration. */
16574 cp_parser_using_declaration (parser
,
16575 /*access_declaration_p=*/false);
16579 /* Check for @defs. */
16580 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
16583 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
16584 ivar
= ivar_chains
;
16588 ivar
= TREE_CHAIN (member
);
16589 TREE_CHAIN (member
) = NULL_TREE
;
16590 finish_member_declaration (member
);
16595 /* If the next token is `static_assert' we have a static assertion. */
16596 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
16598 cp_parser_static_assert (parser
, /*member_p=*/true);
16602 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
16605 /* Parse the decl-specifier-seq. */
16606 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
16607 cp_parser_decl_specifier_seq (parser
,
16608 CP_PARSER_FLAGS_OPTIONAL
,
16610 &declares_class_or_enum
);
16611 prefix_attributes
= decl_specifiers
.attributes
;
16612 decl_specifiers
.attributes
= NULL_TREE
;
16613 /* Check for an invalid type-name. */
16614 if (!decl_specifiers
.any_type_specifiers_p
16615 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
16617 /* If there is no declarator, then the decl-specifier-seq should
16619 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
16621 /* If there was no decl-specifier-seq, and the next token is a
16622 `;', then we have something like:
16628 Each member-declaration shall declare at least one member
16629 name of the class. */
16630 if (!decl_specifiers
.any_specifiers_p
)
16632 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16633 if (!in_system_header_at (token
->location
))
16634 pedwarn (token
->location
, OPT_pedantic
, "extra %<;%>");
16640 /* See if this declaration is a friend. */
16641 friend_p
= cp_parser_friend_p (&decl_specifiers
);
16642 /* If there were decl-specifiers, check to see if there was
16643 a class-declaration. */
16644 type
= check_tag_decl (&decl_specifiers
);
16645 /* Nested classes have already been added to the class, but
16646 a `friend' needs to be explicitly registered. */
16649 /* If the `friend' keyword was present, the friend must
16650 be introduced with a class-key. */
16651 if (!declares_class_or_enum
)
16652 error_at (decl_spec_token_start
->location
,
16653 "a class-key must be used when declaring a friend");
16656 template <typename T> struct A {
16657 friend struct A<T>::B;
16660 A<T>::B will be represented by a TYPENAME_TYPE, and
16661 therefore not recognized by check_tag_decl. */
16663 && decl_specifiers
.type
16664 && TYPE_P (decl_specifiers
.type
))
16665 type
= decl_specifiers
.type
;
16666 if (!type
|| !TYPE_P (type
))
16667 error_at (decl_spec_token_start
->location
,
16668 "friend declaration does not name a class or "
16671 make_friend_class (current_class_type
, type
,
16672 /*complain=*/true);
16674 /* If there is no TYPE, an error message will already have
16676 else if (!type
|| type
== error_mark_node
)
16678 /* An anonymous aggregate has to be handled specially; such
16679 a declaration really declares a data member (with a
16680 particular type), as opposed to a nested class. */
16681 else if (ANON_AGGR_TYPE_P (type
))
16683 /* Remove constructors and such from TYPE, now that we
16684 know it is an anonymous aggregate. */
16685 fixup_anonymous_aggr (type
);
16686 /* And make the corresponding data member. */
16687 decl
= build_decl (decl_spec_token_start
->location
,
16688 FIELD_DECL
, NULL_TREE
, type
);
16689 /* Add it to the class. */
16690 finish_member_declaration (decl
);
16693 cp_parser_check_access_in_redeclaration
16695 decl_spec_token_start
->location
);
16700 /* See if these declarations will be friends. */
16701 friend_p
= cp_parser_friend_p (&decl_specifiers
);
16703 /* Keep going until we hit the `;' at the end of the
16705 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
16707 tree attributes
= NULL_TREE
;
16708 tree first_attribute
;
16710 /* Peek at the next token. */
16711 token
= cp_lexer_peek_token (parser
->lexer
);
16713 /* Check for a bitfield declaration. */
16714 if (token
->type
== CPP_COLON
16715 || (token
->type
== CPP_NAME
16716 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
16722 /* Get the name of the bitfield. Note that we cannot just
16723 check TOKEN here because it may have been invalidated by
16724 the call to cp_lexer_peek_nth_token above. */
16725 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
16726 identifier
= cp_parser_identifier (parser
);
16728 identifier
= NULL_TREE
;
16730 /* Consume the `:' token. */
16731 cp_lexer_consume_token (parser
->lexer
);
16732 /* Get the width of the bitfield. */
16734 = cp_parser_constant_expression (parser
,
16735 /*allow_non_constant=*/false,
16738 /* Look for attributes that apply to the bitfield. */
16739 attributes
= cp_parser_attributes_opt (parser
);
16740 /* Remember which attributes are prefix attributes and
16742 first_attribute
= attributes
;
16743 /* Combine the attributes. */
16744 attributes
= chainon (prefix_attributes
, attributes
);
16746 /* Create the bitfield declaration. */
16747 decl
= grokbitfield (identifier
16748 ? make_id_declarator (NULL_TREE
,
16758 cp_declarator
*declarator
;
16760 tree asm_specification
;
16761 int ctor_dtor_or_conv_p
;
16763 /* Parse the declarator. */
16765 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16766 &ctor_dtor_or_conv_p
,
16767 /*parenthesized_p=*/NULL
,
16768 /*member_p=*/true);
16770 /* If something went wrong parsing the declarator, make sure
16771 that we at least consume some tokens. */
16772 if (declarator
== cp_error_declarator
)
16774 /* Skip to the end of the statement. */
16775 cp_parser_skip_to_end_of_statement (parser
);
16776 /* If the next token is not a semicolon, that is
16777 probably because we just skipped over the body of
16778 a function. So, we consume a semicolon if
16779 present, but do not issue an error message if it
16781 if (cp_lexer_next_token_is (parser
->lexer
,
16783 cp_lexer_consume_token (parser
->lexer
);
16787 if (declares_class_or_enum
& 2)
16788 cp_parser_check_for_definition_in_return_type
16789 (declarator
, decl_specifiers
.type
,
16790 decl_specifiers
.type_location
);
16792 /* Look for an asm-specification. */
16793 asm_specification
= cp_parser_asm_specification_opt (parser
);
16794 /* Look for attributes that apply to the declaration. */
16795 attributes
= cp_parser_attributes_opt (parser
);
16796 /* Remember which attributes are prefix attributes and
16798 first_attribute
= attributes
;
16799 /* Combine the attributes. */
16800 attributes
= chainon (prefix_attributes
, attributes
);
16802 /* If it's an `=', then we have a constant-initializer or a
16803 pure-specifier. It is not correct to parse the
16804 initializer before registering the member declaration
16805 since the member declaration should be in scope while
16806 its initializer is processed. However, the rest of the
16807 front end does not yet provide an interface that allows
16808 us to handle this correctly. */
16809 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16813 A pure-specifier shall be used only in the declaration of
16814 a virtual function.
16816 A member-declarator can contain a constant-initializer
16817 only if it declares a static member of integral or
16820 Therefore, if the DECLARATOR is for a function, we look
16821 for a pure-specifier; otherwise, we look for a
16822 constant-initializer. When we call `grokfield', it will
16823 perform more stringent semantics checks. */
16824 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
16825 if (function_declarator_p (declarator
))
16826 initializer
= cp_parser_pure_specifier (parser
);
16828 /* Parse the initializer. */
16829 initializer
= cp_parser_constant_initializer (parser
);
16831 /* Otherwise, there is no initializer. */
16833 initializer
= NULL_TREE
;
16835 /* See if we are probably looking at a function
16836 definition. We are certainly not looking at a
16837 member-declarator. Calling `grokfield' has
16838 side-effects, so we must not do it unless we are sure
16839 that we are looking at a member-declarator. */
16840 if (cp_parser_token_starts_function_definition_p
16841 (cp_lexer_peek_token (parser
->lexer
)))
16843 /* The grammar does not allow a pure-specifier to be
16844 used when a member function is defined. (It is
16845 possible that this fact is an oversight in the
16846 standard, since a pure function may be defined
16847 outside of the class-specifier. */
16849 error_at (initializer_token_start
->location
,
16850 "pure-specifier on function-definition");
16851 decl
= cp_parser_save_member_function_body (parser
,
16855 /* If the member was not a friend, declare it here. */
16857 finish_member_declaration (decl
);
16858 /* Peek at the next token. */
16859 token
= cp_lexer_peek_token (parser
->lexer
);
16860 /* If the next token is a semicolon, consume it. */
16861 if (token
->type
== CPP_SEMICOLON
)
16862 cp_lexer_consume_token (parser
->lexer
);
16866 if (declarator
->kind
== cdk_function
)
16867 declarator
->id_loc
= token
->location
;
16868 /* Create the declaration. */
16869 decl
= grokfield (declarator
, &decl_specifiers
,
16870 initializer
, /*init_const_expr_p=*/true,
16875 /* Reset PREFIX_ATTRIBUTES. */
16876 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
16877 attributes
= TREE_CHAIN (attributes
);
16879 TREE_CHAIN (attributes
) = NULL_TREE
;
16881 /* If there is any qualification still in effect, clear it
16882 now; we will be starting fresh with the next declarator. */
16883 parser
->scope
= NULL_TREE
;
16884 parser
->qualifying_scope
= NULL_TREE
;
16885 parser
->object_scope
= NULL_TREE
;
16886 /* If it's a `,', then there are more declarators. */
16887 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
16888 cp_lexer_consume_token (parser
->lexer
);
16889 /* If the next token isn't a `;', then we have a parse error. */
16890 else if (cp_lexer_next_token_is_not (parser
->lexer
,
16893 cp_parser_error (parser
, "expected %<;%>");
16894 /* Skip tokens until we find a `;'. */
16895 cp_parser_skip_to_end_of_statement (parser
);
16902 /* Add DECL to the list of members. */
16904 finish_member_declaration (decl
);
16906 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16907 cp_parser_save_default_args (parser
, decl
);
16912 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
16915 /* Parse a pure-specifier.
16920 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16921 Otherwise, ERROR_MARK_NODE is returned. */
16924 cp_parser_pure_specifier (cp_parser
* parser
)
16928 /* Look for the `=' token. */
16929 if (!cp_parser_require (parser
, CPP_EQ
, "%<=%>"))
16930 return error_mark_node
;
16931 /* Look for the `0' token. */
16932 token
= cp_lexer_peek_token (parser
->lexer
);
16934 if (token
->type
== CPP_EOF
16935 || token
->type
== CPP_PRAGMA_EOL
)
16936 return error_mark_node
;
16938 cp_lexer_consume_token (parser
->lexer
);
16940 /* Accept = default or = delete in c++0x mode. */
16941 if (token
->keyword
== RID_DEFAULT
16942 || token
->keyword
== RID_DELETE
)
16944 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
16945 return token
->u
.value
;
16948 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16949 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
16951 cp_parser_error (parser
,
16952 "invalid pure specifier (only %<= 0%> is allowed)");
16953 cp_parser_skip_to_end_of_statement (parser
);
16954 return error_mark_node
;
16956 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16958 error_at (token
->location
, "templates may not be %<virtual%>");
16959 return error_mark_node
;
16962 return integer_zero_node
;
16965 /* Parse a constant-initializer.
16967 constant-initializer:
16968 = constant-expression
16970 Returns a representation of the constant-expression. */
16973 cp_parser_constant_initializer (cp_parser
* parser
)
16975 /* Look for the `=' token. */
16976 if (!cp_parser_require (parser
, CPP_EQ
, "%<=%>"))
16977 return error_mark_node
;
16979 /* It is invalid to write:
16981 struct S { static const int i = { 7 }; };
16984 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
16986 cp_parser_error (parser
,
16987 "a brace-enclosed initializer is not allowed here");
16988 /* Consume the opening brace. */
16989 cp_lexer_consume_token (parser
->lexer
);
16990 /* Skip the initializer. */
16991 cp_parser_skip_to_closing_brace (parser
);
16992 /* Look for the trailing `}'. */
16993 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
16995 return error_mark_node
;
16998 return cp_parser_constant_expression (parser
,
16999 /*allow_non_constant=*/false,
17003 /* Derived classes [gram.class.derived] */
17005 /* Parse a base-clause.
17008 : base-specifier-list
17010 base-specifier-list:
17011 base-specifier ... [opt]
17012 base-specifier-list , base-specifier ... [opt]
17014 Returns a TREE_LIST representing the base-classes, in the order in
17015 which they were declared. The representation of each node is as
17016 described by cp_parser_base_specifier.
17018 In the case that no bases are specified, this function will return
17019 NULL_TREE, not ERROR_MARK_NODE. */
17022 cp_parser_base_clause (cp_parser
* parser
)
17024 tree bases
= NULL_TREE
;
17026 /* Look for the `:' that begins the list. */
17027 cp_parser_require (parser
, CPP_COLON
, "%<:%>");
17029 /* Scan the base-specifier-list. */
17034 bool pack_expansion_p
= false;
17036 /* Look for the base-specifier. */
17037 base
= cp_parser_base_specifier (parser
);
17038 /* Look for the (optional) ellipsis. */
17039 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17041 /* Consume the `...'. */
17042 cp_lexer_consume_token (parser
->lexer
);
17044 pack_expansion_p
= true;
17047 /* Add BASE to the front of the list. */
17048 if (base
!= error_mark_node
)
17050 if (pack_expansion_p
)
17051 /* Make this a pack expansion type. */
17052 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
17055 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
17057 TREE_CHAIN (base
) = bases
;
17061 /* Peek at the next token. */
17062 token
= cp_lexer_peek_token (parser
->lexer
);
17063 /* If it's not a comma, then the list is complete. */
17064 if (token
->type
!= CPP_COMMA
)
17066 /* Consume the `,'. */
17067 cp_lexer_consume_token (parser
->lexer
);
17070 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17071 base class had a qualified name. However, the next name that
17072 appears is certainly not qualified. */
17073 parser
->scope
= NULL_TREE
;
17074 parser
->qualifying_scope
= NULL_TREE
;
17075 parser
->object_scope
= NULL_TREE
;
17077 return nreverse (bases
);
17080 /* Parse a base-specifier.
17083 :: [opt] nested-name-specifier [opt] class-name
17084 virtual access-specifier [opt] :: [opt] nested-name-specifier
17086 access-specifier virtual [opt] :: [opt] nested-name-specifier
17089 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17090 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17091 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17092 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17095 cp_parser_base_specifier (cp_parser
* parser
)
17099 bool virtual_p
= false;
17100 bool duplicate_virtual_error_issued_p
= false;
17101 bool duplicate_access_error_issued_p
= false;
17102 bool class_scope_p
, template_p
;
17103 tree access
= access_default_node
;
17106 /* Process the optional `virtual' and `access-specifier'. */
17109 /* Peek at the next token. */
17110 token
= cp_lexer_peek_token (parser
->lexer
);
17111 /* Process `virtual'. */
17112 switch (token
->keyword
)
17115 /* If `virtual' appears more than once, issue an error. */
17116 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
17118 cp_parser_error (parser
,
17119 "%<virtual%> specified more than once in base-specified");
17120 duplicate_virtual_error_issued_p
= true;
17125 /* Consume the `virtual' token. */
17126 cp_lexer_consume_token (parser
->lexer
);
17131 case RID_PROTECTED
:
17133 /* If more than one access specifier appears, issue an
17135 if (access
!= access_default_node
17136 && !duplicate_access_error_issued_p
)
17138 cp_parser_error (parser
,
17139 "more than one access specifier in base-specified");
17140 duplicate_access_error_issued_p
= true;
17143 access
= ridpointers
[(int) token
->keyword
];
17145 /* Consume the access-specifier. */
17146 cp_lexer_consume_token (parser
->lexer
);
17155 /* It is not uncommon to see programs mechanically, erroneously, use
17156 the 'typename' keyword to denote (dependent) qualified types
17157 as base classes. */
17158 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
17160 token
= cp_lexer_peek_token (parser
->lexer
);
17161 if (!processing_template_decl
)
17162 error_at (token
->location
,
17163 "keyword %<typename%> not allowed outside of templates");
17165 error_at (token
->location
,
17166 "keyword %<typename%> not allowed in this context "
17167 "(the base class is implicitly a type)");
17168 cp_lexer_consume_token (parser
->lexer
);
17171 /* Look for the optional `::' operator. */
17172 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
17173 /* Look for the nested-name-specifier. The simplest way to
17178 The keyword `typename' is not permitted in a base-specifier or
17179 mem-initializer; in these contexts a qualified name that
17180 depends on a template-parameter is implicitly assumed to be a
17183 is to pretend that we have seen the `typename' keyword at this
17185 cp_parser_nested_name_specifier_opt (parser
,
17186 /*typename_keyword_p=*/true,
17187 /*check_dependency_p=*/true,
17189 /*is_declaration=*/true);
17190 /* If the base class is given by a qualified name, assume that names
17191 we see are type names or templates, as appropriate. */
17192 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
17193 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
17195 /* Finally, look for the class-name. */
17196 type
= cp_parser_class_name (parser
,
17200 /*check_dependency_p=*/true,
17201 /*class_head_p=*/false,
17202 /*is_declaration=*/true);
17204 if (type
== error_mark_node
)
17205 return error_mark_node
;
17207 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
17210 /* Exception handling [gram.exception] */
17212 /* Parse an (optional) exception-specification.
17214 exception-specification:
17215 throw ( type-id-list [opt] )
17217 Returns a TREE_LIST representing the exception-specification. The
17218 TREE_VALUE of each node is a type. */
17221 cp_parser_exception_specification_opt (cp_parser
* parser
)
17226 /* Peek at the next token. */
17227 token
= cp_lexer_peek_token (parser
->lexer
);
17228 /* If it's not `throw', then there's no exception-specification. */
17229 if (!cp_parser_is_keyword (token
, RID_THROW
))
17232 /* Consume the `throw'. */
17233 cp_lexer_consume_token (parser
->lexer
);
17235 /* Look for the `('. */
17236 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
17238 /* Peek at the next token. */
17239 token
= cp_lexer_peek_token (parser
->lexer
);
17240 /* If it's not a `)', then there is a type-id-list. */
17241 if (token
->type
!= CPP_CLOSE_PAREN
)
17243 const char *saved_message
;
17245 /* Types may not be defined in an exception-specification. */
17246 saved_message
= parser
->type_definition_forbidden_message
;
17247 parser
->type_definition_forbidden_message
17248 = G_("types may not be defined in an exception-specification");
17249 /* Parse the type-id-list. */
17250 type_id_list
= cp_parser_type_id_list (parser
);
17251 /* Restore the saved message. */
17252 parser
->type_definition_forbidden_message
= saved_message
;
17255 type_id_list
= empty_except_spec
;
17257 /* Look for the `)'. */
17258 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
17260 return type_id_list
;
17263 /* Parse an (optional) type-id-list.
17267 type-id-list , type-id ... [opt]
17269 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17270 in the order that the types were presented. */
17273 cp_parser_type_id_list (cp_parser
* parser
)
17275 tree types
= NULL_TREE
;
17282 /* Get the next type-id. */
17283 type
= cp_parser_type_id (parser
);
17284 /* Parse the optional ellipsis. */
17285 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17287 /* Consume the `...'. */
17288 cp_lexer_consume_token (parser
->lexer
);
17290 /* Turn the type into a pack expansion expression. */
17291 type
= make_pack_expansion (type
);
17293 /* Add it to the list. */
17294 types
= add_exception_specifier (types
, type
, /*complain=*/1);
17295 /* Peek at the next token. */
17296 token
= cp_lexer_peek_token (parser
->lexer
);
17297 /* If it is not a `,', we are done. */
17298 if (token
->type
!= CPP_COMMA
)
17300 /* Consume the `,'. */
17301 cp_lexer_consume_token (parser
->lexer
);
17304 return nreverse (types
);
17307 /* Parse a try-block.
17310 try compound-statement handler-seq */
17313 cp_parser_try_block (cp_parser
* parser
)
17317 cp_parser_require_keyword (parser
, RID_TRY
, "%<try%>");
17318 try_block
= begin_try_block ();
17319 cp_parser_compound_statement (parser
, NULL
, true);
17320 finish_try_block (try_block
);
17321 cp_parser_handler_seq (parser
);
17322 finish_handler_sequence (try_block
);
17327 /* Parse a function-try-block.
17329 function-try-block:
17330 try ctor-initializer [opt] function-body handler-seq */
17333 cp_parser_function_try_block (cp_parser
* parser
)
17335 tree compound_stmt
;
17337 bool ctor_initializer_p
;
17339 /* Look for the `try' keyword. */
17340 if (!cp_parser_require_keyword (parser
, RID_TRY
, "%<try%>"))
17342 /* Let the rest of the front end know where we are. */
17343 try_block
= begin_function_try_block (&compound_stmt
);
17344 /* Parse the function-body. */
17346 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
17347 /* We're done with the `try' part. */
17348 finish_function_try_block (try_block
);
17349 /* Parse the handlers. */
17350 cp_parser_handler_seq (parser
);
17351 /* We're done with the handlers. */
17352 finish_function_handler_sequence (try_block
, compound_stmt
);
17354 return ctor_initializer_p
;
17357 /* Parse a handler-seq.
17360 handler handler-seq [opt] */
17363 cp_parser_handler_seq (cp_parser
* parser
)
17369 /* Parse the handler. */
17370 cp_parser_handler (parser
);
17371 /* Peek at the next token. */
17372 token
= cp_lexer_peek_token (parser
->lexer
);
17373 /* If it's not `catch' then there are no more handlers. */
17374 if (!cp_parser_is_keyword (token
, RID_CATCH
))
17379 /* Parse a handler.
17382 catch ( exception-declaration ) compound-statement */
17385 cp_parser_handler (cp_parser
* parser
)
17390 cp_parser_require_keyword (parser
, RID_CATCH
, "%<catch%>");
17391 handler
= begin_handler ();
17392 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
17393 declaration
= cp_parser_exception_declaration (parser
);
17394 finish_handler_parms (declaration
, handler
);
17395 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
17396 cp_parser_compound_statement (parser
, NULL
, false);
17397 finish_handler (handler
);
17400 /* Parse an exception-declaration.
17402 exception-declaration:
17403 type-specifier-seq declarator
17404 type-specifier-seq abstract-declarator
17408 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17409 ellipsis variant is used. */
17412 cp_parser_exception_declaration (cp_parser
* parser
)
17414 cp_decl_specifier_seq type_specifiers
;
17415 cp_declarator
*declarator
;
17416 const char *saved_message
;
17418 /* If it's an ellipsis, it's easy to handle. */
17419 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17421 /* Consume the `...' token. */
17422 cp_lexer_consume_token (parser
->lexer
);
17426 /* Types may not be defined in exception-declarations. */
17427 saved_message
= parser
->type_definition_forbidden_message
;
17428 parser
->type_definition_forbidden_message
17429 = G_("types may not be defined in exception-declarations");
17431 /* Parse the type-specifier-seq. */
17432 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
17433 /*is_trailing_return=*/false,
17435 /* If it's a `)', then there is no declarator. */
17436 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
17439 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
17440 /*ctor_dtor_or_conv_p=*/NULL
,
17441 /*parenthesized_p=*/NULL
,
17442 /*member_p=*/false);
17444 /* Restore the saved message. */
17445 parser
->type_definition_forbidden_message
= saved_message
;
17447 if (!type_specifiers
.any_specifiers_p
)
17448 return error_mark_node
;
17450 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
17453 /* Parse a throw-expression.
17456 throw assignment-expression [opt]
17458 Returns a THROW_EXPR representing the throw-expression. */
17461 cp_parser_throw_expression (cp_parser
* parser
)
17466 cp_parser_require_keyword (parser
, RID_THROW
, "%<throw%>");
17467 token
= cp_lexer_peek_token (parser
->lexer
);
17468 /* Figure out whether or not there is an assignment-expression
17469 following the "throw" keyword. */
17470 if (token
->type
== CPP_COMMA
17471 || token
->type
== CPP_SEMICOLON
17472 || token
->type
== CPP_CLOSE_PAREN
17473 || token
->type
== CPP_CLOSE_SQUARE
17474 || token
->type
== CPP_CLOSE_BRACE
17475 || token
->type
== CPP_COLON
)
17476 expression
= NULL_TREE
;
17478 expression
= cp_parser_assignment_expression (parser
,
17479 /*cast_p=*/false, NULL
);
17481 return build_throw (expression
);
17484 /* GNU Extensions */
17486 /* Parse an (optional) asm-specification.
17489 asm ( string-literal )
17491 If the asm-specification is present, returns a STRING_CST
17492 corresponding to the string-literal. Otherwise, returns
17496 cp_parser_asm_specification_opt (cp_parser
* parser
)
17499 tree asm_specification
;
17501 /* Peek at the next token. */
17502 token
= cp_lexer_peek_token (parser
->lexer
);
17503 /* If the next token isn't the `asm' keyword, then there's no
17504 asm-specification. */
17505 if (!cp_parser_is_keyword (token
, RID_ASM
))
17508 /* Consume the `asm' token. */
17509 cp_lexer_consume_token (parser
->lexer
);
17510 /* Look for the `('. */
17511 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
17513 /* Look for the string-literal. */
17514 asm_specification
= cp_parser_string_literal (parser
, false, false);
17516 /* Look for the `)'. */
17517 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
17519 return asm_specification
;
17522 /* Parse an asm-operand-list.
17526 asm-operand-list , asm-operand
17529 string-literal ( expression )
17530 [ string-literal ] string-literal ( expression )
17532 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17533 each node is the expression. The TREE_PURPOSE is itself a
17534 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17535 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17536 is a STRING_CST for the string literal before the parenthesis. Returns
17537 ERROR_MARK_NODE if any of the operands are invalid. */
17540 cp_parser_asm_operand_list (cp_parser
* parser
)
17542 tree asm_operands
= NULL_TREE
;
17543 bool invalid_operands
= false;
17547 tree string_literal
;
17551 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
17553 /* Consume the `[' token. */
17554 cp_lexer_consume_token (parser
->lexer
);
17555 /* Read the operand name. */
17556 name
= cp_parser_identifier (parser
);
17557 if (name
!= error_mark_node
)
17558 name
= build_string (IDENTIFIER_LENGTH (name
),
17559 IDENTIFIER_POINTER (name
));
17560 /* Look for the closing `]'. */
17561 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "%<]%>");
17565 /* Look for the string-literal. */
17566 string_literal
= cp_parser_string_literal (parser
, false, false);
17568 /* Look for the `('. */
17569 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
17570 /* Parse the expression. */
17571 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
17572 /* Look for the `)'. */
17573 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
17575 if (name
== error_mark_node
17576 || string_literal
== error_mark_node
17577 || expression
== error_mark_node
)
17578 invalid_operands
= true;
17580 /* Add this operand to the list. */
17581 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
17584 /* If the next token is not a `,', there are no more
17586 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
17588 /* Consume the `,'. */
17589 cp_lexer_consume_token (parser
->lexer
);
17592 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
17595 /* Parse an asm-clobber-list.
17599 asm-clobber-list , string-literal
17601 Returns a TREE_LIST, indicating the clobbers in the order that they
17602 appeared. The TREE_VALUE of each node is a STRING_CST. */
17605 cp_parser_asm_clobber_list (cp_parser
* parser
)
17607 tree clobbers
= NULL_TREE
;
17611 tree string_literal
;
17613 /* Look for the string literal. */
17614 string_literal
= cp_parser_string_literal (parser
, false, false);
17615 /* Add it to the list. */
17616 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
17617 /* If the next token is not a `,', then the list is
17619 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
17621 /* Consume the `,' token. */
17622 cp_lexer_consume_token (parser
->lexer
);
17628 /* Parse an asm-label-list.
17632 asm-label-list , identifier
17634 Returns a TREE_LIST, indicating the labels in the order that they
17635 appeared. The TREE_VALUE of each node is a label. */
17638 cp_parser_asm_label_list (cp_parser
* parser
)
17640 tree labels
= NULL_TREE
;
17644 tree identifier
, label
, name
;
17646 /* Look for the identifier. */
17647 identifier
= cp_parser_identifier (parser
);
17648 if (!error_operand_p (identifier
))
17650 label
= lookup_label (identifier
);
17651 if (TREE_CODE (label
) == LABEL_DECL
)
17653 TREE_USED (label
) = 1;
17654 check_goto (label
);
17655 name
= build_string (IDENTIFIER_LENGTH (identifier
),
17656 IDENTIFIER_POINTER (identifier
));
17657 labels
= tree_cons (name
, label
, labels
);
17660 /* If the next token is not a `,', then the list is
17662 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
17664 /* Consume the `,' token. */
17665 cp_lexer_consume_token (parser
->lexer
);
17668 return nreverse (labels
);
17671 /* Parse an (optional) series of attributes.
17674 attributes attribute
17677 __attribute__ (( attribute-list [opt] ))
17679 The return value is as for cp_parser_attribute_list. */
17682 cp_parser_attributes_opt (cp_parser
* parser
)
17684 tree attributes
= NULL_TREE
;
17689 tree attribute_list
;
17691 /* Peek at the next token. */
17692 token
= cp_lexer_peek_token (parser
->lexer
);
17693 /* If it's not `__attribute__', then we're done. */
17694 if (token
->keyword
!= RID_ATTRIBUTE
)
17697 /* Consume the `__attribute__' keyword. */
17698 cp_lexer_consume_token (parser
->lexer
);
17699 /* Look for the two `(' tokens. */
17700 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
17701 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
17703 /* Peek at the next token. */
17704 token
= cp_lexer_peek_token (parser
->lexer
);
17705 if (token
->type
!= CPP_CLOSE_PAREN
)
17706 /* Parse the attribute-list. */
17707 attribute_list
= cp_parser_attribute_list (parser
);
17709 /* If the next token is a `)', then there is no attribute
17711 attribute_list
= NULL
;
17713 /* Look for the two `)' tokens. */
17714 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
17715 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
17717 /* Add these new attributes to the list. */
17718 attributes
= chainon (attributes
, attribute_list
);
17724 /* Parse an attribute-list.
17728 attribute-list , attribute
17732 identifier ( identifier )
17733 identifier ( identifier , expression-list )
17734 identifier ( expression-list )
17736 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17737 to an attribute. The TREE_PURPOSE of each node is the identifier
17738 indicating which attribute is in use. The TREE_VALUE represents
17739 the arguments, if any. */
17742 cp_parser_attribute_list (cp_parser
* parser
)
17744 tree attribute_list
= NULL_TREE
;
17745 bool save_translate_strings_p
= parser
->translate_strings_p
;
17747 parser
->translate_strings_p
= false;
17754 /* Look for the identifier. We also allow keywords here; for
17755 example `__attribute__ ((const))' is legal. */
17756 token
= cp_lexer_peek_token (parser
->lexer
);
17757 if (token
->type
== CPP_NAME
17758 || token
->type
== CPP_KEYWORD
)
17760 tree arguments
= NULL_TREE
;
17762 /* Consume the token. */
17763 token
= cp_lexer_consume_token (parser
->lexer
);
17765 /* Save away the identifier that indicates which attribute
17767 identifier
= (token
->type
== CPP_KEYWORD
)
17768 /* For keywords, use the canonical spelling, not the
17769 parsed identifier. */
17770 ? ridpointers
[(int) token
->keyword
]
17773 attribute
= build_tree_list (identifier
, NULL_TREE
);
17775 /* Peek at the next token. */
17776 token
= cp_lexer_peek_token (parser
->lexer
);
17777 /* If it's an `(', then parse the attribute arguments. */
17778 if (token
->type
== CPP_OPEN_PAREN
)
17781 int attr_flag
= (attribute_takes_identifier_p (identifier
)
17782 ? id_attr
: normal_attr
);
17783 vec
= cp_parser_parenthesized_expression_list
17784 (parser
, attr_flag
, /*cast_p=*/false,
17785 /*allow_expansion_p=*/false,
17786 /*non_constant_p=*/NULL
);
17788 arguments
= error_mark_node
;
17791 arguments
= build_tree_list_vec (vec
);
17792 release_tree_vector (vec
);
17794 /* Save the arguments away. */
17795 TREE_VALUE (attribute
) = arguments
;
17798 if (arguments
!= error_mark_node
)
17800 /* Add this attribute to the list. */
17801 TREE_CHAIN (attribute
) = attribute_list
;
17802 attribute_list
= attribute
;
17805 token
= cp_lexer_peek_token (parser
->lexer
);
17807 /* Now, look for more attributes. If the next token isn't a
17808 `,', we're done. */
17809 if (token
->type
!= CPP_COMMA
)
17812 /* Consume the comma and keep going. */
17813 cp_lexer_consume_token (parser
->lexer
);
17815 parser
->translate_strings_p
= save_translate_strings_p
;
17817 /* We built up the list in reverse order. */
17818 return nreverse (attribute_list
);
17821 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17822 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17823 current value of the PEDANTIC flag, regardless of whether or not
17824 the `__extension__' keyword is present. The caller is responsible
17825 for restoring the value of the PEDANTIC flag. */
17828 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
17830 /* Save the old value of the PEDANTIC flag. */
17831 *saved_pedantic
= pedantic
;
17833 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
17835 /* Consume the `__extension__' token. */
17836 cp_lexer_consume_token (parser
->lexer
);
17837 /* We're not being pedantic while the `__extension__' keyword is
17847 /* Parse a label declaration.
17850 __label__ label-declarator-seq ;
17852 label-declarator-seq:
17853 identifier , label-declarator-seq
17857 cp_parser_label_declaration (cp_parser
* parser
)
17859 /* Look for the `__label__' keyword. */
17860 cp_parser_require_keyword (parser
, RID_LABEL
, "%<__label__%>");
17866 /* Look for an identifier. */
17867 identifier
= cp_parser_identifier (parser
);
17868 /* If we failed, stop. */
17869 if (identifier
== error_mark_node
)
17871 /* Declare it as a label. */
17872 finish_label_decl (identifier
);
17873 /* If the next token is a `;', stop. */
17874 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17876 /* Look for the `,' separating the label declarations. */
17877 cp_parser_require (parser
, CPP_COMMA
, "%<,%>");
17880 /* Look for the final `;'. */
17881 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
17884 /* Support Functions */
17886 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17887 NAME should have one of the representations used for an
17888 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17889 is returned. If PARSER->SCOPE is a dependent type, then a
17890 SCOPE_REF is returned.
17892 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17893 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17894 was formed. Abstractly, such entities should not be passed to this
17895 function, because they do not need to be looked up, but it is
17896 simpler to check for this special case here, rather than at the
17899 In cases not explicitly covered above, this function returns a
17900 DECL, OVERLOAD, or baselink representing the result of the lookup.
17901 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17904 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17905 (e.g., "struct") that was used. In that case bindings that do not
17906 refer to types are ignored.
17908 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17911 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17914 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17917 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17918 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17919 NULL_TREE otherwise. */
17922 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
17923 enum tag_types tag_type
,
17926 bool check_dependency
,
17927 tree
*ambiguous_decls
,
17928 location_t name_location
)
17932 tree object_type
= parser
->context
->object_type
;
17934 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
17935 flags
|= LOOKUP_COMPLAIN
;
17937 /* Assume that the lookup will be unambiguous. */
17938 if (ambiguous_decls
)
17939 *ambiguous_decls
= NULL_TREE
;
17941 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17942 no longer valid. Note that if we are parsing tentatively, and
17943 the parse fails, OBJECT_TYPE will be automatically restored. */
17944 parser
->context
->object_type
= NULL_TREE
;
17946 if (name
== error_mark_node
)
17947 return error_mark_node
;
17949 /* A template-id has already been resolved; there is no lookup to
17951 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
17953 if (BASELINK_P (name
))
17955 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
17956 == TEMPLATE_ID_EXPR
);
17960 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17961 it should already have been checked to make sure that the name
17962 used matches the type being destroyed. */
17963 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
17967 /* Figure out to which type this destructor applies. */
17969 type
= parser
->scope
;
17970 else if (object_type
)
17971 type
= object_type
;
17973 type
= current_class_type
;
17974 /* If that's not a class type, there is no destructor. */
17975 if (!type
|| !CLASS_TYPE_P (type
))
17976 return error_mark_node
;
17977 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
17978 lazily_declare_fn (sfk_destructor
, type
);
17979 if (!CLASSTYPE_DESTRUCTORS (type
))
17980 return error_mark_node
;
17981 /* If it was a class type, return the destructor. */
17982 return CLASSTYPE_DESTRUCTORS (type
);
17985 /* By this point, the NAME should be an ordinary identifier. If
17986 the id-expression was a qualified name, the qualifying scope is
17987 stored in PARSER->SCOPE at this point. */
17988 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
17990 /* Perform the lookup. */
17995 if (parser
->scope
== error_mark_node
)
17996 return error_mark_node
;
17998 /* If the SCOPE is dependent, the lookup must be deferred until
17999 the template is instantiated -- unless we are explicitly
18000 looking up names in uninstantiated templates. Even then, we
18001 cannot look up the name if the scope is not a class type; it
18002 might, for example, be a template type parameter. */
18003 dependent_p
= (TYPE_P (parser
->scope
)
18004 && dependent_scope_p (parser
->scope
));
18005 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
18007 /* Defer lookup. */
18008 decl
= error_mark_node
;
18011 tree pushed_scope
= NULL_TREE
;
18013 /* If PARSER->SCOPE is a dependent type, then it must be a
18014 class type, and we must not be checking dependencies;
18015 otherwise, we would have processed this lookup above. So
18016 that PARSER->SCOPE is not considered a dependent base by
18017 lookup_member, we must enter the scope here. */
18019 pushed_scope
= push_scope (parser
->scope
);
18021 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18022 lookup result and the nested-name-specifier nominates a class C:
18023 * if the name specified after the nested-name-specifier, when
18024 looked up in C, is the injected-class-name of C (Clause 9), or
18025 * if the name specified after the nested-name-specifier is the
18026 same as the identifier or the simple-template-id's template-
18027 name in the last component of the nested-name-specifier,
18028 the name is instead considered to name the constructor of
18029 class C. [ Note: for example, the constructor is not an
18030 acceptable lookup result in an elaborated-type-specifier so
18031 the constructor would not be used in place of the
18032 injected-class-name. --end note ] Such a constructor name
18033 shall be used only in the declarator-id of a declaration that
18034 names a constructor or in a using-declaration. */
18035 if (tag_type
== none_type
18036 && CLASS_TYPE_P (parser
->scope
)
18037 && constructor_name_p (name
, parser
->scope
))
18038 name
= ctor_identifier
;
18040 /* If the PARSER->SCOPE is a template specialization, it
18041 may be instantiated during name lookup. In that case,
18042 errors may be issued. Even if we rollback the current
18043 tentative parse, those errors are valid. */
18044 decl
= lookup_qualified_name (parser
->scope
, name
,
18045 tag_type
!= none_type
,
18046 /*complain=*/true);
18048 /* If we have a single function from a using decl, pull it out. */
18049 if (TREE_CODE (decl
) == OVERLOAD
18050 && !really_overloaded_fn (decl
))
18051 decl
= OVL_FUNCTION (decl
);
18054 pop_scope (pushed_scope
);
18057 /* If the scope is a dependent type and either we deferred lookup or
18058 we did lookup but didn't find the name, rememeber the name. */
18059 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
18060 && dependent_type_p (parser
->scope
))
18066 /* The resolution to Core Issue 180 says that `struct
18067 A::B' should be considered a type-name, even if `A'
18069 type
= make_typename_type (parser
->scope
, name
, tag_type
,
18070 /*complain=*/tf_error
);
18071 decl
= TYPE_NAME (type
);
18073 else if (is_template
18074 && (cp_parser_next_token_ends_template_argument_p (parser
)
18075 || cp_lexer_next_token_is (parser
->lexer
,
18077 decl
= make_unbound_class_template (parser
->scope
,
18079 /*complain=*/tf_error
);
18081 decl
= build_qualified_name (/*type=*/NULL_TREE
,
18082 parser
->scope
, name
,
18085 parser
->qualifying_scope
= parser
->scope
;
18086 parser
->object_scope
= NULL_TREE
;
18088 else if (object_type
)
18090 tree object_decl
= NULL_TREE
;
18091 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18092 OBJECT_TYPE is not a class. */
18093 if (CLASS_TYPE_P (object_type
))
18094 /* If the OBJECT_TYPE is a template specialization, it may
18095 be instantiated during name lookup. In that case, errors
18096 may be issued. Even if we rollback the current tentative
18097 parse, those errors are valid. */
18098 object_decl
= lookup_member (object_type
,
18101 tag_type
!= none_type
);
18102 /* Look it up in the enclosing context, too. */
18103 decl
= lookup_name_real (name
, tag_type
!= none_type
,
18105 /*block_p=*/true, is_namespace
, flags
);
18106 parser
->object_scope
= object_type
;
18107 parser
->qualifying_scope
= NULL_TREE
;
18109 decl
= object_decl
;
18113 decl
= lookup_name_real (name
, tag_type
!= none_type
,
18115 /*block_p=*/true, is_namespace
, flags
);
18116 parser
->qualifying_scope
= NULL_TREE
;
18117 parser
->object_scope
= NULL_TREE
;
18120 /* If the lookup failed, let our caller know. */
18121 if (!decl
|| decl
== error_mark_node
)
18122 return error_mark_node
;
18124 /* Pull out the template from an injected-class-name (or multiple). */
18126 decl
= maybe_get_template_decl_from_type_decl (decl
);
18128 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18129 if (TREE_CODE (decl
) == TREE_LIST
)
18131 if (ambiguous_decls
)
18132 *ambiguous_decls
= decl
;
18133 /* The error message we have to print is too complicated for
18134 cp_parser_error, so we incorporate its actions directly. */
18135 if (!cp_parser_simulate_error (parser
))
18137 error_at (name_location
, "reference to %qD is ambiguous",
18139 print_candidates (decl
);
18141 return error_mark_node
;
18144 gcc_assert (DECL_P (decl
)
18145 || TREE_CODE (decl
) == OVERLOAD
18146 || TREE_CODE (decl
) == SCOPE_REF
18147 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
18148 || BASELINK_P (decl
));
18150 /* If we have resolved the name of a member declaration, check to
18151 see if the declaration is accessible. When the name resolves to
18152 set of overloaded functions, accessibility is checked when
18153 overload resolution is done.
18155 During an explicit instantiation, access is not checked at all,
18156 as per [temp.explicit]. */
18158 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
18163 /* Like cp_parser_lookup_name, but for use in the typical case where
18164 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18165 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18168 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
18170 return cp_parser_lookup_name (parser
, name
,
18172 /*is_template=*/false,
18173 /*is_namespace=*/false,
18174 /*check_dependency=*/true,
18175 /*ambiguous_decls=*/NULL
,
18179 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18180 the current context, return the TYPE_DECL. If TAG_NAME_P is
18181 true, the DECL indicates the class being defined in a class-head,
18182 or declared in an elaborated-type-specifier.
18184 Otherwise, return DECL. */
18187 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
18189 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18190 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18193 template <typename T> struct B;
18196 template <typename T> struct A::B {};
18198 Similarly, in an elaborated-type-specifier:
18200 namespace N { struct X{}; }
18203 template <typename T> friend struct N::X;
18206 However, if the DECL refers to a class type, and we are in
18207 the scope of the class, then the name lookup automatically
18208 finds the TYPE_DECL created by build_self_reference rather
18209 than a TEMPLATE_DECL. For example, in:
18211 template <class T> struct S {
18215 there is no need to handle such case. */
18217 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
18218 return DECL_TEMPLATE_RESULT (decl
);
18223 /* If too many, or too few, template-parameter lists apply to the
18224 declarator, issue an error message. Returns TRUE if all went well,
18225 and FALSE otherwise. */
18228 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
18229 cp_declarator
*declarator
,
18230 location_t declarator_location
)
18232 unsigned num_templates
;
18234 /* We haven't seen any classes that involve template parameters yet. */
18237 switch (declarator
->kind
)
18240 if (declarator
->u
.id
.qualifying_scope
)
18244 scope
= declarator
->u
.id
.qualifying_scope
;
18246 while (scope
&& CLASS_TYPE_P (scope
))
18248 /* You're supposed to have one `template <...>'
18249 for every template class, but you don't need one
18250 for a full specialization. For example:
18252 template <class T> struct S{};
18253 template <> struct S<int> { void f(); };
18254 void S<int>::f () {}
18256 is correct; there shouldn't be a `template <>' for
18257 the definition of `S<int>::f'. */
18258 if (!CLASSTYPE_TEMPLATE_INFO (scope
))
18259 /* If SCOPE does not have template information of any
18260 kind, then it is not a template, nor is it nested
18261 within a template. */
18263 if (explicit_class_specialization_p (scope
))
18265 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
18268 scope
= TYPE_CONTEXT (scope
);
18271 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
18272 == TEMPLATE_ID_EXPR
)
18273 /* If the DECLARATOR has the form `X<y>' then it uses one
18274 additional level of template parameters. */
18277 return cp_parser_check_template_parameters
18278 (parser
, num_templates
, declarator_location
, declarator
);
18284 case cdk_reference
:
18286 return (cp_parser_check_declarator_template_parameters
18287 (parser
, declarator
->declarator
, declarator_location
));
18293 gcc_unreachable ();
18298 /* NUM_TEMPLATES were used in the current declaration. If that is
18299 invalid, return FALSE and issue an error messages. Otherwise,
18300 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18301 declarator and we can print more accurate diagnostics. */
18304 cp_parser_check_template_parameters (cp_parser
* parser
,
18305 unsigned num_templates
,
18306 location_t location
,
18307 cp_declarator
*declarator
)
18309 /* If there are the same number of template classes and parameter
18310 lists, that's OK. */
18311 if (parser
->num_template_parameter_lists
== num_templates
)
18313 /* If there are more, but only one more, then we are referring to a
18314 member template. That's OK too. */
18315 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
18317 /* If there are more template classes than parameter lists, we have
18320 template <class T> void S<T>::R<T>::f (); */
18321 if (parser
->num_template_parameter_lists
< num_templates
)
18323 if (declarator
&& !current_function_decl
)
18324 error_at (location
, "specializing member %<%T::%E%> "
18325 "requires %<template<>%> syntax",
18326 declarator
->u
.id
.qualifying_scope
,
18327 declarator
->u
.id
.unqualified_name
);
18328 else if (declarator
)
18329 error_at (location
, "invalid declaration of %<%T::%E%>",
18330 declarator
->u
.id
.qualifying_scope
,
18331 declarator
->u
.id
.unqualified_name
);
18333 error_at (location
, "too few template-parameter-lists");
18336 /* Otherwise, there are too many template parameter lists. We have
18339 template <class T> template <class U> void S::f(); */
18340 error_at (location
, "too many template-parameter-lists");
18344 /* Parse an optional `::' token indicating that the following name is
18345 from the global namespace. If so, PARSER->SCOPE is set to the
18346 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18347 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18348 Returns the new value of PARSER->SCOPE, if the `::' token is
18349 present, and NULL_TREE otherwise. */
18352 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
18356 /* Peek at the next token. */
18357 token
= cp_lexer_peek_token (parser
->lexer
);
18358 /* If we're looking at a `::' token then we're starting from the
18359 global namespace, not our current location. */
18360 if (token
->type
== CPP_SCOPE
)
18362 /* Consume the `::' token. */
18363 cp_lexer_consume_token (parser
->lexer
);
18364 /* Set the SCOPE so that we know where to start the lookup. */
18365 parser
->scope
= global_namespace
;
18366 parser
->qualifying_scope
= global_namespace
;
18367 parser
->object_scope
= NULL_TREE
;
18369 return parser
->scope
;
18371 else if (!current_scope_valid_p
)
18373 parser
->scope
= NULL_TREE
;
18374 parser
->qualifying_scope
= NULL_TREE
;
18375 parser
->object_scope
= NULL_TREE
;
18381 /* Returns TRUE if the upcoming token sequence is the start of a
18382 constructor declarator. If FRIEND_P is true, the declarator is
18383 preceded by the `friend' specifier. */
18386 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
18388 bool constructor_p
;
18389 tree nested_name_specifier
;
18390 cp_token
*next_token
;
18392 /* The common case is that this is not a constructor declarator, so
18393 try to avoid doing lots of work if at all possible. It's not
18394 valid declare a constructor at function scope. */
18395 if (parser
->in_function_body
)
18397 /* And only certain tokens can begin a constructor declarator. */
18398 next_token
= cp_lexer_peek_token (parser
->lexer
);
18399 if (next_token
->type
!= CPP_NAME
18400 && next_token
->type
!= CPP_SCOPE
18401 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
18402 && next_token
->type
!= CPP_TEMPLATE_ID
)
18405 /* Parse tentatively; we are going to roll back all of the tokens
18407 cp_parser_parse_tentatively (parser
);
18408 /* Assume that we are looking at a constructor declarator. */
18409 constructor_p
= true;
18411 /* Look for the optional `::' operator. */
18412 cp_parser_global_scope_opt (parser
,
18413 /*current_scope_valid_p=*/false);
18414 /* Look for the nested-name-specifier. */
18415 nested_name_specifier
18416 = (cp_parser_nested_name_specifier_opt (parser
,
18417 /*typename_keyword_p=*/false,
18418 /*check_dependency_p=*/false,
18420 /*is_declaration=*/false));
18421 /* Outside of a class-specifier, there must be a
18422 nested-name-specifier. */
18423 if (!nested_name_specifier
&&
18424 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
18426 constructor_p
= false;
18427 else if (nested_name_specifier
== error_mark_node
)
18428 constructor_p
= false;
18430 /* If we have a class scope, this is easy; DR 147 says that S::S always
18431 names the constructor, and no other qualified name could. */
18432 if (constructor_p
&& nested_name_specifier
18433 && TYPE_P (nested_name_specifier
))
18435 tree id
= cp_parser_unqualified_id (parser
,
18436 /*template_keyword_p=*/false,
18437 /*check_dependency_p=*/false,
18438 /*declarator_p=*/true,
18439 /*optional_p=*/false);
18440 if (is_overloaded_fn (id
))
18441 id
= DECL_NAME (get_first_fn (id
));
18442 if (!constructor_name_p (id
, nested_name_specifier
))
18443 constructor_p
= false;
18445 /* If we still think that this might be a constructor-declarator,
18446 look for a class-name. */
18447 else if (constructor_p
)
18451 template <typename T> struct S {
18455 we must recognize that the nested `S' names a class. */
18457 type_decl
= cp_parser_class_name (parser
,
18458 /*typename_keyword_p=*/false,
18459 /*template_keyword_p=*/false,
18461 /*check_dependency_p=*/false,
18462 /*class_head_p=*/false,
18463 /*is_declaration=*/false);
18464 /* If there was no class-name, then this is not a constructor. */
18465 constructor_p
= !cp_parser_error_occurred (parser
);
18467 /* If we're still considering a constructor, we have to see a `(',
18468 to begin the parameter-declaration-clause, followed by either a
18469 `)', an `...', or a decl-specifier. We need to check for a
18470 type-specifier to avoid being fooled into thinking that:
18474 is a constructor. (It is actually a function named `f' that
18475 takes one parameter (of type `int') and returns a value of type
18478 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
18479 constructor_p
= false;
18482 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
18483 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
18484 /* A parameter declaration begins with a decl-specifier,
18485 which is either the "attribute" keyword, a storage class
18486 specifier, or (usually) a type-specifier. */
18487 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
18490 tree pushed_scope
= NULL_TREE
;
18491 unsigned saved_num_template_parameter_lists
;
18493 /* Names appearing in the type-specifier should be looked up
18494 in the scope of the class. */
18495 if (current_class_type
)
18499 type
= TREE_TYPE (type_decl
);
18500 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18502 type
= resolve_typename_type (type
,
18503 /*only_current_p=*/false);
18504 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18506 cp_parser_abort_tentative_parse (parser
);
18510 pushed_scope
= push_scope (type
);
18513 /* Inside the constructor parameter list, surrounding
18514 template-parameter-lists do not apply. */
18515 saved_num_template_parameter_lists
18516 = parser
->num_template_parameter_lists
;
18517 parser
->num_template_parameter_lists
= 0;
18519 /* Look for the type-specifier. */
18520 cp_parser_type_specifier (parser
,
18521 CP_PARSER_FLAGS_NONE
,
18522 /*decl_specs=*/NULL
,
18523 /*is_declarator=*/true,
18524 /*declares_class_or_enum=*/NULL
,
18525 /*is_cv_qualifier=*/NULL
);
18527 parser
->num_template_parameter_lists
18528 = saved_num_template_parameter_lists
;
18530 /* Leave the scope of the class. */
18532 pop_scope (pushed_scope
);
18534 constructor_p
= !cp_parser_error_occurred (parser
);
18538 /* We did not really want to consume any tokens. */
18539 cp_parser_abort_tentative_parse (parser
);
18541 return constructor_p
;
18544 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18545 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18546 they must be performed once we are in the scope of the function.
18548 Returns the function defined. */
18551 cp_parser_function_definition_from_specifiers_and_declarator
18552 (cp_parser
* parser
,
18553 cp_decl_specifier_seq
*decl_specifiers
,
18555 const cp_declarator
*declarator
)
18560 /* Begin the function-definition. */
18561 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
18563 /* The things we're about to see are not directly qualified by any
18564 template headers we've seen thus far. */
18565 reset_specialization ();
18567 /* If there were names looked up in the decl-specifier-seq that we
18568 did not check, check them now. We must wait until we are in the
18569 scope of the function to perform the checks, since the function
18570 might be a friend. */
18571 perform_deferred_access_checks ();
18575 /* Skip the entire function. */
18576 cp_parser_skip_to_end_of_block_or_statement (parser
);
18577 fn
= error_mark_node
;
18579 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
18581 /* Seen already, skip it. An error message has already been output. */
18582 cp_parser_skip_to_end_of_block_or_statement (parser
);
18583 fn
= current_function_decl
;
18584 current_function_decl
= NULL_TREE
;
18585 /* If this is a function from a class, pop the nested class. */
18586 if (current_class_name
)
18587 pop_nested_class ();
18590 fn
= cp_parser_function_definition_after_declarator (parser
,
18591 /*inline_p=*/false);
18596 /* Parse the part of a function-definition that follows the
18597 declarator. INLINE_P is TRUE iff this function is an inline
18598 function defined within a class-specifier.
18600 Returns the function defined. */
18603 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
18607 bool ctor_initializer_p
= false;
18608 bool saved_in_unbraced_linkage_specification_p
;
18609 bool saved_in_function_body
;
18610 unsigned saved_num_template_parameter_lists
;
18613 saved_in_function_body
= parser
->in_function_body
;
18614 parser
->in_function_body
= true;
18615 /* If the next token is `return', then the code may be trying to
18616 make use of the "named return value" extension that G++ used to
18618 token
= cp_lexer_peek_token (parser
->lexer
);
18619 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
18621 /* Consume the `return' keyword. */
18622 cp_lexer_consume_token (parser
->lexer
);
18623 /* Look for the identifier that indicates what value is to be
18625 cp_parser_identifier (parser
);
18626 /* Issue an error message. */
18627 error_at (token
->location
,
18628 "named return values are no longer supported");
18629 /* Skip tokens until we reach the start of the function body. */
18632 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18633 if (token
->type
== CPP_OPEN_BRACE
18634 || token
->type
== CPP_EOF
18635 || token
->type
== CPP_PRAGMA_EOL
)
18637 cp_lexer_consume_token (parser
->lexer
);
18640 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18641 anything declared inside `f'. */
18642 saved_in_unbraced_linkage_specification_p
18643 = parser
->in_unbraced_linkage_specification_p
;
18644 parser
->in_unbraced_linkage_specification_p
= false;
18645 /* Inside the function, surrounding template-parameter-lists do not
18647 saved_num_template_parameter_lists
18648 = parser
->num_template_parameter_lists
;
18649 parser
->num_template_parameter_lists
= 0;
18651 start_lambda_scope (current_function_decl
);
18653 /* If the next token is `try', then we are looking at a
18654 function-try-block. */
18655 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
18656 ctor_initializer_p
= cp_parser_function_try_block (parser
);
18657 /* A function-try-block includes the function-body, so we only do
18658 this next part if we're not processing a function-try-block. */
18661 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
18663 finish_lambda_scope ();
18665 /* Finish the function. */
18666 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
18667 (inline_p
? 2 : 0));
18668 /* Generate code for it, if necessary. */
18669 expand_or_defer_fn (fn
);
18670 /* Restore the saved values. */
18671 parser
->in_unbraced_linkage_specification_p
18672 = saved_in_unbraced_linkage_specification_p
;
18673 parser
->num_template_parameter_lists
18674 = saved_num_template_parameter_lists
;
18675 parser
->in_function_body
= saved_in_function_body
;
18680 /* Parse a template-declaration, assuming that the `export' (and
18681 `extern') keywords, if present, has already been scanned. MEMBER_P
18682 is as for cp_parser_template_declaration. */
18685 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
18687 tree decl
= NULL_TREE
;
18688 VEC (deferred_access_check
,gc
) *checks
;
18689 tree parameter_list
;
18690 bool friend_p
= false;
18691 bool need_lang_pop
;
18694 /* Look for the `template' keyword. */
18695 token
= cp_lexer_peek_token (parser
->lexer
);
18696 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "%<template%>"))
18700 if (!cp_parser_require (parser
, CPP_LESS
, "%<<%>"))
18702 if (at_class_scope_p () && current_function_decl
)
18704 /* 14.5.2.2 [temp.mem]
18706 A local class shall not have member templates. */
18707 error_at (token
->location
,
18708 "invalid declaration of member template in local class");
18709 cp_parser_skip_to_end_of_block_or_statement (parser
);
18714 A template ... shall not have C linkage. */
18715 if (current_lang_name
== lang_name_c
)
18717 error_at (token
->location
, "template with C linkage");
18718 /* Give it C++ linkage to avoid confusing other parts of the
18720 push_lang_context (lang_name_cplusplus
);
18721 need_lang_pop
= true;
18724 need_lang_pop
= false;
18726 /* We cannot perform access checks on the template parameter
18727 declarations until we know what is being declared, just as we
18728 cannot check the decl-specifier list. */
18729 push_deferring_access_checks (dk_deferred
);
18731 /* If the next token is `>', then we have an invalid
18732 specialization. Rather than complain about an invalid template
18733 parameter, issue an error message here. */
18734 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
18736 cp_parser_error (parser
, "invalid explicit specialization");
18737 begin_specialization ();
18738 parameter_list
= NULL_TREE
;
18741 /* Parse the template parameters. */
18742 parameter_list
= cp_parser_template_parameter_list (parser
);
18744 /* Get the deferred access checks from the parameter list. These
18745 will be checked once we know what is being declared, as for a
18746 member template the checks must be performed in the scope of the
18747 class containing the member. */
18748 checks
= get_deferred_access_checks ();
18750 /* Look for the `>'. */
18751 cp_parser_skip_to_end_of_template_parameter_list (parser
);
18752 /* We just processed one more parameter list. */
18753 ++parser
->num_template_parameter_lists
;
18754 /* If the next token is `template', there are more template
18756 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
18758 cp_parser_template_declaration_after_export (parser
, member_p
);
18761 /* There are no access checks when parsing a template, as we do not
18762 know if a specialization will be a friend. */
18763 push_deferring_access_checks (dk_no_check
);
18764 token
= cp_lexer_peek_token (parser
->lexer
);
18765 decl
= cp_parser_single_declaration (parser
,
18768 /*explicit_specialization_p=*/false,
18770 pop_deferring_access_checks ();
18772 /* If this is a member template declaration, let the front
18774 if (member_p
&& !friend_p
&& decl
)
18776 if (TREE_CODE (decl
) == TYPE_DECL
)
18777 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
18779 decl
= finish_member_template_decl (decl
);
18781 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
18782 make_friend_class (current_class_type
, TREE_TYPE (decl
),
18783 /*complain=*/true);
18785 /* We are done with the current parameter list. */
18786 --parser
->num_template_parameter_lists
;
18788 pop_deferring_access_checks ();
18791 finish_template_decl (parameter_list
);
18793 /* Register member declarations. */
18794 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
18795 finish_member_declaration (decl
);
18796 /* For the erroneous case of a template with C linkage, we pushed an
18797 implicit C++ linkage scope; exit that scope now. */
18799 pop_lang_context ();
18800 /* If DECL is a function template, we must return to parse it later.
18801 (Even though there is no definition, there might be default
18802 arguments that need handling.) */
18803 if (member_p
&& decl
18804 && (TREE_CODE (decl
) == FUNCTION_DECL
18805 || DECL_FUNCTION_TEMPLATE_P (decl
)))
18806 TREE_VALUE (parser
->unparsed_functions_queues
)
18807 = tree_cons (NULL_TREE
, decl
,
18808 TREE_VALUE (parser
->unparsed_functions_queues
));
18811 /* Perform the deferred access checks from a template-parameter-list.
18812 CHECKS is a TREE_LIST of access checks, as returned by
18813 get_deferred_access_checks. */
18816 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check
,gc
)* checks
)
18818 ++processing_template_parmlist
;
18819 perform_access_checks (checks
);
18820 --processing_template_parmlist
;
18823 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18824 `function-definition' sequence. MEMBER_P is true, this declaration
18825 appears in a class scope.
18827 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18828 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18831 cp_parser_single_declaration (cp_parser
* parser
,
18832 VEC (deferred_access_check
,gc
)* checks
,
18834 bool explicit_specialization_p
,
18837 int declares_class_or_enum
;
18838 tree decl
= NULL_TREE
;
18839 cp_decl_specifier_seq decl_specifiers
;
18840 bool function_definition_p
= false;
18841 cp_token
*decl_spec_token_start
;
18843 /* This function is only used when processing a template
18845 gcc_assert (innermost_scope_kind () == sk_template_parms
18846 || innermost_scope_kind () == sk_template_spec
);
18848 /* Defer access checks until we know what is being declared. */
18849 push_deferring_access_checks (dk_deferred
);
18851 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18853 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
18854 cp_parser_decl_specifier_seq (parser
,
18855 CP_PARSER_FLAGS_OPTIONAL
,
18857 &declares_class_or_enum
);
18859 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
18861 /* There are no template typedefs. */
18862 if (decl_specifiers
.specs
[(int) ds_typedef
])
18864 error_at (decl_spec_token_start
->location
,
18865 "template declaration of %<typedef%>");
18866 decl
= error_mark_node
;
18869 /* Gather up the access checks that occurred the
18870 decl-specifier-seq. */
18871 stop_deferring_access_checks ();
18873 /* Check for the declaration of a template class. */
18874 if (declares_class_or_enum
)
18876 if (cp_parser_declares_only_class_p (parser
))
18878 decl
= shadow_tag (&decl_specifiers
);
18883 friend template <typename T> struct A<T>::B;
18886 A<T>::B will be represented by a TYPENAME_TYPE, and
18887 therefore not recognized by shadow_tag. */
18888 if (friend_p
&& *friend_p
18890 && decl_specifiers
.type
18891 && TYPE_P (decl_specifiers
.type
))
18892 decl
= decl_specifiers
.type
;
18894 if (decl
&& decl
!= error_mark_node
)
18895 decl
= TYPE_NAME (decl
);
18897 decl
= error_mark_node
;
18899 /* Perform access checks for template parameters. */
18900 cp_parser_perform_template_parameter_access_checks (checks
);
18904 /* Complain about missing 'typename' or other invalid type names. */
18905 if (!decl_specifiers
.any_type_specifiers_p
)
18906 cp_parser_parse_and_diagnose_invalid_type_name (parser
);
18908 /* If it's not a template class, try for a template function. If
18909 the next token is a `;', then this declaration does not declare
18910 anything. But, if there were errors in the decl-specifiers, then
18911 the error might well have come from an attempted class-specifier.
18912 In that case, there's no need to warn about a missing declarator. */
18914 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
18915 || decl_specifiers
.type
!= error_mark_node
))
18917 decl
= cp_parser_init_declarator (parser
,
18920 /*function_definition_allowed_p=*/true,
18922 declares_class_or_enum
,
18923 &function_definition_p
);
18925 /* 7.1.1-1 [dcl.stc]
18927 A storage-class-specifier shall not be specified in an explicit
18928 specialization... */
18930 && explicit_specialization_p
18931 && decl_specifiers
.storage_class
!= sc_none
)
18933 error_at (decl_spec_token_start
->location
,
18934 "explicit template specialization cannot have a storage class");
18935 decl
= error_mark_node
;
18939 pop_deferring_access_checks ();
18941 /* Clear any current qualification; whatever comes next is the start
18942 of something new. */
18943 parser
->scope
= NULL_TREE
;
18944 parser
->qualifying_scope
= NULL_TREE
;
18945 parser
->object_scope
= NULL_TREE
;
18946 /* Look for a trailing `;' after the declaration. */
18947 if (!function_definition_p
18948 && (decl
== error_mark_node
18949 || !cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>")))
18950 cp_parser_skip_to_end_of_block_or_statement (parser
);
18955 /* Parse a cast-expression that is not the operand of a unary "&". */
18958 cp_parser_simple_cast_expression (cp_parser
*parser
)
18960 return cp_parser_cast_expression (parser
, /*address_p=*/false,
18961 /*cast_p=*/false, NULL
);
18964 /* Parse a functional cast to TYPE. Returns an expression
18965 representing the cast. */
18968 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
18971 tree expression_list
;
18975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18977 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
18978 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
18979 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
18980 if (TREE_CODE (type
) == TYPE_DECL
)
18981 type
= TREE_TYPE (type
);
18982 return finish_compound_literal (type
, expression_list
);
18986 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
18988 /*allow_expansion_p=*/true,
18989 /*non_constant_p=*/NULL
);
18991 expression_list
= error_mark_node
;
18994 expression_list
= build_tree_list_vec (vec
);
18995 release_tree_vector (vec
);
18998 cast
= build_functional_cast (type
, expression_list
,
18999 tf_warning_or_error
);
19000 /* [expr.const]/1: In an integral constant expression "only type
19001 conversions to integral or enumeration type can be used". */
19002 if (TREE_CODE (type
) == TYPE_DECL
)
19003 type
= TREE_TYPE (type
);
19004 if (cast
!= error_mark_node
19005 && !cast_valid_in_integral_constant_expression_p (type
)
19006 && (cp_parser_non_integral_constant_expression
19007 (parser
, "a call to a constructor")))
19008 return error_mark_node
;
19012 /* Save the tokens that make up the body of a member function defined
19013 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19014 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19015 specifiers applied to the declaration. Returns the FUNCTION_DECL
19016 for the member function. */
19019 cp_parser_save_member_function_body (cp_parser
* parser
,
19020 cp_decl_specifier_seq
*decl_specifiers
,
19021 cp_declarator
*declarator
,
19028 /* Create the FUNCTION_DECL. */
19029 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
19030 /* If something went badly wrong, bail out now. */
19031 if (fn
== error_mark_node
)
19033 /* If there's a function-body, skip it. */
19034 if (cp_parser_token_starts_function_definition_p
19035 (cp_lexer_peek_token (parser
->lexer
)))
19036 cp_parser_skip_to_end_of_block_or_statement (parser
);
19037 return error_mark_node
;
19040 /* Remember it, if there default args to post process. */
19041 cp_parser_save_default_args (parser
, fn
);
19043 /* Save away the tokens that make up the body of the
19045 first
= parser
->lexer
->next_token
;
19046 /* We can have braced-init-list mem-initializers before the fn body. */
19047 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19049 cp_lexer_consume_token (parser
->lexer
);
19050 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
19051 && cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_TRY
))
19053 /* cache_group will stop after an un-nested { } pair, too. */
19054 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
19057 /* variadic mem-inits have ... after the ')'. */
19058 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19059 cp_lexer_consume_token (parser
->lexer
);
19062 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
19063 /* Handle function try blocks. */
19064 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
19065 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
19066 last
= parser
->lexer
->next_token
;
19068 /* Save away the inline definition; we will process it when the
19069 class is complete. */
19070 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
19071 DECL_PENDING_INLINE_P (fn
) = 1;
19073 /* We need to know that this was defined in the class, so that
19074 friend templates are handled correctly. */
19075 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
19077 /* Add FN to the queue of functions to be parsed later. */
19078 TREE_VALUE (parser
->unparsed_functions_queues
)
19079 = tree_cons (NULL_TREE
, fn
,
19080 TREE_VALUE (parser
->unparsed_functions_queues
));
19085 /* Parse a template-argument-list, as well as the trailing ">" (but
19086 not the opening ">"). See cp_parser_template_argument_list for the
19090 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
19094 tree saved_qualifying_scope
;
19095 tree saved_object_scope
;
19096 bool saved_greater_than_is_operator_p
;
19097 int saved_unevaluated_operand
;
19098 int saved_inhibit_evaluation_warnings
;
19102 When parsing a template-id, the first non-nested `>' is taken as
19103 the end of the template-argument-list rather than a greater-than
19105 saved_greater_than_is_operator_p
19106 = parser
->greater_than_is_operator_p
;
19107 parser
->greater_than_is_operator_p
= false;
19108 /* Parsing the argument list may modify SCOPE, so we save it
19110 saved_scope
= parser
->scope
;
19111 saved_qualifying_scope
= parser
->qualifying_scope
;
19112 saved_object_scope
= parser
->object_scope
;
19113 /* We need to evaluate the template arguments, even though this
19114 template-id may be nested within a "sizeof". */
19115 saved_unevaluated_operand
= cp_unevaluated_operand
;
19116 cp_unevaluated_operand
= 0;
19117 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
19118 c_inhibit_evaluation_warnings
= 0;
19119 /* Parse the template-argument-list itself. */
19120 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
19121 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
19122 arguments
= NULL_TREE
;
19124 arguments
= cp_parser_template_argument_list (parser
);
19125 /* Look for the `>' that ends the template-argument-list. If we find
19126 a '>>' instead, it's probably just a typo. */
19127 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
19129 if (cxx_dialect
!= cxx98
)
19131 /* In C++0x, a `>>' in a template argument list or cast
19132 expression is considered to be two separate `>'
19133 tokens. So, change the current token to a `>', but don't
19134 consume it: it will be consumed later when the outer
19135 template argument list (or cast expression) is parsed.
19136 Note that this replacement of `>' for `>>' is necessary
19137 even if we are parsing tentatively: in the tentative
19138 case, after calling
19139 cp_parser_enclosed_template_argument_list we will always
19140 throw away all of the template arguments and the first
19141 closing `>', either because the template argument list
19142 was erroneous or because we are replacing those tokens
19143 with a CPP_TEMPLATE_ID token. The second `>' (which will
19144 not have been thrown away) is needed either to close an
19145 outer template argument list or to complete a new-style
19147 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19148 token
->type
= CPP_GREATER
;
19150 else if (!saved_greater_than_is_operator_p
)
19152 /* If we're in a nested template argument list, the '>>' has
19153 to be a typo for '> >'. We emit the error message, but we
19154 continue parsing and we push a '>' as next token, so that
19155 the argument list will be parsed correctly. Note that the
19156 global source location is still on the token before the
19157 '>>', so we need to say explicitly where we want it. */
19158 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19159 error_at (token
->location
, "%<>>%> should be %<> >%> "
19160 "within a nested template argument list");
19162 token
->type
= CPP_GREATER
;
19166 /* If this is not a nested template argument list, the '>>'
19167 is a typo for '>'. Emit an error message and continue.
19168 Same deal about the token location, but here we can get it
19169 right by consuming the '>>' before issuing the diagnostic. */
19170 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
19171 error_at (token
->location
,
19172 "spurious %<>>%>, use %<>%> to terminate "
19173 "a template argument list");
19177 cp_parser_skip_to_end_of_template_parameter_list (parser
);
19178 /* The `>' token might be a greater-than operator again now. */
19179 parser
->greater_than_is_operator_p
19180 = saved_greater_than_is_operator_p
;
19181 /* Restore the SAVED_SCOPE. */
19182 parser
->scope
= saved_scope
;
19183 parser
->qualifying_scope
= saved_qualifying_scope
;
19184 parser
->object_scope
= saved_object_scope
;
19185 cp_unevaluated_operand
= saved_unevaluated_operand
;
19186 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
19191 /* MEMBER_FUNCTION is a member function, or a friend. If default
19192 arguments, or the body of the function have not yet been parsed,
19196 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
19198 /* If this member is a template, get the underlying
19200 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
19201 member_function
= DECL_TEMPLATE_RESULT (member_function
);
19203 /* There should not be any class definitions in progress at this
19204 point; the bodies of members are only parsed outside of all class
19206 gcc_assert (parser
->num_classes_being_defined
== 0);
19207 /* While we're parsing the member functions we might encounter more
19208 classes. We want to handle them right away, but we don't want
19209 them getting mixed up with functions that are currently in the
19211 parser
->unparsed_functions_queues
19212 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
19214 /* Make sure that any template parameters are in scope. */
19215 maybe_begin_member_template_processing (member_function
);
19217 /* If the body of the function has not yet been parsed, parse it
19219 if (DECL_PENDING_INLINE_P (member_function
))
19221 tree function_scope
;
19222 cp_token_cache
*tokens
;
19224 /* The function is no longer pending; we are processing it. */
19225 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
19226 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
19227 DECL_PENDING_INLINE_P (member_function
) = 0;
19229 /* If this is a local class, enter the scope of the containing
19231 function_scope
= current_function_decl
;
19232 if (function_scope
)
19233 push_function_context ();
19235 /* Push the body of the function onto the lexer stack. */
19236 cp_parser_push_lexer_for_tokens (parser
, tokens
);
19238 /* Let the front end know that we going to be defining this
19240 start_preparsed_function (member_function
, NULL_TREE
,
19241 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
19243 /* Don't do access checking if it is a templated function. */
19244 if (processing_template_decl
)
19245 push_deferring_access_checks (dk_no_check
);
19247 /* Now, parse the body of the function. */
19248 cp_parser_function_definition_after_declarator (parser
,
19249 /*inline_p=*/true);
19251 if (processing_template_decl
)
19252 pop_deferring_access_checks ();
19254 /* Leave the scope of the containing function. */
19255 if (function_scope
)
19256 pop_function_context ();
19257 cp_parser_pop_lexer (parser
);
19260 /* Remove any template parameters from the symbol table. */
19261 maybe_end_member_template_processing ();
19263 /* Restore the queue. */
19264 parser
->unparsed_functions_queues
19265 = TREE_CHAIN (parser
->unparsed_functions_queues
);
19268 /* If DECL contains any default args, remember it on the unparsed
19269 functions queue. */
19272 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
19276 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
19278 probe
= TREE_CHAIN (probe
))
19279 if (TREE_PURPOSE (probe
))
19281 TREE_PURPOSE (parser
->unparsed_functions_queues
)
19282 = tree_cons (current_class_type
, decl
,
19283 TREE_PURPOSE (parser
->unparsed_functions_queues
));
19288 /* FN is a FUNCTION_DECL which may contains a parameter with an
19289 unparsed DEFAULT_ARG. Parse the default args now. This function
19290 assumes that the current scope is the scope in which the default
19291 argument should be processed. */
19294 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
19296 bool saved_local_variables_forbidden_p
;
19297 tree parm
, parmdecl
;
19299 /* While we're parsing the default args, we might (due to the
19300 statement expression extension) encounter more classes. We want
19301 to handle them right away, but we don't want them getting mixed
19302 up with default args that are currently in the queue. */
19303 parser
->unparsed_functions_queues
19304 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
19306 /* Local variable names (and the `this' keyword) may not appear
19307 in a default argument. */
19308 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
19309 parser
->local_variables_forbidden_p
= true;
19311 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
19312 parmdecl
= DECL_ARGUMENTS (fn
);
19313 parm
&& parm
!= void_list_node
;
19314 parm
= TREE_CHAIN (parm
),
19315 parmdecl
= TREE_CHAIN (parmdecl
))
19317 cp_token_cache
*tokens
;
19318 tree default_arg
= TREE_PURPOSE (parm
);
19320 VEC(tree
,gc
) *insts
;
19327 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
19328 /* This can happen for a friend declaration for a function
19329 already declared with default arguments. */
19332 /* Push the saved tokens for the default argument onto the parser's
19334 tokens
= DEFARG_TOKENS (default_arg
);
19335 cp_parser_push_lexer_for_tokens (parser
, tokens
);
19337 start_lambda_scope (parmdecl
);
19339 /* Parse the assignment-expression. */
19340 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
19341 if (parsed_arg
== error_mark_node
)
19343 cp_parser_pop_lexer (parser
);
19347 if (!processing_template_decl
)
19348 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
19350 TREE_PURPOSE (parm
) = parsed_arg
;
19352 /* Update any instantiations we've already created. */
19353 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
19354 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
19355 TREE_PURPOSE (copy
) = parsed_arg
;
19357 finish_lambda_scope ();
19359 /* If the token stream has not been completely used up, then
19360 there was extra junk after the end of the default
19362 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
19363 cp_parser_error (parser
, "expected %<,%>");
19365 /* Revert to the main lexer. */
19366 cp_parser_pop_lexer (parser
);
19369 /* Make sure no default arg is missing. */
19370 check_default_args (fn
);
19372 /* Restore the state of local_variables_forbidden_p. */
19373 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
19375 /* Restore the queue. */
19376 parser
->unparsed_functions_queues
19377 = TREE_CHAIN (parser
->unparsed_functions_queues
);
19380 /* Parse the operand of `sizeof' (or a similar operator). Returns
19381 either a TYPE or an expression, depending on the form of the
19382 input. The KEYWORD indicates which kind of expression we have
19386 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
19388 tree expr
= NULL_TREE
;
19389 const char *saved_message
;
19391 bool saved_integral_constant_expression_p
;
19392 bool saved_non_integral_constant_expression_p
;
19393 bool pack_expansion_p
= false;
19395 /* Types cannot be defined in a `sizeof' expression. Save away the
19397 saved_message
= parser
->type_definition_forbidden_message
;
19398 /* And create the new one. */
19399 tmp
= concat ("types may not be defined in %<",
19400 IDENTIFIER_POINTER (ridpointers
[keyword
]),
19401 "%> expressions", NULL
);
19402 parser
->type_definition_forbidden_message
= tmp
;
19404 /* The restrictions on constant-expressions do not apply inside
19405 sizeof expressions. */
19406 saved_integral_constant_expression_p
19407 = parser
->integral_constant_expression_p
;
19408 saved_non_integral_constant_expression_p
19409 = parser
->non_integral_constant_expression_p
;
19410 parser
->integral_constant_expression_p
= false;
19412 /* If it's a `...', then we are computing the length of a parameter
19414 if (keyword
== RID_SIZEOF
19415 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19417 /* Consume the `...'. */
19418 cp_lexer_consume_token (parser
->lexer
);
19419 maybe_warn_variadic_templates ();
19421 /* Note that this is an expansion. */
19422 pack_expansion_p
= true;
19425 /* Do not actually evaluate the expression. */
19426 ++cp_unevaluated_operand
;
19427 ++c_inhibit_evaluation_warnings
;
19428 /* If it's a `(', then we might be looking at the type-id
19430 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
19433 bool saved_in_type_id_in_expr_p
;
19435 /* We can't be sure yet whether we're looking at a type-id or an
19437 cp_parser_parse_tentatively (parser
);
19438 /* Consume the `('. */
19439 cp_lexer_consume_token (parser
->lexer
);
19440 /* Parse the type-id. */
19441 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
19442 parser
->in_type_id_in_expr_p
= true;
19443 type
= cp_parser_type_id (parser
);
19444 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
19445 /* Now, look for the trailing `)'. */
19446 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
19447 /* If all went well, then we're done. */
19448 if (cp_parser_parse_definitely (parser
))
19450 cp_decl_specifier_seq decl_specs
;
19452 /* Build a trivial decl-specifier-seq. */
19453 clear_decl_specs (&decl_specs
);
19454 decl_specs
.type
= type
;
19456 /* Call grokdeclarator to figure out what type this is. */
19457 expr
= grokdeclarator (NULL
,
19461 /*attrlist=*/NULL
);
19465 /* If the type-id production did not work out, then we must be
19466 looking at the unary-expression production. */
19468 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
19469 /*cast_p=*/false, NULL
);
19471 if (pack_expansion_p
)
19472 /* Build a pack expansion. */
19473 expr
= make_pack_expansion (expr
);
19475 /* Go back to evaluating expressions. */
19476 --cp_unevaluated_operand
;
19477 --c_inhibit_evaluation_warnings
;
19479 /* Free the message we created. */
19481 /* And restore the old one. */
19482 parser
->type_definition_forbidden_message
= saved_message
;
19483 parser
->integral_constant_expression_p
19484 = saved_integral_constant_expression_p
;
19485 parser
->non_integral_constant_expression_p
19486 = saved_non_integral_constant_expression_p
;
19491 /* If the current declaration has no declarator, return true. */
19494 cp_parser_declares_only_class_p (cp_parser
*parser
)
19496 /* If the next token is a `;' or a `,' then there is no
19498 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
19499 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
19502 /* Update the DECL_SPECS to reflect the storage class indicated by
19506 cp_parser_set_storage_class (cp_parser
*parser
,
19507 cp_decl_specifier_seq
*decl_specs
,
19509 location_t location
)
19511 cp_storage_class storage_class
;
19513 if (parser
->in_unbraced_linkage_specification_p
)
19515 error_at (location
, "invalid use of %qD in linkage specification",
19516 ridpointers
[keyword
]);
19519 else if (decl_specs
->storage_class
!= sc_none
)
19521 decl_specs
->conflicting_specifiers_p
= true;
19525 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
19526 && decl_specs
->specs
[(int) ds_thread
])
19528 error_at (location
, "%<__thread%> before %qD", ridpointers
[keyword
]);
19529 decl_specs
->specs
[(int) ds_thread
] = 0;
19535 storage_class
= sc_auto
;
19538 storage_class
= sc_register
;
19541 storage_class
= sc_static
;
19544 storage_class
= sc_extern
;
19547 storage_class
= sc_mutable
;
19550 gcc_unreachable ();
19552 decl_specs
->storage_class
= storage_class
;
19554 /* A storage class specifier cannot be applied alongside a typedef
19555 specifier. If there is a typedef specifier present then set
19556 conflicting_specifiers_p which will trigger an error later
19557 on in grokdeclarator. */
19558 if (decl_specs
->specs
[(int)ds_typedef
])
19559 decl_specs
->conflicting_specifiers_p
= true;
19562 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19563 is true, the type is a user-defined type; otherwise it is a
19564 built-in type specified by a keyword. */
19567 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
19569 location_t location
,
19570 bool user_defined_p
)
19572 decl_specs
->any_specifiers_p
= true;
19574 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19575 (with, for example, in "typedef int wchar_t;") we remember that
19576 this is what happened. In system headers, we ignore these
19577 declarations so that G++ can work with system headers that are not
19579 if (decl_specs
->specs
[(int) ds_typedef
]
19581 && (type_spec
== boolean_type_node
19582 || type_spec
== char16_type_node
19583 || type_spec
== char32_type_node
19584 || type_spec
== wchar_type_node
)
19585 && (decl_specs
->type
19586 || decl_specs
->specs
[(int) ds_long
]
19587 || decl_specs
->specs
[(int) ds_short
]
19588 || decl_specs
->specs
[(int) ds_unsigned
]
19589 || decl_specs
->specs
[(int) ds_signed
]))
19591 decl_specs
->redefined_builtin_type
= type_spec
;
19592 if (!decl_specs
->type
)
19594 decl_specs
->type
= type_spec
;
19595 decl_specs
->user_defined_type_p
= false;
19596 decl_specs
->type_location
= location
;
19599 else if (decl_specs
->type
)
19600 decl_specs
->multiple_types_p
= true;
19603 decl_specs
->type
= type_spec
;
19604 decl_specs
->user_defined_type_p
= user_defined_p
;
19605 decl_specs
->redefined_builtin_type
= NULL_TREE
;
19606 decl_specs
->type_location
= location
;
19610 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19611 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19614 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
19616 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
19619 /* If the next token is of the indicated TYPE, consume it. Otherwise,
19620 issue an error message indicating that TOKEN_DESC was expected.
19622 Returns the token consumed, if the token had the appropriate type.
19623 Otherwise, returns NULL. */
19626 cp_parser_require (cp_parser
* parser
,
19627 enum cpp_ttype type
,
19628 const char* token_desc
)
19630 if (cp_lexer_next_token_is (parser
->lexer
, type
))
19631 return cp_lexer_consume_token (parser
->lexer
);
19634 /* Output the MESSAGE -- unless we're parsing tentatively. */
19635 if (!cp_parser_simulate_error (parser
))
19637 char *message
= concat ("expected ", token_desc
, NULL
);
19638 cp_parser_error (parser
, message
);
19645 /* An error message is produced if the next token is not '>'.
19646 All further tokens are skipped until the desired token is
19647 found or '{', '}', ';' or an unbalanced ')' or ']'. */
19650 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
19652 /* Current level of '< ... >'. */
19653 unsigned level
= 0;
19654 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
19655 unsigned nesting_depth
= 0;
19657 /* Are we ready, yet? If not, issue error message. */
19658 if (cp_parser_require (parser
, CPP_GREATER
, "%<>%>"))
19661 /* Skip tokens until the desired token is found. */
19664 /* Peek at the next token. */
19665 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
19668 if (!nesting_depth
)
19673 if (cxx_dialect
== cxx98
)
19674 /* C++0x views the `>>' operator as two `>' tokens, but
19677 else if (!nesting_depth
&& level
-- == 0)
19679 /* We've hit a `>>' where the first `>' closes the
19680 template argument list, and the second `>' is
19681 spurious. Just consume the `>>' and stop; we've
19682 already produced at least one error. */
19683 cp_lexer_consume_token (parser
->lexer
);
19686 /* Fall through for C++0x, so we handle the second `>' in
19690 if (!nesting_depth
&& level
-- == 0)
19692 /* We've reached the token we want, consume it and stop. */
19693 cp_lexer_consume_token (parser
->lexer
);
19698 case CPP_OPEN_PAREN
:
19699 case CPP_OPEN_SQUARE
:
19703 case CPP_CLOSE_PAREN
:
19704 case CPP_CLOSE_SQUARE
:
19705 if (nesting_depth
-- == 0)
19710 case CPP_PRAGMA_EOL
:
19711 case CPP_SEMICOLON
:
19712 case CPP_OPEN_BRACE
:
19713 case CPP_CLOSE_BRACE
:
19714 /* The '>' was probably forgotten, don't look further. */
19721 /* Consume this token. */
19722 cp_lexer_consume_token (parser
->lexer
);
19726 /* If the next token is the indicated keyword, consume it. Otherwise,
19727 issue an error message indicating that TOKEN_DESC was expected.
19729 Returns the token consumed, if the token had the appropriate type.
19730 Otherwise, returns NULL. */
19733 cp_parser_require_keyword (cp_parser
* parser
,
19735 const char* token_desc
)
19737 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
19739 if (token
&& token
->keyword
!= keyword
)
19741 dyn_string_t error_msg
;
19743 /* Format the error message. */
19744 error_msg
= dyn_string_new (0);
19745 dyn_string_append_cstr (error_msg
, "expected ");
19746 dyn_string_append_cstr (error_msg
, token_desc
);
19747 cp_parser_error (parser
, error_msg
->s
);
19748 dyn_string_delete (error_msg
);
19755 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19756 function-definition. */
19759 cp_parser_token_starts_function_definition_p (cp_token
* token
)
19761 return (/* An ordinary function-body begins with an `{'. */
19762 token
->type
== CPP_OPEN_BRACE
19763 /* A ctor-initializer begins with a `:'. */
19764 || token
->type
== CPP_COLON
19765 /* A function-try-block begins with `try'. */
19766 || token
->keyword
== RID_TRY
19767 /* The named return value extension begins with `return'. */
19768 || token
->keyword
== RID_RETURN
);
19771 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19775 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
19779 token
= cp_lexer_peek_token (parser
->lexer
);
19780 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
19783 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19784 C++0x) ending a template-argument. */
19787 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
19791 token
= cp_lexer_peek_token (parser
->lexer
);
19792 return (token
->type
== CPP_COMMA
19793 || token
->type
== CPP_GREATER
19794 || token
->type
== CPP_ELLIPSIS
19795 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
19798 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19799 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19802 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
19807 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
19808 if (token
->type
== CPP_LESS
)
19810 /* Check for the sequence `<::' in the original code. It would be lexed as
19811 `[:', where `[' is a digraph, and there is no whitespace before
19813 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
19816 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
19817 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
19823 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19824 or none_type otherwise. */
19826 static enum tag_types
19827 cp_parser_token_is_class_key (cp_token
* token
)
19829 switch (token
->keyword
)
19834 return record_type
;
19843 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19846 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
19848 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
19849 permerror (input_location
, "%qs tag used in naming %q#T",
19850 class_key
== union_type
? "union"
19851 : class_key
== record_type
? "struct" : "class",
19855 /* Issue an error message if DECL is redeclared with different
19856 access than its original declaration [class.access.spec/3].
19857 This applies to nested classes and nested class templates.
19861 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
19863 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
19866 if ((TREE_PRIVATE (decl
)
19867 != (current_access_specifier
== access_private_node
))
19868 || (TREE_PROTECTED (decl
)
19869 != (current_access_specifier
== access_protected_node
)))
19870 error_at (location
, "%qD redeclared with different access", decl
);
19873 /* Look for the `template' keyword, as a syntactic disambiguator.
19874 Return TRUE iff it is present, in which case it will be
19878 cp_parser_optional_template_keyword (cp_parser
*parser
)
19880 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
19882 /* The `template' keyword can only be used within templates;
19883 outside templates the parser can always figure out what is a
19884 template and what is not. */
19885 if (!processing_template_decl
)
19887 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19888 error_at (token
->location
,
19889 "%<template%> (as a disambiguator) is only allowed "
19890 "within templates");
19891 /* If this part of the token stream is rescanned, the same
19892 error message would be generated. So, we purge the token
19893 from the stream. */
19894 cp_lexer_purge_token (parser
->lexer
);
19899 /* Consume the `template' keyword. */
19900 cp_lexer_consume_token (parser
->lexer
);
19908 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19909 set PARSER->SCOPE, and perform other related actions. */
19912 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
19915 struct tree_check
*check_value
;
19916 deferred_access_check
*chk
;
19917 VEC (deferred_access_check
,gc
) *checks
;
19919 /* Get the stored value. */
19920 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
19921 /* Perform any access checks that were deferred. */
19922 checks
= check_value
->checks
;
19926 VEC_iterate (deferred_access_check
, checks
, i
, chk
) ;
19929 perform_or_defer_access_check (chk
->binfo
,
19934 /* Set the scope from the stored value. */
19935 parser
->scope
= check_value
->value
;
19936 parser
->qualifying_scope
= check_value
->qualifying_scope
;
19937 parser
->object_scope
= NULL_TREE
;
19940 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19941 encounter the end of a block before what we were looking for. */
19944 cp_parser_cache_group (cp_parser
*parser
,
19945 enum cpp_ttype end
,
19950 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19952 /* Abort a parenthesized expression if we encounter a semicolon. */
19953 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
19954 && token
->type
== CPP_SEMICOLON
)
19956 /* If we've reached the end of the file, stop. */
19957 if (token
->type
== CPP_EOF
19958 || (end
!= CPP_PRAGMA_EOL
19959 && token
->type
== CPP_PRAGMA_EOL
))
19961 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
19962 /* We've hit the end of an enclosing block, so there's been some
19963 kind of syntax error. */
19966 /* Consume the token. */
19967 cp_lexer_consume_token (parser
->lexer
);
19968 /* See if it starts a new group. */
19969 if (token
->type
== CPP_OPEN_BRACE
)
19971 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
19972 /* In theory this should probably check end == '}', but
19973 cp_parser_save_member_function_body needs it to exit
19974 after either '}' or ')' when called with ')'. */
19978 else if (token
->type
== CPP_OPEN_PAREN
)
19980 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
19981 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
19984 else if (token
->type
== CPP_PRAGMA
)
19985 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
19986 else if (token
->type
== end
)
19991 /* Begin parsing tentatively. We always save tokens while parsing
19992 tentatively so that if the tentative parsing fails we can restore the
19996 cp_parser_parse_tentatively (cp_parser
* parser
)
19998 /* Enter a new parsing context. */
19999 parser
->context
= cp_parser_context_new (parser
->context
);
20000 /* Begin saving tokens. */
20001 cp_lexer_save_tokens (parser
->lexer
);
20002 /* In order to avoid repetitive access control error messages,
20003 access checks are queued up until we are no longer parsing
20005 push_deferring_access_checks (dk_deferred
);
20008 /* Commit to the currently active tentative parse. */
20011 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
20013 cp_parser_context
*context
;
20016 /* Mark all of the levels as committed. */
20017 lexer
= parser
->lexer
;
20018 for (context
= parser
->context
; context
->next
; context
= context
->next
)
20020 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
20022 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
20023 while (!cp_lexer_saving_tokens (lexer
))
20024 lexer
= lexer
->next
;
20025 cp_lexer_commit_tokens (lexer
);
20029 /* Abort the currently active tentative parse. All consumed tokens
20030 will be rolled back, and no diagnostics will be issued. */
20033 cp_parser_abort_tentative_parse (cp_parser
* parser
)
20035 cp_parser_simulate_error (parser
);
20036 /* Now, pretend that we want to see if the construct was
20037 successfully parsed. */
20038 cp_parser_parse_definitely (parser
);
20041 /* Stop parsing tentatively. If a parse error has occurred, restore the
20042 token stream. Otherwise, commit to the tokens we have consumed.
20043 Returns true if no error occurred; false otherwise. */
20046 cp_parser_parse_definitely (cp_parser
* parser
)
20048 bool error_occurred
;
20049 cp_parser_context
*context
;
20051 /* Remember whether or not an error occurred, since we are about to
20052 destroy that information. */
20053 error_occurred
= cp_parser_error_occurred (parser
);
20054 /* Remove the topmost context from the stack. */
20055 context
= parser
->context
;
20056 parser
->context
= context
->next
;
20057 /* If no parse errors occurred, commit to the tentative parse. */
20058 if (!error_occurred
)
20060 /* Commit to the tokens read tentatively, unless that was
20062 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
20063 cp_lexer_commit_tokens (parser
->lexer
);
20065 pop_to_parent_deferring_access_checks ();
20067 /* Otherwise, if errors occurred, roll back our state so that things
20068 are just as they were before we began the tentative parse. */
20071 cp_lexer_rollback_tokens (parser
->lexer
);
20072 pop_deferring_access_checks ();
20074 /* Add the context to the front of the free list. */
20075 context
->next
= cp_parser_context_free_list
;
20076 cp_parser_context_free_list
= context
;
20078 return !error_occurred
;
20081 /* Returns true if we are parsing tentatively and are not committed to
20082 this tentative parse. */
20085 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
20087 return (cp_parser_parsing_tentatively (parser
)
20088 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
20091 /* Returns nonzero iff an error has occurred during the most recent
20092 tentative parse. */
20095 cp_parser_error_occurred (cp_parser
* parser
)
20097 return (cp_parser_parsing_tentatively (parser
)
20098 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
20101 /* Returns nonzero if GNU extensions are allowed. */
20104 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
20106 return parser
->allow_gnu_extensions_p
;
20109 /* Objective-C++ Productions */
20112 /* Parse an Objective-C expression, which feeds into a primary-expression
20116 objc-message-expression
20117 objc-string-literal
20118 objc-encode-expression
20119 objc-protocol-expression
20120 objc-selector-expression
20122 Returns a tree representation of the expression. */
20125 cp_parser_objc_expression (cp_parser
* parser
)
20127 /* Try to figure out what kind of declaration is present. */
20128 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
20132 case CPP_OPEN_SQUARE
:
20133 return cp_parser_objc_message_expression (parser
);
20135 case CPP_OBJC_STRING
:
20136 kwd
= cp_lexer_consume_token (parser
->lexer
);
20137 return objc_build_string_object (kwd
->u
.value
);
20140 switch (kwd
->keyword
)
20142 case RID_AT_ENCODE
:
20143 return cp_parser_objc_encode_expression (parser
);
20145 case RID_AT_PROTOCOL
:
20146 return cp_parser_objc_protocol_expression (parser
);
20148 case RID_AT_SELECTOR
:
20149 return cp_parser_objc_selector_expression (parser
);
20155 error_at (kwd
->location
,
20156 "misplaced %<@%D%> Objective-C++ construct",
20158 cp_parser_skip_to_end_of_block_or_statement (parser
);
20161 return error_mark_node
;
20164 /* Parse an Objective-C message expression.
20166 objc-message-expression:
20167 [ objc-message-receiver objc-message-args ]
20169 Returns a representation of an Objective-C message. */
20172 cp_parser_objc_message_expression (cp_parser
* parser
)
20174 tree receiver
, messageargs
;
20176 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
20177 receiver
= cp_parser_objc_message_receiver (parser
);
20178 messageargs
= cp_parser_objc_message_args (parser
);
20179 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "%<]%>");
20181 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
20184 /* Parse an objc-message-receiver.
20186 objc-message-receiver:
20188 simple-type-specifier
20190 Returns a representation of the type or expression. */
20193 cp_parser_objc_message_receiver (cp_parser
* parser
)
20197 /* An Objective-C message receiver may be either (1) a type
20198 or (2) an expression. */
20199 cp_parser_parse_tentatively (parser
);
20200 rcv
= cp_parser_expression (parser
, false, NULL
);
20202 if (cp_parser_parse_definitely (parser
))
20205 rcv
= cp_parser_simple_type_specifier (parser
,
20206 /*decl_specs=*/NULL
,
20207 CP_PARSER_FLAGS_NONE
);
20209 return objc_get_class_reference (rcv
);
20212 /* Parse the arguments and selectors comprising an Objective-C message.
20217 objc-selector-args , objc-comma-args
20219 objc-selector-args:
20220 objc-selector [opt] : assignment-expression
20221 objc-selector-args objc-selector [opt] : assignment-expression
20224 assignment-expression
20225 objc-comma-args , assignment-expression
20227 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20228 selector arguments and TREE_VALUE containing a list of comma
20232 cp_parser_objc_message_args (cp_parser
* parser
)
20234 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
20235 bool maybe_unary_selector_p
= true;
20236 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20238 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
20240 tree selector
= NULL_TREE
, arg
;
20242 if (token
->type
!= CPP_COLON
)
20243 selector
= cp_parser_objc_selector (parser
);
20245 /* Detect if we have a unary selector. */
20246 if (maybe_unary_selector_p
20247 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
20248 return build_tree_list (selector
, NULL_TREE
);
20250 maybe_unary_selector_p
= false;
20251 cp_parser_require (parser
, CPP_COLON
, "%<:%>");
20252 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
20255 = chainon (sel_args
,
20256 build_tree_list (selector
, arg
));
20258 token
= cp_lexer_peek_token (parser
->lexer
);
20261 /* Handle non-selector arguments, if any. */
20262 while (token
->type
== CPP_COMMA
)
20266 cp_lexer_consume_token (parser
->lexer
);
20267 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
20270 = chainon (addl_args
,
20271 build_tree_list (NULL_TREE
, arg
));
20273 token
= cp_lexer_peek_token (parser
->lexer
);
20276 return build_tree_list (sel_args
, addl_args
);
20279 /* Parse an Objective-C encode expression.
20281 objc-encode-expression:
20282 @encode objc-typename
20284 Returns an encoded representation of the type argument. */
20287 cp_parser_objc_encode_expression (cp_parser
* parser
)
20292 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
20293 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
20294 token
= cp_lexer_peek_token (parser
->lexer
);
20295 type
= complete_type (cp_parser_type_id (parser
));
20296 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
20300 error_at (token
->location
,
20301 "%<@encode%> must specify a type as an argument");
20302 return error_mark_node
;
20305 return objc_build_encode_expr (type
);
20308 /* Parse an Objective-C @defs expression. */
20311 cp_parser_objc_defs_expression (cp_parser
*parser
)
20315 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
20316 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
20317 name
= cp_parser_identifier (parser
);
20318 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
20320 return objc_get_class_ivars (name
);
20323 /* Parse an Objective-C protocol expression.
20325 objc-protocol-expression:
20326 @protocol ( identifier )
20328 Returns a representation of the protocol expression. */
20331 cp_parser_objc_protocol_expression (cp_parser
* parser
)
20335 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
20336 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
20337 proto
= cp_parser_identifier (parser
);
20338 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
20340 return objc_build_protocol_expr (proto
);
20343 /* Parse an Objective-C selector expression.
20345 objc-selector-expression:
20346 @selector ( objc-method-signature )
20348 objc-method-signature:
20354 objc-selector-seq objc-selector :
20356 Returns a representation of the method selector. */
20359 cp_parser_objc_selector_expression (cp_parser
* parser
)
20361 tree sel_seq
= NULL_TREE
;
20362 bool maybe_unary_selector_p
= true;
20364 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
20366 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
20367 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
20368 token
= cp_lexer_peek_token (parser
->lexer
);
20370 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
20371 || token
->type
== CPP_SCOPE
)
20373 tree selector
= NULL_TREE
;
20375 if (token
->type
!= CPP_COLON
20376 || token
->type
== CPP_SCOPE
)
20377 selector
= cp_parser_objc_selector (parser
);
20379 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
20380 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
20382 /* Detect if we have a unary selector. */
20383 if (maybe_unary_selector_p
)
20385 sel_seq
= selector
;
20386 goto finish_selector
;
20390 cp_parser_error (parser
, "expected %<:%>");
20393 maybe_unary_selector_p
= false;
20394 token
= cp_lexer_consume_token (parser
->lexer
);
20396 if (token
->type
== CPP_SCOPE
)
20399 = chainon (sel_seq
,
20400 build_tree_list (selector
, NULL_TREE
));
20402 = chainon (sel_seq
,
20403 build_tree_list (NULL_TREE
, NULL_TREE
));
20407 = chainon (sel_seq
,
20408 build_tree_list (selector
, NULL_TREE
));
20410 token
= cp_lexer_peek_token (parser
->lexer
);
20414 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
20416 return objc_build_selector_expr (loc
, sel_seq
);
20419 /* Parse a list of identifiers.
20421 objc-identifier-list:
20423 objc-identifier-list , identifier
20425 Returns a TREE_LIST of identifier nodes. */
20428 cp_parser_objc_identifier_list (cp_parser
* parser
)
20430 tree list
= build_tree_list (NULL_TREE
, cp_parser_identifier (parser
));
20431 cp_token
*sep
= cp_lexer_peek_token (parser
->lexer
);
20433 while (sep
->type
== CPP_COMMA
)
20435 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
20436 list
= chainon (list
,
20437 build_tree_list (NULL_TREE
,
20438 cp_parser_identifier (parser
)));
20439 sep
= cp_lexer_peek_token (parser
->lexer
);
20445 /* Parse an Objective-C alias declaration.
20447 objc-alias-declaration:
20448 @compatibility_alias identifier identifier ;
20450 This function registers the alias mapping with the Objective-C front end.
20451 It returns nothing. */
20454 cp_parser_objc_alias_declaration (cp_parser
* parser
)
20458 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
20459 alias
= cp_parser_identifier (parser
);
20460 orig
= cp_parser_identifier (parser
);
20461 objc_declare_alias (alias
, orig
);
20462 cp_parser_consume_semicolon_at_end_of_statement (parser
);
20465 /* Parse an Objective-C class forward-declaration.
20467 objc-class-declaration:
20468 @class objc-identifier-list ;
20470 The function registers the forward declarations with the Objective-C
20471 front end. It returns nothing. */
20474 cp_parser_objc_class_declaration (cp_parser
* parser
)
20476 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
20477 objc_declare_class (cp_parser_objc_identifier_list (parser
));
20478 cp_parser_consume_semicolon_at_end_of_statement (parser
);
20481 /* Parse a list of Objective-C protocol references.
20483 objc-protocol-refs-opt:
20484 objc-protocol-refs [opt]
20486 objc-protocol-refs:
20487 < objc-identifier-list >
20489 Returns a TREE_LIST of identifiers, if any. */
20492 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
20494 tree protorefs
= NULL_TREE
;
20496 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
20498 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
20499 protorefs
= cp_parser_objc_identifier_list (parser
);
20500 cp_parser_require (parser
, CPP_GREATER
, "%<>%>");
20506 /* Parse a Objective-C visibility specification. */
20509 cp_parser_objc_visibility_spec (cp_parser
* parser
)
20511 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
20513 switch (vis
->keyword
)
20515 case RID_AT_PRIVATE
:
20516 objc_set_visibility (2);
20518 case RID_AT_PROTECTED
:
20519 objc_set_visibility (0);
20521 case RID_AT_PUBLIC
:
20522 objc_set_visibility (1);
20528 /* Eat '@private'/'@protected'/'@public'. */
20529 cp_lexer_consume_token (parser
->lexer
);
20532 /* Parse an Objective-C method type. */
20535 cp_parser_objc_method_type (cp_parser
* parser
)
20537 objc_set_method_type
20538 (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
20543 /* Parse an Objective-C protocol qualifier. */
20546 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
20548 tree quals
= NULL_TREE
, node
;
20549 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20551 node
= token
->u
.value
;
20553 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
20554 && (node
== ridpointers
[(int) RID_IN
]
20555 || node
== ridpointers
[(int) RID_OUT
]
20556 || node
== ridpointers
[(int) RID_INOUT
]
20557 || node
== ridpointers
[(int) RID_BYCOPY
]
20558 || node
== ridpointers
[(int) RID_BYREF
]
20559 || node
== ridpointers
[(int) RID_ONEWAY
]))
20561 quals
= tree_cons (NULL_TREE
, node
, quals
);
20562 cp_lexer_consume_token (parser
->lexer
);
20563 token
= cp_lexer_peek_token (parser
->lexer
);
20564 node
= token
->u
.value
;
20570 /* Parse an Objective-C typename. */
20573 cp_parser_objc_typename (cp_parser
* parser
)
20575 tree type_name
= NULL_TREE
;
20577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
20579 tree proto_quals
, cp_type
= NULL_TREE
;
20581 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
20582 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
20584 /* An ObjC type name may consist of just protocol qualifiers, in which
20585 case the type shall default to 'id'. */
20586 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
20587 cp_type
= cp_parser_type_id (parser
);
20589 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
20590 type_name
= build_tree_list (proto_quals
, cp_type
);
20596 /* Check to see if TYPE refers to an Objective-C selector name. */
20599 cp_parser_objc_selector_p (enum cpp_ttype type
)
20601 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
20602 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
20603 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
20604 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
20605 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
20608 /* Parse an Objective-C selector. */
20611 cp_parser_objc_selector (cp_parser
* parser
)
20613 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
20615 if (!cp_parser_objc_selector_p (token
->type
))
20617 error_at (token
->location
, "invalid Objective-C++ selector name");
20618 return error_mark_node
;
20621 /* C++ operator names are allowed to appear in ObjC selectors. */
20622 switch (token
->type
)
20624 case CPP_AND_AND
: return get_identifier ("and");
20625 case CPP_AND_EQ
: return get_identifier ("and_eq");
20626 case CPP_AND
: return get_identifier ("bitand");
20627 case CPP_OR
: return get_identifier ("bitor");
20628 case CPP_COMPL
: return get_identifier ("compl");
20629 case CPP_NOT
: return get_identifier ("not");
20630 case CPP_NOT_EQ
: return get_identifier ("not_eq");
20631 case CPP_OR_OR
: return get_identifier ("or");
20632 case CPP_OR_EQ
: return get_identifier ("or_eq");
20633 case CPP_XOR
: return get_identifier ("xor");
20634 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
20635 default: return token
->u
.value
;
20639 /* Parse an Objective-C params list. */
20642 cp_parser_objc_method_keyword_params (cp_parser
* parser
)
20644 tree params
= NULL_TREE
;
20645 bool maybe_unary_selector_p
= true;
20646 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20648 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
20650 tree selector
= NULL_TREE
, type_name
, identifier
;
20652 if (token
->type
!= CPP_COLON
)
20653 selector
= cp_parser_objc_selector (parser
);
20655 /* Detect if we have a unary selector. */
20656 if (maybe_unary_selector_p
20657 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
20660 maybe_unary_selector_p
= false;
20661 cp_parser_require (parser
, CPP_COLON
, "%<:%>");
20662 type_name
= cp_parser_objc_typename (parser
);
20663 identifier
= cp_parser_identifier (parser
);
20667 objc_build_keyword_decl (selector
,
20671 token
= cp_lexer_peek_token (parser
->lexer
);
20677 /* Parse the non-keyword Objective-C params. */
20680 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
)
20682 tree params
= make_node (TREE_LIST
);
20683 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20684 *ellipsisp
= false; /* Initially, assume no ellipsis. */
20686 while (token
->type
== CPP_COMMA
)
20688 cp_parameter_declarator
*parmdecl
;
20691 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
20692 token
= cp_lexer_peek_token (parser
->lexer
);
20694 if (token
->type
== CPP_ELLIPSIS
)
20696 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
20701 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
20702 parm
= grokdeclarator (parmdecl
->declarator
,
20703 &parmdecl
->decl_specifiers
,
20704 PARM
, /*initialized=*/0,
20705 /*attrlist=*/NULL
);
20707 chainon (params
, build_tree_list (NULL_TREE
, parm
));
20708 token
= cp_lexer_peek_token (parser
->lexer
);
20714 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
20717 cp_parser_objc_interstitial_code (cp_parser
* parser
)
20719 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20721 /* If the next token is `extern' and the following token is a string
20722 literal, then we have a linkage specification. */
20723 if (token
->keyword
== RID_EXTERN
20724 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
20725 cp_parser_linkage_specification (parser
);
20726 /* Handle #pragma, if any. */
20727 else if (token
->type
== CPP_PRAGMA
)
20728 cp_parser_pragma (parser
, pragma_external
);
20729 /* Allow stray semicolons. */
20730 else if (token
->type
== CPP_SEMICOLON
)
20731 cp_lexer_consume_token (parser
->lexer
);
20732 /* Finally, try to parse a block-declaration, or a function-definition. */
20734 cp_parser_block_declaration (parser
, /*statement_p=*/false);
20737 /* Parse a method signature. */
20740 cp_parser_objc_method_signature (cp_parser
* parser
)
20742 tree rettype
, kwdparms
, optparms
;
20743 bool ellipsis
= false;
20745 cp_parser_objc_method_type (parser
);
20746 rettype
= cp_parser_objc_typename (parser
);
20747 kwdparms
= cp_parser_objc_method_keyword_params (parser
);
20748 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
);
20750 return objc_build_method_signature (rettype
, kwdparms
, optparms
, ellipsis
);
20753 /* Pars an Objective-C method prototype list. */
20756 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
20758 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20760 while (token
->keyword
!= RID_AT_END
)
20762 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
20764 objc_add_method_declaration
20765 (cp_parser_objc_method_signature (parser
));
20766 cp_parser_consume_semicolon_at_end_of_statement (parser
);
20769 /* Allow for interspersed non-ObjC++ code. */
20770 cp_parser_objc_interstitial_code (parser
);
20772 token
= cp_lexer_peek_token (parser
->lexer
);
20775 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
20776 objc_finish_interface ();
20779 /* Parse an Objective-C method definition list. */
20782 cp_parser_objc_method_definition_list (cp_parser
* parser
)
20784 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20786 while (token
->keyword
!= RID_AT_END
)
20790 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
20792 push_deferring_access_checks (dk_deferred
);
20793 objc_start_method_definition
20794 (cp_parser_objc_method_signature (parser
));
20796 /* For historical reasons, we accept an optional semicolon. */
20797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20798 cp_lexer_consume_token (parser
->lexer
);
20800 perform_deferred_access_checks ();
20801 stop_deferring_access_checks ();
20802 meth
= cp_parser_function_definition_after_declarator (parser
,
20804 pop_deferring_access_checks ();
20805 objc_finish_method_definition (meth
);
20808 /* Allow for interspersed non-ObjC++ code. */
20809 cp_parser_objc_interstitial_code (parser
);
20811 token
= cp_lexer_peek_token (parser
->lexer
);
20814 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
20815 objc_finish_implementation ();
20818 /* Parse Objective-C ivars. */
20821 cp_parser_objc_class_ivars (cp_parser
* parser
)
20823 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20825 if (token
->type
!= CPP_OPEN_BRACE
)
20826 return; /* No ivars specified. */
20828 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
20829 token
= cp_lexer_peek_token (parser
->lexer
);
20831 while (token
->type
!= CPP_CLOSE_BRACE
)
20833 cp_decl_specifier_seq declspecs
;
20834 int decl_class_or_enum_p
;
20835 tree prefix_attributes
;
20837 cp_parser_objc_visibility_spec (parser
);
20839 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
20842 cp_parser_decl_specifier_seq (parser
,
20843 CP_PARSER_FLAGS_OPTIONAL
,
20845 &decl_class_or_enum_p
);
20846 prefix_attributes
= declspecs
.attributes
;
20847 declspecs
.attributes
= NULL_TREE
;
20849 /* Keep going until we hit the `;' at the end of the
20851 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20853 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
20854 cp_declarator
*declarator
= NULL
;
20855 int ctor_dtor_or_conv_p
;
20857 /* Check for a (possibly unnamed) bitfield declaration. */
20858 token
= cp_lexer_peek_token (parser
->lexer
);
20859 if (token
->type
== CPP_COLON
)
20862 if (token
->type
== CPP_NAME
20863 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
20866 /* Get the name of the bitfield. */
20867 declarator
= make_id_declarator (NULL_TREE
,
20868 cp_parser_identifier (parser
),
20872 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
20873 /* Get the width of the bitfield. */
20875 = cp_parser_constant_expression (parser
,
20876 /*allow_non_constant=*/false,
20881 /* Parse the declarator. */
20883 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20884 &ctor_dtor_or_conv_p
,
20885 /*parenthesized_p=*/NULL
,
20886 /*member_p=*/false);
20889 /* Look for attributes that apply to the ivar. */
20890 attributes
= cp_parser_attributes_opt (parser
);
20891 /* Remember which attributes are prefix attributes and
20893 first_attribute
= attributes
;
20894 /* Combine the attributes. */
20895 attributes
= chainon (prefix_attributes
, attributes
);
20898 /* Create the bitfield declaration. */
20899 decl
= grokbitfield (declarator
, &declspecs
,
20903 decl
= grokfield (declarator
, &declspecs
,
20904 NULL_TREE
, /*init_const_expr_p=*/false,
20905 NULL_TREE
, attributes
);
20907 /* Add the instance variable. */
20908 objc_add_instance_variable (decl
);
20910 /* Reset PREFIX_ATTRIBUTES. */
20911 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
20912 attributes
= TREE_CHAIN (attributes
);
20914 TREE_CHAIN (attributes
) = NULL_TREE
;
20916 token
= cp_lexer_peek_token (parser
->lexer
);
20918 if (token
->type
== CPP_COMMA
)
20920 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
20926 cp_parser_consume_semicolon_at_end_of_statement (parser
);
20927 token
= cp_lexer_peek_token (parser
->lexer
);
20930 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
20931 /* For historical reasons, we accept an optional semicolon. */
20932 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20933 cp_lexer_consume_token (parser
->lexer
);
20936 /* Parse an Objective-C protocol declaration. */
20939 cp_parser_objc_protocol_declaration (cp_parser
* parser
)
20941 tree proto
, protorefs
;
20944 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
20945 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
20947 tok
= cp_lexer_peek_token (parser
->lexer
);
20948 error_at (tok
->location
, "identifier expected after %<@protocol%>");
20952 /* See if we have a forward declaration or a definition. */
20953 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
20955 /* Try a forward declaration first. */
20956 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
20958 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
20960 cp_parser_consume_semicolon_at_end_of_statement (parser
);
20963 /* Ok, we got a full-fledged definition (or at least should). */
20966 proto
= cp_parser_identifier (parser
);
20967 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
20968 objc_start_protocol (proto
, protorefs
);
20969 cp_parser_objc_method_prototype_list (parser
);
20973 /* Parse an Objective-C superclass or category. */
20976 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
20979 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
20981 *super
= *categ
= NULL_TREE
;
20982 if (next
->type
== CPP_COLON
)
20984 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
20985 *super
= cp_parser_identifier (parser
);
20987 else if (next
->type
== CPP_OPEN_PAREN
)
20989 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
20990 *categ
= cp_parser_identifier (parser
);
20991 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
20995 /* Parse an Objective-C class interface. */
20998 cp_parser_objc_class_interface (cp_parser
* parser
)
21000 tree name
, super
, categ
, protos
;
21002 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
21003 name
= cp_parser_identifier (parser
);
21004 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
21005 protos
= cp_parser_objc_protocol_refs_opt (parser
);
21007 /* We have either a class or a category on our hands. */
21009 objc_start_category_interface (name
, categ
, protos
);
21012 objc_start_class_interface (name
, super
, protos
);
21013 /* Handle instance variable declarations, if any. */
21014 cp_parser_objc_class_ivars (parser
);
21015 objc_continue_interface ();
21018 cp_parser_objc_method_prototype_list (parser
);
21021 /* Parse an Objective-C class implementation. */
21024 cp_parser_objc_class_implementation (cp_parser
* parser
)
21026 tree name
, super
, categ
;
21028 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
21029 name
= cp_parser_identifier (parser
);
21030 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
21032 /* We have either a class or a category on our hands. */
21034 objc_start_category_implementation (name
, categ
);
21037 objc_start_class_implementation (name
, super
);
21038 /* Handle instance variable declarations, if any. */
21039 cp_parser_objc_class_ivars (parser
);
21040 objc_continue_implementation ();
21043 cp_parser_objc_method_definition_list (parser
);
21046 /* Consume the @end token and finish off the implementation. */
21049 cp_parser_objc_end_implementation (cp_parser
* parser
)
21051 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
21052 objc_finish_implementation ();
21055 /* Parse an Objective-C declaration. */
21058 cp_parser_objc_declaration (cp_parser
* parser
)
21060 /* Try to figure out what kind of declaration is present. */
21061 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
21063 switch (kwd
->keyword
)
21066 cp_parser_objc_alias_declaration (parser
);
21069 cp_parser_objc_class_declaration (parser
);
21071 case RID_AT_PROTOCOL
:
21072 cp_parser_objc_protocol_declaration (parser
);
21074 case RID_AT_INTERFACE
:
21075 cp_parser_objc_class_interface (parser
);
21077 case RID_AT_IMPLEMENTATION
:
21078 cp_parser_objc_class_implementation (parser
);
21081 cp_parser_objc_end_implementation (parser
);
21084 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
21086 cp_parser_skip_to_end_of_block_or_statement (parser
);
21090 /* Parse an Objective-C try-catch-finally statement.
21092 objc-try-catch-finally-stmt:
21093 @try compound-statement objc-catch-clause-seq [opt]
21094 objc-finally-clause [opt]
21096 objc-catch-clause-seq:
21097 objc-catch-clause objc-catch-clause-seq [opt]
21100 @catch ( exception-declaration ) compound-statement
21102 objc-finally-clause
21103 @finally compound-statement
21105 Returns NULL_TREE. */
21108 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
21109 location_t location
;
21112 cp_parser_require_keyword (parser
, RID_AT_TRY
, "%<@try%>");
21113 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
21114 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21115 node, lest it get absorbed into the surrounding block. */
21116 stmt
= push_stmt_list ();
21117 cp_parser_compound_statement (parser
, NULL
, false);
21118 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
21120 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
21122 cp_parameter_declarator
*parmdecl
;
21125 cp_lexer_consume_token (parser
->lexer
);
21126 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
21127 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
21128 parm
= grokdeclarator (parmdecl
->declarator
,
21129 &parmdecl
->decl_specifiers
,
21130 PARM
, /*initialized=*/0,
21131 /*attrlist=*/NULL
);
21132 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
21133 objc_begin_catch_clause (parm
);
21134 cp_parser_compound_statement (parser
, NULL
, false);
21135 objc_finish_catch_clause ();
21138 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
21140 cp_lexer_consume_token (parser
->lexer
);
21141 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
21142 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21143 node, lest it get absorbed into the surrounding block. */
21144 stmt
= push_stmt_list ();
21145 cp_parser_compound_statement (parser
, NULL
, false);
21146 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
21149 return objc_finish_try_stmt ();
21152 /* Parse an Objective-C synchronized statement.
21154 objc-synchronized-stmt:
21155 @synchronized ( expression ) compound-statement
21157 Returns NULL_TREE. */
21160 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
21161 location_t location
;
21164 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, "%<@synchronized%>");
21166 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
21167 cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>");
21168 lock
= cp_parser_expression (parser
, false, NULL
);
21169 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
21171 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21172 node, lest it get absorbed into the surrounding block. */
21173 stmt
= push_stmt_list ();
21174 cp_parser_compound_statement (parser
, NULL
, false);
21176 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
21179 /* Parse an Objective-C throw statement.
21182 @throw assignment-expression [opt] ;
21184 Returns a constructed '@throw' statement. */
21187 cp_parser_objc_throw_statement (cp_parser
*parser
) {
21188 tree expr
= NULL_TREE
;
21189 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21191 cp_parser_require_keyword (parser
, RID_AT_THROW
, "%<@throw%>");
21193 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
21194 expr
= cp_parser_assignment_expression (parser
, false, NULL
);
21196 cp_parser_consume_semicolon_at_end_of_statement (parser
);
21198 return objc_build_throw_stmt (loc
, expr
);
21201 /* Parse an Objective-C statement. */
21204 cp_parser_objc_statement (cp_parser
* parser
) {
21205 /* Try to figure out what kind of declaration is present. */
21206 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
21208 switch (kwd
->keyword
)
21211 return cp_parser_objc_try_catch_finally_statement (parser
);
21212 case RID_AT_SYNCHRONIZED
:
21213 return cp_parser_objc_synchronized_statement (parser
);
21215 return cp_parser_objc_throw_statement (parser
);
21217 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
21219 cp_parser_skip_to_end_of_block_or_statement (parser
);
21222 return error_mark_node
;
21225 /* OpenMP 2.5 parsing routines. */
21227 /* Returns name of the next clause.
21228 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21229 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21230 returned and the token is consumed. */
21232 static pragma_omp_clause
21233 cp_parser_omp_clause_name (cp_parser
*parser
)
21235 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
21237 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
21238 result
= PRAGMA_OMP_CLAUSE_IF
;
21239 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
21240 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
21241 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
21242 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
21243 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
21245 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
21246 const char *p
= IDENTIFIER_POINTER (id
);
21251 if (!strcmp ("collapse", p
))
21252 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
21253 else if (!strcmp ("copyin", p
))
21254 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
21255 else if (!strcmp ("copyprivate", p
))
21256 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
21259 if (!strcmp ("firstprivate", p
))
21260 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
21263 if (!strcmp ("lastprivate", p
))
21264 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
21267 if (!strcmp ("nowait", p
))
21268 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
21269 else if (!strcmp ("num_threads", p
))
21270 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
21273 if (!strcmp ("ordered", p
))
21274 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
21277 if (!strcmp ("reduction", p
))
21278 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
21281 if (!strcmp ("schedule", p
))
21282 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
21283 else if (!strcmp ("shared", p
))
21284 result
= PRAGMA_OMP_CLAUSE_SHARED
;
21287 if (!strcmp ("untied", p
))
21288 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
21293 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
21294 cp_lexer_consume_token (parser
->lexer
);
21299 /* Validate that a clause of the given type does not already exist. */
21302 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
21303 const char *name
, location_t location
)
21307 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
21308 if (OMP_CLAUSE_CODE (c
) == code
)
21310 error_at (location
, "too many %qs clauses", name
);
21318 variable-list , identifier
21320 In addition, we match a closing parenthesis. An opening parenthesis
21321 will have been consumed by the caller.
21323 If KIND is nonzero, create the appropriate node and install the decl
21324 in OMP_CLAUSE_DECL and add the node to the head of the list.
21326 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21327 return the list created. */
21330 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
21338 token
= cp_lexer_peek_token (parser
->lexer
);
21339 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
21340 /*check_dependency_p=*/true,
21341 /*template_p=*/NULL
,
21342 /*declarator_p=*/false,
21343 /*optional_p=*/false);
21344 if (name
== error_mark_node
)
21347 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
21348 if (decl
== error_mark_node
)
21349 cp_parser_name_lookup_error (parser
, name
, decl
, NULL
, token
->location
);
21350 else if (kind
!= 0)
21352 tree u
= build_omp_clause (token
->location
, kind
);
21353 OMP_CLAUSE_DECL (u
) = decl
;
21354 OMP_CLAUSE_CHAIN (u
) = list
;
21358 list
= tree_cons (decl
, NULL_TREE
, list
);
21361 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21363 cp_lexer_consume_token (parser
->lexer
);
21366 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
21370 /* Try to resync to an unnested comma. Copied from
21371 cp_parser_parenthesized_expression_list. */
21373 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
21374 /*recovering=*/true,
21376 /*consume_paren=*/true);
21384 /* Similarly, but expect leading and trailing parenthesis. This is a very
21385 common case for omp clauses. */
21388 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
21390 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
21391 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
21396 collapse ( constant-expression ) */
21399 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
21405 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21406 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
21409 num
= cp_parser_constant_expression (parser
, false, NULL
);
21411 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
21412 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
21413 /*or_comma=*/false,
21414 /*consume_paren=*/true);
21416 if (num
== error_mark_node
)
21418 num
= fold_non_dependent_expr (num
);
21419 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
21420 || !host_integerp (num
, 0)
21421 || (n
= tree_low_cst (num
, 0)) <= 0
21424 error_at (loc
, "collapse argument needs positive constant integer expression");
21428 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
21429 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
21430 OMP_CLAUSE_CHAIN (c
) = list
;
21431 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
21437 default ( shared | none ) */
21440 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
21442 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
21445 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
21447 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
21449 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
21450 const char *p
= IDENTIFIER_POINTER (id
);
21455 if (strcmp ("none", p
) != 0)
21457 kind
= OMP_CLAUSE_DEFAULT_NONE
;
21461 if (strcmp ("shared", p
) != 0)
21463 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
21470 cp_lexer_consume_token (parser
->lexer
);
21475 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
21478 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
21479 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
21480 /*or_comma=*/false,
21481 /*consume_paren=*/true);
21483 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
21486 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
21487 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
21488 OMP_CLAUSE_CHAIN (c
) = list
;
21489 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
21495 if ( expression ) */
21498 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
21502 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
21505 t
= cp_parser_condition (parser
);
21507 if (t
== error_mark_node
21508 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
21509 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
21510 /*or_comma=*/false,
21511 /*consume_paren=*/true);
21513 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
21515 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
21516 OMP_CLAUSE_IF_EXPR (c
) = t
;
21517 OMP_CLAUSE_CHAIN (c
) = list
;
21526 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
,
21527 tree list
, location_t location
)
21531 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
21533 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
21534 OMP_CLAUSE_CHAIN (c
) = list
;
21539 num_threads ( expression ) */
21542 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
21543 location_t location
)
21547 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
21550 t
= cp_parser_expression (parser
, false, NULL
);
21552 if (t
== error_mark_node
21553 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
21554 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
21555 /*or_comma=*/false,
21556 /*consume_paren=*/true);
21558 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
21559 "num_threads", location
);
21561 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
21562 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
21563 OMP_CLAUSE_CHAIN (c
) = list
;
21572 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
,
21573 tree list
, location_t location
)
21577 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
21578 "ordered", location
);
21580 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
21581 OMP_CLAUSE_CHAIN (c
) = list
;
21586 reduction ( reduction-operator : variable-list )
21588 reduction-operator:
21589 One of: + * - & ^ | && || */
21592 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
21594 enum tree_code code
;
21597 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
21600 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
21612 code
= BIT_AND_EXPR
;
21615 code
= BIT_XOR_EXPR
;
21618 code
= BIT_IOR_EXPR
;
21621 code
= TRUTH_ANDIF_EXPR
;
21624 code
= TRUTH_ORIF_EXPR
;
21627 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21628 "%<|%>, %<&&%>, or %<||%>");
21630 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
21631 /*or_comma=*/false,
21632 /*consume_paren=*/true);
21635 cp_lexer_consume_token (parser
->lexer
);
21637 if (!cp_parser_require (parser
, CPP_COLON
, "%<:%>"))
21640 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
21641 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
21642 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
21648 schedule ( schedule-kind )
21649 schedule ( schedule-kind , expression )
21652 static | dynamic | guided | runtime | auto */
21655 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
21659 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
21662 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
21664 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
21666 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
21667 const char *p
= IDENTIFIER_POINTER (id
);
21672 if (strcmp ("dynamic", p
) != 0)
21674 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
21678 if (strcmp ("guided", p
) != 0)
21680 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
21684 if (strcmp ("runtime", p
) != 0)
21686 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
21693 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
21694 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
21695 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
21696 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
21699 cp_lexer_consume_token (parser
->lexer
);
21701 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21704 cp_lexer_consume_token (parser
->lexer
);
21706 token
= cp_lexer_peek_token (parser
->lexer
);
21707 t
= cp_parser_assignment_expression (parser
, false, NULL
);
21709 if (t
== error_mark_node
)
21711 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
21712 error_at (token
->location
, "schedule %<runtime%> does not take "
21713 "a %<chunk_size%> parameter");
21714 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
21715 error_at (token
->location
, "schedule %<auto%> does not take "
21716 "a %<chunk_size%> parameter");
21718 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
21720 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
21723 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<,%> or %<)%>"))
21726 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
21727 OMP_CLAUSE_CHAIN (c
) = list
;
21731 cp_parser_error (parser
, "invalid schedule kind");
21733 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
21734 /*or_comma=*/false,
21735 /*consume_paren=*/true);
21743 cp_parser_omp_clause_untied (cp_parser
*parser ATTRIBUTE_UNUSED
,
21744 tree list
, location_t location
)
21748 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
21750 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
21751 OMP_CLAUSE_CHAIN (c
) = list
;
21755 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21756 is a bitmask in MASK. Return the list of clauses found; the result
21757 of clause default goes in *pdefault. */
21760 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
21761 const char *where
, cp_token
*pragma_tok
)
21763 tree clauses
= NULL
;
21765 cp_token
*token
= NULL
;
21767 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
21769 pragma_omp_clause c_kind
;
21770 const char *c_name
;
21771 tree prev
= clauses
;
21773 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21774 cp_lexer_consume_token (parser
->lexer
);
21776 token
= cp_lexer_peek_token (parser
->lexer
);
21777 c_kind
= cp_parser_omp_clause_name (parser
);
21782 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
21783 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
21785 c_name
= "collapse";
21787 case PRAGMA_OMP_CLAUSE_COPYIN
:
21788 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
21791 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
21792 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
21794 c_name
= "copyprivate";
21796 case PRAGMA_OMP_CLAUSE_DEFAULT
:
21797 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
21799 c_name
= "default";
21801 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
21802 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
21804 c_name
= "firstprivate";
21806 case PRAGMA_OMP_CLAUSE_IF
:
21807 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
21810 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
21811 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
21813 c_name
= "lastprivate";
21815 case PRAGMA_OMP_CLAUSE_NOWAIT
:
21816 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
21819 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
21820 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
21822 c_name
= "num_threads";
21824 case PRAGMA_OMP_CLAUSE_ORDERED
:
21825 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
21827 c_name
= "ordered";
21829 case PRAGMA_OMP_CLAUSE_PRIVATE
:
21830 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
21832 c_name
= "private";
21834 case PRAGMA_OMP_CLAUSE_REDUCTION
:
21835 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
21836 c_name
= "reduction";
21838 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
21839 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
21841 c_name
= "schedule";
21843 case PRAGMA_OMP_CLAUSE_SHARED
:
21844 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
21848 case PRAGMA_OMP_CLAUSE_UNTIED
:
21849 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
21854 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
21858 if (((mask
>> c_kind
) & 1) == 0)
21860 /* Remove the invalid clause(s) from the list to avoid
21861 confusing the rest of the compiler. */
21863 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
21867 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
21868 return finish_omp_clauses (clauses
);
21875 In practice, we're also interested in adding the statement to an
21876 outer node. So it is convenient if we work around the fact that
21877 cp_parser_statement calls add_stmt. */
21880 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
21882 unsigned save
= parser
->in_statement
;
21884 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21885 This preserves the "not within loop or switch" style error messages
21886 for nonsense cases like
21892 if (parser
->in_statement
)
21893 parser
->in_statement
= IN_OMP_BLOCK
;
21899 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
21901 parser
->in_statement
= save
;
21905 cp_parser_omp_structured_block (cp_parser
*parser
)
21907 tree stmt
= begin_omp_structured_block ();
21908 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
21910 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
21912 cp_parser_end_omp_structured_block (parser
, save
);
21913 return finish_omp_structured_block (stmt
);
21917 # pragma omp atomic new-line
21921 x binop= expr | x++ | ++x | x-- | --x
21923 +, *, -, /, &, ^, |, <<, >>
21925 where x is an lvalue expression with scalar type. */
21928 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
21931 enum tree_code code
;
21933 cp_parser_require_pragma_eol (parser
, pragma_tok
);
21935 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
21936 /*cast_p=*/false, NULL
);
21937 switch (TREE_CODE (lhs
))
21942 case PREINCREMENT_EXPR
:
21943 case POSTINCREMENT_EXPR
:
21944 lhs
= TREE_OPERAND (lhs
, 0);
21946 rhs
= integer_one_node
;
21949 case PREDECREMENT_EXPR
:
21950 case POSTDECREMENT_EXPR
:
21951 lhs
= TREE_OPERAND (lhs
, 0);
21953 rhs
= integer_one_node
;
21957 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
21963 code
= TRUNC_DIV_EXPR
;
21971 case CPP_LSHIFT_EQ
:
21972 code
= LSHIFT_EXPR
;
21974 case CPP_RSHIFT_EQ
:
21975 code
= RSHIFT_EXPR
;
21978 code
= BIT_AND_EXPR
;
21981 code
= BIT_IOR_EXPR
;
21984 code
= BIT_XOR_EXPR
;
21987 cp_parser_error (parser
,
21988 "invalid operator for %<#pragma omp atomic%>");
21991 cp_lexer_consume_token (parser
->lexer
);
21993 rhs
= cp_parser_expression (parser
, false, NULL
);
21994 if (rhs
== error_mark_node
)
21998 finish_omp_atomic (code
, lhs
, rhs
);
21999 cp_parser_consume_semicolon_at_end_of_statement (parser
);
22003 cp_parser_skip_to_end_of_block_or_statement (parser
);
22008 # pragma omp barrier new-line */
22011 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
22013 cp_parser_require_pragma_eol (parser
, pragma_tok
);
22014 finish_omp_barrier ();
22018 # pragma omp critical [(name)] new-line
22019 structured-block */
22022 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
22024 tree stmt
, name
= NULL
;
22026 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
22028 cp_lexer_consume_token (parser
->lexer
);
22030 name
= cp_parser_identifier (parser
);
22032 if (name
== error_mark_node
22033 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
22034 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
22035 /*or_comma=*/false,
22036 /*consume_paren=*/true);
22037 if (name
== error_mark_node
)
22040 cp_parser_require_pragma_eol (parser
, pragma_tok
);
22042 stmt
= cp_parser_omp_structured_block (parser
);
22043 return c_finish_omp_critical (input_location
, stmt
, name
);
22047 # pragma omp flush flush-vars[opt] new-line
22050 ( variable-list ) */
22053 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
22055 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
22056 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
22057 cp_parser_require_pragma_eol (parser
, pragma_tok
);
22059 finish_omp_flush ();
22062 /* Helper function, to parse omp for increment expression. */
22065 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
22067 tree cond
= cp_parser_binary_expression (parser
, false, true,
22068 PREC_NOT_OPERATOR
, NULL
);
22071 if (cond
== error_mark_node
22072 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
22074 cp_parser_skip_to_end_of_statement (parser
);
22075 return error_mark_node
;
22078 switch (TREE_CODE (cond
))
22086 return error_mark_node
;
22089 /* If decl is an iterator, preserve LHS and RHS of the relational
22090 expr until finish_omp_for. */
22092 && (type_dependent_expression_p (decl
)
22093 || CLASS_TYPE_P (TREE_TYPE (decl
))))
22096 return build_x_binary_op (TREE_CODE (cond
),
22097 TREE_OPERAND (cond
, 0), ERROR_MARK
,
22098 TREE_OPERAND (cond
, 1), ERROR_MARK
,
22099 &overloaded_p
, tf_warning_or_error
);
22102 /* Helper function, to parse omp for increment expression. */
22105 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
22107 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22113 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
22115 op
= (token
->type
== CPP_PLUS_PLUS
22116 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
22117 cp_lexer_consume_token (parser
->lexer
);
22118 lhs
= cp_parser_cast_expression (parser
, false, false, NULL
);
22120 return error_mark_node
;
22121 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
22124 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
22126 return error_mark_node
;
22128 token
= cp_lexer_peek_token (parser
->lexer
);
22129 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
22131 op
= (token
->type
== CPP_PLUS_PLUS
22132 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
22133 cp_lexer_consume_token (parser
->lexer
);
22134 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
22137 op
= cp_parser_assignment_operator_opt (parser
);
22138 if (op
== ERROR_MARK
)
22139 return error_mark_node
;
22141 if (op
!= NOP_EXPR
)
22143 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
22144 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
22145 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
22148 lhs
= cp_parser_binary_expression (parser
, false, false,
22149 PREC_ADDITIVE_EXPRESSION
, NULL
);
22150 token
= cp_lexer_peek_token (parser
->lexer
);
22151 decl_first
= lhs
== decl
;
22154 if (token
->type
!= CPP_PLUS
22155 && token
->type
!= CPP_MINUS
)
22156 return error_mark_node
;
22160 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
22161 cp_lexer_consume_token (parser
->lexer
);
22162 rhs
= cp_parser_binary_expression (parser
, false, false,
22163 PREC_ADDITIVE_EXPRESSION
, NULL
);
22164 token
= cp_lexer_peek_token (parser
->lexer
);
22165 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
22167 if (lhs
== NULL_TREE
)
22169 if (op
== PLUS_EXPR
)
22172 lhs
= build_x_unary_op (NEGATE_EXPR
, rhs
, tf_warning_or_error
);
22175 lhs
= build_x_binary_op (op
, lhs
, ERROR_MARK
, rhs
, ERROR_MARK
,
22176 NULL
, tf_warning_or_error
);
22179 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
22183 if (rhs
!= decl
|| op
== MINUS_EXPR
)
22184 return error_mark_node
;
22185 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
22188 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
22190 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
22193 /* Parse the restricted form of the for statement allowed by OpenMP. */
22196 cp_parser_omp_for_loop (cp_parser
*parser
, tree clauses
, tree
*par_clauses
)
22198 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
22199 tree for_block
= NULL_TREE
, real_decl
, initv
, condv
, incrv
, declv
;
22200 tree this_pre_body
, cl
;
22201 location_t loc_first
;
22202 bool collapse_err
= false;
22203 int i
, collapse
= 1, nbraces
= 0;
22205 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
22206 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
22207 collapse
= tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl
), 0);
22209 gcc_assert (collapse
>= 1);
22211 declv
= make_tree_vec (collapse
);
22212 initv
= make_tree_vec (collapse
);
22213 condv
= make_tree_vec (collapse
);
22214 incrv
= make_tree_vec (collapse
);
22216 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
22218 for (i
= 0; i
< collapse
; i
++)
22220 int bracecount
= 0;
22221 bool add_private_clause
= false;
22224 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
22226 cp_parser_error (parser
, "for statement expected");
22229 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
22231 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "%<(%>"))
22234 init
= decl
= real_decl
= NULL
;
22235 this_pre_body
= push_stmt_list ();
22236 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
22238 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22242 integer-type var = lb
22243 random-access-iterator-type var = lb
22244 pointer-type var = lb
22246 cp_decl_specifier_seq type_specifiers
;
22248 /* First, try to parse as an initialized declaration. See
22249 cp_parser_condition, from whence the bulk of this is copied. */
22251 cp_parser_parse_tentatively (parser
);
22252 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
22253 /*is_trailing_return=*/false,
22255 if (cp_parser_parse_definitely (parser
))
22257 /* If parsing a type specifier seq succeeded, then this
22258 MUST be a initialized declaration. */
22259 tree asm_specification
, attributes
;
22260 cp_declarator
*declarator
;
22262 declarator
= cp_parser_declarator (parser
,
22263 CP_PARSER_DECLARATOR_NAMED
,
22264 /*ctor_dtor_or_conv_p=*/NULL
,
22265 /*parenthesized_p=*/NULL
,
22266 /*member_p=*/false);
22267 attributes
= cp_parser_attributes_opt (parser
);
22268 asm_specification
= cp_parser_asm_specification_opt (parser
);
22270 if (declarator
== cp_error_declarator
)
22271 cp_parser_skip_to_end_of_statement (parser
);
22275 tree pushed_scope
, auto_node
;
22277 decl
= start_decl (declarator
, &type_specifiers
,
22278 SD_INITIALIZED
, attributes
,
22279 /*prefix_attributes=*/NULL_TREE
,
22282 auto_node
= type_uses_auto (TREE_TYPE (decl
));
22283 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
22285 if (cp_lexer_next_token_is (parser
->lexer
,
22287 error ("parenthesized initialization is not allowed in "
22288 "OpenMP %<for%> loop");
22290 /* Trigger an error. */
22291 cp_parser_require (parser
, CPP_EQ
, "%<=%>");
22293 init
= error_mark_node
;
22294 cp_parser_skip_to_end_of_statement (parser
);
22296 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
22297 || type_dependent_expression_p (decl
)
22300 bool is_direct_init
, is_non_constant_init
;
22302 init
= cp_parser_initializer (parser
,
22304 &is_non_constant_init
);
22306 if (auto_node
&& describable_type (init
))
22309 = do_auto_deduction (TREE_TYPE (decl
), init
,
22312 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
22313 && !type_dependent_expression_p (decl
))
22317 cp_finish_decl (decl
, init
, !is_non_constant_init
,
22319 LOOKUP_ONLYCONVERTING
);
22320 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
22323 = tree_cons (NULL
, this_pre_body
, for_block
);
22327 init
= pop_stmt_list (this_pre_body
);
22328 this_pre_body
= NULL_TREE
;
22333 cp_lexer_consume_token (parser
->lexer
);
22334 init
= cp_parser_assignment_expression (parser
, false, NULL
);
22337 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
22338 init
= error_mark_node
;
22340 cp_finish_decl (decl
, NULL_TREE
,
22341 /*init_const_expr_p=*/false,
22343 LOOKUP_ONLYCONVERTING
);
22347 pop_scope (pushed_scope
);
22353 /* If parsing a type specifier sequence failed, then
22354 this MUST be a simple expression. */
22355 cp_parser_parse_tentatively (parser
);
22356 decl
= cp_parser_primary_expression (parser
, false, false,
22358 if (!cp_parser_error_occurred (parser
)
22361 && CLASS_TYPE_P (TREE_TYPE (decl
)))
22365 cp_parser_parse_definitely (parser
);
22366 cp_parser_require (parser
, CPP_EQ
, "%<=%>");
22367 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
22368 finish_expr_stmt (build_x_modify_expr (decl
, NOP_EXPR
,
22370 tf_warning_or_error
));
22371 add_private_clause
= true;
22376 cp_parser_abort_tentative_parse (parser
);
22377 init
= cp_parser_expression (parser
, false, NULL
);
22380 if (TREE_CODE (init
) == MODIFY_EXPR
22381 || TREE_CODE (init
) == MODOP_EXPR
)
22382 real_decl
= TREE_OPERAND (init
, 0);
22387 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
22390 this_pre_body
= pop_stmt_list (this_pre_body
);
22394 pre_body
= push_stmt_list ();
22396 add_stmt (this_pre_body
);
22397 pre_body
= pop_stmt_list (pre_body
);
22400 pre_body
= this_pre_body
;
22405 if (par_clauses
!= NULL
&& real_decl
!= NULL_TREE
)
22408 for (c
= par_clauses
; *c
; )
22409 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
22410 && OMP_CLAUSE_DECL (*c
) == real_decl
)
22412 error_at (loc
, "iteration variable %qD"
22413 " should not be firstprivate", real_decl
);
22414 *c
= OMP_CLAUSE_CHAIN (*c
);
22416 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
22417 && OMP_CLAUSE_DECL (*c
) == real_decl
)
22419 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22420 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22421 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
22422 OMP_CLAUSE_DECL (l
) = real_decl
;
22423 OMP_CLAUSE_CHAIN (l
) = clauses
;
22424 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
22426 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
22427 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
22428 add_private_clause
= false;
22432 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
22433 && OMP_CLAUSE_DECL (*c
) == real_decl
)
22434 add_private_clause
= false;
22435 c
= &OMP_CLAUSE_CHAIN (*c
);
22439 if (add_private_clause
)
22442 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
22444 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
22445 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
22446 && OMP_CLAUSE_DECL (c
) == decl
)
22448 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
22449 && OMP_CLAUSE_DECL (c
) == decl
)
22450 error_at (loc
, "iteration variable %qD "
22451 "should not be firstprivate",
22453 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
22454 && OMP_CLAUSE_DECL (c
) == decl
)
22455 error_at (loc
, "iteration variable %qD should not be reduction",
22460 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
22461 OMP_CLAUSE_DECL (c
) = decl
;
22462 c
= finish_omp_clauses (c
);
22465 OMP_CLAUSE_CHAIN (c
) = clauses
;
22472 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
22473 cond
= cp_parser_omp_for_cond (parser
, decl
);
22474 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
22477 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
22479 /* If decl is an iterator, preserve the operator on decl
22480 until finish_omp_for. */
22482 && (type_dependent_expression_p (decl
)
22483 || CLASS_TYPE_P (TREE_TYPE (decl
))))
22484 incr
= cp_parser_omp_for_incr (parser
, decl
);
22486 incr
= cp_parser_expression (parser
, false, NULL
);
22489 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>"))
22490 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
22491 /*or_comma=*/false,
22492 /*consume_paren=*/true);
22494 TREE_VEC_ELT (declv
, i
) = decl
;
22495 TREE_VEC_ELT (initv
, i
) = init
;
22496 TREE_VEC_ELT (condv
, i
) = cond
;
22497 TREE_VEC_ELT (incrv
, i
) = incr
;
22499 if (i
== collapse
- 1)
22502 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22503 in between the collapsed for loops to be still considered perfectly
22504 nested. Hopefully the final version clarifies this.
22505 For now handle (multiple) {'s and empty statements. */
22506 cp_parser_parse_tentatively (parser
);
22509 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
22511 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22513 cp_lexer_consume_token (parser
->lexer
);
22516 else if (bracecount
22517 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22518 cp_lexer_consume_token (parser
->lexer
);
22521 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22522 error_at (loc
, "not enough collapsed for loops");
22523 collapse_err
= true;
22524 cp_parser_abort_tentative_parse (parser
);
22533 cp_parser_parse_definitely (parser
);
22534 nbraces
+= bracecount
;
22538 /* Note that we saved the original contents of this flag when we entered
22539 the structured block, and so we don't need to re-save it here. */
22540 parser
->in_statement
= IN_OMP_FOR
;
22542 /* Note that the grammar doesn't call for a structured block here,
22543 though the loop as a whole is a structured block. */
22544 body
= push_stmt_list ();
22545 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
22546 body
= pop_stmt_list (body
);
22548 if (declv
== NULL_TREE
)
22551 ret
= finish_omp_for (loc_first
, declv
, initv
, condv
, incrv
, body
,
22552 pre_body
, clauses
);
22556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
22558 cp_lexer_consume_token (parser
->lexer
);
22561 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22562 cp_lexer_consume_token (parser
->lexer
);
22567 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
22568 "collapsed loops not perfectly nested");
22570 collapse_err
= true;
22571 cp_parser_statement_seq_opt (parser
, NULL
);
22572 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
22579 add_stmt (pop_stmt_list (TREE_VALUE (for_block
)));
22580 for_block
= TREE_CHAIN (for_block
);
22587 #pragma omp for for-clause[optseq] new-line
22590 #define OMP_FOR_CLAUSE_MASK \
22591 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22592 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22593 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22594 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22595 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22596 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22597 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22598 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22601 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
22603 tree clauses
, sb
, ret
;
22606 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
22607 "#pragma omp for", pragma_tok
);
22609 sb
= begin_omp_structured_block ();
22610 save
= cp_parser_begin_omp_structured_block (parser
);
22612 ret
= cp_parser_omp_for_loop (parser
, clauses
, NULL
);
22614 cp_parser_end_omp_structured_block (parser
, save
);
22615 add_stmt (finish_omp_structured_block (sb
));
22621 # pragma omp master new-line
22622 structured-block */
22625 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
22627 cp_parser_require_pragma_eol (parser
, pragma_tok
);
22628 return c_finish_omp_master (input_location
,
22629 cp_parser_omp_structured_block (parser
));
22633 # pragma omp ordered new-line
22634 structured-block */
22637 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
22639 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22640 cp_parser_require_pragma_eol (parser
, pragma_tok
);
22641 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
22647 { section-sequence }
22650 section-directive[opt] structured-block
22651 section-sequence section-directive structured-block */
22654 cp_parser_omp_sections_scope (cp_parser
*parser
)
22656 tree stmt
, substmt
;
22657 bool error_suppress
= false;
22660 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "%<{%>"))
22663 stmt
= push_stmt_list ();
22665 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
22669 substmt
= begin_omp_structured_block ();
22670 save
= cp_parser_begin_omp_structured_block (parser
);
22674 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
22676 tok
= cp_lexer_peek_token (parser
->lexer
);
22677 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
22679 if (tok
->type
== CPP_CLOSE_BRACE
)
22681 if (tok
->type
== CPP_EOF
)
22685 cp_parser_end_omp_structured_block (parser
, save
);
22686 substmt
= finish_omp_structured_block (substmt
);
22687 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
22688 add_stmt (substmt
);
22693 tok
= cp_lexer_peek_token (parser
->lexer
);
22694 if (tok
->type
== CPP_CLOSE_BRACE
)
22696 if (tok
->type
== CPP_EOF
)
22699 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
22701 cp_lexer_consume_token (parser
->lexer
);
22702 cp_parser_require_pragma_eol (parser
, tok
);
22703 error_suppress
= false;
22705 else if (!error_suppress
)
22707 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
22708 error_suppress
= true;
22711 substmt
= cp_parser_omp_structured_block (parser
);
22712 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
22713 add_stmt (substmt
);
22715 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "%<}%>");
22717 substmt
= pop_stmt_list (stmt
);
22719 stmt
= make_node (OMP_SECTIONS
);
22720 TREE_TYPE (stmt
) = void_type_node
;
22721 OMP_SECTIONS_BODY (stmt
) = substmt
;
22728 # pragma omp sections sections-clause[optseq] newline
22731 #define OMP_SECTIONS_CLAUSE_MASK \
22732 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22733 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22734 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22735 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22736 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22739 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
22743 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
22744 "#pragma omp sections", pragma_tok
);
22746 ret
= cp_parser_omp_sections_scope (parser
);
22748 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
22754 # pragma parallel parallel-clause new-line
22755 # pragma parallel for parallel-for-clause new-line
22756 # pragma parallel sections parallel-sections-clause new-line */
22758 #define OMP_PARALLEL_CLAUSE_MASK \
22759 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22760 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22761 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22762 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22763 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22764 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22765 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22766 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22769 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
22771 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
22772 const char *p_name
= "#pragma omp parallel";
22773 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
22774 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
22776 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22778 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
22780 cp_lexer_consume_token (parser
->lexer
);
22781 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
22782 p_name
= "#pragma omp parallel for";
22783 mask
|= OMP_FOR_CLAUSE_MASK
;
22784 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
22786 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22788 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
22789 const char *p
= IDENTIFIER_POINTER (id
);
22790 if (strcmp (p
, "sections") == 0)
22792 cp_lexer_consume_token (parser
->lexer
);
22793 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
22794 p_name
= "#pragma omp parallel sections";
22795 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
22796 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
22800 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
22801 block
= begin_omp_parallel ();
22802 save
= cp_parser_begin_omp_structured_block (parser
);
22806 case PRAGMA_OMP_PARALLEL
:
22807 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
22808 par_clause
= clauses
;
22811 case PRAGMA_OMP_PARALLEL_FOR
:
22812 c_split_parallel_clauses (loc
, clauses
, &par_clause
, &ws_clause
);
22813 cp_parser_omp_for_loop (parser
, ws_clause
, &par_clause
);
22816 case PRAGMA_OMP_PARALLEL_SECTIONS
:
22817 c_split_parallel_clauses (loc
, clauses
, &par_clause
, &ws_clause
);
22818 stmt
= cp_parser_omp_sections_scope (parser
);
22820 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
22824 gcc_unreachable ();
22827 cp_parser_end_omp_structured_block (parser
, save
);
22828 stmt
= finish_omp_parallel (par_clause
, block
);
22829 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
22830 OMP_PARALLEL_COMBINED (stmt
) = 1;
22835 # pragma omp single single-clause[optseq] new-line
22836 structured-block */
22838 #define OMP_SINGLE_CLAUSE_MASK \
22839 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22840 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22841 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22842 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22845 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
22847 tree stmt
= make_node (OMP_SINGLE
);
22848 TREE_TYPE (stmt
) = void_type_node
;
22850 OMP_SINGLE_CLAUSES (stmt
)
22851 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
22852 "#pragma omp single", pragma_tok
);
22853 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
22855 return add_stmt (stmt
);
22859 # pragma omp task task-clause[optseq] new-line
22860 structured-block */
22862 #define OMP_TASK_CLAUSE_MASK \
22863 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22864 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22865 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22866 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22867 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22868 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22871 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
22873 tree clauses
, block
;
22876 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
22877 "#pragma omp task", pragma_tok
);
22878 block
= begin_omp_task ();
22879 save
= cp_parser_begin_omp_structured_block (parser
);
22880 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
22881 cp_parser_end_omp_structured_block (parser
, save
);
22882 return finish_omp_task (clauses
, block
);
22886 # pragma omp taskwait new-line */
22889 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
22891 cp_parser_require_pragma_eol (parser
, pragma_tok
);
22892 finish_omp_taskwait ();
22896 # pragma omp threadprivate (variable-list) */
22899 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
22903 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
22904 cp_parser_require_pragma_eol (parser
, pragma_tok
);
22906 finish_omp_threadprivate (vars
);
22909 /* Main entry point to OpenMP statement pragmas. */
22912 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
22916 switch (pragma_tok
->pragma_kind
)
22918 case PRAGMA_OMP_ATOMIC
:
22919 cp_parser_omp_atomic (parser
, pragma_tok
);
22921 case PRAGMA_OMP_CRITICAL
:
22922 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
22924 case PRAGMA_OMP_FOR
:
22925 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
22927 case PRAGMA_OMP_MASTER
:
22928 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
22930 case PRAGMA_OMP_ORDERED
:
22931 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
22933 case PRAGMA_OMP_PARALLEL
:
22934 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
22936 case PRAGMA_OMP_SECTIONS
:
22937 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
22939 case PRAGMA_OMP_SINGLE
:
22940 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
22942 case PRAGMA_OMP_TASK
:
22943 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
22946 gcc_unreachable ();
22950 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
22955 static GTY (()) cp_parser
*the_parser
;
22958 /* Special handling for the first token or line in the file. The first
22959 thing in the file might be #pragma GCC pch_preprocess, which loads a
22960 PCH file, which is a GC collection point. So we need to handle this
22961 first pragma without benefit of an existing lexer structure.
22963 Always returns one token to the caller in *FIRST_TOKEN. This is
22964 either the true first token of the file, or the first token after
22965 the initial pragma. */
22968 cp_parser_initial_pragma (cp_token
*first_token
)
22972 cp_lexer_get_preprocessor_token (NULL
, first_token
);
22973 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
22976 cp_lexer_get_preprocessor_token (NULL
, first_token
);
22977 if (first_token
->type
== CPP_STRING
)
22979 name
= first_token
->u
.value
;
22981 cp_lexer_get_preprocessor_token (NULL
, first_token
);
22982 if (first_token
->type
!= CPP_PRAGMA_EOL
)
22983 error_at (first_token
->location
,
22984 "junk at end of %<#pragma GCC pch_preprocess%>");
22987 error_at (first_token
->location
, "expected string literal");
22989 /* Skip to the end of the pragma. */
22990 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
22991 cp_lexer_get_preprocessor_token (NULL
, first_token
);
22993 /* Now actually load the PCH file. */
22995 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
22997 /* Read one more token to return to our caller. We have to do this
22998 after reading the PCH file in, since its pointers have to be
23000 cp_lexer_get_preprocessor_token (NULL
, first_token
);
23003 /* Normal parsing of a pragma token. Here we can (and must) use the
23007 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
23009 cp_token
*pragma_tok
;
23012 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
23013 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
23014 parser
->lexer
->in_pragma
= true;
23016 id
= pragma_tok
->pragma_kind
;
23019 case PRAGMA_GCC_PCH_PREPROCESS
:
23020 error_at (pragma_tok
->location
,
23021 "%<#pragma GCC pch_preprocess%> must be first");
23024 case PRAGMA_OMP_BARRIER
:
23027 case pragma_compound
:
23028 cp_parser_omp_barrier (parser
, pragma_tok
);
23031 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
23032 "used in compound statements");
23039 case PRAGMA_OMP_FLUSH
:
23042 case pragma_compound
:
23043 cp_parser_omp_flush (parser
, pragma_tok
);
23046 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
23047 "used in compound statements");
23054 case PRAGMA_OMP_TASKWAIT
:
23057 case pragma_compound
:
23058 cp_parser_omp_taskwait (parser
, pragma_tok
);
23061 error_at (pragma_tok
->location
,
23062 "%<#pragma omp taskwait%> may only be "
23063 "used in compound statements");
23070 case PRAGMA_OMP_THREADPRIVATE
:
23071 cp_parser_omp_threadprivate (parser
, pragma_tok
);
23074 case PRAGMA_OMP_ATOMIC
:
23075 case PRAGMA_OMP_CRITICAL
:
23076 case PRAGMA_OMP_FOR
:
23077 case PRAGMA_OMP_MASTER
:
23078 case PRAGMA_OMP_ORDERED
:
23079 case PRAGMA_OMP_PARALLEL
:
23080 case PRAGMA_OMP_SECTIONS
:
23081 case PRAGMA_OMP_SINGLE
:
23082 case PRAGMA_OMP_TASK
:
23083 if (context
== pragma_external
)
23085 cp_parser_omp_construct (parser
, pragma_tok
);
23088 case PRAGMA_OMP_SECTION
:
23089 error_at (pragma_tok
->location
,
23090 "%<#pragma omp section%> may only be used in "
23091 "%<#pragma omp sections%> construct");
23095 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
23096 c_invoke_pragma_handler (id
);
23100 cp_parser_error (parser
, "expected declaration specifiers");
23104 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
23108 /* The interface the pragma parsers have to the lexer. */
23111 pragma_lex (tree
*value
)
23114 enum cpp_ttype ret
;
23116 tok
= cp_lexer_peek_token (the_parser
->lexer
);
23119 *value
= tok
->u
.value
;
23121 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
23123 else if (ret
== CPP_STRING
)
23124 *value
= cp_parser_string_literal (the_parser
, false, false);
23127 cp_lexer_consume_token (the_parser
->lexer
);
23128 if (ret
== CPP_KEYWORD
)
23136 /* External interface. */
23138 /* Parse one entire translation unit. */
23141 c_parse_file (void)
23143 static bool already_called
= false;
23145 if (already_called
)
23147 sorry ("inter-module optimizations not implemented for C++");
23150 already_called
= true;
23152 the_parser
= cp_parser_new ();
23153 push_deferring_access_checks (flag_access_control
23154 ? dk_no_deferred
: dk_no_check
);
23155 cp_parser_translation_unit (the_parser
);
23159 #include "gt-cp-parser.h"