2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007 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"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct tree_check
GTY(())
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 cp_token
GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype
) type
: 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid
) keyword
: 8;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind
) pragma_kind
: 6;
74 /* True if this token is from a system header. */
75 BOOL_BITFIELD in_system_header
: 1;
76 /* True if this token is from a context where it is implicitly extern "C" */
77 BOOL_BITFIELD implicit_extern_c
: 1;
78 /* True for a CPP_NAME token that is not a keyword (i.e., for which
79 KEYWORD is RID_MAX) iff this name was looked up and found to be
80 ambiguous. An error has already been reported. */
81 BOOL_BITFIELD ambiguous_p
: 1;
82 /* The input file stack index at which this token was found. */
83 unsigned input_file_stack_index
: INPUT_FILE_STACK_BITS
;
84 /* The value associated with this token, if any. */
85 union cp_token_value
{
86 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
87 struct tree_check
* GTY((tag ("1"))) tree_check_value
;
88 /* Use for all other tokens. */
89 tree
GTY((tag ("0"))) value
;
90 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u
;
91 /* The location at which this token was found. */
95 /* We use a stack of token pointer for saving token sets. */
96 typedef struct cp_token
*cp_token_position
;
97 DEF_VEC_P (cp_token_position
);
98 DEF_VEC_ALLOC_P (cp_token_position
,heap
);
100 static cp_token eof_token
=
102 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, 0, 0, false, 0, { NULL
},
103 #if USE_MAPPED_LOCATION
110 /* The cp_lexer structure represents the C++ lexer. It is responsible
111 for managing the token stream from the preprocessor and supplying
112 it to the parser. Tokens are never added to the cp_lexer after
115 typedef struct cp_lexer
GTY (())
117 /* The memory allocated for the buffer. NULL if this lexer does not
118 own the token buffer. */
119 cp_token
* GTY ((length ("%h.buffer_length"))) buffer
;
120 /* If the lexer owns the buffer, this is the number of tokens in the
122 size_t buffer_length
;
124 /* A pointer just past the last available token. The tokens
125 in this lexer are [buffer, last_token). */
126 cp_token_position
GTY ((skip
)) last_token
;
128 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
129 no more available tokens. */
130 cp_token_position
GTY ((skip
)) next_token
;
132 /* A stack indicating positions at which cp_lexer_save_tokens was
133 called. The top entry is the most recent position at which we
134 began saving tokens. If the stack is non-empty, we are saving
136 VEC(cp_token_position
,heap
) *GTY ((skip
)) saved_tokens
;
138 /* The next lexer in a linked list of lexers. */
139 struct cp_lexer
*next
;
141 /* True if we should output debugging information. */
144 /* True if we're in the context of parsing a pragma, and should not
145 increment past the end-of-line marker. */
149 /* cp_token_cache is a range of tokens. There is no need to represent
150 allocate heap memory for it, since tokens are never removed from the
151 lexer's array. There is also no need for the GC to walk through
152 a cp_token_cache, since everything in here is referenced through
155 typedef struct cp_token_cache
GTY(())
157 /* The beginning of the token range. */
158 cp_token
* GTY((skip
)) first
;
160 /* Points immediately after the last token in the range. */
161 cp_token
* GTY ((skip
)) last
;
166 static cp_lexer
*cp_lexer_new_main
168 static cp_lexer
*cp_lexer_new_from_tokens
169 (cp_token_cache
*tokens
);
170 static void cp_lexer_destroy
172 static int cp_lexer_saving_tokens
174 static cp_token_position cp_lexer_token_position
176 static cp_token
*cp_lexer_token_at
177 (cp_lexer
*, cp_token_position
);
178 static void cp_lexer_get_preprocessor_token
179 (cp_lexer
*, cp_token
*);
180 static inline cp_token
*cp_lexer_peek_token
182 static cp_token
*cp_lexer_peek_nth_token
183 (cp_lexer
*, size_t);
184 static inline bool cp_lexer_next_token_is
185 (cp_lexer
*, enum cpp_ttype
);
186 static bool cp_lexer_next_token_is_not
187 (cp_lexer
*, enum cpp_ttype
);
188 static bool cp_lexer_next_token_is_keyword
189 (cp_lexer
*, enum rid
);
190 static cp_token
*cp_lexer_consume_token
192 static void cp_lexer_purge_token
194 static void cp_lexer_purge_tokens_after
195 (cp_lexer
*, cp_token_position
);
196 static void cp_lexer_save_tokens
198 static void cp_lexer_commit_tokens
200 static void cp_lexer_rollback_tokens
202 #ifdef ENABLE_CHECKING
203 static void cp_lexer_print_token
204 (FILE *, cp_token
*);
205 static inline bool cp_lexer_debugging_p
207 static void cp_lexer_start_debugging
208 (cp_lexer
*) ATTRIBUTE_UNUSED
;
209 static void cp_lexer_stop_debugging
210 (cp_lexer
*) ATTRIBUTE_UNUSED
;
212 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
213 about passing NULL to functions that require non-NULL arguments
214 (fputs, fprintf). It will never be used, so all we need is a value
215 of the right type that's guaranteed not to be NULL. */
216 #define cp_lexer_debug_stream stdout
217 #define cp_lexer_print_token(str, tok) (void) 0
218 #define cp_lexer_debugging_p(lexer) 0
219 #endif /* ENABLE_CHECKING */
221 static cp_token_cache
*cp_token_cache_new
222 (cp_token
*, cp_token
*);
224 static void cp_parser_initial_pragma
227 /* Manifest constants. */
228 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
229 #define CP_SAVED_TOKEN_STACK 5
231 /* A token type for keywords, as opposed to ordinary identifiers. */
232 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
234 /* A token type for template-ids. If a template-id is processed while
235 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
236 the value of the CPP_TEMPLATE_ID is whatever was returned by
237 cp_parser_template_id. */
238 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
240 /* A token type for nested-name-specifiers. If a
241 nested-name-specifier is processed while parsing tentatively, it is
242 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
243 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
244 cp_parser_nested_name_specifier_opt. */
245 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
247 /* A token type for tokens that are not tokens at all; these are used
248 to represent slots in the array where there used to be a token
249 that has now been deleted. */
250 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
252 /* The number of token types, including C++-specific ones. */
253 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
257 #ifdef ENABLE_CHECKING
258 /* The stream to which debugging output should be written. */
259 static FILE *cp_lexer_debug_stream
;
260 #endif /* ENABLE_CHECKING */
262 /* Create a new main C++ lexer, the lexer that gets tokens from the
266 cp_lexer_new_main (void)
268 cp_token first_token
;
275 /* It's possible that parsing the first pragma will load a PCH file,
276 which is a GC collection point. So we have to do that before
277 allocating any memory. */
278 cp_parser_initial_pragma (&first_token
);
280 c_common_no_more_pch ();
282 /* Allocate the memory. */
283 lexer
= GGC_CNEW (cp_lexer
);
285 #ifdef ENABLE_CHECKING
286 /* Initially we are not debugging. */
287 lexer
->debugging_p
= false;
288 #endif /* ENABLE_CHECKING */
289 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
290 CP_SAVED_TOKEN_STACK
);
292 /* Create the buffer. */
293 alloc
= CP_LEXER_BUFFER_SIZE
;
294 buffer
= GGC_NEWVEC (cp_token
, alloc
);
296 /* Put the first token in the buffer. */
301 /* Get the remaining tokens from the preprocessor. */
302 while (pos
->type
!= CPP_EOF
)
309 buffer
= GGC_RESIZEVEC (cp_token
, buffer
, alloc
);
310 pos
= buffer
+ space
;
312 cp_lexer_get_preprocessor_token (lexer
, pos
);
314 lexer
->buffer
= buffer
;
315 lexer
->buffer_length
= alloc
- space
;
316 lexer
->last_token
= pos
;
317 lexer
->next_token
= lexer
->buffer_length
? buffer
: &eof_token
;
319 /* Subsequent preprocessor diagnostics should use compiler
320 diagnostic functions to get the compiler source location. */
321 cpp_get_options (parse_in
)->client_diagnostic
= true;
322 cpp_get_callbacks (parse_in
)->error
= cp_cpp_error
;
324 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
328 /* Create a new lexer whose token stream is primed with the tokens in
329 CACHE. When these tokens are exhausted, no new tokens will be read. */
332 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
334 cp_token
*first
= cache
->first
;
335 cp_token
*last
= cache
->last
;
336 cp_lexer
*lexer
= GGC_CNEW (cp_lexer
);
338 /* We do not own the buffer. */
339 lexer
->buffer
= NULL
;
340 lexer
->buffer_length
= 0;
341 lexer
->next_token
= first
== last
? &eof_token
: first
;
342 lexer
->last_token
= last
;
344 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
345 CP_SAVED_TOKEN_STACK
);
347 #ifdef ENABLE_CHECKING
348 /* Initially we are not debugging. */
349 lexer
->debugging_p
= false;
352 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
356 /* Frees all resources associated with LEXER. */
359 cp_lexer_destroy (cp_lexer
*lexer
)
362 ggc_free (lexer
->buffer
);
363 VEC_free (cp_token_position
, heap
, lexer
->saved_tokens
);
367 /* Returns nonzero if debugging information should be output. */
369 #ifdef ENABLE_CHECKING
372 cp_lexer_debugging_p (cp_lexer
*lexer
)
374 return lexer
->debugging_p
;
377 #endif /* ENABLE_CHECKING */
379 static inline cp_token_position
380 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
382 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
384 return lexer
->next_token
- previous_p
;
387 static inline cp_token
*
388 cp_lexer_token_at (cp_lexer
*lexer ATTRIBUTE_UNUSED
, cp_token_position pos
)
393 /* nonzero if we are presently saving tokens. */
396 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
398 return VEC_length (cp_token_position
, lexer
->saved_tokens
) != 0;
401 /* Store the next token from the preprocessor in *TOKEN. Return true
402 if we reach EOF. If LEXER is NULL, assume we are handling an
403 initial #pragma pch_preprocess, and thus want the lexer to return
404 processed strings. */
407 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
409 static int is_extern_c
= 0;
411 /* Get a new token from the preprocessor. */
413 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
414 lexer
== NULL
? 0 : C_LEX_RAW_STRINGS
);
415 token
->input_file_stack_index
= input_file_stack_tick
;
416 token
->keyword
= RID_MAX
;
417 token
->pragma_kind
= PRAGMA_NONE
;
418 token
->in_system_header
= in_system_header
;
420 /* On some systems, some header files are surrounded by an
421 implicit extern "C" block. Set a flag in the token if it
422 comes from such a header. */
423 is_extern_c
+= pending_lang_change
;
424 pending_lang_change
= 0;
425 token
->implicit_extern_c
= is_extern_c
> 0;
427 /* Check to see if this token is a keyword. */
428 if (token
->type
== CPP_NAME
)
430 if (C_IS_RESERVED_WORD (token
->u
.value
))
432 /* Mark this token as a keyword. */
433 token
->type
= CPP_KEYWORD
;
434 /* Record which keyword. */
435 token
->keyword
= C_RID_CODE (token
->u
.value
);
436 /* Update the value. Some keywords are mapped to particular
437 entities, rather than simply having the value of the
438 corresponding IDENTIFIER_NODE. For example, `__const' is
439 mapped to `const'. */
440 token
->u
.value
= ridpointers
[token
->keyword
];
444 if (warn_cxx0x_compat
445 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
446 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
448 /* Warn about the C++0x keyword (but still treat it as
450 warning (OPT_Wc__0x_compat
,
451 "identifier %<%s%> will become a keyword in C++0x",
452 IDENTIFIER_POINTER (token
->u
.value
));
454 /* Clear out the C_RID_CODE so we don't warn about this
455 particular identifier-turned-keyword again. */
456 C_RID_CODE (token
->u
.value
) = RID_MAX
;
459 token
->ambiguous_p
= false;
460 token
->keyword
= RID_MAX
;
463 /* Handle Objective-C++ keywords. */
464 else if (token
->type
== CPP_AT_NAME
)
466 token
->type
= CPP_KEYWORD
;
467 switch (C_RID_CODE (token
->u
.value
))
469 /* Map 'class' to '@class', 'private' to '@private', etc. */
470 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
471 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
472 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
473 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
474 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
475 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
476 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
477 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
480 else if (token
->type
== CPP_PRAGMA
)
482 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
483 token
->pragma_kind
= TREE_INT_CST_LOW (token
->u
.value
);
484 token
->u
.value
= NULL_TREE
;
488 /* Update the globals input_location and in_system_header and the
489 input file stack from TOKEN. */
491 cp_lexer_set_source_position_from_token (cp_token
*token
)
493 if (token
->type
!= CPP_EOF
)
495 input_location
= token
->location
;
496 in_system_header
= token
->in_system_header
;
497 restore_input_file_stack (token
->input_file_stack_index
);
501 /* Return a pointer to the next token in the token stream, but do not
504 static inline cp_token
*
505 cp_lexer_peek_token (cp_lexer
*lexer
)
507 if (cp_lexer_debugging_p (lexer
))
509 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
510 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
511 putc ('\n', cp_lexer_debug_stream
);
513 return lexer
->next_token
;
516 /* Return true if the next token has the indicated TYPE. */
519 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
521 return cp_lexer_peek_token (lexer
)->type
== type
;
524 /* Return true if the next token does not have the indicated TYPE. */
527 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
529 return !cp_lexer_next_token_is (lexer
, type
);
532 /* Return true if the next token is the indicated KEYWORD. */
535 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
537 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
540 /* Return true if the next token is a keyword for a decl-specifier. */
543 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
547 token
= cp_lexer_peek_token (lexer
);
548 switch (token
->keyword
)
550 /* Storage classes. */
557 /* Elaborated type specifiers. */
563 /* Simple type specifiers. */
575 /* GNU extensions. */
578 /* C++0x extensions. */
587 /* Return a pointer to the Nth token in the token stream. If N is 1,
588 then this is precisely equivalent to cp_lexer_peek_token (except
589 that it is not inline). One would like to disallow that case, but
590 there is one case (cp_parser_nth_token_starts_template_id) where
591 the caller passes a variable for N and it might be 1. */
594 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
598 /* N is 1-based, not zero-based. */
601 if (cp_lexer_debugging_p (lexer
))
602 fprintf (cp_lexer_debug_stream
,
603 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
606 token
= lexer
->next_token
;
607 gcc_assert (!n
|| token
!= &eof_token
);
611 if (token
== lexer
->last_token
)
617 if (token
->type
!= CPP_PURGED
)
621 if (cp_lexer_debugging_p (lexer
))
623 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
624 putc ('\n', cp_lexer_debug_stream
);
630 /* Return the next token, and advance the lexer's next_token pointer
631 to point to the next non-purged token. */
634 cp_lexer_consume_token (cp_lexer
* lexer
)
636 cp_token
*token
= lexer
->next_token
;
638 gcc_assert (token
!= &eof_token
);
639 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
644 if (lexer
->next_token
== lexer
->last_token
)
646 lexer
->next_token
= &eof_token
;
651 while (lexer
->next_token
->type
== CPP_PURGED
);
653 cp_lexer_set_source_position_from_token (token
);
655 /* Provide debugging output. */
656 if (cp_lexer_debugging_p (lexer
))
658 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
659 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
660 putc ('\n', cp_lexer_debug_stream
);
666 /* Permanently remove the next token from the token stream, and
667 advance the next_token pointer to refer to the next non-purged
671 cp_lexer_purge_token (cp_lexer
*lexer
)
673 cp_token
*tok
= lexer
->next_token
;
675 gcc_assert (tok
!= &eof_token
);
676 tok
->type
= CPP_PURGED
;
677 tok
->location
= UNKNOWN_LOCATION
;
678 tok
->u
.value
= NULL_TREE
;
679 tok
->keyword
= RID_MAX
;
684 if (tok
== lexer
->last_token
)
690 while (tok
->type
== CPP_PURGED
);
691 lexer
->next_token
= tok
;
694 /* Permanently remove all tokens after TOK, up to, but not
695 including, the token that will be returned next by
696 cp_lexer_peek_token. */
699 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
701 cp_token
*peek
= lexer
->next_token
;
703 if (peek
== &eof_token
)
704 peek
= lexer
->last_token
;
706 gcc_assert (tok
< peek
);
708 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
710 tok
->type
= CPP_PURGED
;
711 tok
->location
= UNKNOWN_LOCATION
;
712 tok
->u
.value
= NULL_TREE
;
713 tok
->keyword
= RID_MAX
;
717 /* Begin saving tokens. All tokens consumed after this point will be
721 cp_lexer_save_tokens (cp_lexer
* lexer
)
723 /* Provide debugging output. */
724 if (cp_lexer_debugging_p (lexer
))
725 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
727 VEC_safe_push (cp_token_position
, heap
,
728 lexer
->saved_tokens
, lexer
->next_token
);
731 /* Commit to the portion of the token stream most recently saved. */
734 cp_lexer_commit_tokens (cp_lexer
* lexer
)
736 /* Provide debugging output. */
737 if (cp_lexer_debugging_p (lexer
))
738 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
740 VEC_pop (cp_token_position
, lexer
->saved_tokens
);
743 /* Return all tokens saved since the last call to cp_lexer_save_tokens
744 to the token stream. Stop saving tokens. */
747 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
749 /* Provide debugging output. */
750 if (cp_lexer_debugging_p (lexer
))
751 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
753 lexer
->next_token
= VEC_pop (cp_token_position
, lexer
->saved_tokens
);
756 /* Print a representation of the TOKEN on the STREAM. */
758 #ifdef ENABLE_CHECKING
761 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
763 /* We don't use cpp_type2name here because the parser defines
764 a few tokens of its own. */
765 static const char *const token_names
[] = {
766 /* cpplib-defined token types */
772 /* C++ parser token types - see "Manifest constants", above. */
775 "NESTED_NAME_SPECIFIER",
779 /* If we have a name for the token, print it out. Otherwise, we
780 simply give the numeric code. */
781 gcc_assert (token
->type
< ARRAY_SIZE(token_names
));
782 fputs (token_names
[token
->type
], stream
);
784 /* For some tokens, print the associated data. */
788 /* Some keywords have a value that is not an IDENTIFIER_NODE.
789 For example, `struct' is mapped to an INTEGER_CST. */
790 if (TREE_CODE (token
->u
.value
) != IDENTIFIER_NODE
)
792 /* else fall through */
794 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
799 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
807 /* Start emitting debugging information. */
810 cp_lexer_start_debugging (cp_lexer
* lexer
)
812 lexer
->debugging_p
= true;
815 /* Stop emitting debugging information. */
818 cp_lexer_stop_debugging (cp_lexer
* lexer
)
820 lexer
->debugging_p
= false;
823 #endif /* ENABLE_CHECKING */
825 /* Create a new cp_token_cache, representing a range of tokens. */
827 static cp_token_cache
*
828 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
830 cp_token_cache
*cache
= GGC_NEW (cp_token_cache
);
831 cache
->first
= first
;
837 /* Decl-specifiers. */
839 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
842 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
844 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
849 /* Nothing other than the parser should be creating declarators;
850 declarators are a semi-syntactic representation of C++ entities.
851 Other parts of the front end that need to create entities (like
852 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
854 static cp_declarator
*make_call_declarator
855 (cp_declarator
*, cp_parameter_declarator
*, cp_cv_quals
, tree
);
856 static cp_declarator
*make_array_declarator
857 (cp_declarator
*, tree
);
858 static cp_declarator
*make_pointer_declarator
859 (cp_cv_quals
, cp_declarator
*);
860 static cp_declarator
*make_reference_declarator
861 (cp_cv_quals
, cp_declarator
*, bool);
862 static cp_parameter_declarator
*make_parameter_declarator
863 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
864 static cp_declarator
*make_ptrmem_declarator
865 (cp_cv_quals
, tree
, cp_declarator
*);
867 /* An erroneous declarator. */
868 static cp_declarator
*cp_error_declarator
;
870 /* The obstack on which declarators and related data structures are
872 static struct obstack declarator_obstack
;
874 /* Alloc BYTES from the declarator memory pool. */
877 alloc_declarator (size_t bytes
)
879 return obstack_alloc (&declarator_obstack
, bytes
);
882 /* Allocate a declarator of the indicated KIND. Clear fields that are
883 common to all declarators. */
885 static cp_declarator
*
886 make_declarator (cp_declarator_kind kind
)
888 cp_declarator
*declarator
;
890 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
891 declarator
->kind
= kind
;
892 declarator
->attributes
= NULL_TREE
;
893 declarator
->declarator
= NULL
;
894 declarator
->parameter_pack_p
= false;
899 /* Make a declarator for a generalized identifier. If
900 QUALIFYING_SCOPE is non-NULL, the identifier is
901 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
902 UNQUALIFIED_NAME. SFK indicates the kind of special function this
905 static cp_declarator
*
906 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
907 special_function_kind sfk
)
909 cp_declarator
*declarator
;
911 /* It is valid to write:
913 class C { void f(); };
917 The standard is not clear about whether `typedef const C D' is
918 legal; as of 2002-09-15 the committee is considering that
919 question. EDG 3.0 allows that syntax. Therefore, we do as
921 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
922 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
924 gcc_assert (TREE_CODE (unqualified_name
) == IDENTIFIER_NODE
925 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
926 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
928 declarator
= make_declarator (cdk_id
);
929 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
930 declarator
->u
.id
.unqualified_name
= unqualified_name
;
931 declarator
->u
.id
.sfk
= sfk
;
936 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
937 of modifiers such as const or volatile to apply to the pointer
938 type, represented as identifiers. */
941 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
943 cp_declarator
*declarator
;
945 declarator
= make_declarator (cdk_pointer
);
946 declarator
->declarator
= target
;
947 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
948 declarator
->u
.pointer
.class_type
= NULL_TREE
;
951 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
952 target
->parameter_pack_p
= false;
955 declarator
->parameter_pack_p
= false;
960 /* Like make_pointer_declarator -- but for references. */
963 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
966 cp_declarator
*declarator
;
968 declarator
= make_declarator (cdk_reference
);
969 declarator
->declarator
= target
;
970 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
971 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
974 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
975 target
->parameter_pack_p
= false;
978 declarator
->parameter_pack_p
= false;
983 /* Like make_pointer_declarator -- but for a pointer to a non-static
984 member of CLASS_TYPE. */
987 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
988 cp_declarator
*pointee
)
990 cp_declarator
*declarator
;
992 declarator
= make_declarator (cdk_ptrmem
);
993 declarator
->declarator
= pointee
;
994 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
995 declarator
->u
.pointer
.class_type
= class_type
;
999 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1000 pointee
->parameter_pack_p
= false;
1003 declarator
->parameter_pack_p
= false;
1008 /* Make a declarator for the function given by TARGET, with the
1009 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1010 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1011 indicates what exceptions can be thrown. */
1014 make_call_declarator (cp_declarator
*target
,
1015 cp_parameter_declarator
*parms
,
1016 cp_cv_quals cv_qualifiers
,
1017 tree exception_specification
)
1019 cp_declarator
*declarator
;
1021 declarator
= make_declarator (cdk_function
);
1022 declarator
->declarator
= target
;
1023 declarator
->u
.function
.parameters
= parms
;
1024 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1025 declarator
->u
.function
.exception_specification
= exception_specification
;
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
)
1081 declarator
= declarator
->declarator
;
1089 cp_parameter_declarator
*no_parameters
;
1091 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1092 DECLARATOR and DEFAULT_ARGUMENT. */
1094 cp_parameter_declarator
*
1095 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1096 cp_declarator
*declarator
,
1097 tree default_argument
)
1099 cp_parameter_declarator
*parameter
;
1101 parameter
= ((cp_parameter_declarator
*)
1102 alloc_declarator (sizeof (cp_parameter_declarator
)));
1103 parameter
->next
= NULL
;
1104 if (decl_specifiers
)
1105 parameter
->decl_specifiers
= *decl_specifiers
;
1107 clear_decl_specs (¶meter
->decl_specifiers
);
1108 parameter
->declarator
= declarator
;
1109 parameter
->default_argument
= default_argument
;
1110 parameter
->ellipsis_p
= false;
1115 /* Returns true iff DECLARATOR is a declaration for a function. */
1118 function_declarator_p (const cp_declarator
*declarator
)
1122 if (declarator
->kind
== cdk_function
1123 && declarator
->declarator
->kind
== cdk_id
)
1125 if (declarator
->kind
== cdk_id
1126 || declarator
->kind
== cdk_error
)
1128 declarator
= declarator
->declarator
;
1138 A cp_parser parses the token stream as specified by the C++
1139 grammar. Its job is purely parsing, not semantic analysis. For
1140 example, the parser breaks the token stream into declarators,
1141 expressions, statements, and other similar syntactic constructs.
1142 It does not check that the types of the expressions on either side
1143 of an assignment-statement are compatible, or that a function is
1144 not declared with a parameter of type `void'.
1146 The parser invokes routines elsewhere in the compiler to perform
1147 semantic analysis and to build up the abstract syntax tree for the
1150 The parser (and the template instantiation code, which is, in a
1151 way, a close relative of parsing) are the only parts of the
1152 compiler that should be calling push_scope and pop_scope, or
1153 related functions. The parser (and template instantiation code)
1154 keeps track of what scope is presently active; everything else
1155 should simply honor that. (The code that generates static
1156 initializers may also need to set the scope, in order to check
1157 access control correctly when emitting the initializers.)
1162 The parser is of the standard recursive-descent variety. Upcoming
1163 tokens in the token stream are examined in order to determine which
1164 production to use when parsing a non-terminal. Some C++ constructs
1165 require arbitrary look ahead to disambiguate. For example, it is
1166 impossible, in the general case, to tell whether a statement is an
1167 expression or declaration without scanning the entire statement.
1168 Therefore, the parser is capable of "parsing tentatively." When the
1169 parser is not sure what construct comes next, it enters this mode.
1170 Then, while we attempt to parse the construct, the parser queues up
1171 error messages, rather than issuing them immediately, and saves the
1172 tokens it consumes. If the construct is parsed successfully, the
1173 parser "commits", i.e., it issues any queued error messages and
1174 the tokens that were being preserved are permanently discarded.
1175 If, however, the construct is not parsed successfully, the parser
1176 rolls back its state completely so that it can resume parsing using
1177 a different alternative.
1182 The performance of the parser could probably be improved substantially.
1183 We could often eliminate the need to parse tentatively by looking ahead
1184 a little bit. In some places, this approach might not entirely eliminate
1185 the need to parse tentatively, but it might still speed up the average
1188 /* Flags that are passed to some parsing functions. These values can
1189 be bitwise-ored together. */
1191 typedef enum cp_parser_flags
1194 CP_PARSER_FLAGS_NONE
= 0x0,
1195 /* The construct is optional. If it is not present, then no error
1196 should be issued. */
1197 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1198 /* When parsing a type-specifier, do not allow user-defined types. */
1199 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1202 /* The different kinds of declarators we want to parse. */
1204 typedef enum cp_parser_declarator_kind
1206 /* We want an abstract declarator. */
1207 CP_PARSER_DECLARATOR_ABSTRACT
,
1208 /* We want a named declarator. */
1209 CP_PARSER_DECLARATOR_NAMED
,
1210 /* We don't mind, but the name must be an unqualified-id. */
1211 CP_PARSER_DECLARATOR_EITHER
1212 } cp_parser_declarator_kind
;
1214 /* The precedence values used to parse binary expressions. The minimum value
1215 of PREC must be 1, because zero is reserved to quickly discriminate
1216 binary operators from other tokens. */
1221 PREC_LOGICAL_OR_EXPRESSION
,
1222 PREC_LOGICAL_AND_EXPRESSION
,
1223 PREC_INCLUSIVE_OR_EXPRESSION
,
1224 PREC_EXCLUSIVE_OR_EXPRESSION
,
1225 PREC_AND_EXPRESSION
,
1226 PREC_EQUALITY_EXPRESSION
,
1227 PREC_RELATIONAL_EXPRESSION
,
1228 PREC_SHIFT_EXPRESSION
,
1229 PREC_ADDITIVE_EXPRESSION
,
1230 PREC_MULTIPLICATIVE_EXPRESSION
,
1232 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1235 /* A mapping from a token type to a corresponding tree node type, with a
1236 precedence value. */
1238 typedef struct cp_parser_binary_operations_map_node
1240 /* The token type. */
1241 enum cpp_ttype token_type
;
1242 /* The corresponding tree code. */
1243 enum tree_code tree_type
;
1244 /* The precedence of this operator. */
1245 enum cp_parser_prec prec
;
1246 } cp_parser_binary_operations_map_node
;
1248 /* The status of a tentative parse. */
1250 typedef enum cp_parser_status_kind
1252 /* No errors have occurred. */
1253 CP_PARSER_STATUS_KIND_NO_ERROR
,
1254 /* An error has occurred. */
1255 CP_PARSER_STATUS_KIND_ERROR
,
1256 /* We are committed to this tentative parse, whether or not an error
1258 CP_PARSER_STATUS_KIND_COMMITTED
1259 } cp_parser_status_kind
;
1261 typedef struct cp_parser_expression_stack_entry
1263 /* Left hand side of the binary operation we are currently
1266 /* Original tree code for left hand side, if it was a binary
1267 expression itself (used for -Wparentheses). */
1268 enum tree_code lhs_type
;
1269 /* Tree code for the binary operation we are parsing. */
1270 enum tree_code tree_type
;
1271 /* Precedence of the binary operation we are parsing. */
1273 } cp_parser_expression_stack_entry
;
1275 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1276 entries because precedence levels on the stack are monotonically
1278 typedef struct cp_parser_expression_stack_entry
1279 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1281 /* Context that is saved and restored when parsing tentatively. */
1282 typedef struct cp_parser_context
GTY (())
1284 /* If this is a tentative parsing context, the status of the
1286 enum cp_parser_status_kind status
;
1287 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1288 that are looked up in this context must be looked up both in the
1289 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1290 the context of the containing expression. */
1293 /* The next parsing context in the stack. */
1294 struct cp_parser_context
*next
;
1295 } cp_parser_context
;
1299 /* Constructors and destructors. */
1301 static cp_parser_context
*cp_parser_context_new
1302 (cp_parser_context
*);
1304 /* Class variables. */
1306 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1308 /* The operator-precedence table used by cp_parser_binary_expression.
1309 Transformed into an associative array (binops_by_token) by
1312 static const cp_parser_binary_operations_map_node binops
[] = {
1313 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1314 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1316 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1317 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1318 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1320 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1321 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1323 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1324 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1326 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1327 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1328 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1329 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1331 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1332 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1334 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1336 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1338 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1340 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1342 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1345 /* The same as binops, but initialized by cp_parser_new so that
1346 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1348 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1350 /* Constructors and destructors. */
1352 /* Construct a new context. The context below this one on the stack
1353 is given by NEXT. */
1355 static cp_parser_context
*
1356 cp_parser_context_new (cp_parser_context
* next
)
1358 cp_parser_context
*context
;
1360 /* Allocate the storage. */
1361 if (cp_parser_context_free_list
!= NULL
)
1363 /* Pull the first entry from the free list. */
1364 context
= cp_parser_context_free_list
;
1365 cp_parser_context_free_list
= context
->next
;
1366 memset (context
, 0, sizeof (*context
));
1369 context
= GGC_CNEW (cp_parser_context
);
1371 /* No errors have occurred yet in this context. */
1372 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1373 /* If this is not the bottomost context, copy information that we
1374 need from the previous context. */
1377 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1378 expression, then we are parsing one in this context, too. */
1379 context
->object_type
= next
->object_type
;
1380 /* Thread the stack. */
1381 context
->next
= next
;
1387 /* The cp_parser structure represents the C++ parser. */
1389 typedef struct cp_parser
GTY(())
1391 /* The lexer from which we are obtaining tokens. */
1394 /* The scope in which names should be looked up. If NULL_TREE, then
1395 we look up names in the scope that is currently open in the
1396 source program. If non-NULL, this is either a TYPE or
1397 NAMESPACE_DECL for the scope in which we should look. It can
1398 also be ERROR_MARK, when we've parsed a bogus scope.
1400 This value is not cleared automatically after a name is looked
1401 up, so we must be careful to clear it before starting a new look
1402 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1403 will look up `Z' in the scope of `X', rather than the current
1404 scope.) Unfortunately, it is difficult to tell when name lookup
1405 is complete, because we sometimes peek at a token, look it up,
1406 and then decide not to consume it. */
1409 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1410 last lookup took place. OBJECT_SCOPE is used if an expression
1411 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1412 respectively. QUALIFYING_SCOPE is used for an expression of the
1413 form "X::Y"; it refers to X. */
1415 tree qualifying_scope
;
1417 /* A stack of parsing contexts. All but the bottom entry on the
1418 stack will be tentative contexts.
1420 We parse tentatively in order to determine which construct is in
1421 use in some situations. For example, in order to determine
1422 whether a statement is an expression-statement or a
1423 declaration-statement we parse it tentatively as a
1424 declaration-statement. If that fails, we then reparse the same
1425 token stream as an expression-statement. */
1426 cp_parser_context
*context
;
1428 /* True if we are parsing GNU C++. If this flag is not set, then
1429 GNU extensions are not recognized. */
1430 bool allow_gnu_extensions_p
;
1432 /* TRUE if the `>' token should be interpreted as the greater-than
1433 operator. FALSE if it is the end of a template-id or
1434 template-parameter-list. In C++0x mode, this flag also applies to
1435 `>>' tokens, which are viewed as two consecutive `>' tokens when
1436 this flag is FALSE. */
1437 bool greater_than_is_operator_p
;
1439 /* TRUE if default arguments are allowed within a parameter list
1440 that starts at this point. FALSE if only a gnu extension makes
1441 them permissible. */
1442 bool default_arg_ok_p
;
1444 /* TRUE if we are parsing an integral constant-expression. See
1445 [expr.const] for a precise definition. */
1446 bool integral_constant_expression_p
;
1448 /* TRUE if we are parsing an integral constant-expression -- but a
1449 non-constant expression should be permitted as well. This flag
1450 is used when parsing an array bound so that GNU variable-length
1451 arrays are tolerated. */
1452 bool allow_non_integral_constant_expression_p
;
1454 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1455 been seen that makes the expression non-constant. */
1456 bool non_integral_constant_expression_p
;
1458 /* TRUE if local variable names and `this' are forbidden in the
1460 bool local_variables_forbidden_p
;
1462 /* TRUE if the declaration we are parsing is part of a
1463 linkage-specification of the form `extern string-literal
1465 bool in_unbraced_linkage_specification_p
;
1467 /* TRUE if we are presently parsing a declarator, after the
1468 direct-declarator. */
1469 bool in_declarator_p
;
1471 /* TRUE if we are presently parsing a template-argument-list. */
1472 bool in_template_argument_list_p
;
1474 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1475 to IN_OMP_BLOCK if parsing OpenMP structured block and
1476 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1477 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1478 iteration-statement, OpenMP block or loop within that switch. */
1479 #define IN_SWITCH_STMT 1
1480 #define IN_ITERATION_STMT 2
1481 #define IN_OMP_BLOCK 4
1482 #define IN_OMP_FOR 8
1483 #define IN_IF_STMT 16
1484 unsigned char in_statement
;
1486 /* TRUE if we are presently parsing the body of a switch statement.
1487 Note that this doesn't quite overlap with in_statement above.
1488 The difference relates to giving the right sets of error messages:
1489 "case not in switch" vs "break statement used with OpenMP...". */
1490 bool in_switch_statement_p
;
1492 /* TRUE if we are parsing a type-id in an expression context. In
1493 such a situation, both "type (expr)" and "type (type)" are valid
1495 bool in_type_id_in_expr_p
;
1497 /* TRUE if we are currently in a header file where declarations are
1498 implicitly extern "C". */
1499 bool implicit_extern_c
;
1501 /* TRUE if strings in expressions should be translated to the execution
1503 bool translate_strings_p
;
1505 /* TRUE if we are presently parsing the body of a function, but not
1507 bool in_function_body
;
1509 /* If non-NULL, then we are parsing a construct where new type
1510 definitions are not permitted. The string stored here will be
1511 issued as an error message if a type is defined. */
1512 const char *type_definition_forbidden_message
;
1514 /* A list of lists. The outer list is a stack, used for member
1515 functions of local classes. At each level there are two sub-list,
1516 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1517 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1518 TREE_VALUE's. The functions are chained in reverse declaration
1521 The TREE_PURPOSE sublist contains those functions with default
1522 arguments that need post processing, and the TREE_VALUE sublist
1523 contains those functions with definitions that need post
1526 These lists can only be processed once the outermost class being
1527 defined is complete. */
1528 tree unparsed_functions_queues
;
1530 /* The number of classes whose definitions are currently in
1532 unsigned num_classes_being_defined
;
1534 /* The number of template parameter lists that apply directly to the
1535 current declaration. */
1536 unsigned num_template_parameter_lists
;
1541 /* Constructors and destructors. */
1543 static cp_parser
*cp_parser_new
1546 /* Routines to parse various constructs.
1548 Those that return `tree' will return the error_mark_node (rather
1549 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1550 Sometimes, they will return an ordinary node if error-recovery was
1551 attempted, even though a parse error occurred. So, to check
1552 whether or not a parse error occurred, you should always use
1553 cp_parser_error_occurred. If the construct is optional (indicated
1554 either by an `_opt' in the name of the function that does the
1555 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1556 the construct is not present. */
1558 /* Lexical conventions [gram.lex] */
1560 static tree cp_parser_identifier
1562 static tree cp_parser_string_literal
1563 (cp_parser
*, bool, bool);
1565 /* Basic concepts [gram.basic] */
1567 static bool cp_parser_translation_unit
1570 /* Expressions [gram.expr] */
1572 static tree cp_parser_primary_expression
1573 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1574 static tree cp_parser_id_expression
1575 (cp_parser
*, bool, bool, bool *, bool, bool);
1576 static tree cp_parser_unqualified_id
1577 (cp_parser
*, bool, bool, bool, bool);
1578 static tree cp_parser_nested_name_specifier_opt
1579 (cp_parser
*, bool, bool, bool, bool);
1580 static tree cp_parser_nested_name_specifier
1581 (cp_parser
*, bool, bool, bool, bool);
1582 static tree cp_parser_class_or_namespace_name
1583 (cp_parser
*, bool, bool, bool, bool, bool);
1584 static tree cp_parser_postfix_expression
1585 (cp_parser
*, bool, bool, bool);
1586 static tree cp_parser_postfix_open_square_expression
1587 (cp_parser
*, tree
, bool);
1588 static tree cp_parser_postfix_dot_deref_expression
1589 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*);
1590 static tree cp_parser_parenthesized_expression_list
1591 (cp_parser
*, bool, bool, bool, bool *);
1592 static void cp_parser_pseudo_destructor_name
1593 (cp_parser
*, tree
*, tree
*);
1594 static tree cp_parser_unary_expression
1595 (cp_parser
*, bool, bool);
1596 static enum tree_code cp_parser_unary_operator
1598 static tree cp_parser_new_expression
1600 static tree cp_parser_new_placement
1602 static tree cp_parser_new_type_id
1603 (cp_parser
*, tree
*);
1604 static cp_declarator
*cp_parser_new_declarator_opt
1606 static cp_declarator
*cp_parser_direct_new_declarator
1608 static tree cp_parser_new_initializer
1610 static tree cp_parser_delete_expression
1612 static tree cp_parser_cast_expression
1613 (cp_parser
*, bool, bool);
1614 static tree cp_parser_binary_expression
1615 (cp_parser
*, bool);
1616 static tree cp_parser_question_colon_clause
1617 (cp_parser
*, tree
);
1618 static tree cp_parser_assignment_expression
1619 (cp_parser
*, bool);
1620 static enum tree_code cp_parser_assignment_operator_opt
1622 static tree cp_parser_expression
1623 (cp_parser
*, bool);
1624 static tree cp_parser_constant_expression
1625 (cp_parser
*, bool, bool *);
1626 static tree cp_parser_builtin_offsetof
1629 /* Statements [gram.stmt.stmt] */
1631 static void cp_parser_statement
1632 (cp_parser
*, tree
, bool, bool *);
1633 static void cp_parser_label_for_labeled_statement
1635 static tree cp_parser_expression_statement
1636 (cp_parser
*, tree
);
1637 static tree cp_parser_compound_statement
1638 (cp_parser
*, tree
, bool);
1639 static void cp_parser_statement_seq_opt
1640 (cp_parser
*, tree
);
1641 static tree cp_parser_selection_statement
1642 (cp_parser
*, bool *);
1643 static tree cp_parser_condition
1645 static tree cp_parser_iteration_statement
1647 static void cp_parser_for_init_statement
1649 static tree cp_parser_jump_statement
1651 static void cp_parser_declaration_statement
1654 static tree cp_parser_implicitly_scoped_statement
1655 (cp_parser
*, bool *);
1656 static void cp_parser_already_scoped_statement
1659 /* Declarations [gram.dcl.dcl] */
1661 static void cp_parser_declaration_seq_opt
1663 static void cp_parser_declaration
1665 static void cp_parser_block_declaration
1666 (cp_parser
*, bool);
1667 static void cp_parser_simple_declaration
1668 (cp_parser
*, bool);
1669 static void cp_parser_decl_specifier_seq
1670 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1671 static tree cp_parser_storage_class_specifier_opt
1673 static tree cp_parser_function_specifier_opt
1674 (cp_parser
*, cp_decl_specifier_seq
*);
1675 static tree cp_parser_type_specifier
1676 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1678 static tree cp_parser_simple_type_specifier
1679 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1680 static tree cp_parser_type_name
1682 static tree cp_parser_elaborated_type_specifier
1683 (cp_parser
*, bool, bool);
1684 static tree cp_parser_enum_specifier
1686 static void cp_parser_enumerator_list
1687 (cp_parser
*, tree
);
1688 static void cp_parser_enumerator_definition
1689 (cp_parser
*, tree
);
1690 static tree cp_parser_namespace_name
1692 static void cp_parser_namespace_definition
1694 static void cp_parser_namespace_body
1696 static tree cp_parser_qualified_namespace_specifier
1698 static void cp_parser_namespace_alias_definition
1700 static bool cp_parser_using_declaration
1701 (cp_parser
*, bool);
1702 static void cp_parser_using_directive
1704 static void cp_parser_asm_definition
1706 static void cp_parser_linkage_specification
1708 static void cp_parser_static_assert
1709 (cp_parser
*, bool);
1710 static tree cp_parser_decltype
1713 /* Declarators [gram.dcl.decl] */
1715 static tree cp_parser_init_declarator
1716 (cp_parser
*, cp_decl_specifier_seq
*, VEC (deferred_access_check
,gc
)*, bool, bool, int, bool *);
1717 static cp_declarator
*cp_parser_declarator
1718 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1719 static cp_declarator
*cp_parser_direct_declarator
1720 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1721 static enum tree_code cp_parser_ptr_operator
1722 (cp_parser
*, tree
*, cp_cv_quals
*);
1723 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1725 static tree cp_parser_declarator_id
1726 (cp_parser
*, bool);
1727 static tree cp_parser_type_id
1729 static void cp_parser_type_specifier_seq
1730 (cp_parser
*, bool, cp_decl_specifier_seq
*);
1731 static cp_parameter_declarator
*cp_parser_parameter_declaration_clause
1733 static cp_parameter_declarator
*cp_parser_parameter_declaration_list
1734 (cp_parser
*, bool *);
1735 static cp_parameter_declarator
*cp_parser_parameter_declaration
1736 (cp_parser
*, bool, bool *);
1737 static void cp_parser_function_body
1739 static tree cp_parser_initializer
1740 (cp_parser
*, bool *, bool *);
1741 static tree cp_parser_initializer_clause
1742 (cp_parser
*, bool *);
1743 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1744 (cp_parser
*, bool *);
1746 static bool cp_parser_ctor_initializer_opt_and_function_body
1749 /* Classes [gram.class] */
1751 static tree cp_parser_class_name
1752 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1753 static tree cp_parser_class_specifier
1755 static tree cp_parser_class_head
1756 (cp_parser
*, bool *, tree
*, tree
*);
1757 static enum tag_types cp_parser_class_key
1759 static void cp_parser_member_specification_opt
1761 static void cp_parser_member_declaration
1763 static tree cp_parser_pure_specifier
1765 static tree cp_parser_constant_initializer
1768 /* Derived classes [gram.class.derived] */
1770 static tree cp_parser_base_clause
1772 static tree cp_parser_base_specifier
1775 /* Special member functions [gram.special] */
1777 static tree cp_parser_conversion_function_id
1779 static tree cp_parser_conversion_type_id
1781 static cp_declarator
*cp_parser_conversion_declarator_opt
1783 static bool cp_parser_ctor_initializer_opt
1785 static void cp_parser_mem_initializer_list
1787 static tree cp_parser_mem_initializer
1789 static tree cp_parser_mem_initializer_id
1792 /* Overloading [gram.over] */
1794 static tree cp_parser_operator_function_id
1796 static tree cp_parser_operator
1799 /* Templates [gram.temp] */
1801 static void cp_parser_template_declaration
1802 (cp_parser
*, bool);
1803 static tree cp_parser_template_parameter_list
1805 static tree cp_parser_template_parameter
1806 (cp_parser
*, bool *, bool *);
1807 static tree cp_parser_type_parameter
1808 (cp_parser
*, bool *);
1809 static tree cp_parser_template_id
1810 (cp_parser
*, bool, bool, bool);
1811 static tree cp_parser_template_name
1812 (cp_parser
*, bool, bool, bool, bool *);
1813 static tree cp_parser_template_argument_list
1815 static tree cp_parser_template_argument
1817 static void cp_parser_explicit_instantiation
1819 static void cp_parser_explicit_specialization
1822 /* Exception handling [gram.exception] */
1824 static tree cp_parser_try_block
1826 static bool cp_parser_function_try_block
1828 static void cp_parser_handler_seq
1830 static void cp_parser_handler
1832 static tree cp_parser_exception_declaration
1834 static tree cp_parser_throw_expression
1836 static tree cp_parser_exception_specification_opt
1838 static tree cp_parser_type_id_list
1841 /* GNU Extensions */
1843 static tree cp_parser_asm_specification_opt
1845 static tree cp_parser_asm_operand_list
1847 static tree cp_parser_asm_clobber_list
1849 static tree cp_parser_attributes_opt
1851 static tree cp_parser_attribute_list
1853 static bool cp_parser_extension_opt
1854 (cp_parser
*, int *);
1855 static void cp_parser_label_declaration
1858 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
1859 static bool cp_parser_pragma
1860 (cp_parser
*, enum pragma_context
);
1862 /* Objective-C++ Productions */
1864 static tree cp_parser_objc_message_receiver
1866 static tree cp_parser_objc_message_args
1868 static tree cp_parser_objc_message_expression
1870 static tree cp_parser_objc_encode_expression
1872 static tree cp_parser_objc_defs_expression
1874 static tree cp_parser_objc_protocol_expression
1876 static tree cp_parser_objc_selector_expression
1878 static tree cp_parser_objc_expression
1880 static bool cp_parser_objc_selector_p
1882 static tree cp_parser_objc_selector
1884 static tree cp_parser_objc_protocol_refs_opt
1886 static void cp_parser_objc_declaration
1888 static tree cp_parser_objc_statement
1891 /* Utility Routines */
1893 static tree cp_parser_lookup_name
1894 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*);
1895 static tree cp_parser_lookup_name_simple
1896 (cp_parser
*, tree
);
1897 static tree cp_parser_maybe_treat_template_as_class
1899 static bool cp_parser_check_declarator_template_parameters
1900 (cp_parser
*, cp_declarator
*);
1901 static bool cp_parser_check_template_parameters
1902 (cp_parser
*, unsigned);
1903 static tree cp_parser_simple_cast_expression
1905 static tree cp_parser_global_scope_opt
1906 (cp_parser
*, bool);
1907 static bool cp_parser_constructor_declarator_p
1908 (cp_parser
*, bool);
1909 static tree cp_parser_function_definition_from_specifiers_and_declarator
1910 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1911 static tree cp_parser_function_definition_after_declarator
1912 (cp_parser
*, bool);
1913 static void cp_parser_template_declaration_after_export
1914 (cp_parser
*, bool);
1915 static void cp_parser_perform_template_parameter_access_checks
1916 (VEC (deferred_access_check
,gc
)*);
1917 static tree cp_parser_single_declaration
1918 (cp_parser
*, VEC (deferred_access_check
,gc
)*, bool, bool, bool *);
1919 static tree cp_parser_functional_cast
1920 (cp_parser
*, tree
);
1921 static tree cp_parser_save_member_function_body
1922 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1923 static tree cp_parser_enclosed_template_argument_list
1925 static void cp_parser_save_default_args
1926 (cp_parser
*, tree
);
1927 static void cp_parser_late_parsing_for_member
1928 (cp_parser
*, tree
);
1929 static void cp_parser_late_parsing_default_args
1930 (cp_parser
*, tree
);
1931 static tree cp_parser_sizeof_operand
1932 (cp_parser
*, enum rid
);
1933 static tree cp_parser_trait_expr
1934 (cp_parser
*, enum rid
);
1935 static bool cp_parser_declares_only_class_p
1937 static void cp_parser_set_storage_class
1938 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
);
1939 static void cp_parser_set_decl_spec_type
1940 (cp_decl_specifier_seq
*, tree
, bool);
1941 static bool cp_parser_friend_p
1942 (const cp_decl_specifier_seq
*);
1943 static cp_token
*cp_parser_require
1944 (cp_parser
*, enum cpp_ttype
, const char *);
1945 static cp_token
*cp_parser_require_keyword
1946 (cp_parser
*, enum rid
, const char *);
1947 static bool cp_parser_token_starts_function_definition_p
1949 static bool cp_parser_next_token_starts_class_definition_p
1951 static bool cp_parser_next_token_ends_template_argument_p
1953 static bool cp_parser_nth_token_starts_template_argument_list_p
1954 (cp_parser
*, size_t);
1955 static enum tag_types cp_parser_token_is_class_key
1957 static void cp_parser_check_class_key
1958 (enum tag_types
, tree type
);
1959 static void cp_parser_check_access_in_redeclaration
1961 static bool cp_parser_optional_template_keyword
1963 static void cp_parser_pre_parsed_nested_name_specifier
1965 static void cp_parser_cache_group
1966 (cp_parser
*, enum cpp_ttype
, unsigned);
1967 static void cp_parser_parse_tentatively
1969 static void cp_parser_commit_to_tentative_parse
1971 static void cp_parser_abort_tentative_parse
1973 static bool cp_parser_parse_definitely
1975 static inline bool cp_parser_parsing_tentatively
1977 static bool cp_parser_uncommitted_to_tentative_parse_p
1979 static void cp_parser_error
1980 (cp_parser
*, const char *);
1981 static void cp_parser_name_lookup_error
1982 (cp_parser
*, tree
, tree
, const char *);
1983 static bool cp_parser_simulate_error
1985 static bool cp_parser_check_type_definition
1987 static void cp_parser_check_for_definition_in_return_type
1988 (cp_declarator
*, tree
);
1989 static void cp_parser_check_for_invalid_template_id
1990 (cp_parser
*, tree
);
1991 static bool cp_parser_non_integral_constant_expression
1992 (cp_parser
*, const char *);
1993 static void cp_parser_diagnose_invalid_type_name
1994 (cp_parser
*, tree
, tree
);
1995 static bool cp_parser_parse_and_diagnose_invalid_type_name
1997 static int cp_parser_skip_to_closing_parenthesis
1998 (cp_parser
*, bool, bool, bool);
1999 static void cp_parser_skip_to_end_of_statement
2001 static void cp_parser_consume_semicolon_at_end_of_statement
2003 static void cp_parser_skip_to_end_of_block_or_statement
2005 static bool cp_parser_skip_to_closing_brace
2007 static void cp_parser_skip_to_end_of_template_parameter_list
2009 static void cp_parser_skip_to_pragma_eol
2010 (cp_parser
*, cp_token
*);
2011 static bool cp_parser_error_occurred
2013 static bool cp_parser_allow_gnu_extensions_p
2015 static bool cp_parser_is_string_literal
2017 static bool cp_parser_is_keyword
2018 (cp_token
*, enum rid
);
2019 static tree cp_parser_make_typename_type
2020 (cp_parser
*, tree
, tree
);
2021 static cp_declarator
* cp_parser_make_indirect_declarator
2022 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*);
2024 /* Returns nonzero if we are parsing tentatively. */
2027 cp_parser_parsing_tentatively (cp_parser
* parser
)
2029 return parser
->context
->next
!= NULL
;
2032 /* Returns nonzero if TOKEN is a string literal. */
2035 cp_parser_is_string_literal (cp_token
* token
)
2037 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
2040 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2043 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2045 return token
->keyword
== keyword
;
2048 /* If not parsing tentatively, issue a diagnostic of the form
2049 FILE:LINE: MESSAGE before TOKEN
2050 where TOKEN is the next token in the input stream. MESSAGE
2051 (specified by the caller) is usually of the form "expected
2055 cp_parser_error (cp_parser
* parser
, const char* message
)
2057 if (!cp_parser_simulate_error (parser
))
2059 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2060 /* This diagnostic makes more sense if it is tagged to the line
2061 of the token we just peeked at. */
2062 cp_lexer_set_source_position_from_token (token
);
2064 if (token
->type
== CPP_PRAGMA
)
2066 error ("%<#pragma%> is not allowed here");
2067 cp_parser_skip_to_pragma_eol (parser
, token
);
2071 c_parse_error (message
,
2072 /* Because c_parser_error does not understand
2073 CPP_KEYWORD, keywords are treated like
2075 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2080 /* Issue an error about name-lookup failing. NAME is the
2081 IDENTIFIER_NODE DECL is the result of
2082 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2083 the thing that we hoped to find. */
2086 cp_parser_name_lookup_error (cp_parser
* parser
,
2089 const char* desired
)
2091 /* If name lookup completely failed, tell the user that NAME was not
2093 if (decl
== error_mark_node
)
2095 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2096 error ("%<%E::%E%> has not been declared",
2097 parser
->scope
, name
);
2098 else if (parser
->scope
== global_namespace
)
2099 error ("%<::%E%> has not been declared", name
);
2100 else if (parser
->object_scope
2101 && !CLASS_TYPE_P (parser
->object_scope
))
2102 error ("request for member %qE in non-class type %qT",
2103 name
, parser
->object_scope
);
2104 else if (parser
->object_scope
)
2105 error ("%<%T::%E%> has not been declared",
2106 parser
->object_scope
, name
);
2108 error ("%qE has not been declared", name
);
2110 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2111 error ("%<%E::%E%> %s", parser
->scope
, name
, desired
);
2112 else if (parser
->scope
== global_namespace
)
2113 error ("%<::%E%> %s", name
, desired
);
2115 error ("%qE %s", name
, desired
);
2118 /* If we are parsing tentatively, remember that an error has occurred
2119 during this tentative parse. Returns true if the error was
2120 simulated; false if a message should be issued by the caller. */
2123 cp_parser_simulate_error (cp_parser
* parser
)
2125 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2127 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2133 /* Check for repeated decl-specifiers. */
2136 cp_parser_check_decl_spec (cp_decl_specifier_seq
*decl_specs
)
2140 for (ds
= ds_first
; ds
!= ds_last
; ++ds
)
2142 unsigned count
= decl_specs
->specs
[(int)ds
];
2145 /* The "long" specifier is a special case because of "long long". */
2149 error ("%<long long long%> is too long for GCC");
2150 else if (pedantic
&& !in_system_header
&& warn_long_long
)
2151 pedwarn ("ISO C++ does not support %<long long%>");
2155 static const char *const decl_spec_names
[] = {
2171 error ("duplicate %qs", decl_spec_names
[(int)ds
]);
2176 /* This function is called when a type is defined. If type
2177 definitions are forbidden at this point, an error message is
2181 cp_parser_check_type_definition (cp_parser
* parser
)
2183 /* If types are forbidden here, issue a message. */
2184 if (parser
->type_definition_forbidden_message
)
2186 /* Use `%s' to print the string in case there are any escape
2187 characters in the message. */
2188 error ("%s", parser
->type_definition_forbidden_message
);
2194 /* This function is called when the DECLARATOR is processed. The TYPE
2195 was a type defined in the decl-specifiers. If it is invalid to
2196 define a type in the decl-specifiers for DECLARATOR, an error is
2200 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2203 /* [dcl.fct] forbids type definitions in return types.
2204 Unfortunately, it's not easy to know whether or not we are
2205 processing a return type until after the fact. */
2207 && (declarator
->kind
== cdk_pointer
2208 || declarator
->kind
== cdk_reference
2209 || declarator
->kind
== cdk_ptrmem
))
2210 declarator
= declarator
->declarator
;
2212 && declarator
->kind
== cdk_function
)
2214 error ("new types may not be defined in a return type");
2215 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2220 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2221 "<" in any valid C++ program. If the next token is indeed "<",
2222 issue a message warning the user about what appears to be an
2223 invalid attempt to form a template-id. */
2226 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2229 cp_token_position start
= 0;
2231 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2234 error ("%qT is not a template", type
);
2235 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2236 error ("%qE is not a template", type
);
2238 error ("invalid template-id");
2239 /* Remember the location of the invalid "<". */
2240 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2241 start
= cp_lexer_token_position (parser
->lexer
, true);
2242 /* Consume the "<". */
2243 cp_lexer_consume_token (parser
->lexer
);
2244 /* Parse the template arguments. */
2245 cp_parser_enclosed_template_argument_list (parser
);
2246 /* Permanently remove the invalid template arguments so that
2247 this error message is not issued again. */
2249 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2253 /* If parsing an integral constant-expression, issue an error message
2254 about the fact that THING appeared and return true. Otherwise,
2255 return false. In either case, set
2256 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2259 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2262 parser
->non_integral_constant_expression_p
= true;
2263 if (parser
->integral_constant_expression_p
)
2265 if (!parser
->allow_non_integral_constant_expression_p
)
2267 error ("%s cannot appear in a constant-expression", thing
);
2274 /* Emit a diagnostic for an invalid type name. SCOPE is the
2275 qualifying scope (or NULL, if none) for ID. This function commits
2276 to the current active tentative parse, if any. (Otherwise, the
2277 problematic construct might be encountered again later, resulting
2278 in duplicate error messages.) */
2281 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree scope
, tree id
)
2283 tree decl
, old_scope
;
2284 /* Try to lookup the identifier. */
2285 old_scope
= parser
->scope
;
2286 parser
->scope
= scope
;
2287 decl
= cp_parser_lookup_name_simple (parser
, id
);
2288 parser
->scope
= old_scope
;
2289 /* If the lookup found a template-name, it means that the user forgot
2290 to specify an argument list. Emit a useful error message. */
2291 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2292 error ("invalid use of template-name %qE without an argument list", decl
);
2293 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2294 error ("invalid use of destructor %qD as a type", id
);
2295 else if (TREE_CODE (decl
) == TYPE_DECL
)
2296 /* Something like 'unsigned A a;' */
2297 error ("invalid combination of multiple type-specifiers");
2298 else if (!parser
->scope
)
2300 /* Issue an error message. */
2301 error ("%qE does not name a type", id
);
2302 /* If we're in a template class, it's possible that the user was
2303 referring to a type from a base class. For example:
2305 template <typename T> struct A { typedef T X; };
2306 template <typename T> struct B : public A<T> { X x; };
2308 The user should have said "typename A<T>::X". */
2309 if (processing_template_decl
&& current_class_type
2310 && TYPE_BINFO (current_class_type
))
2314 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2318 tree base_type
= BINFO_TYPE (b
);
2319 if (CLASS_TYPE_P (base_type
)
2320 && dependent_type_p (base_type
))
2323 /* Go from a particular instantiation of the
2324 template (which will have an empty TYPE_FIELDs),
2325 to the main version. */
2326 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2327 for (field
= TYPE_FIELDS (base_type
);
2329 field
= TREE_CHAIN (field
))
2330 if (TREE_CODE (field
) == TYPE_DECL
2331 && DECL_NAME (field
) == id
)
2333 inform ("(perhaps %<typename %T::%E%> was intended)",
2334 BINFO_TYPE (b
), id
);
2343 /* Here we diagnose qualified-ids where the scope is actually correct,
2344 but the identifier does not resolve to a valid type name. */
2345 else if (parser
->scope
!= error_mark_node
)
2347 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2348 error ("%qE in namespace %qE does not name a type",
2350 else if (TYPE_P (parser
->scope
))
2351 error ("%qE in class %qT does not name a type", id
, parser
->scope
);
2355 cp_parser_commit_to_tentative_parse (parser
);
2358 /* Check for a common situation where a type-name should be present,
2359 but is not, and issue a sensible error message. Returns true if an
2360 invalid type-name was detected.
2362 The situation handled by this function are variable declarations of the
2363 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2364 Usually, `ID' should name a type, but if we got here it means that it
2365 does not. We try to emit the best possible error message depending on
2366 how exactly the id-expression looks like. */
2369 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2373 cp_parser_parse_tentatively (parser
);
2374 id
= cp_parser_id_expression (parser
,
2375 /*template_keyword_p=*/false,
2376 /*check_dependency_p=*/true,
2377 /*template_p=*/NULL
,
2378 /*declarator_p=*/true,
2379 /*optional_p=*/false);
2380 /* After the id-expression, there should be a plain identifier,
2381 otherwise this is not a simple variable declaration. Also, if
2382 the scope is dependent, we cannot do much. */
2383 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2384 || (parser
->scope
&& TYPE_P (parser
->scope
)
2385 && dependent_type_p (parser
->scope
))
2386 || TREE_CODE (id
) == TYPE_DECL
)
2388 cp_parser_abort_tentative_parse (parser
);
2391 if (!cp_parser_parse_definitely (parser
))
2394 /* Emit a diagnostic for the invalid type. */
2395 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
, id
);
2396 /* Skip to the end of the declaration; there's no point in
2397 trying to process it. */
2398 cp_parser_skip_to_end_of_block_or_statement (parser
);
2402 /* Consume tokens up to, and including, the next non-nested closing `)'.
2403 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2404 are doing error recovery. Returns -1 if OR_COMMA is true and we
2405 found an unnested comma. */
2408 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2413 unsigned paren_depth
= 0;
2414 unsigned brace_depth
= 0;
2416 if (recovering
&& !or_comma
2417 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2422 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2424 switch (token
->type
)
2427 case CPP_PRAGMA_EOL
:
2428 /* If we've run out of tokens, then there is no closing `)'. */
2432 /* This matches the processing in skip_to_end_of_statement. */
2437 case CPP_OPEN_BRACE
:
2440 case CPP_CLOSE_BRACE
:
2446 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
)
2450 case CPP_OPEN_PAREN
:
2455 case CPP_CLOSE_PAREN
:
2456 if (!brace_depth
&& !paren_depth
--)
2459 cp_lexer_consume_token (parser
->lexer
);
2468 /* Consume the token. */
2469 cp_lexer_consume_token (parser
->lexer
);
2473 /* Consume tokens until we reach the end of the current statement.
2474 Normally, that will be just before consuming a `;'. However, if a
2475 non-nested `}' comes first, then we stop before consuming that. */
2478 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2480 unsigned nesting_depth
= 0;
2484 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2486 switch (token
->type
)
2489 case CPP_PRAGMA_EOL
:
2490 /* If we've run out of tokens, stop. */
2494 /* If the next token is a `;', we have reached the end of the
2500 case CPP_CLOSE_BRACE
:
2501 /* If this is a non-nested '}', stop before consuming it.
2502 That way, when confronted with something like:
2506 we stop before consuming the closing '}', even though we
2507 have not yet reached a `;'. */
2508 if (nesting_depth
== 0)
2511 /* If it is the closing '}' for a block that we have
2512 scanned, stop -- but only after consuming the token.
2518 we will stop after the body of the erroneously declared
2519 function, but before consuming the following `typedef'
2521 if (--nesting_depth
== 0)
2523 cp_lexer_consume_token (parser
->lexer
);
2527 case CPP_OPEN_BRACE
:
2535 /* Consume the token. */
2536 cp_lexer_consume_token (parser
->lexer
);
2540 /* This function is called at the end of a statement or declaration.
2541 If the next token is a semicolon, it is consumed; otherwise, error
2542 recovery is attempted. */
2545 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2547 /* Look for the trailing `;'. */
2548 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2550 /* If there is additional (erroneous) input, skip to the end of
2552 cp_parser_skip_to_end_of_statement (parser
);
2553 /* If the next token is now a `;', consume it. */
2554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2555 cp_lexer_consume_token (parser
->lexer
);
2559 /* Skip tokens until we have consumed an entire block, or until we
2560 have consumed a non-nested `;'. */
2563 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2565 int nesting_depth
= 0;
2567 while (nesting_depth
>= 0)
2569 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2571 switch (token
->type
)
2574 case CPP_PRAGMA_EOL
:
2575 /* If we've run out of tokens, stop. */
2579 /* Stop if this is an unnested ';'. */
2584 case CPP_CLOSE_BRACE
:
2585 /* Stop if this is an unnested '}', or closes the outermost
2592 case CPP_OPEN_BRACE
:
2601 /* Consume the token. */
2602 cp_lexer_consume_token (parser
->lexer
);
2606 /* Skip tokens until a non-nested closing curly brace is the next
2607 token, or there are no more tokens. Return true in the first case,
2611 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2613 unsigned nesting_depth
= 0;
2617 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2619 switch (token
->type
)
2622 case CPP_PRAGMA_EOL
:
2623 /* If we've run out of tokens, stop. */
2626 case CPP_CLOSE_BRACE
:
2627 /* If the next token is a non-nested `}', then we have reached
2628 the end of the current block. */
2629 if (nesting_depth
-- == 0)
2633 case CPP_OPEN_BRACE
:
2634 /* If it the next token is a `{', then we are entering a new
2635 block. Consume the entire block. */
2643 /* Consume the token. */
2644 cp_lexer_consume_token (parser
->lexer
);
2648 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2649 parameter is the PRAGMA token, allowing us to purge the entire pragma
2653 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
2657 parser
->lexer
->in_pragma
= false;
2660 token
= cp_lexer_consume_token (parser
->lexer
);
2661 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
2663 /* Ensure that the pragma is not parsed again. */
2664 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
2667 /* Require pragma end of line, resyncing with it as necessary. The
2668 arguments are as for cp_parser_skip_to_pragma_eol. */
2671 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
2673 parser
->lexer
->in_pragma
= false;
2674 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, "end of line"))
2675 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
2678 /* This is a simple wrapper around make_typename_type. When the id is
2679 an unresolved identifier node, we can provide a superior diagnostic
2680 using cp_parser_diagnose_invalid_type_name. */
2683 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
, tree id
)
2686 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2688 result
= make_typename_type (scope
, id
, typename_type
,
2689 /*complain=*/tf_none
);
2690 if (result
== error_mark_node
)
2691 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
);
2694 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2697 /* This is a wrapper around the
2698 make_{pointer,ptrmem,reference}_declarator functions that decides
2699 which one to call based on the CODE and CLASS_TYPE arguments. The
2700 CODE argument should be one of the values returned by
2701 cp_parser_ptr_operator. */
2702 static cp_declarator
*
2703 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
2704 cp_cv_quals cv_qualifiers
,
2705 cp_declarator
*target
)
2707 if (code
== ERROR_MARK
)
2708 return cp_error_declarator
;
2710 if (code
== INDIRECT_REF
)
2711 if (class_type
== NULL_TREE
)
2712 return make_pointer_declarator (cv_qualifiers
, target
);
2714 return make_ptrmem_declarator (cv_qualifiers
, class_type
, target
);
2715 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
2716 return make_reference_declarator (cv_qualifiers
, target
, false);
2717 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
2718 return make_reference_declarator (cv_qualifiers
, target
, true);
2722 /* Create a new C++ parser. */
2725 cp_parser_new (void)
2731 /* cp_lexer_new_main is called before calling ggc_alloc because
2732 cp_lexer_new_main might load a PCH file. */
2733 lexer
= cp_lexer_new_main ();
2735 /* Initialize the binops_by_token so that we can get the tree
2736 directly from the token. */
2737 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2738 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2740 parser
= GGC_CNEW (cp_parser
);
2741 parser
->lexer
= lexer
;
2742 parser
->context
= cp_parser_context_new (NULL
);
2744 /* For now, we always accept GNU extensions. */
2745 parser
->allow_gnu_extensions_p
= 1;
2747 /* The `>' token is a greater-than operator, not the end of a
2749 parser
->greater_than_is_operator_p
= true;
2751 parser
->default_arg_ok_p
= true;
2753 /* We are not parsing a constant-expression. */
2754 parser
->integral_constant_expression_p
= false;
2755 parser
->allow_non_integral_constant_expression_p
= false;
2756 parser
->non_integral_constant_expression_p
= false;
2758 /* Local variable names are not forbidden. */
2759 parser
->local_variables_forbidden_p
= false;
2761 /* We are not processing an `extern "C"' declaration. */
2762 parser
->in_unbraced_linkage_specification_p
= false;
2764 /* We are not processing a declarator. */
2765 parser
->in_declarator_p
= false;
2767 /* We are not processing a template-argument-list. */
2768 parser
->in_template_argument_list_p
= false;
2770 /* We are not in an iteration statement. */
2771 parser
->in_statement
= 0;
2773 /* We are not in a switch statement. */
2774 parser
->in_switch_statement_p
= false;
2776 /* We are not parsing a type-id inside an expression. */
2777 parser
->in_type_id_in_expr_p
= false;
2779 /* Declarations aren't implicitly extern "C". */
2780 parser
->implicit_extern_c
= false;
2782 /* String literals should be translated to the execution character set. */
2783 parser
->translate_strings_p
= true;
2785 /* We are not parsing a function body. */
2786 parser
->in_function_body
= false;
2788 /* The unparsed function queue is empty. */
2789 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2791 /* There are no classes being defined. */
2792 parser
->num_classes_being_defined
= 0;
2794 /* No template parameters apply. */
2795 parser
->num_template_parameter_lists
= 0;
2800 /* Create a cp_lexer structure which will emit the tokens in CACHE
2801 and push it onto the parser's lexer stack. This is used for delayed
2802 parsing of in-class method bodies and default arguments, and should
2803 not be confused with tentative parsing. */
2805 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2807 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2808 lexer
->next
= parser
->lexer
;
2809 parser
->lexer
= lexer
;
2811 /* Move the current source position to that of the first token in the
2813 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2816 /* Pop the top lexer off the parser stack. This is never used for the
2817 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2819 cp_parser_pop_lexer (cp_parser
*parser
)
2821 cp_lexer
*lexer
= parser
->lexer
;
2822 parser
->lexer
= lexer
->next
;
2823 cp_lexer_destroy (lexer
);
2825 /* Put the current source position back where it was before this
2826 lexer was pushed. */
2827 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2830 /* Lexical conventions [gram.lex] */
2832 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2836 cp_parser_identifier (cp_parser
* parser
)
2840 /* Look for the identifier. */
2841 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2842 /* Return the value. */
2843 return token
? token
->u
.value
: error_mark_node
;
2846 /* Parse a sequence of adjacent string constants. Returns a
2847 TREE_STRING representing the combined, nul-terminated string
2848 constant. If TRANSLATE is true, translate the string to the
2849 execution character set. If WIDE_OK is true, a wide string is
2852 C++98 [lex.string] says that if a narrow string literal token is
2853 adjacent to a wide string literal token, the behavior is undefined.
2854 However, C99 6.4.5p4 says that this results in a wide string literal.
2855 We follow C99 here, for consistency with the C front end.
2857 This code is largely lifted from lex_string() in c-lex.c.
2859 FUTURE: ObjC++ will need to handle @-strings here. */
2861 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2866 struct obstack str_ob
;
2867 cpp_string str
, istr
, *strs
;
2870 tok
= cp_lexer_peek_token (parser
->lexer
);
2871 if (!cp_parser_is_string_literal (tok
))
2873 cp_parser_error (parser
, "expected string-literal");
2874 return error_mark_node
;
2877 /* Try to avoid the overhead of creating and destroying an obstack
2878 for the common case of just one string. */
2879 if (!cp_parser_is_string_literal
2880 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2882 cp_lexer_consume_token (parser
->lexer
);
2884 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
2885 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
2887 if (tok
->type
== CPP_WSTRING
)
2894 gcc_obstack_init (&str_ob
);
2899 cp_lexer_consume_token (parser
->lexer
);
2901 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
2902 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
2903 if (tok
->type
== CPP_WSTRING
)
2906 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
2908 tok
= cp_lexer_peek_token (parser
->lexer
);
2910 while (cp_parser_is_string_literal (tok
));
2912 strs
= (cpp_string
*) obstack_finish (&str_ob
);
2915 if (wide
&& !wide_ok
)
2917 cp_parser_error (parser
, "a wide string is invalid in this context");
2921 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
2922 (parse_in
, strs
, count
, &istr
, wide
))
2924 value
= build_string (istr
.len
, (const char *)istr
.text
);
2925 free (CONST_CAST (unsigned char *, istr
.text
));
2927 TREE_TYPE (value
) = wide
? wchar_array_type_node
: char_array_type_node
;
2928 value
= fix_string_type (value
);
2931 /* cpp_interpret_string has issued an error. */
2932 value
= error_mark_node
;
2935 obstack_free (&str_ob
, 0);
2941 /* Basic concepts [gram.basic] */
2943 /* Parse a translation-unit.
2946 declaration-seq [opt]
2948 Returns TRUE if all went well. */
2951 cp_parser_translation_unit (cp_parser
* parser
)
2953 /* The address of the first non-permanent object on the declarator
2955 static void *declarator_obstack_base
;
2959 /* Create the declarator obstack, if necessary. */
2960 if (!cp_error_declarator
)
2962 gcc_obstack_init (&declarator_obstack
);
2963 /* Create the error declarator. */
2964 cp_error_declarator
= make_declarator (cdk_error
);
2965 /* Create the empty parameter list. */
2966 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
2967 /* Remember where the base of the declarator obstack lies. */
2968 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
2971 cp_parser_declaration_seq_opt (parser
);
2973 /* If there are no tokens left then all went well. */
2974 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2976 /* Get rid of the token array; we don't need it any more. */
2977 cp_lexer_destroy (parser
->lexer
);
2978 parser
->lexer
= NULL
;
2980 /* This file might have been a context that's implicitly extern
2981 "C". If so, pop the lang context. (Only relevant for PCH.) */
2982 if (parser
->implicit_extern_c
)
2984 pop_lang_context ();
2985 parser
->implicit_extern_c
= false;
2989 finish_translation_unit ();
2995 cp_parser_error (parser
, "expected declaration");
2999 /* Make sure the declarator obstack was fully cleaned up. */
3000 gcc_assert (obstack_next_free (&declarator_obstack
)
3001 == declarator_obstack_base
);
3003 /* All went well. */
3007 /* Expressions [gram.expr] */
3009 /* Parse a primary-expression.
3020 ( compound-statement )
3021 __builtin_va_arg ( assignment-expression , type-id )
3022 __builtin_offsetof ( type-id , offsetof-expression )
3025 __has_nothrow_assign ( type-id )
3026 __has_nothrow_constructor ( type-id )
3027 __has_nothrow_copy ( type-id )
3028 __has_trivial_assign ( type-id )
3029 __has_trivial_constructor ( type-id )
3030 __has_trivial_copy ( type-id )
3031 __has_trivial_destructor ( type-id )
3032 __has_virtual_destructor ( type-id )
3033 __is_abstract ( type-id )
3034 __is_base_of ( type-id , type-id )
3035 __is_class ( type-id )
3036 __is_convertible_to ( type-id , type-id )
3037 __is_empty ( type-id )
3038 __is_enum ( type-id )
3039 __is_pod ( type-id )
3040 __is_polymorphic ( type-id )
3041 __is_union ( type-id )
3043 Objective-C++ Extension:
3051 ADDRESS_P is true iff this expression was immediately preceded by
3052 "&" and therefore might denote a pointer-to-member. CAST_P is true
3053 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3054 true iff this expression is a template argument.
3056 Returns a representation of the expression. Upon return, *IDK
3057 indicates what kind of id-expression (if any) was present. */
3060 cp_parser_primary_expression (cp_parser
*parser
,
3063 bool template_arg_p
,
3068 /* Assume the primary expression is not an id-expression. */
3069 *idk
= CP_ID_KIND_NONE
;
3071 /* Peek at the next token. */
3072 token
= cp_lexer_peek_token (parser
->lexer
);
3073 switch (token
->type
)
3084 token
= cp_lexer_consume_token (parser
->lexer
);
3085 /* Floating-point literals are only allowed in an integral
3086 constant expression if they are cast to an integral or
3087 enumeration type. */
3088 if (TREE_CODE (token
->u
.value
) == REAL_CST
3089 && parser
->integral_constant_expression_p
3092 /* CAST_P will be set even in invalid code like "int(2.7 +
3093 ...)". Therefore, we have to check that the next token
3094 is sure to end the cast. */
3097 cp_token
*next_token
;
3099 next_token
= cp_lexer_peek_token (parser
->lexer
);
3100 if (/* The comma at the end of an
3101 enumerator-definition. */
3102 next_token
->type
!= CPP_COMMA
3103 /* The curly brace at the end of an enum-specifier. */
3104 && next_token
->type
!= CPP_CLOSE_BRACE
3105 /* The end of a statement. */
3106 && next_token
->type
!= CPP_SEMICOLON
3107 /* The end of the cast-expression. */
3108 && next_token
->type
!= CPP_CLOSE_PAREN
3109 /* The end of an array bound. */
3110 && next_token
->type
!= CPP_CLOSE_SQUARE
3111 /* The closing ">" in a template-argument-list. */
3112 && (next_token
->type
!= CPP_GREATER
3113 || parser
->greater_than_is_operator_p
)
3114 /* C++0x only: A ">>" treated like two ">" tokens,
3115 in a template-argument-list. */
3116 && (next_token
->type
!= CPP_RSHIFT
3117 || (cxx_dialect
== cxx98
)
3118 || parser
->greater_than_is_operator_p
))
3122 /* If we are within a cast, then the constraint that the
3123 cast is to an integral or enumeration type will be
3124 checked at that point. If we are not within a cast, then
3125 this code is invalid. */
3127 cp_parser_non_integral_constant_expression
3128 (parser
, "floating-point literal");
3130 return token
->u
.value
;
3134 /* ??? Should wide strings be allowed when parser->translate_strings_p
3135 is false (i.e. in attributes)? If not, we can kill the third
3136 argument to cp_parser_string_literal. */
3137 return cp_parser_string_literal (parser
,
3138 parser
->translate_strings_p
,
3141 case CPP_OPEN_PAREN
:
3144 bool saved_greater_than_is_operator_p
;
3146 /* Consume the `('. */
3147 cp_lexer_consume_token (parser
->lexer
);
3148 /* Within a parenthesized expression, a `>' token is always
3149 the greater-than operator. */
3150 saved_greater_than_is_operator_p
3151 = parser
->greater_than_is_operator_p
;
3152 parser
->greater_than_is_operator_p
= true;
3153 /* If we see `( { ' then we are looking at the beginning of
3154 a GNU statement-expression. */
3155 if (cp_parser_allow_gnu_extensions_p (parser
)
3156 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
3158 /* Statement-expressions are not allowed by the standard. */
3160 pedwarn ("ISO C++ forbids braced-groups within expressions");
3162 /* And they're not allowed outside of a function-body; you
3163 cannot, for example, write:
3165 int i = ({ int j = 3; j + 1; });
3167 at class or namespace scope. */
3168 if (!parser
->in_function_body
3169 || parser
->in_template_argument_list_p
)
3171 error ("statement-expressions are not allowed outside "
3172 "functions nor in template-argument lists");
3173 cp_parser_skip_to_end_of_block_or_statement (parser
);
3174 expr
= error_mark_node
;
3178 /* Start the statement-expression. */
3179 expr
= begin_stmt_expr ();
3180 /* Parse the compound-statement. */
3181 cp_parser_compound_statement (parser
, expr
, false);
3183 expr
= finish_stmt_expr (expr
, false);
3188 /* Parse the parenthesized expression. */
3189 expr
= cp_parser_expression (parser
, cast_p
);
3190 /* Let the front end know that this expression was
3191 enclosed in parentheses. This matters in case, for
3192 example, the expression is of the form `A::B', since
3193 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3195 finish_parenthesized_expr (expr
);
3197 /* The `>' token might be the end of a template-id or
3198 template-parameter-list now. */
3199 parser
->greater_than_is_operator_p
3200 = saved_greater_than_is_operator_p
;
3201 /* Consume the `)'. */
3202 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
3203 cp_parser_skip_to_end_of_statement (parser
);
3209 switch (token
->keyword
)
3211 /* These two are the boolean literals. */
3213 cp_lexer_consume_token (parser
->lexer
);
3214 return boolean_true_node
;
3216 cp_lexer_consume_token (parser
->lexer
);
3217 return boolean_false_node
;
3219 /* The `__null' literal. */
3221 cp_lexer_consume_token (parser
->lexer
);
3224 /* Recognize the `this' keyword. */
3226 cp_lexer_consume_token (parser
->lexer
);
3227 if (parser
->local_variables_forbidden_p
)
3229 error ("%<this%> may not be used in this context");
3230 return error_mark_node
;
3232 /* Pointers cannot appear in constant-expressions. */
3233 if (cp_parser_non_integral_constant_expression (parser
,
3235 return error_mark_node
;
3236 return finish_this_expr ();
3238 /* The `operator' keyword can be the beginning of an
3243 case RID_FUNCTION_NAME
:
3244 case RID_PRETTY_FUNCTION_NAME
:
3245 case RID_C99_FUNCTION_NAME
:
3246 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3247 __func__ are the names of variables -- but they are
3248 treated specially. Therefore, they are handled here,
3249 rather than relying on the generic id-expression logic
3250 below. Grammatically, these names are id-expressions.
3252 Consume the token. */
3253 token
= cp_lexer_consume_token (parser
->lexer
);
3254 /* Look up the name. */
3255 return finish_fname (token
->u
.value
);
3262 /* The `__builtin_va_arg' construct is used to handle
3263 `va_arg'. Consume the `__builtin_va_arg' token. */
3264 cp_lexer_consume_token (parser
->lexer
);
3265 /* Look for the opening `('. */
3266 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3267 /* Now, parse the assignment-expression. */
3268 expression
= cp_parser_assignment_expression (parser
,
3270 /* Look for the `,'. */
3271 cp_parser_require (parser
, CPP_COMMA
, "`,'");
3272 /* Parse the type-id. */
3273 type
= cp_parser_type_id (parser
);
3274 /* Look for the closing `)'. */
3275 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3276 /* Using `va_arg' in a constant-expression is not
3278 if (cp_parser_non_integral_constant_expression (parser
,
3280 return error_mark_node
;
3281 return build_x_va_arg (expression
, type
);
3285 return cp_parser_builtin_offsetof (parser
);
3287 case RID_HAS_NOTHROW_ASSIGN
:
3288 case RID_HAS_NOTHROW_CONSTRUCTOR
:
3289 case RID_HAS_NOTHROW_COPY
:
3290 case RID_HAS_TRIVIAL_ASSIGN
:
3291 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
3292 case RID_HAS_TRIVIAL_COPY
:
3293 case RID_HAS_TRIVIAL_DESTRUCTOR
:
3294 case RID_HAS_VIRTUAL_DESTRUCTOR
:
3295 case RID_IS_ABSTRACT
:
3296 case RID_IS_BASE_OF
:
3298 case RID_IS_CONVERTIBLE_TO
:
3302 case RID_IS_POLYMORPHIC
:
3304 return cp_parser_trait_expr (parser
, token
->keyword
);
3306 /* Objective-C++ expressions. */
3308 case RID_AT_PROTOCOL
:
3309 case RID_AT_SELECTOR
:
3310 return cp_parser_objc_expression (parser
);
3313 cp_parser_error (parser
, "expected primary-expression");
3314 return error_mark_node
;
3317 /* An id-expression can start with either an identifier, a
3318 `::' as the beginning of a qualified-id, or the "operator"
3322 case CPP_TEMPLATE_ID
:
3323 case CPP_NESTED_NAME_SPECIFIER
:
3327 const char *error_msg
;
3332 /* Parse the id-expression. */
3334 = cp_parser_id_expression (parser
,
3335 /*template_keyword_p=*/false,
3336 /*check_dependency_p=*/true,
3338 /*declarator_p=*/false,
3339 /*optional_p=*/false);
3340 if (id_expression
== error_mark_node
)
3341 return error_mark_node
;
3342 token
= cp_lexer_peek_token (parser
->lexer
);
3343 done
= (token
->type
!= CPP_OPEN_SQUARE
3344 && token
->type
!= CPP_OPEN_PAREN
3345 && token
->type
!= CPP_DOT
3346 && token
->type
!= CPP_DEREF
3347 && token
->type
!= CPP_PLUS_PLUS
3348 && token
->type
!= CPP_MINUS_MINUS
);
3349 /* If we have a template-id, then no further lookup is
3350 required. If the template-id was for a template-class, we
3351 will sometimes have a TYPE_DECL at this point. */
3352 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
3353 || TREE_CODE (id_expression
) == TYPE_DECL
)
3354 decl
= id_expression
;
3355 /* Look up the name. */
3358 tree ambiguous_decls
;
3360 decl
= cp_parser_lookup_name (parser
, id_expression
,
3363 /*is_namespace=*/false,
3364 /*check_dependency=*/true,
3366 /* If the lookup was ambiguous, an error will already have
3368 if (ambiguous_decls
)
3369 return error_mark_node
;
3371 /* In Objective-C++, an instance variable (ivar) may be preferred
3372 to whatever cp_parser_lookup_name() found. */
3373 decl
= objc_lookup_ivar (decl
, id_expression
);
3375 /* If name lookup gives us a SCOPE_REF, then the
3376 qualifying scope was dependent. */
3377 if (TREE_CODE (decl
) == SCOPE_REF
)
3379 /* At this point, we do not know if DECL is a valid
3380 integral constant expression. We assume that it is
3381 in fact such an expression, so that code like:
3383 template <int N> struct A {
3387 is accepted. At template-instantiation time, we
3388 will check that B<N>::i is actually a constant. */
3391 /* Check to see if DECL is a local variable in a context
3392 where that is forbidden. */
3393 if (parser
->local_variables_forbidden_p
3394 && local_variable_p (decl
))
3396 /* It might be that we only found DECL because we are
3397 trying to be generous with pre-ISO scoping rules.
3398 For example, consider:
3402 for (int i = 0; i < 10; ++i) {}
3403 extern void f(int j = i);
3406 Here, name look up will originally find the out
3407 of scope `i'. We need to issue a warning message,
3408 but then use the global `i'. */
3409 decl
= check_for_out_of_scope_variable (decl
);
3410 if (local_variable_p (decl
))
3412 error ("local variable %qD may not appear in this context",
3414 return error_mark_node
;
3419 decl
= (finish_id_expression
3420 (id_expression
, decl
, parser
->scope
,
3422 parser
->integral_constant_expression_p
,
3423 parser
->allow_non_integral_constant_expression_p
,
3424 &parser
->non_integral_constant_expression_p
,
3425 template_p
, done
, address_p
,
3429 cp_parser_error (parser
, error_msg
);
3433 /* Anything else is an error. */
3435 /* ...unless we have an Objective-C++ message or string literal,
3437 if (c_dialect_objc ()
3438 && (token
->type
== CPP_OPEN_SQUARE
3439 || token
->type
== CPP_OBJC_STRING
))
3440 return cp_parser_objc_expression (parser
);
3442 cp_parser_error (parser
, "expected primary-expression");
3443 return error_mark_node
;
3447 /* Parse an id-expression.
3454 :: [opt] nested-name-specifier template [opt] unqualified-id
3456 :: operator-function-id
3459 Return a representation of the unqualified portion of the
3460 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3461 a `::' or nested-name-specifier.
3463 Often, if the id-expression was a qualified-id, the caller will
3464 want to make a SCOPE_REF to represent the qualified-id. This
3465 function does not do this in order to avoid wastefully creating
3466 SCOPE_REFs when they are not required.
3468 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3471 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3472 uninstantiated templates.
3474 If *TEMPLATE_P is non-NULL, it is set to true iff the
3475 `template' keyword is used to explicitly indicate that the entity
3476 named is a template.
3478 If DECLARATOR_P is true, the id-expression is appearing as part of
3479 a declarator, rather than as part of an expression. */
3482 cp_parser_id_expression (cp_parser
*parser
,
3483 bool template_keyword_p
,
3484 bool check_dependency_p
,
3489 bool global_scope_p
;
3490 bool nested_name_specifier_p
;
3492 /* Assume the `template' keyword was not used. */
3494 *template_p
= template_keyword_p
;
3496 /* Look for the optional `::' operator. */
3498 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3500 /* Look for the optional nested-name-specifier. */
3501 nested_name_specifier_p
3502 = (cp_parser_nested_name_specifier_opt (parser
,
3503 /*typename_keyword_p=*/false,
3508 /* If there is a nested-name-specifier, then we are looking at
3509 the first qualified-id production. */
3510 if (nested_name_specifier_p
)
3513 tree saved_object_scope
;
3514 tree saved_qualifying_scope
;
3515 tree unqualified_id
;
3518 /* See if the next token is the `template' keyword. */
3520 template_p
= &is_template
;
3521 *template_p
= cp_parser_optional_template_keyword (parser
);
3522 /* Name lookup we do during the processing of the
3523 unqualified-id might obliterate SCOPE. */
3524 saved_scope
= parser
->scope
;
3525 saved_object_scope
= parser
->object_scope
;
3526 saved_qualifying_scope
= parser
->qualifying_scope
;
3527 /* Process the final unqualified-id. */
3528 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3531 /*optional_p=*/false);
3532 /* Restore the SAVED_SCOPE for our caller. */
3533 parser
->scope
= saved_scope
;
3534 parser
->object_scope
= saved_object_scope
;
3535 parser
->qualifying_scope
= saved_qualifying_scope
;
3537 return unqualified_id
;
3539 /* Otherwise, if we are in global scope, then we are looking at one
3540 of the other qualified-id productions. */
3541 else if (global_scope_p
)
3546 /* Peek at the next token. */
3547 token
= cp_lexer_peek_token (parser
->lexer
);
3549 /* If it's an identifier, and the next token is not a "<", then
3550 we can avoid the template-id case. This is an optimization
3551 for this common case. */
3552 if (token
->type
== CPP_NAME
3553 && !cp_parser_nth_token_starts_template_argument_list_p
3555 return cp_parser_identifier (parser
);
3557 cp_parser_parse_tentatively (parser
);
3558 /* Try a template-id. */
3559 id
= cp_parser_template_id (parser
,
3560 /*template_keyword_p=*/false,
3561 /*check_dependency_p=*/true,
3563 /* If that worked, we're done. */
3564 if (cp_parser_parse_definitely (parser
))
3567 /* Peek at the next token. (Changes in the token buffer may
3568 have invalidated the pointer obtained above.) */
3569 token
= cp_lexer_peek_token (parser
->lexer
);
3571 switch (token
->type
)
3574 return cp_parser_identifier (parser
);
3577 if (token
->keyword
== RID_OPERATOR
)
3578 return cp_parser_operator_function_id (parser
);
3582 cp_parser_error (parser
, "expected id-expression");
3583 return error_mark_node
;
3587 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3588 /*check_dependency_p=*/true,
3593 /* Parse an unqualified-id.
3597 operator-function-id
3598 conversion-function-id
3602 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3603 keyword, in a construct like `A::template ...'.
3605 Returns a representation of unqualified-id. For the `identifier'
3606 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3607 production a BIT_NOT_EXPR is returned; the operand of the
3608 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3609 other productions, see the documentation accompanying the
3610 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3611 names are looked up in uninstantiated templates. If DECLARATOR_P
3612 is true, the unqualified-id is appearing as part of a declarator,
3613 rather than as part of an expression. */
3616 cp_parser_unqualified_id (cp_parser
* parser
,
3617 bool template_keyword_p
,
3618 bool check_dependency_p
,
3624 /* Peek at the next token. */
3625 token
= cp_lexer_peek_token (parser
->lexer
);
3627 switch (token
->type
)
3633 /* We don't know yet whether or not this will be a
3635 cp_parser_parse_tentatively (parser
);
3636 /* Try a template-id. */
3637 id
= cp_parser_template_id (parser
, template_keyword_p
,
3640 /* If it worked, we're done. */
3641 if (cp_parser_parse_definitely (parser
))
3643 /* Otherwise, it's an ordinary identifier. */
3644 return cp_parser_identifier (parser
);
3647 case CPP_TEMPLATE_ID
:
3648 return cp_parser_template_id (parser
, template_keyword_p
,
3655 tree qualifying_scope
;
3660 /* Consume the `~' token. */
3661 cp_lexer_consume_token (parser
->lexer
);
3662 /* Parse the class-name. The standard, as written, seems to
3665 template <typename T> struct S { ~S (); };
3666 template <typename T> S<T>::~S() {}
3668 is invalid, since `~' must be followed by a class-name, but
3669 `S<T>' is dependent, and so not known to be a class.
3670 That's not right; we need to look in uninstantiated
3671 templates. A further complication arises from:
3673 template <typename T> void f(T t) {
3677 Here, it is not possible to look up `T' in the scope of `T'
3678 itself. We must look in both the current scope, and the
3679 scope of the containing complete expression.
3681 Yet another issue is:
3690 The standard does not seem to say that the `S' in `~S'
3691 should refer to the type `S' and not the data member
3694 /* DR 244 says that we look up the name after the "~" in the
3695 same scope as we looked up the qualifying name. That idea
3696 isn't fully worked out; it's more complicated than that. */
3697 scope
= parser
->scope
;
3698 object_scope
= parser
->object_scope
;
3699 qualifying_scope
= parser
->qualifying_scope
;
3701 /* Check for invalid scopes. */
3702 if (scope
== error_mark_node
)
3704 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3705 cp_lexer_consume_token (parser
->lexer
);
3706 return error_mark_node
;
3708 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
3710 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3711 error ("scope %qT before %<~%> is not a class-name", scope
);
3712 cp_parser_simulate_error (parser
);
3713 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3714 cp_lexer_consume_token (parser
->lexer
);
3715 return error_mark_node
;
3717 gcc_assert (!scope
|| TYPE_P (scope
));
3719 /* If the name is of the form "X::~X" it's OK. */
3720 token
= cp_lexer_peek_token (parser
->lexer
);
3722 && token
->type
== CPP_NAME
3723 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3725 && constructor_name_p (token
->u
.value
, scope
))
3727 cp_lexer_consume_token (parser
->lexer
);
3728 return build_nt (BIT_NOT_EXPR
, scope
);
3731 /* If there was an explicit qualification (S::~T), first look
3732 in the scope given by the qualification (i.e., S). */
3734 type_decl
= NULL_TREE
;
3737 cp_parser_parse_tentatively (parser
);
3738 type_decl
= cp_parser_class_name (parser
,
3739 /*typename_keyword_p=*/false,
3740 /*template_keyword_p=*/false,
3742 /*check_dependency=*/false,
3743 /*class_head_p=*/false,
3745 if (cp_parser_parse_definitely (parser
))
3748 /* In "N::S::~S", look in "N" as well. */
3749 if (!done
&& scope
&& qualifying_scope
)
3751 cp_parser_parse_tentatively (parser
);
3752 parser
->scope
= qualifying_scope
;
3753 parser
->object_scope
= NULL_TREE
;
3754 parser
->qualifying_scope
= NULL_TREE
;
3756 = cp_parser_class_name (parser
,
3757 /*typename_keyword_p=*/false,
3758 /*template_keyword_p=*/false,
3760 /*check_dependency=*/false,
3761 /*class_head_p=*/false,
3763 if (cp_parser_parse_definitely (parser
))
3766 /* In "p->S::~T", look in the scope given by "*p" as well. */
3767 else if (!done
&& object_scope
)
3769 cp_parser_parse_tentatively (parser
);
3770 parser
->scope
= object_scope
;
3771 parser
->object_scope
= NULL_TREE
;
3772 parser
->qualifying_scope
= NULL_TREE
;
3774 = cp_parser_class_name (parser
,
3775 /*typename_keyword_p=*/false,
3776 /*template_keyword_p=*/false,
3778 /*check_dependency=*/false,
3779 /*class_head_p=*/false,
3781 if (cp_parser_parse_definitely (parser
))
3784 /* Look in the surrounding context. */
3787 parser
->scope
= NULL_TREE
;
3788 parser
->object_scope
= NULL_TREE
;
3789 parser
->qualifying_scope
= NULL_TREE
;
3791 = cp_parser_class_name (parser
,
3792 /*typename_keyword_p=*/false,
3793 /*template_keyword_p=*/false,
3795 /*check_dependency=*/false,
3796 /*class_head_p=*/false,
3799 /* If an error occurred, assume that the name of the
3800 destructor is the same as the name of the qualifying
3801 class. That allows us to keep parsing after running
3802 into ill-formed destructor names. */
3803 if (type_decl
== error_mark_node
&& scope
)
3804 return build_nt (BIT_NOT_EXPR
, scope
);
3805 else if (type_decl
== error_mark_node
)
3806 return error_mark_node
;
3808 /* Check that destructor name and scope match. */
3809 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
3811 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3812 error ("declaration of %<~%T%> as member of %qT",
3814 cp_parser_simulate_error (parser
);
3815 return error_mark_node
;
3820 A typedef-name that names a class shall not be used as the
3821 identifier in the declarator for a destructor declaration. */
3823 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3824 && !DECL_SELF_REFERENCE_P (type_decl
)
3825 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
3826 error ("typedef-name %qD used as destructor declarator",
3829 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3833 if (token
->keyword
== RID_OPERATOR
)
3837 /* This could be a template-id, so we try that first. */
3838 cp_parser_parse_tentatively (parser
);
3839 /* Try a template-id. */
3840 id
= cp_parser_template_id (parser
, template_keyword_p
,
3841 /*check_dependency_p=*/true,
3843 /* If that worked, we're done. */
3844 if (cp_parser_parse_definitely (parser
))
3846 /* We still don't know whether we're looking at an
3847 operator-function-id or a conversion-function-id. */
3848 cp_parser_parse_tentatively (parser
);
3849 /* Try an operator-function-id. */
3850 id
= cp_parser_operator_function_id (parser
);
3851 /* If that didn't work, try a conversion-function-id. */
3852 if (!cp_parser_parse_definitely (parser
))
3853 id
= cp_parser_conversion_function_id (parser
);
3862 cp_parser_error (parser
, "expected unqualified-id");
3863 return error_mark_node
;
3867 /* Parse an (optional) nested-name-specifier.
3869 nested-name-specifier:
3870 class-or-namespace-name :: nested-name-specifier [opt]
3871 class-or-namespace-name :: template nested-name-specifier [opt]
3873 PARSER->SCOPE should be set appropriately before this function is
3874 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3875 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3878 Sets PARSER->SCOPE to the class (TYPE) or namespace
3879 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3880 it unchanged if there is no nested-name-specifier. Returns the new
3881 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3883 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3884 part of a declaration and/or decl-specifier. */
3887 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3888 bool typename_keyword_p
,
3889 bool check_dependency_p
,
3891 bool is_declaration
)
3893 bool success
= false;
3894 cp_token_position start
= 0;
3897 /* Remember where the nested-name-specifier starts. */
3898 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3900 start
= cp_lexer_token_position (parser
->lexer
, false);
3901 push_deferring_access_checks (dk_deferred
);
3908 tree saved_qualifying_scope
;
3909 bool template_keyword_p
;
3911 /* Spot cases that cannot be the beginning of a
3912 nested-name-specifier. */
3913 token
= cp_lexer_peek_token (parser
->lexer
);
3915 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3916 the already parsed nested-name-specifier. */
3917 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3919 /* Grab the nested-name-specifier and continue the loop. */
3920 cp_parser_pre_parsed_nested_name_specifier (parser
);
3921 /* If we originally encountered this nested-name-specifier
3922 with IS_DECLARATION set to false, we will not have
3923 resolved TYPENAME_TYPEs, so we must do so here. */
3925 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3927 new_scope
= resolve_typename_type (parser
->scope
,
3928 /*only_current_p=*/false);
3929 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
3930 parser
->scope
= new_scope
;
3936 /* Spot cases that cannot be the beginning of a
3937 nested-name-specifier. On the second and subsequent times
3938 through the loop, we look for the `template' keyword. */
3939 if (success
&& token
->keyword
== RID_TEMPLATE
)
3941 /* A template-id can start a nested-name-specifier. */
3942 else if (token
->type
== CPP_TEMPLATE_ID
)
3946 /* If the next token is not an identifier, then it is
3947 definitely not a class-or-namespace-name. */
3948 if (token
->type
!= CPP_NAME
)
3950 /* If the following token is neither a `<' (to begin a
3951 template-id), nor a `::', then we are not looking at a
3952 nested-name-specifier. */
3953 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3954 if (token
->type
!= CPP_SCOPE
3955 && !cp_parser_nth_token_starts_template_argument_list_p
3960 /* The nested-name-specifier is optional, so we parse
3962 cp_parser_parse_tentatively (parser
);
3964 /* Look for the optional `template' keyword, if this isn't the
3965 first time through the loop. */
3967 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3969 template_keyword_p
= false;
3971 /* Save the old scope since the name lookup we are about to do
3972 might destroy it. */
3973 old_scope
= parser
->scope
;
3974 saved_qualifying_scope
= parser
->qualifying_scope
;
3975 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3976 look up names in "X<T>::I" in order to determine that "Y" is
3977 a template. So, if we have a typename at this point, we make
3978 an effort to look through it. */
3980 && !typename_keyword_p
3982 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3983 parser
->scope
= resolve_typename_type (parser
->scope
,
3984 /*only_current_p=*/false);
3985 /* Parse the qualifying entity. */
3987 = cp_parser_class_or_namespace_name (parser
,
3993 /* Look for the `::' token. */
3994 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3996 /* If we found what we wanted, we keep going; otherwise, we're
3998 if (!cp_parser_parse_definitely (parser
))
4000 bool error_p
= false;
4002 /* Restore the OLD_SCOPE since it was valid before the
4003 failed attempt at finding the last
4004 class-or-namespace-name. */
4005 parser
->scope
= old_scope
;
4006 parser
->qualifying_scope
= saved_qualifying_scope
;
4007 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4009 /* If the next token is an identifier, and the one after
4010 that is a `::', then any valid interpretation would have
4011 found a class-or-namespace-name. */
4012 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
4013 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4015 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
4018 token
= cp_lexer_consume_token (parser
->lexer
);
4021 if (!token
->ambiguous_p
)
4024 tree ambiguous_decls
;
4026 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
4028 /*is_template=*/false,
4029 /*is_namespace=*/false,
4030 /*check_dependency=*/true,
4032 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4033 error ("%qD used without template parameters", decl
);
4034 else if (ambiguous_decls
)
4036 error ("reference to %qD is ambiguous",
4038 print_candidates (ambiguous_decls
);
4039 decl
= error_mark_node
;
4042 cp_parser_name_lookup_error
4043 (parser
, token
->u
.value
, decl
,
4044 "is not a class or namespace");
4046 parser
->scope
= error_mark_node
;
4048 /* Treat this as a successful nested-name-specifier
4053 If the name found is not a class-name (clause
4054 _class_) or namespace-name (_namespace.def_), the
4055 program is ill-formed. */
4058 cp_lexer_consume_token (parser
->lexer
);
4062 /* We've found one valid nested-name-specifier. */
4064 /* Name lookup always gives us a DECL. */
4065 if (TREE_CODE (new_scope
) == TYPE_DECL
)
4066 new_scope
= TREE_TYPE (new_scope
);
4067 /* Uses of "template" must be followed by actual templates. */
4068 if (template_keyword_p
4069 && !(CLASS_TYPE_P (new_scope
)
4070 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
4071 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
4072 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
4073 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
4074 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
4075 == TEMPLATE_ID_EXPR
)))
4076 pedwarn (TYPE_P (new_scope
)
4077 ? "%qT is not a template"
4078 : "%qD is not a template",
4080 /* If it is a class scope, try to complete it; we are about to
4081 be looking up names inside the class. */
4082 if (TYPE_P (new_scope
)
4083 /* Since checking types for dependency can be expensive,
4084 avoid doing it if the type is already complete. */
4085 && !COMPLETE_TYPE_P (new_scope
)
4086 /* Do not try to complete dependent types. */
4087 && !dependent_type_p (new_scope
))
4089 new_scope
= complete_type (new_scope
);
4090 /* If it is a typedef to current class, use the current
4091 class instead, as the typedef won't have any names inside
4093 if (!COMPLETE_TYPE_P (new_scope
)
4094 && currently_open_class (new_scope
))
4095 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
4097 /* Make sure we look in the right scope the next time through
4099 parser
->scope
= new_scope
;
4102 /* If parsing tentatively, replace the sequence of tokens that makes
4103 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4104 token. That way, should we re-parse the token stream, we will
4105 not have to repeat the effort required to do the parse, nor will
4106 we issue duplicate error messages. */
4107 if (success
&& start
)
4111 token
= cp_lexer_token_at (parser
->lexer
, start
);
4112 /* Reset the contents of the START token. */
4113 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
4114 /* Retrieve any deferred checks. Do not pop this access checks yet
4115 so the memory will not be reclaimed during token replacing below. */
4116 token
->u
.tree_check_value
= GGC_CNEW (struct tree_check
);
4117 token
->u
.tree_check_value
->value
= parser
->scope
;
4118 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
4119 token
->u
.tree_check_value
->qualifying_scope
=
4120 parser
->qualifying_scope
;
4121 token
->keyword
= RID_MAX
;
4123 /* Purge all subsequent tokens. */
4124 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4128 pop_to_parent_deferring_access_checks ();
4130 return success
? parser
->scope
: NULL_TREE
;
4133 /* Parse a nested-name-specifier. See
4134 cp_parser_nested_name_specifier_opt for details. This function
4135 behaves identically, except that it will an issue an error if no
4136 nested-name-specifier is present. */
4139 cp_parser_nested_name_specifier (cp_parser
*parser
,
4140 bool typename_keyword_p
,
4141 bool check_dependency_p
,
4143 bool is_declaration
)
4147 /* Look for the nested-name-specifier. */
4148 scope
= cp_parser_nested_name_specifier_opt (parser
,
4153 /* If it was not present, issue an error message. */
4156 cp_parser_error (parser
, "expected nested-name-specifier");
4157 parser
->scope
= NULL_TREE
;
4163 /* Parse a class-or-namespace-name.
4165 class-or-namespace-name:
4169 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4170 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4171 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4172 TYPE_P is TRUE iff the next name should be taken as a class-name,
4173 even the same name is declared to be another entity in the same
4176 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4177 specified by the class-or-namespace-name. If neither is found the
4178 ERROR_MARK_NODE is returned. */
4181 cp_parser_class_or_namespace_name (cp_parser
*parser
,
4182 bool typename_keyword_p
,
4183 bool template_keyword_p
,
4184 bool check_dependency_p
,
4186 bool is_declaration
)
4189 tree saved_qualifying_scope
;
4190 tree saved_object_scope
;
4194 /* Before we try to parse the class-name, we must save away the
4195 current PARSER->SCOPE since cp_parser_class_name will destroy
4197 saved_scope
= parser
->scope
;
4198 saved_qualifying_scope
= parser
->qualifying_scope
;
4199 saved_object_scope
= parser
->object_scope
;
4200 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4201 there is no need to look for a namespace-name. */
4202 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
4204 cp_parser_parse_tentatively (parser
);
4205 scope
= cp_parser_class_name (parser
,
4208 type_p
? class_type
: none_type
,
4210 /*class_head_p=*/false,
4212 /* If that didn't work, try for a namespace-name. */
4213 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
4215 /* Restore the saved scope. */
4216 parser
->scope
= saved_scope
;
4217 parser
->qualifying_scope
= saved_qualifying_scope
;
4218 parser
->object_scope
= saved_object_scope
;
4219 /* If we are not looking at an identifier followed by the scope
4220 resolution operator, then this is not part of a
4221 nested-name-specifier. (Note that this function is only used
4222 to parse the components of a nested-name-specifier.) */
4223 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
4224 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
4225 return error_mark_node
;
4226 scope
= cp_parser_namespace_name (parser
);
4232 /* Parse a postfix-expression.
4236 postfix-expression [ expression ]
4237 postfix-expression ( expression-list [opt] )
4238 simple-type-specifier ( expression-list [opt] )
4239 typename :: [opt] nested-name-specifier identifier
4240 ( expression-list [opt] )
4241 typename :: [opt] nested-name-specifier template [opt] template-id
4242 ( expression-list [opt] )
4243 postfix-expression . template [opt] id-expression
4244 postfix-expression -> template [opt] id-expression
4245 postfix-expression . pseudo-destructor-name
4246 postfix-expression -> pseudo-destructor-name
4247 postfix-expression ++
4248 postfix-expression --
4249 dynamic_cast < type-id > ( expression )
4250 static_cast < type-id > ( expression )
4251 reinterpret_cast < type-id > ( expression )
4252 const_cast < type-id > ( expression )
4253 typeid ( expression )
4259 ( type-id ) { initializer-list , [opt] }
4261 This extension is a GNU version of the C99 compound-literal
4262 construct. (The C99 grammar uses `type-name' instead of `type-id',
4263 but they are essentially the same concept.)
4265 If ADDRESS_P is true, the postfix expression is the operand of the
4266 `&' operator. CAST_P is true if this expression is the target of a
4269 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4270 class member access expressions [expr.ref].
4272 Returns a representation of the expression. */
4275 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
4276 bool member_access_only_p
)
4280 cp_id_kind idk
= CP_ID_KIND_NONE
;
4281 tree postfix_expression
= NULL_TREE
;
4282 bool is_member_access
= false;
4284 /* Peek at the next token. */
4285 token
= cp_lexer_peek_token (parser
->lexer
);
4286 /* Some of the productions are determined by keywords. */
4287 keyword
= token
->keyword
;
4297 const char *saved_message
;
4299 /* All of these can be handled in the same way from the point
4300 of view of parsing. Begin by consuming the token
4301 identifying the cast. */
4302 cp_lexer_consume_token (parser
->lexer
);
4304 /* New types cannot be defined in the cast. */
4305 saved_message
= parser
->type_definition_forbidden_message
;
4306 parser
->type_definition_forbidden_message
4307 = "types may not be defined in casts";
4309 /* Look for the opening `<'. */
4310 cp_parser_require (parser
, CPP_LESS
, "`<'");
4311 /* Parse the type to which we are casting. */
4312 type
= cp_parser_type_id (parser
);
4313 /* Look for the closing `>'. */
4314 cp_parser_require (parser
, CPP_GREATER
, "`>'");
4315 /* Restore the old message. */
4316 parser
->type_definition_forbidden_message
= saved_message
;
4318 /* And the expression which is being cast. */
4319 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4320 expression
= cp_parser_expression (parser
, /*cast_p=*/true);
4321 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4323 /* Only type conversions to integral or enumeration types
4324 can be used in constant-expressions. */
4325 if (!cast_valid_in_integral_constant_expression_p (type
)
4326 && (cp_parser_non_integral_constant_expression
4328 "a cast to a type other than an integral or "
4329 "enumeration type")))
4330 return error_mark_node
;
4336 = build_dynamic_cast (type
, expression
);
4340 = build_static_cast (type
, expression
);
4344 = build_reinterpret_cast (type
, expression
);
4348 = build_const_cast (type
, expression
);
4359 const char *saved_message
;
4360 bool saved_in_type_id_in_expr_p
;
4362 /* Consume the `typeid' token. */
4363 cp_lexer_consume_token (parser
->lexer
);
4364 /* Look for the `(' token. */
4365 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4366 /* Types cannot be defined in a `typeid' expression. */
4367 saved_message
= parser
->type_definition_forbidden_message
;
4368 parser
->type_definition_forbidden_message
4369 = "types may not be defined in a `typeid\' expression";
4370 /* We can't be sure yet whether we're looking at a type-id or an
4372 cp_parser_parse_tentatively (parser
);
4373 /* Try a type-id first. */
4374 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4375 parser
->in_type_id_in_expr_p
= true;
4376 type
= cp_parser_type_id (parser
);
4377 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4378 /* Look for the `)' token. Otherwise, we can't be sure that
4379 we're not looking at an expression: consider `typeid (int
4380 (3))', for example. */
4381 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4382 /* If all went well, simply lookup the type-id. */
4383 if (cp_parser_parse_definitely (parser
))
4384 postfix_expression
= get_typeid (type
);
4385 /* Otherwise, fall back to the expression variant. */
4390 /* Look for an expression. */
4391 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
4392 /* Compute its typeid. */
4393 postfix_expression
= build_typeid (expression
);
4394 /* Look for the `)' token. */
4395 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4397 /* Restore the saved message. */
4398 parser
->type_definition_forbidden_message
= saved_message
;
4399 /* `typeid' may not appear in an integral constant expression. */
4400 if (cp_parser_non_integral_constant_expression(parser
,
4401 "`typeid' operator"))
4402 return error_mark_node
;
4409 /* The syntax permitted here is the same permitted for an
4410 elaborated-type-specifier. */
4411 type
= cp_parser_elaborated_type_specifier (parser
,
4412 /*is_friend=*/false,
4413 /*is_declaration=*/false);
4414 postfix_expression
= cp_parser_functional_cast (parser
, type
);
4422 /* If the next thing is a simple-type-specifier, we may be
4423 looking at a functional cast. We could also be looking at
4424 an id-expression. So, we try the functional cast, and if
4425 that doesn't work we fall back to the primary-expression. */
4426 cp_parser_parse_tentatively (parser
);
4427 /* Look for the simple-type-specifier. */
4428 type
= cp_parser_simple_type_specifier (parser
,
4429 /*decl_specs=*/NULL
,
4430 CP_PARSER_FLAGS_NONE
);
4431 /* Parse the cast itself. */
4432 if (!cp_parser_error_occurred (parser
))
4434 = cp_parser_functional_cast (parser
, type
);
4435 /* If that worked, we're done. */
4436 if (cp_parser_parse_definitely (parser
))
4439 /* If the functional-cast didn't work out, try a
4440 compound-literal. */
4441 if (cp_parser_allow_gnu_extensions_p (parser
)
4442 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4444 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
4445 bool saved_in_type_id_in_expr_p
;
4447 cp_parser_parse_tentatively (parser
);
4448 /* Consume the `('. */
4449 cp_lexer_consume_token (parser
->lexer
);
4450 /* Parse the type. */
4451 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4452 parser
->in_type_id_in_expr_p
= true;
4453 type
= cp_parser_type_id (parser
);
4454 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4455 /* Look for the `)'. */
4456 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4457 /* Look for the `{'. */
4458 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
4459 /* If things aren't going well, there's no need to
4461 if (!cp_parser_error_occurred (parser
))
4463 bool non_constant_p
;
4464 /* Parse the initializer-list. */
4466 = cp_parser_initializer_list (parser
, &non_constant_p
);
4467 /* Allow a trailing `,'. */
4468 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
4469 cp_lexer_consume_token (parser
->lexer
);
4470 /* Look for the final `}'. */
4471 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
4473 /* If that worked, we're definitely looking at a
4474 compound-literal expression. */
4475 if (cp_parser_parse_definitely (parser
))
4477 /* Warn the user that a compound literal is not
4478 allowed in standard C++. */
4480 pedwarn ("ISO C++ forbids compound-literals");
4481 /* For simplicity, we disallow compound literals in
4482 constant-expressions. We could
4483 allow compound literals of integer type, whose
4484 initializer was a constant, in constant
4485 expressions. Permitting that usage, as a further
4486 extension, would not change the meaning of any
4487 currently accepted programs. (Of course, as
4488 compound literals are not part of ISO C++, the
4489 standard has nothing to say.) */
4490 if (cp_parser_non_integral_constant_expression
4491 (parser
, "non-constant compound literals"))
4493 postfix_expression
= error_mark_node
;
4496 /* Form the representation of the compound-literal. */
4498 = finish_compound_literal (type
, initializer_list
);
4503 /* It must be a primary-expression. */
4505 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
4506 /*template_arg_p=*/false,
4512 /* Keep looping until the postfix-expression is complete. */
4515 if (idk
== CP_ID_KIND_UNQUALIFIED
4516 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4517 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4518 /* It is not a Koenig lookup function call. */
4520 = unqualified_name_lookup_error (postfix_expression
);
4522 /* Peek at the next token. */
4523 token
= cp_lexer_peek_token (parser
->lexer
);
4525 switch (token
->type
)
4527 case CPP_OPEN_SQUARE
:
4529 = cp_parser_postfix_open_square_expression (parser
,
4532 idk
= CP_ID_KIND_NONE
;
4533 is_member_access
= false;
4536 case CPP_OPEN_PAREN
:
4537 /* postfix-expression ( expression-list [opt] ) */
4540 bool is_builtin_constant_p
;
4541 bool saved_integral_constant_expression_p
= false;
4542 bool saved_non_integral_constant_expression_p
= false;
4545 is_member_access
= false;
4547 is_builtin_constant_p
4548 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
4549 if (is_builtin_constant_p
)
4551 /* The whole point of __builtin_constant_p is to allow
4552 non-constant expressions to appear as arguments. */
4553 saved_integral_constant_expression_p
4554 = parser
->integral_constant_expression_p
;
4555 saved_non_integral_constant_expression_p
4556 = parser
->non_integral_constant_expression_p
;
4557 parser
->integral_constant_expression_p
= false;
4559 args
= (cp_parser_parenthesized_expression_list
4560 (parser
, /*is_attribute_list=*/false,
4561 /*cast_p=*/false, /*allow_expansion_p=*/true,
4562 /*non_constant_p=*/NULL
));
4563 if (is_builtin_constant_p
)
4565 parser
->integral_constant_expression_p
4566 = saved_integral_constant_expression_p
;
4567 parser
->non_integral_constant_expression_p
4568 = saved_non_integral_constant_expression_p
;
4571 if (args
== error_mark_node
)
4573 postfix_expression
= error_mark_node
;
4577 /* Function calls are not permitted in
4578 constant-expressions. */
4579 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
4580 && cp_parser_non_integral_constant_expression (parser
,
4583 postfix_expression
= error_mark_node
;
4588 if (idk
== CP_ID_KIND_UNQUALIFIED
)
4590 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
4596 = perform_koenig_lookup (postfix_expression
, args
);
4600 = unqualified_fn_lookup_error (postfix_expression
);
4602 /* We do not perform argument-dependent lookup if
4603 normal lookup finds a non-function, in accordance
4604 with the expected resolution of DR 218. */
4605 else if (args
&& is_overloaded_fn (postfix_expression
))
4607 tree fn
= get_first_fn (postfix_expression
);
4609 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4610 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
4612 /* Only do argument dependent lookup if regular
4613 lookup does not find a set of member functions.
4614 [basic.lookup.koenig]/2a */
4615 if (!DECL_FUNCTION_MEMBER_P (fn
))
4619 = perform_koenig_lookup (postfix_expression
, args
);
4624 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4626 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4627 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4629 if (processing_template_decl
4630 && (type_dependent_expression_p (instance
)
4631 || (!BASELINK_P (fn
)
4632 && TREE_CODE (fn
) != FIELD_DECL
)
4633 || type_dependent_expression_p (fn
)
4634 || any_type_dependent_arguments_p (args
)))
4637 = build_nt_call_list (postfix_expression
, args
);
4641 if (BASELINK_P (fn
))
4643 = (build_new_method_call
4644 (instance
, fn
, args
, NULL_TREE
,
4645 (idk
== CP_ID_KIND_QUALIFIED
4646 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
),
4650 = finish_call_expr (postfix_expression
, args
,
4651 /*disallow_virtual=*/false,
4652 /*koenig_p=*/false);
4654 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4655 || TREE_CODE (postfix_expression
) == MEMBER_REF
4656 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4657 postfix_expression
= (build_offset_ref_call_from_tree
4658 (postfix_expression
, args
));
4659 else if (idk
== CP_ID_KIND_QUALIFIED
)
4660 /* A call to a static class member, or a namespace-scope
4663 = finish_call_expr (postfix_expression
, args
,
4664 /*disallow_virtual=*/true,
4667 /* All other function calls. */
4669 = finish_call_expr (postfix_expression
, args
,
4670 /*disallow_virtual=*/false,
4673 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4674 idk
= CP_ID_KIND_NONE
;
4680 /* postfix-expression . template [opt] id-expression
4681 postfix-expression . pseudo-destructor-name
4682 postfix-expression -> template [opt] id-expression
4683 postfix-expression -> pseudo-destructor-name */
4685 /* Consume the `.' or `->' operator. */
4686 cp_lexer_consume_token (parser
->lexer
);
4689 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4693 is_member_access
= true;
4697 /* postfix-expression ++ */
4698 /* Consume the `++' token. */
4699 cp_lexer_consume_token (parser
->lexer
);
4700 /* Generate a representation for the complete expression. */
4702 = finish_increment_expr (postfix_expression
,
4703 POSTINCREMENT_EXPR
);
4704 /* Increments may not appear in constant-expressions. */
4705 if (cp_parser_non_integral_constant_expression (parser
,
4707 postfix_expression
= error_mark_node
;
4708 idk
= CP_ID_KIND_NONE
;
4709 is_member_access
= false;
4712 case CPP_MINUS_MINUS
:
4713 /* postfix-expression -- */
4714 /* Consume the `--' token. */
4715 cp_lexer_consume_token (parser
->lexer
);
4716 /* Generate a representation for the complete expression. */
4718 = finish_increment_expr (postfix_expression
,
4719 POSTDECREMENT_EXPR
);
4720 /* Decrements may not appear in constant-expressions. */
4721 if (cp_parser_non_integral_constant_expression (parser
,
4723 postfix_expression
= error_mark_node
;
4724 idk
= CP_ID_KIND_NONE
;
4725 is_member_access
= false;
4729 if (member_access_only_p
)
4730 return is_member_access
? postfix_expression
: error_mark_node
;
4732 return postfix_expression
;
4736 /* We should never get here. */
4738 return error_mark_node
;
4741 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4742 by cp_parser_builtin_offsetof. We're looking for
4744 postfix-expression [ expression ]
4746 FOR_OFFSETOF is set if we're being called in that context, which
4747 changes how we deal with integer constant expressions. */
4750 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
4751 tree postfix_expression
,
4756 /* Consume the `[' token. */
4757 cp_lexer_consume_token (parser
->lexer
);
4759 /* Parse the index expression. */
4760 /* ??? For offsetof, there is a question of what to allow here. If
4761 offsetof is not being used in an integral constant expression context,
4762 then we *could* get the right answer by computing the value at runtime.
4763 If we are in an integral constant expression context, then we might
4764 could accept any constant expression; hard to say without analysis.
4765 Rather than open the barn door too wide right away, allow only integer
4766 constant expressions here. */
4768 index
= cp_parser_constant_expression (parser
, false, NULL
);
4770 index
= cp_parser_expression (parser
, /*cast_p=*/false);
4772 /* Look for the closing `]'. */
4773 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4775 /* Build the ARRAY_REF. */
4776 postfix_expression
= grok_array_decl (postfix_expression
, index
);
4778 /* When not doing offsetof, array references are not permitted in
4779 constant-expressions. */
4781 && (cp_parser_non_integral_constant_expression
4782 (parser
, "an array reference")))
4783 postfix_expression
= error_mark_node
;
4785 return postfix_expression
;
4788 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4789 by cp_parser_builtin_offsetof. We're looking for
4791 postfix-expression . template [opt] id-expression
4792 postfix-expression . pseudo-destructor-name
4793 postfix-expression -> template [opt] id-expression
4794 postfix-expression -> pseudo-destructor-name
4796 FOR_OFFSETOF is set if we're being called in that context. That sorta
4797 limits what of the above we'll actually accept, but nevermind.
4798 TOKEN_TYPE is the "." or "->" token, which will already have been
4799 removed from the stream. */
4802 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
4803 enum cpp_ttype token_type
,
4804 tree postfix_expression
,
4805 bool for_offsetof
, cp_id_kind
*idk
)
4809 bool pseudo_destructor_p
;
4810 tree scope
= NULL_TREE
;
4812 /* If this is a `->' operator, dereference the pointer. */
4813 if (token_type
== CPP_DEREF
)
4814 postfix_expression
= build_x_arrow (postfix_expression
);
4815 /* Check to see whether or not the expression is type-dependent. */
4816 dependent_p
= type_dependent_expression_p (postfix_expression
);
4817 /* The identifier following the `->' or `.' is not qualified. */
4818 parser
->scope
= NULL_TREE
;
4819 parser
->qualifying_scope
= NULL_TREE
;
4820 parser
->object_scope
= NULL_TREE
;
4821 *idk
= CP_ID_KIND_NONE
;
4822 /* Enter the scope corresponding to the type of the object
4823 given by the POSTFIX_EXPRESSION. */
4824 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
4826 scope
= TREE_TYPE (postfix_expression
);
4827 /* According to the standard, no expression should ever have
4828 reference type. Unfortunately, we do not currently match
4829 the standard in this respect in that our internal representation
4830 of an expression may have reference type even when the standard
4831 says it does not. Therefore, we have to manually obtain the
4832 underlying type here. */
4833 scope
= non_reference (scope
);
4834 /* The type of the POSTFIX_EXPRESSION must be complete. */
4835 if (scope
== unknown_type_node
)
4837 error ("%qE does not have class type", postfix_expression
);
4841 scope
= complete_type_or_else (scope
, NULL_TREE
);
4842 /* Let the name lookup machinery know that we are processing a
4843 class member access expression. */
4844 parser
->context
->object_type
= scope
;
4845 /* If something went wrong, we want to be able to discern that case,
4846 as opposed to the case where there was no SCOPE due to the type
4847 of expression being dependent. */
4849 scope
= error_mark_node
;
4850 /* If the SCOPE was erroneous, make the various semantic analysis
4851 functions exit quickly -- and without issuing additional error
4853 if (scope
== error_mark_node
)
4854 postfix_expression
= error_mark_node
;
4857 /* Assume this expression is not a pseudo-destructor access. */
4858 pseudo_destructor_p
= false;
4860 /* If the SCOPE is a scalar type, then, if this is a valid program,
4861 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4862 is type dependent, it can be pseudo-destructor-name or something else.
4863 Try to parse it as pseudo-destructor-name first. */
4864 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
4869 cp_parser_parse_tentatively (parser
);
4870 /* Parse the pseudo-destructor-name. */
4872 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4874 && (cp_parser_error_occurred (parser
)
4875 || TREE_CODE (type
) != TYPE_DECL
4876 || !SCALAR_TYPE_P (TREE_TYPE (type
))))
4877 cp_parser_abort_tentative_parse (parser
);
4878 else if (cp_parser_parse_definitely (parser
))
4880 pseudo_destructor_p
= true;
4882 = finish_pseudo_destructor_expr (postfix_expression
,
4883 s
, TREE_TYPE (type
));
4887 if (!pseudo_destructor_p
)
4889 /* If the SCOPE is not a scalar type, we are looking at an
4890 ordinary class member access expression, rather than a
4891 pseudo-destructor-name. */
4893 /* Parse the id-expression. */
4894 name
= (cp_parser_id_expression
4896 cp_parser_optional_template_keyword (parser
),
4897 /*check_dependency_p=*/true,
4899 /*declarator_p=*/false,
4900 /*optional_p=*/false));
4901 /* In general, build a SCOPE_REF if the member name is qualified.
4902 However, if the name was not dependent and has already been
4903 resolved; there is no need to build the SCOPE_REF. For example;
4905 struct X { void f(); };
4906 template <typename T> void f(T* t) { t->X::f(); }
4908 Even though "t" is dependent, "X::f" is not and has been resolved
4909 to a BASELINK; there is no need to include scope information. */
4911 /* But we do need to remember that there was an explicit scope for
4912 virtual function calls. */
4914 *idk
= CP_ID_KIND_QUALIFIED
;
4916 /* If the name is a template-id that names a type, we will get a
4917 TYPE_DECL here. That is invalid code. */
4918 if (TREE_CODE (name
) == TYPE_DECL
)
4920 error ("invalid use of %qD", name
);
4921 postfix_expression
= error_mark_node
;
4925 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
4927 name
= build_qualified_name (/*type=*/NULL_TREE
,
4931 parser
->scope
= NULL_TREE
;
4932 parser
->qualifying_scope
= NULL_TREE
;
4933 parser
->object_scope
= NULL_TREE
;
4935 if (scope
&& name
&& BASELINK_P (name
))
4936 adjust_result_of_qualified_name_lookup
4937 (name
, BINFO_TYPE (BASELINK_ACCESS_BINFO (name
)), scope
);
4939 = finish_class_member_access_expr (postfix_expression
, name
,
4944 /* We no longer need to look up names in the scope of the object on
4945 the left-hand side of the `.' or `->' operator. */
4946 parser
->context
->object_type
= NULL_TREE
;
4948 /* Outside of offsetof, these operators may not appear in
4949 constant-expressions. */
4951 && (cp_parser_non_integral_constant_expression
4952 (parser
, token_type
== CPP_DEREF
? "'->'" : "`.'")))
4953 postfix_expression
= error_mark_node
;
4955 return postfix_expression
;
4958 /* Parse a parenthesized expression-list.
4961 assignment-expression
4962 expression-list, assignment-expression
4967 identifier, expression-list
4969 CAST_P is true if this expression is the target of a cast.
4971 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4974 Returns a TREE_LIST. The TREE_VALUE of each node is a
4975 representation of an assignment-expression. Note that a TREE_LIST
4976 is returned even if there is only a single expression in the list.
4977 error_mark_node is returned if the ( and or ) are
4978 missing. NULL_TREE is returned on no expressions. The parentheses
4979 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4980 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4981 indicates whether or not all of the expressions in the list were
4985 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4986 bool is_attribute_list
,
4988 bool allow_expansion_p
,
4989 bool *non_constant_p
)
4991 tree expression_list
= NULL_TREE
;
4992 bool fold_expr_p
= is_attribute_list
;
4993 tree identifier
= NULL_TREE
;
4994 bool saved_greater_than_is_operator_p
;
4996 /* Assume all the expressions will be constant. */
4998 *non_constant_p
= false;
5000 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
5001 return error_mark_node
;
5003 /* Within a parenthesized expression, a `>' token is always
5004 the greater-than operator. */
5005 saved_greater_than_is_operator_p
5006 = parser
->greater_than_is_operator_p
;
5007 parser
->greater_than_is_operator_p
= true;
5009 /* Consume expressions until there are no more. */
5010 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
5015 /* At the beginning of attribute lists, check to see if the
5016 next token is an identifier. */
5017 if (is_attribute_list
5018 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
5022 /* Consume the identifier. */
5023 token
= cp_lexer_consume_token (parser
->lexer
);
5024 /* Save the identifier. */
5025 identifier
= token
->u
.value
;
5029 /* Parse the next assignment-expression. */
5032 bool expr_non_constant_p
;
5033 expr
= (cp_parser_constant_expression
5034 (parser
, /*allow_non_constant_p=*/true,
5035 &expr_non_constant_p
));
5036 if (expr_non_constant_p
)
5037 *non_constant_p
= true;
5040 expr
= cp_parser_assignment_expression (parser
, cast_p
);
5043 expr
= fold_non_dependent_expr (expr
);
5045 /* If we have an ellipsis, then this is an expression
5047 if (allow_expansion_p
5048 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5050 /* Consume the `...'. */
5051 cp_lexer_consume_token (parser
->lexer
);
5053 /* Build the argument pack. */
5054 expr
= make_pack_expansion (expr
);
5057 /* Add it to the list. We add error_mark_node
5058 expressions to the list, so that we can still tell if
5059 the correct form for a parenthesized expression-list
5060 is found. That gives better errors. */
5061 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
5063 if (expr
== error_mark_node
)
5067 /* After the first item, attribute lists look the same as
5068 expression lists. */
5069 is_attribute_list
= false;
5072 /* If the next token isn't a `,', then we are done. */
5073 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5076 /* Otherwise, consume the `,' and keep going. */
5077 cp_lexer_consume_token (parser
->lexer
);
5080 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
5085 /* We try and resync to an unnested comma, as that will give the
5086 user better diagnostics. */
5087 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
5088 /*recovering=*/true,
5090 /*consume_paren=*/true);
5095 parser
->greater_than_is_operator_p
5096 = saved_greater_than_is_operator_p
;
5097 return error_mark_node
;
5101 parser
->greater_than_is_operator_p
5102 = saved_greater_than_is_operator_p
;
5104 /* We built up the list in reverse order so we must reverse it now. */
5105 expression_list
= nreverse (expression_list
);
5107 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
5109 return expression_list
;
5112 /* Parse a pseudo-destructor-name.
5114 pseudo-destructor-name:
5115 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5116 :: [opt] nested-name-specifier template template-id :: ~ type-name
5117 :: [opt] nested-name-specifier [opt] ~ type-name
5119 If either of the first two productions is used, sets *SCOPE to the
5120 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5121 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5122 or ERROR_MARK_NODE if the parse fails. */
5125 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
5129 bool nested_name_specifier_p
;
5131 /* Assume that things will not work out. */
5132 *type
= error_mark_node
;
5134 /* Look for the optional `::' operator. */
5135 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
5136 /* Look for the optional nested-name-specifier. */
5137 nested_name_specifier_p
5138 = (cp_parser_nested_name_specifier_opt (parser
,
5139 /*typename_keyword_p=*/false,
5140 /*check_dependency_p=*/true,
5142 /*is_declaration=*/true)
5144 /* Now, if we saw a nested-name-specifier, we might be doing the
5145 second production. */
5146 if (nested_name_specifier_p
5147 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
5149 /* Consume the `template' keyword. */
5150 cp_lexer_consume_token (parser
->lexer
);
5151 /* Parse the template-id. */
5152 cp_parser_template_id (parser
,
5153 /*template_keyword_p=*/true,
5154 /*check_dependency_p=*/false,
5155 /*is_declaration=*/true);
5156 /* Look for the `::' token. */
5157 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
5159 /* If the next token is not a `~', then there might be some
5160 additional qualification. */
5161 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
5163 /* Look for the type-name. */
5164 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
5166 if (*scope
== error_mark_node
)
5169 /* If we don't have ::~, then something has gone wrong. Since
5170 the only caller of this function is looking for something
5171 after `.' or `->' after a scalar type, most likely the
5172 program is trying to get a member of a non-aggregate
5174 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
)
5175 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_COMPL
)
5177 cp_parser_error (parser
, "request for member of non-aggregate type");
5181 /* Look for the `::' token. */
5182 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
5187 /* Look for the `~'. */
5188 cp_parser_require (parser
, CPP_COMPL
, "`~'");
5189 /* Look for the type-name again. We are not responsible for
5190 checking that it matches the first type-name. */
5191 *type
= cp_parser_type_name (parser
);
5194 /* Parse a unary-expression.
5200 unary-operator cast-expression
5201 sizeof unary-expression
5209 __extension__ cast-expression
5210 __alignof__ unary-expression
5211 __alignof__ ( type-id )
5212 __real__ cast-expression
5213 __imag__ cast-expression
5216 ADDRESS_P is true iff the unary-expression is appearing as the
5217 operand of the `&' operator. CAST_P is true if this expression is
5218 the target of a cast.
5220 Returns a representation of the expression. */
5223 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5226 enum tree_code unary_operator
;
5228 /* Peek at the next token. */
5229 token
= cp_lexer_peek_token (parser
->lexer
);
5230 /* Some keywords give away the kind of expression. */
5231 if (token
->type
== CPP_KEYWORD
)
5233 enum rid keyword
= token
->keyword
;
5243 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
5244 /* Consume the token. */
5245 cp_lexer_consume_token (parser
->lexer
);
5246 /* Parse the operand. */
5247 operand
= cp_parser_sizeof_operand (parser
, keyword
);
5249 if (TYPE_P (operand
))
5250 return cxx_sizeof_or_alignof_type (operand
, op
, true);
5252 return cxx_sizeof_or_alignof_expr (operand
, op
);
5256 return cp_parser_new_expression (parser
);
5259 return cp_parser_delete_expression (parser
);
5263 /* The saved value of the PEDANTIC flag. */
5267 /* Save away the PEDANTIC flag. */
5268 cp_parser_extension_opt (parser
, &saved_pedantic
);
5269 /* Parse the cast-expression. */
5270 expr
= cp_parser_simple_cast_expression (parser
);
5271 /* Restore the PEDANTIC flag. */
5272 pedantic
= saved_pedantic
;
5282 /* Consume the `__real__' or `__imag__' token. */
5283 cp_lexer_consume_token (parser
->lexer
);
5284 /* Parse the cast-expression. */
5285 expression
= cp_parser_simple_cast_expression (parser
);
5286 /* Create the complete representation. */
5287 return build_x_unary_op ((keyword
== RID_REALPART
5288 ? REALPART_EXPR
: IMAGPART_EXPR
),
5298 /* Look for the `:: new' and `:: delete', which also signal the
5299 beginning of a new-expression, or delete-expression,
5300 respectively. If the next token is `::', then it might be one of
5302 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
5306 /* See if the token after the `::' is one of the keywords in
5307 which we're interested. */
5308 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
5309 /* If it's `new', we have a new-expression. */
5310 if (keyword
== RID_NEW
)
5311 return cp_parser_new_expression (parser
);
5312 /* Similarly, for `delete'. */
5313 else if (keyword
== RID_DELETE
)
5314 return cp_parser_delete_expression (parser
);
5317 /* Look for a unary operator. */
5318 unary_operator
= cp_parser_unary_operator (token
);
5319 /* The `++' and `--' operators can be handled similarly, even though
5320 they are not technically unary-operators in the grammar. */
5321 if (unary_operator
== ERROR_MARK
)
5323 if (token
->type
== CPP_PLUS_PLUS
)
5324 unary_operator
= PREINCREMENT_EXPR
;
5325 else if (token
->type
== CPP_MINUS_MINUS
)
5326 unary_operator
= PREDECREMENT_EXPR
;
5327 /* Handle the GNU address-of-label extension. */
5328 else if (cp_parser_allow_gnu_extensions_p (parser
)
5329 && token
->type
== CPP_AND_AND
)
5334 /* Consume the '&&' token. */
5335 cp_lexer_consume_token (parser
->lexer
);
5336 /* Look for the identifier. */
5337 identifier
= cp_parser_identifier (parser
);
5338 /* Create an expression representing the address. */
5339 expression
= finish_label_address_expr (identifier
);
5340 if (cp_parser_non_integral_constant_expression (parser
,
5341 "the address of a label"))
5342 expression
= error_mark_node
;
5346 if (unary_operator
!= ERROR_MARK
)
5348 tree cast_expression
;
5349 tree expression
= error_mark_node
;
5350 const char *non_constant_p
= NULL
;
5352 /* Consume the operator token. */
5353 token
= cp_lexer_consume_token (parser
->lexer
);
5354 /* Parse the cast-expression. */
5356 = cp_parser_cast_expression (parser
,
5357 unary_operator
== ADDR_EXPR
,
5359 /* Now, build an appropriate representation. */
5360 switch (unary_operator
)
5363 non_constant_p
= "`*'";
5364 expression
= build_x_indirect_ref (cast_expression
, "unary *");
5368 non_constant_p
= "`&'";
5371 expression
= build_x_unary_op (unary_operator
, cast_expression
);
5374 case PREINCREMENT_EXPR
:
5375 case PREDECREMENT_EXPR
:
5376 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
5379 case UNARY_PLUS_EXPR
:
5381 case TRUTH_NOT_EXPR
:
5382 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
5390 && cp_parser_non_integral_constant_expression (parser
,
5392 expression
= error_mark_node
;
5397 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
5398 /*member_access_only_p=*/false);
5401 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5402 unary-operator, the corresponding tree code is returned. */
5404 static enum tree_code
5405 cp_parser_unary_operator (cp_token
* token
)
5407 switch (token
->type
)
5410 return INDIRECT_REF
;
5416 return UNARY_PLUS_EXPR
;
5422 return TRUTH_NOT_EXPR
;
5425 return BIT_NOT_EXPR
;
5432 /* Parse a new-expression.
5435 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5436 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5438 Returns a representation of the expression. */
5441 cp_parser_new_expression (cp_parser
* parser
)
5443 bool global_scope_p
;
5449 /* Look for the optional `::' operator. */
5451 = (cp_parser_global_scope_opt (parser
,
5452 /*current_scope_valid_p=*/false)
5454 /* Look for the `new' operator. */
5455 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
5456 /* There's no easy way to tell a new-placement from the
5457 `( type-id )' construct. */
5458 cp_parser_parse_tentatively (parser
);
5459 /* Look for a new-placement. */
5460 placement
= cp_parser_new_placement (parser
);
5461 /* If that didn't work out, there's no new-placement. */
5462 if (!cp_parser_parse_definitely (parser
))
5463 placement
= NULL_TREE
;
5465 /* If the next token is a `(', then we have a parenthesized
5467 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5469 /* Consume the `('. */
5470 cp_lexer_consume_token (parser
->lexer
);
5471 /* Parse the type-id. */
5472 type
= cp_parser_type_id (parser
);
5473 /* Look for the closing `)'. */
5474 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5475 /* There should not be a direct-new-declarator in this production,
5476 but GCC used to allowed this, so we check and emit a sensible error
5477 message for this case. */
5478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5480 error ("array bound forbidden after parenthesized type-id");
5481 inform ("try removing the parentheses around the type-id");
5482 cp_parser_direct_new_declarator (parser
);
5486 /* Otherwise, there must be a new-type-id. */
5488 type
= cp_parser_new_type_id (parser
, &nelts
);
5490 /* If the next token is a `(', then we have a new-initializer. */
5491 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5492 initializer
= cp_parser_new_initializer (parser
);
5494 initializer
= NULL_TREE
;
5496 /* A new-expression may not appear in an integral constant
5498 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
5499 return error_mark_node
;
5501 /* Create a representation of the new-expression. */
5502 return build_new (placement
, type
, nelts
, initializer
, global_scope_p
);
5505 /* Parse a new-placement.
5510 Returns the same representation as for an expression-list. */
5513 cp_parser_new_placement (cp_parser
* parser
)
5515 tree expression_list
;
5517 /* Parse the expression-list. */
5518 expression_list
= (cp_parser_parenthesized_expression_list
5519 (parser
, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5520 /*non_constant_p=*/NULL
));
5522 return expression_list
;
5525 /* Parse a new-type-id.
5528 type-specifier-seq new-declarator [opt]
5530 Returns the TYPE allocated. If the new-type-id indicates an array
5531 type, *NELTS is set to the number of elements in the last array
5532 bound; the TYPE will not include the last array bound. */
5535 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
5537 cp_decl_specifier_seq type_specifier_seq
;
5538 cp_declarator
*new_declarator
;
5539 cp_declarator
*declarator
;
5540 cp_declarator
*outer_declarator
;
5541 const char *saved_message
;
5544 /* The type-specifier sequence must not contain type definitions.
5545 (It cannot contain declarations of new types either, but if they
5546 are not definitions we will catch that because they are not
5548 saved_message
= parser
->type_definition_forbidden_message
;
5549 parser
->type_definition_forbidden_message
5550 = "types may not be defined in a new-type-id";
5551 /* Parse the type-specifier-seq. */
5552 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
5553 &type_specifier_seq
);
5554 /* Restore the old message. */
5555 parser
->type_definition_forbidden_message
= saved_message
;
5556 /* Parse the new-declarator. */
5557 new_declarator
= cp_parser_new_declarator_opt (parser
);
5559 /* Determine the number of elements in the last array dimension, if
5562 /* Skip down to the last array dimension. */
5563 declarator
= new_declarator
;
5564 outer_declarator
= NULL
;
5565 while (declarator
&& (declarator
->kind
== cdk_pointer
5566 || declarator
->kind
== cdk_ptrmem
))
5568 outer_declarator
= declarator
;
5569 declarator
= declarator
->declarator
;
5572 && declarator
->kind
== cdk_array
5573 && declarator
->declarator
5574 && declarator
->declarator
->kind
== cdk_array
)
5576 outer_declarator
= declarator
;
5577 declarator
= declarator
->declarator
;
5580 if (declarator
&& declarator
->kind
== cdk_array
)
5582 *nelts
= declarator
->u
.array
.bounds
;
5583 if (*nelts
== error_mark_node
)
5584 *nelts
= integer_one_node
;
5586 if (outer_declarator
)
5587 outer_declarator
->declarator
= declarator
->declarator
;
5589 new_declarator
= NULL
;
5592 type
= groktypename (&type_specifier_seq
, new_declarator
);
5596 /* Parse an (optional) new-declarator.
5599 ptr-operator new-declarator [opt]
5600 direct-new-declarator
5602 Returns the declarator. */
5604 static cp_declarator
*
5605 cp_parser_new_declarator_opt (cp_parser
* parser
)
5607 enum tree_code code
;
5609 cp_cv_quals cv_quals
;
5611 /* We don't know if there's a ptr-operator next, or not. */
5612 cp_parser_parse_tentatively (parser
);
5613 /* Look for a ptr-operator. */
5614 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
5615 /* If that worked, look for more new-declarators. */
5616 if (cp_parser_parse_definitely (parser
))
5618 cp_declarator
*declarator
;
5620 /* Parse another optional declarator. */
5621 declarator
= cp_parser_new_declarator_opt (parser
);
5623 return cp_parser_make_indirect_declarator
5624 (code
, type
, cv_quals
, declarator
);
5627 /* If the next token is a `[', there is a direct-new-declarator. */
5628 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5629 return cp_parser_direct_new_declarator (parser
);
5634 /* Parse a direct-new-declarator.
5636 direct-new-declarator:
5638 direct-new-declarator [constant-expression]
5642 static cp_declarator
*
5643 cp_parser_direct_new_declarator (cp_parser
* parser
)
5645 cp_declarator
*declarator
= NULL
;
5651 /* Look for the opening `['. */
5652 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
5653 /* The first expression is not required to be constant. */
5656 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
5657 /* The standard requires that the expression have integral
5658 type. DR 74 adds enumeration types. We believe that the
5659 real intent is that these expressions be handled like the
5660 expression in a `switch' condition, which also allows
5661 classes with a single conversion to integral or
5662 enumeration type. */
5663 if (!processing_template_decl
)
5666 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5671 error ("expression in new-declarator must have integral "
5672 "or enumeration type");
5673 expression
= error_mark_node
;
5677 /* But all the other expressions must be. */
5680 = cp_parser_constant_expression (parser
,
5681 /*allow_non_constant=*/false,
5683 /* Look for the closing `]'. */
5684 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5686 /* Add this bound to the declarator. */
5687 declarator
= make_array_declarator (declarator
, expression
);
5689 /* If the next token is not a `[', then there are no more
5691 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
5698 /* Parse a new-initializer.
5701 ( expression-list [opt] )
5703 Returns a representation of the expression-list. If there is no
5704 expression-list, VOID_ZERO_NODE is returned. */
5707 cp_parser_new_initializer (cp_parser
* parser
)
5709 tree expression_list
;
5711 expression_list
= (cp_parser_parenthesized_expression_list
5712 (parser
, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5713 /*non_constant_p=*/NULL
));
5714 if (!expression_list
)
5715 expression_list
= void_zero_node
;
5717 return expression_list
;
5720 /* Parse a delete-expression.
5723 :: [opt] delete cast-expression
5724 :: [opt] delete [ ] cast-expression
5726 Returns a representation of the expression. */
5729 cp_parser_delete_expression (cp_parser
* parser
)
5731 bool global_scope_p
;
5735 /* Look for the optional `::' operator. */
5737 = (cp_parser_global_scope_opt (parser
,
5738 /*current_scope_valid_p=*/false)
5740 /* Look for the `delete' keyword. */
5741 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
5742 /* See if the array syntax is in use. */
5743 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5745 /* Consume the `[' token. */
5746 cp_lexer_consume_token (parser
->lexer
);
5747 /* Look for the `]' token. */
5748 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5749 /* Remember that this is the `[]' construct. */
5755 /* Parse the cast-expression. */
5756 expression
= cp_parser_simple_cast_expression (parser
);
5758 /* A delete-expression may not appear in an integral constant
5760 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
5761 return error_mark_node
;
5763 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
5766 /* Parse a cast-expression.
5770 ( type-id ) cast-expression
5772 ADDRESS_P is true iff the unary-expression is appearing as the
5773 operand of the `&' operator. CAST_P is true if this expression is
5774 the target of a cast.
5776 Returns a representation of the expression. */
5779 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5781 /* If it's a `(', then we might be looking at a cast. */
5782 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5784 tree type
= NULL_TREE
;
5785 tree expr
= NULL_TREE
;
5786 bool compound_literal_p
;
5787 const char *saved_message
;
5789 /* There's no way to know yet whether or not this is a cast.
5790 For example, `(int (3))' is a unary-expression, while `(int)
5791 3' is a cast. So, we resort to parsing tentatively. */
5792 cp_parser_parse_tentatively (parser
);
5793 /* Types may not be defined in a cast. */
5794 saved_message
= parser
->type_definition_forbidden_message
;
5795 parser
->type_definition_forbidden_message
5796 = "types may not be defined in casts";
5797 /* Consume the `('. */
5798 cp_lexer_consume_token (parser
->lexer
);
5799 /* A very tricky bit is that `(struct S) { 3 }' is a
5800 compound-literal (which we permit in C++ as an extension).
5801 But, that construct is not a cast-expression -- it is a
5802 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5803 is legal; if the compound-literal were a cast-expression,
5804 you'd need an extra set of parentheses.) But, if we parse
5805 the type-id, and it happens to be a class-specifier, then we
5806 will commit to the parse at that point, because we cannot
5807 undo the action that is done when creating a new class. So,
5808 then we cannot back up and do a postfix-expression.
5810 Therefore, we scan ahead to the closing `)', and check to see
5811 if the token after the `)' is a `{'. If so, we are not
5812 looking at a cast-expression.
5814 Save tokens so that we can put them back. */
5815 cp_lexer_save_tokens (parser
->lexer
);
5816 /* Skip tokens until the next token is a closing parenthesis.
5817 If we find the closing `)', and the next token is a `{', then
5818 we are looking at a compound-literal. */
5820 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5821 /*consume_paren=*/true)
5822 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5823 /* Roll back the tokens we skipped. */
5824 cp_lexer_rollback_tokens (parser
->lexer
);
5825 /* If we were looking at a compound-literal, simulate an error
5826 so that the call to cp_parser_parse_definitely below will
5828 if (compound_literal_p
)
5829 cp_parser_simulate_error (parser
);
5832 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5833 parser
->in_type_id_in_expr_p
= true;
5834 /* Look for the type-id. */
5835 type
= cp_parser_type_id (parser
);
5836 /* Look for the closing `)'. */
5837 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5838 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5841 /* Restore the saved message. */
5842 parser
->type_definition_forbidden_message
= saved_message
;
5844 /* If ok so far, parse the dependent expression. We cannot be
5845 sure it is a cast. Consider `(T ())'. It is a parenthesized
5846 ctor of T, but looks like a cast to function returning T
5847 without a dependent expression. */
5848 if (!cp_parser_error_occurred (parser
))
5849 expr
= cp_parser_cast_expression (parser
,
5850 /*address_p=*/false,
5853 if (cp_parser_parse_definitely (parser
))
5855 /* Warn about old-style casts, if so requested. */
5856 if (warn_old_style_cast
5857 && !in_system_header
5858 && !VOID_TYPE_P (type
)
5859 && current_lang_name
!= lang_name_c
)
5860 warning (OPT_Wold_style_cast
, "use of old-style cast");
5862 /* Only type conversions to integral or enumeration types
5863 can be used in constant-expressions. */
5864 if (!cast_valid_in_integral_constant_expression_p (type
)
5865 && (cp_parser_non_integral_constant_expression
5867 "a cast to a type other than an integral or "
5868 "enumeration type")))
5869 return error_mark_node
;
5871 /* Perform the cast. */
5872 expr
= build_c_cast (type
, expr
);
5877 /* If we get here, then it's not a cast, so it must be a
5878 unary-expression. */
5879 return cp_parser_unary_expression (parser
, address_p
, cast_p
);
5882 /* Parse a binary expression of the general form:
5886 pm-expression .* cast-expression
5887 pm-expression ->* cast-expression
5889 multiplicative-expression:
5891 multiplicative-expression * pm-expression
5892 multiplicative-expression / pm-expression
5893 multiplicative-expression % pm-expression
5895 additive-expression:
5896 multiplicative-expression
5897 additive-expression + multiplicative-expression
5898 additive-expression - multiplicative-expression
5902 shift-expression << additive-expression
5903 shift-expression >> additive-expression
5905 relational-expression:
5907 relational-expression < shift-expression
5908 relational-expression > shift-expression
5909 relational-expression <= shift-expression
5910 relational-expression >= shift-expression
5914 relational-expression:
5915 relational-expression <? shift-expression
5916 relational-expression >? shift-expression
5918 equality-expression:
5919 relational-expression
5920 equality-expression == relational-expression
5921 equality-expression != relational-expression
5925 and-expression & equality-expression
5927 exclusive-or-expression:
5929 exclusive-or-expression ^ and-expression
5931 inclusive-or-expression:
5932 exclusive-or-expression
5933 inclusive-or-expression | exclusive-or-expression
5935 logical-and-expression:
5936 inclusive-or-expression
5937 logical-and-expression && inclusive-or-expression
5939 logical-or-expression:
5940 logical-and-expression
5941 logical-or-expression || logical-and-expression
5943 All these are implemented with a single function like:
5946 simple-cast-expression
5947 binary-expression <token> binary-expression
5949 CAST_P is true if this expression is the target of a cast.
5951 The binops_by_token map is used to get the tree codes for each <token> type.
5952 binary-expressions are associated according to a precedence table. */
5954 #define TOKEN_PRECEDENCE(token) \
5955 (((token->type == CPP_GREATER \
5956 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5957 && !parser->greater_than_is_operator_p) \
5958 ? PREC_NOT_OPERATOR \
5959 : binops_by_token[token->type].prec)
5962 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
)
5964 cp_parser_expression_stack stack
;
5965 cp_parser_expression_stack_entry
*sp
= &stack
[0];
5968 enum tree_code tree_type
, lhs_type
, rhs_type
;
5969 enum cp_parser_prec prec
= PREC_NOT_OPERATOR
, new_prec
, lookahead_prec
;
5972 /* Parse the first expression. */
5973 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
);
5974 lhs_type
= ERROR_MARK
;
5978 /* Get an operator token. */
5979 token
= cp_lexer_peek_token (parser
->lexer
);
5981 if (warn_cxx0x_compat
5982 && token
->type
== CPP_RSHIFT
5983 && !parser
->greater_than_is_operator_p
)
5985 warning (OPT_Wc__0x_compat
,
5986 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5988 warning (OPT_Wc__0x_compat
,
5989 "suggest parentheses around %<>>%> expression");
5992 new_prec
= TOKEN_PRECEDENCE (token
);
5994 /* Popping an entry off the stack means we completed a subexpression:
5995 - either we found a token which is not an operator (`>' where it is not
5996 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5997 will happen repeatedly;
5998 - or, we found an operator which has lower priority. This is the case
5999 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6001 if (new_prec
<= prec
)
6010 tree_type
= binops_by_token
[token
->type
].tree_type
;
6012 /* We used the operator token. */
6013 cp_lexer_consume_token (parser
->lexer
);
6015 /* Extract another operand. It may be the RHS of this expression
6016 or the LHS of a new, higher priority expression. */
6017 rhs
= cp_parser_simple_cast_expression (parser
);
6018 rhs_type
= ERROR_MARK
;
6020 /* Get another operator token. Look up its precedence to avoid
6021 building a useless (immediately popped) stack entry for common
6022 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6023 token
= cp_lexer_peek_token (parser
->lexer
);
6024 lookahead_prec
= TOKEN_PRECEDENCE (token
);
6025 if (lookahead_prec
> new_prec
)
6027 /* ... and prepare to parse the RHS of the new, higher priority
6028 expression. Since precedence levels on the stack are
6029 monotonically increasing, we do not have to care about
6032 sp
->tree_type
= tree_type
;
6034 sp
->lhs_type
= lhs_type
;
6037 lhs_type
= rhs_type
;
6039 new_prec
= lookahead_prec
;
6043 /* If the stack is not empty, we have parsed into LHS the right side
6044 (`4' in the example above) of an expression we had suspended.
6045 We can use the information on the stack to recover the LHS (`3')
6046 from the stack together with the tree code (`MULT_EXPR'), and
6047 the precedence of the higher level subexpression
6048 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6049 which will be used to actually build the additive expression. */
6052 tree_type
= sp
->tree_type
;
6054 rhs_type
= lhs_type
;
6056 lhs_type
= sp
->lhs_type
;
6059 overloaded_p
= false;
6060 lhs
= build_x_binary_op (tree_type
, lhs
, lhs_type
, rhs
, rhs_type
,
6062 lhs_type
= tree_type
;
6064 /* If the binary operator required the use of an overloaded operator,
6065 then this expression cannot be an integral constant-expression.
6066 An overloaded operator can be used even if both operands are
6067 otherwise permissible in an integral constant-expression if at
6068 least one of the operands is of enumeration type. */
6071 && (cp_parser_non_integral_constant_expression
6072 (parser
, "calls to overloaded operators")))
6073 return error_mark_node
;
6080 /* Parse the `? expression : assignment-expression' part of a
6081 conditional-expression. The LOGICAL_OR_EXPR is the
6082 logical-or-expression that started the conditional-expression.
6083 Returns a representation of the entire conditional-expression.
6085 This routine is used by cp_parser_assignment_expression.
6087 ? expression : assignment-expression
6091 ? : assignment-expression */
6094 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
6097 tree assignment_expr
;
6099 /* Consume the `?' token. */
6100 cp_lexer_consume_token (parser
->lexer
);
6101 if (cp_parser_allow_gnu_extensions_p (parser
)
6102 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
6103 /* Implicit true clause. */
6106 /* Parse the expression. */
6107 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
6109 /* The next token should be a `:'. */
6110 cp_parser_require (parser
, CPP_COLON
, "`:'");
6111 /* Parse the assignment-expression. */
6112 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
6114 /* Build the conditional-expression. */
6115 return build_x_conditional_expr (logical_or_expr
,
6120 /* Parse an assignment-expression.
6122 assignment-expression:
6123 conditional-expression
6124 logical-or-expression assignment-operator assignment_expression
6127 CAST_P is true if this expression is the target of a cast.
6129 Returns a representation for the expression. */
6132 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
)
6136 /* If the next token is the `throw' keyword, then we're looking at
6137 a throw-expression. */
6138 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
6139 expr
= cp_parser_throw_expression (parser
);
6140 /* Otherwise, it must be that we are looking at a
6141 logical-or-expression. */
6144 /* Parse the binary expressions (logical-or-expression). */
6145 expr
= cp_parser_binary_expression (parser
, cast_p
);
6146 /* If the next token is a `?' then we're actually looking at a
6147 conditional-expression. */
6148 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
6149 return cp_parser_question_colon_clause (parser
, expr
);
6152 enum tree_code assignment_operator
;
6154 /* If it's an assignment-operator, we're using the second
6157 = cp_parser_assignment_operator_opt (parser
);
6158 if (assignment_operator
!= ERROR_MARK
)
6162 /* Parse the right-hand side of the assignment. */
6163 rhs
= cp_parser_assignment_expression (parser
, cast_p
);
6164 /* An assignment may not appear in a
6165 constant-expression. */
6166 if (cp_parser_non_integral_constant_expression (parser
,
6168 return error_mark_node
;
6169 /* Build the assignment expression. */
6170 expr
= build_x_modify_expr (expr
,
6171 assignment_operator
,
6180 /* Parse an (optional) assignment-operator.
6182 assignment-operator: one of
6183 = *= /= %= += -= >>= <<= &= ^= |=
6187 assignment-operator: one of
6190 If the next token is an assignment operator, the corresponding tree
6191 code is returned, and the token is consumed. For example, for
6192 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6193 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6194 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6195 operator, ERROR_MARK is returned. */
6197 static enum tree_code
6198 cp_parser_assignment_operator_opt (cp_parser
* parser
)
6203 /* Peek at the next toen. */
6204 token
= cp_lexer_peek_token (parser
->lexer
);
6206 switch (token
->type
)
6217 op
= TRUNC_DIV_EXPR
;
6221 op
= TRUNC_MOD_EXPR
;
6253 /* Nothing else is an assignment operator. */
6257 /* If it was an assignment operator, consume it. */
6258 if (op
!= ERROR_MARK
)
6259 cp_lexer_consume_token (parser
->lexer
);
6264 /* Parse an expression.
6267 assignment-expression
6268 expression , assignment-expression
6270 CAST_P is true if this expression is the target of a cast.
6272 Returns a representation of the expression. */
6275 cp_parser_expression (cp_parser
* parser
, bool cast_p
)
6277 tree expression
= NULL_TREE
;
6281 tree assignment_expression
;
6283 /* Parse the next assignment-expression. */
6284 assignment_expression
6285 = cp_parser_assignment_expression (parser
, cast_p
);
6286 /* If this is the first assignment-expression, we can just
6289 expression
= assignment_expression
;
6291 expression
= build_x_compound_expr (expression
,
6292 assignment_expression
);
6293 /* If the next token is not a comma, then we are done with the
6295 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6297 /* Consume the `,'. */
6298 cp_lexer_consume_token (parser
->lexer
);
6299 /* A comma operator cannot appear in a constant-expression. */
6300 if (cp_parser_non_integral_constant_expression (parser
,
6301 "a comma operator"))
6302 expression
= error_mark_node
;
6308 /* Parse a constant-expression.
6310 constant-expression:
6311 conditional-expression
6313 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6314 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6315 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6316 is false, NON_CONSTANT_P should be NULL. */
6319 cp_parser_constant_expression (cp_parser
* parser
,
6320 bool allow_non_constant_p
,
6321 bool *non_constant_p
)
6323 bool saved_integral_constant_expression_p
;
6324 bool saved_allow_non_integral_constant_expression_p
;
6325 bool saved_non_integral_constant_expression_p
;
6328 /* It might seem that we could simply parse the
6329 conditional-expression, and then check to see if it were
6330 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6331 one that the compiler can figure out is constant, possibly after
6332 doing some simplifications or optimizations. The standard has a
6333 precise definition of constant-expression, and we must honor
6334 that, even though it is somewhat more restrictive.
6340 is not a legal declaration, because `(2, 3)' is not a
6341 constant-expression. The `,' operator is forbidden in a
6342 constant-expression. However, GCC's constant-folding machinery
6343 will fold this operation to an INTEGER_CST for `3'. */
6345 /* Save the old settings. */
6346 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
6347 saved_allow_non_integral_constant_expression_p
6348 = parser
->allow_non_integral_constant_expression_p
;
6349 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
6350 /* We are now parsing a constant-expression. */
6351 parser
->integral_constant_expression_p
= true;
6352 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
6353 parser
->non_integral_constant_expression_p
= false;
6354 /* Although the grammar says "conditional-expression", we parse an
6355 "assignment-expression", which also permits "throw-expression"
6356 and the use of assignment operators. In the case that
6357 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6358 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6359 actually essential that we look for an assignment-expression.
6360 For example, cp_parser_initializer_clauses uses this function to
6361 determine whether a particular assignment-expression is in fact
6363 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
6364 /* Restore the old settings. */
6365 parser
->integral_constant_expression_p
6366 = saved_integral_constant_expression_p
;
6367 parser
->allow_non_integral_constant_expression_p
6368 = saved_allow_non_integral_constant_expression_p
;
6369 if (allow_non_constant_p
)
6370 *non_constant_p
= parser
->non_integral_constant_expression_p
;
6371 else if (parser
->non_integral_constant_expression_p
)
6372 expression
= error_mark_node
;
6373 parser
->non_integral_constant_expression_p
6374 = saved_non_integral_constant_expression_p
;
6379 /* Parse __builtin_offsetof.
6381 offsetof-expression:
6382 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6384 offsetof-member-designator:
6386 | offsetof-member-designator "." id-expression
6387 | offsetof-member-designator "[" expression "]" */
6390 cp_parser_builtin_offsetof (cp_parser
*parser
)
6392 int save_ice_p
, save_non_ice_p
;
6396 /* We're about to accept non-integral-constant things, but will
6397 definitely yield an integral constant expression. Save and
6398 restore these values around our local parsing. */
6399 save_ice_p
= parser
->integral_constant_expression_p
;
6400 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
6402 /* Consume the "__builtin_offsetof" token. */
6403 cp_lexer_consume_token (parser
->lexer
);
6404 /* Consume the opening `('. */
6405 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6406 /* Parse the type-id. */
6407 type
= cp_parser_type_id (parser
);
6408 /* Look for the `,'. */
6409 cp_parser_require (parser
, CPP_COMMA
, "`,'");
6411 /* Build the (type *)null that begins the traditional offsetof macro. */
6412 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
);
6414 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6415 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
6419 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6420 switch (token
->type
)
6422 case CPP_OPEN_SQUARE
:
6423 /* offsetof-member-designator "[" expression "]" */
6424 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
6428 /* offsetof-member-designator "." identifier */
6429 cp_lexer_consume_token (parser
->lexer
);
6430 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
, expr
,
6434 case CPP_CLOSE_PAREN
:
6435 /* Consume the ")" token. */
6436 cp_lexer_consume_token (parser
->lexer
);
6440 /* Error. We know the following require will fail, but
6441 that gives the proper error message. */
6442 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6443 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
6444 expr
= error_mark_node
;
6450 /* If we're processing a template, we can't finish the semantics yet.
6451 Otherwise we can fold the entire expression now. */
6452 if (processing_template_decl
)
6453 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
6455 expr
= finish_offsetof (expr
);
6458 parser
->integral_constant_expression_p
= save_ice_p
;
6459 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
6464 /* Parse a trait expression. */
6467 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
6470 tree type1
, type2
= NULL_TREE
;
6471 bool binary
= false;
6472 cp_decl_specifier_seq decl_specs
;
6476 case RID_HAS_NOTHROW_ASSIGN
:
6477 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
6479 case RID_HAS_NOTHROW_CONSTRUCTOR
:
6480 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
6482 case RID_HAS_NOTHROW_COPY
:
6483 kind
= CPTK_HAS_NOTHROW_COPY
;
6485 case RID_HAS_TRIVIAL_ASSIGN
:
6486 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
6488 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
6489 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
6491 case RID_HAS_TRIVIAL_COPY
:
6492 kind
= CPTK_HAS_TRIVIAL_COPY
;
6494 case RID_HAS_TRIVIAL_DESTRUCTOR
:
6495 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
6497 case RID_HAS_VIRTUAL_DESTRUCTOR
:
6498 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
6500 case RID_IS_ABSTRACT
:
6501 kind
= CPTK_IS_ABSTRACT
;
6503 case RID_IS_BASE_OF
:
6504 kind
= CPTK_IS_BASE_OF
;
6508 kind
= CPTK_IS_CLASS
;
6510 case RID_IS_CONVERTIBLE_TO
:
6511 kind
= CPTK_IS_CONVERTIBLE_TO
;
6515 kind
= CPTK_IS_EMPTY
;
6518 kind
= CPTK_IS_ENUM
;
6523 case RID_IS_POLYMORPHIC
:
6524 kind
= CPTK_IS_POLYMORPHIC
;
6527 kind
= CPTK_IS_UNION
;
6533 /* Consume the token. */
6534 cp_lexer_consume_token (parser
->lexer
);
6536 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6538 type1
= cp_parser_type_id (parser
);
6540 if (type1
== error_mark_node
)
6541 return error_mark_node
;
6543 /* Build a trivial decl-specifier-seq. */
6544 clear_decl_specs (&decl_specs
);
6545 decl_specs
.type
= type1
;
6547 /* Call grokdeclarator to figure out what type this is. */
6548 type1
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
6549 /*initialized=*/0, /*attrlist=*/NULL
);
6553 cp_parser_require (parser
, CPP_COMMA
, "`,'");
6555 type2
= cp_parser_type_id (parser
);
6557 if (type2
== error_mark_node
)
6558 return error_mark_node
;
6560 /* Build a trivial decl-specifier-seq. */
6561 clear_decl_specs (&decl_specs
);
6562 decl_specs
.type
= type2
;
6564 /* Call grokdeclarator to figure out what type this is. */
6565 type2
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
6566 /*initialized=*/0, /*attrlist=*/NULL
);
6569 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6571 /* Complete the trait expression, which may mean either processing
6572 the trait expr now or saving it for template instantiation. */
6573 return finish_trait_expr (kind
, type1
, type2
);
6576 /* Statements [gram.stmt.stmt] */
6578 /* Parse a statement.
6582 expression-statement
6587 declaration-statement
6590 IN_COMPOUND is true when the statement is nested inside a
6591 cp_parser_compound_statement; this matters for certain pragmas.
6593 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6594 is a (possibly labeled) if statement which is not enclosed in braces
6595 and has an else clause. This is used to implement -Wparentheses. */
6598 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
6599 bool in_compound
, bool *if_p
)
6603 location_t statement_location
;
6608 /* There is no statement yet. */
6609 statement
= NULL_TREE
;
6610 /* Peek at the next token. */
6611 token
= cp_lexer_peek_token (parser
->lexer
);
6612 /* Remember the location of the first token in the statement. */
6613 statement_location
= token
->location
;
6614 /* If this is a keyword, then that will often determine what kind of
6615 statement we have. */
6616 if (token
->type
== CPP_KEYWORD
)
6618 enum rid keyword
= token
->keyword
;
6624 /* Looks like a labeled-statement with a case label.
6625 Parse the label, and then use tail recursion to parse
6627 cp_parser_label_for_labeled_statement (parser
);
6632 statement
= cp_parser_selection_statement (parser
, if_p
);
6638 statement
= cp_parser_iteration_statement (parser
);
6645 statement
= cp_parser_jump_statement (parser
);
6648 /* Objective-C++ exception-handling constructs. */
6651 case RID_AT_FINALLY
:
6652 case RID_AT_SYNCHRONIZED
:
6654 statement
= cp_parser_objc_statement (parser
);
6658 statement
= cp_parser_try_block (parser
);
6662 /* This must be a namespace alias definition. */
6663 cp_parser_declaration_statement (parser
);
6667 /* It might be a keyword like `int' that can start a
6668 declaration-statement. */
6672 else if (token
->type
== CPP_NAME
)
6674 /* If the next token is a `:', then we are looking at a
6675 labeled-statement. */
6676 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6677 if (token
->type
== CPP_COLON
)
6679 /* Looks like a labeled-statement with an ordinary label.
6680 Parse the label, and then use tail recursion to parse
6682 cp_parser_label_for_labeled_statement (parser
);
6686 /* Anything that starts with a `{' must be a compound-statement. */
6687 else if (token
->type
== CPP_OPEN_BRACE
)
6688 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6689 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6690 a statement all its own. */
6691 else if (token
->type
== CPP_PRAGMA
)
6693 /* Only certain OpenMP pragmas are attached to statements, and thus
6694 are considered statements themselves. All others are not. In
6695 the context of a compound, accept the pragma as a "statement" and
6696 return so that we can check for a close brace. Otherwise we
6697 require a real statement and must go back and read one. */
6699 cp_parser_pragma (parser
, pragma_compound
);
6700 else if (!cp_parser_pragma (parser
, pragma_stmt
))
6704 else if (token
->type
== CPP_EOF
)
6706 cp_parser_error (parser
, "expected statement");
6710 /* Everything else must be a declaration-statement or an
6711 expression-statement. Try for the declaration-statement
6712 first, unless we are looking at a `;', in which case we know that
6713 we have an expression-statement. */
6716 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6718 cp_parser_parse_tentatively (parser
);
6719 /* Try to parse the declaration-statement. */
6720 cp_parser_declaration_statement (parser
);
6721 /* If that worked, we're done. */
6722 if (cp_parser_parse_definitely (parser
))
6725 /* Look for an expression-statement instead. */
6726 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
6729 /* Set the line number for the statement. */
6730 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
6731 SET_EXPR_LOCATION (statement
, statement_location
);
6734 /* Parse the label for a labeled-statement, i.e.
6737 case constant-expression :
6741 case constant-expression ... constant-expression : statement
6743 When a label is parsed without errors, the label is added to the
6744 parse tree by the finish_* functions, so this function doesn't
6745 have to return the label. */
6748 cp_parser_label_for_labeled_statement (cp_parser
* parser
)
6752 /* The next token should be an identifier. */
6753 token
= cp_lexer_peek_token (parser
->lexer
);
6754 if (token
->type
!= CPP_NAME
6755 && token
->type
!= CPP_KEYWORD
)
6757 cp_parser_error (parser
, "expected labeled-statement");
6761 switch (token
->keyword
)
6768 /* Consume the `case' token. */
6769 cp_lexer_consume_token (parser
->lexer
);
6770 /* Parse the constant-expression. */
6771 expr
= cp_parser_constant_expression (parser
,
6772 /*allow_non_constant_p=*/false,
6775 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
6776 if (ellipsis
->type
== CPP_ELLIPSIS
)
6778 /* Consume the `...' token. */
6779 cp_lexer_consume_token (parser
->lexer
);
6781 cp_parser_constant_expression (parser
,
6782 /*allow_non_constant_p=*/false,
6784 /* We don't need to emit warnings here, as the common code
6785 will do this for us. */
6788 expr_hi
= NULL_TREE
;
6790 if (parser
->in_switch_statement_p
)
6791 finish_case_label (expr
, expr_hi
);
6793 error ("case label %qE not within a switch statement", expr
);
6798 /* Consume the `default' token. */
6799 cp_lexer_consume_token (parser
->lexer
);
6801 if (parser
->in_switch_statement_p
)
6802 finish_case_label (NULL_TREE
, NULL_TREE
);
6804 error ("case label not within a switch statement");
6808 /* Anything else must be an ordinary label. */
6809 finish_label_stmt (cp_parser_identifier (parser
));
6813 /* Require the `:' token. */
6814 cp_parser_require (parser
, CPP_COLON
, "`:'");
6817 /* Parse an expression-statement.
6819 expression-statement:
6822 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6823 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6824 indicates whether this expression-statement is part of an
6825 expression statement. */
6828 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
6830 tree statement
= NULL_TREE
;
6832 /* If the next token is a ';', then there is no expression
6834 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6835 statement
= cp_parser_expression (parser
, /*cast_p=*/false);
6837 /* Consume the final `;'. */
6838 cp_parser_consume_semicolon_at_end_of_statement (parser
);
6840 if (in_statement_expr
6841 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
6842 /* This is the final expression statement of a statement
6844 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
6846 statement
= finish_expr_stmt (statement
);
6853 /* Parse a compound-statement.
6856 { statement-seq [opt] }
6861 { label-declaration-seq [opt] statement-seq [opt] }
6863 label-declaration-seq:
6865 label-declaration-seq label-declaration
6867 Returns a tree representing the statement. */
6870 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
6875 /* Consume the `{'. */
6876 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
6877 return error_mark_node
;
6878 /* Begin the compound-statement. */
6879 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
6880 /* If the next keyword is `__label__' we have a label declaration. */
6881 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
6882 cp_parser_label_declaration (parser
);
6883 /* Parse an (optional) statement-seq. */
6884 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
6885 /* Finish the compound-statement. */
6886 finish_compound_stmt (compound_stmt
);
6887 /* Consume the `}'. */
6888 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6890 return compound_stmt
;
6893 /* Parse an (optional) statement-seq.
6897 statement-seq [opt] statement */
6900 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
6902 /* Scan statements until there aren't any more. */
6905 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6907 /* If we're looking at a `}', then we've run out of statements. */
6908 if (token
->type
== CPP_CLOSE_BRACE
6909 || token
->type
== CPP_EOF
6910 || token
->type
== CPP_PRAGMA_EOL
)
6913 /* If we are in a compound statement and find 'else' then
6914 something went wrong. */
6915 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
6917 if (parser
->in_statement
& IN_IF_STMT
)
6921 token
= cp_lexer_consume_token (parser
->lexer
);
6922 error ("%<else%> without a previous %<if%>");
6926 /* Parse the statement. */
6927 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
6931 /* Parse a selection-statement.
6933 selection-statement:
6934 if ( condition ) statement
6935 if ( condition ) statement else statement
6936 switch ( condition ) statement
6938 Returns the new IF_STMT or SWITCH_STMT.
6940 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6941 is a (possibly labeled) if statement which is not enclosed in
6942 braces and has an else clause. This is used to implement
6946 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
6954 /* Peek at the next token. */
6955 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
6957 /* See what kind of keyword it is. */
6958 keyword
= token
->keyword
;
6967 /* Look for the `('. */
6968 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
6970 cp_parser_skip_to_end_of_statement (parser
);
6971 return error_mark_node
;
6974 /* Begin the selection-statement. */
6975 if (keyword
== RID_IF
)
6976 statement
= begin_if_stmt ();
6978 statement
= begin_switch_stmt ();
6980 /* Parse the condition. */
6981 condition
= cp_parser_condition (parser
);
6982 /* Look for the `)'. */
6983 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
6984 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
6985 /*consume_paren=*/true);
6987 if (keyword
== RID_IF
)
6990 unsigned char in_statement
;
6992 /* Add the condition. */
6993 finish_if_stmt_cond (condition
, statement
);
6995 /* Parse the then-clause. */
6996 in_statement
= parser
->in_statement
;
6997 parser
->in_statement
|= IN_IF_STMT
;
6998 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
6999 parser
->in_statement
= in_statement
;
7001 finish_then_clause (statement
);
7003 /* If the next token is `else', parse the else-clause. */
7004 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
7007 /* Consume the `else' keyword. */
7008 cp_lexer_consume_token (parser
->lexer
);
7009 begin_else_clause (statement
);
7010 /* Parse the else-clause. */
7011 cp_parser_implicitly_scoped_statement (parser
, NULL
);
7012 finish_else_clause (statement
);
7014 /* If we are currently parsing a then-clause, then
7015 IF_P will not be NULL. We set it to true to
7016 indicate that this if statement has an else clause.
7017 This may trigger the Wparentheses warning below
7018 when we get back up to the parent if statement. */
7024 /* This if statement does not have an else clause. If
7025 NESTED_IF is true, then the then-clause is an if
7026 statement which does have an else clause. We warn
7027 about the potential ambiguity. */
7029 warning (OPT_Wparentheses
,
7030 ("%Hsuggest explicit braces "
7031 "to avoid ambiguous %<else%>"),
7032 EXPR_LOCUS (statement
));
7035 /* Now we're all done with the if-statement. */
7036 finish_if_stmt (statement
);
7040 bool in_switch_statement_p
;
7041 unsigned char in_statement
;
7043 /* Add the condition. */
7044 finish_switch_cond (condition
, statement
);
7046 /* Parse the body of the switch-statement. */
7047 in_switch_statement_p
= parser
->in_switch_statement_p
;
7048 in_statement
= parser
->in_statement
;
7049 parser
->in_switch_statement_p
= true;
7050 parser
->in_statement
|= IN_SWITCH_STMT
;
7051 cp_parser_implicitly_scoped_statement (parser
, NULL
);
7052 parser
->in_switch_statement_p
= in_switch_statement_p
;
7053 parser
->in_statement
= in_statement
;
7055 /* Now we're all done with the switch-statement. */
7056 finish_switch_stmt (statement
);
7064 cp_parser_error (parser
, "expected selection-statement");
7065 return error_mark_node
;
7069 /* Parse a condition.
7073 type-specifier-seq declarator = assignment-expression
7078 type-specifier-seq declarator asm-specification [opt]
7079 attributes [opt] = assignment-expression
7081 Returns the expression that should be tested. */
7084 cp_parser_condition (cp_parser
* parser
)
7086 cp_decl_specifier_seq type_specifiers
;
7087 const char *saved_message
;
7089 /* Try the declaration first. */
7090 cp_parser_parse_tentatively (parser
);
7091 /* New types are not allowed in the type-specifier-seq for a
7093 saved_message
= parser
->type_definition_forbidden_message
;
7094 parser
->type_definition_forbidden_message
7095 = "types may not be defined in conditions";
7096 /* Parse the type-specifier-seq. */
7097 cp_parser_type_specifier_seq (parser
, /*is_condition==*/true,
7099 /* Restore the saved message. */
7100 parser
->type_definition_forbidden_message
= saved_message
;
7101 /* If all is well, we might be looking at a declaration. */
7102 if (!cp_parser_error_occurred (parser
))
7105 tree asm_specification
;
7107 cp_declarator
*declarator
;
7108 tree initializer
= NULL_TREE
;
7110 /* Parse the declarator. */
7111 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
7112 /*ctor_dtor_or_conv_p=*/NULL
,
7113 /*parenthesized_p=*/NULL
,
7114 /*member_p=*/false);
7115 /* Parse the attributes. */
7116 attributes
= cp_parser_attributes_opt (parser
);
7117 /* Parse the asm-specification. */
7118 asm_specification
= cp_parser_asm_specification_opt (parser
);
7119 /* If the next token is not an `=', then we might still be
7120 looking at an expression. For example:
7124 looks like a decl-specifier-seq and a declarator -- but then
7125 there is no `=', so this is an expression. */
7126 cp_parser_require (parser
, CPP_EQ
, "`='");
7127 /* If we did see an `=', then we are looking at a declaration
7129 if (cp_parser_parse_definitely (parser
))
7132 bool non_constant_p
;
7134 /* Create the declaration. */
7135 decl
= start_decl (declarator
, &type_specifiers
,
7136 /*initialized_p=*/true,
7137 attributes
, /*prefix_attributes=*/NULL_TREE
,
7139 /* Parse the assignment-expression. */
7141 = cp_parser_constant_expression (parser
,
7142 /*allow_non_constant_p=*/true,
7144 if (!non_constant_p
)
7145 initializer
= fold_non_dependent_expr (initializer
);
7147 /* Process the initializer. */
7148 cp_finish_decl (decl
,
7149 initializer
, !non_constant_p
,
7151 LOOKUP_ONLYCONVERTING
);
7154 pop_scope (pushed_scope
);
7156 return convert_from_reference (decl
);
7159 /* If we didn't even get past the declarator successfully, we are
7160 definitely not looking at a declaration. */
7162 cp_parser_abort_tentative_parse (parser
);
7164 /* Otherwise, we are looking at an expression. */
7165 return cp_parser_expression (parser
, /*cast_p=*/false);
7168 /* We check for a ) immediately followed by ; with no whitespacing
7169 between. This is used to issue a warning for:
7177 as the semicolon is probably extraneous.
7179 On parse errors, the next token might not be a ), so do nothing in
7183 check_empty_body (cp_parser
* parser
, const char* type
)
7186 cp_token
*close_paren
;
7187 expanded_location close_loc
;
7188 expanded_location semi_loc
;
7190 close_paren
= cp_lexer_peek_token (parser
->lexer
);
7191 if (close_paren
->type
!= CPP_CLOSE_PAREN
)
7194 close_loc
= expand_location (close_paren
->location
);
7195 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7197 if (token
->type
!= CPP_SEMICOLON
7198 || (token
->flags
& PREV_WHITE
))
7201 semi_loc
= expand_location (token
->location
);
7202 if (close_loc
.line
== semi_loc
.line
7203 #ifdef USE_MAPPED_LOCATION
7204 && close_loc
.column
+1 == semi_loc
.column
7207 warning (OPT_Wempty_body
,
7208 "suggest a space before %<;%> or explicit braces around empty "
7209 "body in %<%s%> statement",
7213 /* Parse an iteration-statement.
7215 iteration-statement:
7216 while ( condition ) statement
7217 do statement while ( expression ) ;
7218 for ( for-init-statement condition [opt] ; expression [opt] )
7221 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7224 cp_parser_iteration_statement (cp_parser
* parser
)
7229 unsigned char in_statement
;
7231 /* Peek at the next token. */
7232 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
7234 return error_mark_node
;
7236 /* Remember whether or not we are already within an iteration
7238 in_statement
= parser
->in_statement
;
7240 /* See what kind of keyword it is. */
7241 keyword
= token
->keyword
;
7248 /* Begin the while-statement. */
7249 statement
= begin_while_stmt ();
7250 /* Look for the `('. */
7251 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
7252 /* Parse the condition. */
7253 condition
= cp_parser_condition (parser
);
7254 finish_while_stmt_cond (condition
, statement
);
7255 check_empty_body (parser
, "while");
7256 /* Look for the `)'. */
7257 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
7258 /* Parse the dependent statement. */
7259 parser
->in_statement
= IN_ITERATION_STMT
;
7260 cp_parser_already_scoped_statement (parser
);
7261 parser
->in_statement
= in_statement
;
7262 /* We're done with the while-statement. */
7263 finish_while_stmt (statement
);
7271 /* Begin the do-statement. */
7272 statement
= begin_do_stmt ();
7273 /* Parse the body of the do-statement. */
7274 parser
->in_statement
= IN_ITERATION_STMT
;
7275 cp_parser_implicitly_scoped_statement (parser
, NULL
);
7276 parser
->in_statement
= in_statement
;
7277 finish_do_body (statement
);
7278 /* Look for the `while' keyword. */
7279 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
7280 /* Look for the `('. */
7281 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
7282 /* Parse the expression. */
7283 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
7284 /* We're done with the do-statement. */
7285 finish_do_stmt (expression
, statement
);
7286 /* Look for the `)'. */
7287 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
7288 /* Look for the `;'. */
7289 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7295 tree condition
= NULL_TREE
;
7296 tree expression
= NULL_TREE
;
7298 /* Begin the for-statement. */
7299 statement
= begin_for_stmt ();
7300 /* Look for the `('. */
7301 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
7302 /* Parse the initialization. */
7303 cp_parser_for_init_statement (parser
);
7304 finish_for_init_stmt (statement
);
7306 /* If there's a condition, process it. */
7307 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7308 condition
= cp_parser_condition (parser
);
7309 finish_for_cond (condition
, statement
);
7310 /* Look for the `;'. */
7311 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7313 /* If there's an expression, process it. */
7314 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7315 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
7316 finish_for_expr (expression
, statement
);
7317 check_empty_body (parser
, "for");
7318 /* Look for the `)'. */
7319 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
7321 /* Parse the body of the for-statement. */
7322 parser
->in_statement
= IN_ITERATION_STMT
;
7323 cp_parser_already_scoped_statement (parser
);
7324 parser
->in_statement
= in_statement
;
7326 /* We're done with the for-statement. */
7327 finish_for_stmt (statement
);
7332 cp_parser_error (parser
, "expected iteration-statement");
7333 statement
= error_mark_node
;
7340 /* Parse a for-init-statement.
7343 expression-statement
7344 simple-declaration */
7347 cp_parser_for_init_statement (cp_parser
* parser
)
7349 /* If the next token is a `;', then we have an empty
7350 expression-statement. Grammatically, this is also a
7351 simple-declaration, but an invalid one, because it does not
7352 declare anything. Therefore, if we did not handle this case
7353 specially, we would issue an error message about an invalid
7355 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7357 /* We're going to speculatively look for a declaration, falling back
7358 to an expression, if necessary. */
7359 cp_parser_parse_tentatively (parser
);
7360 /* Parse the declaration. */
7361 cp_parser_simple_declaration (parser
,
7362 /*function_definition_allowed_p=*/false);
7363 /* If the tentative parse failed, then we shall need to look for an
7364 expression-statement. */
7365 if (cp_parser_parse_definitely (parser
))
7369 cp_parser_expression_statement (parser
, false);
7372 /* Parse a jump-statement.
7377 return expression [opt] ;
7385 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7388 cp_parser_jump_statement (cp_parser
* parser
)
7390 tree statement
= error_mark_node
;
7393 unsigned char in_statement
;
7395 /* Peek at the next token. */
7396 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
7398 return error_mark_node
;
7400 /* See what kind of keyword it is. */
7401 keyword
= token
->keyword
;
7405 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
7406 switch (in_statement
)
7409 error ("break statement not within loop or switch");
7412 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
7413 || in_statement
== IN_ITERATION_STMT
);
7414 statement
= finish_break_stmt ();
7417 error ("invalid exit from OpenMP structured block");
7420 error ("break statement used with OpenMP for loop");
7423 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7427 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
7430 error ("continue statement not within a loop");
7432 case IN_ITERATION_STMT
:
7434 statement
= finish_continue_stmt ();
7437 error ("invalid exit from OpenMP structured block");
7442 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7449 /* If the next token is a `;', then there is no
7451 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7452 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
7455 /* Build the return-statement. */
7456 statement
= finish_return_stmt (expr
);
7457 /* Look for the final `;'. */
7458 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7463 /* Create the goto-statement. */
7464 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
7466 /* Issue a warning about this use of a GNU extension. */
7468 pedwarn ("ISO C++ forbids computed gotos");
7469 /* Consume the '*' token. */
7470 cp_lexer_consume_token (parser
->lexer
);
7471 /* Parse the dependent expression. */
7472 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false));
7475 finish_goto_stmt (cp_parser_identifier (parser
));
7476 /* Look for the final `;'. */
7477 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7481 cp_parser_error (parser
, "expected jump-statement");
7488 /* Parse a declaration-statement.
7490 declaration-statement:
7491 block-declaration */
7494 cp_parser_declaration_statement (cp_parser
* parser
)
7498 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7499 p
= obstack_alloc (&declarator_obstack
, 0);
7501 /* Parse the block-declaration. */
7502 cp_parser_block_declaration (parser
, /*statement_p=*/true);
7504 /* Free any declarators allocated. */
7505 obstack_free (&declarator_obstack
, p
);
7507 /* Finish off the statement. */
7511 /* Some dependent statements (like `if (cond) statement'), are
7512 implicitly in their own scope. In other words, if the statement is
7513 a single statement (as opposed to a compound-statement), it is
7514 none-the-less treated as if it were enclosed in braces. Any
7515 declarations appearing in the dependent statement are out of scope
7516 after control passes that point. This function parses a statement,
7517 but ensures that is in its own scope, even if it is not a
7520 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7521 is a (possibly labeled) if statement which is not enclosed in
7522 braces and has an else clause. This is used to implement
7525 Returns the new statement. */
7528 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
7535 /* Mark if () ; with a special NOP_EXPR. */
7536 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7538 cp_lexer_consume_token (parser
->lexer
);
7539 statement
= add_stmt (build_empty_stmt ());
7541 /* if a compound is opened, we simply parse the statement directly. */
7542 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7543 statement
= cp_parser_compound_statement (parser
, NULL
, false);
7544 /* If the token is not a `{', then we must take special action. */
7547 /* Create a compound-statement. */
7548 statement
= begin_compound_stmt (0);
7549 /* Parse the dependent-statement. */
7550 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
7551 /* Finish the dummy compound-statement. */
7552 finish_compound_stmt (statement
);
7555 /* Return the statement. */
7559 /* For some dependent statements (like `while (cond) statement'), we
7560 have already created a scope. Therefore, even if the dependent
7561 statement is a compound-statement, we do not want to create another
7565 cp_parser_already_scoped_statement (cp_parser
* parser
)
7567 /* If the token is a `{', then we must take special action. */
7568 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
7569 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
7572 /* Avoid calling cp_parser_compound_statement, so that we
7573 don't create a new scope. Do everything else by hand. */
7574 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
7575 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
7576 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7580 /* Declarations [gram.dcl.dcl] */
7582 /* Parse an optional declaration-sequence.
7586 declaration-seq declaration */
7589 cp_parser_declaration_seq_opt (cp_parser
* parser
)
7595 token
= cp_lexer_peek_token (parser
->lexer
);
7597 if (token
->type
== CPP_CLOSE_BRACE
7598 || token
->type
== CPP_EOF
7599 || token
->type
== CPP_PRAGMA_EOL
)
7602 if (token
->type
== CPP_SEMICOLON
)
7604 /* A declaration consisting of a single semicolon is
7605 invalid. Allow it unless we're being pedantic. */
7606 cp_lexer_consume_token (parser
->lexer
);
7607 if (pedantic
&& !in_system_header
)
7608 pedwarn ("extra %<;%>");
7612 /* If we're entering or exiting a region that's implicitly
7613 extern "C", modify the lang context appropriately. */
7614 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
7616 push_lang_context (lang_name_c
);
7617 parser
->implicit_extern_c
= true;
7619 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
7621 pop_lang_context ();
7622 parser
->implicit_extern_c
= false;
7625 if (token
->type
== CPP_PRAGMA
)
7627 /* A top-level declaration can consist solely of a #pragma.
7628 A nested declaration cannot, so this is done here and not
7629 in cp_parser_declaration. (A #pragma at block scope is
7630 handled in cp_parser_statement.) */
7631 cp_parser_pragma (parser
, pragma_external
);
7635 /* Parse the declaration itself. */
7636 cp_parser_declaration (parser
);
7640 /* Parse a declaration.
7645 template-declaration
7646 explicit-instantiation
7647 explicit-specialization
7648 linkage-specification
7649 namespace-definition
7654 __extension__ declaration */
7657 cp_parser_declaration (cp_parser
* parser
)
7664 /* Check for the `__extension__' keyword. */
7665 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7667 /* Parse the qualified declaration. */
7668 cp_parser_declaration (parser
);
7669 /* Restore the PEDANTIC flag. */
7670 pedantic
= saved_pedantic
;
7675 /* Try to figure out what kind of declaration is present. */
7676 token1
= *cp_lexer_peek_token (parser
->lexer
);
7678 if (token1
.type
!= CPP_EOF
)
7679 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
7682 token2
.type
= CPP_EOF
;
7683 token2
.keyword
= RID_MAX
;
7686 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7687 p
= obstack_alloc (&declarator_obstack
, 0);
7689 /* If the next token is `extern' and the following token is a string
7690 literal, then we have a linkage specification. */
7691 if (token1
.keyword
== RID_EXTERN
7692 && cp_parser_is_string_literal (&token2
))
7693 cp_parser_linkage_specification (parser
);
7694 /* If the next token is `template', then we have either a template
7695 declaration, an explicit instantiation, or an explicit
7697 else if (token1
.keyword
== RID_TEMPLATE
)
7699 /* `template <>' indicates a template specialization. */
7700 if (token2
.type
== CPP_LESS
7701 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
7702 cp_parser_explicit_specialization (parser
);
7703 /* `template <' indicates a template declaration. */
7704 else if (token2
.type
== CPP_LESS
)
7705 cp_parser_template_declaration (parser
, /*member_p=*/false);
7706 /* Anything else must be an explicit instantiation. */
7708 cp_parser_explicit_instantiation (parser
);
7710 /* If the next token is `export', then we have a template
7712 else if (token1
.keyword
== RID_EXPORT
)
7713 cp_parser_template_declaration (parser
, /*member_p=*/false);
7714 /* If the next token is `extern', 'static' or 'inline' and the one
7715 after that is `template', we have a GNU extended explicit
7716 instantiation directive. */
7717 else if (cp_parser_allow_gnu_extensions_p (parser
)
7718 && (token1
.keyword
== RID_EXTERN
7719 || token1
.keyword
== RID_STATIC
7720 || token1
.keyword
== RID_INLINE
)
7721 && token2
.keyword
== RID_TEMPLATE
)
7722 cp_parser_explicit_instantiation (parser
);
7723 /* If the next token is `namespace', check for a named or unnamed
7724 namespace definition. */
7725 else if (token1
.keyword
== RID_NAMESPACE
7726 && (/* A named namespace definition. */
7727 (token2
.type
== CPP_NAME
7728 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
7730 /* An unnamed namespace definition. */
7731 || token2
.type
== CPP_OPEN_BRACE
7732 || token2
.keyword
== RID_ATTRIBUTE
))
7733 cp_parser_namespace_definition (parser
);
7734 /* Objective-C++ declaration/definition. */
7735 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
7736 cp_parser_objc_declaration (parser
);
7737 /* We must have either a block declaration or a function
7740 /* Try to parse a block-declaration, or a function-definition. */
7741 cp_parser_block_declaration (parser
, /*statement_p=*/false);
7743 /* Free any declarators allocated. */
7744 obstack_free (&declarator_obstack
, p
);
7747 /* Parse a block-declaration.
7752 namespace-alias-definition
7759 __extension__ block-declaration
7764 static_assert-declaration
7766 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7767 part of a declaration-statement. */
7770 cp_parser_block_declaration (cp_parser
*parser
,
7776 /* Check for the `__extension__' keyword. */
7777 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7779 /* Parse the qualified declaration. */
7780 cp_parser_block_declaration (parser
, statement_p
);
7781 /* Restore the PEDANTIC flag. */
7782 pedantic
= saved_pedantic
;
7787 /* Peek at the next token to figure out which kind of declaration is
7789 token1
= cp_lexer_peek_token (parser
->lexer
);
7791 /* If the next keyword is `asm', we have an asm-definition. */
7792 if (token1
->keyword
== RID_ASM
)
7795 cp_parser_commit_to_tentative_parse (parser
);
7796 cp_parser_asm_definition (parser
);
7798 /* If the next keyword is `namespace', we have a
7799 namespace-alias-definition. */
7800 else if (token1
->keyword
== RID_NAMESPACE
)
7801 cp_parser_namespace_alias_definition (parser
);
7802 /* If the next keyword is `using', we have either a
7803 using-declaration or a using-directive. */
7804 else if (token1
->keyword
== RID_USING
)
7809 cp_parser_commit_to_tentative_parse (parser
);
7810 /* If the token after `using' is `namespace', then we have a
7812 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7813 if (token2
->keyword
== RID_NAMESPACE
)
7814 cp_parser_using_directive (parser
);
7815 /* Otherwise, it's a using-declaration. */
7817 cp_parser_using_declaration (parser
,
7818 /*access_declaration_p=*/false);
7820 /* If the next keyword is `__label__' we have a misplaced label
7822 else if (token1
->keyword
== RID_LABEL
)
7824 cp_lexer_consume_token (parser
->lexer
);
7825 error ("%<__label__%> not at the beginning of a block");
7826 cp_parser_skip_to_end_of_statement (parser
);
7827 /* If the next token is now a `;', consume it. */
7828 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7829 cp_lexer_consume_token (parser
->lexer
);
7831 /* If the next token is `static_assert' we have a static assertion. */
7832 else if (token1
->keyword
== RID_STATIC_ASSERT
)
7833 cp_parser_static_assert (parser
, /*member_p=*/false);
7834 /* Anything else must be a simple-declaration. */
7836 cp_parser_simple_declaration (parser
, !statement_p
);
7839 /* Parse a simple-declaration.
7842 decl-specifier-seq [opt] init-declarator-list [opt] ;
7844 init-declarator-list:
7846 init-declarator-list , init-declarator
7848 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7849 function-definition as a simple-declaration. */
7852 cp_parser_simple_declaration (cp_parser
* parser
,
7853 bool function_definition_allowed_p
)
7855 cp_decl_specifier_seq decl_specifiers
;
7856 int declares_class_or_enum
;
7857 bool saw_declarator
;
7859 /* Defer access checks until we know what is being declared; the
7860 checks for names appearing in the decl-specifier-seq should be
7861 done as if we were in the scope of the thing being declared. */
7862 push_deferring_access_checks (dk_deferred
);
7864 /* Parse the decl-specifier-seq. We have to keep track of whether
7865 or not the decl-specifier-seq declares a named class or
7866 enumeration type, since that is the only case in which the
7867 init-declarator-list is allowed to be empty.
7871 In a simple-declaration, the optional init-declarator-list can be
7872 omitted only when declaring a class or enumeration, that is when
7873 the decl-specifier-seq contains either a class-specifier, an
7874 elaborated-type-specifier, or an enum-specifier. */
7875 cp_parser_decl_specifier_seq (parser
,
7876 CP_PARSER_FLAGS_OPTIONAL
,
7878 &declares_class_or_enum
);
7879 /* We no longer need to defer access checks. */
7880 stop_deferring_access_checks ();
7882 /* In a block scope, a valid declaration must always have a
7883 decl-specifier-seq. By not trying to parse declarators, we can
7884 resolve the declaration/expression ambiguity more quickly. */
7885 if (!function_definition_allowed_p
7886 && !decl_specifiers
.any_specifiers_p
)
7888 cp_parser_error (parser
, "expected declaration");
7892 /* If the next two tokens are both identifiers, the code is
7893 erroneous. The usual cause of this situation is code like:
7897 where "T" should name a type -- but does not. */
7898 if (!decl_specifiers
.type
7899 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
7901 /* If parsing tentatively, we should commit; we really are
7902 looking at a declaration. */
7903 cp_parser_commit_to_tentative_parse (parser
);
7908 /* If we have seen at least one decl-specifier, and the next token
7909 is not a parenthesis, then we must be looking at a declaration.
7910 (After "int (" we might be looking at a functional cast.) */
7911 if (decl_specifiers
.any_specifiers_p
7912 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7913 cp_parser_commit_to_tentative_parse (parser
);
7915 /* Keep going until we hit the `;' at the end of the simple
7917 saw_declarator
= false;
7918 while (cp_lexer_next_token_is_not (parser
->lexer
,
7922 bool function_definition_p
;
7927 /* If we are processing next declarator, coma is expected */
7928 token
= cp_lexer_peek_token (parser
->lexer
);
7929 gcc_assert (token
->type
== CPP_COMMA
);
7930 cp_lexer_consume_token (parser
->lexer
);
7933 saw_declarator
= true;
7935 /* Parse the init-declarator. */
7936 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
7938 function_definition_allowed_p
,
7940 declares_class_or_enum
,
7941 &function_definition_p
);
7942 /* If an error occurred while parsing tentatively, exit quickly.
7943 (That usually happens when in the body of a function; each
7944 statement is treated as a declaration-statement until proven
7946 if (cp_parser_error_occurred (parser
))
7948 /* Handle function definitions specially. */
7949 if (function_definition_p
)
7951 /* If the next token is a `,', then we are probably
7952 processing something like:
7956 which is erroneous. */
7957 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
7958 error ("mixing declarations and function-definitions is forbidden");
7959 /* Otherwise, we're done with the list of declarators. */
7962 pop_deferring_access_checks ();
7966 /* The next token should be either a `,' or a `;'. */
7967 token
= cp_lexer_peek_token (parser
->lexer
);
7968 /* If it's a `,', there are more declarators to come. */
7969 if (token
->type
== CPP_COMMA
)
7970 /* will be consumed next time around */;
7971 /* If it's a `;', we are done. */
7972 else if (token
->type
== CPP_SEMICOLON
)
7974 /* Anything else is an error. */
7977 /* If we have already issued an error message we don't need
7978 to issue another one. */
7979 if (decl
!= error_mark_node
7980 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
7981 cp_parser_error (parser
, "expected %<,%> or %<;%>");
7982 /* Skip tokens until we reach the end of the statement. */
7983 cp_parser_skip_to_end_of_statement (parser
);
7984 /* If the next token is now a `;', consume it. */
7985 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7986 cp_lexer_consume_token (parser
->lexer
);
7989 /* After the first time around, a function-definition is not
7990 allowed -- even if it was OK at first. For example:
7995 function_definition_allowed_p
= false;
7998 /* Issue an error message if no declarators are present, and the
7999 decl-specifier-seq does not itself declare a class or
8001 if (!saw_declarator
)
8003 if (cp_parser_declares_only_class_p (parser
))
8004 shadow_tag (&decl_specifiers
);
8005 /* Perform any deferred access checks. */
8006 perform_deferred_access_checks ();
8009 /* Consume the `;'. */
8010 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
8013 pop_deferring_access_checks ();
8016 /* Parse a decl-specifier-seq.
8019 decl-specifier-seq [opt] decl-specifier
8022 storage-class-specifier
8033 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8035 The parser flags FLAGS is used to control type-specifier parsing.
8037 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8040 1: one of the decl-specifiers is an elaborated-type-specifier
8041 (i.e., a type declaration)
8042 2: one of the decl-specifiers is an enum-specifier or a
8043 class-specifier (i.e., a type definition)
8048 cp_parser_decl_specifier_seq (cp_parser
* parser
,
8049 cp_parser_flags flags
,
8050 cp_decl_specifier_seq
*decl_specs
,
8051 int* declares_class_or_enum
)
8053 bool constructor_possible_p
= !parser
->in_declarator_p
;
8055 /* Clear DECL_SPECS. */
8056 clear_decl_specs (decl_specs
);
8058 /* Assume no class or enumeration type is declared. */
8059 *declares_class_or_enum
= 0;
8061 /* Keep reading specifiers until there are no more to read. */
8065 bool found_decl_spec
;
8068 /* Peek at the next token. */
8069 token
= cp_lexer_peek_token (parser
->lexer
);
8070 /* Handle attributes. */
8071 if (token
->keyword
== RID_ATTRIBUTE
)
8073 /* Parse the attributes. */
8074 decl_specs
->attributes
8075 = chainon (decl_specs
->attributes
,
8076 cp_parser_attributes_opt (parser
));
8079 /* Assume we will find a decl-specifier keyword. */
8080 found_decl_spec
= true;
8081 /* If the next token is an appropriate keyword, we can simply
8082 add it to the list. */
8083 switch (token
->keyword
)
8088 if (!at_class_scope_p ())
8090 error ("%<friend%> used outside of class");
8091 cp_lexer_purge_token (parser
->lexer
);
8095 ++decl_specs
->specs
[(int) ds_friend
];
8096 /* Consume the token. */
8097 cp_lexer_consume_token (parser
->lexer
);
8101 /* function-specifier:
8108 cp_parser_function_specifier_opt (parser
, decl_specs
);
8114 ++decl_specs
->specs
[(int) ds_typedef
];
8115 /* Consume the token. */
8116 cp_lexer_consume_token (parser
->lexer
);
8117 /* A constructor declarator cannot appear in a typedef. */
8118 constructor_possible_p
= false;
8119 /* The "typedef" keyword can only occur in a declaration; we
8120 may as well commit at this point. */
8121 cp_parser_commit_to_tentative_parse (parser
);
8123 if (decl_specs
->storage_class
!= sc_none
)
8124 decl_specs
->conflicting_specifiers_p
= true;
8127 /* storage-class-specifier:
8141 /* Consume the token. */
8142 cp_lexer_consume_token (parser
->lexer
);
8143 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
);
8146 /* Consume the token. */
8147 cp_lexer_consume_token (parser
->lexer
);
8148 ++decl_specs
->specs
[(int) ds_thread
];
8152 /* We did not yet find a decl-specifier yet. */
8153 found_decl_spec
= false;
8157 /* Constructors are a special case. The `S' in `S()' is not a
8158 decl-specifier; it is the beginning of the declarator. */
8161 && constructor_possible_p
8162 && (cp_parser_constructor_declarator_p
8163 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
8165 /* If we don't have a DECL_SPEC yet, then we must be looking at
8166 a type-specifier. */
8167 if (!found_decl_spec
&& !constructor_p
)
8169 int decl_spec_declares_class_or_enum
;
8170 bool is_cv_qualifier
;
8174 = cp_parser_type_specifier (parser
, flags
,
8176 /*is_declaration=*/true,
8177 &decl_spec_declares_class_or_enum
,
8180 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
8182 /* If this type-specifier referenced a user-defined type
8183 (a typedef, class-name, etc.), then we can't allow any
8184 more such type-specifiers henceforth.
8188 The longest sequence of decl-specifiers that could
8189 possibly be a type name is taken as the
8190 decl-specifier-seq of a declaration. The sequence shall
8191 be self-consistent as described below.
8195 As a general rule, at most one type-specifier is allowed
8196 in the complete decl-specifier-seq of a declaration. The
8197 only exceptions are the following:
8199 -- const or volatile can be combined with any other
8202 -- signed or unsigned can be combined with char, long,
8210 void g (const int Pc);
8212 Here, Pc is *not* part of the decl-specifier seq; it's
8213 the declarator. Therefore, once we see a type-specifier
8214 (other than a cv-qualifier), we forbid any additional
8215 user-defined types. We *do* still allow things like `int
8216 int' to be considered a decl-specifier-seq, and issue the
8217 error message later. */
8218 if (type_spec
&& !is_cv_qualifier
)
8219 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
8220 /* A constructor declarator cannot follow a type-specifier. */
8223 constructor_possible_p
= false;
8224 found_decl_spec
= true;
8228 /* If we still do not have a DECL_SPEC, then there are no more
8230 if (!found_decl_spec
)
8233 decl_specs
->any_specifiers_p
= true;
8234 /* After we see one decl-specifier, further decl-specifiers are
8236 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
8239 cp_parser_check_decl_spec (decl_specs
);
8241 /* Don't allow a friend specifier with a class definition. */
8242 if (decl_specs
->specs
[(int) ds_friend
] != 0
8243 && (*declares_class_or_enum
& 2))
8244 error ("class definition may not be declared a friend");
8247 /* Parse an (optional) storage-class-specifier.
8249 storage-class-specifier:
8258 storage-class-specifier:
8261 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8264 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
8266 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
8274 /* Consume the token. */
8275 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
8282 /* Parse an (optional) function-specifier.
8289 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8290 Updates DECL_SPECS, if it is non-NULL. */
8293 cp_parser_function_specifier_opt (cp_parser
* parser
,
8294 cp_decl_specifier_seq
*decl_specs
)
8296 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
8300 ++decl_specs
->specs
[(int) ds_inline
];
8304 /* 14.5.2.3 [temp.mem]
8306 A member function template shall not be virtual. */
8307 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8308 error ("templates may not be %<virtual%>");
8309 else if (decl_specs
)
8310 ++decl_specs
->specs
[(int) ds_virtual
];
8315 ++decl_specs
->specs
[(int) ds_explicit
];
8322 /* Consume the token. */
8323 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
8326 /* Parse a linkage-specification.
8328 linkage-specification:
8329 extern string-literal { declaration-seq [opt] }
8330 extern string-literal declaration */
8333 cp_parser_linkage_specification (cp_parser
* parser
)
8337 /* Look for the `extern' keyword. */
8338 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
8340 /* Look for the string-literal. */
8341 linkage
= cp_parser_string_literal (parser
, false, false);
8343 /* Transform the literal into an identifier. If the literal is a
8344 wide-character string, or contains embedded NULs, then we can't
8345 handle it as the user wants. */
8346 if (strlen (TREE_STRING_POINTER (linkage
))
8347 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
8349 cp_parser_error (parser
, "invalid linkage-specification");
8350 /* Assume C++ linkage. */
8351 linkage
= lang_name_cplusplus
;
8354 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
8356 /* We're now using the new linkage. */
8357 push_lang_context (linkage
);
8359 /* If the next token is a `{', then we're using the first
8361 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8363 /* Consume the `{' token. */
8364 cp_lexer_consume_token (parser
->lexer
);
8365 /* Parse the declarations. */
8366 cp_parser_declaration_seq_opt (parser
);
8367 /* Look for the closing `}'. */
8368 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
8370 /* Otherwise, there's just one declaration. */
8373 bool saved_in_unbraced_linkage_specification_p
;
8375 saved_in_unbraced_linkage_specification_p
8376 = parser
->in_unbraced_linkage_specification_p
;
8377 parser
->in_unbraced_linkage_specification_p
= true;
8378 cp_parser_declaration (parser
);
8379 parser
->in_unbraced_linkage_specification_p
8380 = saved_in_unbraced_linkage_specification_p
;
8383 /* We're done with the linkage-specification. */
8384 pop_lang_context ();
8387 /* Parse a static_assert-declaration.
8389 static_assert-declaration:
8390 static_assert ( constant-expression , string-literal ) ;
8392 If MEMBER_P, this static_assert is a class member. */
8395 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
8400 location_t saved_loc
;
8402 /* Peek at the `static_assert' token so we can keep track of exactly
8403 where the static assertion started. */
8404 token
= cp_lexer_peek_token (parser
->lexer
);
8405 saved_loc
= token
->location
;
8407 /* Look for the `static_assert' keyword. */
8408 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
8412 /* We know we are in a static assertion; commit to any tentative
8414 if (cp_parser_parsing_tentatively (parser
))
8415 cp_parser_commit_to_tentative_parse (parser
);
8417 /* Parse the `(' starting the static assertion condition. */
8418 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
8420 /* Parse the constant-expression. */
8422 cp_parser_constant_expression (parser
,
8423 /*allow_non_constant_p=*/false,
8424 /*non_constant_p=*/NULL
);
8426 /* Parse the separating `,'. */
8427 cp_parser_require (parser
, CPP_COMMA
, "`,'");
8429 /* Parse the string-literal message. */
8430 message
= cp_parser_string_literal (parser
,
8431 /*translate=*/false,
8434 /* A `)' completes the static assertion. */
8435 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
8436 cp_parser_skip_to_closing_parenthesis (parser
,
8437 /*recovering=*/true,
8439 /*consume_paren=*/true);
8441 /* A semicolon terminates the declaration. */
8442 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
8444 /* Complete the static assertion, which may mean either processing
8445 the static assert now or saving it for template instantiation. */
8446 finish_static_assert (condition
, message
, saved_loc
, member_p
);
8449 /* Parse a `decltype' type. Returns the type.
8451 simple-type-specifier:
8452 decltype ( expression ) */
8455 cp_parser_decltype (cp_parser
*parser
)
8458 bool id_expression_or_member_access_p
= false;
8459 const char *saved_message
;
8460 bool saved_integral_constant_expression_p
;
8461 bool saved_non_integral_constant_expression_p
;
8463 /* Look for the `decltype' token. */
8464 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, "`decltype'"))
8465 return error_mark_node
;
8467 /* Types cannot be defined in a `decltype' expression. Save away the
8469 saved_message
= parser
->type_definition_forbidden_message
;
8471 /* And create the new one. */
8472 parser
->type_definition_forbidden_message
8473 = "types may not be defined in `decltype' expressions";
8475 /* The restrictions on constant-expressions do not apply inside
8476 decltype expressions. */
8477 saved_integral_constant_expression_p
8478 = parser
->integral_constant_expression_p
;
8479 saved_non_integral_constant_expression_p
8480 = parser
->non_integral_constant_expression_p
;
8481 parser
->integral_constant_expression_p
= false;
8483 /* Do not actually evaluate the expression. */
8486 /* Parse the opening `('. */
8487 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
8488 return error_mark_node
;
8490 /* First, try parsing an id-expression. */
8491 cp_parser_parse_tentatively (parser
);
8492 expr
= cp_parser_id_expression (parser
,
8493 /*template_keyword_p=*/false,
8494 /*check_dependency_p=*/true,
8495 /*template_p=*/NULL
,
8496 /*declarator_p=*/false,
8497 /*optional_p=*/false);
8499 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
8501 bool non_integral_constant_expression_p
= false;
8502 tree id_expression
= expr
;
8504 const char *error_msg
;
8506 if (TREE_CODE (expr
) == IDENTIFIER_NODE
)
8507 /* Lookup the name we got back from the id-expression. */
8508 expr
= cp_parser_lookup_name (parser
, expr
,
8510 /*is_template=*/false,
8511 /*is_namespace=*/false,
8512 /*check_dependency=*/true,
8513 /*ambiguous_decls=*/NULL
);
8516 && expr
!= error_mark_node
8517 && TREE_CODE (expr
) != TEMPLATE_ID_EXPR
8518 && TREE_CODE (expr
) != TYPE_DECL
8519 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
8521 /* Complete lookup of the id-expression. */
8522 expr
= (finish_id_expression
8523 (id_expression
, expr
, parser
->scope
, &idk
,
8524 /*integral_constant_expression_p=*/false,
8525 /*allow_non_integral_constant_expression_p=*/true,
8526 &non_integral_constant_expression_p
,
8527 /*template_p=*/false,
8529 /*address_p=*/false,
8530 /*template_arg_p=*/false,
8533 if (expr
== error_mark_node
)
8534 /* We found an id-expression, but it was something that we
8535 should not have found. This is an error, not something
8536 we can recover from, so note that we found an
8537 id-expression and we'll recover as gracefully as
8539 id_expression_or_member_access_p
= true;
8543 && expr
!= error_mark_node
8544 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
8545 /* We have an id-expression. */
8546 id_expression_or_member_access_p
= true;
8549 if (!id_expression_or_member_access_p
)
8551 /* Abort the id-expression parse. */
8552 cp_parser_abort_tentative_parse (parser
);
8554 /* Parsing tentatively, again. */
8555 cp_parser_parse_tentatively (parser
);
8557 /* Parse a class member access. */
8558 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
8560 /*member_access_only_p=*/true);
8563 && expr
!= error_mark_node
8564 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
8565 /* We have an id-expression. */
8566 id_expression_or_member_access_p
= true;
8569 if (id_expression_or_member_access_p
)
8570 /* We have parsed the complete id-expression or member access. */
8571 cp_parser_parse_definitely (parser
);
8574 /* Abort our attempt to parse an id-expression or member access
8576 cp_parser_abort_tentative_parse (parser
);
8578 /* Parse a full expression. */
8579 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
8582 /* Go back to evaluating expressions. */
8585 /* Restore the old message and the integral constant expression
8587 parser
->type_definition_forbidden_message
= saved_message
;
8588 parser
->integral_constant_expression_p
8589 = saved_integral_constant_expression_p
;
8590 parser
->non_integral_constant_expression_p
8591 = saved_non_integral_constant_expression_p
;
8593 if (expr
== error_mark_node
)
8595 /* Skip everything up to the closing `)'. */
8596 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
8597 /*consume_paren=*/true);
8598 return error_mark_node
;
8601 /* Parse to the closing `)'. */
8602 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
8603 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
8604 /*consume_paren=*/true);
8606 return finish_decltype_type (expr
, id_expression_or_member_access_p
);
8609 /* Special member functions [gram.special] */
8611 /* Parse a conversion-function-id.
8613 conversion-function-id:
8614 operator conversion-type-id
8616 Returns an IDENTIFIER_NODE representing the operator. */
8619 cp_parser_conversion_function_id (cp_parser
* parser
)
8623 tree saved_qualifying_scope
;
8624 tree saved_object_scope
;
8625 tree pushed_scope
= NULL_TREE
;
8627 /* Look for the `operator' token. */
8628 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
8629 return error_mark_node
;
8630 /* When we parse the conversion-type-id, the current scope will be
8631 reset. However, we need that information in able to look up the
8632 conversion function later, so we save it here. */
8633 saved_scope
= parser
->scope
;
8634 saved_qualifying_scope
= parser
->qualifying_scope
;
8635 saved_object_scope
= parser
->object_scope
;
8636 /* We must enter the scope of the class so that the names of
8637 entities declared within the class are available in the
8638 conversion-type-id. For example, consider:
8645 S::operator I() { ... }
8647 In order to see that `I' is a type-name in the definition, we
8648 must be in the scope of `S'. */
8650 pushed_scope
= push_scope (saved_scope
);
8651 /* Parse the conversion-type-id. */
8652 type
= cp_parser_conversion_type_id (parser
);
8653 /* Leave the scope of the class, if any. */
8655 pop_scope (pushed_scope
);
8656 /* Restore the saved scope. */
8657 parser
->scope
= saved_scope
;
8658 parser
->qualifying_scope
= saved_qualifying_scope
;
8659 parser
->object_scope
= saved_object_scope
;
8660 /* If the TYPE is invalid, indicate failure. */
8661 if (type
== error_mark_node
)
8662 return error_mark_node
;
8663 return mangle_conv_op_name_for_type (type
);
8666 /* Parse a conversion-type-id:
8669 type-specifier-seq conversion-declarator [opt]
8671 Returns the TYPE specified. */
8674 cp_parser_conversion_type_id (cp_parser
* parser
)
8677 cp_decl_specifier_seq type_specifiers
;
8678 cp_declarator
*declarator
;
8679 tree type_specified
;
8681 /* Parse the attributes. */
8682 attributes
= cp_parser_attributes_opt (parser
);
8683 /* Parse the type-specifiers. */
8684 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
8686 /* If that didn't work, stop. */
8687 if (type_specifiers
.type
== error_mark_node
)
8688 return error_mark_node
;
8689 /* Parse the conversion-declarator. */
8690 declarator
= cp_parser_conversion_declarator_opt (parser
);
8692 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
8693 /*initialized=*/0, &attributes
);
8695 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
8696 return type_specified
;
8699 /* Parse an (optional) conversion-declarator.
8701 conversion-declarator:
8702 ptr-operator conversion-declarator [opt]
8706 static cp_declarator
*
8707 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
8709 enum tree_code code
;
8711 cp_cv_quals cv_quals
;
8713 /* We don't know if there's a ptr-operator next, or not. */
8714 cp_parser_parse_tentatively (parser
);
8715 /* Try the ptr-operator. */
8716 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
8717 /* If it worked, look for more conversion-declarators. */
8718 if (cp_parser_parse_definitely (parser
))
8720 cp_declarator
*declarator
;
8722 /* Parse another optional declarator. */
8723 declarator
= cp_parser_conversion_declarator_opt (parser
);
8725 return cp_parser_make_indirect_declarator
8726 (code
, class_type
, cv_quals
, declarator
);
8732 /* Parse an (optional) ctor-initializer.
8735 : mem-initializer-list
8737 Returns TRUE iff the ctor-initializer was actually present. */
8740 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
8742 /* If the next token is not a `:', then there is no
8743 ctor-initializer. */
8744 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
8746 /* Do default initialization of any bases and members. */
8747 if (DECL_CONSTRUCTOR_P (current_function_decl
))
8748 finish_mem_initializers (NULL_TREE
);
8753 /* Consume the `:' token. */
8754 cp_lexer_consume_token (parser
->lexer
);
8755 /* And the mem-initializer-list. */
8756 cp_parser_mem_initializer_list (parser
);
8761 /* Parse a mem-initializer-list.
8763 mem-initializer-list:
8764 mem-initializer ... [opt]
8765 mem-initializer ... [opt] , mem-initializer-list */
8768 cp_parser_mem_initializer_list (cp_parser
* parser
)
8770 tree mem_initializer_list
= NULL_TREE
;
8772 /* Let the semantic analysis code know that we are starting the
8773 mem-initializer-list. */
8774 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
8775 error ("only constructors take base initializers");
8777 /* Loop through the list. */
8780 tree mem_initializer
;
8782 /* Parse the mem-initializer. */
8783 mem_initializer
= cp_parser_mem_initializer (parser
);
8784 /* If the next token is a `...', we're expanding member initializers. */
8785 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8787 /* Consume the `...'. */
8788 cp_lexer_consume_token (parser
->lexer
);
8790 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8791 can be expanded but members cannot. */
8792 if (mem_initializer
!= error_mark_node
8793 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
8795 error ("cannot expand initializer for member %<%D%>",
8796 TREE_PURPOSE (mem_initializer
));
8797 mem_initializer
= error_mark_node
;
8800 /* Construct the pack expansion type. */
8801 if (mem_initializer
!= error_mark_node
)
8802 mem_initializer
= make_pack_expansion (mem_initializer
);
8804 /* Add it to the list, unless it was erroneous. */
8805 if (mem_initializer
!= error_mark_node
)
8807 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
8808 mem_initializer_list
= mem_initializer
;
8810 /* If the next token is not a `,', we're done. */
8811 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8813 /* Consume the `,' token. */
8814 cp_lexer_consume_token (parser
->lexer
);
8817 /* Perform semantic analysis. */
8818 if (DECL_CONSTRUCTOR_P (current_function_decl
))
8819 finish_mem_initializers (mem_initializer_list
);
8822 /* Parse a mem-initializer.
8825 mem-initializer-id ( expression-list [opt] )
8830 ( expression-list [opt] )
8832 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8833 class) or FIELD_DECL (for a non-static data member) to initialize;
8834 the TREE_VALUE is the expression-list. An empty initialization
8835 list is represented by void_list_node. */
8838 cp_parser_mem_initializer (cp_parser
* parser
)
8840 tree mem_initializer_id
;
8841 tree expression_list
;
8844 /* Find out what is being initialized. */
8845 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8847 pedwarn ("anachronistic old-style base class initializer");
8848 mem_initializer_id
= NULL_TREE
;
8851 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
8852 member
= expand_member_init (mem_initializer_id
);
8853 if (member
&& !DECL_P (member
))
8854 in_base_initializer
= 1;
8857 = cp_parser_parenthesized_expression_list (parser
, false,
8859 /*allow_expansion_p=*/true,
8860 /*non_constant_p=*/NULL
);
8861 if (expression_list
== error_mark_node
)
8862 return error_mark_node
;
8863 if (!expression_list
)
8864 expression_list
= void_type_node
;
8866 in_base_initializer
= 0;
8868 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
8871 /* Parse a mem-initializer-id.
8874 :: [opt] nested-name-specifier [opt] class-name
8877 Returns a TYPE indicating the class to be initializer for the first
8878 production. Returns an IDENTIFIER_NODE indicating the data member
8879 to be initialized for the second production. */
8882 cp_parser_mem_initializer_id (cp_parser
* parser
)
8884 bool global_scope_p
;
8885 bool nested_name_specifier_p
;
8886 bool template_p
= false;
8889 /* `typename' is not allowed in this context ([temp.res]). */
8890 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
8892 error ("keyword %<typename%> not allowed in this context (a qualified "
8893 "member initializer is implicitly a type)");
8894 cp_lexer_consume_token (parser
->lexer
);
8896 /* Look for the optional `::' operator. */
8898 = (cp_parser_global_scope_opt (parser
,
8899 /*current_scope_valid_p=*/false)
8901 /* Look for the optional nested-name-specifier. The simplest way to
8906 The keyword `typename' is not permitted in a base-specifier or
8907 mem-initializer; in these contexts a qualified name that
8908 depends on a template-parameter is implicitly assumed to be a
8911 is to assume that we have seen the `typename' keyword at this
8913 nested_name_specifier_p
8914 = (cp_parser_nested_name_specifier_opt (parser
,
8915 /*typename_keyword_p=*/true,
8916 /*check_dependency_p=*/true,
8918 /*is_declaration=*/true)
8920 if (nested_name_specifier_p
)
8921 template_p
= cp_parser_optional_template_keyword (parser
);
8922 /* If there is a `::' operator or a nested-name-specifier, then we
8923 are definitely looking for a class-name. */
8924 if (global_scope_p
|| nested_name_specifier_p
)
8925 return cp_parser_class_name (parser
,
8926 /*typename_keyword_p=*/true,
8927 /*template_keyword_p=*/template_p
,
8929 /*check_dependency_p=*/true,
8930 /*class_head_p=*/false,
8931 /*is_declaration=*/true);
8932 /* Otherwise, we could also be looking for an ordinary identifier. */
8933 cp_parser_parse_tentatively (parser
);
8934 /* Try a class-name. */
8935 id
= cp_parser_class_name (parser
,
8936 /*typename_keyword_p=*/true,
8937 /*template_keyword_p=*/false,
8939 /*check_dependency_p=*/true,
8940 /*class_head_p=*/false,
8941 /*is_declaration=*/true);
8942 /* If we found one, we're done. */
8943 if (cp_parser_parse_definitely (parser
))
8945 /* Otherwise, look for an ordinary identifier. */
8946 return cp_parser_identifier (parser
);
8949 /* Overloading [gram.over] */
8951 /* Parse an operator-function-id.
8953 operator-function-id:
8956 Returns an IDENTIFIER_NODE for the operator which is a
8957 human-readable spelling of the identifier, e.g., `operator +'. */
8960 cp_parser_operator_function_id (cp_parser
* parser
)
8962 /* Look for the `operator' keyword. */
8963 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
8964 return error_mark_node
;
8965 /* And then the name of the operator itself. */
8966 return cp_parser_operator (parser
);
8969 /* Parse an operator.
8972 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8973 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8974 || ++ -- , ->* -> () []
8981 Returns an IDENTIFIER_NODE for the operator which is a
8982 human-readable spelling of the identifier, e.g., `operator +'. */
8985 cp_parser_operator (cp_parser
* parser
)
8987 tree id
= NULL_TREE
;
8990 /* Peek at the next token. */
8991 token
= cp_lexer_peek_token (parser
->lexer
);
8992 /* Figure out which operator we have. */
8993 switch (token
->type
)
8999 /* The keyword should be either `new' or `delete'. */
9000 if (token
->keyword
== RID_NEW
)
9002 else if (token
->keyword
== RID_DELETE
)
9007 /* Consume the `new' or `delete' token. */
9008 cp_lexer_consume_token (parser
->lexer
);
9010 /* Peek at the next token. */
9011 token
= cp_lexer_peek_token (parser
->lexer
);
9012 /* If it's a `[' token then this is the array variant of the
9014 if (token
->type
== CPP_OPEN_SQUARE
)
9016 /* Consume the `[' token. */
9017 cp_lexer_consume_token (parser
->lexer
);
9018 /* Look for the `]' token. */
9019 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
9020 id
= ansi_opname (op
== NEW_EXPR
9021 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
9023 /* Otherwise, we have the non-array variant. */
9025 id
= ansi_opname (op
);
9031 id
= ansi_opname (PLUS_EXPR
);
9035 id
= ansi_opname (MINUS_EXPR
);
9039 id
= ansi_opname (MULT_EXPR
);
9043 id
= ansi_opname (TRUNC_DIV_EXPR
);
9047 id
= ansi_opname (TRUNC_MOD_EXPR
);
9051 id
= ansi_opname (BIT_XOR_EXPR
);
9055 id
= ansi_opname (BIT_AND_EXPR
);
9059 id
= ansi_opname (BIT_IOR_EXPR
);
9063 id
= ansi_opname (BIT_NOT_EXPR
);
9067 id
= ansi_opname (TRUTH_NOT_EXPR
);
9071 id
= ansi_assopname (NOP_EXPR
);
9075 id
= ansi_opname (LT_EXPR
);
9079 id
= ansi_opname (GT_EXPR
);
9083 id
= ansi_assopname (PLUS_EXPR
);
9087 id
= ansi_assopname (MINUS_EXPR
);
9091 id
= ansi_assopname (MULT_EXPR
);
9095 id
= ansi_assopname (TRUNC_DIV_EXPR
);
9099 id
= ansi_assopname (TRUNC_MOD_EXPR
);
9103 id
= ansi_assopname (BIT_XOR_EXPR
);
9107 id
= ansi_assopname (BIT_AND_EXPR
);
9111 id
= ansi_assopname (BIT_IOR_EXPR
);
9115 id
= ansi_opname (LSHIFT_EXPR
);
9119 id
= ansi_opname (RSHIFT_EXPR
);
9123 id
= ansi_assopname (LSHIFT_EXPR
);
9127 id
= ansi_assopname (RSHIFT_EXPR
);
9131 id
= ansi_opname (EQ_EXPR
);
9135 id
= ansi_opname (NE_EXPR
);
9139 id
= ansi_opname (LE_EXPR
);
9142 case CPP_GREATER_EQ
:
9143 id
= ansi_opname (GE_EXPR
);
9147 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
9151 id
= ansi_opname (TRUTH_ORIF_EXPR
);
9155 id
= ansi_opname (POSTINCREMENT_EXPR
);
9158 case CPP_MINUS_MINUS
:
9159 id
= ansi_opname (PREDECREMENT_EXPR
);
9163 id
= ansi_opname (COMPOUND_EXPR
);
9166 case CPP_DEREF_STAR
:
9167 id
= ansi_opname (MEMBER_REF
);
9171 id
= ansi_opname (COMPONENT_REF
);
9174 case CPP_OPEN_PAREN
:
9175 /* Consume the `('. */
9176 cp_lexer_consume_token (parser
->lexer
);
9177 /* Look for the matching `)'. */
9178 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
9179 return ansi_opname (CALL_EXPR
);
9181 case CPP_OPEN_SQUARE
:
9182 /* Consume the `['. */
9183 cp_lexer_consume_token (parser
->lexer
);
9184 /* Look for the matching `]'. */
9185 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
9186 return ansi_opname (ARRAY_REF
);
9189 /* Anything else is an error. */
9193 /* If we have selected an identifier, we need to consume the
9196 cp_lexer_consume_token (parser
->lexer
);
9197 /* Otherwise, no valid operator name was present. */
9200 cp_parser_error (parser
, "expected operator");
9201 id
= error_mark_node
;
9207 /* Parse a template-declaration.
9209 template-declaration:
9210 export [opt] template < template-parameter-list > declaration
9212 If MEMBER_P is TRUE, this template-declaration occurs within a
9215 The grammar rule given by the standard isn't correct. What
9218 template-declaration:
9219 export [opt] template-parameter-list-seq
9220 decl-specifier-seq [opt] init-declarator [opt] ;
9221 export [opt] template-parameter-list-seq
9224 template-parameter-list-seq:
9225 template-parameter-list-seq [opt]
9226 template < template-parameter-list > */
9229 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
9231 /* Check for `export'. */
9232 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
9234 /* Consume the `export' token. */
9235 cp_lexer_consume_token (parser
->lexer
);
9236 /* Warn that we do not support `export'. */
9237 warning (0, "keyword %<export%> not implemented, and will be ignored");
9240 cp_parser_template_declaration_after_export (parser
, member_p
);
9243 /* Parse a template-parameter-list.
9245 template-parameter-list:
9247 template-parameter-list , template-parameter
9249 Returns a TREE_LIST. Each node represents a template parameter.
9250 The nodes are connected via their TREE_CHAINs. */
9253 cp_parser_template_parameter_list (cp_parser
* parser
)
9255 tree parameter_list
= NULL_TREE
;
9257 begin_template_parm_list ();
9263 bool is_parameter_pack
;
9265 /* Parse the template-parameter. */
9266 parameter
= cp_parser_template_parameter (parser
,
9268 &is_parameter_pack
);
9269 /* Add it to the list. */
9270 if (parameter
!= error_mark_node
)
9271 parameter_list
= process_template_parm (parameter_list
,
9277 tree err_parm
= build_tree_list (parameter
, parameter
);
9278 TREE_VALUE (err_parm
) = error_mark_node
;
9279 parameter_list
= chainon (parameter_list
, err_parm
);
9282 /* Peek at the next token. */
9283 token
= cp_lexer_peek_token (parser
->lexer
);
9284 /* If it's not a `,', we're done. */
9285 if (token
->type
!= CPP_COMMA
)
9287 /* Otherwise, consume the `,' token. */
9288 cp_lexer_consume_token (parser
->lexer
);
9291 return end_template_parm_list (parameter_list
);
9294 /* Parse a template-parameter.
9298 parameter-declaration
9300 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9301 the parameter. The TREE_PURPOSE is the default value, if any.
9302 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9303 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9304 set to true iff this parameter is a parameter pack. */
9307 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
9308 bool *is_parameter_pack
)
9311 cp_parameter_declarator
*parameter_declarator
;
9314 /* Assume it is a type parameter or a template parameter. */
9315 *is_non_type
= false;
9316 /* Assume it not a parameter pack. */
9317 *is_parameter_pack
= false;
9318 /* Peek at the next token. */
9319 token
= cp_lexer_peek_token (parser
->lexer
);
9320 /* If it is `class' or `template', we have a type-parameter. */
9321 if (token
->keyword
== RID_TEMPLATE
)
9322 return cp_parser_type_parameter (parser
, is_parameter_pack
);
9323 /* If it is `class' or `typename' we do not know yet whether it is a
9324 type parameter or a non-type parameter. Consider:
9326 template <typename T, typename T::X X> ...
9330 template <class C, class D*> ...
9332 Here, the first parameter is a type parameter, and the second is
9333 a non-type parameter. We can tell by looking at the token after
9334 the identifier -- if it is a `,', `=', or `>' then we have a type
9336 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
9338 /* Peek at the token after `class' or `typename'. */
9339 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9340 /* If it's an ellipsis, we have a template type parameter
9342 if (token
->type
== CPP_ELLIPSIS
)
9343 return cp_parser_type_parameter (parser
, is_parameter_pack
);
9344 /* If it's an identifier, skip it. */
9345 if (token
->type
== CPP_NAME
)
9346 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
9347 /* Now, see if the token looks like the end of a template
9349 if (token
->type
== CPP_COMMA
9350 || token
->type
== CPP_EQ
9351 || token
->type
== CPP_GREATER
)
9352 return cp_parser_type_parameter (parser
, is_parameter_pack
);
9355 /* Otherwise, it is a non-type parameter.
9359 When parsing a default template-argument for a non-type
9360 template-parameter, the first non-nested `>' is taken as the end
9361 of the template parameter-list rather than a greater-than
9363 *is_non_type
= true;
9364 parameter_declarator
9365 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
9366 /*parenthesized_p=*/NULL
);
9368 /* If the parameter declaration is marked as a parameter pack, set
9369 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9370 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9372 if (parameter_declarator
9373 && parameter_declarator
->declarator
9374 && parameter_declarator
->declarator
->parameter_pack_p
)
9376 *is_parameter_pack
= true;
9377 parameter_declarator
->declarator
->parameter_pack_p
= false;
9380 /* If the next token is an ellipsis, and we don't already have it
9381 marked as a parameter pack, then we have a parameter pack (that
9382 has no declarator); */
9383 if (!*is_parameter_pack
9384 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
9385 && declarator_can_be_parameter_pack (parameter_declarator
->declarator
))
9387 /* Consume the `...'. */
9388 cp_lexer_consume_token (parser
->lexer
);
9389 maybe_warn_variadic_templates ();
9391 *is_parameter_pack
= true;
9394 parm
= grokdeclarator (parameter_declarator
->declarator
,
9395 ¶meter_declarator
->decl_specifiers
,
9396 PARM
, /*initialized=*/0,
9398 if (parm
== error_mark_node
)
9399 return error_mark_node
;
9401 return build_tree_list (parameter_declarator
->default_argument
, parm
);
9404 /* Parse a type-parameter.
9407 class identifier [opt]
9408 class identifier [opt] = type-id
9409 typename identifier [opt]
9410 typename identifier [opt] = type-id
9411 template < template-parameter-list > class identifier [opt]
9412 template < template-parameter-list > class identifier [opt]
9415 GNU Extension (variadic templates):
9418 class ... identifier [opt]
9419 typename ... identifier [opt]
9421 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9422 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9423 the declaration of the parameter.
9425 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9428 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
9433 /* Look for a keyword to tell us what kind of parameter this is. */
9434 token
= cp_parser_require (parser
, CPP_KEYWORD
,
9435 "`class', `typename', or `template'");
9437 return error_mark_node
;
9439 switch (token
->keyword
)
9445 tree default_argument
;
9447 /* If the next token is an ellipsis, we have a template
9449 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9451 /* Consume the `...' token. */
9452 cp_lexer_consume_token (parser
->lexer
);
9453 maybe_warn_variadic_templates ();
9455 *is_parameter_pack
= true;
9458 /* If the next token is an identifier, then it names the
9460 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9461 identifier
= cp_parser_identifier (parser
);
9463 identifier
= NULL_TREE
;
9465 /* Create the parameter. */
9466 parameter
= finish_template_type_parm (class_type_node
, identifier
);
9468 /* If the next token is an `=', we have a default argument. */
9469 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9471 /* Consume the `=' token. */
9472 cp_lexer_consume_token (parser
->lexer
);
9473 /* Parse the default-argument. */
9474 push_deferring_access_checks (dk_no_deferred
);
9475 default_argument
= cp_parser_type_id (parser
);
9477 /* Template parameter packs cannot have default
9479 if (*is_parameter_pack
)
9482 error ("template parameter pack %qD cannot have a default argument",
9485 error ("template parameter packs cannot have default arguments");
9486 default_argument
= NULL_TREE
;
9488 pop_deferring_access_checks ();
9491 default_argument
= NULL_TREE
;
9493 /* Create the combined representation of the parameter and the
9494 default argument. */
9495 parameter
= build_tree_list (default_argument
, parameter
);
9501 tree parameter_list
;
9503 tree default_argument
;
9505 /* Look for the `<'. */
9506 cp_parser_require (parser
, CPP_LESS
, "`<'");
9507 /* Parse the template-parameter-list. */
9508 parameter_list
= cp_parser_template_parameter_list (parser
);
9509 /* Look for the `>'. */
9510 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9511 /* Look for the `class' keyword. */
9512 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
9513 /* If the next token is an ellipsis, we have a template
9515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9517 /* Consume the `...' token. */
9518 cp_lexer_consume_token (parser
->lexer
);
9519 maybe_warn_variadic_templates ();
9521 *is_parameter_pack
= true;
9523 /* If the next token is an `=', then there is a
9524 default-argument. If the next token is a `>', we are at
9525 the end of the parameter-list. If the next token is a `,',
9526 then we are at the end of this parameter. */
9527 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
9528 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
9529 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
9531 identifier
= cp_parser_identifier (parser
);
9532 /* Treat invalid names as if the parameter were nameless. */
9533 if (identifier
== error_mark_node
)
9534 identifier
= NULL_TREE
;
9537 identifier
= NULL_TREE
;
9539 /* Create the template parameter. */
9540 parameter
= finish_template_template_parm (class_type_node
,
9543 /* If the next token is an `=', then there is a
9544 default-argument. */
9545 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9549 /* Consume the `='. */
9550 cp_lexer_consume_token (parser
->lexer
);
9551 /* Parse the id-expression. */
9552 push_deferring_access_checks (dk_no_deferred
);
9554 = cp_parser_id_expression (parser
,
9555 /*template_keyword_p=*/false,
9556 /*check_dependency_p=*/true,
9557 /*template_p=*/&is_template
,
9558 /*declarator_p=*/false,
9559 /*optional_p=*/false);
9560 if (TREE_CODE (default_argument
) == TYPE_DECL
)
9561 /* If the id-expression was a template-id that refers to
9562 a template-class, we already have the declaration here,
9563 so no further lookup is needed. */
9566 /* Look up the name. */
9568 = cp_parser_lookup_name (parser
, default_argument
,
9570 /*is_template=*/is_template
,
9571 /*is_namespace=*/false,
9572 /*check_dependency=*/true,
9573 /*ambiguous_decls=*/NULL
);
9574 /* See if the default argument is valid. */
9576 = check_template_template_default_arg (default_argument
);
9578 /* Template parameter packs cannot have default
9580 if (*is_parameter_pack
)
9583 error ("template parameter pack %qD cannot have a default argument",
9586 error ("template parameter packs cannot have default arguments");
9587 default_argument
= NULL_TREE
;
9589 pop_deferring_access_checks ();
9592 default_argument
= NULL_TREE
;
9594 /* Create the combined representation of the parameter and the
9595 default argument. */
9596 parameter
= build_tree_list (default_argument
, parameter
);
9608 /* Parse a template-id.
9611 template-name < template-argument-list [opt] >
9613 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9614 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9615 returned. Otherwise, if the template-name names a function, or set
9616 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9617 names a class, returns a TYPE_DECL for the specialization.
9619 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9620 uninstantiated templates. */
9623 cp_parser_template_id (cp_parser
*parser
,
9624 bool template_keyword_p
,
9625 bool check_dependency_p
,
9626 bool is_declaration
)
9632 cp_token_position start_of_id
= 0;
9633 deferred_access_check
*chk
;
9634 VEC (deferred_access_check
,gc
) *access_check
;
9635 cp_token
*next_token
, *next_token_2
;
9638 /* If the next token corresponds to a template-id, there is no need
9640 next_token
= cp_lexer_peek_token (parser
->lexer
);
9641 if (next_token
->type
== CPP_TEMPLATE_ID
)
9643 struct tree_check
*check_value
;
9645 /* Get the stored value. */
9646 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
9647 /* Perform any access checks that were deferred. */
9648 access_check
= check_value
->checks
;
9652 VEC_iterate (deferred_access_check
, access_check
, i
, chk
) ;
9655 perform_or_defer_access_check (chk
->binfo
,
9660 /* Return the stored value. */
9661 return check_value
->value
;
9664 /* Avoid performing name lookup if there is no possibility of
9665 finding a template-id. */
9666 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
9667 || (next_token
->type
== CPP_NAME
9668 && !cp_parser_nth_token_starts_template_argument_list_p
9671 cp_parser_error (parser
, "expected template-id");
9672 return error_mark_node
;
9675 /* Remember where the template-id starts. */
9676 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
9677 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
9679 push_deferring_access_checks (dk_deferred
);
9681 /* Parse the template-name. */
9682 is_identifier
= false;
9683 template = cp_parser_template_name (parser
, template_keyword_p
,
9687 if (template == error_mark_node
|| is_identifier
)
9689 pop_deferring_access_checks ();
9693 /* If we find the sequence `[:' after a template-name, it's probably
9694 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9695 parse correctly the argument list. */
9696 next_token
= cp_lexer_peek_token (parser
->lexer
);
9697 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9698 if (next_token
->type
== CPP_OPEN_SQUARE
9699 && next_token
->flags
& DIGRAPH
9700 && next_token_2
->type
== CPP_COLON
9701 && !(next_token_2
->flags
& PREV_WHITE
))
9703 cp_parser_parse_tentatively (parser
);
9704 /* Change `:' into `::'. */
9705 next_token_2
->type
= CPP_SCOPE
;
9706 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9708 cp_lexer_consume_token (parser
->lexer
);
9709 /* Parse the arguments. */
9710 arguments
= cp_parser_enclosed_template_argument_list (parser
);
9711 if (!cp_parser_parse_definitely (parser
))
9713 /* If we couldn't parse an argument list, then we revert our changes
9714 and return simply an error. Maybe this is not a template-id
9716 next_token_2
->type
= CPP_COLON
;
9717 cp_parser_error (parser
, "expected %<<%>");
9718 pop_deferring_access_checks ();
9719 return error_mark_node
;
9721 /* Otherwise, emit an error about the invalid digraph, but continue
9722 parsing because we got our argument list. */
9723 pedwarn ("%<<::%> cannot begin a template-argument list");
9724 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9725 "between %<<%> and %<::%>");
9726 if (!flag_permissive
)
9731 inform ("(if you use -fpermissive G++ will accept your code)");
9738 /* Look for the `<' that starts the template-argument-list. */
9739 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
9741 pop_deferring_access_checks ();
9742 return error_mark_node
;
9744 /* Parse the arguments. */
9745 arguments
= cp_parser_enclosed_template_argument_list (parser
);
9748 /* Build a representation of the specialization. */
9749 if (TREE_CODE (template) == IDENTIFIER_NODE
)
9750 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
9751 else if (DECL_CLASS_TEMPLATE_P (template)
9752 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9754 bool entering_scope
;
9755 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9756 template (rather than some instantiation thereof) only if
9757 is not nested within some other construct. For example, in
9758 "template <typename T> void f(T) { A<T>::", A<T> is just an
9759 instantiation of A. */
9760 entering_scope
= (template_parm_scope_p ()
9761 && cp_lexer_next_token_is (parser
->lexer
,
9764 = finish_template_type (template, arguments
, entering_scope
);
9768 /* If it's not a class-template or a template-template, it should be
9769 a function-template. */
9770 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9771 || TREE_CODE (template) == OVERLOAD
9772 || BASELINK_P (template)));
9774 template_id
= lookup_template_function (template, arguments
);
9777 /* If parsing tentatively, replace the sequence of tokens that makes
9778 up the template-id with a CPP_TEMPLATE_ID token. That way,
9779 should we re-parse the token stream, we will not have to repeat
9780 the effort required to do the parse, nor will we issue duplicate
9781 error messages about problems during instantiation of the
9785 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
9787 /* Reset the contents of the START_OF_ID token. */
9788 token
->type
= CPP_TEMPLATE_ID
;
9789 /* Retrieve any deferred checks. Do not pop this access checks yet
9790 so the memory will not be reclaimed during token replacing below. */
9791 token
->u
.tree_check_value
= GGC_CNEW (struct tree_check
);
9792 token
->u
.tree_check_value
->value
= template_id
;
9793 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
9794 token
->keyword
= RID_MAX
;
9796 /* Purge all subsequent tokens. */
9797 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
9799 /* ??? Can we actually assume that, if template_id ==
9800 error_mark_node, we will have issued a diagnostic to the
9801 user, as opposed to simply marking the tentative parse as
9803 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
9804 error ("parse error in template argument list");
9807 pop_deferring_access_checks ();
9811 /* Parse a template-name.
9816 The standard should actually say:
9820 operator-function-id
9822 A defect report has been filed about this issue.
9824 A conversion-function-id cannot be a template name because they cannot
9825 be part of a template-id. In fact, looking at this code:
9829 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9830 It is impossible to call a templated conversion-function-id with an
9831 explicit argument list, since the only allowed template parameter is
9832 the type to which it is converting.
9834 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9835 `template' keyword, in a construction like:
9839 In that case `f' is taken to be a template-name, even though there
9840 is no way of knowing for sure.
9842 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9843 name refers to a set of overloaded functions, at least one of which
9844 is a template, or an IDENTIFIER_NODE with the name of the template,
9845 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9846 names are looked up inside uninstantiated templates. */
9849 cp_parser_template_name (cp_parser
* parser
,
9850 bool template_keyword_p
,
9851 bool check_dependency_p
,
9852 bool is_declaration
,
9853 bool *is_identifier
)
9859 /* If the next token is `operator', then we have either an
9860 operator-function-id or a conversion-function-id. */
9861 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
9863 /* We don't know whether we're looking at an
9864 operator-function-id or a conversion-function-id. */
9865 cp_parser_parse_tentatively (parser
);
9866 /* Try an operator-function-id. */
9867 identifier
= cp_parser_operator_function_id (parser
);
9868 /* If that didn't work, try a conversion-function-id. */
9869 if (!cp_parser_parse_definitely (parser
))
9871 cp_parser_error (parser
, "expected template-name");
9872 return error_mark_node
;
9875 /* Look for the identifier. */
9877 identifier
= cp_parser_identifier (parser
);
9879 /* If we didn't find an identifier, we don't have a template-id. */
9880 if (identifier
== error_mark_node
)
9881 return error_mark_node
;
9883 /* If the name immediately followed the `template' keyword, then it
9884 is a template-name. However, if the next token is not `<', then
9885 we do not treat it as a template-name, since it is not being used
9886 as part of a template-id. This enables us to handle constructs
9889 template <typename T> struct S { S(); };
9890 template <typename T> S<T>::S();
9892 correctly. We would treat `S' as a template -- if it were `S<T>'
9893 -- but we do not if there is no `<'. */
9895 if (processing_template_decl
9896 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
9898 /* In a declaration, in a dependent context, we pretend that the
9899 "template" keyword was present in order to improve error
9900 recovery. For example, given:
9902 template <typename T> void f(T::X<int>);
9904 we want to treat "X<int>" as a template-id. */
9906 && !template_keyword_p
9907 && parser
->scope
&& TYPE_P (parser
->scope
)
9908 && check_dependency_p
9909 && dependent_type_p (parser
->scope
)
9910 /* Do not do this for dtors (or ctors), since they never
9911 need the template keyword before their name. */
9912 && !constructor_name_p (identifier
, parser
->scope
))
9914 cp_token_position start
= 0;
9916 /* Explain what went wrong. */
9917 error ("non-template %qD used as template", identifier
);
9918 inform ("use %<%T::template %D%> to indicate that it is a template",
9919 parser
->scope
, identifier
);
9920 /* If parsing tentatively, find the location of the "<" token. */
9921 if (cp_parser_simulate_error (parser
))
9922 start
= cp_lexer_token_position (parser
->lexer
, true);
9923 /* Parse the template arguments so that we can issue error
9924 messages about them. */
9925 cp_lexer_consume_token (parser
->lexer
);
9926 cp_parser_enclosed_template_argument_list (parser
);
9927 /* Skip tokens until we find a good place from which to
9928 continue parsing. */
9929 cp_parser_skip_to_closing_parenthesis (parser
,
9930 /*recovering=*/true,
9932 /*consume_paren=*/false);
9933 /* If parsing tentatively, permanently remove the
9934 template argument list. That will prevent duplicate
9935 error messages from being issued about the missing
9936 "template" keyword. */
9938 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
9940 *is_identifier
= true;
9944 /* If the "template" keyword is present, then there is generally
9945 no point in doing name-lookup, so we just return IDENTIFIER.
9946 But, if the qualifying scope is non-dependent then we can
9947 (and must) do name-lookup normally. */
9948 if (template_keyword_p
9950 || (TYPE_P (parser
->scope
)
9951 && dependent_type_p (parser
->scope
))))
9955 /* Look up the name. */
9956 decl
= cp_parser_lookup_name (parser
, identifier
,
9958 /*is_template=*/false,
9959 /*is_namespace=*/false,
9961 /*ambiguous_decls=*/NULL
);
9962 decl
= maybe_get_template_decl_from_type_decl (decl
);
9964 /* If DECL is a template, then the name was a template-name. */
9965 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9969 tree fn
= NULL_TREE
;
9971 /* The standard does not explicitly indicate whether a name that
9972 names a set of overloaded declarations, some of which are
9973 templates, is a template-name. However, such a name should
9974 be a template-name; otherwise, there is no way to form a
9975 template-id for the overloaded templates. */
9976 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
9977 if (TREE_CODE (fns
) == OVERLOAD
)
9978 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
9979 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
9984 /* The name does not name a template. */
9985 cp_parser_error (parser
, "expected template-name");
9986 return error_mark_node
;
9990 /* If DECL is dependent, and refers to a function, then just return
9991 its name; we will look it up again during template instantiation. */
9992 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
9994 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
9995 if (TYPE_P (scope
) && dependent_type_p (scope
))
10002 /* Parse a template-argument-list.
10004 template-argument-list:
10005 template-argument ... [opt]
10006 template-argument-list , template-argument ... [opt]
10008 Returns a TREE_VEC containing the arguments. */
10011 cp_parser_template_argument_list (cp_parser
* parser
)
10013 tree fixed_args
[10];
10014 unsigned n_args
= 0;
10015 unsigned alloced
= 10;
10016 tree
*arg_ary
= fixed_args
;
10018 bool saved_in_template_argument_list_p
;
10020 bool saved_non_ice_p
;
10022 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
10023 parser
->in_template_argument_list_p
= true;
10024 /* Even if the template-id appears in an integral
10025 constant-expression, the contents of the argument list do
10027 saved_ice_p
= parser
->integral_constant_expression_p
;
10028 parser
->integral_constant_expression_p
= false;
10029 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
10030 parser
->non_integral_constant_expression_p
= false;
10031 /* Parse the arguments. */
10037 /* Consume the comma. */
10038 cp_lexer_consume_token (parser
->lexer
);
10040 /* Parse the template-argument. */
10041 argument
= cp_parser_template_argument (parser
);
10043 /* If the next token is an ellipsis, we're expanding a template
10045 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10047 /* Consume the `...' token. */
10048 cp_lexer_consume_token (parser
->lexer
);
10050 /* Make the argument into a TYPE_PACK_EXPANSION or
10051 EXPR_PACK_EXPANSION. */
10052 argument
= make_pack_expansion (argument
);
10055 if (n_args
== alloced
)
10059 if (arg_ary
== fixed_args
)
10061 arg_ary
= XNEWVEC (tree
, alloced
);
10062 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
10065 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
10067 arg_ary
[n_args
++] = argument
;
10069 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
10071 vec
= make_tree_vec (n_args
);
10074 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
10076 if (arg_ary
!= fixed_args
)
10078 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
10079 parser
->integral_constant_expression_p
= saved_ice_p
;
10080 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
10084 /* Parse a template-argument.
10087 assignment-expression
10091 The representation is that of an assignment-expression, type-id, or
10092 id-expression -- except that the qualified id-expression is
10093 evaluated, so that the value returned is either a DECL or an
10096 Although the standard says "assignment-expression", it forbids
10097 throw-expressions or assignments in the template argument.
10098 Therefore, we use "conditional-expression" instead. */
10101 cp_parser_template_argument (cp_parser
* parser
)
10106 bool maybe_type_id
= false;
10110 /* There's really no way to know what we're looking at, so we just
10111 try each alternative in order.
10115 In a template-argument, an ambiguity between a type-id and an
10116 expression is resolved to a type-id, regardless of the form of
10117 the corresponding template-parameter.
10119 Therefore, we try a type-id first. */
10120 cp_parser_parse_tentatively (parser
);
10121 argument
= cp_parser_type_id (parser
);
10122 /* If there was no error parsing the type-id but the next token is a '>>',
10123 we probably found a typo for '> >'. But there are type-id which are
10124 also valid expressions. For instance:
10126 struct X { int operator >> (int); };
10127 template <int V> struct Foo {};
10130 Here 'X()' is a valid type-id of a function type, but the user just
10131 wanted to write the expression "X() >> 5". Thus, we remember that we
10132 found a valid type-id, but we still try to parse the argument as an
10133 expression to see what happens. */
10134 if (!cp_parser_error_occurred (parser
)
10135 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
10137 maybe_type_id
= true;
10138 cp_parser_abort_tentative_parse (parser
);
10142 /* If the next token isn't a `,' or a `>', then this argument wasn't
10143 really finished. This means that the argument is not a valid
10145 if (!cp_parser_next_token_ends_template_argument_p (parser
))
10146 cp_parser_error (parser
, "expected template-argument");
10147 /* If that worked, we're done. */
10148 if (cp_parser_parse_definitely (parser
))
10151 /* We're still not sure what the argument will be. */
10152 cp_parser_parse_tentatively (parser
);
10153 /* Try a template. */
10154 argument
= cp_parser_id_expression (parser
,
10155 /*template_keyword_p=*/false,
10156 /*check_dependency_p=*/true,
10158 /*declarator_p=*/false,
10159 /*optional_p=*/false);
10160 /* If the next token isn't a `,' or a `>', then this argument wasn't
10161 really finished. */
10162 if (!cp_parser_next_token_ends_template_argument_p (parser
))
10163 cp_parser_error (parser
, "expected template-argument");
10164 if (!cp_parser_error_occurred (parser
))
10166 /* Figure out what is being referred to. If the id-expression
10167 was for a class template specialization, then we will have a
10168 TYPE_DECL at this point. There is no need to do name lookup
10169 at this point in that case. */
10170 if (TREE_CODE (argument
) != TYPE_DECL
)
10171 argument
= cp_parser_lookup_name (parser
, argument
,
10173 /*is_template=*/template_p
,
10174 /*is_namespace=*/false,
10175 /*check_dependency=*/true,
10176 /*ambiguous_decls=*/NULL
);
10177 if (TREE_CODE (argument
) != TEMPLATE_DECL
10178 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
10179 cp_parser_error (parser
, "expected template-name");
10181 if (cp_parser_parse_definitely (parser
))
10183 /* It must be a non-type argument. There permitted cases are given
10184 in [temp.arg.nontype]:
10186 -- an integral constant-expression of integral or enumeration
10189 -- the name of a non-type template-parameter; or
10191 -- the name of an object or function with external linkage...
10193 -- the address of an object or function with external linkage...
10195 -- a pointer to member... */
10196 /* Look for a non-type template parameter. */
10197 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10199 cp_parser_parse_tentatively (parser
);
10200 argument
= cp_parser_primary_expression (parser
,
10201 /*adress_p=*/false,
10203 /*template_arg_p=*/true,
10205 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
10206 || !cp_parser_next_token_ends_template_argument_p (parser
))
10207 cp_parser_simulate_error (parser
);
10208 if (cp_parser_parse_definitely (parser
))
10212 /* If the next token is "&", the argument must be the address of an
10213 object or function with external linkage. */
10214 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
10216 cp_lexer_consume_token (parser
->lexer
);
10217 /* See if we might have an id-expression. */
10218 token
= cp_lexer_peek_token (parser
->lexer
);
10219 if (token
->type
== CPP_NAME
10220 || token
->keyword
== RID_OPERATOR
10221 || token
->type
== CPP_SCOPE
10222 || token
->type
== CPP_TEMPLATE_ID
10223 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
10225 cp_parser_parse_tentatively (parser
);
10226 argument
= cp_parser_primary_expression (parser
,
10229 /*template_arg_p=*/true,
10231 if (cp_parser_error_occurred (parser
)
10232 || !cp_parser_next_token_ends_template_argument_p (parser
))
10233 cp_parser_abort_tentative_parse (parser
);
10236 if (TREE_CODE (argument
) == INDIRECT_REF
)
10238 gcc_assert (REFERENCE_REF_P (argument
));
10239 argument
= TREE_OPERAND (argument
, 0);
10242 if (TREE_CODE (argument
) == VAR_DECL
)
10244 /* A variable without external linkage might still be a
10245 valid constant-expression, so no error is issued here
10246 if the external-linkage check fails. */
10247 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (argument
))
10248 cp_parser_simulate_error (parser
);
10250 else if (is_overloaded_fn (argument
))
10251 /* All overloaded functions are allowed; if the external
10252 linkage test does not pass, an error will be issued
10256 && (TREE_CODE (argument
) == OFFSET_REF
10257 || TREE_CODE (argument
) == SCOPE_REF
))
10258 /* A pointer-to-member. */
10260 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
10263 cp_parser_simulate_error (parser
);
10265 if (cp_parser_parse_definitely (parser
))
10268 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
10273 /* If the argument started with "&", there are no other valid
10274 alternatives at this point. */
10277 cp_parser_error (parser
, "invalid non-type template argument");
10278 return error_mark_node
;
10281 /* If the argument wasn't successfully parsed as a type-id followed
10282 by '>>', the argument can only be a constant expression now.
10283 Otherwise, we try parsing the constant-expression tentatively,
10284 because the argument could really be a type-id. */
10286 cp_parser_parse_tentatively (parser
);
10287 argument
= cp_parser_constant_expression (parser
,
10288 /*allow_non_constant_p=*/false,
10289 /*non_constant_p=*/NULL
);
10290 argument
= fold_non_dependent_expr (argument
);
10291 if (!maybe_type_id
)
10293 if (!cp_parser_next_token_ends_template_argument_p (parser
))
10294 cp_parser_error (parser
, "expected template-argument");
10295 if (cp_parser_parse_definitely (parser
))
10297 /* We did our best to parse the argument as a non type-id, but that
10298 was the only alternative that matched (albeit with a '>' after
10299 it). We can assume it's just a typo from the user, and a
10300 diagnostic will then be issued. */
10301 return cp_parser_type_id (parser
);
10304 /* Parse an explicit-instantiation.
10306 explicit-instantiation:
10307 template declaration
10309 Although the standard says `declaration', what it really means is:
10311 explicit-instantiation:
10312 template decl-specifier-seq [opt] declarator [opt] ;
10314 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10315 supposed to be allowed. A defect report has been filed about this
10320 explicit-instantiation:
10321 storage-class-specifier template
10322 decl-specifier-seq [opt] declarator [opt] ;
10323 function-specifier template
10324 decl-specifier-seq [opt] declarator [opt] ; */
10327 cp_parser_explicit_instantiation (cp_parser
* parser
)
10329 int declares_class_or_enum
;
10330 cp_decl_specifier_seq decl_specifiers
;
10331 tree extension_specifier
= NULL_TREE
;
10333 /* Look for an (optional) storage-class-specifier or
10334 function-specifier. */
10335 if (cp_parser_allow_gnu_extensions_p (parser
))
10337 extension_specifier
10338 = cp_parser_storage_class_specifier_opt (parser
);
10339 if (!extension_specifier
)
10340 extension_specifier
10341 = cp_parser_function_specifier_opt (parser
,
10342 /*decl_specs=*/NULL
);
10345 /* Look for the `template' keyword. */
10346 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
10347 /* Let the front end know that we are processing an explicit
10349 begin_explicit_instantiation ();
10350 /* [temp.explicit] says that we are supposed to ignore access
10351 control while processing explicit instantiation directives. */
10352 push_deferring_access_checks (dk_no_check
);
10353 /* Parse a decl-specifier-seq. */
10354 cp_parser_decl_specifier_seq (parser
,
10355 CP_PARSER_FLAGS_OPTIONAL
,
10357 &declares_class_or_enum
);
10358 /* If there was exactly one decl-specifier, and it declared a class,
10359 and there's no declarator, then we have an explicit type
10361 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
10365 type
= check_tag_decl (&decl_specifiers
);
10366 /* Turn access control back on for names used during
10367 template instantiation. */
10368 pop_deferring_access_checks ();
10370 do_type_instantiation (type
, extension_specifier
,
10371 /*complain=*/tf_error
);
10375 cp_declarator
*declarator
;
10378 /* Parse the declarator. */
10380 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10381 /*ctor_dtor_or_conv_p=*/NULL
,
10382 /*parenthesized_p=*/NULL
,
10383 /*member_p=*/false);
10384 if (declares_class_or_enum
& 2)
10385 cp_parser_check_for_definition_in_return_type (declarator
,
10386 decl_specifiers
.type
);
10387 if (declarator
!= cp_error_declarator
)
10389 decl
= grokdeclarator (declarator
, &decl_specifiers
,
10390 NORMAL
, 0, &decl_specifiers
.attributes
);
10391 /* Turn access control back on for names used during
10392 template instantiation. */
10393 pop_deferring_access_checks ();
10394 /* Do the explicit instantiation. */
10395 do_decl_instantiation (decl
, extension_specifier
);
10399 pop_deferring_access_checks ();
10400 /* Skip the body of the explicit instantiation. */
10401 cp_parser_skip_to_end_of_statement (parser
);
10404 /* We're done with the instantiation. */
10405 end_explicit_instantiation ();
10407 cp_parser_consume_semicolon_at_end_of_statement (parser
);
10410 /* Parse an explicit-specialization.
10412 explicit-specialization:
10413 template < > declaration
10415 Although the standard says `declaration', what it really means is:
10417 explicit-specialization:
10418 template <> decl-specifier [opt] init-declarator [opt] ;
10419 template <> function-definition
10420 template <> explicit-specialization
10421 template <> template-declaration */
10424 cp_parser_explicit_specialization (cp_parser
* parser
)
10426 bool need_lang_pop
;
10427 /* Look for the `template' keyword. */
10428 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
10429 /* Look for the `<'. */
10430 cp_parser_require (parser
, CPP_LESS
, "`<'");
10431 /* Look for the `>'. */
10432 cp_parser_require (parser
, CPP_GREATER
, "`>'");
10433 /* We have processed another parameter list. */
10434 ++parser
->num_template_parameter_lists
;
10437 A template ... explicit specialization ... shall not have C
10439 if (current_lang_name
== lang_name_c
)
10441 error ("template specialization with C linkage");
10442 /* Give it C++ linkage to avoid confusing other parts of the
10444 push_lang_context (lang_name_cplusplus
);
10445 need_lang_pop
= true;
10448 need_lang_pop
= false;
10449 /* Let the front end know that we are beginning a specialization. */
10450 if (!begin_specialization ())
10452 end_specialization ();
10453 cp_parser_skip_to_end_of_block_or_statement (parser
);
10457 /* If the next keyword is `template', we need to figure out whether
10458 or not we're looking a template-declaration. */
10459 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
10461 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
10462 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
10463 cp_parser_template_declaration_after_export (parser
,
10464 /*member_p=*/false);
10466 cp_parser_explicit_specialization (parser
);
10469 /* Parse the dependent declaration. */
10470 cp_parser_single_declaration (parser
,
10472 /*member_p=*/false,
10473 /*explicit_specialization_p=*/true,
10474 /*friend_p=*/NULL
);
10475 /* We're done with the specialization. */
10476 end_specialization ();
10477 /* For the erroneous case of a template with C linkage, we pushed an
10478 implicit C++ linkage scope; exit that scope now. */
10480 pop_lang_context ();
10481 /* We're done with this parameter list. */
10482 --parser
->num_template_parameter_lists
;
10485 /* Parse a type-specifier.
10488 simple-type-specifier
10491 elaborated-type-specifier
10499 Returns a representation of the type-specifier. For a
10500 class-specifier, enum-specifier, or elaborated-type-specifier, a
10501 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10503 The parser flags FLAGS is used to control type-specifier parsing.
10505 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10506 in a decl-specifier-seq.
10508 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10509 class-specifier, enum-specifier, or elaborated-type-specifier, then
10510 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10511 if a type is declared; 2 if it is defined. Otherwise, it is set to
10514 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10515 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10516 is set to FALSE. */
10519 cp_parser_type_specifier (cp_parser
* parser
,
10520 cp_parser_flags flags
,
10521 cp_decl_specifier_seq
*decl_specs
,
10522 bool is_declaration
,
10523 int* declares_class_or_enum
,
10524 bool* is_cv_qualifier
)
10526 tree type_spec
= NULL_TREE
;
10529 cp_decl_spec ds
= ds_last
;
10531 /* Assume this type-specifier does not declare a new type. */
10532 if (declares_class_or_enum
)
10533 *declares_class_or_enum
= 0;
10534 /* And that it does not specify a cv-qualifier. */
10535 if (is_cv_qualifier
)
10536 *is_cv_qualifier
= false;
10537 /* Peek at the next token. */
10538 token
= cp_lexer_peek_token (parser
->lexer
);
10540 /* If we're looking at a keyword, we can use that to guide the
10541 production we choose. */
10542 keyword
= token
->keyword
;
10546 /* Look for the enum-specifier. */
10547 type_spec
= cp_parser_enum_specifier (parser
);
10548 /* If that worked, we're done. */
10551 if (declares_class_or_enum
)
10552 *declares_class_or_enum
= 2;
10554 cp_parser_set_decl_spec_type (decl_specs
,
10556 /*user_defined_p=*/true);
10560 goto elaborated_type_specifier
;
10562 /* Any of these indicate either a class-specifier, or an
10563 elaborated-type-specifier. */
10567 /* Parse tentatively so that we can back up if we don't find a
10568 class-specifier. */
10569 cp_parser_parse_tentatively (parser
);
10570 /* Look for the class-specifier. */
10571 type_spec
= cp_parser_class_specifier (parser
);
10572 /* If that worked, we're done. */
10573 if (cp_parser_parse_definitely (parser
))
10575 if (declares_class_or_enum
)
10576 *declares_class_or_enum
= 2;
10578 cp_parser_set_decl_spec_type (decl_specs
,
10580 /*user_defined_p=*/true);
10584 /* Fall through. */
10585 elaborated_type_specifier
:
10586 /* We're declaring (not defining) a class or enum. */
10587 if (declares_class_or_enum
)
10588 *declares_class_or_enum
= 1;
10590 /* Fall through. */
10592 /* Look for an elaborated-type-specifier. */
10594 = (cp_parser_elaborated_type_specifier
10596 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
10599 cp_parser_set_decl_spec_type (decl_specs
,
10601 /*user_defined_p=*/true);
10606 if (is_cv_qualifier
)
10607 *is_cv_qualifier
= true;
10612 if (is_cv_qualifier
)
10613 *is_cv_qualifier
= true;
10618 if (is_cv_qualifier
)
10619 *is_cv_qualifier
= true;
10623 /* The `__complex__' keyword is a GNU extension. */
10631 /* Handle simple keywords. */
10636 ++decl_specs
->specs
[(int)ds
];
10637 decl_specs
->any_specifiers_p
= true;
10639 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
10642 /* If we do not already have a type-specifier, assume we are looking
10643 at a simple-type-specifier. */
10644 type_spec
= cp_parser_simple_type_specifier (parser
,
10648 /* If we didn't find a type-specifier, and a type-specifier was not
10649 optional in this context, issue an error message. */
10650 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
10652 cp_parser_error (parser
, "expected type specifier");
10653 return error_mark_node
;
10659 /* Parse a simple-type-specifier.
10661 simple-type-specifier:
10662 :: [opt] nested-name-specifier [opt] type-name
10663 :: [opt] nested-name-specifier template template-id
10678 simple-type-specifier:
10679 decltype ( expression )
10683 simple-type-specifier:
10684 __typeof__ unary-expression
10685 __typeof__ ( type-id )
10687 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10688 appropriately updated. */
10691 cp_parser_simple_type_specifier (cp_parser
* parser
,
10692 cp_decl_specifier_seq
*decl_specs
,
10693 cp_parser_flags flags
)
10695 tree type
= NULL_TREE
;
10698 /* Peek at the next token. */
10699 token
= cp_lexer_peek_token (parser
->lexer
);
10701 /* If we're looking at a keyword, things are easy. */
10702 switch (token
->keyword
)
10706 decl_specs
->explicit_char_p
= true;
10707 type
= char_type_node
;
10710 type
= wchar_type_node
;
10713 type
= boolean_type_node
;
10717 ++decl_specs
->specs
[(int) ds_short
];
10718 type
= short_integer_type_node
;
10722 decl_specs
->explicit_int_p
= true;
10723 type
= integer_type_node
;
10727 ++decl_specs
->specs
[(int) ds_long
];
10728 type
= long_integer_type_node
;
10732 ++decl_specs
->specs
[(int) ds_signed
];
10733 type
= integer_type_node
;
10737 ++decl_specs
->specs
[(int) ds_unsigned
];
10738 type
= unsigned_type_node
;
10741 type
= float_type_node
;
10744 type
= double_type_node
;
10747 type
= void_type_node
;
10751 /* Parse the `decltype' type. */
10752 type
= cp_parser_decltype (parser
);
10755 cp_parser_set_decl_spec_type (decl_specs
, type
,
10756 /*user_defined_p=*/true);
10761 /* Consume the `typeof' token. */
10762 cp_lexer_consume_token (parser
->lexer
);
10763 /* Parse the operand to `typeof'. */
10764 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
10765 /* If it is not already a TYPE, take its type. */
10766 if (!TYPE_P (type
))
10767 type
= finish_typeof (type
);
10770 cp_parser_set_decl_spec_type (decl_specs
, type
,
10771 /*user_defined_p=*/true);
10779 /* If the type-specifier was for a built-in type, we're done. */
10784 /* Record the type. */
10786 && (token
->keyword
!= RID_SIGNED
10787 && token
->keyword
!= RID_UNSIGNED
10788 && token
->keyword
!= RID_SHORT
10789 && token
->keyword
!= RID_LONG
))
10790 cp_parser_set_decl_spec_type (decl_specs
,
10792 /*user_defined=*/false);
10794 decl_specs
->any_specifiers_p
= true;
10796 /* Consume the token. */
10797 id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
10799 /* There is no valid C++ program where a non-template type is
10800 followed by a "<". That usually indicates that the user thought
10801 that the type was a template. */
10802 cp_parser_check_for_invalid_template_id (parser
, type
);
10804 return TYPE_NAME (type
);
10807 /* The type-specifier must be a user-defined type. */
10808 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
10813 /* Don't gobble tokens or issue error messages if this is an
10814 optional type-specifier. */
10815 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
10816 cp_parser_parse_tentatively (parser
);
10818 /* Look for the optional `::' operator. */
10820 = (cp_parser_global_scope_opt (parser
,
10821 /*current_scope_valid_p=*/false)
10823 /* Look for the nested-name specifier. */
10825 = (cp_parser_nested_name_specifier_opt (parser
,
10826 /*typename_keyword_p=*/false,
10827 /*check_dependency_p=*/true,
10829 /*is_declaration=*/false)
10831 /* If we have seen a nested-name-specifier, and the next token
10832 is `template', then we are using the template-id production. */
10834 && cp_parser_optional_template_keyword (parser
))
10836 /* Look for the template-id. */
10837 type
= cp_parser_template_id (parser
,
10838 /*template_keyword_p=*/true,
10839 /*check_dependency_p=*/true,
10840 /*is_declaration=*/false);
10841 /* If the template-id did not name a type, we are out of
10843 if (TREE_CODE (type
) != TYPE_DECL
)
10845 cp_parser_error (parser
, "expected template-id for type");
10849 /* Otherwise, look for a type-name. */
10851 type
= cp_parser_type_name (parser
);
10852 /* Keep track of all name-lookups performed in class scopes. */
10856 && TREE_CODE (type
) == TYPE_DECL
10857 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
10858 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
10859 /* If it didn't work out, we don't have a TYPE. */
10860 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
10861 && !cp_parser_parse_definitely (parser
))
10863 if (type
&& decl_specs
)
10864 cp_parser_set_decl_spec_type (decl_specs
, type
,
10865 /*user_defined=*/true);
10868 /* If we didn't get a type-name, issue an error message. */
10869 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
10871 cp_parser_error (parser
, "expected type-name");
10872 return error_mark_node
;
10875 /* There is no valid C++ program where a non-template type is
10876 followed by a "<". That usually indicates that the user thought
10877 that the type was a template. */
10878 if (type
&& type
!= error_mark_node
)
10880 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10881 If it is, then the '<'...'>' enclose protocol names rather than
10882 template arguments, and so everything is fine. */
10883 if (c_dialect_objc ()
10884 && (objc_is_id (type
) || objc_is_class_name (type
)))
10886 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
10887 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
10889 /* Clobber the "unqualified" type previously entered into
10890 DECL_SPECS with the new, improved protocol-qualified version. */
10892 decl_specs
->type
= qual_type
;
10897 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
10903 /* Parse a type-name.
10916 Returns a TYPE_DECL for the type. */
10919 cp_parser_type_name (cp_parser
* parser
)
10924 /* We can't know yet whether it is a class-name or not. */
10925 cp_parser_parse_tentatively (parser
);
10926 /* Try a class-name. */
10927 type_decl
= cp_parser_class_name (parser
,
10928 /*typename_keyword_p=*/false,
10929 /*template_keyword_p=*/false,
10931 /*check_dependency_p=*/true,
10932 /*class_head_p=*/false,
10933 /*is_declaration=*/false);
10934 /* If it's not a class-name, keep looking. */
10935 if (!cp_parser_parse_definitely (parser
))
10937 /* It must be a typedef-name or an enum-name. */
10938 identifier
= cp_parser_identifier (parser
);
10939 if (identifier
== error_mark_node
)
10940 return error_mark_node
;
10942 /* Look up the type-name. */
10943 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10945 if (TREE_CODE (type_decl
) != TYPE_DECL
10946 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
10948 /* See if this is an Objective-C type. */
10949 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
10950 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
10952 type_decl
= TYPE_NAME (type
);
10955 /* Issue an error if we did not find a type-name. */
10956 if (TREE_CODE (type_decl
) != TYPE_DECL
)
10958 if (!cp_parser_simulate_error (parser
))
10959 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
10961 type_decl
= error_mark_node
;
10963 /* Remember that the name was used in the definition of the
10964 current class so that we can check later to see if the
10965 meaning would have been different after the class was
10966 entirely defined. */
10967 else if (type_decl
!= error_mark_node
10969 maybe_note_name_used_in_class (identifier
, type_decl
);
10976 /* Parse an elaborated-type-specifier. Note that the grammar given
10977 here incorporates the resolution to DR68.
10979 elaborated-type-specifier:
10980 class-key :: [opt] nested-name-specifier [opt] identifier
10981 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10982 enum :: [opt] nested-name-specifier [opt] identifier
10983 typename :: [opt] nested-name-specifier identifier
10984 typename :: [opt] nested-name-specifier template [opt]
10989 elaborated-type-specifier:
10990 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10991 class-key attributes :: [opt] nested-name-specifier [opt]
10992 template [opt] template-id
10993 enum attributes :: [opt] nested-name-specifier [opt] identifier
10995 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10996 declared `friend'. If IS_DECLARATION is TRUE, then this
10997 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10998 something is being declared.
11000 Returns the TYPE specified. */
11003 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
11005 bool is_declaration
)
11007 enum tag_types tag_type
;
11009 tree type
= NULL_TREE
;
11010 tree attributes
= NULL_TREE
;
11012 /* See if we're looking at the `enum' keyword. */
11013 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
11015 /* Consume the `enum' token. */
11016 cp_lexer_consume_token (parser
->lexer
);
11017 /* Remember that it's an enumeration type. */
11018 tag_type
= enum_type
;
11019 /* Parse the attributes. */
11020 attributes
= cp_parser_attributes_opt (parser
);
11022 /* Or, it might be `typename'. */
11023 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11026 /* Consume the `typename' token. */
11027 cp_lexer_consume_token (parser
->lexer
);
11028 /* Remember that it's a `typename' type. */
11029 tag_type
= typename_type
;
11030 /* The `typename' keyword is only allowed in templates. */
11031 if (!processing_template_decl
)
11032 pedwarn ("using %<typename%> outside of template");
11034 /* Otherwise it must be a class-key. */
11037 tag_type
= cp_parser_class_key (parser
);
11038 if (tag_type
== none_type
)
11039 return error_mark_node
;
11040 /* Parse the attributes. */
11041 attributes
= cp_parser_attributes_opt (parser
);
11044 /* Look for the `::' operator. */
11045 cp_parser_global_scope_opt (parser
,
11046 /*current_scope_valid_p=*/false);
11047 /* Look for the nested-name-specifier. */
11048 if (tag_type
== typename_type
)
11050 if (!cp_parser_nested_name_specifier (parser
,
11051 /*typename_keyword_p=*/true,
11052 /*check_dependency_p=*/true,
11055 return error_mark_node
;
11058 /* Even though `typename' is not present, the proposed resolution
11059 to Core Issue 180 says that in `class A<T>::B', `B' should be
11060 considered a type-name, even if `A<T>' is dependent. */
11061 cp_parser_nested_name_specifier_opt (parser
,
11062 /*typename_keyword_p=*/true,
11063 /*check_dependency_p=*/true,
11066 /* For everything but enumeration types, consider a template-id.
11067 For an enumeration type, consider only a plain identifier. */
11068 if (tag_type
!= enum_type
)
11070 bool template_p
= false;
11073 /* Allow the `template' keyword. */
11074 template_p
= cp_parser_optional_template_keyword (parser
);
11075 /* If we didn't see `template', we don't know if there's a
11076 template-id or not. */
11078 cp_parser_parse_tentatively (parser
);
11079 /* Parse the template-id. */
11080 decl
= cp_parser_template_id (parser
, template_p
,
11081 /*check_dependency_p=*/true,
11083 /* If we didn't find a template-id, look for an ordinary
11085 if (!template_p
&& !cp_parser_parse_definitely (parser
))
11087 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11088 in effect, then we must assume that, upon instantiation, the
11089 template will correspond to a class. */
11090 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
11091 && tag_type
== typename_type
)
11092 type
= make_typename_type (parser
->scope
, decl
,
11094 /*complain=*/tf_error
);
11096 type
= TREE_TYPE (decl
);
11101 identifier
= cp_parser_identifier (parser
);
11103 if (identifier
== error_mark_node
)
11105 parser
->scope
= NULL_TREE
;
11106 return error_mark_node
;
11109 /* For a `typename', we needn't call xref_tag. */
11110 if (tag_type
== typename_type
11111 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
11112 return cp_parser_make_typename_type (parser
, parser
->scope
,
11114 /* Look up a qualified name in the usual way. */
11118 tree ambiguous_decls
;
11120 decl
= cp_parser_lookup_name (parser
, identifier
,
11122 /*is_template=*/false,
11123 /*is_namespace=*/false,
11124 /*check_dependency=*/true,
11127 /* If the lookup was ambiguous, an error will already have been
11129 if (ambiguous_decls
)
11130 return error_mark_node
;
11132 /* If we are parsing friend declaration, DECL may be a
11133 TEMPLATE_DECL tree node here. However, we need to check
11134 whether this TEMPLATE_DECL results in valid code. Consider
11135 the following example:
11138 template <class T> class C {};
11141 template <class T> friend class N::C; // #1, valid code
11143 template <class T> class Y {
11144 friend class N::C; // #2, invalid code
11147 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11148 name lookup of `N::C'. We see that friend declaration must
11149 be template for the code to be valid. Note that
11150 processing_template_decl does not work here since it is
11151 always 1 for the above two cases. */
11153 decl
= (cp_parser_maybe_treat_template_as_class
11154 (decl
, /*tag_name_p=*/is_friend
11155 && parser
->num_template_parameter_lists
));
11157 if (TREE_CODE (decl
) != TYPE_DECL
)
11159 cp_parser_diagnose_invalid_type_name (parser
,
11162 return error_mark_node
;
11165 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
11167 bool allow_template
= (parser
->num_template_parameter_lists
11168 || DECL_SELF_REFERENCE_P (decl
));
11169 type
= check_elaborated_type_specifier (tag_type
, decl
,
11172 if (type
== error_mark_node
)
11173 return error_mark_node
;
11176 /* Forward declarations of nested types, such as
11181 are invalid unless all components preceding the final '::'
11182 are complete. If all enclosing types are complete, these
11183 declarations become merely pointless.
11185 Invalid forward declarations of nested types are errors
11186 caught elsewhere in parsing. Those that are pointless arrive
11189 if (cp_parser_declares_only_class_p (parser
)
11190 && !is_friend
&& !processing_explicit_instantiation
)
11191 warning (0, "declaration %qD does not declare anything", decl
);
11193 type
= TREE_TYPE (decl
);
11197 /* An elaborated-type-specifier sometimes introduces a new type and
11198 sometimes names an existing type. Normally, the rule is that it
11199 introduces a new type only if there is not an existing type of
11200 the same name already in scope. For example, given:
11203 void f() { struct S s; }
11205 the `struct S' in the body of `f' is the same `struct S' as in
11206 the global scope; the existing definition is used. However, if
11207 there were no global declaration, this would introduce a new
11208 local class named `S'.
11210 An exception to this rule applies to the following code:
11212 namespace N { struct S; }
11214 Here, the elaborated-type-specifier names a new type
11215 unconditionally; even if there is already an `S' in the
11216 containing scope this declaration names a new type.
11217 This exception only applies if the elaborated-type-specifier
11218 forms the complete declaration:
11222 A declaration consisting solely of `class-key identifier ;' is
11223 either a redeclaration of the name in the current scope or a
11224 forward declaration of the identifier as a class name. It
11225 introduces the name into the current scope.
11227 We are in this situation precisely when the next token is a `;'.
11229 An exception to the exception is that a `friend' declaration does
11230 *not* name a new type; i.e., given:
11232 struct S { friend struct T; };
11234 `T' is not a new type in the scope of `S'.
11236 Also, `new struct S' or `sizeof (struct S)' never results in the
11237 definition of a new type; a new type can only be declared in a
11238 declaration context. */
11244 /* Friends have special name lookup rules. */
11245 ts
= ts_within_enclosing_non_class
;
11246 else if (is_declaration
11247 && cp_lexer_next_token_is (parser
->lexer
,
11249 /* This is a `class-key identifier ;' */
11255 (parser
->num_template_parameter_lists
11256 && (cp_parser_next_token_starts_class_definition_p (parser
)
11257 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
11258 /* An unqualified name was used to reference this type, so
11259 there were no qualifying templates. */
11260 if (!cp_parser_check_template_parameters (parser
,
11261 /*num_templates=*/0))
11262 return error_mark_node
;
11263 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
11267 if (type
== error_mark_node
)
11268 return error_mark_node
;
11270 /* Allow attributes on forward declarations of classes. */
11273 if (TREE_CODE (type
) == TYPENAME_TYPE
)
11274 warning (OPT_Wattributes
,
11275 "attributes ignored on uninstantiated type");
11276 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
11277 && ! processing_explicit_instantiation
)
11278 warning (OPT_Wattributes
,
11279 "attributes ignored on template instantiation");
11280 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
11281 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
11283 warning (OPT_Wattributes
,
11284 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11287 if (tag_type
!= enum_type
)
11288 cp_parser_check_class_key (tag_type
, type
);
11290 /* A "<" cannot follow an elaborated type specifier. If that
11291 happens, the user was probably trying to form a template-id. */
11292 cp_parser_check_for_invalid_template_id (parser
, type
);
11297 /* Parse an enum-specifier.
11300 enum identifier [opt] { enumerator-list [opt] }
11303 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11306 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11307 if the token stream isn't an enum-specifier after all. */
11310 cp_parser_enum_specifier (cp_parser
* parser
)
11316 /* Parse tentatively so that we can back up if we don't find a
11318 cp_parser_parse_tentatively (parser
);
11320 /* Caller guarantees that the current token is 'enum', an identifier
11321 possibly follows, and the token after that is an opening brace.
11322 If we don't have an identifier, fabricate an anonymous name for
11323 the enumeration being defined. */
11324 cp_lexer_consume_token (parser
->lexer
);
11326 attributes
= cp_parser_attributes_opt (parser
);
11328 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11329 identifier
= cp_parser_identifier (parser
);
11331 identifier
= make_anon_name ();
11333 /* Look for the `{' but don't consume it yet. */
11334 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11335 cp_parser_simulate_error (parser
);
11337 if (!cp_parser_parse_definitely (parser
))
11340 /* Issue an error message if type-definitions are forbidden here. */
11341 if (!cp_parser_check_type_definition (parser
))
11342 type
= error_mark_node
;
11344 /* Create the new type. We do this before consuming the opening
11345 brace so the enum will be recorded as being on the line of its
11346 tag (or the 'enum' keyword, if there is no tag). */
11347 type
= start_enum (identifier
);
11349 /* Consume the opening brace. */
11350 cp_lexer_consume_token (parser
->lexer
);
11352 if (type
== error_mark_node
)
11354 cp_parser_skip_to_end_of_block_or_statement (parser
);
11355 return error_mark_node
;
11358 /* If the next token is not '}', then there are some enumerators. */
11359 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
11360 cp_parser_enumerator_list (parser
, type
);
11362 /* Consume the final '}'. */
11363 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11365 /* Look for trailing attributes to apply to this enumeration, and
11366 apply them if appropriate. */
11367 if (cp_parser_allow_gnu_extensions_p (parser
))
11369 tree trailing_attr
= cp_parser_attributes_opt (parser
);
11370 cplus_decl_attributes (&type
,
11372 (int) ATTR_FLAG_TYPE_IN_PLACE
);
11375 /* Finish up the enumeration. */
11376 finish_enum (type
);
11381 /* Parse an enumerator-list. The enumerators all have the indicated
11385 enumerator-definition
11386 enumerator-list , enumerator-definition */
11389 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
11393 /* Parse an enumerator-definition. */
11394 cp_parser_enumerator_definition (parser
, type
);
11396 /* If the next token is not a ',', we've reached the end of
11398 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11400 /* Otherwise, consume the `,' and keep going. */
11401 cp_lexer_consume_token (parser
->lexer
);
11402 /* If the next token is a `}', there is a trailing comma. */
11403 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11405 if (pedantic
&& !in_system_header
)
11406 pedwarn ("comma at end of enumerator list");
11412 /* Parse an enumerator-definition. The enumerator has the indicated
11415 enumerator-definition:
11417 enumerator = constant-expression
11423 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
11428 /* Look for the identifier. */
11429 identifier
= cp_parser_identifier (parser
);
11430 if (identifier
== error_mark_node
)
11433 /* If the next token is an '=', then there is an explicit value. */
11434 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11436 /* Consume the `=' token. */
11437 cp_lexer_consume_token (parser
->lexer
);
11438 /* Parse the value. */
11439 value
= cp_parser_constant_expression (parser
,
11440 /*allow_non_constant_p=*/false,
11446 /* Create the enumerator. */
11447 build_enumerator (identifier
, value
, type
);
11450 /* Parse a namespace-name.
11453 original-namespace-name
11456 Returns the NAMESPACE_DECL for the namespace. */
11459 cp_parser_namespace_name (cp_parser
* parser
)
11462 tree namespace_decl
;
11464 /* Get the name of the namespace. */
11465 identifier
= cp_parser_identifier (parser
);
11466 if (identifier
== error_mark_node
)
11467 return error_mark_node
;
11469 /* Look up the identifier in the currently active scope. Look only
11470 for namespaces, due to:
11472 [basic.lookup.udir]
11474 When looking up a namespace-name in a using-directive or alias
11475 definition, only namespace names are considered.
11479 [basic.lookup.qual]
11481 During the lookup of a name preceding the :: scope resolution
11482 operator, object, function, and enumerator names are ignored.
11484 (Note that cp_parser_class_or_namespace_name only calls this
11485 function if the token after the name is the scope resolution
11487 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
11489 /*is_template=*/false,
11490 /*is_namespace=*/true,
11491 /*check_dependency=*/true,
11492 /*ambiguous_decls=*/NULL
);
11493 /* If it's not a namespace, issue an error. */
11494 if (namespace_decl
== error_mark_node
11495 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
11497 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
11498 error ("%qD is not a namespace-name", identifier
);
11499 cp_parser_error (parser
, "expected namespace-name");
11500 namespace_decl
= error_mark_node
;
11503 return namespace_decl
;
11506 /* Parse a namespace-definition.
11508 namespace-definition:
11509 named-namespace-definition
11510 unnamed-namespace-definition
11512 named-namespace-definition:
11513 original-namespace-definition
11514 extension-namespace-definition
11516 original-namespace-definition:
11517 namespace identifier { namespace-body }
11519 extension-namespace-definition:
11520 namespace original-namespace-name { namespace-body }
11522 unnamed-namespace-definition:
11523 namespace { namespace-body } */
11526 cp_parser_namespace_definition (cp_parser
* parser
)
11528 tree identifier
, attribs
;
11529 bool has_visibility
;
11531 /* Look for the `namespace' keyword. */
11532 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
11534 /* Get the name of the namespace. We do not attempt to distinguish
11535 between an original-namespace-definition and an
11536 extension-namespace-definition at this point. The semantic
11537 analysis routines are responsible for that. */
11538 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11539 identifier
= cp_parser_identifier (parser
);
11541 identifier
= NULL_TREE
;
11543 /* Parse any specified attributes. */
11544 attribs
= cp_parser_attributes_opt (parser
);
11546 /* Look for the `{' to start the namespace. */
11547 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
11548 /* Start the namespace. */
11549 push_namespace (identifier
);
11551 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
11553 /* Parse the body of the namespace. */
11554 cp_parser_namespace_body (parser
);
11556 #ifdef HANDLE_PRAGMA_VISIBILITY
11557 if (has_visibility
)
11561 /* Finish the namespace. */
11563 /* Look for the final `}'. */
11564 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11567 /* Parse a namespace-body.
11570 declaration-seq [opt] */
11573 cp_parser_namespace_body (cp_parser
* parser
)
11575 cp_parser_declaration_seq_opt (parser
);
11578 /* Parse a namespace-alias-definition.
11580 namespace-alias-definition:
11581 namespace identifier = qualified-namespace-specifier ; */
11584 cp_parser_namespace_alias_definition (cp_parser
* parser
)
11587 tree namespace_specifier
;
11589 /* Look for the `namespace' keyword. */
11590 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
11591 /* Look for the identifier. */
11592 identifier
= cp_parser_identifier (parser
);
11593 if (identifier
== error_mark_node
)
11595 /* Look for the `=' token. */
11596 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
11597 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11599 error ("%<namespace%> definition is not allowed here");
11600 /* Skip the definition. */
11601 cp_lexer_consume_token (parser
->lexer
);
11602 if (cp_parser_skip_to_closing_brace (parser
))
11603 cp_lexer_consume_token (parser
->lexer
);
11606 cp_parser_require (parser
, CPP_EQ
, "`='");
11607 /* Look for the qualified-namespace-specifier. */
11608 namespace_specifier
11609 = cp_parser_qualified_namespace_specifier (parser
);
11610 /* Look for the `;' token. */
11611 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11613 /* Register the alias in the symbol table. */
11614 do_namespace_alias (identifier
, namespace_specifier
);
11617 /* Parse a qualified-namespace-specifier.
11619 qualified-namespace-specifier:
11620 :: [opt] nested-name-specifier [opt] namespace-name
11622 Returns a NAMESPACE_DECL corresponding to the specified
11626 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
11628 /* Look for the optional `::'. */
11629 cp_parser_global_scope_opt (parser
,
11630 /*current_scope_valid_p=*/false);
11632 /* Look for the optional nested-name-specifier. */
11633 cp_parser_nested_name_specifier_opt (parser
,
11634 /*typename_keyword_p=*/false,
11635 /*check_dependency_p=*/true,
11637 /*is_declaration=*/true);
11639 return cp_parser_namespace_name (parser
);
11642 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11643 access declaration.
11646 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11647 using :: unqualified-id ;
11649 access-declaration:
11655 cp_parser_using_declaration (cp_parser
* parser
,
11656 bool access_declaration_p
)
11659 bool typename_p
= false;
11660 bool global_scope_p
;
11665 if (access_declaration_p
)
11666 cp_parser_parse_tentatively (parser
);
11669 /* Look for the `using' keyword. */
11670 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
11672 /* Peek at the next token. */
11673 token
= cp_lexer_peek_token (parser
->lexer
);
11674 /* See if it's `typename'. */
11675 if (token
->keyword
== RID_TYPENAME
)
11677 /* Remember that we've seen it. */
11679 /* Consume the `typename' token. */
11680 cp_lexer_consume_token (parser
->lexer
);
11684 /* Look for the optional global scope qualification. */
11686 = (cp_parser_global_scope_opt (parser
,
11687 /*current_scope_valid_p=*/false)
11690 /* If we saw `typename', or didn't see `::', then there must be a
11691 nested-name-specifier present. */
11692 if (typename_p
|| !global_scope_p
)
11693 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
11694 /*check_dependency_p=*/true,
11696 /*is_declaration=*/true);
11697 /* Otherwise, we could be in either of the two productions. In that
11698 case, treat the nested-name-specifier as optional. */
11700 qscope
= cp_parser_nested_name_specifier_opt (parser
,
11701 /*typename_keyword_p=*/false,
11702 /*check_dependency_p=*/true,
11704 /*is_declaration=*/true);
11706 qscope
= global_namespace
;
11708 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
11709 /* Something has already gone wrong; there's no need to parse
11710 further. Since an error has occurred, the return value of
11711 cp_parser_parse_definitely will be false, as required. */
11712 return cp_parser_parse_definitely (parser
);
11714 /* Parse the unqualified-id. */
11715 identifier
= cp_parser_unqualified_id (parser
,
11716 /*template_keyword_p=*/false,
11717 /*check_dependency_p=*/true,
11718 /*declarator_p=*/true,
11719 /*optional_p=*/false);
11721 if (access_declaration_p
)
11723 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11724 cp_parser_simulate_error (parser
);
11725 if (!cp_parser_parse_definitely (parser
))
11729 /* The function we call to handle a using-declaration is different
11730 depending on what scope we are in. */
11731 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
11733 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
11734 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
11735 /* [namespace.udecl]
11737 A using declaration shall not name a template-id. */
11738 error ("a template-id may not appear in a using-declaration");
11741 if (at_class_scope_p ())
11743 /* Create the USING_DECL. */
11744 decl
= do_class_using_decl (parser
->scope
, identifier
);
11745 /* Add it to the list of members in this class. */
11746 finish_member_declaration (decl
);
11750 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
11751 if (decl
== error_mark_node
)
11752 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
11753 else if (!at_namespace_scope_p ())
11754 do_local_using_decl (decl
, qscope
, identifier
);
11756 do_toplevel_using_decl (decl
, qscope
, identifier
);
11760 /* Look for the final `;'. */
11761 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11766 /* Parse a using-directive.
11769 using namespace :: [opt] nested-name-specifier [opt]
11770 namespace-name ; */
11773 cp_parser_using_directive (cp_parser
* parser
)
11775 tree namespace_decl
;
11778 /* Look for the `using' keyword. */
11779 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
11780 /* And the `namespace' keyword. */
11781 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
11782 /* Look for the optional `::' operator. */
11783 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
11784 /* And the optional nested-name-specifier. */
11785 cp_parser_nested_name_specifier_opt (parser
,
11786 /*typename_keyword_p=*/false,
11787 /*check_dependency_p=*/true,
11789 /*is_declaration=*/true);
11790 /* Get the namespace being used. */
11791 namespace_decl
= cp_parser_namespace_name (parser
);
11792 /* And any specified attributes. */
11793 attribs
= cp_parser_attributes_opt (parser
);
11794 /* Update the symbol table. */
11795 parse_using_directive (namespace_decl
, attribs
);
11796 /* Look for the final `;'. */
11797 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11800 /* Parse an asm-definition.
11803 asm ( string-literal ) ;
11808 asm volatile [opt] ( string-literal ) ;
11809 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11810 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11811 : asm-operand-list [opt] ) ;
11812 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11813 : asm-operand-list [opt]
11814 : asm-operand-list [opt] ) ; */
11817 cp_parser_asm_definition (cp_parser
* parser
)
11820 tree outputs
= NULL_TREE
;
11821 tree inputs
= NULL_TREE
;
11822 tree clobbers
= NULL_TREE
;
11824 bool volatile_p
= false;
11825 bool extended_p
= false;
11826 bool invalid_inputs_p
= false;
11827 bool invalid_outputs_p
= false;
11829 /* Look for the `asm' keyword. */
11830 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
11831 /* See if the next token is `volatile'. */
11832 if (cp_parser_allow_gnu_extensions_p (parser
)
11833 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
11835 /* Remember that we saw the `volatile' keyword. */
11837 /* Consume the token. */
11838 cp_lexer_consume_token (parser
->lexer
);
11840 /* Look for the opening `('. */
11841 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
11843 /* Look for the string. */
11844 string
= cp_parser_string_literal (parser
, false, false);
11845 if (string
== error_mark_node
)
11847 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11848 /*consume_paren=*/true);
11852 /* If we're allowing GNU extensions, check for the extended assembly
11853 syntax. Unfortunately, the `:' tokens need not be separated by
11854 a space in C, and so, for compatibility, we tolerate that here
11855 too. Doing that means that we have to treat the `::' operator as
11857 if (cp_parser_allow_gnu_extensions_p (parser
)
11858 && parser
->in_function_body
11859 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
11860 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
11862 bool inputs_p
= false;
11863 bool clobbers_p
= false;
11865 /* The extended syntax was used. */
11868 /* Look for outputs. */
11869 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11871 /* Consume the `:'. */
11872 cp_lexer_consume_token (parser
->lexer
);
11873 /* Parse the output-operands. */
11874 if (cp_lexer_next_token_is_not (parser
->lexer
,
11876 && cp_lexer_next_token_is_not (parser
->lexer
,
11878 && cp_lexer_next_token_is_not (parser
->lexer
,
11880 outputs
= cp_parser_asm_operand_list (parser
);
11882 if (outputs
== error_mark_node
)
11883 invalid_outputs_p
= true;
11885 /* If the next token is `::', there are no outputs, and the
11886 next token is the beginning of the inputs. */
11887 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11888 /* The inputs are coming next. */
11891 /* Look for inputs. */
11893 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11895 /* Consume the `:' or `::'. */
11896 cp_lexer_consume_token (parser
->lexer
);
11897 /* Parse the output-operands. */
11898 if (cp_lexer_next_token_is_not (parser
->lexer
,
11900 && cp_lexer_next_token_is_not (parser
->lexer
,
11902 inputs
= cp_parser_asm_operand_list (parser
);
11904 if (inputs
== error_mark_node
)
11905 invalid_inputs_p
= true;
11907 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11908 /* The clobbers are coming next. */
11911 /* Look for clobbers. */
11913 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11915 /* Consume the `:' or `::'. */
11916 cp_lexer_consume_token (parser
->lexer
);
11917 /* Parse the clobbers. */
11918 if (cp_lexer_next_token_is_not (parser
->lexer
,
11920 clobbers
= cp_parser_asm_clobber_list (parser
);
11923 /* Look for the closing `)'. */
11924 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11925 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11926 /*consume_paren=*/true);
11927 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11929 if (!invalid_inputs_p
&& !invalid_outputs_p
)
11931 /* Create the ASM_EXPR. */
11932 if (parser
->in_function_body
)
11934 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
11936 /* If the extended syntax was not used, mark the ASM_EXPR. */
11939 tree temp
= asm_stmt
;
11940 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
11941 temp
= TREE_OPERAND (temp
, 0);
11943 ASM_INPUT_P (temp
) = 1;
11947 cgraph_add_asm_node (string
);
11951 /* Declarators [gram.dcl.decl] */
11953 /* Parse an init-declarator.
11956 declarator initializer [opt]
11961 declarator asm-specification [opt] attributes [opt] initializer [opt]
11963 function-definition:
11964 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11966 decl-specifier-seq [opt] declarator function-try-block
11970 function-definition:
11971 __extension__ function-definition
11973 The DECL_SPECIFIERS apply to this declarator. Returns a
11974 representation of the entity declared. If MEMBER_P is TRUE, then
11975 this declarator appears in a class scope. The new DECL created by
11976 this declarator is returned.
11978 The CHECKS are access checks that should be performed once we know
11979 what entity is being declared (and, therefore, what classes have
11982 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11983 for a function-definition here as well. If the declarator is a
11984 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11985 be TRUE upon return. By that point, the function-definition will
11986 have been completely parsed.
11988 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11992 cp_parser_init_declarator (cp_parser
* parser
,
11993 cp_decl_specifier_seq
*decl_specifiers
,
11994 VEC (deferred_access_check
,gc
)* checks
,
11995 bool function_definition_allowed_p
,
11997 int declares_class_or_enum
,
11998 bool* function_definition_p
)
12001 cp_declarator
*declarator
;
12002 tree prefix_attributes
;
12004 tree asm_specification
;
12006 tree decl
= NULL_TREE
;
12008 bool is_initialized
;
12009 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12010 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12012 enum cpp_ttype initialization_kind
;
12013 bool is_parenthesized_init
= false;
12014 bool is_non_constant_init
;
12015 int ctor_dtor_or_conv_p
;
12017 tree pushed_scope
= NULL
;
12019 /* Gather the attributes that were provided with the
12020 decl-specifiers. */
12021 prefix_attributes
= decl_specifiers
->attributes
;
12023 /* Assume that this is not the declarator for a function
12025 if (function_definition_p
)
12026 *function_definition_p
= false;
12028 /* Defer access checks while parsing the declarator; we cannot know
12029 what names are accessible until we know what is being
12031 resume_deferring_access_checks ();
12033 /* Parse the declarator. */
12035 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12036 &ctor_dtor_or_conv_p
,
12037 /*parenthesized_p=*/NULL
,
12038 /*member_p=*/false);
12039 /* Gather up the deferred checks. */
12040 stop_deferring_access_checks ();
12042 /* If the DECLARATOR was erroneous, there's no need to go
12044 if (declarator
== cp_error_declarator
)
12045 return error_mark_node
;
12047 /* Check that the number of template-parameter-lists is OK. */
12048 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
12049 return error_mark_node
;
12051 if (declares_class_or_enum
& 2)
12052 cp_parser_check_for_definition_in_return_type (declarator
,
12053 decl_specifiers
->type
);
12055 /* Figure out what scope the entity declared by the DECLARATOR is
12056 located in. `grokdeclarator' sometimes changes the scope, so
12057 we compute it now. */
12058 scope
= get_scope_of_declarator (declarator
);
12060 /* If we're allowing GNU extensions, look for an asm-specification
12062 if (cp_parser_allow_gnu_extensions_p (parser
))
12064 /* Look for an asm-specification. */
12065 asm_specification
= cp_parser_asm_specification_opt (parser
);
12066 /* And attributes. */
12067 attributes
= cp_parser_attributes_opt (parser
);
12071 asm_specification
= NULL_TREE
;
12072 attributes
= NULL_TREE
;
12075 /* Peek at the next token. */
12076 token
= cp_lexer_peek_token (parser
->lexer
);
12077 /* Check to see if the token indicates the start of a
12078 function-definition. */
12079 if (cp_parser_token_starts_function_definition_p (token
))
12081 if (!function_definition_allowed_p
)
12083 /* If a function-definition should not appear here, issue an
12085 cp_parser_error (parser
,
12086 "a function-definition is not allowed here");
12087 return error_mark_node
;
12091 /* Neither attributes nor an asm-specification are allowed
12092 on a function-definition. */
12093 if (asm_specification
)
12094 error ("an asm-specification is not allowed on a function-definition");
12096 error ("attributes are not allowed on a function-definition");
12097 /* This is a function-definition. */
12098 *function_definition_p
= true;
12100 /* Parse the function definition. */
12102 decl
= cp_parser_save_member_function_body (parser
,
12105 prefix_attributes
);
12108 = (cp_parser_function_definition_from_specifiers_and_declarator
12109 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
12117 Only in function declarations for constructors, destructors, and
12118 type conversions can the decl-specifier-seq be omitted.
12120 We explicitly postpone this check past the point where we handle
12121 function-definitions because we tolerate function-definitions
12122 that are missing their return types in some modes. */
12123 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
12125 cp_parser_error (parser
,
12126 "expected constructor, destructor, or type conversion");
12127 return error_mark_node
;
12130 /* An `=' or an `(' indicates an initializer. */
12131 if (token
->type
== CPP_EQ
12132 || token
->type
== CPP_OPEN_PAREN
)
12134 is_initialized
= true;
12135 initialization_kind
= token
->type
;
12139 /* If the init-declarator isn't initialized and isn't followed by a
12140 `,' or `;', it's not a valid init-declarator. */
12141 if (token
->type
!= CPP_COMMA
12142 && token
->type
!= CPP_SEMICOLON
)
12144 cp_parser_error (parser
, "expected initializer");
12145 return error_mark_node
;
12147 is_initialized
= false;
12148 initialization_kind
= CPP_EOF
;
12151 /* Because start_decl has side-effects, we should only call it if we
12152 know we're going ahead. By this point, we know that we cannot
12153 possibly be looking at any other construct. */
12154 cp_parser_commit_to_tentative_parse (parser
);
12156 /* If the decl specifiers were bad, issue an error now that we're
12157 sure this was intended to be a declarator. Then continue
12158 declaring the variable(s), as int, to try to cut down on further
12160 if (decl_specifiers
->any_specifiers_p
12161 && decl_specifiers
->type
== error_mark_node
)
12163 cp_parser_error (parser
, "invalid type in declaration");
12164 decl_specifiers
->type
= integer_type_node
;
12167 /* Check to see whether or not this declaration is a friend. */
12168 friend_p
= cp_parser_friend_p (decl_specifiers
);
12170 /* Enter the newly declared entry in the symbol table. If we're
12171 processing a declaration in a class-specifier, we wait until
12172 after processing the initializer. */
12175 if (parser
->in_unbraced_linkage_specification_p
)
12176 decl_specifiers
->storage_class
= sc_extern
;
12177 decl
= start_decl (declarator
, decl_specifiers
,
12178 is_initialized
, attributes
, prefix_attributes
,
12182 /* Enter the SCOPE. That way unqualified names appearing in the
12183 initializer will be looked up in SCOPE. */
12184 pushed_scope
= push_scope (scope
);
12186 /* Perform deferred access control checks, now that we know in which
12187 SCOPE the declared entity resides. */
12188 if (!member_p
&& decl
)
12190 tree saved_current_function_decl
= NULL_TREE
;
12192 /* If the entity being declared is a function, pretend that we
12193 are in its scope. If it is a `friend', it may have access to
12194 things that would not otherwise be accessible. */
12195 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12197 saved_current_function_decl
= current_function_decl
;
12198 current_function_decl
= decl
;
12201 /* Perform access checks for template parameters. */
12202 cp_parser_perform_template_parameter_access_checks (checks
);
12204 /* Perform the access control checks for the declarator and the
12205 the decl-specifiers. */
12206 perform_deferred_access_checks ();
12208 /* Restore the saved value. */
12209 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12210 current_function_decl
= saved_current_function_decl
;
12213 /* Parse the initializer. */
12214 initializer
= NULL_TREE
;
12215 is_parenthesized_init
= false;
12216 is_non_constant_init
= true;
12217 if (is_initialized
)
12219 if (function_declarator_p (declarator
))
12221 if (initialization_kind
== CPP_EQ
)
12222 initializer
= cp_parser_pure_specifier (parser
);
12225 /* If the declaration was erroneous, we don't really
12226 know what the user intended, so just silently
12227 consume the initializer. */
12228 if (decl
!= error_mark_node
)
12229 error ("initializer provided for function");
12230 cp_parser_skip_to_closing_parenthesis (parser
,
12231 /*recovering=*/true,
12232 /*or_comma=*/false,
12233 /*consume_paren=*/true);
12237 initializer
= cp_parser_initializer (parser
,
12238 &is_parenthesized_init
,
12239 &is_non_constant_init
);
12242 /* The old parser allows attributes to appear after a parenthesized
12243 initializer. Mark Mitchell proposed removing this functionality
12244 on the GCC mailing lists on 2002-08-13. This parser accepts the
12245 attributes -- but ignores them. */
12246 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
12247 if (cp_parser_attributes_opt (parser
))
12248 warning (OPT_Wattributes
,
12249 "attributes after parenthesized initializer ignored");
12251 /* For an in-class declaration, use `grokfield' to create the
12257 pop_scope (pushed_scope
);
12258 pushed_scope
= false;
12260 decl
= grokfield (declarator
, decl_specifiers
,
12261 initializer
, !is_non_constant_init
,
12262 /*asmspec=*/NULL_TREE
,
12263 prefix_attributes
);
12264 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
12265 cp_parser_save_default_args (parser
, decl
);
12268 /* Finish processing the declaration. But, skip friend
12270 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
12272 cp_finish_decl (decl
,
12273 initializer
, !is_non_constant_init
,
12275 /* If the initializer is in parentheses, then this is
12276 a direct-initialization, which means that an
12277 `explicit' constructor is OK. Otherwise, an
12278 `explicit' constructor cannot be used. */
12279 ((is_parenthesized_init
|| !is_initialized
)
12280 ? 0 : LOOKUP_ONLYCONVERTING
));
12282 else if ((cxx_dialect
!= cxx98
) && friend_p
12283 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
12284 /* Core issue #226 (C++0x only): A default template-argument
12285 shall not be specified in a friend class template
12287 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/1,
12288 /*is_partial=*/0, /*is_friend_decl=*/1);
12290 if (!friend_p
&& pushed_scope
)
12291 pop_scope (pushed_scope
);
12296 /* Parse a declarator.
12300 ptr-operator declarator
12302 abstract-declarator:
12303 ptr-operator abstract-declarator [opt]
12304 direct-abstract-declarator
12309 attributes [opt] direct-declarator
12310 attributes [opt] ptr-operator declarator
12312 abstract-declarator:
12313 attributes [opt] ptr-operator abstract-declarator [opt]
12314 attributes [opt] direct-abstract-declarator
12316 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12317 detect constructor, destructor or conversion operators. It is set
12318 to -1 if the declarator is a name, and +1 if it is a
12319 function. Otherwise it is set to zero. Usually you just want to
12320 test for >0, but internally the negative value is used.
12322 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12323 a decl-specifier-seq unless it declares a constructor, destructor,
12324 or conversion. It might seem that we could check this condition in
12325 semantic analysis, rather than parsing, but that makes it difficult
12326 to handle something like `f()'. We want to notice that there are
12327 no decl-specifiers, and therefore realize that this is an
12328 expression, not a declaration.)
12330 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12331 the declarator is a direct-declarator of the form "(...)".
12333 MEMBER_P is true iff this declarator is a member-declarator. */
12335 static cp_declarator
*
12336 cp_parser_declarator (cp_parser
* parser
,
12337 cp_parser_declarator_kind dcl_kind
,
12338 int* ctor_dtor_or_conv_p
,
12339 bool* parenthesized_p
,
12343 cp_declarator
*declarator
;
12344 enum tree_code code
;
12345 cp_cv_quals cv_quals
;
12347 tree attributes
= NULL_TREE
;
12349 /* Assume this is not a constructor, destructor, or type-conversion
12351 if (ctor_dtor_or_conv_p
)
12352 *ctor_dtor_or_conv_p
= 0;
12354 if (cp_parser_allow_gnu_extensions_p (parser
))
12355 attributes
= cp_parser_attributes_opt (parser
);
12357 /* Peek at the next token. */
12358 token
= cp_lexer_peek_token (parser
->lexer
);
12360 /* Check for the ptr-operator production. */
12361 cp_parser_parse_tentatively (parser
);
12362 /* Parse the ptr-operator. */
12363 code
= cp_parser_ptr_operator (parser
,
12366 /* If that worked, then we have a ptr-operator. */
12367 if (cp_parser_parse_definitely (parser
))
12369 /* If a ptr-operator was found, then this declarator was not
12371 if (parenthesized_p
)
12372 *parenthesized_p
= true;
12373 /* The dependent declarator is optional if we are parsing an
12374 abstract-declarator. */
12375 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
12376 cp_parser_parse_tentatively (parser
);
12378 /* Parse the dependent declarator. */
12379 declarator
= cp_parser_declarator (parser
, dcl_kind
,
12380 /*ctor_dtor_or_conv_p=*/NULL
,
12381 /*parenthesized_p=*/NULL
,
12382 /*member_p=*/false);
12384 /* If we are parsing an abstract-declarator, we must handle the
12385 case where the dependent declarator is absent. */
12386 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
12387 && !cp_parser_parse_definitely (parser
))
12390 declarator
= cp_parser_make_indirect_declarator
12391 (code
, class_type
, cv_quals
, declarator
);
12393 /* Everything else is a direct-declarator. */
12396 if (parenthesized_p
)
12397 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
12399 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
12400 ctor_dtor_or_conv_p
,
12404 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
12405 declarator
->attributes
= attributes
;
12410 /* Parse a direct-declarator or direct-abstract-declarator.
12414 direct-declarator ( parameter-declaration-clause )
12415 cv-qualifier-seq [opt]
12416 exception-specification [opt]
12417 direct-declarator [ constant-expression [opt] ]
12420 direct-abstract-declarator:
12421 direct-abstract-declarator [opt]
12422 ( parameter-declaration-clause )
12423 cv-qualifier-seq [opt]
12424 exception-specification [opt]
12425 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12426 ( abstract-declarator )
12428 Returns a representation of the declarator. DCL_KIND is
12429 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12430 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12431 we are parsing a direct-declarator. It is
12432 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12433 of ambiguity we prefer an abstract declarator, as per
12434 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12435 cp_parser_declarator. */
12437 static cp_declarator
*
12438 cp_parser_direct_declarator (cp_parser
* parser
,
12439 cp_parser_declarator_kind dcl_kind
,
12440 int* ctor_dtor_or_conv_p
,
12444 cp_declarator
*declarator
= NULL
;
12445 tree scope
= NULL_TREE
;
12446 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
12447 bool saved_in_declarator_p
= parser
->in_declarator_p
;
12449 tree pushed_scope
= NULL_TREE
;
12453 /* Peek at the next token. */
12454 token
= cp_lexer_peek_token (parser
->lexer
);
12455 if (token
->type
== CPP_OPEN_PAREN
)
12457 /* This is either a parameter-declaration-clause, or a
12458 parenthesized declarator. When we know we are parsing a
12459 named declarator, it must be a parenthesized declarator
12460 if FIRST is true. For instance, `(int)' is a
12461 parameter-declaration-clause, with an omitted
12462 direct-abstract-declarator. But `((*))', is a
12463 parenthesized abstract declarator. Finally, when T is a
12464 template parameter `(T)' is a
12465 parameter-declaration-clause, and not a parenthesized
12468 We first try and parse a parameter-declaration-clause,
12469 and then try a nested declarator (if FIRST is true).
12471 It is not an error for it not to be a
12472 parameter-declaration-clause, even when FIRST is
12478 The first is the declaration of a function while the
12479 second is a the definition of a variable, including its
12482 Having seen only the parenthesis, we cannot know which of
12483 these two alternatives should be selected. Even more
12484 complex are examples like:
12489 The former is a function-declaration; the latter is a
12490 variable initialization.
12492 Thus again, we try a parameter-declaration-clause, and if
12493 that fails, we back out and return. */
12495 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
12497 cp_parameter_declarator
*params
;
12498 unsigned saved_num_template_parameter_lists
;
12500 /* In a member-declarator, the only valid interpretation
12501 of a parenthesis is the start of a
12502 parameter-declaration-clause. (It is invalid to
12503 initialize a static data member with a parenthesized
12504 initializer; only the "=" form of initialization is
12507 cp_parser_parse_tentatively (parser
);
12509 /* Consume the `('. */
12510 cp_lexer_consume_token (parser
->lexer
);
12513 /* If this is going to be an abstract declarator, we're
12514 in a declarator and we can't have default args. */
12515 parser
->default_arg_ok_p
= false;
12516 parser
->in_declarator_p
= true;
12519 /* Inside the function parameter list, surrounding
12520 template-parameter-lists do not apply. */
12521 saved_num_template_parameter_lists
12522 = parser
->num_template_parameter_lists
;
12523 parser
->num_template_parameter_lists
= 0;
12525 /* Parse the parameter-declaration-clause. */
12526 params
= cp_parser_parameter_declaration_clause (parser
);
12528 parser
->num_template_parameter_lists
12529 = saved_num_template_parameter_lists
;
12531 /* If all went well, parse the cv-qualifier-seq and the
12532 exception-specification. */
12533 if (member_p
|| cp_parser_parse_definitely (parser
))
12535 cp_cv_quals cv_quals
;
12536 tree exception_specification
;
12538 if (ctor_dtor_or_conv_p
)
12539 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
12541 /* Consume the `)'. */
12542 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12544 /* Parse the cv-qualifier-seq. */
12545 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
12546 /* And the exception-specification. */
12547 exception_specification
12548 = cp_parser_exception_specification_opt (parser
);
12550 /* Create the function-declarator. */
12551 declarator
= make_call_declarator (declarator
,
12554 exception_specification
);
12555 /* Any subsequent parameter lists are to do with
12556 return type, so are not those of the declared
12558 parser
->default_arg_ok_p
= false;
12560 /* Repeat the main loop. */
12565 /* If this is the first, we can try a parenthesized
12569 bool saved_in_type_id_in_expr_p
;
12571 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12572 parser
->in_declarator_p
= saved_in_declarator_p
;
12574 /* Consume the `('. */
12575 cp_lexer_consume_token (parser
->lexer
);
12576 /* Parse the nested declarator. */
12577 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
12578 parser
->in_type_id_in_expr_p
= true;
12580 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
12581 /*parenthesized_p=*/NULL
,
12583 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
12585 /* Expect a `)'. */
12586 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
12587 declarator
= cp_error_declarator
;
12588 if (declarator
== cp_error_declarator
)
12591 goto handle_declarator
;
12593 /* Otherwise, we must be done. */
12597 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
12598 && token
->type
== CPP_OPEN_SQUARE
)
12600 /* Parse an array-declarator. */
12603 if (ctor_dtor_or_conv_p
)
12604 *ctor_dtor_or_conv_p
= 0;
12607 parser
->default_arg_ok_p
= false;
12608 parser
->in_declarator_p
= true;
12609 /* Consume the `['. */
12610 cp_lexer_consume_token (parser
->lexer
);
12611 /* Peek at the next token. */
12612 token
= cp_lexer_peek_token (parser
->lexer
);
12613 /* If the next token is `]', then there is no
12614 constant-expression. */
12615 if (token
->type
!= CPP_CLOSE_SQUARE
)
12617 bool non_constant_p
;
12620 = cp_parser_constant_expression (parser
,
12621 /*allow_non_constant=*/true,
12623 if (!non_constant_p
)
12624 bounds
= fold_non_dependent_expr (bounds
);
12625 /* Normally, the array bound must be an integral constant
12626 expression. However, as an extension, we allow VLAs
12627 in function scopes. */
12628 else if (!parser
->in_function_body
)
12630 error ("array bound is not an integer constant");
12631 bounds
= error_mark_node
;
12635 bounds
= NULL_TREE
;
12636 /* Look for the closing `]'. */
12637 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
12639 declarator
= cp_error_declarator
;
12643 declarator
= make_array_declarator (declarator
, bounds
);
12645 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
12647 tree qualifying_scope
;
12648 tree unqualified_name
;
12649 special_function_kind sfk
;
12651 bool pack_expansion_p
= false;
12653 /* Parse a declarator-id */
12654 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
12657 cp_parser_parse_tentatively (parser
);
12659 /* If we see an ellipsis, we should be looking at a
12661 if (token
->type
== CPP_ELLIPSIS
)
12663 /* Consume the `...' */
12664 cp_lexer_consume_token (parser
->lexer
);
12666 pack_expansion_p
= true;
12671 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
12672 qualifying_scope
= parser
->scope
;
12677 if (!unqualified_name
&& pack_expansion_p
)
12679 /* Check whether an error occurred. */
12680 okay
= !cp_parser_error_occurred (parser
);
12682 /* We already consumed the ellipsis to mark a
12683 parameter pack, but we have no way to report it,
12684 so abort the tentative parse. We will be exiting
12685 immediately anyway. */
12686 cp_parser_abort_tentative_parse (parser
);
12689 okay
= cp_parser_parse_definitely (parser
);
12692 unqualified_name
= error_mark_node
;
12693 else if (unqualified_name
12694 && (qualifying_scope
12695 || (TREE_CODE (unqualified_name
)
12696 != IDENTIFIER_NODE
)))
12698 cp_parser_error (parser
, "expected unqualified-id");
12699 unqualified_name
= error_mark_node
;
12703 if (!unqualified_name
)
12705 if (unqualified_name
== error_mark_node
)
12707 declarator
= cp_error_declarator
;
12708 pack_expansion_p
= false;
12709 declarator
->parameter_pack_p
= false;
12713 if (qualifying_scope
&& at_namespace_scope_p ()
12714 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
12716 /* In the declaration of a member of a template class
12717 outside of the class itself, the SCOPE will sometimes
12718 be a TYPENAME_TYPE. For example, given:
12720 template <typename T>
12721 int S<T>::R::i = 3;
12723 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12724 this context, we must resolve S<T>::R to an ordinary
12725 type, rather than a typename type.
12727 The reason we normally avoid resolving TYPENAME_TYPEs
12728 is that a specialization of `S' might render
12729 `S<T>::R' not a type. However, if `S' is
12730 specialized, then this `i' will not be used, so there
12731 is no harm in resolving the types here. */
12734 /* Resolve the TYPENAME_TYPE. */
12735 type
= resolve_typename_type (qualifying_scope
,
12736 /*only_current_p=*/false);
12737 /* If that failed, the declarator is invalid. */
12738 if (TREE_CODE (type
) == TYPENAME_TYPE
)
12739 error ("%<%T::%E%> is not a type",
12740 TYPE_CONTEXT (qualifying_scope
),
12741 TYPE_IDENTIFIER (qualifying_scope
));
12742 qualifying_scope
= type
;
12747 if (unqualified_name
)
12751 if (qualifying_scope
12752 && CLASS_TYPE_P (qualifying_scope
))
12753 class_type
= qualifying_scope
;
12755 class_type
= current_class_type
;
12757 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
12759 tree name_type
= TREE_TYPE (unqualified_name
);
12760 if (class_type
&& same_type_p (name_type
, class_type
))
12762 if (qualifying_scope
12763 && CLASSTYPE_USE_TEMPLATE (name_type
))
12765 error ("invalid use of constructor as a template");
12766 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12767 "name the constructor in a qualified name",
12769 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
12770 class_type
, name_type
);
12771 declarator
= cp_error_declarator
;
12775 unqualified_name
= constructor_name (class_type
);
12779 /* We do not attempt to print the declarator
12780 here because we do not have enough
12781 information about its original syntactic
12783 cp_parser_error (parser
, "invalid declarator");
12784 declarator
= cp_error_declarator
;
12791 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
12792 sfk
= sfk_destructor
;
12793 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
12794 sfk
= sfk_conversion
;
12795 else if (/* There's no way to declare a constructor
12796 for an anonymous type, even if the type
12797 got a name for linkage purposes. */
12798 !TYPE_WAS_ANONYMOUS (class_type
)
12799 && constructor_name_p (unqualified_name
,
12802 unqualified_name
= constructor_name (class_type
);
12803 sfk
= sfk_constructor
;
12806 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
12807 *ctor_dtor_or_conv_p
= -1;
12810 declarator
= make_id_declarator (qualifying_scope
,
12813 declarator
->id_loc
= token
->location
;
12814 declarator
->parameter_pack_p
= pack_expansion_p
;
12816 if (pack_expansion_p
)
12817 maybe_warn_variadic_templates ();
12819 handle_declarator
:;
12820 scope
= get_scope_of_declarator (declarator
);
12822 /* Any names that appear after the declarator-id for a
12823 member are looked up in the containing scope. */
12824 pushed_scope
= push_scope (scope
);
12825 parser
->in_declarator_p
= true;
12826 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
12827 || (declarator
&& declarator
->kind
== cdk_id
))
12828 /* Default args are only allowed on function
12830 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12832 parser
->default_arg_ok_p
= false;
12841 /* For an abstract declarator, we might wind up with nothing at this
12842 point. That's an error; the declarator is not optional. */
12844 cp_parser_error (parser
, "expected declarator");
12846 /* If we entered a scope, we must exit it now. */
12848 pop_scope (pushed_scope
);
12850 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12851 parser
->in_declarator_p
= saved_in_declarator_p
;
12856 /* Parse a ptr-operator.
12859 * cv-qualifier-seq [opt]
12861 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12866 & cv-qualifier-seq [opt]
12868 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12869 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12870 an rvalue reference. In the case of a pointer-to-member, *TYPE is
12871 filled in with the TYPE containing the member. *CV_QUALS is
12872 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12873 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
12874 Note that the tree codes returned by this function have nothing
12875 to do with the types of trees that will be eventually be created
12876 to represent the pointer or reference type being parsed. They are
12877 just constants with suggestive names. */
12878 static enum tree_code
12879 cp_parser_ptr_operator (cp_parser
* parser
,
12881 cp_cv_quals
*cv_quals
)
12883 enum tree_code code
= ERROR_MARK
;
12886 /* Assume that it's not a pointer-to-member. */
12888 /* And that there are no cv-qualifiers. */
12889 *cv_quals
= TYPE_UNQUALIFIED
;
12891 /* Peek at the next token. */
12892 token
= cp_lexer_peek_token (parser
->lexer
);
12894 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
12895 if (token
->type
== CPP_MULT
)
12896 code
= INDIRECT_REF
;
12897 else if (token
->type
== CPP_AND
)
12899 else if ((cxx_dialect
!= cxx98
) &&
12900 token
->type
== CPP_AND_AND
) /* C++0x only */
12901 code
= NON_LVALUE_EXPR
;
12903 if (code
!= ERROR_MARK
)
12905 /* Consume the `*', `&' or `&&'. */
12906 cp_lexer_consume_token (parser
->lexer
);
12908 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12909 `&', if we are allowing GNU extensions. (The only qualifier
12910 that can legally appear after `&' is `restrict', but that is
12911 enforced during semantic analysis. */
12912 if (code
== INDIRECT_REF
12913 || cp_parser_allow_gnu_extensions_p (parser
))
12914 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
12918 /* Try the pointer-to-member case. */
12919 cp_parser_parse_tentatively (parser
);
12920 /* Look for the optional `::' operator. */
12921 cp_parser_global_scope_opt (parser
,
12922 /*current_scope_valid_p=*/false);
12923 /* Look for the nested-name specifier. */
12924 cp_parser_nested_name_specifier (parser
,
12925 /*typename_keyword_p=*/false,
12926 /*check_dependency_p=*/true,
12928 /*is_declaration=*/false);
12929 /* If we found it, and the next token is a `*', then we are
12930 indeed looking at a pointer-to-member operator. */
12931 if (!cp_parser_error_occurred (parser
)
12932 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
12934 /* Indicate that the `*' operator was used. */
12935 code
= INDIRECT_REF
;
12937 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
12938 error ("%qD is a namespace", parser
->scope
);
12941 /* The type of which the member is a member is given by the
12943 *type
= parser
->scope
;
12944 /* The next name will not be qualified. */
12945 parser
->scope
= NULL_TREE
;
12946 parser
->qualifying_scope
= NULL_TREE
;
12947 parser
->object_scope
= NULL_TREE
;
12948 /* Look for the optional cv-qualifier-seq. */
12949 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
12952 /* If that didn't work we don't have a ptr-operator. */
12953 if (!cp_parser_parse_definitely (parser
))
12954 cp_parser_error (parser
, "expected ptr-operator");
12960 /* Parse an (optional) cv-qualifier-seq.
12963 cv-qualifier cv-qualifier-seq [opt]
12974 Returns a bitmask representing the cv-qualifiers. */
12977 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
12979 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
12984 cp_cv_quals cv_qualifier
;
12986 /* Peek at the next token. */
12987 token
= cp_lexer_peek_token (parser
->lexer
);
12988 /* See if it's a cv-qualifier. */
12989 switch (token
->keyword
)
12992 cv_qualifier
= TYPE_QUAL_CONST
;
12996 cv_qualifier
= TYPE_QUAL_VOLATILE
;
13000 cv_qualifier
= TYPE_QUAL_RESTRICT
;
13004 cv_qualifier
= TYPE_UNQUALIFIED
;
13011 if (cv_quals
& cv_qualifier
)
13013 error ("duplicate cv-qualifier");
13014 cp_lexer_purge_token (parser
->lexer
);
13018 cp_lexer_consume_token (parser
->lexer
);
13019 cv_quals
|= cv_qualifier
;
13026 /* Parse a declarator-id.
13030 :: [opt] nested-name-specifier [opt] type-name
13032 In the `id-expression' case, the value returned is as for
13033 cp_parser_id_expression if the id-expression was an unqualified-id.
13034 If the id-expression was a qualified-id, then a SCOPE_REF is
13035 returned. The first operand is the scope (either a NAMESPACE_DECL
13036 or TREE_TYPE), but the second is still just a representation of an
13040 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
13043 /* The expression must be an id-expression. Assume that qualified
13044 names are the names of types so that:
13047 int S<T>::R::i = 3;
13049 will work; we must treat `S<T>::R' as the name of a type.
13050 Similarly, assume that qualified names are templates, where
13054 int S<T>::R<T>::i = 3;
13057 id
= cp_parser_id_expression (parser
,
13058 /*template_keyword_p=*/false,
13059 /*check_dependency_p=*/false,
13060 /*template_p=*/NULL
,
13061 /*declarator_p=*/true,
13063 if (id
&& BASELINK_P (id
))
13064 id
= BASELINK_FUNCTIONS (id
);
13068 /* Parse a type-id.
13071 type-specifier-seq abstract-declarator [opt]
13073 Returns the TYPE specified. */
13076 cp_parser_type_id (cp_parser
* parser
)
13078 cp_decl_specifier_seq type_specifier_seq
;
13079 cp_declarator
*abstract_declarator
;
13081 /* Parse the type-specifier-seq. */
13082 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
13083 &type_specifier_seq
);
13084 if (type_specifier_seq
.type
== error_mark_node
)
13085 return error_mark_node
;
13087 /* There might or might not be an abstract declarator. */
13088 cp_parser_parse_tentatively (parser
);
13089 /* Look for the declarator. */
13090 abstract_declarator
13091 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
13092 /*parenthesized_p=*/NULL
,
13093 /*member_p=*/false);
13094 /* Check to see if there really was a declarator. */
13095 if (!cp_parser_parse_definitely (parser
))
13096 abstract_declarator
= NULL
;
13098 return groktypename (&type_specifier_seq
, abstract_declarator
);
13101 /* Parse a type-specifier-seq.
13103 type-specifier-seq:
13104 type-specifier type-specifier-seq [opt]
13108 type-specifier-seq:
13109 attributes type-specifier-seq [opt]
13111 If IS_CONDITION is true, we are at the start of a "condition",
13112 e.g., we've just seen "if (".
13114 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13117 cp_parser_type_specifier_seq (cp_parser
* parser
,
13119 cp_decl_specifier_seq
*type_specifier_seq
)
13121 bool seen_type_specifier
= false;
13122 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
13124 /* Clear the TYPE_SPECIFIER_SEQ. */
13125 clear_decl_specs (type_specifier_seq
);
13127 /* Parse the type-specifiers and attributes. */
13130 tree type_specifier
;
13131 bool is_cv_qualifier
;
13133 /* Check for attributes first. */
13134 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
13136 type_specifier_seq
->attributes
=
13137 chainon (type_specifier_seq
->attributes
,
13138 cp_parser_attributes_opt (parser
));
13142 /* Look for the type-specifier. */
13143 type_specifier
= cp_parser_type_specifier (parser
,
13145 type_specifier_seq
,
13146 /*is_declaration=*/false,
13149 if (!type_specifier
)
13151 /* If the first type-specifier could not be found, this is not a
13152 type-specifier-seq at all. */
13153 if (!seen_type_specifier
)
13155 cp_parser_error (parser
, "expected type-specifier");
13156 type_specifier_seq
->type
= error_mark_node
;
13159 /* If subsequent type-specifiers could not be found, the
13160 type-specifier-seq is complete. */
13164 seen_type_specifier
= true;
13165 /* The standard says that a condition can be:
13167 type-specifier-seq declarator = assignment-expression
13174 we should treat the "S" as a declarator, not as a
13175 type-specifier. The standard doesn't say that explicitly for
13176 type-specifier-seq, but it does say that for
13177 decl-specifier-seq in an ordinary declaration. Perhaps it
13178 would be clearer just to allow a decl-specifier-seq here, and
13179 then add a semantic restriction that if any decl-specifiers
13180 that are not type-specifiers appear, the program is invalid. */
13181 if (is_condition
&& !is_cv_qualifier
)
13182 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13185 cp_parser_check_decl_spec (type_specifier_seq
);
13188 /* Parse a parameter-declaration-clause.
13190 parameter-declaration-clause:
13191 parameter-declaration-list [opt] ... [opt]
13192 parameter-declaration-list , ...
13194 Returns a representation for the parameter declarations. A return
13195 value of NULL indicates a parameter-declaration-clause consisting
13196 only of an ellipsis. */
13198 static cp_parameter_declarator
*
13199 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
13201 cp_parameter_declarator
*parameters
;
13206 /* Peek at the next token. */
13207 token
= cp_lexer_peek_token (parser
->lexer
);
13208 /* Check for trivial parameter-declaration-clauses. */
13209 if (token
->type
== CPP_ELLIPSIS
)
13211 /* Consume the `...' token. */
13212 cp_lexer_consume_token (parser
->lexer
);
13215 else if (token
->type
== CPP_CLOSE_PAREN
)
13216 /* There are no parameters. */
13218 #ifndef NO_IMPLICIT_EXTERN_C
13219 if (in_system_header
&& current_class_type
== NULL
13220 && current_lang_name
== lang_name_c
)
13224 return no_parameters
;
13226 /* Check for `(void)', too, which is a special case. */
13227 else if (token
->keyword
== RID_VOID
13228 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
13229 == CPP_CLOSE_PAREN
))
13231 /* Consume the `void' token. */
13232 cp_lexer_consume_token (parser
->lexer
);
13233 /* There are no parameters. */
13234 return no_parameters
;
13237 /* Parse the parameter-declaration-list. */
13238 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
13239 /* If a parse error occurred while parsing the
13240 parameter-declaration-list, then the entire
13241 parameter-declaration-clause is erroneous. */
13245 /* Peek at the next token. */
13246 token
= cp_lexer_peek_token (parser
->lexer
);
13247 /* If it's a `,', the clause should terminate with an ellipsis. */
13248 if (token
->type
== CPP_COMMA
)
13250 /* Consume the `,'. */
13251 cp_lexer_consume_token (parser
->lexer
);
13252 /* Expect an ellipsis. */
13254 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
13256 /* It might also be `...' if the optional trailing `,' was
13258 else if (token
->type
== CPP_ELLIPSIS
)
13260 /* Consume the `...' token. */
13261 cp_lexer_consume_token (parser
->lexer
);
13262 /* And remember that we saw it. */
13266 ellipsis_p
= false;
13268 /* Finish the parameter list. */
13269 if (parameters
&& ellipsis_p
)
13270 parameters
->ellipsis_p
= true;
13275 /* Parse a parameter-declaration-list.
13277 parameter-declaration-list:
13278 parameter-declaration
13279 parameter-declaration-list , parameter-declaration
13281 Returns a representation of the parameter-declaration-list, as for
13282 cp_parser_parameter_declaration_clause. However, the
13283 `void_list_node' is never appended to the list. Upon return,
13284 *IS_ERROR will be true iff an error occurred. */
13286 static cp_parameter_declarator
*
13287 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
13289 cp_parameter_declarator
*parameters
= NULL
;
13290 cp_parameter_declarator
**tail
= ¶meters
;
13291 bool saved_in_unbraced_linkage_specification_p
;
13293 /* Assume all will go well. */
13295 /* The special considerations that apply to a function within an
13296 unbraced linkage specifications do not apply to the parameters
13297 to the function. */
13298 saved_in_unbraced_linkage_specification_p
13299 = parser
->in_unbraced_linkage_specification_p
;
13300 parser
->in_unbraced_linkage_specification_p
= false;
13302 /* Look for more parameters. */
13305 cp_parameter_declarator
*parameter
;
13306 bool parenthesized_p
;
13307 /* Parse the parameter. */
13309 = cp_parser_parameter_declaration (parser
,
13310 /*template_parm_p=*/false,
13313 /* If a parse error occurred parsing the parameter declaration,
13314 then the entire parameter-declaration-list is erroneous. */
13321 /* Add the new parameter to the list. */
13323 tail
= ¶meter
->next
;
13325 /* Peek at the next token. */
13326 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
13327 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
13328 /* These are for Objective-C++ */
13329 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
13330 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13331 /* The parameter-declaration-list is complete. */
13333 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13337 /* Peek at the next token. */
13338 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13339 /* If it's an ellipsis, then the list is complete. */
13340 if (token
->type
== CPP_ELLIPSIS
)
13342 /* Otherwise, there must be more parameters. Consume the
13344 cp_lexer_consume_token (parser
->lexer
);
13345 /* When parsing something like:
13347 int i(float f, double d)
13349 we can tell after seeing the declaration for "f" that we
13350 are not looking at an initialization of a variable "i",
13351 but rather at the declaration of a function "i".
13353 Due to the fact that the parsing of template arguments
13354 (as specified to a template-id) requires backtracking we
13355 cannot use this technique when inside a template argument
13357 if (!parser
->in_template_argument_list_p
13358 && !parser
->in_type_id_in_expr_p
13359 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
13360 /* However, a parameter-declaration of the form
13361 "foat(f)" (which is a valid declaration of a
13362 parameter "f") can also be interpreted as an
13363 expression (the conversion of "f" to "float"). */
13364 && !parenthesized_p
)
13365 cp_parser_commit_to_tentative_parse (parser
);
13369 cp_parser_error (parser
, "expected %<,%> or %<...%>");
13370 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
13371 cp_parser_skip_to_closing_parenthesis (parser
,
13372 /*recovering=*/true,
13373 /*or_comma=*/false,
13374 /*consume_paren=*/false);
13379 parser
->in_unbraced_linkage_specification_p
13380 = saved_in_unbraced_linkage_specification_p
;
13385 /* Parse a parameter declaration.
13387 parameter-declaration:
13388 decl-specifier-seq ... [opt] declarator
13389 decl-specifier-seq declarator = assignment-expression
13390 decl-specifier-seq ... [opt] abstract-declarator [opt]
13391 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13393 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13394 declares a template parameter. (In that case, a non-nested `>'
13395 token encountered during the parsing of the assignment-expression
13396 is not interpreted as a greater-than operator.)
13398 Returns a representation of the parameter, or NULL if an error
13399 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13400 true iff the declarator is of the form "(p)". */
13402 static cp_parameter_declarator
*
13403 cp_parser_parameter_declaration (cp_parser
*parser
,
13404 bool template_parm_p
,
13405 bool *parenthesized_p
)
13407 int declares_class_or_enum
;
13408 bool greater_than_is_operator_p
;
13409 cp_decl_specifier_seq decl_specifiers
;
13410 cp_declarator
*declarator
;
13411 tree default_argument
;
13413 const char *saved_message
;
13415 /* In a template parameter, `>' is not an operator.
13419 When parsing a default template-argument for a non-type
13420 template-parameter, the first non-nested `>' is taken as the end
13421 of the template parameter-list rather than a greater-than
13423 greater_than_is_operator_p
= !template_parm_p
;
13425 /* Type definitions may not appear in parameter types. */
13426 saved_message
= parser
->type_definition_forbidden_message
;
13427 parser
->type_definition_forbidden_message
13428 = "types may not be defined in parameter types";
13430 /* Parse the declaration-specifiers. */
13431 cp_parser_decl_specifier_seq (parser
,
13432 CP_PARSER_FLAGS_NONE
,
13434 &declares_class_or_enum
);
13435 /* If an error occurred, there's no reason to attempt to parse the
13436 rest of the declaration. */
13437 if (cp_parser_error_occurred (parser
))
13439 parser
->type_definition_forbidden_message
= saved_message
;
13443 /* Peek at the next token. */
13444 token
= cp_lexer_peek_token (parser
->lexer
);
13446 /* If the next token is a `)', `,', `=', `>', or `...', then there
13447 is no declarator. However, when variadic templates are enabled,
13448 there may be a declarator following `...'. */
13449 if (token
->type
== CPP_CLOSE_PAREN
13450 || token
->type
== CPP_COMMA
13451 || token
->type
== CPP_EQ
13452 || token
->type
== CPP_GREATER
)
13455 if (parenthesized_p
)
13456 *parenthesized_p
= false;
13458 /* Otherwise, there should be a declarator. */
13461 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
13462 parser
->default_arg_ok_p
= false;
13464 /* After seeing a decl-specifier-seq, if the next token is not a
13465 "(", there is no possibility that the code is a valid
13466 expression. Therefore, if parsing tentatively, we commit at
13468 if (!parser
->in_template_argument_list_p
13469 /* In an expression context, having seen:
13473 we cannot be sure whether we are looking at a
13474 function-type (taking a "char" as a parameter) or a cast
13475 of some object of type "char" to "int". */
13476 && !parser
->in_type_id_in_expr_p
13477 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
13478 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
13479 cp_parser_commit_to_tentative_parse (parser
);
13480 /* Parse the declarator. */
13481 declarator
= cp_parser_declarator (parser
,
13482 CP_PARSER_DECLARATOR_EITHER
,
13483 /*ctor_dtor_or_conv_p=*/NULL
,
13485 /*member_p=*/false);
13486 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
13487 /* After the declarator, allow more attributes. */
13488 decl_specifiers
.attributes
13489 = chainon (decl_specifiers
.attributes
,
13490 cp_parser_attributes_opt (parser
));
13493 /* If the next token is an ellipsis, and we have not seen a
13494 declarator name, and the type of the declarator contains parameter
13495 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13496 a parameter pack expansion expression. Otherwise, leave the
13497 ellipsis for a C-style variadic function. */
13498 token
= cp_lexer_peek_token (parser
->lexer
);
13499 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13501 tree type
= decl_specifiers
.type
;
13503 if (type
&& DECL_P (type
))
13504 type
= TREE_TYPE (type
);
13507 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
13508 && declarator_can_be_parameter_pack (declarator
)
13509 && (!declarator
|| !declarator
->parameter_pack_p
)
13510 && uses_parameter_packs (type
))
13512 /* Consume the `...'. */
13513 cp_lexer_consume_token (parser
->lexer
);
13514 maybe_warn_variadic_templates ();
13516 /* Build a pack expansion type */
13518 declarator
->parameter_pack_p
= true;
13520 decl_specifiers
.type
= make_pack_expansion (type
);
13524 /* The restriction on defining new types applies only to the type
13525 of the parameter, not to the default argument. */
13526 parser
->type_definition_forbidden_message
= saved_message
;
13528 /* If the next token is `=', then process a default argument. */
13529 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13531 bool saved_greater_than_is_operator_p
;
13532 /* Consume the `='. */
13533 cp_lexer_consume_token (parser
->lexer
);
13535 /* If we are defining a class, then the tokens that make up the
13536 default argument must be saved and processed later. */
13537 if (!template_parm_p
&& at_class_scope_p ()
13538 && TYPE_BEING_DEFINED (current_class_type
))
13540 unsigned depth
= 0;
13541 cp_token
*first_token
;
13544 /* Add tokens until we have processed the entire default
13545 argument. We add the range [first_token, token). */
13546 first_token
= cp_lexer_peek_token (parser
->lexer
);
13551 /* Peek at the next token. */
13552 token
= cp_lexer_peek_token (parser
->lexer
);
13553 /* What we do depends on what token we have. */
13554 switch (token
->type
)
13556 /* In valid code, a default argument must be
13557 immediately followed by a `,' `)', or `...'. */
13559 case CPP_CLOSE_PAREN
:
13561 /* If we run into a non-nested `;', `}', or `]',
13562 then the code is invalid -- but the default
13563 argument is certainly over. */
13564 case CPP_SEMICOLON
:
13565 case CPP_CLOSE_BRACE
:
13566 case CPP_CLOSE_SQUARE
:
13569 /* Update DEPTH, if necessary. */
13570 else if (token
->type
== CPP_CLOSE_PAREN
13571 || token
->type
== CPP_CLOSE_BRACE
13572 || token
->type
== CPP_CLOSE_SQUARE
)
13576 case CPP_OPEN_PAREN
:
13577 case CPP_OPEN_SQUARE
:
13578 case CPP_OPEN_BRACE
:
13583 if (cxx_dialect
== cxx98
)
13585 /* Fall through for C++0x, which treats the `>>'
13586 operator like two `>' tokens in certain
13590 /* If we see a non-nested `>', and `>' is not an
13591 operator, then it marks the end of the default
13593 if (!depth
&& !greater_than_is_operator_p
)
13597 /* If we run out of tokens, issue an error message. */
13599 case CPP_PRAGMA_EOL
:
13600 error ("file ends in default argument");
13606 /* In these cases, we should look for template-ids.
13607 For example, if the default argument is
13608 `X<int, double>()', we need to do name lookup to
13609 figure out whether or not `X' is a template; if
13610 so, the `,' does not end the default argument.
13612 That is not yet done. */
13619 /* If we've reached the end, stop. */
13623 /* Add the token to the token block. */
13624 token
= cp_lexer_consume_token (parser
->lexer
);
13627 /* Create a DEFAULT_ARG to represent the unparsed default
13629 default_argument
= make_node (DEFAULT_ARG
);
13630 DEFARG_TOKENS (default_argument
)
13631 = cp_token_cache_new (first_token
, token
);
13632 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
13634 /* Outside of a class definition, we can just parse the
13635 assignment-expression. */
13638 bool saved_local_variables_forbidden_p
;
13640 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13642 saved_greater_than_is_operator_p
13643 = parser
->greater_than_is_operator_p
;
13644 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
13645 /* Local variable names (and the `this' keyword) may not
13646 appear in a default argument. */
13647 saved_local_variables_forbidden_p
13648 = parser
->local_variables_forbidden_p
;
13649 parser
->local_variables_forbidden_p
= true;
13650 /* The default argument expression may cause implicitly
13651 defined member functions to be synthesized, which will
13652 result in garbage collection. We must treat this
13653 situation as if we were within the body of function so as
13654 to avoid collecting live data on the stack. */
13656 /* Parse the assignment-expression. */
13657 if (template_parm_p
)
13658 push_deferring_access_checks (dk_no_deferred
);
13660 = cp_parser_assignment_expression (parser
, /*cast_p=*/false);
13661 if (template_parm_p
)
13662 pop_deferring_access_checks ();
13663 /* Restore saved state. */
13665 parser
->greater_than_is_operator_p
13666 = saved_greater_than_is_operator_p
;
13667 parser
->local_variables_forbidden_p
13668 = saved_local_variables_forbidden_p
;
13670 if (!parser
->default_arg_ok_p
)
13672 if (!flag_pedantic_errors
)
13673 warning (0, "deprecated use of default argument for parameter of non-function");
13676 error ("default arguments are only permitted for function parameters");
13677 default_argument
= NULL_TREE
;
13682 default_argument
= NULL_TREE
;
13684 return make_parameter_declarator (&decl_specifiers
,
13689 /* Parse a function-body.
13692 compound_statement */
13695 cp_parser_function_body (cp_parser
*parser
)
13697 cp_parser_compound_statement (parser
, NULL
, false);
13700 /* Parse a ctor-initializer-opt followed by a function-body. Return
13701 true if a ctor-initializer was present. */
13704 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
13707 bool ctor_initializer_p
;
13709 /* Begin the function body. */
13710 body
= begin_function_body ();
13711 /* Parse the optional ctor-initializer. */
13712 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
13713 /* Parse the function-body. */
13714 cp_parser_function_body (parser
);
13715 /* Finish the function body. */
13716 finish_function_body (body
);
13718 return ctor_initializer_p
;
13721 /* Parse an initializer.
13724 = initializer-clause
13725 ( expression-list )
13727 Returns an expression representing the initializer. If no
13728 initializer is present, NULL_TREE is returned.
13730 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13731 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13732 set to FALSE if there is no initializer present. If there is an
13733 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13734 is set to true; otherwise it is set to false. */
13737 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
13738 bool* non_constant_p
)
13743 /* Peek at the next token. */
13744 token
= cp_lexer_peek_token (parser
->lexer
);
13746 /* Let our caller know whether or not this initializer was
13748 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
13749 /* Assume that the initializer is constant. */
13750 *non_constant_p
= false;
13752 if (token
->type
== CPP_EQ
)
13754 /* Consume the `='. */
13755 cp_lexer_consume_token (parser
->lexer
);
13756 /* Parse the initializer-clause. */
13757 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
13759 else if (token
->type
== CPP_OPEN_PAREN
)
13760 init
= cp_parser_parenthesized_expression_list (parser
, false,
13762 /*allow_expansion_p=*/true,
13766 /* Anything else is an error. */
13767 cp_parser_error (parser
, "expected initializer");
13768 init
= error_mark_node
;
13774 /* Parse an initializer-clause.
13776 initializer-clause:
13777 assignment-expression
13778 { initializer-list , [opt] }
13781 Returns an expression representing the initializer.
13783 If the `assignment-expression' production is used the value
13784 returned is simply a representation for the expression.
13786 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13787 the elements of the initializer-list (or NULL, if the last
13788 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13789 NULL_TREE. There is no way to detect whether or not the optional
13790 trailing `,' was provided. NON_CONSTANT_P is as for
13791 cp_parser_initializer. */
13794 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
13798 /* Assume the expression is constant. */
13799 *non_constant_p
= false;
13801 /* If it is not a `{', then we are looking at an
13802 assignment-expression. */
13803 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13806 = cp_parser_constant_expression (parser
,
13807 /*allow_non_constant_p=*/true,
13809 if (!*non_constant_p
)
13810 initializer
= fold_non_dependent_expr (initializer
);
13814 /* Consume the `{' token. */
13815 cp_lexer_consume_token (parser
->lexer
);
13816 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13817 initializer
= make_node (CONSTRUCTOR
);
13818 /* If it's not a `}', then there is a non-trivial initializer. */
13819 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
13821 /* Parse the initializer list. */
13822 CONSTRUCTOR_ELTS (initializer
)
13823 = cp_parser_initializer_list (parser
, non_constant_p
);
13824 /* A trailing `,' token is allowed. */
13825 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13826 cp_lexer_consume_token (parser
->lexer
);
13828 /* Now, there should be a trailing `}'. */
13829 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13832 return initializer
;
13835 /* Parse an initializer-list.
13838 initializer-clause ... [opt]
13839 initializer-list , initializer-clause ... [opt]
13844 identifier : initializer-clause
13845 initializer-list, identifier : initializer-clause
13847 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13848 for the initializer. If the INDEX of the elt is non-NULL, it is the
13849 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13850 as for cp_parser_initializer. */
13852 static VEC(constructor_elt
,gc
) *
13853 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
13855 VEC(constructor_elt
,gc
) *v
= NULL
;
13857 /* Assume all of the expressions are constant. */
13858 *non_constant_p
= false;
13860 /* Parse the rest of the list. */
13866 bool clause_non_constant_p
;
13868 /* If the next token is an identifier and the following one is a
13869 colon, we are looking at the GNU designated-initializer
13871 if (cp_parser_allow_gnu_extensions_p (parser
)
13872 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
13873 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
13875 /* Warn the user that they are using an extension. */
13877 pedwarn ("ISO C++ does not allow designated initializers");
13878 /* Consume the identifier. */
13879 identifier
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13880 /* Consume the `:'. */
13881 cp_lexer_consume_token (parser
->lexer
);
13884 identifier
= NULL_TREE
;
13886 /* Parse the initializer. */
13887 initializer
= cp_parser_initializer_clause (parser
,
13888 &clause_non_constant_p
);
13889 /* If any clause is non-constant, so is the entire initializer. */
13890 if (clause_non_constant_p
)
13891 *non_constant_p
= true;
13893 /* If we have an ellipsis, this is an initializer pack
13895 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13897 /* Consume the `...'. */
13898 cp_lexer_consume_token (parser
->lexer
);
13900 /* Turn the initializer into an initializer expansion. */
13901 initializer
= make_pack_expansion (initializer
);
13904 /* Add it to the vector. */
13905 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
13907 /* If the next token is not a comma, we have reached the end of
13909 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13912 /* Peek at the next token. */
13913 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13914 /* If the next token is a `}', then we're still done. An
13915 initializer-clause can have a trailing `,' after the
13916 initializer-list and before the closing `}'. */
13917 if (token
->type
== CPP_CLOSE_BRACE
)
13920 /* Consume the `,' token. */
13921 cp_lexer_consume_token (parser
->lexer
);
13927 /* Classes [gram.class] */
13929 /* Parse a class-name.
13935 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13936 to indicate that names looked up in dependent types should be
13937 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13938 keyword has been used to indicate that the name that appears next
13939 is a template. TAG_TYPE indicates the explicit tag given before
13940 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13941 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13942 is the class being defined in a class-head.
13944 Returns the TYPE_DECL representing the class. */
13947 cp_parser_class_name (cp_parser
*parser
,
13948 bool typename_keyword_p
,
13949 bool template_keyword_p
,
13950 enum tag_types tag_type
,
13951 bool check_dependency_p
,
13953 bool is_declaration
)
13960 /* All class-names start with an identifier. */
13961 token
= cp_lexer_peek_token (parser
->lexer
);
13962 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
13964 cp_parser_error (parser
, "expected class-name");
13965 return error_mark_node
;
13968 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13969 to a template-id, so we save it here. */
13970 scope
= parser
->scope
;
13971 if (scope
== error_mark_node
)
13972 return error_mark_node
;
13974 /* Any name names a type if we're following the `typename' keyword
13975 in a qualified name where the enclosing scope is type-dependent. */
13976 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
13977 && dependent_type_p (scope
));
13978 /* Handle the common case (an identifier, but not a template-id)
13980 if (token
->type
== CPP_NAME
13981 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
13983 cp_token
*identifier_token
;
13987 /* Look for the identifier. */
13988 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
13989 ambiguous_p
= identifier_token
->ambiguous_p
;
13990 identifier
= cp_parser_identifier (parser
);
13991 /* If the next token isn't an identifier, we are certainly not
13992 looking at a class-name. */
13993 if (identifier
== error_mark_node
)
13994 decl
= error_mark_node
;
13995 /* If we know this is a type-name, there's no need to look it
13997 else if (typename_p
)
14001 tree ambiguous_decls
;
14002 /* If we already know that this lookup is ambiguous, then
14003 we've already issued an error message; there's no reason
14007 cp_parser_simulate_error (parser
);
14008 return error_mark_node
;
14010 /* If the next token is a `::', then the name must be a type
14013 [basic.lookup.qual]
14015 During the lookup for a name preceding the :: scope
14016 resolution operator, object, function, and enumerator
14017 names are ignored. */
14018 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
14019 tag_type
= typename_type
;
14020 /* Look up the name. */
14021 decl
= cp_parser_lookup_name (parser
, identifier
,
14023 /*is_template=*/false,
14024 /*is_namespace=*/false,
14025 check_dependency_p
,
14027 if (ambiguous_decls
)
14029 error ("reference to %qD is ambiguous", identifier
);
14030 print_candidates (ambiguous_decls
);
14031 if (cp_parser_parsing_tentatively (parser
))
14033 identifier_token
->ambiguous_p
= true;
14034 cp_parser_simulate_error (parser
);
14036 return error_mark_node
;
14042 /* Try a template-id. */
14043 decl
= cp_parser_template_id (parser
, template_keyword_p
,
14044 check_dependency_p
,
14046 if (decl
== error_mark_node
)
14047 return error_mark_node
;
14050 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
14052 /* If this is a typename, create a TYPENAME_TYPE. */
14053 if (typename_p
&& decl
!= error_mark_node
)
14055 decl
= make_typename_type (scope
, decl
, typename_type
,
14056 /*complain=*/tf_error
);
14057 if (decl
!= error_mark_node
)
14058 decl
= TYPE_NAME (decl
);
14061 /* Check to see that it is really the name of a class. */
14062 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
14063 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
14064 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
14065 /* Situations like this:
14067 template <typename T> struct A {
14068 typename T::template X<int>::I i;
14071 are problematic. Is `T::template X<int>' a class-name? The
14072 standard does not seem to be definitive, but there is no other
14073 valid interpretation of the following `::'. Therefore, those
14074 names are considered class-names. */
14076 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
14077 if (decl
!= error_mark_node
)
14078 decl
= TYPE_NAME (decl
);
14080 else if (TREE_CODE (decl
) != TYPE_DECL
14081 || TREE_TYPE (decl
) == error_mark_node
14082 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
14083 decl
= error_mark_node
;
14085 if (decl
== error_mark_node
)
14086 cp_parser_error (parser
, "expected class-name");
14091 /* Parse a class-specifier.
14094 class-head { member-specification [opt] }
14096 Returns the TREE_TYPE representing the class. */
14099 cp_parser_class_specifier (cp_parser
* parser
)
14103 tree attributes
= NULL_TREE
;
14104 int has_trailing_semicolon
;
14105 bool nested_name_specifier_p
;
14106 unsigned saved_num_template_parameter_lists
;
14107 bool saved_in_function_body
;
14108 tree old_scope
= NULL_TREE
;
14109 tree scope
= NULL_TREE
;
14112 push_deferring_access_checks (dk_no_deferred
);
14114 /* Parse the class-head. */
14115 type
= cp_parser_class_head (parser
,
14116 &nested_name_specifier_p
,
14119 /* If the class-head was a semantic disaster, skip the entire body
14123 cp_parser_skip_to_end_of_block_or_statement (parser
);
14124 pop_deferring_access_checks ();
14125 return error_mark_node
;
14128 /* Look for the `{'. */
14129 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
14131 pop_deferring_access_checks ();
14132 return error_mark_node
;
14135 /* Process the base classes. If they're invalid, skip the
14136 entire class body. */
14137 if (!xref_basetypes (type
, bases
))
14139 /* Consuming the closing brace yields better error messages
14141 if (cp_parser_skip_to_closing_brace (parser
))
14142 cp_lexer_consume_token (parser
->lexer
);
14143 pop_deferring_access_checks ();
14144 return error_mark_node
;
14147 /* Issue an error message if type-definitions are forbidden here. */
14148 cp_parser_check_type_definition (parser
);
14149 /* Remember that we are defining one more class. */
14150 ++parser
->num_classes_being_defined
;
14151 /* Inside the class, surrounding template-parameter-lists do not
14153 saved_num_template_parameter_lists
14154 = parser
->num_template_parameter_lists
;
14155 parser
->num_template_parameter_lists
= 0;
14156 /* We are not in a function body. */
14157 saved_in_function_body
= parser
->in_function_body
;
14158 parser
->in_function_body
= false;
14160 /* Start the class. */
14161 if (nested_name_specifier_p
)
14163 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
14164 old_scope
= push_inner_scope (scope
);
14166 type
= begin_class_definition (type
, attributes
);
14168 if (type
== error_mark_node
)
14169 /* If the type is erroneous, skip the entire body of the class. */
14170 cp_parser_skip_to_closing_brace (parser
);
14172 /* Parse the member-specification. */
14173 cp_parser_member_specification_opt (parser
);
14175 /* Look for the trailing `}'. */
14176 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
14177 /* We get better error messages by noticing a common problem: a
14178 missing trailing `;'. */
14179 token
= cp_lexer_peek_token (parser
->lexer
);
14180 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
14181 /* Look for trailing attributes to apply to this class. */
14182 if (cp_parser_allow_gnu_extensions_p (parser
))
14183 attributes
= cp_parser_attributes_opt (parser
);
14184 if (type
!= error_mark_node
)
14185 type
= finish_struct (type
, attributes
);
14186 if (nested_name_specifier_p
)
14187 pop_inner_scope (old_scope
, scope
);
14188 /* If this class is not itself within the scope of another class,
14189 then we need to parse the bodies of all of the queued function
14190 definitions. Note that the queued functions defined in a class
14191 are not always processed immediately following the
14192 class-specifier for that class. Consider:
14195 struct B { void f() { sizeof (A); } };
14198 If `f' were processed before the processing of `A' were
14199 completed, there would be no way to compute the size of `A'.
14200 Note that the nesting we are interested in here is lexical --
14201 not the semantic nesting given by TYPE_CONTEXT. In particular,
14204 struct A { struct B; };
14205 struct A::B { void f() { } };
14207 there is no need to delay the parsing of `A::B::f'. */
14208 if (--parser
->num_classes_being_defined
== 0)
14212 tree class_type
= NULL_TREE
;
14213 tree pushed_scope
= NULL_TREE
;
14215 /* In a first pass, parse default arguments to the functions.
14216 Then, in a second pass, parse the bodies of the functions.
14217 This two-phased approach handles cases like:
14225 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
14226 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
14227 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
14228 TREE_PURPOSE (parser
->unparsed_functions_queues
)
14229 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
14231 fn
= TREE_VALUE (queue_entry
);
14232 /* If there are default arguments that have not yet been processed,
14233 take care of them now. */
14234 if (class_type
!= TREE_PURPOSE (queue_entry
))
14237 pop_scope (pushed_scope
);
14238 class_type
= TREE_PURPOSE (queue_entry
);
14239 pushed_scope
= push_scope (class_type
);
14241 /* Make sure that any template parameters are in scope. */
14242 maybe_begin_member_template_processing (fn
);
14243 /* Parse the default argument expressions. */
14244 cp_parser_late_parsing_default_args (parser
, fn
);
14245 /* Remove any template parameters from the symbol table. */
14246 maybe_end_member_template_processing ();
14249 pop_scope (pushed_scope
);
14250 /* Now parse the body of the functions. */
14251 for (TREE_VALUE (parser
->unparsed_functions_queues
)
14252 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
14253 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
14254 TREE_VALUE (parser
->unparsed_functions_queues
)
14255 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
14257 /* Figure out which function we need to process. */
14258 fn
= TREE_VALUE (queue_entry
);
14259 /* Parse the function. */
14260 cp_parser_late_parsing_for_member (parser
, fn
);
14264 /* Put back any saved access checks. */
14265 pop_deferring_access_checks ();
14267 /* Restore saved state. */
14268 parser
->in_function_body
= saved_in_function_body
;
14269 parser
->num_template_parameter_lists
14270 = saved_num_template_parameter_lists
;
14275 /* Parse a class-head.
14278 class-key identifier [opt] base-clause [opt]
14279 class-key nested-name-specifier identifier base-clause [opt]
14280 class-key nested-name-specifier [opt] template-id
14284 class-key attributes identifier [opt] base-clause [opt]
14285 class-key attributes nested-name-specifier identifier base-clause [opt]
14286 class-key attributes nested-name-specifier [opt] template-id
14289 Upon return BASES is initialized to the list of base classes (or
14290 NULL, if there are none) in the same form returned by
14291 cp_parser_base_clause.
14293 Returns the TYPE of the indicated class. Sets
14294 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14295 involving a nested-name-specifier was used, and FALSE otherwise.
14297 Returns error_mark_node if this is not a class-head.
14299 Returns NULL_TREE if the class-head is syntactically valid, but
14300 semantically invalid in a way that means we should skip the entire
14301 body of the class. */
14304 cp_parser_class_head (cp_parser
* parser
,
14305 bool* nested_name_specifier_p
,
14306 tree
*attributes_p
,
14309 tree nested_name_specifier
;
14310 enum tag_types class_key
;
14311 tree id
= NULL_TREE
;
14312 tree type
= NULL_TREE
;
14314 bool template_id_p
= false;
14315 bool qualified_p
= false;
14316 bool invalid_nested_name_p
= false;
14317 bool invalid_explicit_specialization_p
= false;
14318 tree pushed_scope
= NULL_TREE
;
14319 unsigned num_templates
;
14321 /* Assume no nested-name-specifier will be present. */
14322 *nested_name_specifier_p
= false;
14323 /* Assume no template parameter lists will be used in defining the
14327 *bases
= NULL_TREE
;
14329 /* Look for the class-key. */
14330 class_key
= cp_parser_class_key (parser
);
14331 if (class_key
== none_type
)
14332 return error_mark_node
;
14334 /* Parse the attributes. */
14335 attributes
= cp_parser_attributes_opt (parser
);
14337 /* If the next token is `::', that is invalid -- but sometimes
14338 people do try to write:
14342 Handle this gracefully by accepting the extra qualifier, and then
14343 issuing an error about it later if this really is a
14344 class-head. If it turns out just to be an elaborated type
14345 specifier, remain silent. */
14346 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
14347 qualified_p
= true;
14349 push_deferring_access_checks (dk_no_check
);
14351 /* Determine the name of the class. Begin by looking for an
14352 optional nested-name-specifier. */
14353 nested_name_specifier
14354 = cp_parser_nested_name_specifier_opt (parser
,
14355 /*typename_keyword_p=*/false,
14356 /*check_dependency_p=*/false,
14358 /*is_declaration=*/false);
14359 /* If there was a nested-name-specifier, then there *must* be an
14361 if (nested_name_specifier
)
14363 /* Although the grammar says `identifier', it really means
14364 `class-name' or `template-name'. You are only allowed to
14365 define a class that has already been declared with this
14368 The proposed resolution for Core Issue 180 says that wherever
14369 you see `class T::X' you should treat `X' as a type-name.
14371 It is OK to define an inaccessible class; for example:
14373 class A { class B; };
14376 We do not know if we will see a class-name, or a
14377 template-name. We look for a class-name first, in case the
14378 class-name is a template-id; if we looked for the
14379 template-name first we would stop after the template-name. */
14380 cp_parser_parse_tentatively (parser
);
14381 type
= cp_parser_class_name (parser
,
14382 /*typename_keyword_p=*/false,
14383 /*template_keyword_p=*/false,
14385 /*check_dependency_p=*/false,
14386 /*class_head_p=*/true,
14387 /*is_declaration=*/false);
14388 /* If that didn't work, ignore the nested-name-specifier. */
14389 if (!cp_parser_parse_definitely (parser
))
14391 invalid_nested_name_p
= true;
14392 id
= cp_parser_identifier (parser
);
14393 if (id
== error_mark_node
)
14396 /* If we could not find a corresponding TYPE, treat this
14397 declaration like an unqualified declaration. */
14398 if (type
== error_mark_node
)
14399 nested_name_specifier
= NULL_TREE
;
14400 /* Otherwise, count the number of templates used in TYPE and its
14401 containing scopes. */
14406 for (scope
= TREE_TYPE (type
);
14407 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
14408 scope
= (TYPE_P (scope
)
14409 ? TYPE_CONTEXT (scope
)
14410 : DECL_CONTEXT (scope
)))
14412 && CLASS_TYPE_P (scope
)
14413 && CLASSTYPE_TEMPLATE_INFO (scope
)
14414 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
14415 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
14419 /* Otherwise, the identifier is optional. */
14422 /* We don't know whether what comes next is a template-id,
14423 an identifier, or nothing at all. */
14424 cp_parser_parse_tentatively (parser
);
14425 /* Check for a template-id. */
14426 id
= cp_parser_template_id (parser
,
14427 /*template_keyword_p=*/false,
14428 /*check_dependency_p=*/true,
14429 /*is_declaration=*/true);
14430 /* If that didn't work, it could still be an identifier. */
14431 if (!cp_parser_parse_definitely (parser
))
14433 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
14434 id
= cp_parser_identifier (parser
);
14440 template_id_p
= true;
14445 pop_deferring_access_checks ();
14448 cp_parser_check_for_invalid_template_id (parser
, id
);
14450 /* If it's not a `:' or a `{' then we can't really be looking at a
14451 class-head, since a class-head only appears as part of a
14452 class-specifier. We have to detect this situation before calling
14453 xref_tag, since that has irreversible side-effects. */
14454 if (!cp_parser_next_token_starts_class_definition_p (parser
))
14456 cp_parser_error (parser
, "expected %<{%> or %<:%>");
14457 return error_mark_node
;
14460 /* At this point, we're going ahead with the class-specifier, even
14461 if some other problem occurs. */
14462 cp_parser_commit_to_tentative_parse (parser
);
14463 /* Issue the error about the overly-qualified name now. */
14465 cp_parser_error (parser
,
14466 "global qualification of class name is invalid");
14467 else if (invalid_nested_name_p
)
14468 cp_parser_error (parser
,
14469 "qualified name does not name a class");
14470 else if (nested_name_specifier
)
14474 /* Reject typedef-names in class heads. */
14475 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
14477 error ("invalid class name in declaration of %qD", type
);
14482 /* Figure out in what scope the declaration is being placed. */
14483 scope
= current_scope ();
14484 /* If that scope does not contain the scope in which the
14485 class was originally declared, the program is invalid. */
14486 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
14488 if (at_namespace_scope_p ())
14489 error ("declaration of %qD in namespace %qD which does not "
14490 "enclose %qD", type
, scope
, nested_name_specifier
);
14492 error ("declaration of %qD in %qD which does not enclose %qD",
14493 type
, scope
, nested_name_specifier
);
14499 A declarator-id shall not be qualified exception of the
14500 definition of a ... nested class outside of its class
14501 ... [or] a the definition or explicit instantiation of a
14502 class member of a namespace outside of its namespace. */
14503 if (scope
== nested_name_specifier
)
14505 pedwarn ("extra qualification ignored");
14506 nested_name_specifier
= NULL_TREE
;
14510 /* An explicit-specialization must be preceded by "template <>". If
14511 it is not, try to recover gracefully. */
14512 if (at_namespace_scope_p ()
14513 && parser
->num_template_parameter_lists
== 0
14516 error ("an explicit specialization must be preceded by %<template <>%>");
14517 invalid_explicit_specialization_p
= true;
14518 /* Take the same action that would have been taken by
14519 cp_parser_explicit_specialization. */
14520 ++parser
->num_template_parameter_lists
;
14521 begin_specialization ();
14523 /* There must be no "return" statements between this point and the
14524 end of this function; set "type "to the correct return value and
14525 use "goto done;" to return. */
14526 /* Make sure that the right number of template parameters were
14528 if (!cp_parser_check_template_parameters (parser
, num_templates
))
14530 /* If something went wrong, there is no point in even trying to
14531 process the class-definition. */
14536 /* Look up the type. */
14539 type
= TREE_TYPE (id
);
14540 type
= maybe_process_partial_specialization (type
);
14541 if (nested_name_specifier
)
14542 pushed_scope
= push_scope (nested_name_specifier
);
14544 else if (nested_name_specifier
)
14550 template <typename T> struct S { struct T };
14551 template <typename T> struct S<T>::T { };
14553 we will get a TYPENAME_TYPE when processing the definition of
14554 `S::T'. We need to resolve it to the actual type before we
14555 try to define it. */
14556 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
14558 class_type
= resolve_typename_type (TREE_TYPE (type
),
14559 /*only_current_p=*/false);
14560 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
14561 type
= TYPE_NAME (class_type
);
14564 cp_parser_error (parser
, "could not resolve typename type");
14565 type
= error_mark_node
;
14569 maybe_process_partial_specialization (TREE_TYPE (type
));
14570 class_type
= current_class_type
;
14571 /* Enter the scope indicated by the nested-name-specifier. */
14572 pushed_scope
= push_scope (nested_name_specifier
);
14573 /* Get the canonical version of this type. */
14574 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
14575 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14576 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
14578 type
= push_template_decl (type
);
14579 if (type
== error_mark_node
)
14586 type
= TREE_TYPE (type
);
14587 *nested_name_specifier_p
= true;
14589 else /* The name is not a nested name. */
14591 /* If the class was unnamed, create a dummy name. */
14593 id
= make_anon_name ();
14594 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
14595 parser
->num_template_parameter_lists
);
14598 /* Indicate whether this class was declared as a `class' or as a
14600 if (TREE_CODE (type
) == RECORD_TYPE
)
14601 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
14602 cp_parser_check_class_key (class_key
, type
);
14604 /* If this type was already complete, and we see another definition,
14605 that's an error. */
14606 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
14608 error ("redefinition of %q#T", type
);
14609 error ("previous definition of %q+#T", type
);
14613 else if (type
== error_mark_node
)
14616 /* We will have entered the scope containing the class; the names of
14617 base classes should be looked up in that context. For example:
14619 struct A { struct B {}; struct C; };
14620 struct A::C : B {};
14624 /* Get the list of base-classes, if there is one. */
14625 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
14626 *bases
= cp_parser_base_clause (parser
);
14629 /* Leave the scope given by the nested-name-specifier. We will
14630 enter the class scope itself while processing the members. */
14632 pop_scope (pushed_scope
);
14634 if (invalid_explicit_specialization_p
)
14636 end_specialization ();
14637 --parser
->num_template_parameter_lists
;
14639 *attributes_p
= attributes
;
14643 /* Parse a class-key.
14650 Returns the kind of class-key specified, or none_type to indicate
14653 static enum tag_types
14654 cp_parser_class_key (cp_parser
* parser
)
14657 enum tag_types tag_type
;
14659 /* Look for the class-key. */
14660 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
14664 /* Check to see if the TOKEN is a class-key. */
14665 tag_type
= cp_parser_token_is_class_key (token
);
14667 cp_parser_error (parser
, "expected class-key");
14671 /* Parse an (optional) member-specification.
14673 member-specification:
14674 member-declaration member-specification [opt]
14675 access-specifier : member-specification [opt] */
14678 cp_parser_member_specification_opt (cp_parser
* parser
)
14685 /* Peek at the next token. */
14686 token
= cp_lexer_peek_token (parser
->lexer
);
14687 /* If it's a `}', or EOF then we've seen all the members. */
14688 if (token
->type
== CPP_CLOSE_BRACE
14689 || token
->type
== CPP_EOF
14690 || token
->type
== CPP_PRAGMA_EOL
)
14693 /* See if this token is a keyword. */
14694 keyword
= token
->keyword
;
14698 case RID_PROTECTED
:
14700 /* Consume the access-specifier. */
14701 cp_lexer_consume_token (parser
->lexer
);
14702 /* Remember which access-specifier is active. */
14703 current_access_specifier
= token
->u
.value
;
14704 /* Look for the `:'. */
14705 cp_parser_require (parser
, CPP_COLON
, "`:'");
14709 /* Accept #pragmas at class scope. */
14710 if (token
->type
== CPP_PRAGMA
)
14712 cp_parser_pragma (parser
, pragma_external
);
14716 /* Otherwise, the next construction must be a
14717 member-declaration. */
14718 cp_parser_member_declaration (parser
);
14723 /* Parse a member-declaration.
14725 member-declaration:
14726 decl-specifier-seq [opt] member-declarator-list [opt] ;
14727 function-definition ; [opt]
14728 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14730 template-declaration
14732 member-declarator-list:
14734 member-declarator-list , member-declarator
14737 declarator pure-specifier [opt]
14738 declarator constant-initializer [opt]
14739 identifier [opt] : constant-expression
14743 member-declaration:
14744 __extension__ member-declaration
14747 declarator attributes [opt] pure-specifier [opt]
14748 declarator attributes [opt] constant-initializer [opt]
14749 identifier [opt] attributes [opt] : constant-expression
14753 member-declaration:
14754 static_assert-declaration */
14757 cp_parser_member_declaration (cp_parser
* parser
)
14759 cp_decl_specifier_seq decl_specifiers
;
14760 tree prefix_attributes
;
14762 int declares_class_or_enum
;
14765 int saved_pedantic
;
14767 /* Check for the `__extension__' keyword. */
14768 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
14771 cp_parser_member_declaration (parser
);
14772 /* Restore the old value of the PEDANTIC flag. */
14773 pedantic
= saved_pedantic
;
14778 /* Check for a template-declaration. */
14779 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14781 /* An explicit specialization here is an error condition, and we
14782 expect the specialization handler to detect and report this. */
14783 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
14784 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
14785 cp_parser_explicit_specialization (parser
);
14787 cp_parser_template_declaration (parser
, /*member_p=*/true);
14792 /* Check for a using-declaration. */
14793 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
14795 /* Parse the using-declaration. */
14796 cp_parser_using_declaration (parser
,
14797 /*access_declaration_p=*/false);
14801 /* Check for @defs. */
14802 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
14805 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
14806 ivar
= ivar_chains
;
14810 ivar
= TREE_CHAIN (member
);
14811 TREE_CHAIN (member
) = NULL_TREE
;
14812 finish_member_declaration (member
);
14817 /* If the next token is `static_assert' we have a static assertion. */
14818 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
14820 cp_parser_static_assert (parser
, /*member_p=*/true);
14824 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
14827 /* Parse the decl-specifier-seq. */
14828 cp_parser_decl_specifier_seq (parser
,
14829 CP_PARSER_FLAGS_OPTIONAL
,
14831 &declares_class_or_enum
);
14832 prefix_attributes
= decl_specifiers
.attributes
;
14833 decl_specifiers
.attributes
= NULL_TREE
;
14834 /* Check for an invalid type-name. */
14835 if (!decl_specifiers
.type
14836 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
14838 /* If there is no declarator, then the decl-specifier-seq should
14840 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14842 /* If there was no decl-specifier-seq, and the next token is a
14843 `;', then we have something like:
14849 Each member-declaration shall declare at least one member
14850 name of the class. */
14851 if (!decl_specifiers
.any_specifiers_p
)
14853 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14854 if (pedantic
&& !token
->in_system_header
)
14855 pedwarn ("%Hextra %<;%>", &token
->location
);
14861 /* See if this declaration is a friend. */
14862 friend_p
= cp_parser_friend_p (&decl_specifiers
);
14863 /* If there were decl-specifiers, check to see if there was
14864 a class-declaration. */
14865 type
= check_tag_decl (&decl_specifiers
);
14866 /* Nested classes have already been added to the class, but
14867 a `friend' needs to be explicitly registered. */
14870 /* If the `friend' keyword was present, the friend must
14871 be introduced with a class-key. */
14872 if (!declares_class_or_enum
)
14873 error ("a class-key must be used when declaring a friend");
14876 template <typename T> struct A {
14877 friend struct A<T>::B;
14880 A<T>::B will be represented by a TYPENAME_TYPE, and
14881 therefore not recognized by check_tag_decl. */
14883 && decl_specifiers
.type
14884 && TYPE_P (decl_specifiers
.type
))
14885 type
= decl_specifiers
.type
;
14886 if (!type
|| !TYPE_P (type
))
14887 error ("friend declaration does not name a class or "
14890 make_friend_class (current_class_type
, type
,
14891 /*complain=*/true);
14893 /* If there is no TYPE, an error message will already have
14895 else if (!type
|| type
== error_mark_node
)
14897 /* An anonymous aggregate has to be handled specially; such
14898 a declaration really declares a data member (with a
14899 particular type), as opposed to a nested class. */
14900 else if (ANON_AGGR_TYPE_P (type
))
14902 /* Remove constructors and such from TYPE, now that we
14903 know it is an anonymous aggregate. */
14904 fixup_anonymous_aggr (type
);
14905 /* And make the corresponding data member. */
14906 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
14907 /* Add it to the class. */
14908 finish_member_declaration (decl
);
14911 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
14916 /* See if these declarations will be friends. */
14917 friend_p
= cp_parser_friend_p (&decl_specifiers
);
14919 /* Keep going until we hit the `;' at the end of the
14921 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
14923 tree attributes
= NULL_TREE
;
14924 tree first_attribute
;
14926 /* Peek at the next token. */
14927 token
= cp_lexer_peek_token (parser
->lexer
);
14929 /* Check for a bitfield declaration. */
14930 if (token
->type
== CPP_COLON
14931 || (token
->type
== CPP_NAME
14932 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
14938 /* Get the name of the bitfield. Note that we cannot just
14939 check TOKEN here because it may have been invalidated by
14940 the call to cp_lexer_peek_nth_token above. */
14941 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
14942 identifier
= cp_parser_identifier (parser
);
14944 identifier
= NULL_TREE
;
14946 /* Consume the `:' token. */
14947 cp_lexer_consume_token (parser
->lexer
);
14948 /* Get the width of the bitfield. */
14950 = cp_parser_constant_expression (parser
,
14951 /*allow_non_constant=*/false,
14954 /* Look for attributes that apply to the bitfield. */
14955 attributes
= cp_parser_attributes_opt (parser
);
14956 /* Remember which attributes are prefix attributes and
14958 first_attribute
= attributes
;
14959 /* Combine the attributes. */
14960 attributes
= chainon (prefix_attributes
, attributes
);
14962 /* Create the bitfield declaration. */
14963 decl
= grokbitfield (identifier
14964 ? make_id_declarator (NULL_TREE
,
14970 /* Apply the attributes. */
14971 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
14975 cp_declarator
*declarator
;
14977 tree asm_specification
;
14978 int ctor_dtor_or_conv_p
;
14980 /* Parse the declarator. */
14982 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14983 &ctor_dtor_or_conv_p
,
14984 /*parenthesized_p=*/NULL
,
14985 /*member_p=*/true);
14987 /* If something went wrong parsing the declarator, make sure
14988 that we at least consume some tokens. */
14989 if (declarator
== cp_error_declarator
)
14991 /* Skip to the end of the statement. */
14992 cp_parser_skip_to_end_of_statement (parser
);
14993 /* If the next token is not a semicolon, that is
14994 probably because we just skipped over the body of
14995 a function. So, we consume a semicolon if
14996 present, but do not issue an error message if it
14998 if (cp_lexer_next_token_is (parser
->lexer
,
15000 cp_lexer_consume_token (parser
->lexer
);
15004 if (declares_class_or_enum
& 2)
15005 cp_parser_check_for_definition_in_return_type
15006 (declarator
, decl_specifiers
.type
);
15008 /* Look for an asm-specification. */
15009 asm_specification
= cp_parser_asm_specification_opt (parser
);
15010 /* Look for attributes that apply to the declaration. */
15011 attributes
= cp_parser_attributes_opt (parser
);
15012 /* Remember which attributes are prefix attributes and
15014 first_attribute
= attributes
;
15015 /* Combine the attributes. */
15016 attributes
= chainon (prefix_attributes
, attributes
);
15018 /* If it's an `=', then we have a constant-initializer or a
15019 pure-specifier. It is not correct to parse the
15020 initializer before registering the member declaration
15021 since the member declaration should be in scope while
15022 its initializer is processed. However, the rest of the
15023 front end does not yet provide an interface that allows
15024 us to handle this correctly. */
15025 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15029 A pure-specifier shall be used only in the declaration of
15030 a virtual function.
15032 A member-declarator can contain a constant-initializer
15033 only if it declares a static member of integral or
15036 Therefore, if the DECLARATOR is for a function, we look
15037 for a pure-specifier; otherwise, we look for a
15038 constant-initializer. When we call `grokfield', it will
15039 perform more stringent semantics checks. */
15040 if (function_declarator_p (declarator
))
15041 initializer
= cp_parser_pure_specifier (parser
);
15043 /* Parse the initializer. */
15044 initializer
= cp_parser_constant_initializer (parser
);
15046 /* Otherwise, there is no initializer. */
15048 initializer
= NULL_TREE
;
15050 /* See if we are probably looking at a function
15051 definition. We are certainly not looking at a
15052 member-declarator. Calling `grokfield' has
15053 side-effects, so we must not do it unless we are sure
15054 that we are looking at a member-declarator. */
15055 if (cp_parser_token_starts_function_definition_p
15056 (cp_lexer_peek_token (parser
->lexer
)))
15058 /* The grammar does not allow a pure-specifier to be
15059 used when a member function is defined. (It is
15060 possible that this fact is an oversight in the
15061 standard, since a pure function may be defined
15062 outside of the class-specifier. */
15064 error ("pure-specifier on function-definition");
15065 decl
= cp_parser_save_member_function_body (parser
,
15069 /* If the member was not a friend, declare it here. */
15071 finish_member_declaration (decl
);
15072 /* Peek at the next token. */
15073 token
= cp_lexer_peek_token (parser
->lexer
);
15074 /* If the next token is a semicolon, consume it. */
15075 if (token
->type
== CPP_SEMICOLON
)
15076 cp_lexer_consume_token (parser
->lexer
);
15080 /* Create the declaration. */
15081 decl
= grokfield (declarator
, &decl_specifiers
,
15082 initializer
, /*init_const_expr_p=*/true,
15087 /* Reset PREFIX_ATTRIBUTES. */
15088 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
15089 attributes
= TREE_CHAIN (attributes
);
15091 TREE_CHAIN (attributes
) = NULL_TREE
;
15093 /* If there is any qualification still in effect, clear it
15094 now; we will be starting fresh with the next declarator. */
15095 parser
->scope
= NULL_TREE
;
15096 parser
->qualifying_scope
= NULL_TREE
;
15097 parser
->object_scope
= NULL_TREE
;
15098 /* If it's a `,', then there are more declarators. */
15099 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
15100 cp_lexer_consume_token (parser
->lexer
);
15101 /* If the next token isn't a `;', then we have a parse error. */
15102 else if (cp_lexer_next_token_is_not (parser
->lexer
,
15105 cp_parser_error (parser
, "expected %<;%>");
15106 /* Skip tokens until we find a `;'. */
15107 cp_parser_skip_to_end_of_statement (parser
);
15114 /* Add DECL to the list of members. */
15116 finish_member_declaration (decl
);
15118 if (TREE_CODE (decl
) == FUNCTION_DECL
)
15119 cp_parser_save_default_args (parser
, decl
);
15124 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
15127 /* Parse a pure-specifier.
15132 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15133 Otherwise, ERROR_MARK_NODE is returned. */
15136 cp_parser_pure_specifier (cp_parser
* parser
)
15140 /* Look for the `=' token. */
15141 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
15142 return error_mark_node
;
15143 /* Look for the `0' token. */
15144 token
= cp_lexer_consume_token (parser
->lexer
);
15145 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15146 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
15148 cp_parser_error (parser
,
15149 "invalid pure specifier (only `= 0' is allowed)");
15150 cp_parser_skip_to_end_of_statement (parser
);
15151 return error_mark_node
;
15153 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15155 error ("templates may not be %<virtual%>");
15156 return error_mark_node
;
15159 return integer_zero_node
;
15162 /* Parse a constant-initializer.
15164 constant-initializer:
15165 = constant-expression
15167 Returns a representation of the constant-expression. */
15170 cp_parser_constant_initializer (cp_parser
* parser
)
15172 /* Look for the `=' token. */
15173 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
15174 return error_mark_node
;
15176 /* It is invalid to write:
15178 struct S { static const int i = { 7 }; };
15181 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15183 cp_parser_error (parser
,
15184 "a brace-enclosed initializer is not allowed here");
15185 /* Consume the opening brace. */
15186 cp_lexer_consume_token (parser
->lexer
);
15187 /* Skip the initializer. */
15188 cp_parser_skip_to_closing_brace (parser
);
15189 /* Look for the trailing `}'. */
15190 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
15192 return error_mark_node
;
15195 return cp_parser_constant_expression (parser
,
15196 /*allow_non_constant=*/false,
15200 /* Derived classes [gram.class.derived] */
15202 /* Parse a base-clause.
15205 : base-specifier-list
15207 base-specifier-list:
15208 base-specifier ... [opt]
15209 base-specifier-list , base-specifier ... [opt]
15211 Returns a TREE_LIST representing the base-classes, in the order in
15212 which they were declared. The representation of each node is as
15213 described by cp_parser_base_specifier.
15215 In the case that no bases are specified, this function will return
15216 NULL_TREE, not ERROR_MARK_NODE. */
15219 cp_parser_base_clause (cp_parser
* parser
)
15221 tree bases
= NULL_TREE
;
15223 /* Look for the `:' that begins the list. */
15224 cp_parser_require (parser
, CPP_COLON
, "`:'");
15226 /* Scan the base-specifier-list. */
15231 bool pack_expansion_p
= false;
15233 /* Look for the base-specifier. */
15234 base
= cp_parser_base_specifier (parser
);
15235 /* Look for the (optional) ellipsis. */
15236 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15238 /* Consume the `...'. */
15239 cp_lexer_consume_token (parser
->lexer
);
15241 pack_expansion_p
= true;
15244 /* Add BASE to the front of the list. */
15245 if (base
!= error_mark_node
)
15247 if (pack_expansion_p
)
15248 /* Make this a pack expansion type. */
15249 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
15251 check_for_bare_parameter_packs (&TREE_VALUE (base
));
15253 TREE_CHAIN (base
) = bases
;
15256 /* Peek at the next token. */
15257 token
= cp_lexer_peek_token (parser
->lexer
);
15258 /* If it's not a comma, then the list is complete. */
15259 if (token
->type
!= CPP_COMMA
)
15261 /* Consume the `,'. */
15262 cp_lexer_consume_token (parser
->lexer
);
15265 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15266 base class had a qualified name. However, the next name that
15267 appears is certainly not qualified. */
15268 parser
->scope
= NULL_TREE
;
15269 parser
->qualifying_scope
= NULL_TREE
;
15270 parser
->object_scope
= NULL_TREE
;
15272 return nreverse (bases
);
15275 /* Parse a base-specifier.
15278 :: [opt] nested-name-specifier [opt] class-name
15279 virtual access-specifier [opt] :: [opt] nested-name-specifier
15281 access-specifier virtual [opt] :: [opt] nested-name-specifier
15284 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15285 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15286 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15287 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15290 cp_parser_base_specifier (cp_parser
* parser
)
15294 bool virtual_p
= false;
15295 bool duplicate_virtual_error_issued_p
= false;
15296 bool duplicate_access_error_issued_p
= false;
15297 bool class_scope_p
, template_p
;
15298 tree access
= access_default_node
;
15301 /* Process the optional `virtual' and `access-specifier'. */
15304 /* Peek at the next token. */
15305 token
= cp_lexer_peek_token (parser
->lexer
);
15306 /* Process `virtual'. */
15307 switch (token
->keyword
)
15310 /* If `virtual' appears more than once, issue an error. */
15311 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
15313 cp_parser_error (parser
,
15314 "%<virtual%> specified more than once in base-specified");
15315 duplicate_virtual_error_issued_p
= true;
15320 /* Consume the `virtual' token. */
15321 cp_lexer_consume_token (parser
->lexer
);
15326 case RID_PROTECTED
:
15328 /* If more than one access specifier appears, issue an
15330 if (access
!= access_default_node
15331 && !duplicate_access_error_issued_p
)
15333 cp_parser_error (parser
,
15334 "more than one access specifier in base-specified");
15335 duplicate_access_error_issued_p
= true;
15338 access
= ridpointers
[(int) token
->keyword
];
15340 /* Consume the access-specifier. */
15341 cp_lexer_consume_token (parser
->lexer
);
15350 /* It is not uncommon to see programs mechanically, erroneously, use
15351 the 'typename' keyword to denote (dependent) qualified types
15352 as base classes. */
15353 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
15355 if (!processing_template_decl
)
15356 error ("keyword %<typename%> not allowed outside of templates");
15358 error ("keyword %<typename%> not allowed in this context "
15359 "(the base class is implicitly a type)");
15360 cp_lexer_consume_token (parser
->lexer
);
15363 /* Look for the optional `::' operator. */
15364 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
15365 /* Look for the nested-name-specifier. The simplest way to
15370 The keyword `typename' is not permitted in a base-specifier or
15371 mem-initializer; in these contexts a qualified name that
15372 depends on a template-parameter is implicitly assumed to be a
15375 is to pretend that we have seen the `typename' keyword at this
15377 cp_parser_nested_name_specifier_opt (parser
,
15378 /*typename_keyword_p=*/true,
15379 /*check_dependency_p=*/true,
15381 /*is_declaration=*/true);
15382 /* If the base class is given by a qualified name, assume that names
15383 we see are type names or templates, as appropriate. */
15384 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
15385 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
15387 /* Finally, look for the class-name. */
15388 type
= cp_parser_class_name (parser
,
15392 /*check_dependency_p=*/true,
15393 /*class_head_p=*/false,
15394 /*is_declaration=*/true);
15396 if (type
== error_mark_node
)
15397 return error_mark_node
;
15399 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
15402 /* Exception handling [gram.exception] */
15404 /* Parse an (optional) exception-specification.
15406 exception-specification:
15407 throw ( type-id-list [opt] )
15409 Returns a TREE_LIST representing the exception-specification. The
15410 TREE_VALUE of each node is a type. */
15413 cp_parser_exception_specification_opt (cp_parser
* parser
)
15418 /* Peek at the next token. */
15419 token
= cp_lexer_peek_token (parser
->lexer
);
15420 /* If it's not `throw', then there's no exception-specification. */
15421 if (!cp_parser_is_keyword (token
, RID_THROW
))
15424 /* Consume the `throw'. */
15425 cp_lexer_consume_token (parser
->lexer
);
15427 /* Look for the `('. */
15428 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15430 /* Peek at the next token. */
15431 token
= cp_lexer_peek_token (parser
->lexer
);
15432 /* If it's not a `)', then there is a type-id-list. */
15433 if (token
->type
!= CPP_CLOSE_PAREN
)
15435 const char *saved_message
;
15437 /* Types may not be defined in an exception-specification. */
15438 saved_message
= parser
->type_definition_forbidden_message
;
15439 parser
->type_definition_forbidden_message
15440 = "types may not be defined in an exception-specification";
15441 /* Parse the type-id-list. */
15442 type_id_list
= cp_parser_type_id_list (parser
);
15443 /* Restore the saved message. */
15444 parser
->type_definition_forbidden_message
= saved_message
;
15447 type_id_list
= empty_except_spec
;
15449 /* Look for the `)'. */
15450 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15452 return type_id_list
;
15455 /* Parse an (optional) type-id-list.
15459 type-id-list , type-id ... [opt]
15461 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15462 in the order that the types were presented. */
15465 cp_parser_type_id_list (cp_parser
* parser
)
15467 tree types
= NULL_TREE
;
15474 /* Get the next type-id. */
15475 type
= cp_parser_type_id (parser
);
15476 /* Parse the optional ellipsis. */
15477 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15479 /* Consume the `...'. */
15480 cp_lexer_consume_token (parser
->lexer
);
15482 /* Turn the type into a pack expansion expression. */
15483 type
= make_pack_expansion (type
);
15485 /* Add it to the list. */
15486 types
= add_exception_specifier (types
, type
, /*complain=*/1);
15487 /* Peek at the next token. */
15488 token
= cp_lexer_peek_token (parser
->lexer
);
15489 /* If it is not a `,', we are done. */
15490 if (token
->type
!= CPP_COMMA
)
15492 /* Consume the `,'. */
15493 cp_lexer_consume_token (parser
->lexer
);
15496 return nreverse (types
);
15499 /* Parse a try-block.
15502 try compound-statement handler-seq */
15505 cp_parser_try_block (cp_parser
* parser
)
15509 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
15510 try_block
= begin_try_block ();
15511 cp_parser_compound_statement (parser
, NULL
, true);
15512 finish_try_block (try_block
);
15513 cp_parser_handler_seq (parser
);
15514 finish_handler_sequence (try_block
);
15519 /* Parse a function-try-block.
15521 function-try-block:
15522 try ctor-initializer [opt] function-body handler-seq */
15525 cp_parser_function_try_block (cp_parser
* parser
)
15527 tree compound_stmt
;
15529 bool ctor_initializer_p
;
15531 /* Look for the `try' keyword. */
15532 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
15534 /* Let the rest of the front end know where we are. */
15535 try_block
= begin_function_try_block (&compound_stmt
);
15536 /* Parse the function-body. */
15538 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
15539 /* We're done with the `try' part. */
15540 finish_function_try_block (try_block
);
15541 /* Parse the handlers. */
15542 cp_parser_handler_seq (parser
);
15543 /* We're done with the handlers. */
15544 finish_function_handler_sequence (try_block
, compound_stmt
);
15546 return ctor_initializer_p
;
15549 /* Parse a handler-seq.
15552 handler handler-seq [opt] */
15555 cp_parser_handler_seq (cp_parser
* parser
)
15561 /* Parse the handler. */
15562 cp_parser_handler (parser
);
15563 /* Peek at the next token. */
15564 token
= cp_lexer_peek_token (parser
->lexer
);
15565 /* If it's not `catch' then there are no more handlers. */
15566 if (!cp_parser_is_keyword (token
, RID_CATCH
))
15571 /* Parse a handler.
15574 catch ( exception-declaration ) compound-statement */
15577 cp_parser_handler (cp_parser
* parser
)
15582 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
15583 handler
= begin_handler ();
15584 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15585 declaration
= cp_parser_exception_declaration (parser
);
15586 finish_handler_parms (declaration
, handler
);
15587 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15588 cp_parser_compound_statement (parser
, NULL
, false);
15589 finish_handler (handler
);
15592 /* Parse an exception-declaration.
15594 exception-declaration:
15595 type-specifier-seq declarator
15596 type-specifier-seq abstract-declarator
15600 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15601 ellipsis variant is used. */
15604 cp_parser_exception_declaration (cp_parser
* parser
)
15606 cp_decl_specifier_seq type_specifiers
;
15607 cp_declarator
*declarator
;
15608 const char *saved_message
;
15610 /* If it's an ellipsis, it's easy to handle. */
15611 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15613 /* Consume the `...' token. */
15614 cp_lexer_consume_token (parser
->lexer
);
15618 /* Types may not be defined in exception-declarations. */
15619 saved_message
= parser
->type_definition_forbidden_message
;
15620 parser
->type_definition_forbidden_message
15621 = "types may not be defined in exception-declarations";
15623 /* Parse the type-specifier-seq. */
15624 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
15626 /* If it's a `)', then there is no declarator. */
15627 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
15630 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
15631 /*ctor_dtor_or_conv_p=*/NULL
,
15632 /*parenthesized_p=*/NULL
,
15633 /*member_p=*/false);
15635 /* Restore the saved message. */
15636 parser
->type_definition_forbidden_message
= saved_message
;
15638 if (!type_specifiers
.any_specifiers_p
)
15639 return error_mark_node
;
15641 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
15644 /* Parse a throw-expression.
15647 throw assignment-expression [opt]
15649 Returns a THROW_EXPR representing the throw-expression. */
15652 cp_parser_throw_expression (cp_parser
* parser
)
15657 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
15658 token
= cp_lexer_peek_token (parser
->lexer
);
15659 /* Figure out whether or not there is an assignment-expression
15660 following the "throw" keyword. */
15661 if (token
->type
== CPP_COMMA
15662 || token
->type
== CPP_SEMICOLON
15663 || token
->type
== CPP_CLOSE_PAREN
15664 || token
->type
== CPP_CLOSE_SQUARE
15665 || token
->type
== CPP_CLOSE_BRACE
15666 || token
->type
== CPP_COLON
)
15667 expression
= NULL_TREE
;
15669 expression
= cp_parser_assignment_expression (parser
,
15672 return build_throw (expression
);
15675 /* GNU Extensions */
15677 /* Parse an (optional) asm-specification.
15680 asm ( string-literal )
15682 If the asm-specification is present, returns a STRING_CST
15683 corresponding to the string-literal. Otherwise, returns
15687 cp_parser_asm_specification_opt (cp_parser
* parser
)
15690 tree asm_specification
;
15692 /* Peek at the next token. */
15693 token
= cp_lexer_peek_token (parser
->lexer
);
15694 /* If the next token isn't the `asm' keyword, then there's no
15695 asm-specification. */
15696 if (!cp_parser_is_keyword (token
, RID_ASM
))
15699 /* Consume the `asm' token. */
15700 cp_lexer_consume_token (parser
->lexer
);
15701 /* Look for the `('. */
15702 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15704 /* Look for the string-literal. */
15705 asm_specification
= cp_parser_string_literal (parser
, false, false);
15707 /* Look for the `)'. */
15708 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
15710 return asm_specification
;
15713 /* Parse an asm-operand-list.
15717 asm-operand-list , asm-operand
15720 string-literal ( expression )
15721 [ string-literal ] string-literal ( expression )
15723 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15724 each node is the expression. The TREE_PURPOSE is itself a
15725 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15726 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15727 is a STRING_CST for the string literal before the parenthesis. Returns
15728 ERROR_MARK_NODE if any of the operands are invalid. */
15731 cp_parser_asm_operand_list (cp_parser
* parser
)
15733 tree asm_operands
= NULL_TREE
;
15734 bool invalid_operands
= false;
15738 tree string_literal
;
15742 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
15744 /* Consume the `[' token. */
15745 cp_lexer_consume_token (parser
->lexer
);
15746 /* Read the operand name. */
15747 name
= cp_parser_identifier (parser
);
15748 if (name
!= error_mark_node
)
15749 name
= build_string (IDENTIFIER_LENGTH (name
),
15750 IDENTIFIER_POINTER (name
));
15751 /* Look for the closing `]'. */
15752 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
15756 /* Look for the string-literal. */
15757 string_literal
= cp_parser_string_literal (parser
, false, false);
15759 /* Look for the `('. */
15760 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15761 /* Parse the expression. */
15762 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
15763 /* Look for the `)'. */
15764 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15766 if (name
== error_mark_node
15767 || string_literal
== error_mark_node
15768 || expression
== error_mark_node
)
15769 invalid_operands
= true;
15771 /* Add this operand to the list. */
15772 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
15775 /* If the next token is not a `,', there are no more
15777 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15779 /* Consume the `,'. */
15780 cp_lexer_consume_token (parser
->lexer
);
15783 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
15786 /* Parse an asm-clobber-list.
15790 asm-clobber-list , string-literal
15792 Returns a TREE_LIST, indicating the clobbers in the order that they
15793 appeared. The TREE_VALUE of each node is a STRING_CST. */
15796 cp_parser_asm_clobber_list (cp_parser
* parser
)
15798 tree clobbers
= NULL_TREE
;
15802 tree string_literal
;
15804 /* Look for the string literal. */
15805 string_literal
= cp_parser_string_literal (parser
, false, false);
15806 /* Add it to the list. */
15807 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
15808 /* If the next token is not a `,', then the list is
15810 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15812 /* Consume the `,' token. */
15813 cp_lexer_consume_token (parser
->lexer
);
15819 /* Parse an (optional) series of attributes.
15822 attributes attribute
15825 __attribute__ (( attribute-list [opt] ))
15827 The return value is as for cp_parser_attribute_list. */
15830 cp_parser_attributes_opt (cp_parser
* parser
)
15832 tree attributes
= NULL_TREE
;
15837 tree attribute_list
;
15839 /* Peek at the next token. */
15840 token
= cp_lexer_peek_token (parser
->lexer
);
15841 /* If it's not `__attribute__', then we're done. */
15842 if (token
->keyword
!= RID_ATTRIBUTE
)
15845 /* Consume the `__attribute__' keyword. */
15846 cp_lexer_consume_token (parser
->lexer
);
15847 /* Look for the two `(' tokens. */
15848 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15849 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15851 /* Peek at the next token. */
15852 token
= cp_lexer_peek_token (parser
->lexer
);
15853 if (token
->type
!= CPP_CLOSE_PAREN
)
15854 /* Parse the attribute-list. */
15855 attribute_list
= cp_parser_attribute_list (parser
);
15857 /* If the next token is a `)', then there is no attribute
15859 attribute_list
= NULL
;
15861 /* Look for the two `)' tokens. */
15862 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15863 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15865 /* Add these new attributes to the list. */
15866 attributes
= chainon (attributes
, attribute_list
);
15872 /* Parse an attribute-list.
15876 attribute-list , attribute
15880 identifier ( identifier )
15881 identifier ( identifier , expression-list )
15882 identifier ( expression-list )
15884 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15885 to an attribute. The TREE_PURPOSE of each node is the identifier
15886 indicating which attribute is in use. The TREE_VALUE represents
15887 the arguments, if any. */
15890 cp_parser_attribute_list (cp_parser
* parser
)
15892 tree attribute_list
= NULL_TREE
;
15893 bool save_translate_strings_p
= parser
->translate_strings_p
;
15895 parser
->translate_strings_p
= false;
15902 /* Look for the identifier. We also allow keywords here; for
15903 example `__attribute__ ((const))' is legal. */
15904 token
= cp_lexer_peek_token (parser
->lexer
);
15905 if (token
->type
== CPP_NAME
15906 || token
->type
== CPP_KEYWORD
)
15908 tree arguments
= NULL_TREE
;
15910 /* Consume the token. */
15911 token
= cp_lexer_consume_token (parser
->lexer
);
15913 /* Save away the identifier that indicates which attribute
15915 identifier
= token
->u
.value
;
15916 attribute
= build_tree_list (identifier
, NULL_TREE
);
15918 /* Peek at the next token. */
15919 token
= cp_lexer_peek_token (parser
->lexer
);
15920 /* If it's an `(', then parse the attribute arguments. */
15921 if (token
->type
== CPP_OPEN_PAREN
)
15923 arguments
= cp_parser_parenthesized_expression_list
15924 (parser
, true, /*cast_p=*/false,
15925 /*allow_expansion_p=*/false,
15926 /*non_constant_p=*/NULL
);
15927 /* Save the arguments away. */
15928 TREE_VALUE (attribute
) = arguments
;
15931 if (arguments
!= error_mark_node
)
15933 /* Add this attribute to the list. */
15934 TREE_CHAIN (attribute
) = attribute_list
;
15935 attribute_list
= attribute
;
15938 token
= cp_lexer_peek_token (parser
->lexer
);
15940 /* Now, look for more attributes. If the next token isn't a
15941 `,', we're done. */
15942 if (token
->type
!= CPP_COMMA
)
15945 /* Consume the comma and keep going. */
15946 cp_lexer_consume_token (parser
->lexer
);
15948 parser
->translate_strings_p
= save_translate_strings_p
;
15950 /* We built up the list in reverse order. */
15951 return nreverse (attribute_list
);
15954 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15955 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15956 current value of the PEDANTIC flag, regardless of whether or not
15957 the `__extension__' keyword is present. The caller is responsible
15958 for restoring the value of the PEDANTIC flag. */
15961 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
15963 /* Save the old value of the PEDANTIC flag. */
15964 *saved_pedantic
= pedantic
;
15966 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
15968 /* Consume the `__extension__' token. */
15969 cp_lexer_consume_token (parser
->lexer
);
15970 /* We're not being pedantic while the `__extension__' keyword is
15980 /* Parse a label declaration.
15983 __label__ label-declarator-seq ;
15985 label-declarator-seq:
15986 identifier , label-declarator-seq
15990 cp_parser_label_declaration (cp_parser
* parser
)
15992 /* Look for the `__label__' keyword. */
15993 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
15999 /* Look for an identifier. */
16000 identifier
= cp_parser_identifier (parser
);
16001 /* If we failed, stop. */
16002 if (identifier
== error_mark_node
)
16004 /* Declare it as a label. */
16005 finish_label_decl (identifier
);
16006 /* If the next token is a `;', stop. */
16007 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
16009 /* Look for the `,' separating the label declarations. */
16010 cp_parser_require (parser
, CPP_COMMA
, "`,'");
16013 /* Look for the final `;'. */
16014 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
16017 /* Support Functions */
16019 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16020 NAME should have one of the representations used for an
16021 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16022 is returned. If PARSER->SCOPE is a dependent type, then a
16023 SCOPE_REF is returned.
16025 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16026 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16027 was formed. Abstractly, such entities should not be passed to this
16028 function, because they do not need to be looked up, but it is
16029 simpler to check for this special case here, rather than at the
16032 In cases not explicitly covered above, this function returns a
16033 DECL, OVERLOAD, or baselink representing the result of the lookup.
16034 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16037 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16038 (e.g., "struct") that was used. In that case bindings that do not
16039 refer to types are ignored.
16041 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16044 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16047 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16050 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16051 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16052 NULL_TREE otherwise. */
16055 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
16056 enum tag_types tag_type
,
16059 bool check_dependency
,
16060 tree
*ambiguous_decls
)
16064 tree object_type
= parser
->context
->object_type
;
16066 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
16067 flags
|= LOOKUP_COMPLAIN
;
16069 /* Assume that the lookup will be unambiguous. */
16070 if (ambiguous_decls
)
16071 *ambiguous_decls
= NULL_TREE
;
16073 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16074 no longer valid. Note that if we are parsing tentatively, and
16075 the parse fails, OBJECT_TYPE will be automatically restored. */
16076 parser
->context
->object_type
= NULL_TREE
;
16078 if (name
== error_mark_node
)
16079 return error_mark_node
;
16081 /* A template-id has already been resolved; there is no lookup to
16083 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
16085 if (BASELINK_P (name
))
16087 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
16088 == TEMPLATE_ID_EXPR
);
16092 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16093 it should already have been checked to make sure that the name
16094 used matches the type being destroyed. */
16095 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
16099 /* Figure out to which type this destructor applies. */
16101 type
= parser
->scope
;
16102 else if (object_type
)
16103 type
= object_type
;
16105 type
= current_class_type
;
16106 /* If that's not a class type, there is no destructor. */
16107 if (!type
|| !CLASS_TYPE_P (type
))
16108 return error_mark_node
;
16109 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
16110 lazily_declare_fn (sfk_destructor
, type
);
16111 if (!CLASSTYPE_DESTRUCTORS (type
))
16112 return error_mark_node
;
16113 /* If it was a class type, return the destructor. */
16114 return CLASSTYPE_DESTRUCTORS (type
);
16117 /* By this point, the NAME should be an ordinary identifier. If
16118 the id-expression was a qualified name, the qualifying scope is
16119 stored in PARSER->SCOPE at this point. */
16120 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
16122 /* Perform the lookup. */
16127 if (parser
->scope
== error_mark_node
)
16128 return error_mark_node
;
16130 /* If the SCOPE is dependent, the lookup must be deferred until
16131 the template is instantiated -- unless we are explicitly
16132 looking up names in uninstantiated templates. Even then, we
16133 cannot look up the name if the scope is not a class type; it
16134 might, for example, be a template type parameter. */
16135 dependent_p
= (TYPE_P (parser
->scope
)
16136 && !(parser
->in_declarator_p
16137 && currently_open_class (parser
->scope
))
16138 && dependent_type_p (parser
->scope
));
16139 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
16146 /* The resolution to Core Issue 180 says that `struct
16147 A::B' should be considered a type-name, even if `A'
16149 type
= make_typename_type (parser
->scope
, name
, tag_type
,
16150 /*complain=*/tf_error
);
16151 decl
= TYPE_NAME (type
);
16153 else if (is_template
16154 && (cp_parser_next_token_ends_template_argument_p (parser
)
16155 || cp_lexer_next_token_is (parser
->lexer
,
16157 decl
= make_unbound_class_template (parser
->scope
,
16159 /*complain=*/tf_error
);
16161 decl
= build_qualified_name (/*type=*/NULL_TREE
,
16162 parser
->scope
, name
,
16167 tree pushed_scope
= NULL_TREE
;
16169 /* If PARSER->SCOPE is a dependent type, then it must be a
16170 class type, and we must not be checking dependencies;
16171 otherwise, we would have processed this lookup above. So
16172 that PARSER->SCOPE is not considered a dependent base by
16173 lookup_member, we must enter the scope here. */
16175 pushed_scope
= push_scope (parser
->scope
);
16176 /* If the PARSER->SCOPE is a template specialization, it
16177 may be instantiated during name lookup. In that case,
16178 errors may be issued. Even if we rollback the current
16179 tentative parse, those errors are valid. */
16180 decl
= lookup_qualified_name (parser
->scope
, name
,
16181 tag_type
!= none_type
,
16182 /*complain=*/true);
16184 pop_scope (pushed_scope
);
16186 parser
->qualifying_scope
= parser
->scope
;
16187 parser
->object_scope
= NULL_TREE
;
16189 else if (object_type
)
16191 tree object_decl
= NULL_TREE
;
16192 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16193 OBJECT_TYPE is not a class. */
16194 if (CLASS_TYPE_P (object_type
))
16195 /* If the OBJECT_TYPE is a template specialization, it may
16196 be instantiated during name lookup. In that case, errors
16197 may be issued. Even if we rollback the current tentative
16198 parse, those errors are valid. */
16199 object_decl
= lookup_member (object_type
,
16202 tag_type
!= none_type
);
16203 /* Look it up in the enclosing context, too. */
16204 decl
= lookup_name_real (name
, tag_type
!= none_type
,
16206 /*block_p=*/true, is_namespace
, flags
);
16207 parser
->object_scope
= object_type
;
16208 parser
->qualifying_scope
= NULL_TREE
;
16210 decl
= object_decl
;
16214 decl
= lookup_name_real (name
, tag_type
!= none_type
,
16216 /*block_p=*/true, is_namespace
, flags
);
16217 parser
->qualifying_scope
= NULL_TREE
;
16218 parser
->object_scope
= NULL_TREE
;
16221 /* If the lookup failed, let our caller know. */
16222 if (!decl
|| decl
== error_mark_node
)
16223 return error_mark_node
;
16225 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16226 if (TREE_CODE (decl
) == TREE_LIST
)
16228 if (ambiguous_decls
)
16229 *ambiguous_decls
= decl
;
16230 /* The error message we have to print is too complicated for
16231 cp_parser_error, so we incorporate its actions directly. */
16232 if (!cp_parser_simulate_error (parser
))
16234 error ("reference to %qD is ambiguous", name
);
16235 print_candidates (decl
);
16237 return error_mark_node
;
16240 gcc_assert (DECL_P (decl
)
16241 || TREE_CODE (decl
) == OVERLOAD
16242 || TREE_CODE (decl
) == SCOPE_REF
16243 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
16244 || BASELINK_P (decl
));
16246 /* If we have resolved the name of a member declaration, check to
16247 see if the declaration is accessible. When the name resolves to
16248 set of overloaded functions, accessibility is checked when
16249 overload resolution is done.
16251 During an explicit instantiation, access is not checked at all,
16252 as per [temp.explicit]. */
16254 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
16259 /* Like cp_parser_lookup_name, but for use in the typical case where
16260 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16261 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16264 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
16266 return cp_parser_lookup_name (parser
, name
,
16268 /*is_template=*/false,
16269 /*is_namespace=*/false,
16270 /*check_dependency=*/true,
16271 /*ambiguous_decls=*/NULL
);
16274 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16275 the current context, return the TYPE_DECL. If TAG_NAME_P is
16276 true, the DECL indicates the class being defined in a class-head,
16277 or declared in an elaborated-type-specifier.
16279 Otherwise, return DECL. */
16282 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
16284 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16285 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16288 template <typename T> struct B;
16291 template <typename T> struct A::B {};
16293 Similarly, in an elaborated-type-specifier:
16295 namespace N { struct X{}; }
16298 template <typename T> friend struct N::X;
16301 However, if the DECL refers to a class type, and we are in
16302 the scope of the class, then the name lookup automatically
16303 finds the TYPE_DECL created by build_self_reference rather
16304 than a TEMPLATE_DECL. For example, in:
16306 template <class T> struct S {
16310 there is no need to handle such case. */
16312 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
16313 return DECL_TEMPLATE_RESULT (decl
);
16318 /* If too many, or too few, template-parameter lists apply to the
16319 declarator, issue an error message. Returns TRUE if all went well,
16320 and FALSE otherwise. */
16323 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
16324 cp_declarator
*declarator
)
16326 unsigned num_templates
;
16328 /* We haven't seen any classes that involve template parameters yet. */
16331 switch (declarator
->kind
)
16334 if (declarator
->u
.id
.qualifying_scope
)
16339 scope
= declarator
->u
.id
.qualifying_scope
;
16340 member
= declarator
->u
.id
.unqualified_name
;
16342 while (scope
&& CLASS_TYPE_P (scope
))
16344 /* You're supposed to have one `template <...>'
16345 for every template class, but you don't need one
16346 for a full specialization. For example:
16348 template <class T> struct S{};
16349 template <> struct S<int> { void f(); };
16350 void S<int>::f () {}
16352 is correct; there shouldn't be a `template <>' for
16353 the definition of `S<int>::f'. */
16354 if (!CLASSTYPE_TEMPLATE_INFO (scope
))
16355 /* If SCOPE does not have template information of any
16356 kind, then it is not a template, nor is it nested
16357 within a template. */
16359 if (explicit_class_specialization_p (scope
))
16361 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
16364 scope
= TYPE_CONTEXT (scope
);
16367 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
16368 == TEMPLATE_ID_EXPR
)
16369 /* If the DECLARATOR has the form `X<y>' then it uses one
16370 additional level of template parameters. */
16373 return cp_parser_check_template_parameters (parser
,
16379 case cdk_reference
:
16381 return (cp_parser_check_declarator_template_parameters
16382 (parser
, declarator
->declarator
));
16388 gcc_unreachable ();
16393 /* NUM_TEMPLATES were used in the current declaration. If that is
16394 invalid, return FALSE and issue an error messages. Otherwise,
16398 cp_parser_check_template_parameters (cp_parser
* parser
,
16399 unsigned num_templates
)
16401 /* If there are more template classes than parameter lists, we have
16404 template <class T> void S<T>::R<T>::f (); */
16405 if (parser
->num_template_parameter_lists
< num_templates
)
16407 error ("too few template-parameter-lists");
16410 /* If there are the same number of template classes and parameter
16411 lists, that's OK. */
16412 if (parser
->num_template_parameter_lists
== num_templates
)
16414 /* If there are more, but only one more, then we are referring to a
16415 member template. That's OK too. */
16416 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
16418 /* Otherwise, there are too many template parameter lists. We have
16421 template <class T> template <class U> void S::f(); */
16422 error ("too many template-parameter-lists");
16426 /* Parse an optional `::' token indicating that the following name is
16427 from the global namespace. If so, PARSER->SCOPE is set to the
16428 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16429 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16430 Returns the new value of PARSER->SCOPE, if the `::' token is
16431 present, and NULL_TREE otherwise. */
16434 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
16438 /* Peek at the next token. */
16439 token
= cp_lexer_peek_token (parser
->lexer
);
16440 /* If we're looking at a `::' token then we're starting from the
16441 global namespace, not our current location. */
16442 if (token
->type
== CPP_SCOPE
)
16444 /* Consume the `::' token. */
16445 cp_lexer_consume_token (parser
->lexer
);
16446 /* Set the SCOPE so that we know where to start the lookup. */
16447 parser
->scope
= global_namespace
;
16448 parser
->qualifying_scope
= global_namespace
;
16449 parser
->object_scope
= NULL_TREE
;
16451 return parser
->scope
;
16453 else if (!current_scope_valid_p
)
16455 parser
->scope
= NULL_TREE
;
16456 parser
->qualifying_scope
= NULL_TREE
;
16457 parser
->object_scope
= NULL_TREE
;
16463 /* Returns TRUE if the upcoming token sequence is the start of a
16464 constructor declarator. If FRIEND_P is true, the declarator is
16465 preceded by the `friend' specifier. */
16468 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
16470 bool constructor_p
;
16471 tree type_decl
= NULL_TREE
;
16472 bool nested_name_p
;
16473 cp_token
*next_token
;
16475 /* The common case is that this is not a constructor declarator, so
16476 try to avoid doing lots of work if at all possible. It's not
16477 valid declare a constructor at function scope. */
16478 if (parser
->in_function_body
)
16480 /* And only certain tokens can begin a constructor declarator. */
16481 next_token
= cp_lexer_peek_token (parser
->lexer
);
16482 if (next_token
->type
!= CPP_NAME
16483 && next_token
->type
!= CPP_SCOPE
16484 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
16485 && next_token
->type
!= CPP_TEMPLATE_ID
)
16488 /* Parse tentatively; we are going to roll back all of the tokens
16490 cp_parser_parse_tentatively (parser
);
16491 /* Assume that we are looking at a constructor declarator. */
16492 constructor_p
= true;
16494 /* Look for the optional `::' operator. */
16495 cp_parser_global_scope_opt (parser
,
16496 /*current_scope_valid_p=*/false);
16497 /* Look for the nested-name-specifier. */
16499 = (cp_parser_nested_name_specifier_opt (parser
,
16500 /*typename_keyword_p=*/false,
16501 /*check_dependency_p=*/false,
16503 /*is_declaration=*/false)
16505 /* Outside of a class-specifier, there must be a
16506 nested-name-specifier. */
16507 if (!nested_name_p
&&
16508 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
16510 constructor_p
= false;
16511 /* If we still think that this might be a constructor-declarator,
16512 look for a class-name. */
16517 template <typename T> struct S { S(); };
16518 template <typename T> S<T>::S ();
16520 we must recognize that the nested `S' names a class.
16523 template <typename T> S<T>::S<T> ();
16525 we must recognize that the nested `S' names a template. */
16526 type_decl
= cp_parser_class_name (parser
,
16527 /*typename_keyword_p=*/false,
16528 /*template_keyword_p=*/false,
16530 /*check_dependency_p=*/false,
16531 /*class_head_p=*/false,
16532 /*is_declaration=*/false);
16533 /* If there was no class-name, then this is not a constructor. */
16534 constructor_p
= !cp_parser_error_occurred (parser
);
16537 /* If we're still considering a constructor, we have to see a `(',
16538 to begin the parameter-declaration-clause, followed by either a
16539 `)', an `...', or a decl-specifier. We need to check for a
16540 type-specifier to avoid being fooled into thinking that:
16544 is a constructor. (It is actually a function named `f' that
16545 takes one parameter (of type `int') and returns a value of type
16548 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
16550 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
16551 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
16552 /* A parameter declaration begins with a decl-specifier,
16553 which is either the "attribute" keyword, a storage class
16554 specifier, or (usually) a type-specifier. */
16555 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
16558 tree pushed_scope
= NULL_TREE
;
16559 unsigned saved_num_template_parameter_lists
;
16561 /* Names appearing in the type-specifier should be looked up
16562 in the scope of the class. */
16563 if (current_class_type
)
16567 type
= TREE_TYPE (type_decl
);
16568 if (TREE_CODE (type
) == TYPENAME_TYPE
)
16570 type
= resolve_typename_type (type
,
16571 /*only_current_p=*/false);
16572 if (TREE_CODE (type
) == TYPENAME_TYPE
)
16574 cp_parser_abort_tentative_parse (parser
);
16578 pushed_scope
= push_scope (type
);
16581 /* Inside the constructor parameter list, surrounding
16582 template-parameter-lists do not apply. */
16583 saved_num_template_parameter_lists
16584 = parser
->num_template_parameter_lists
;
16585 parser
->num_template_parameter_lists
= 0;
16587 /* Look for the type-specifier. */
16588 cp_parser_type_specifier (parser
,
16589 CP_PARSER_FLAGS_NONE
,
16590 /*decl_specs=*/NULL
,
16591 /*is_declarator=*/true,
16592 /*declares_class_or_enum=*/NULL
,
16593 /*is_cv_qualifier=*/NULL
);
16595 parser
->num_template_parameter_lists
16596 = saved_num_template_parameter_lists
;
16598 /* Leave the scope of the class. */
16600 pop_scope (pushed_scope
);
16602 constructor_p
= !cp_parser_error_occurred (parser
);
16606 constructor_p
= false;
16607 /* We did not really want to consume any tokens. */
16608 cp_parser_abort_tentative_parse (parser
);
16610 return constructor_p
;
16613 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16614 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16615 they must be performed once we are in the scope of the function.
16617 Returns the function defined. */
16620 cp_parser_function_definition_from_specifiers_and_declarator
16621 (cp_parser
* parser
,
16622 cp_decl_specifier_seq
*decl_specifiers
,
16624 const cp_declarator
*declarator
)
16629 /* Begin the function-definition. */
16630 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
16632 /* The things we're about to see are not directly qualified by any
16633 template headers we've seen thus far. */
16634 reset_specialization ();
16636 /* If there were names looked up in the decl-specifier-seq that we
16637 did not check, check them now. We must wait until we are in the
16638 scope of the function to perform the checks, since the function
16639 might be a friend. */
16640 perform_deferred_access_checks ();
16644 /* Skip the entire function. */
16645 cp_parser_skip_to_end_of_block_or_statement (parser
);
16646 fn
= error_mark_node
;
16648 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
16650 /* Seen already, skip it. An error message has already been output. */
16651 cp_parser_skip_to_end_of_block_or_statement (parser
);
16652 fn
= current_function_decl
;
16653 current_function_decl
= NULL_TREE
;
16654 /* If this is a function from a class, pop the nested class. */
16655 if (current_class_name
)
16656 pop_nested_class ();
16659 fn
= cp_parser_function_definition_after_declarator (parser
,
16660 /*inline_p=*/false);
16665 /* Parse the part of a function-definition that follows the
16666 declarator. INLINE_P is TRUE iff this function is an inline
16667 function defined with a class-specifier.
16669 Returns the function defined. */
16672 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
16676 bool ctor_initializer_p
= false;
16677 bool saved_in_unbraced_linkage_specification_p
;
16678 bool saved_in_function_body
;
16679 unsigned saved_num_template_parameter_lists
;
16681 saved_in_function_body
= parser
->in_function_body
;
16682 parser
->in_function_body
= true;
16683 /* If the next token is `return', then the code may be trying to
16684 make use of the "named return value" extension that G++ used to
16686 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
16688 /* Consume the `return' keyword. */
16689 cp_lexer_consume_token (parser
->lexer
);
16690 /* Look for the identifier that indicates what value is to be
16692 cp_parser_identifier (parser
);
16693 /* Issue an error message. */
16694 error ("named return values are no longer supported");
16695 /* Skip tokens until we reach the start of the function body. */
16698 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16699 if (token
->type
== CPP_OPEN_BRACE
16700 || token
->type
== CPP_EOF
16701 || token
->type
== CPP_PRAGMA_EOL
)
16703 cp_lexer_consume_token (parser
->lexer
);
16706 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16707 anything declared inside `f'. */
16708 saved_in_unbraced_linkage_specification_p
16709 = parser
->in_unbraced_linkage_specification_p
;
16710 parser
->in_unbraced_linkage_specification_p
= false;
16711 /* Inside the function, surrounding template-parameter-lists do not
16713 saved_num_template_parameter_lists
16714 = parser
->num_template_parameter_lists
;
16715 parser
->num_template_parameter_lists
= 0;
16716 /* If the next token is `try', then we are looking at a
16717 function-try-block. */
16718 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
16719 ctor_initializer_p
= cp_parser_function_try_block (parser
);
16720 /* A function-try-block includes the function-body, so we only do
16721 this next part if we're not processing a function-try-block. */
16724 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
16726 /* Finish the function. */
16727 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
16728 (inline_p
? 2 : 0));
16729 /* Generate code for it, if necessary. */
16730 expand_or_defer_fn (fn
);
16731 /* Restore the saved values. */
16732 parser
->in_unbraced_linkage_specification_p
16733 = saved_in_unbraced_linkage_specification_p
;
16734 parser
->num_template_parameter_lists
16735 = saved_num_template_parameter_lists
;
16736 parser
->in_function_body
= saved_in_function_body
;
16741 /* Parse a template-declaration, assuming that the `export' (and
16742 `extern') keywords, if present, has already been scanned. MEMBER_P
16743 is as for cp_parser_template_declaration. */
16746 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
16748 tree decl
= NULL_TREE
;
16749 VEC (deferred_access_check
,gc
) *checks
;
16750 tree parameter_list
;
16751 bool friend_p
= false;
16752 bool need_lang_pop
;
16754 /* Look for the `template' keyword. */
16755 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
16759 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
16761 if (at_class_scope_p () && current_function_decl
)
16763 /* 14.5.2.2 [temp.mem]
16765 A local class shall not have member templates. */
16766 error ("invalid declaration of member template in local class");
16767 cp_parser_skip_to_end_of_block_or_statement (parser
);
16772 A template ... shall not have C linkage. */
16773 if (current_lang_name
== lang_name_c
)
16775 error ("template with C linkage");
16776 /* Give it C++ linkage to avoid confusing other parts of the
16778 push_lang_context (lang_name_cplusplus
);
16779 need_lang_pop
= true;
16782 need_lang_pop
= false;
16784 /* We cannot perform access checks on the template parameter
16785 declarations until we know what is being declared, just as we
16786 cannot check the decl-specifier list. */
16787 push_deferring_access_checks (dk_deferred
);
16789 /* If the next token is `>', then we have an invalid
16790 specialization. Rather than complain about an invalid template
16791 parameter, issue an error message here. */
16792 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
16794 cp_parser_error (parser
, "invalid explicit specialization");
16795 begin_specialization ();
16796 parameter_list
= NULL_TREE
;
16799 /* Parse the template parameters. */
16800 parameter_list
= cp_parser_template_parameter_list (parser
);
16802 /* Get the deferred access checks from the parameter list. These
16803 will be checked once we know what is being declared, as for a
16804 member template the checks must be performed in the scope of the
16805 class containing the member. */
16806 checks
= get_deferred_access_checks ();
16808 /* Look for the `>'. */
16809 cp_parser_skip_to_end_of_template_parameter_list (parser
);
16810 /* We just processed one more parameter list. */
16811 ++parser
->num_template_parameter_lists
;
16812 /* If the next token is `template', there are more template
16814 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
16816 cp_parser_template_declaration_after_export (parser
, member_p
);
16819 /* There are no access checks when parsing a template, as we do not
16820 know if a specialization will be a friend. */
16821 push_deferring_access_checks (dk_no_check
);
16822 decl
= cp_parser_single_declaration (parser
,
16825 /*explicit_specialization_p=*/false,
16827 pop_deferring_access_checks ();
16829 /* If this is a member template declaration, let the front
16831 if (member_p
&& !friend_p
&& decl
)
16833 if (TREE_CODE (decl
) == TYPE_DECL
)
16834 cp_parser_check_access_in_redeclaration (decl
);
16836 decl
= finish_member_template_decl (decl
);
16838 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
16839 make_friend_class (current_class_type
, TREE_TYPE (decl
),
16840 /*complain=*/true);
16842 /* We are done with the current parameter list. */
16843 --parser
->num_template_parameter_lists
;
16845 pop_deferring_access_checks ();
16848 finish_template_decl (parameter_list
);
16850 /* Register member declarations. */
16851 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
16852 finish_member_declaration (decl
);
16853 /* For the erroneous case of a template with C linkage, we pushed an
16854 implicit C++ linkage scope; exit that scope now. */
16856 pop_lang_context ();
16857 /* If DECL is a function template, we must return to parse it later.
16858 (Even though there is no definition, there might be default
16859 arguments that need handling.) */
16860 if (member_p
&& decl
16861 && (TREE_CODE (decl
) == FUNCTION_DECL
16862 || DECL_FUNCTION_TEMPLATE_P (decl
)))
16863 TREE_VALUE (parser
->unparsed_functions_queues
)
16864 = tree_cons (NULL_TREE
, decl
,
16865 TREE_VALUE (parser
->unparsed_functions_queues
));
16868 /* Perform the deferred access checks from a template-parameter-list.
16869 CHECKS is a TREE_LIST of access checks, as returned by
16870 get_deferred_access_checks. */
16873 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check
,gc
)* checks
)
16875 ++processing_template_parmlist
;
16876 perform_access_checks (checks
);
16877 --processing_template_parmlist
;
16880 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16881 `function-definition' sequence. MEMBER_P is true, this declaration
16882 appears in a class scope.
16884 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16885 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16888 cp_parser_single_declaration (cp_parser
* parser
,
16889 VEC (deferred_access_check
,gc
)* checks
,
16891 bool explicit_specialization_p
,
16894 int declares_class_or_enum
;
16895 tree decl
= NULL_TREE
;
16896 cp_decl_specifier_seq decl_specifiers
;
16897 bool function_definition_p
= false;
16899 /* This function is only used when processing a template
16901 gcc_assert (innermost_scope_kind () == sk_template_parms
16902 || innermost_scope_kind () == sk_template_spec
);
16904 /* Defer access checks until we know what is being declared. */
16905 push_deferring_access_checks (dk_deferred
);
16907 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16909 cp_parser_decl_specifier_seq (parser
,
16910 CP_PARSER_FLAGS_OPTIONAL
,
16912 &declares_class_or_enum
);
16914 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
16916 /* There are no template typedefs. */
16917 if (decl_specifiers
.specs
[(int) ds_typedef
])
16919 error ("template declaration of %qs", "typedef");
16920 decl
= error_mark_node
;
16923 /* Gather up the access checks that occurred the
16924 decl-specifier-seq. */
16925 stop_deferring_access_checks ();
16927 /* Check for the declaration of a template class. */
16928 if (declares_class_or_enum
)
16930 if (cp_parser_declares_only_class_p (parser
))
16932 decl
= shadow_tag (&decl_specifiers
);
16937 friend template <typename T> struct A<T>::B;
16940 A<T>::B will be represented by a TYPENAME_TYPE, and
16941 therefore not recognized by shadow_tag. */
16942 if (friend_p
&& *friend_p
16944 && decl_specifiers
.type
16945 && TYPE_P (decl_specifiers
.type
))
16946 decl
= decl_specifiers
.type
;
16948 if (decl
&& decl
!= error_mark_node
)
16949 decl
= TYPE_NAME (decl
);
16951 decl
= error_mark_node
;
16953 /* Perform access checks for template parameters. */
16954 cp_parser_perform_template_parameter_access_checks (checks
);
16957 /* If it's not a template class, try for a template function. If
16958 the next token is a `;', then this declaration does not declare
16959 anything. But, if there were errors in the decl-specifiers, then
16960 the error might well have come from an attempted class-specifier.
16961 In that case, there's no need to warn about a missing declarator. */
16963 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
16964 || decl_specifiers
.type
!= error_mark_node
))
16966 decl
= cp_parser_init_declarator (parser
,
16969 /*function_definition_allowed_p=*/true,
16971 declares_class_or_enum
,
16972 &function_definition_p
);
16974 /* 7.1.1-1 [dcl.stc]
16976 A storage-class-specifier shall not be specified in an explicit
16977 specialization... */
16979 && explicit_specialization_p
16980 && decl_specifiers
.storage_class
!= sc_none
)
16982 error ("explicit template specialization cannot have a storage class");
16983 decl
= error_mark_node
;
16987 pop_deferring_access_checks ();
16989 /* Clear any current qualification; whatever comes next is the start
16990 of something new. */
16991 parser
->scope
= NULL_TREE
;
16992 parser
->qualifying_scope
= NULL_TREE
;
16993 parser
->object_scope
= NULL_TREE
;
16994 /* Look for a trailing `;' after the declaration. */
16995 if (!function_definition_p
16996 && (decl
== error_mark_node
16997 || !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")))
16998 cp_parser_skip_to_end_of_block_or_statement (parser
);
17003 /* Parse a cast-expression that is not the operand of a unary "&". */
17006 cp_parser_simple_cast_expression (cp_parser
*parser
)
17008 return cp_parser_cast_expression (parser
, /*address_p=*/false,
17012 /* Parse a functional cast to TYPE. Returns an expression
17013 representing the cast. */
17016 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
17018 tree expression_list
;
17022 = cp_parser_parenthesized_expression_list (parser
, false,
17024 /*allow_expansion_p=*/true,
17025 /*non_constant_p=*/NULL
);
17027 cast
= build_functional_cast (type
, expression_list
);
17028 /* [expr.const]/1: In an integral constant expression "only type
17029 conversions to integral or enumeration type can be used". */
17030 if (TREE_CODE (type
) == TYPE_DECL
)
17031 type
= TREE_TYPE (type
);
17032 if (cast
!= error_mark_node
17033 && !cast_valid_in_integral_constant_expression_p (type
)
17034 && (cp_parser_non_integral_constant_expression
17035 (parser
, "a call to a constructor")))
17036 return error_mark_node
;
17040 /* Save the tokens that make up the body of a member function defined
17041 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17042 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17043 specifiers applied to the declaration. Returns the FUNCTION_DECL
17044 for the member function. */
17047 cp_parser_save_member_function_body (cp_parser
* parser
,
17048 cp_decl_specifier_seq
*decl_specifiers
,
17049 cp_declarator
*declarator
,
17056 /* Create the function-declaration. */
17057 fn
= start_method (decl_specifiers
, declarator
, attributes
);
17058 /* If something went badly wrong, bail out now. */
17059 if (fn
== error_mark_node
)
17061 /* If there's a function-body, skip it. */
17062 if (cp_parser_token_starts_function_definition_p
17063 (cp_lexer_peek_token (parser
->lexer
)))
17064 cp_parser_skip_to_end_of_block_or_statement (parser
);
17065 return error_mark_node
;
17068 /* Remember it, if there default args to post process. */
17069 cp_parser_save_default_args (parser
, fn
);
17071 /* Save away the tokens that make up the body of the
17073 first
= parser
->lexer
->next_token
;
17074 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
17075 /* Handle function try blocks. */
17076 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
17077 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
17078 last
= parser
->lexer
->next_token
;
17080 /* Save away the inline definition; we will process it when the
17081 class is complete. */
17082 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
17083 DECL_PENDING_INLINE_P (fn
) = 1;
17085 /* We need to know that this was defined in the class, so that
17086 friend templates are handled correctly. */
17087 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
17089 /* We're done with the inline definition. */
17090 finish_method (fn
);
17092 /* Add FN to the queue of functions to be parsed later. */
17093 TREE_VALUE (parser
->unparsed_functions_queues
)
17094 = tree_cons (NULL_TREE
, fn
,
17095 TREE_VALUE (parser
->unparsed_functions_queues
));
17100 /* Parse a template-argument-list, as well as the trailing ">" (but
17101 not the opening ">"). See cp_parser_template_argument_list for the
17105 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
17109 tree saved_qualifying_scope
;
17110 tree saved_object_scope
;
17111 bool saved_greater_than_is_operator_p
;
17112 bool saved_skip_evaluation
;
17116 When parsing a template-id, the first non-nested `>' is taken as
17117 the end of the template-argument-list rather than a greater-than
17119 saved_greater_than_is_operator_p
17120 = parser
->greater_than_is_operator_p
;
17121 parser
->greater_than_is_operator_p
= false;
17122 /* Parsing the argument list may modify SCOPE, so we save it
17124 saved_scope
= parser
->scope
;
17125 saved_qualifying_scope
= parser
->qualifying_scope
;
17126 saved_object_scope
= parser
->object_scope
;
17127 /* We need to evaluate the template arguments, even though this
17128 template-id may be nested within a "sizeof". */
17129 saved_skip_evaluation
= skip_evaluation
;
17130 skip_evaluation
= false;
17131 /* Parse the template-argument-list itself. */
17132 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
17133 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
17134 arguments
= NULL_TREE
;
17136 arguments
= cp_parser_template_argument_list (parser
);
17137 /* Look for the `>' that ends the template-argument-list. If we find
17138 a '>>' instead, it's probably just a typo. */
17139 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
17141 if (cxx_dialect
!= cxx98
)
17143 /* In C++0x, a `>>' in a template argument list or cast
17144 expression is considered to be two separate `>'
17145 tokens. So, change the current token to a `>', but don't
17146 consume it: it will be consumed later when the outer
17147 template argument list (or cast expression) is parsed.
17148 Note that this replacement of `>' for `>>' is necessary
17149 even if we are parsing tentatively: in the tentative
17150 case, after calling
17151 cp_parser_enclosed_template_argument_list we will always
17152 throw away all of the template arguments and the first
17153 closing `>', either because the template argument list
17154 was erroneous or because we are replacing those tokens
17155 with a CPP_TEMPLATE_ID token. The second `>' (which will
17156 not have been thrown away) is needed either to close an
17157 outer template argument list or to complete a new-style
17159 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17160 token
->type
= CPP_GREATER
;
17162 else if (!saved_greater_than_is_operator_p
)
17164 /* If we're in a nested template argument list, the '>>' has
17165 to be a typo for '> >'. We emit the error message, but we
17166 continue parsing and we push a '>' as next token, so that
17167 the argument list will be parsed correctly. Note that the
17168 global source location is still on the token before the
17169 '>>', so we need to say explicitly where we want it. */
17170 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17171 error ("%H%<>>%> should be %<> >%> "
17172 "within a nested template argument list",
17175 token
->type
= CPP_GREATER
;
17179 /* If this is not a nested template argument list, the '>>'
17180 is a typo for '>'. Emit an error message and continue.
17181 Same deal about the token location, but here we can get it
17182 right by consuming the '>>' before issuing the diagnostic. */
17183 cp_lexer_consume_token (parser
->lexer
);
17184 error ("spurious %<>>%>, use %<>%> to terminate "
17185 "a template argument list");
17189 cp_parser_skip_to_end_of_template_parameter_list (parser
);
17190 /* The `>' token might be a greater-than operator again now. */
17191 parser
->greater_than_is_operator_p
17192 = saved_greater_than_is_operator_p
;
17193 /* Restore the SAVED_SCOPE. */
17194 parser
->scope
= saved_scope
;
17195 parser
->qualifying_scope
= saved_qualifying_scope
;
17196 parser
->object_scope
= saved_object_scope
;
17197 skip_evaluation
= saved_skip_evaluation
;
17202 /* MEMBER_FUNCTION is a member function, or a friend. If default
17203 arguments, or the body of the function have not yet been parsed,
17207 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
17209 /* If this member is a template, get the underlying
17211 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
17212 member_function
= DECL_TEMPLATE_RESULT (member_function
);
17214 /* There should not be any class definitions in progress at this
17215 point; the bodies of members are only parsed outside of all class
17217 gcc_assert (parser
->num_classes_being_defined
== 0);
17218 /* While we're parsing the member functions we might encounter more
17219 classes. We want to handle them right away, but we don't want
17220 them getting mixed up with functions that are currently in the
17222 parser
->unparsed_functions_queues
17223 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
17225 /* Make sure that any template parameters are in scope. */
17226 maybe_begin_member_template_processing (member_function
);
17228 /* If the body of the function has not yet been parsed, parse it
17230 if (DECL_PENDING_INLINE_P (member_function
))
17232 tree function_scope
;
17233 cp_token_cache
*tokens
;
17235 /* The function is no longer pending; we are processing it. */
17236 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
17237 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
17238 DECL_PENDING_INLINE_P (member_function
) = 0;
17240 /* If this is a local class, enter the scope of the containing
17242 function_scope
= current_function_decl
;
17243 if (function_scope
)
17244 push_function_context_to (function_scope
);
17247 /* Push the body of the function onto the lexer stack. */
17248 cp_parser_push_lexer_for_tokens (parser
, tokens
);
17250 /* Let the front end know that we going to be defining this
17252 start_preparsed_function (member_function
, NULL_TREE
,
17253 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
17255 /* Don't do access checking if it is a templated function. */
17256 if (processing_template_decl
)
17257 push_deferring_access_checks (dk_no_check
);
17259 /* Now, parse the body of the function. */
17260 cp_parser_function_definition_after_declarator (parser
,
17261 /*inline_p=*/true);
17263 if (processing_template_decl
)
17264 pop_deferring_access_checks ();
17266 /* Leave the scope of the containing function. */
17267 if (function_scope
)
17268 pop_function_context_from (function_scope
);
17269 cp_parser_pop_lexer (parser
);
17272 /* Remove any template parameters from the symbol table. */
17273 maybe_end_member_template_processing ();
17275 /* Restore the queue. */
17276 parser
->unparsed_functions_queues
17277 = TREE_CHAIN (parser
->unparsed_functions_queues
);
17280 /* If DECL contains any default args, remember it on the unparsed
17281 functions queue. */
17284 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
17288 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
17290 probe
= TREE_CHAIN (probe
))
17291 if (TREE_PURPOSE (probe
))
17293 TREE_PURPOSE (parser
->unparsed_functions_queues
)
17294 = tree_cons (current_class_type
, decl
,
17295 TREE_PURPOSE (parser
->unparsed_functions_queues
));
17300 /* FN is a FUNCTION_DECL which may contains a parameter with an
17301 unparsed DEFAULT_ARG. Parse the default args now. This function
17302 assumes that the current scope is the scope in which the default
17303 argument should be processed. */
17306 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
17308 bool saved_local_variables_forbidden_p
;
17311 /* While we're parsing the default args, we might (due to the
17312 statement expression extension) encounter more classes. We want
17313 to handle them right away, but we don't want them getting mixed
17314 up with default args that are currently in the queue. */
17315 parser
->unparsed_functions_queues
17316 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
17318 /* Local variable names (and the `this' keyword) may not appear
17319 in a default argument. */
17320 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
17321 parser
->local_variables_forbidden_p
= true;
17323 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
17325 parm
= TREE_CHAIN (parm
))
17327 cp_token_cache
*tokens
;
17328 tree default_arg
= TREE_PURPOSE (parm
);
17330 VEC(tree
,gc
) *insts
;
17337 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
17338 /* This can happen for a friend declaration for a function
17339 already declared with default arguments. */
17342 /* Push the saved tokens for the default argument onto the parser's
17344 tokens
= DEFARG_TOKENS (default_arg
);
17345 cp_parser_push_lexer_for_tokens (parser
, tokens
);
17347 /* Parse the assignment-expression. */
17348 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
17350 if (!processing_template_decl
)
17351 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
17353 TREE_PURPOSE (parm
) = parsed_arg
;
17355 /* Update any instantiations we've already created. */
17356 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
17357 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
17358 TREE_PURPOSE (copy
) = parsed_arg
;
17360 /* If the token stream has not been completely used up, then
17361 there was extra junk after the end of the default
17363 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
17364 cp_parser_error (parser
, "expected %<,%>");
17366 /* Revert to the main lexer. */
17367 cp_parser_pop_lexer (parser
);
17370 /* Make sure no default arg is missing. */
17371 check_default_args (fn
);
17373 /* Restore the state of local_variables_forbidden_p. */
17374 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
17376 /* Restore the queue. */
17377 parser
->unparsed_functions_queues
17378 = TREE_CHAIN (parser
->unparsed_functions_queues
);
17381 /* Parse the operand of `sizeof' (or a similar operator). Returns
17382 either a TYPE or an expression, depending on the form of the
17383 input. The KEYWORD indicates which kind of expression we have
17387 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
17389 static const char *format
;
17390 tree expr
= NULL_TREE
;
17391 const char *saved_message
;
17393 bool saved_integral_constant_expression_p
;
17394 bool saved_non_integral_constant_expression_p
;
17395 bool pack_expansion_p
= false;
17397 /* Initialize FORMAT the first time we get here. */
17399 format
= "types may not be defined in '%s' expressions";
17401 /* Types cannot be defined in a `sizeof' expression. Save away the
17403 saved_message
= parser
->type_definition_forbidden_message
;
17404 /* And create the new one. */
17405 parser
->type_definition_forbidden_message
= tmp
17406 = XNEWVEC (char, strlen (format
)
17407 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
17409 sprintf (tmp
, format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
17411 /* The restrictions on constant-expressions do not apply inside
17412 sizeof expressions. */
17413 saved_integral_constant_expression_p
17414 = parser
->integral_constant_expression_p
;
17415 saved_non_integral_constant_expression_p
17416 = parser
->non_integral_constant_expression_p
;
17417 parser
->integral_constant_expression_p
= false;
17419 /* If it's a `...', then we are computing the length of a parameter
17421 if (keyword
== RID_SIZEOF
17422 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17424 /* Consume the `...'. */
17425 cp_lexer_consume_token (parser
->lexer
);
17426 maybe_warn_variadic_templates ();
17428 /* Note that this is an expansion. */
17429 pack_expansion_p
= true;
17432 /* Do not actually evaluate the expression. */
17434 /* If it's a `(', then we might be looking at the type-id
17436 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17439 bool saved_in_type_id_in_expr_p
;
17441 /* We can't be sure yet whether we're looking at a type-id or an
17443 cp_parser_parse_tentatively (parser
);
17444 /* Consume the `('. */
17445 cp_lexer_consume_token (parser
->lexer
);
17446 /* Parse the type-id. */
17447 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
17448 parser
->in_type_id_in_expr_p
= true;
17449 type
= cp_parser_type_id (parser
);
17450 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
17451 /* Now, look for the trailing `)'. */
17452 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
17453 /* If all went well, then we're done. */
17454 if (cp_parser_parse_definitely (parser
))
17456 cp_decl_specifier_seq decl_specs
;
17458 /* Build a trivial decl-specifier-seq. */
17459 clear_decl_specs (&decl_specs
);
17460 decl_specs
.type
= type
;
17462 /* Call grokdeclarator to figure out what type this is. */
17463 expr
= grokdeclarator (NULL
,
17467 /*attrlist=*/NULL
);
17471 /* If the type-id production did not work out, then we must be
17472 looking at the unary-expression production. */
17474 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
17477 if (pack_expansion_p
)
17478 /* Build a pack expansion. */
17479 expr
= make_pack_expansion (expr
);
17481 /* Go back to evaluating expressions. */
17484 /* Free the message we created. */
17486 /* And restore the old one. */
17487 parser
->type_definition_forbidden_message
= saved_message
;
17488 parser
->integral_constant_expression_p
17489 = saved_integral_constant_expression_p
;
17490 parser
->non_integral_constant_expression_p
17491 = saved_non_integral_constant_expression_p
;
17496 /* If the current declaration has no declarator, return true. */
17499 cp_parser_declares_only_class_p (cp_parser
*parser
)
17501 /* If the next token is a `;' or a `,' then there is no
17503 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17504 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
17507 /* Update the DECL_SPECS to reflect the storage class indicated by
17511 cp_parser_set_storage_class (cp_parser
*parser
,
17512 cp_decl_specifier_seq
*decl_specs
,
17515 cp_storage_class storage_class
;
17517 if (parser
->in_unbraced_linkage_specification_p
)
17519 error ("invalid use of %qD in linkage specification",
17520 ridpointers
[keyword
]);
17523 else if (decl_specs
->storage_class
!= sc_none
)
17525 decl_specs
->conflicting_specifiers_p
= true;
17529 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
17530 && decl_specs
->specs
[(int) ds_thread
])
17532 error ("%<__thread%> before %qD", ridpointers
[keyword
]);
17533 decl_specs
->specs
[(int) ds_thread
] = 0;
17539 storage_class
= sc_auto
;
17542 storage_class
= sc_register
;
17545 storage_class
= sc_static
;
17548 storage_class
= sc_extern
;
17551 storage_class
= sc_mutable
;
17554 gcc_unreachable ();
17556 decl_specs
->storage_class
= storage_class
;
17558 /* A storage class specifier cannot be applied alongside a typedef
17559 specifier. If there is a typedef specifier present then set
17560 conflicting_specifiers_p which will trigger an error later
17561 on in grokdeclarator. */
17562 if (decl_specs
->specs
[(int)ds_typedef
])
17563 decl_specs
->conflicting_specifiers_p
= true;
17566 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17567 is true, the type is a user-defined type; otherwise it is a
17568 built-in type specified by a keyword. */
17571 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
17573 bool user_defined_p
)
17575 decl_specs
->any_specifiers_p
= true;
17577 /* If the user tries to redeclare bool or wchar_t (with, for
17578 example, in "typedef int wchar_t;") we remember that this is what
17579 happened. In system headers, we ignore these declarations so
17580 that G++ can work with system headers that are not C++-safe. */
17581 if (decl_specs
->specs
[(int) ds_typedef
]
17583 && (type_spec
== boolean_type_node
17584 || type_spec
== wchar_type_node
)
17585 && (decl_specs
->type
17586 || decl_specs
->specs
[(int) ds_long
]
17587 || decl_specs
->specs
[(int) ds_short
]
17588 || decl_specs
->specs
[(int) ds_unsigned
]
17589 || decl_specs
->specs
[(int) ds_signed
]))
17591 decl_specs
->redefined_builtin_type
= type_spec
;
17592 if (!decl_specs
->type
)
17594 decl_specs
->type
= type_spec
;
17595 decl_specs
->user_defined_type_p
= false;
17598 else if (decl_specs
->type
)
17599 decl_specs
->multiple_types_p
= true;
17602 decl_specs
->type
= type_spec
;
17603 decl_specs
->user_defined_type_p
= user_defined_p
;
17604 decl_specs
->redefined_builtin_type
= NULL_TREE
;
17608 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17609 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17612 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
17614 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
17617 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17618 issue an error message indicating that TOKEN_DESC was expected.
17620 Returns the token consumed, if the token had the appropriate type.
17621 Otherwise, returns NULL. */
17624 cp_parser_require (cp_parser
* parser
,
17625 enum cpp_ttype type
,
17626 const char* token_desc
)
17628 if (cp_lexer_next_token_is (parser
->lexer
, type
))
17629 return cp_lexer_consume_token (parser
->lexer
);
17632 /* Output the MESSAGE -- unless we're parsing tentatively. */
17633 if (!cp_parser_simulate_error (parser
))
17635 char *message
= concat ("expected ", token_desc
, NULL
);
17636 cp_parser_error (parser
, message
);
17643 /* An error message is produced if the next token is not '>'.
17644 All further tokens are skipped until the desired token is
17645 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17648 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
17650 /* Current level of '< ... >'. */
17651 unsigned level
= 0;
17652 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17653 unsigned nesting_depth
= 0;
17655 /* Are we ready, yet? If not, issue error message. */
17656 if (cp_parser_require (parser
, CPP_GREATER
, "%<>%>"))
17659 /* Skip tokens until the desired token is found. */
17662 /* Peek at the next token. */
17663 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
17666 if (!nesting_depth
)
17671 if (cxx_dialect
== cxx98
)
17672 /* C++0x views the `>>' operator as two `>' tokens, but
17675 else if (!nesting_depth
&& level
-- == 0)
17677 /* We've hit a `>>' where the first `>' closes the
17678 template argument list, and the second `>' is
17679 spurious. Just consume the `>>' and stop; we've
17680 already produced at least one error. */
17681 cp_lexer_consume_token (parser
->lexer
);
17684 /* Fall through for C++0x, so we handle the second `>' in
17688 if (!nesting_depth
&& level
-- == 0)
17690 /* We've reached the token we want, consume it and stop. */
17691 cp_lexer_consume_token (parser
->lexer
);
17696 case CPP_OPEN_PAREN
:
17697 case CPP_OPEN_SQUARE
:
17701 case CPP_CLOSE_PAREN
:
17702 case CPP_CLOSE_SQUARE
:
17703 if (nesting_depth
-- == 0)
17708 case CPP_PRAGMA_EOL
:
17709 case CPP_SEMICOLON
:
17710 case CPP_OPEN_BRACE
:
17711 case CPP_CLOSE_BRACE
:
17712 /* The '>' was probably forgotten, don't look further. */
17719 /* Consume this token. */
17720 cp_lexer_consume_token (parser
->lexer
);
17724 /* If the next token is the indicated keyword, consume it. Otherwise,
17725 issue an error message indicating that TOKEN_DESC was expected.
17727 Returns the token consumed, if the token had the appropriate type.
17728 Otherwise, returns NULL. */
17731 cp_parser_require_keyword (cp_parser
* parser
,
17733 const char* token_desc
)
17735 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
17737 if (token
&& token
->keyword
!= keyword
)
17739 dyn_string_t error_msg
;
17741 /* Format the error message. */
17742 error_msg
= dyn_string_new (0);
17743 dyn_string_append_cstr (error_msg
, "expected ");
17744 dyn_string_append_cstr (error_msg
, token_desc
);
17745 cp_parser_error (parser
, error_msg
->s
);
17746 dyn_string_delete (error_msg
);
17753 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17754 function-definition. */
17757 cp_parser_token_starts_function_definition_p (cp_token
* token
)
17759 return (/* An ordinary function-body begins with an `{'. */
17760 token
->type
== CPP_OPEN_BRACE
17761 /* A ctor-initializer begins with a `:'. */
17762 || token
->type
== CPP_COLON
17763 /* A function-try-block begins with `try'. */
17764 || token
->keyword
== RID_TRY
17765 /* The named return value extension begins with `return'. */
17766 || token
->keyword
== RID_RETURN
);
17769 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17773 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
17777 token
= cp_lexer_peek_token (parser
->lexer
);
17778 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
17781 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17782 C++0x) ending a template-argument. */
17785 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
17789 token
= cp_lexer_peek_token (parser
->lexer
);
17790 return (token
->type
== CPP_COMMA
17791 || token
->type
== CPP_GREATER
17792 || token
->type
== CPP_ELLIPSIS
17793 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
17796 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17797 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17800 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
17805 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
17806 if (token
->type
== CPP_LESS
)
17808 /* Check for the sequence `<::' in the original code. It would be lexed as
17809 `[:', where `[' is a digraph, and there is no whitespace before
17811 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
17814 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
17815 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
17821 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17822 or none_type otherwise. */
17824 static enum tag_types
17825 cp_parser_token_is_class_key (cp_token
* token
)
17827 switch (token
->keyword
)
17832 return record_type
;
17841 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17844 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
17846 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
17847 pedwarn ("%qs tag used in naming %q#T",
17848 class_key
== union_type
? "union"
17849 : class_key
== record_type
? "struct" : "class",
17853 /* Issue an error message if DECL is redeclared with different
17854 access than its original declaration [class.access.spec/3].
17855 This applies to nested classes and nested class templates.
17859 cp_parser_check_access_in_redeclaration (tree decl
)
17861 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
17864 if ((TREE_PRIVATE (decl
)
17865 != (current_access_specifier
== access_private_node
))
17866 || (TREE_PROTECTED (decl
)
17867 != (current_access_specifier
== access_protected_node
)))
17868 error ("%qD redeclared with different access", decl
);
17871 /* Look for the `template' keyword, as a syntactic disambiguator.
17872 Return TRUE iff it is present, in which case it will be
17876 cp_parser_optional_template_keyword (cp_parser
*parser
)
17878 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17880 /* The `template' keyword can only be used within templates;
17881 outside templates the parser can always figure out what is a
17882 template and what is not. */
17883 if (!processing_template_decl
)
17885 error ("%<template%> (as a disambiguator) is only allowed "
17886 "within templates");
17887 /* If this part of the token stream is rescanned, the same
17888 error message would be generated. So, we purge the token
17889 from the stream. */
17890 cp_lexer_purge_token (parser
->lexer
);
17895 /* Consume the `template' keyword. */
17896 cp_lexer_consume_token (parser
->lexer
);
17904 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17905 set PARSER->SCOPE, and perform other related actions. */
17908 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
17911 struct tree_check
*check_value
;
17912 deferred_access_check
*chk
;
17913 VEC (deferred_access_check
,gc
) *checks
;
17915 /* Get the stored value. */
17916 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
17917 /* Perform any access checks that were deferred. */
17918 checks
= check_value
->checks
;
17922 VEC_iterate (deferred_access_check
, checks
, i
, chk
) ;
17925 perform_or_defer_access_check (chk
->binfo
,
17930 /* Set the scope from the stored value. */
17931 parser
->scope
= check_value
->value
;
17932 parser
->qualifying_scope
= check_value
->qualifying_scope
;
17933 parser
->object_scope
= NULL_TREE
;
17936 /* Consume tokens up through a non-nested END token. */
17939 cp_parser_cache_group (cp_parser
*parser
,
17940 enum cpp_ttype end
,
17947 /* Abort a parenthesized expression if we encounter a brace. */
17948 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
17949 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17951 /* If we've reached the end of the file, stop. */
17952 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
)
17953 || (end
!= CPP_PRAGMA_EOL
17954 && cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)))
17956 /* Consume the next token. */
17957 token
= cp_lexer_consume_token (parser
->lexer
);
17958 /* See if it starts a new group. */
17959 if (token
->type
== CPP_OPEN_BRACE
)
17961 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
17965 else if (token
->type
== CPP_OPEN_PAREN
)
17966 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
17967 else if (token
->type
== CPP_PRAGMA
)
17968 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
17969 else if (token
->type
== end
)
17974 /* Begin parsing tentatively. We always save tokens while parsing
17975 tentatively so that if the tentative parsing fails we can restore the
17979 cp_parser_parse_tentatively (cp_parser
* parser
)
17981 /* Enter a new parsing context. */
17982 parser
->context
= cp_parser_context_new (parser
->context
);
17983 /* Begin saving tokens. */
17984 cp_lexer_save_tokens (parser
->lexer
);
17985 /* In order to avoid repetitive access control error messages,
17986 access checks are queued up until we are no longer parsing
17988 push_deferring_access_checks (dk_deferred
);
17991 /* Commit to the currently active tentative parse. */
17994 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
17996 cp_parser_context
*context
;
17999 /* Mark all of the levels as committed. */
18000 lexer
= parser
->lexer
;
18001 for (context
= parser
->context
; context
->next
; context
= context
->next
)
18003 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
18005 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
18006 while (!cp_lexer_saving_tokens (lexer
))
18007 lexer
= lexer
->next
;
18008 cp_lexer_commit_tokens (lexer
);
18012 /* Abort the currently active tentative parse. All consumed tokens
18013 will be rolled back, and no diagnostics will be issued. */
18016 cp_parser_abort_tentative_parse (cp_parser
* parser
)
18018 cp_parser_simulate_error (parser
);
18019 /* Now, pretend that we want to see if the construct was
18020 successfully parsed. */
18021 cp_parser_parse_definitely (parser
);
18024 /* Stop parsing tentatively. If a parse error has occurred, restore the
18025 token stream. Otherwise, commit to the tokens we have consumed.
18026 Returns true if no error occurred; false otherwise. */
18029 cp_parser_parse_definitely (cp_parser
* parser
)
18031 bool error_occurred
;
18032 cp_parser_context
*context
;
18034 /* Remember whether or not an error occurred, since we are about to
18035 destroy that information. */
18036 error_occurred
= cp_parser_error_occurred (parser
);
18037 /* Remove the topmost context from the stack. */
18038 context
= parser
->context
;
18039 parser
->context
= context
->next
;
18040 /* If no parse errors occurred, commit to the tentative parse. */
18041 if (!error_occurred
)
18043 /* Commit to the tokens read tentatively, unless that was
18045 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
18046 cp_lexer_commit_tokens (parser
->lexer
);
18048 pop_to_parent_deferring_access_checks ();
18050 /* Otherwise, if errors occurred, roll back our state so that things
18051 are just as they were before we began the tentative parse. */
18054 cp_lexer_rollback_tokens (parser
->lexer
);
18055 pop_deferring_access_checks ();
18057 /* Add the context to the front of the free list. */
18058 context
->next
= cp_parser_context_free_list
;
18059 cp_parser_context_free_list
= context
;
18061 return !error_occurred
;
18064 /* Returns true if we are parsing tentatively and are not committed to
18065 this tentative parse. */
18068 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
18070 return (cp_parser_parsing_tentatively (parser
)
18071 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
18074 /* Returns nonzero iff an error has occurred during the most recent
18075 tentative parse. */
18078 cp_parser_error_occurred (cp_parser
* parser
)
18080 return (cp_parser_parsing_tentatively (parser
)
18081 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
18084 /* Returns nonzero if GNU extensions are allowed. */
18087 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
18089 return parser
->allow_gnu_extensions_p
;
18092 /* Objective-C++ Productions */
18095 /* Parse an Objective-C expression, which feeds into a primary-expression
18099 objc-message-expression
18100 objc-string-literal
18101 objc-encode-expression
18102 objc-protocol-expression
18103 objc-selector-expression
18105 Returns a tree representation of the expression. */
18108 cp_parser_objc_expression (cp_parser
* parser
)
18110 /* Try to figure out what kind of declaration is present. */
18111 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
18115 case CPP_OPEN_SQUARE
:
18116 return cp_parser_objc_message_expression (parser
);
18118 case CPP_OBJC_STRING
:
18119 kwd
= cp_lexer_consume_token (parser
->lexer
);
18120 return objc_build_string_object (kwd
->u
.value
);
18123 switch (kwd
->keyword
)
18125 case RID_AT_ENCODE
:
18126 return cp_parser_objc_encode_expression (parser
);
18128 case RID_AT_PROTOCOL
:
18129 return cp_parser_objc_protocol_expression (parser
);
18131 case RID_AT_SELECTOR
:
18132 return cp_parser_objc_selector_expression (parser
);
18138 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->u
.value
);
18139 cp_parser_skip_to_end_of_block_or_statement (parser
);
18142 return error_mark_node
;
18145 /* Parse an Objective-C message expression.
18147 objc-message-expression:
18148 [ objc-message-receiver objc-message-args ]
18150 Returns a representation of an Objective-C message. */
18153 cp_parser_objc_message_expression (cp_parser
* parser
)
18155 tree receiver
, messageargs
;
18157 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
18158 receiver
= cp_parser_objc_message_receiver (parser
);
18159 messageargs
= cp_parser_objc_message_args (parser
);
18160 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
18162 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
18165 /* Parse an objc-message-receiver.
18167 objc-message-receiver:
18169 simple-type-specifier
18171 Returns a representation of the type or expression. */
18174 cp_parser_objc_message_receiver (cp_parser
* parser
)
18178 /* An Objective-C message receiver may be either (1) a type
18179 or (2) an expression. */
18180 cp_parser_parse_tentatively (parser
);
18181 rcv
= cp_parser_expression (parser
, false);
18183 if (cp_parser_parse_definitely (parser
))
18186 rcv
= cp_parser_simple_type_specifier (parser
,
18187 /*decl_specs=*/NULL
,
18188 CP_PARSER_FLAGS_NONE
);
18190 return objc_get_class_reference (rcv
);
18193 /* Parse the arguments and selectors comprising an Objective-C message.
18198 objc-selector-args , objc-comma-args
18200 objc-selector-args:
18201 objc-selector [opt] : assignment-expression
18202 objc-selector-args objc-selector [opt] : assignment-expression
18205 assignment-expression
18206 objc-comma-args , assignment-expression
18208 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18209 selector arguments and TREE_VALUE containing a list of comma
18213 cp_parser_objc_message_args (cp_parser
* parser
)
18215 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
18216 bool maybe_unary_selector_p
= true;
18217 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18219 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
18221 tree selector
= NULL_TREE
, arg
;
18223 if (token
->type
!= CPP_COLON
)
18224 selector
= cp_parser_objc_selector (parser
);
18226 /* Detect if we have a unary selector. */
18227 if (maybe_unary_selector_p
18228 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
18229 return build_tree_list (selector
, NULL_TREE
);
18231 maybe_unary_selector_p
= false;
18232 cp_parser_require (parser
, CPP_COLON
, "`:'");
18233 arg
= cp_parser_assignment_expression (parser
, false);
18236 = chainon (sel_args
,
18237 build_tree_list (selector
, arg
));
18239 token
= cp_lexer_peek_token (parser
->lexer
);
18242 /* Handle non-selector arguments, if any. */
18243 while (token
->type
== CPP_COMMA
)
18247 cp_lexer_consume_token (parser
->lexer
);
18248 arg
= cp_parser_assignment_expression (parser
, false);
18251 = chainon (addl_args
,
18252 build_tree_list (NULL_TREE
, arg
));
18254 token
= cp_lexer_peek_token (parser
->lexer
);
18257 return build_tree_list (sel_args
, addl_args
);
18260 /* Parse an Objective-C encode expression.
18262 objc-encode-expression:
18263 @encode objc-typename
18265 Returns an encoded representation of the type argument. */
18268 cp_parser_objc_encode_expression (cp_parser
* parser
)
18272 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
18273 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
18274 type
= complete_type (cp_parser_type_id (parser
));
18275 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18279 error ("%<@encode%> must specify a type as an argument");
18280 return error_mark_node
;
18283 return objc_build_encode_expr (type
);
18286 /* Parse an Objective-C @defs expression. */
18289 cp_parser_objc_defs_expression (cp_parser
*parser
)
18293 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
18294 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
18295 name
= cp_parser_identifier (parser
);
18296 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18298 return objc_get_class_ivars (name
);
18301 /* Parse an Objective-C protocol expression.
18303 objc-protocol-expression:
18304 @protocol ( identifier )
18306 Returns a representation of the protocol expression. */
18309 cp_parser_objc_protocol_expression (cp_parser
* parser
)
18313 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
18314 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
18315 proto
= cp_parser_identifier (parser
);
18316 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18318 return objc_build_protocol_expr (proto
);
18321 /* Parse an Objective-C selector expression.
18323 objc-selector-expression:
18324 @selector ( objc-method-signature )
18326 objc-method-signature:
18332 objc-selector-seq objc-selector :
18334 Returns a representation of the method selector. */
18337 cp_parser_objc_selector_expression (cp_parser
* parser
)
18339 tree sel_seq
= NULL_TREE
;
18340 bool maybe_unary_selector_p
= true;
18343 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
18344 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
18345 token
= cp_lexer_peek_token (parser
->lexer
);
18347 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
18348 || token
->type
== CPP_SCOPE
)
18350 tree selector
= NULL_TREE
;
18352 if (token
->type
!= CPP_COLON
18353 || token
->type
== CPP_SCOPE
)
18354 selector
= cp_parser_objc_selector (parser
);
18356 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
18357 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18359 /* Detect if we have a unary selector. */
18360 if (maybe_unary_selector_p
)
18362 sel_seq
= selector
;
18363 goto finish_selector
;
18367 cp_parser_error (parser
, "expected %<:%>");
18370 maybe_unary_selector_p
= false;
18371 token
= cp_lexer_consume_token (parser
->lexer
);
18373 if (token
->type
== CPP_SCOPE
)
18376 = chainon (sel_seq
,
18377 build_tree_list (selector
, NULL_TREE
));
18379 = chainon (sel_seq
,
18380 build_tree_list (NULL_TREE
, NULL_TREE
));
18384 = chainon (sel_seq
,
18385 build_tree_list (selector
, NULL_TREE
));
18387 token
= cp_lexer_peek_token (parser
->lexer
);
18391 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18393 return objc_build_selector_expr (sel_seq
);
18396 /* Parse a list of identifiers.
18398 objc-identifier-list:
18400 objc-identifier-list , identifier
18402 Returns a TREE_LIST of identifier nodes. */
18405 cp_parser_objc_identifier_list (cp_parser
* parser
)
18407 tree list
= build_tree_list (NULL_TREE
, cp_parser_identifier (parser
));
18408 cp_token
*sep
= cp_lexer_peek_token (parser
->lexer
);
18410 while (sep
->type
== CPP_COMMA
)
18412 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
18413 list
= chainon (list
,
18414 build_tree_list (NULL_TREE
,
18415 cp_parser_identifier (parser
)));
18416 sep
= cp_lexer_peek_token (parser
->lexer
);
18422 /* Parse an Objective-C alias declaration.
18424 objc-alias-declaration:
18425 @compatibility_alias identifier identifier ;
18427 This function registers the alias mapping with the Objective-C front end.
18428 It returns nothing. */
18431 cp_parser_objc_alias_declaration (cp_parser
* parser
)
18435 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
18436 alias
= cp_parser_identifier (parser
);
18437 orig
= cp_parser_identifier (parser
);
18438 objc_declare_alias (alias
, orig
);
18439 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18442 /* Parse an Objective-C class forward-declaration.
18444 objc-class-declaration:
18445 @class objc-identifier-list ;
18447 The function registers the forward declarations with the Objective-C
18448 front end. It returns nothing. */
18451 cp_parser_objc_class_declaration (cp_parser
* parser
)
18453 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
18454 objc_declare_class (cp_parser_objc_identifier_list (parser
));
18455 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18458 /* Parse a list of Objective-C protocol references.
18460 objc-protocol-refs-opt:
18461 objc-protocol-refs [opt]
18463 objc-protocol-refs:
18464 < objc-identifier-list >
18466 Returns a TREE_LIST of identifiers, if any. */
18469 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
18471 tree protorefs
= NULL_TREE
;
18473 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
18475 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
18476 protorefs
= cp_parser_objc_identifier_list (parser
);
18477 cp_parser_require (parser
, CPP_GREATER
, "`>'");
18483 /* Parse a Objective-C visibility specification. */
18486 cp_parser_objc_visibility_spec (cp_parser
* parser
)
18488 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
18490 switch (vis
->keyword
)
18492 case RID_AT_PRIVATE
:
18493 objc_set_visibility (2);
18495 case RID_AT_PROTECTED
:
18496 objc_set_visibility (0);
18498 case RID_AT_PUBLIC
:
18499 objc_set_visibility (1);
18505 /* Eat '@private'/'@protected'/'@public'. */
18506 cp_lexer_consume_token (parser
->lexer
);
18509 /* Parse an Objective-C method type. */
18512 cp_parser_objc_method_type (cp_parser
* parser
)
18514 objc_set_method_type
18515 (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
18520 /* Parse an Objective-C protocol qualifier. */
18523 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
18525 tree quals
= NULL_TREE
, node
;
18526 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18528 node
= token
->u
.value
;
18530 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
18531 && (node
== ridpointers
[(int) RID_IN
]
18532 || node
== ridpointers
[(int) RID_OUT
]
18533 || node
== ridpointers
[(int) RID_INOUT
]
18534 || node
== ridpointers
[(int) RID_BYCOPY
]
18535 || node
== ridpointers
[(int) RID_BYREF
]
18536 || node
== ridpointers
[(int) RID_ONEWAY
]))
18538 quals
= tree_cons (NULL_TREE
, node
, quals
);
18539 cp_lexer_consume_token (parser
->lexer
);
18540 token
= cp_lexer_peek_token (parser
->lexer
);
18541 node
= token
->u
.value
;
18547 /* Parse an Objective-C typename. */
18550 cp_parser_objc_typename (cp_parser
* parser
)
18552 tree typename
= NULL_TREE
;
18554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18556 tree proto_quals
, cp_type
= NULL_TREE
;
18558 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
18559 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
18561 /* An ObjC type name may consist of just protocol qualifiers, in which
18562 case the type shall default to 'id'. */
18563 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
18564 cp_type
= cp_parser_type_id (parser
);
18566 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18567 typename
= build_tree_list (proto_quals
, cp_type
);
18573 /* Check to see if TYPE refers to an Objective-C selector name. */
18576 cp_parser_objc_selector_p (enum cpp_ttype type
)
18578 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
18579 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
18580 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
18581 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
18582 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
18585 /* Parse an Objective-C selector. */
18588 cp_parser_objc_selector (cp_parser
* parser
)
18590 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
18592 if (!cp_parser_objc_selector_p (token
->type
))
18594 error ("invalid Objective-C++ selector name");
18595 return error_mark_node
;
18598 /* C++ operator names are allowed to appear in ObjC selectors. */
18599 switch (token
->type
)
18601 case CPP_AND_AND
: return get_identifier ("and");
18602 case CPP_AND_EQ
: return get_identifier ("and_eq");
18603 case CPP_AND
: return get_identifier ("bitand");
18604 case CPP_OR
: return get_identifier ("bitor");
18605 case CPP_COMPL
: return get_identifier ("compl");
18606 case CPP_NOT
: return get_identifier ("not");
18607 case CPP_NOT_EQ
: return get_identifier ("not_eq");
18608 case CPP_OR_OR
: return get_identifier ("or");
18609 case CPP_OR_EQ
: return get_identifier ("or_eq");
18610 case CPP_XOR
: return get_identifier ("xor");
18611 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
18612 default: return token
->u
.value
;
18616 /* Parse an Objective-C params list. */
18619 cp_parser_objc_method_keyword_params (cp_parser
* parser
)
18621 tree params
= NULL_TREE
;
18622 bool maybe_unary_selector_p
= true;
18623 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18625 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
18627 tree selector
= NULL_TREE
, typename
, identifier
;
18629 if (token
->type
!= CPP_COLON
)
18630 selector
= cp_parser_objc_selector (parser
);
18632 /* Detect if we have a unary selector. */
18633 if (maybe_unary_selector_p
18634 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
18637 maybe_unary_selector_p
= false;
18638 cp_parser_require (parser
, CPP_COLON
, "`:'");
18639 typename
= cp_parser_objc_typename (parser
);
18640 identifier
= cp_parser_identifier (parser
);
18644 objc_build_keyword_decl (selector
,
18648 token
= cp_lexer_peek_token (parser
->lexer
);
18654 /* Parse the non-keyword Objective-C params. */
18657 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
)
18659 tree params
= make_node (TREE_LIST
);
18660 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18661 *ellipsisp
= false; /* Initially, assume no ellipsis. */
18663 while (token
->type
== CPP_COMMA
)
18665 cp_parameter_declarator
*parmdecl
;
18668 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
18669 token
= cp_lexer_peek_token (parser
->lexer
);
18671 if (token
->type
== CPP_ELLIPSIS
)
18673 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
18678 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
18679 parm
= grokdeclarator (parmdecl
->declarator
,
18680 &parmdecl
->decl_specifiers
,
18681 PARM
, /*initialized=*/0,
18682 /*attrlist=*/NULL
);
18684 chainon (params
, build_tree_list (NULL_TREE
, parm
));
18685 token
= cp_lexer_peek_token (parser
->lexer
);
18691 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18694 cp_parser_objc_interstitial_code (cp_parser
* parser
)
18696 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18698 /* If the next token is `extern' and the following token is a string
18699 literal, then we have a linkage specification. */
18700 if (token
->keyword
== RID_EXTERN
18701 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
18702 cp_parser_linkage_specification (parser
);
18703 /* Handle #pragma, if any. */
18704 else if (token
->type
== CPP_PRAGMA
)
18705 cp_parser_pragma (parser
, pragma_external
);
18706 /* Allow stray semicolons. */
18707 else if (token
->type
== CPP_SEMICOLON
)
18708 cp_lexer_consume_token (parser
->lexer
);
18709 /* Finally, try to parse a block-declaration, or a function-definition. */
18711 cp_parser_block_declaration (parser
, /*statement_p=*/false);
18714 /* Parse a method signature. */
18717 cp_parser_objc_method_signature (cp_parser
* parser
)
18719 tree rettype
, kwdparms
, optparms
;
18720 bool ellipsis
= false;
18722 cp_parser_objc_method_type (parser
);
18723 rettype
= cp_parser_objc_typename (parser
);
18724 kwdparms
= cp_parser_objc_method_keyword_params (parser
);
18725 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
);
18727 return objc_build_method_signature (rettype
, kwdparms
, optparms
, ellipsis
);
18730 /* Pars an Objective-C method prototype list. */
18733 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
18735 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18737 while (token
->keyword
!= RID_AT_END
)
18739 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
18741 objc_add_method_declaration
18742 (cp_parser_objc_method_signature (parser
));
18743 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18746 /* Allow for interspersed non-ObjC++ code. */
18747 cp_parser_objc_interstitial_code (parser
);
18749 token
= cp_lexer_peek_token (parser
->lexer
);
18752 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
18753 objc_finish_interface ();
18756 /* Parse an Objective-C method definition list. */
18759 cp_parser_objc_method_definition_list (cp_parser
* parser
)
18761 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18763 while (token
->keyword
!= RID_AT_END
)
18767 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
18769 push_deferring_access_checks (dk_deferred
);
18770 objc_start_method_definition
18771 (cp_parser_objc_method_signature (parser
));
18773 /* For historical reasons, we accept an optional semicolon. */
18774 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18775 cp_lexer_consume_token (parser
->lexer
);
18777 perform_deferred_access_checks ();
18778 stop_deferring_access_checks ();
18779 meth
= cp_parser_function_definition_after_declarator (parser
,
18781 pop_deferring_access_checks ();
18782 objc_finish_method_definition (meth
);
18785 /* Allow for interspersed non-ObjC++ code. */
18786 cp_parser_objc_interstitial_code (parser
);
18788 token
= cp_lexer_peek_token (parser
->lexer
);
18791 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
18792 objc_finish_implementation ();
18795 /* Parse Objective-C ivars. */
18798 cp_parser_objc_class_ivars (cp_parser
* parser
)
18800 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18802 if (token
->type
!= CPP_OPEN_BRACE
)
18803 return; /* No ivars specified. */
18805 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
18806 token
= cp_lexer_peek_token (parser
->lexer
);
18808 while (token
->type
!= CPP_CLOSE_BRACE
)
18810 cp_decl_specifier_seq declspecs
;
18811 int decl_class_or_enum_p
;
18812 tree prefix_attributes
;
18814 cp_parser_objc_visibility_spec (parser
);
18816 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18819 cp_parser_decl_specifier_seq (parser
,
18820 CP_PARSER_FLAGS_OPTIONAL
,
18822 &decl_class_or_enum_p
);
18823 prefix_attributes
= declspecs
.attributes
;
18824 declspecs
.attributes
= NULL_TREE
;
18826 /* Keep going until we hit the `;' at the end of the
18828 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18830 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
18831 cp_declarator
*declarator
= NULL
;
18832 int ctor_dtor_or_conv_p
;
18834 /* Check for a (possibly unnamed) bitfield declaration. */
18835 token
= cp_lexer_peek_token (parser
->lexer
);
18836 if (token
->type
== CPP_COLON
)
18839 if (token
->type
== CPP_NAME
18840 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
18843 /* Get the name of the bitfield. */
18844 declarator
= make_id_declarator (NULL_TREE
,
18845 cp_parser_identifier (parser
),
18849 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
18850 /* Get the width of the bitfield. */
18852 = cp_parser_constant_expression (parser
,
18853 /*allow_non_constant=*/false,
18858 /* Parse the declarator. */
18860 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
18861 &ctor_dtor_or_conv_p
,
18862 /*parenthesized_p=*/NULL
,
18863 /*member_p=*/false);
18866 /* Look for attributes that apply to the ivar. */
18867 attributes
= cp_parser_attributes_opt (parser
);
18868 /* Remember which attributes are prefix attributes and
18870 first_attribute
= attributes
;
18871 /* Combine the attributes. */
18872 attributes
= chainon (prefix_attributes
, attributes
);
18876 /* Create the bitfield declaration. */
18877 decl
= grokbitfield (declarator
, &declspecs
, width
);
18878 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
18881 decl
= grokfield (declarator
, &declspecs
,
18882 NULL_TREE
, /*init_const_expr_p=*/false,
18883 NULL_TREE
, attributes
);
18885 /* Add the instance variable. */
18886 objc_add_instance_variable (decl
);
18888 /* Reset PREFIX_ATTRIBUTES. */
18889 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
18890 attributes
= TREE_CHAIN (attributes
);
18892 TREE_CHAIN (attributes
) = NULL_TREE
;
18894 token
= cp_lexer_peek_token (parser
->lexer
);
18896 if (token
->type
== CPP_COMMA
)
18898 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
18904 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18905 token
= cp_lexer_peek_token (parser
->lexer
);
18908 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
18909 /* For historical reasons, we accept an optional semicolon. */
18910 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18911 cp_lexer_consume_token (parser
->lexer
);
18914 /* Parse an Objective-C protocol declaration. */
18917 cp_parser_objc_protocol_declaration (cp_parser
* parser
)
18919 tree proto
, protorefs
;
18922 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
18923 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
18925 error ("identifier expected after %<@protocol%>");
18929 /* See if we have a forward declaration or a definition. */
18930 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18932 /* Try a forward declaration first. */
18933 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
18935 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
18937 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18940 /* Ok, we got a full-fledged definition (or at least should). */
18943 proto
= cp_parser_identifier (parser
);
18944 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
18945 objc_start_protocol (proto
, protorefs
);
18946 cp_parser_objc_method_prototype_list (parser
);
18950 /* Parse an Objective-C superclass or category. */
18953 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
18956 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
18958 *super
= *categ
= NULL_TREE
;
18959 if (next
->type
== CPP_COLON
)
18961 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
18962 *super
= cp_parser_identifier (parser
);
18964 else if (next
->type
== CPP_OPEN_PAREN
)
18966 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
18967 *categ
= cp_parser_identifier (parser
);
18968 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18972 /* Parse an Objective-C class interface. */
18975 cp_parser_objc_class_interface (cp_parser
* parser
)
18977 tree name
, super
, categ
, protos
;
18979 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
18980 name
= cp_parser_identifier (parser
);
18981 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
18982 protos
= cp_parser_objc_protocol_refs_opt (parser
);
18984 /* We have either a class or a category on our hands. */
18986 objc_start_category_interface (name
, categ
, protos
);
18989 objc_start_class_interface (name
, super
, protos
);
18990 /* Handle instance variable declarations, if any. */
18991 cp_parser_objc_class_ivars (parser
);
18992 objc_continue_interface ();
18995 cp_parser_objc_method_prototype_list (parser
);
18998 /* Parse an Objective-C class implementation. */
19001 cp_parser_objc_class_implementation (cp_parser
* parser
)
19003 tree name
, super
, categ
;
19005 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
19006 name
= cp_parser_identifier (parser
);
19007 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
19009 /* We have either a class or a category on our hands. */
19011 objc_start_category_implementation (name
, categ
);
19014 objc_start_class_implementation (name
, super
);
19015 /* Handle instance variable declarations, if any. */
19016 cp_parser_objc_class_ivars (parser
);
19017 objc_continue_implementation ();
19020 cp_parser_objc_method_definition_list (parser
);
19023 /* Consume the @end token and finish off the implementation. */
19026 cp_parser_objc_end_implementation (cp_parser
* parser
)
19028 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
19029 objc_finish_implementation ();
19032 /* Parse an Objective-C declaration. */
19035 cp_parser_objc_declaration (cp_parser
* parser
)
19037 /* Try to figure out what kind of declaration is present. */
19038 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
19040 switch (kwd
->keyword
)
19043 cp_parser_objc_alias_declaration (parser
);
19046 cp_parser_objc_class_declaration (parser
);
19048 case RID_AT_PROTOCOL
:
19049 cp_parser_objc_protocol_declaration (parser
);
19051 case RID_AT_INTERFACE
:
19052 cp_parser_objc_class_interface (parser
);
19054 case RID_AT_IMPLEMENTATION
:
19055 cp_parser_objc_class_implementation (parser
);
19058 cp_parser_objc_end_implementation (parser
);
19061 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->u
.value
);
19062 cp_parser_skip_to_end_of_block_or_statement (parser
);
19066 /* Parse an Objective-C try-catch-finally statement.
19068 objc-try-catch-finally-stmt:
19069 @try compound-statement objc-catch-clause-seq [opt]
19070 objc-finally-clause [opt]
19072 objc-catch-clause-seq:
19073 objc-catch-clause objc-catch-clause-seq [opt]
19076 @catch ( exception-declaration ) compound-statement
19078 objc-finally-clause
19079 @finally compound-statement
19081 Returns NULL_TREE. */
19084 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
19085 location_t location
;
19088 cp_parser_require_keyword (parser
, RID_AT_TRY
, "`@try'");
19089 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19090 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19091 node, lest it get absorbed into the surrounding block. */
19092 stmt
= push_stmt_list ();
19093 cp_parser_compound_statement (parser
, NULL
, false);
19094 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
19096 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
19098 cp_parameter_declarator
*parmdecl
;
19101 cp_lexer_consume_token (parser
->lexer
);
19102 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
19103 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
19104 parm
= grokdeclarator (parmdecl
->declarator
,
19105 &parmdecl
->decl_specifiers
,
19106 PARM
, /*initialized=*/0,
19107 /*attrlist=*/NULL
);
19108 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
19109 objc_begin_catch_clause (parm
);
19110 cp_parser_compound_statement (parser
, NULL
, false);
19111 objc_finish_catch_clause ();
19114 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
19116 cp_lexer_consume_token (parser
->lexer
);
19117 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19118 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19119 node, lest it get absorbed into the surrounding block. */
19120 stmt
= push_stmt_list ();
19121 cp_parser_compound_statement (parser
, NULL
, false);
19122 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
19125 return objc_finish_try_stmt ();
19128 /* Parse an Objective-C synchronized statement.
19130 objc-synchronized-stmt:
19131 @synchronized ( expression ) compound-statement
19133 Returns NULL_TREE. */
19136 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
19137 location_t location
;
19140 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, "`@synchronized'");
19142 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19143 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
19144 lock
= cp_parser_expression (parser
, false);
19145 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
19147 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19148 node, lest it get absorbed into the surrounding block. */
19149 stmt
= push_stmt_list ();
19150 cp_parser_compound_statement (parser
, NULL
, false);
19152 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
19155 /* Parse an Objective-C throw statement.
19158 @throw assignment-expression [opt] ;
19160 Returns a constructed '@throw' statement. */
19163 cp_parser_objc_throw_statement (cp_parser
*parser
) {
19164 tree expr
= NULL_TREE
;
19166 cp_parser_require_keyword (parser
, RID_AT_THROW
, "`@throw'");
19168 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19169 expr
= cp_parser_assignment_expression (parser
, false);
19171 cp_parser_consume_semicolon_at_end_of_statement (parser
);
19173 return objc_build_throw_stmt (expr
);
19176 /* Parse an Objective-C statement. */
19179 cp_parser_objc_statement (cp_parser
* parser
) {
19180 /* Try to figure out what kind of declaration is present. */
19181 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
19183 switch (kwd
->keyword
)
19186 return cp_parser_objc_try_catch_finally_statement (parser
);
19187 case RID_AT_SYNCHRONIZED
:
19188 return cp_parser_objc_synchronized_statement (parser
);
19190 return cp_parser_objc_throw_statement (parser
);
19192 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->u
.value
);
19193 cp_parser_skip_to_end_of_block_or_statement (parser
);
19196 return error_mark_node
;
19199 /* OpenMP 2.5 parsing routines. */
19201 /* Returns name of the next clause.
19202 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19203 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19204 returned and the token is consumed. */
19206 static pragma_omp_clause
19207 cp_parser_omp_clause_name (cp_parser
*parser
)
19209 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
19211 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
19212 result
= PRAGMA_OMP_CLAUSE_IF
;
19213 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
19214 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
19215 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
19216 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
19217 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19219 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
19220 const char *p
= IDENTIFIER_POINTER (id
);
19225 if (!strcmp ("copyin", p
))
19226 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
19227 else if (!strcmp ("copyprivate", p
))
19228 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
19231 if (!strcmp ("firstprivate", p
))
19232 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
19235 if (!strcmp ("lastprivate", p
))
19236 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
19239 if (!strcmp ("nowait", p
))
19240 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
19241 else if (!strcmp ("num_threads", p
))
19242 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
19245 if (!strcmp ("ordered", p
))
19246 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
19249 if (!strcmp ("reduction", p
))
19250 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
19253 if (!strcmp ("schedule", p
))
19254 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
19255 else if (!strcmp ("shared", p
))
19256 result
= PRAGMA_OMP_CLAUSE_SHARED
;
19261 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
19262 cp_lexer_consume_token (parser
->lexer
);
19267 /* Validate that a clause of the given type does not already exist. */
19270 check_no_duplicate_clause (tree clauses
, enum tree_code code
, const char *name
)
19274 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
19275 if (OMP_CLAUSE_CODE (c
) == code
)
19277 error ("too many %qs clauses", name
);
19285 variable-list , identifier
19287 In addition, we match a closing parenthesis. An opening parenthesis
19288 will have been consumed by the caller.
19290 If KIND is nonzero, create the appropriate node and install the decl
19291 in OMP_CLAUSE_DECL and add the node to the head of the list.
19293 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19294 return the list created. */
19297 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
19304 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
19305 /*check_dependency_p=*/true,
19306 /*template_p=*/NULL
,
19307 /*declarator_p=*/false,
19308 /*optional_p=*/false);
19309 if (name
== error_mark_node
)
19312 decl
= cp_parser_lookup_name_simple (parser
, name
);
19313 if (decl
== error_mark_node
)
19314 cp_parser_name_lookup_error (parser
, name
, decl
, NULL
);
19315 else if (kind
!= 0)
19317 tree u
= build_omp_clause (kind
);
19318 OMP_CLAUSE_DECL (u
) = decl
;
19319 OMP_CLAUSE_CHAIN (u
) = list
;
19323 list
= tree_cons (decl
, NULL_TREE
, list
);
19326 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
19328 cp_lexer_consume_token (parser
->lexer
);
19331 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
19335 /* Try to resync to an unnested comma. Copied from
19336 cp_parser_parenthesized_expression_list. */
19338 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
19339 /*recovering=*/true,
19341 /*consume_paren=*/true);
19349 /* Similarly, but expect leading and trailing parenthesis. This is a very
19350 common case for omp clauses. */
19353 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
19355 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
19356 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
19361 default ( shared | none ) */
19364 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
)
19366 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
19369 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
19371 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19373 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
19374 const char *p
= IDENTIFIER_POINTER (id
);
19379 if (strcmp ("none", p
) != 0)
19381 kind
= OMP_CLAUSE_DEFAULT_NONE
;
19385 if (strcmp ("shared", p
) != 0)
19387 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
19394 cp_lexer_consume_token (parser
->lexer
);
19399 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
19402 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
19403 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19404 /*or_comma=*/false,
19405 /*consume_paren=*/true);
19407 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
19410 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default");
19411 c
= build_omp_clause (OMP_CLAUSE_DEFAULT
);
19412 OMP_CLAUSE_CHAIN (c
) = list
;
19413 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
19419 if ( expression ) */
19422 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
)
19426 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
19429 t
= cp_parser_condition (parser
);
19431 if (t
== error_mark_node
19432 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
19433 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19434 /*or_comma=*/false,
19435 /*consume_paren=*/true);
19437 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if");
19439 c
= build_omp_clause (OMP_CLAUSE_IF
);
19440 OMP_CLAUSE_IF_EXPR (c
) = t
;
19441 OMP_CLAUSE_CHAIN (c
) = list
;
19450 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
19454 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait");
19456 c
= build_omp_clause (OMP_CLAUSE_NOWAIT
);
19457 OMP_CLAUSE_CHAIN (c
) = list
;
19462 num_threads ( expression ) */
19465 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
)
19469 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
19472 t
= cp_parser_expression (parser
, false);
19474 if (t
== error_mark_node
19475 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
19476 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19477 /*or_comma=*/false,
19478 /*consume_paren=*/true);
19480 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
, "num_threads");
19482 c
= build_omp_clause (OMP_CLAUSE_NUM_THREADS
);
19483 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
19484 OMP_CLAUSE_CHAIN (c
) = list
;
19493 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
19497 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
, "ordered");
19499 c
= build_omp_clause (OMP_CLAUSE_ORDERED
);
19500 OMP_CLAUSE_CHAIN (c
) = list
;
19505 reduction ( reduction-operator : variable-list )
19507 reduction-operator:
19508 One of: + * - & ^ | && || */
19511 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
19513 enum tree_code code
;
19516 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
19519 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
19531 code
= BIT_AND_EXPR
;
19534 code
= BIT_XOR_EXPR
;
19537 code
= BIT_IOR_EXPR
;
19540 code
= TRUTH_ANDIF_EXPR
;
19543 code
= TRUTH_ORIF_EXPR
;
19546 cp_parser_error (parser
, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19548 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19549 /*or_comma=*/false,
19550 /*consume_paren=*/true);
19553 cp_lexer_consume_token (parser
->lexer
);
19555 if (!cp_parser_require (parser
, CPP_COLON
, "`:'"))
19558 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
19559 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
19560 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
19566 schedule ( schedule-kind )
19567 schedule ( schedule-kind , expression )
19570 static | dynamic | guided | runtime */
19573 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
)
19577 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
19580 c
= build_omp_clause (OMP_CLAUSE_SCHEDULE
);
19582 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19584 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
19585 const char *p
= IDENTIFIER_POINTER (id
);
19590 if (strcmp ("dynamic", p
) != 0)
19592 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
19596 if (strcmp ("guided", p
) != 0)
19598 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
19602 if (strcmp ("runtime", p
) != 0)
19604 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
19611 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
19612 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
19615 cp_lexer_consume_token (parser
->lexer
);
19617 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19619 cp_lexer_consume_token (parser
->lexer
);
19621 t
= cp_parser_assignment_expression (parser
, false);
19623 if (t
== error_mark_node
)
19625 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
19626 error ("schedule %<runtime%> does not take "
19627 "a %<chunk_size%> parameter");
19629 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
19631 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
19634 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`,' or `)'"))
19637 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule");
19638 OMP_CLAUSE_CHAIN (c
) = list
;
19642 cp_parser_error (parser
, "invalid schedule kind");
19644 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19645 /*or_comma=*/false,
19646 /*consume_paren=*/true);
19650 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19651 is a bitmask in MASK. Return the list of clauses found; the result
19652 of clause default goes in *pdefault. */
19655 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
19656 const char *where
, cp_token
*pragma_tok
)
19658 tree clauses
= NULL
;
19660 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
19662 pragma_omp_clause c_kind
= cp_parser_omp_clause_name (parser
);
19663 const char *c_name
;
19664 tree prev
= clauses
;
19668 case PRAGMA_OMP_CLAUSE_COPYIN
:
19669 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
19672 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
19673 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
19675 c_name
= "copyprivate";
19677 case PRAGMA_OMP_CLAUSE_DEFAULT
:
19678 clauses
= cp_parser_omp_clause_default (parser
, clauses
);
19679 c_name
= "default";
19681 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
19682 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
19684 c_name
= "firstprivate";
19686 case PRAGMA_OMP_CLAUSE_IF
:
19687 clauses
= cp_parser_omp_clause_if (parser
, clauses
);
19690 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
19691 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
19693 c_name
= "lastprivate";
19695 case PRAGMA_OMP_CLAUSE_NOWAIT
:
19696 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
);
19699 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
19700 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
);
19701 c_name
= "num_threads";
19703 case PRAGMA_OMP_CLAUSE_ORDERED
:
19704 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
);
19705 c_name
= "ordered";
19707 case PRAGMA_OMP_CLAUSE_PRIVATE
:
19708 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
19710 c_name
= "private";
19712 case PRAGMA_OMP_CLAUSE_REDUCTION
:
19713 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
19714 c_name
= "reduction";
19716 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
19717 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
);
19718 c_name
= "schedule";
19720 case PRAGMA_OMP_CLAUSE_SHARED
:
19721 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
19726 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
19730 if (((mask
>> c_kind
) & 1) == 0)
19732 /* Remove the invalid clause(s) from the list to avoid
19733 confusing the rest of the compiler. */
19735 error ("%qs is not valid for %qs", c_name
, where
);
19739 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
19740 return finish_omp_clauses (clauses
);
19747 In practice, we're also interested in adding the statement to an
19748 outer node. So it is convenient if we work around the fact that
19749 cp_parser_statement calls add_stmt. */
19752 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
19754 unsigned save
= parser
->in_statement
;
19756 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19757 This preserves the "not within loop or switch" style error messages
19758 for nonsense cases like
19764 if (parser
->in_statement
)
19765 parser
->in_statement
= IN_OMP_BLOCK
;
19771 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
19773 parser
->in_statement
= save
;
19777 cp_parser_omp_structured_block (cp_parser
*parser
)
19779 tree stmt
= begin_omp_structured_block ();
19780 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
19782 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
19784 cp_parser_end_omp_structured_block (parser
, save
);
19785 return finish_omp_structured_block (stmt
);
19789 # pragma omp atomic new-line
19793 x binop= expr | x++ | ++x | x-- | --x
19795 +, *, -, /, &, ^, |, <<, >>
19797 where x is an lvalue expression with scalar type. */
19800 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
19803 enum tree_code code
;
19805 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19807 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
19809 switch (TREE_CODE (lhs
))
19814 case PREINCREMENT_EXPR
:
19815 case POSTINCREMENT_EXPR
:
19816 lhs
= TREE_OPERAND (lhs
, 0);
19818 rhs
= integer_one_node
;
19821 case PREDECREMENT_EXPR
:
19822 case POSTDECREMENT_EXPR
:
19823 lhs
= TREE_OPERAND (lhs
, 0);
19825 rhs
= integer_one_node
;
19829 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
19835 code
= TRUNC_DIV_EXPR
;
19843 case CPP_LSHIFT_EQ
:
19844 code
= LSHIFT_EXPR
;
19846 case CPP_RSHIFT_EQ
:
19847 code
= RSHIFT_EXPR
;
19850 code
= BIT_AND_EXPR
;
19853 code
= BIT_IOR_EXPR
;
19856 code
= BIT_XOR_EXPR
;
19859 cp_parser_error (parser
,
19860 "invalid operator for %<#pragma omp atomic%>");
19863 cp_lexer_consume_token (parser
->lexer
);
19865 rhs
= cp_parser_expression (parser
, false);
19866 if (rhs
== error_mark_node
)
19870 finish_omp_atomic (code
, lhs
, rhs
);
19871 cp_parser_consume_semicolon_at_end_of_statement (parser
);
19875 cp_parser_skip_to_end_of_block_or_statement (parser
);
19880 # pragma omp barrier new-line */
19883 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
19885 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19886 finish_omp_barrier ();
19890 # pragma omp critical [(name)] new-line
19891 structured-block */
19894 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
19896 tree stmt
, name
= NULL
;
19898 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
19900 cp_lexer_consume_token (parser
->lexer
);
19902 name
= cp_parser_identifier (parser
);
19904 if (name
== error_mark_node
19905 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
19906 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19907 /*or_comma=*/false,
19908 /*consume_paren=*/true);
19909 if (name
== error_mark_node
)
19912 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19914 stmt
= cp_parser_omp_structured_block (parser
);
19915 return c_finish_omp_critical (stmt
, name
);
19919 # pragma omp flush flush-vars[opt] new-line
19922 ( variable-list ) */
19925 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
19927 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
19928 (void) cp_parser_omp_var_list (parser
, 0, NULL
);
19929 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19931 finish_omp_flush ();
19934 /* Parse the restricted form of the for statment allowed by OpenMP. */
19937 cp_parser_omp_for_loop (cp_parser
*parser
)
19939 tree init
, cond
, incr
, body
, decl
, pre_body
;
19942 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
19944 cp_parser_error (parser
, "for statement expected");
19947 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
19948 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
19951 init
= decl
= NULL
;
19952 pre_body
= push_stmt_list ();
19953 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19955 cp_decl_specifier_seq type_specifiers
;
19957 /* First, try to parse as an initialized declaration. See
19958 cp_parser_condition, from whence the bulk of this is copied. */
19960 cp_parser_parse_tentatively (parser
);
19961 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
19963 if (!cp_parser_error_occurred (parser
))
19965 tree asm_specification
, attributes
;
19966 cp_declarator
*declarator
;
19968 declarator
= cp_parser_declarator (parser
,
19969 CP_PARSER_DECLARATOR_NAMED
,
19970 /*ctor_dtor_or_conv_p=*/NULL
,
19971 /*parenthesized_p=*/NULL
,
19972 /*member_p=*/false);
19973 attributes
= cp_parser_attributes_opt (parser
);
19974 asm_specification
= cp_parser_asm_specification_opt (parser
);
19976 cp_parser_require (parser
, CPP_EQ
, "`='");
19977 if (cp_parser_parse_definitely (parser
))
19981 decl
= start_decl (declarator
, &type_specifiers
,
19982 /*initialized_p=*/false, attributes
,
19983 /*prefix_attributes=*/NULL_TREE
,
19986 init
= cp_parser_assignment_expression (parser
, false);
19988 cp_finish_decl (decl
, NULL_TREE
, /*init_const_expr_p=*/false,
19989 asm_specification
, LOOKUP_ONLYCONVERTING
);
19992 pop_scope (pushed_scope
);
19996 cp_parser_abort_tentative_parse (parser
);
19998 /* If parsing as an initialized declaration failed, try again as
19999 a simple expression. */
20001 init
= cp_parser_expression (parser
, false);
20003 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
20004 pre_body
= pop_stmt_list (pre_body
);
20007 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20008 cond
= cp_parser_condition (parser
);
20009 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
20012 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
20013 incr
= cp_parser_expression (parser
, false);
20015 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
20016 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
20017 /*or_comma=*/false,
20018 /*consume_paren=*/true);
20020 /* Note that we saved the original contents of this flag when we entered
20021 the structured block, and so we don't need to re-save it here. */
20022 parser
->in_statement
= IN_OMP_FOR
;
20024 /* Note that the grammar doesn't call for a structured block here,
20025 though the loop as a whole is a structured block. */
20026 body
= push_stmt_list ();
20027 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
20028 body
= pop_stmt_list (body
);
20030 return finish_omp_for (loc
, decl
, init
, cond
, incr
, body
, pre_body
);
20034 #pragma omp for for-clause[optseq] new-line
20037 #define OMP_FOR_CLAUSE_MASK \
20038 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20039 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20040 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20041 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20042 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
20043 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
20044 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20047 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
20049 tree clauses
, sb
, ret
;
20052 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
20053 "#pragma omp for", pragma_tok
);
20055 sb
= begin_omp_structured_block ();
20056 save
= cp_parser_begin_omp_structured_block (parser
);
20058 ret
= cp_parser_omp_for_loop (parser
);
20060 OMP_FOR_CLAUSES (ret
) = clauses
;
20062 cp_parser_end_omp_structured_block (parser
, save
);
20063 add_stmt (finish_omp_structured_block (sb
));
20069 # pragma omp master new-line
20070 structured-block */
20073 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
20075 cp_parser_require_pragma_eol (parser
, pragma_tok
);
20076 return c_finish_omp_master (cp_parser_omp_structured_block (parser
));
20080 # pragma omp ordered new-line
20081 structured-block */
20084 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
20086 cp_parser_require_pragma_eol (parser
, pragma_tok
);
20087 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser
));
20093 { section-sequence }
20096 section-directive[opt] structured-block
20097 section-sequence section-directive structured-block */
20100 cp_parser_omp_sections_scope (cp_parser
*parser
)
20102 tree stmt
, substmt
;
20103 bool error_suppress
= false;
20106 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
20109 stmt
= push_stmt_list ();
20111 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
20115 substmt
= begin_omp_structured_block ();
20116 save
= cp_parser_begin_omp_structured_block (parser
);
20120 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
20122 tok
= cp_lexer_peek_token (parser
->lexer
);
20123 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
20125 if (tok
->type
== CPP_CLOSE_BRACE
)
20127 if (tok
->type
== CPP_EOF
)
20131 cp_parser_end_omp_structured_block (parser
, save
);
20132 substmt
= finish_omp_structured_block (substmt
);
20133 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
20134 add_stmt (substmt
);
20139 tok
= cp_lexer_peek_token (parser
->lexer
);
20140 if (tok
->type
== CPP_CLOSE_BRACE
)
20142 if (tok
->type
== CPP_EOF
)
20145 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
20147 cp_lexer_consume_token (parser
->lexer
);
20148 cp_parser_require_pragma_eol (parser
, tok
);
20149 error_suppress
= false;
20151 else if (!error_suppress
)
20153 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
20154 error_suppress
= true;
20157 substmt
= cp_parser_omp_structured_block (parser
);
20158 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
20159 add_stmt (substmt
);
20161 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
20163 substmt
= pop_stmt_list (stmt
);
20165 stmt
= make_node (OMP_SECTIONS
);
20166 TREE_TYPE (stmt
) = void_type_node
;
20167 OMP_SECTIONS_BODY (stmt
) = substmt
;
20174 # pragma omp sections sections-clause[optseq] newline
20177 #define OMP_SECTIONS_CLAUSE_MASK \
20178 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20179 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20180 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20181 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20182 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20185 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
20189 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
20190 "#pragma omp sections", pragma_tok
);
20192 ret
= cp_parser_omp_sections_scope (parser
);
20194 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
20200 # pragma parallel parallel-clause new-line
20201 # pragma parallel for parallel-for-clause new-line
20202 # pragma parallel sections parallel-sections-clause new-line */
20204 #define OMP_PARALLEL_CLAUSE_MASK \
20205 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
20206 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20207 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20208 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
20209 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
20210 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
20211 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20212 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20215 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
20217 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
20218 const char *p_name
= "#pragma omp parallel";
20219 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
20220 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
20223 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
20225 cp_lexer_consume_token (parser
->lexer
);
20226 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
20227 p_name
= "#pragma omp parallel for";
20228 mask
|= OMP_FOR_CLAUSE_MASK
;
20229 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
20231 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
20233 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
20234 const char *p
= IDENTIFIER_POINTER (id
);
20235 if (strcmp (p
, "sections") == 0)
20237 cp_lexer_consume_token (parser
->lexer
);
20238 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
20239 p_name
= "#pragma omp parallel sections";
20240 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
20241 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
20245 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
20246 block
= begin_omp_parallel ();
20247 save
= cp_parser_begin_omp_structured_block (parser
);
20251 case PRAGMA_OMP_PARALLEL
:
20252 cp_parser_already_scoped_statement (parser
);
20253 par_clause
= clauses
;
20256 case PRAGMA_OMP_PARALLEL_FOR
:
20257 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
20258 stmt
= cp_parser_omp_for_loop (parser
);
20260 OMP_FOR_CLAUSES (stmt
) = ws_clause
;
20263 case PRAGMA_OMP_PARALLEL_SECTIONS
:
20264 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
20265 stmt
= cp_parser_omp_sections_scope (parser
);
20267 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
20271 gcc_unreachable ();
20274 cp_parser_end_omp_structured_block (parser
, save
);
20275 stmt
= finish_omp_parallel (par_clause
, block
);
20276 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
20277 OMP_PARALLEL_COMBINED (stmt
) = 1;
20282 # pragma omp single single-clause[optseq] new-line
20283 structured-block */
20285 #define OMP_SINGLE_CLAUSE_MASK \
20286 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20287 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20288 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
20289 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20292 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
20294 tree stmt
= make_node (OMP_SINGLE
);
20295 TREE_TYPE (stmt
) = void_type_node
;
20297 OMP_SINGLE_CLAUSES (stmt
)
20298 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
20299 "#pragma omp single", pragma_tok
);
20300 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
20302 return add_stmt (stmt
);
20306 # pragma omp threadprivate (variable-list) */
20309 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
20313 vars
= cp_parser_omp_var_list (parser
, 0, NULL
);
20314 cp_parser_require_pragma_eol (parser
, pragma_tok
);
20316 finish_omp_threadprivate (vars
);
20319 /* Main entry point to OpenMP statement pragmas. */
20322 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
20326 switch (pragma_tok
->pragma_kind
)
20328 case PRAGMA_OMP_ATOMIC
:
20329 cp_parser_omp_atomic (parser
, pragma_tok
);
20331 case PRAGMA_OMP_CRITICAL
:
20332 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
20334 case PRAGMA_OMP_FOR
:
20335 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
20337 case PRAGMA_OMP_MASTER
:
20338 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
20340 case PRAGMA_OMP_ORDERED
:
20341 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
20343 case PRAGMA_OMP_PARALLEL
:
20344 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
20346 case PRAGMA_OMP_SECTIONS
:
20347 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
20349 case PRAGMA_OMP_SINGLE
:
20350 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
20353 gcc_unreachable ();
20357 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
20362 static GTY (()) cp_parser
*the_parser
;
20365 /* Special handling for the first token or line in the file. The first
20366 thing in the file might be #pragma GCC pch_preprocess, which loads a
20367 PCH file, which is a GC collection point. So we need to handle this
20368 first pragma without benefit of an existing lexer structure.
20370 Always returns one token to the caller in *FIRST_TOKEN. This is
20371 either the true first token of the file, or the first token after
20372 the initial pragma. */
20375 cp_parser_initial_pragma (cp_token
*first_token
)
20379 cp_lexer_get_preprocessor_token (NULL
, first_token
);
20380 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
20383 cp_lexer_get_preprocessor_token (NULL
, first_token
);
20384 if (first_token
->type
== CPP_STRING
)
20386 name
= first_token
->u
.value
;
20388 cp_lexer_get_preprocessor_token (NULL
, first_token
);
20389 if (first_token
->type
!= CPP_PRAGMA_EOL
)
20390 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20393 error ("expected string literal");
20395 /* Skip to the end of the pragma. */
20396 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
20397 cp_lexer_get_preprocessor_token (NULL
, first_token
);
20399 /* Now actually load the PCH file. */
20401 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
20403 /* Read one more token to return to our caller. We have to do this
20404 after reading the PCH file in, since its pointers have to be
20406 cp_lexer_get_preprocessor_token (NULL
, first_token
);
20409 /* Normal parsing of a pragma token. Here we can (and must) use the
20413 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
20415 cp_token
*pragma_tok
;
20418 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
20419 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
20420 parser
->lexer
->in_pragma
= true;
20422 id
= pragma_tok
->pragma_kind
;
20425 case PRAGMA_GCC_PCH_PREPROCESS
:
20426 error ("%<#pragma GCC pch_preprocess%> must be first");
20429 case PRAGMA_OMP_BARRIER
:
20432 case pragma_compound
:
20433 cp_parser_omp_barrier (parser
, pragma_tok
);
20436 error ("%<#pragma omp barrier%> may only be "
20437 "used in compound statements");
20444 case PRAGMA_OMP_FLUSH
:
20447 case pragma_compound
:
20448 cp_parser_omp_flush (parser
, pragma_tok
);
20451 error ("%<#pragma omp flush%> may only be "
20452 "used in compound statements");
20459 case PRAGMA_OMP_THREADPRIVATE
:
20460 cp_parser_omp_threadprivate (parser
, pragma_tok
);
20463 case PRAGMA_OMP_ATOMIC
:
20464 case PRAGMA_OMP_CRITICAL
:
20465 case PRAGMA_OMP_FOR
:
20466 case PRAGMA_OMP_MASTER
:
20467 case PRAGMA_OMP_ORDERED
:
20468 case PRAGMA_OMP_PARALLEL
:
20469 case PRAGMA_OMP_SECTIONS
:
20470 case PRAGMA_OMP_SINGLE
:
20471 if (context
== pragma_external
)
20473 cp_parser_omp_construct (parser
, pragma_tok
);
20476 case PRAGMA_OMP_SECTION
:
20477 error ("%<#pragma omp section%> may only be used in "
20478 "%<#pragma omp sections%> construct");
20482 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
20483 c_invoke_pragma_handler (id
);
20487 cp_parser_error (parser
, "expected declaration specifiers");
20491 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
20495 /* The interface the pragma parsers have to the lexer. */
20498 pragma_lex (tree
*value
)
20501 enum cpp_ttype ret
;
20503 tok
= cp_lexer_peek_token (the_parser
->lexer
);
20506 *value
= tok
->u
.value
;
20508 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
20510 else if (ret
== CPP_STRING
)
20511 *value
= cp_parser_string_literal (the_parser
, false, false);
20514 cp_lexer_consume_token (the_parser
->lexer
);
20515 if (ret
== CPP_KEYWORD
)
20523 /* External interface. */
20525 /* Parse one entire translation unit. */
20528 c_parse_file (void)
20530 bool error_occurred
;
20531 static bool already_called
= false;
20533 if (already_called
)
20535 sorry ("inter-module optimizations not implemented for C++");
20538 already_called
= true;
20540 the_parser
= cp_parser_new ();
20541 push_deferring_access_checks (flag_access_control
20542 ? dk_no_deferred
: dk_no_check
);
20543 error_occurred
= cp_parser_translation_unit (the_parser
);
20547 #include "gt-cp-parser.h"