2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "coretypes.h"
27 #include "dyn-string.h"
35 #include "diagnostic.h"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 /* A token's value and its associated deferred access checks and
51 struct tree_check
GTY(())
53 /* The value associated with the token. */
55 /* The checks that have been associated with value. */
56 VEC (deferred_access_check
, gc
)* checks
;
57 /* The token's qualifying scope (used when it is a
58 CPP_NESTED_NAME_SPECIFIER). */
59 tree qualifying_scope
;
64 typedef struct cp_token
GTY (())
66 /* The kind of token. */
67 ENUM_BITFIELD (cpp_ttype
) type
: 8;
68 /* If this token is a keyword, this value indicates which keyword.
69 Otherwise, this value is RID_MAX. */
70 ENUM_BITFIELD (rid
) keyword
: 8;
73 /* Identifier for the pragma. */
74 ENUM_BITFIELD (pragma_kind
) pragma_kind
: 6;
75 /* True if this token is from a system header. */
76 BOOL_BITFIELD in_system_header
: 1;
77 /* True if this token is from a context where it is implicitly extern "C" */
78 BOOL_BITFIELD implicit_extern_c
: 1;
79 /* True for a CPP_NAME token that is not a keyword (i.e., for which
80 KEYWORD is RID_MAX) iff this name was looked up and found to be
81 ambiguous. An error has already been reported. */
82 BOOL_BITFIELD ambiguous_p
: 1;
83 /* The input file stack index at which this token was found. */
84 unsigned input_file_stack_index
: INPUT_FILE_STACK_BITS
;
85 /* The value associated with this token, if any. */
86 union cp_token_value
{
87 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
88 struct tree_check
* GTY((tag ("1"))) tree_check_value
;
89 /* Use for all other tokens. */
90 tree
GTY((tag ("0"))) value
;
91 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u
;
92 /* The location at which this token was found. */
96 /* We use a stack of token pointer for saving token sets. */
97 typedef struct cp_token
*cp_token_position
;
98 DEF_VEC_P (cp_token_position
);
99 DEF_VEC_ALLOC_P (cp_token_position
,heap
);
101 static const cp_token eof_token
=
103 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, 0, 0, false, 0, { NULL
},
104 #if USE_MAPPED_LOCATION
111 /* The cp_lexer structure represents the C++ lexer. It is responsible
112 for managing the token stream from the preprocessor and supplying
113 it to the parser. Tokens are never added to the cp_lexer after
116 typedef struct cp_lexer
GTY (())
118 /* The memory allocated for the buffer. NULL if this lexer does not
119 own the token buffer. */
120 cp_token
* GTY ((length ("%h.buffer_length"))) buffer
;
121 /* If the lexer owns the buffer, this is the number of tokens in the
123 size_t buffer_length
;
125 /* A pointer just past the last available token. The tokens
126 in this lexer are [buffer, last_token). */
127 cp_token_position
GTY ((skip
)) last_token
;
129 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
130 no more available tokens. */
131 cp_token_position
GTY ((skip
)) next_token
;
133 /* A stack indicating positions at which cp_lexer_save_tokens was
134 called. The top entry is the most recent position at which we
135 began saving tokens. If the stack is non-empty, we are saving
137 VEC(cp_token_position
,heap
) *GTY ((skip
)) saved_tokens
;
139 /* The next lexer in a linked list of lexers. */
140 struct cp_lexer
*next
;
142 /* True if we should output debugging information. */
145 /* True if we're in the context of parsing a pragma, and should not
146 increment past the end-of-line marker. */
150 /* cp_token_cache is a range of tokens. There is no need to represent
151 allocate heap memory for it, since tokens are never removed from the
152 lexer's array. There is also no need for the GC to walk through
153 a cp_token_cache, since everything in here is referenced through
156 typedef struct cp_token_cache
GTY(())
158 /* The beginning of the token range. */
159 cp_token
* GTY((skip
)) first
;
161 /* Points immediately after the last token in the range. */
162 cp_token
* GTY ((skip
)) last
;
167 static cp_lexer
*cp_lexer_new_main
169 static cp_lexer
*cp_lexer_new_from_tokens
170 (cp_token_cache
*tokens
);
171 static void cp_lexer_destroy
173 static int cp_lexer_saving_tokens
175 static cp_token_position cp_lexer_token_position
177 static cp_token
*cp_lexer_token_at
178 (cp_lexer
*, cp_token_position
);
179 static void cp_lexer_get_preprocessor_token
180 (cp_lexer
*, cp_token
*);
181 static inline cp_token
*cp_lexer_peek_token
183 static cp_token
*cp_lexer_peek_nth_token
184 (cp_lexer
*, size_t);
185 static inline bool cp_lexer_next_token_is
186 (cp_lexer
*, enum cpp_ttype
);
187 static bool cp_lexer_next_token_is_not
188 (cp_lexer
*, enum cpp_ttype
);
189 static bool cp_lexer_next_token_is_keyword
190 (cp_lexer
*, enum rid
);
191 static cp_token
*cp_lexer_consume_token
193 static void cp_lexer_purge_token
195 static void cp_lexer_purge_tokens_after
196 (cp_lexer
*, cp_token_position
);
197 static void cp_lexer_save_tokens
199 static void cp_lexer_commit_tokens
201 static void cp_lexer_rollback_tokens
203 #ifdef ENABLE_CHECKING
204 static void cp_lexer_print_token
205 (FILE *, cp_token
*);
206 static inline bool cp_lexer_debugging_p
208 static void cp_lexer_start_debugging
209 (cp_lexer
*) ATTRIBUTE_UNUSED
;
210 static void cp_lexer_stop_debugging
211 (cp_lexer
*) ATTRIBUTE_UNUSED
;
213 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
214 about passing NULL to functions that require non-NULL arguments
215 (fputs, fprintf). It will never be used, so all we need is a value
216 of the right type that's guaranteed not to be NULL. */
217 #define cp_lexer_debug_stream stdout
218 #define cp_lexer_print_token(str, tok) (void) 0
219 #define cp_lexer_debugging_p(lexer) 0
220 #endif /* ENABLE_CHECKING */
222 static cp_token_cache
*cp_token_cache_new
223 (cp_token
*, cp_token
*);
225 static void cp_parser_initial_pragma
228 /* Manifest constants. */
229 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
230 #define CP_SAVED_TOKEN_STACK 5
232 /* A token type for keywords, as opposed to ordinary identifiers. */
233 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
235 /* A token type for template-ids. If a template-id is processed while
236 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
237 the value of the CPP_TEMPLATE_ID is whatever was returned by
238 cp_parser_template_id. */
239 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
241 /* A token type for nested-name-specifiers. If a
242 nested-name-specifier is processed while parsing tentatively, it is
243 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
244 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
245 cp_parser_nested_name_specifier_opt. */
246 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
248 /* A token type for tokens that are not tokens at all; these are used
249 to represent slots in the array where there used to be a token
250 that has now been deleted. */
251 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
253 /* The number of token types, including C++-specific ones. */
254 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
258 #ifdef ENABLE_CHECKING
259 /* The stream to which debugging output should be written. */
260 static FILE *cp_lexer_debug_stream
;
261 #endif /* ENABLE_CHECKING */
263 /* Create a new main C++ lexer, the lexer that gets tokens from the
267 cp_lexer_new_main (void)
269 cp_token first_token
;
276 /* It's possible that parsing the first pragma will load a PCH file,
277 which is a GC collection point. So we have to do that before
278 allocating any memory. */
279 cp_parser_initial_pragma (&first_token
);
281 /* Tell c_lex_with_flags not to merge string constants. */
282 c_lex_return_raw_strings
= true;
284 c_common_no_more_pch ();
286 /* Allocate the memory. */
287 lexer
= GGC_CNEW (cp_lexer
);
289 #ifdef ENABLE_CHECKING
290 /* Initially we are not debugging. */
291 lexer
->debugging_p
= false;
292 #endif /* ENABLE_CHECKING */
293 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
294 CP_SAVED_TOKEN_STACK
);
296 /* Create the buffer. */
297 alloc
= CP_LEXER_BUFFER_SIZE
;
298 buffer
= GGC_NEWVEC (cp_token
, alloc
);
300 /* Put the first token in the buffer. */
305 /* Get the remaining tokens from the preprocessor. */
306 while (pos
->type
!= CPP_EOF
)
313 buffer
= GGC_RESIZEVEC (cp_token
, buffer
, alloc
);
314 pos
= buffer
+ space
;
316 cp_lexer_get_preprocessor_token (lexer
, pos
);
318 lexer
->buffer
= buffer
;
319 lexer
->buffer_length
= alloc
- space
;
320 lexer
->last_token
= pos
;
321 lexer
->next_token
= lexer
->buffer_length
? buffer
: (cp_token
*)&eof_token
;
323 /* Subsequent preprocessor diagnostics should use compiler
324 diagnostic functions to get the compiler source location. */
325 cpp_get_options (parse_in
)->client_diagnostic
= true;
326 cpp_get_callbacks (parse_in
)->error
= cp_cpp_error
;
328 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
332 /* Create a new lexer whose token stream is primed with the tokens in
333 CACHE. When these tokens are exhausted, no new tokens will be read. */
336 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
338 cp_token
*first
= cache
->first
;
339 cp_token
*last
= cache
->last
;
340 cp_lexer
*lexer
= GGC_CNEW (cp_lexer
);
342 /* We do not own the buffer. */
343 lexer
->buffer
= NULL
;
344 lexer
->buffer_length
= 0;
345 lexer
->next_token
= first
== last
? (cp_token
*)&eof_token
: first
;
346 lexer
->last_token
= last
;
348 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
349 CP_SAVED_TOKEN_STACK
);
351 #ifdef ENABLE_CHECKING
352 /* Initially we are not debugging. */
353 lexer
->debugging_p
= false;
356 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
360 /* Frees all resources associated with LEXER. */
363 cp_lexer_destroy (cp_lexer
*lexer
)
366 ggc_free (lexer
->buffer
);
367 VEC_free (cp_token_position
, heap
, lexer
->saved_tokens
);
371 /* Returns nonzero if debugging information should be output. */
373 #ifdef ENABLE_CHECKING
376 cp_lexer_debugging_p (cp_lexer
*lexer
)
378 return lexer
->debugging_p
;
381 #endif /* ENABLE_CHECKING */
383 static inline cp_token_position
384 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
386 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
388 return lexer
->next_token
- previous_p
;
391 static inline cp_token
*
392 cp_lexer_token_at (cp_lexer
*lexer ATTRIBUTE_UNUSED
, cp_token_position pos
)
397 /* nonzero if we are presently saving tokens. */
400 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
402 return VEC_length (cp_token_position
, lexer
->saved_tokens
) != 0;
405 /* Store the next token from the preprocessor in *TOKEN. Return true
409 cp_lexer_get_preprocessor_token (cp_lexer
*lexer ATTRIBUTE_UNUSED
,
412 static int is_extern_c
= 0;
414 /* Get a new token from the preprocessor. */
416 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
);
417 token
->input_file_stack_index
= input_file_stack_tick
;
418 token
->keyword
= RID_MAX
;
419 token
->pragma_kind
= PRAGMA_NONE
;
420 token
->in_system_header
= in_system_header
;
422 /* On some systems, some header files are surrounded by an
423 implicit extern "C" block. Set a flag in the token if it
424 comes from such a header. */
425 is_extern_c
+= pending_lang_change
;
426 pending_lang_change
= 0;
427 token
->implicit_extern_c
= is_extern_c
> 0;
429 /* Check to see if this token is a keyword. */
430 if (token
->type
== CPP_NAME
)
432 if (C_IS_RESERVED_WORD (token
->u
.value
))
434 /* Mark this token as a keyword. */
435 token
->type
= CPP_KEYWORD
;
436 /* Record which keyword. */
437 token
->keyword
= C_RID_CODE (token
->u
.value
);
438 /* Update the value. Some keywords are mapped to particular
439 entities, rather than simply having the value of the
440 corresponding IDENTIFIER_NODE. For example, `__const' is
441 mapped to `const'. */
442 token
->u
.value
= ridpointers
[token
->keyword
];
446 if (warn_cxx0x_compat
447 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
448 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
450 /* Warn about the C++0x keyword (but still treat it as
452 warning (OPT_Wc__0x_compat
,
453 "identifier %<%s%> will become a keyword in C++0x",
454 IDENTIFIER_POINTER (token
->u
.value
));
456 /* Clear out the C_RID_CODE so we don't warn about this
457 particular identifier-turned-keyword again. */
458 C_RID_CODE (token
->u
.value
) = RID_MAX
;
461 token
->ambiguous_p
= false;
462 token
->keyword
= RID_MAX
;
465 /* Handle Objective-C++ keywords. */
466 else if (token
->type
== CPP_AT_NAME
)
468 token
->type
= CPP_KEYWORD
;
469 switch (C_RID_CODE (token
->u
.value
))
471 /* Map 'class' to '@class', 'private' to '@private', etc. */
472 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
473 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
474 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
475 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
476 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
477 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
478 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
479 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
482 else if (token
->type
== CPP_PRAGMA
)
484 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
485 token
->pragma_kind
= TREE_INT_CST_LOW (token
->u
.value
);
486 token
->u
.value
= NULL_TREE
;
490 /* Update the globals input_location and in_system_header and the
491 input file stack from TOKEN. */
493 cp_lexer_set_source_position_from_token (cp_token
*token
)
495 if (token
->type
!= CPP_EOF
)
497 input_location
= token
->location
;
498 in_system_header
= token
->in_system_header
;
499 restore_input_file_stack (token
->input_file_stack_index
);
503 /* Return a pointer to the next token in the token stream, but do not
506 static inline cp_token
*
507 cp_lexer_peek_token (cp_lexer
*lexer
)
509 if (cp_lexer_debugging_p (lexer
))
511 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
512 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
513 putc ('\n', cp_lexer_debug_stream
);
515 return lexer
->next_token
;
518 /* Return true if the next token has the indicated TYPE. */
521 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
523 return cp_lexer_peek_token (lexer
)->type
== type
;
526 /* Return true if the next token does not have the indicated TYPE. */
529 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
531 return !cp_lexer_next_token_is (lexer
, type
);
534 /* Return true if the next token is the indicated KEYWORD. */
537 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
539 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
542 /* Return true if the next token is a keyword for a decl-specifier. */
545 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
549 token
= cp_lexer_peek_token (lexer
);
550 switch (token
->keyword
)
552 /* Storage classes. */
559 /* Elaborated type specifiers. */
565 /* Simple type specifiers. */
577 /* GNU 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
)
613 token
= (cp_token
*)&eof_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
= (cp_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
)
686 tok
= (cp_token
*)&eof_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
*);
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
)
965 cp_declarator
*declarator
;
967 declarator
= make_declarator (cdk_reference
);
968 declarator
->declarator
= target
;
969 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
970 declarator
->u
.pointer
.class_type
= NULL_TREE
;
973 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
974 target
->parameter_pack_p
= false;
977 declarator
->parameter_pack_p
= false;
982 /* Like make_pointer_declarator -- but for a pointer to a non-static
983 member of CLASS_TYPE. */
986 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
987 cp_declarator
*pointee
)
989 cp_declarator
*declarator
;
991 declarator
= make_declarator (cdk_ptrmem
);
992 declarator
->declarator
= pointee
;
993 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
994 declarator
->u
.pointer
.class_type
= class_type
;
998 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
999 pointee
->parameter_pack_p
= false;
1002 declarator
->parameter_pack_p
= false;
1007 /* Make a declarator for the function given by TARGET, with the
1008 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1009 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1010 indicates what exceptions can be thrown. */
1013 make_call_declarator (cp_declarator
*target
,
1014 cp_parameter_declarator
*parms
,
1015 cp_cv_quals cv_qualifiers
,
1016 tree exception_specification
)
1018 cp_declarator
*declarator
;
1020 declarator
= make_declarator (cdk_function
);
1021 declarator
->declarator
= target
;
1022 declarator
->u
.function
.parameters
= parms
;
1023 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1024 declarator
->u
.function
.exception_specification
= exception_specification
;
1027 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1028 target
->parameter_pack_p
= false;
1031 declarator
->parameter_pack_p
= false;
1036 /* Make a declarator for an array of BOUNDS elements, each of which is
1037 defined by ELEMENT. */
1040 make_array_declarator (cp_declarator
*element
, tree bounds
)
1042 cp_declarator
*declarator
;
1044 declarator
= make_declarator (cdk_array
);
1045 declarator
->declarator
= element
;
1046 declarator
->u
.array
.bounds
= bounds
;
1049 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1050 element
->parameter_pack_p
= false;
1053 declarator
->parameter_pack_p
= false;
1058 cp_parameter_declarator
*no_parameters
;
1060 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1061 DECLARATOR and DEFAULT_ARGUMENT. */
1063 cp_parameter_declarator
*
1064 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1065 cp_declarator
*declarator
,
1066 tree default_argument
)
1068 cp_parameter_declarator
*parameter
;
1070 parameter
= ((cp_parameter_declarator
*)
1071 alloc_declarator (sizeof (cp_parameter_declarator
)));
1072 parameter
->next
= NULL
;
1073 if (decl_specifiers
)
1074 parameter
->decl_specifiers
= *decl_specifiers
;
1076 clear_decl_specs (¶meter
->decl_specifiers
);
1077 parameter
->declarator
= declarator
;
1078 parameter
->default_argument
= default_argument
;
1079 parameter
->ellipsis_p
= false;
1084 /* Returns true iff DECLARATOR is a declaration for a function. */
1087 function_declarator_p (const cp_declarator
*declarator
)
1091 if (declarator
->kind
== cdk_function
1092 && declarator
->declarator
->kind
== cdk_id
)
1094 if (declarator
->kind
== cdk_id
1095 || declarator
->kind
== cdk_error
)
1097 declarator
= declarator
->declarator
;
1107 A cp_parser parses the token stream as specified by the C++
1108 grammar. Its job is purely parsing, not semantic analysis. For
1109 example, the parser breaks the token stream into declarators,
1110 expressions, statements, and other similar syntactic constructs.
1111 It does not check that the types of the expressions on either side
1112 of an assignment-statement are compatible, or that a function is
1113 not declared with a parameter of type `void'.
1115 The parser invokes routines elsewhere in the compiler to perform
1116 semantic analysis and to build up the abstract syntax tree for the
1119 The parser (and the template instantiation code, which is, in a
1120 way, a close relative of parsing) are the only parts of the
1121 compiler that should be calling push_scope and pop_scope, or
1122 related functions. The parser (and template instantiation code)
1123 keeps track of what scope is presently active; everything else
1124 should simply honor that. (The code that generates static
1125 initializers may also need to set the scope, in order to check
1126 access control correctly when emitting the initializers.)
1131 The parser is of the standard recursive-descent variety. Upcoming
1132 tokens in the token stream are examined in order to determine which
1133 production to use when parsing a non-terminal. Some C++ constructs
1134 require arbitrary look ahead to disambiguate. For example, it is
1135 impossible, in the general case, to tell whether a statement is an
1136 expression or declaration without scanning the entire statement.
1137 Therefore, the parser is capable of "parsing tentatively." When the
1138 parser is not sure what construct comes next, it enters this mode.
1139 Then, while we attempt to parse the construct, the parser queues up
1140 error messages, rather than issuing them immediately, and saves the
1141 tokens it consumes. If the construct is parsed successfully, the
1142 parser "commits", i.e., it issues any queued error messages and
1143 the tokens that were being preserved are permanently discarded.
1144 If, however, the construct is not parsed successfully, the parser
1145 rolls back its state completely so that it can resume parsing using
1146 a different alternative.
1151 The performance of the parser could probably be improved substantially.
1152 We could often eliminate the need to parse tentatively by looking ahead
1153 a little bit. In some places, this approach might not entirely eliminate
1154 the need to parse tentatively, but it might still speed up the average
1157 /* Flags that are passed to some parsing functions. These values can
1158 be bitwise-ored together. */
1160 typedef enum cp_parser_flags
1163 CP_PARSER_FLAGS_NONE
= 0x0,
1164 /* The construct is optional. If it is not present, then no error
1165 should be issued. */
1166 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1167 /* When parsing a type-specifier, do not allow user-defined types. */
1168 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2
1171 /* The different kinds of declarators we want to parse. */
1173 typedef enum cp_parser_declarator_kind
1175 /* We want an abstract declarator. */
1176 CP_PARSER_DECLARATOR_ABSTRACT
,
1177 /* We want a named declarator. */
1178 CP_PARSER_DECLARATOR_NAMED
,
1179 /* We don't mind, but the name must be an unqualified-id. */
1180 CP_PARSER_DECLARATOR_EITHER
1181 } cp_parser_declarator_kind
;
1183 /* The precedence values used to parse binary expressions. The minimum value
1184 of PREC must be 1, because zero is reserved to quickly discriminate
1185 binary operators from other tokens. */
1190 PREC_LOGICAL_OR_EXPRESSION
,
1191 PREC_LOGICAL_AND_EXPRESSION
,
1192 PREC_INCLUSIVE_OR_EXPRESSION
,
1193 PREC_EXCLUSIVE_OR_EXPRESSION
,
1194 PREC_AND_EXPRESSION
,
1195 PREC_EQUALITY_EXPRESSION
,
1196 PREC_RELATIONAL_EXPRESSION
,
1197 PREC_SHIFT_EXPRESSION
,
1198 PREC_ADDITIVE_EXPRESSION
,
1199 PREC_MULTIPLICATIVE_EXPRESSION
,
1201 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1204 /* A mapping from a token type to a corresponding tree node type, with a
1205 precedence value. */
1207 typedef struct cp_parser_binary_operations_map_node
1209 /* The token type. */
1210 enum cpp_ttype token_type
;
1211 /* The corresponding tree code. */
1212 enum tree_code tree_type
;
1213 /* The precedence of this operator. */
1214 enum cp_parser_prec prec
;
1215 } cp_parser_binary_operations_map_node
;
1217 /* The status of a tentative parse. */
1219 typedef enum cp_parser_status_kind
1221 /* No errors have occurred. */
1222 CP_PARSER_STATUS_KIND_NO_ERROR
,
1223 /* An error has occurred. */
1224 CP_PARSER_STATUS_KIND_ERROR
,
1225 /* We are committed to this tentative parse, whether or not an error
1227 CP_PARSER_STATUS_KIND_COMMITTED
1228 } cp_parser_status_kind
;
1230 typedef struct cp_parser_expression_stack_entry
1232 /* Left hand side of the binary operation we are currently
1235 /* Original tree code for left hand side, if it was a binary
1236 expression itself (used for -Wparentheses). */
1237 enum tree_code lhs_type
;
1238 /* Tree code for the binary operation we are parsing. */
1239 enum tree_code tree_type
;
1240 /* Precedence of the binary operation we are parsing. */
1242 } cp_parser_expression_stack_entry
;
1244 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1245 entries because precedence levels on the stack are monotonically
1247 typedef struct cp_parser_expression_stack_entry
1248 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1250 /* Context that is saved and restored when parsing tentatively. */
1251 typedef struct cp_parser_context
GTY (())
1253 /* If this is a tentative parsing context, the status of the
1255 enum cp_parser_status_kind status
;
1256 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1257 that are looked up in this context must be looked up both in the
1258 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1259 the context of the containing expression. */
1262 /* The next parsing context in the stack. */
1263 struct cp_parser_context
*next
;
1264 } cp_parser_context
;
1268 /* Constructors and destructors. */
1270 static cp_parser_context
*cp_parser_context_new
1271 (cp_parser_context
*);
1273 /* Class variables. */
1275 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1277 /* The operator-precedence table used by cp_parser_binary_expression.
1278 Transformed into an associative array (binops_by_token) by
1281 static const cp_parser_binary_operations_map_node binops
[] = {
1282 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1283 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1285 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1286 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1287 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1289 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1290 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1292 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1293 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1295 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1296 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1297 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1298 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1300 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1301 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1303 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1305 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1307 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1309 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1311 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1314 /* The same as binops, but initialized by cp_parser_new so that
1315 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1317 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1319 /* Constructors and destructors. */
1321 /* Construct a new context. The context below this one on the stack
1322 is given by NEXT. */
1324 static cp_parser_context
*
1325 cp_parser_context_new (cp_parser_context
* next
)
1327 cp_parser_context
*context
;
1329 /* Allocate the storage. */
1330 if (cp_parser_context_free_list
!= NULL
)
1332 /* Pull the first entry from the free list. */
1333 context
= cp_parser_context_free_list
;
1334 cp_parser_context_free_list
= context
->next
;
1335 memset (context
, 0, sizeof (*context
));
1338 context
= GGC_CNEW (cp_parser_context
);
1340 /* No errors have occurred yet in this context. */
1341 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1342 /* If this is not the bottomost context, copy information that we
1343 need from the previous context. */
1346 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1347 expression, then we are parsing one in this context, too. */
1348 context
->object_type
= next
->object_type
;
1349 /* Thread the stack. */
1350 context
->next
= next
;
1356 /* The cp_parser structure represents the C++ parser. */
1358 typedef struct cp_parser
GTY(())
1360 /* The lexer from which we are obtaining tokens. */
1363 /* The scope in which names should be looked up. If NULL_TREE, then
1364 we look up names in the scope that is currently open in the
1365 source program. If non-NULL, this is either a TYPE or
1366 NAMESPACE_DECL for the scope in which we should look. It can
1367 also be ERROR_MARK, when we've parsed a bogus scope.
1369 This value is not cleared automatically after a name is looked
1370 up, so we must be careful to clear it before starting a new look
1371 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1372 will look up `Z' in the scope of `X', rather than the current
1373 scope.) Unfortunately, it is difficult to tell when name lookup
1374 is complete, because we sometimes peek at a token, look it up,
1375 and then decide not to consume it. */
1378 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1379 last lookup took place. OBJECT_SCOPE is used if an expression
1380 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1381 respectively. QUALIFYING_SCOPE is used for an expression of the
1382 form "X::Y"; it refers to X. */
1384 tree qualifying_scope
;
1386 /* A stack of parsing contexts. All but the bottom entry on the
1387 stack will be tentative contexts.
1389 We parse tentatively in order to determine which construct is in
1390 use in some situations. For example, in order to determine
1391 whether a statement is an expression-statement or a
1392 declaration-statement we parse it tentatively as a
1393 declaration-statement. If that fails, we then reparse the same
1394 token stream as an expression-statement. */
1395 cp_parser_context
*context
;
1397 /* True if we are parsing GNU C++. If this flag is not set, then
1398 GNU extensions are not recognized. */
1399 bool allow_gnu_extensions_p
;
1401 /* TRUE if the `>' token should be interpreted as the greater-than
1402 operator. FALSE if it is the end of a template-id or
1403 template-parameter-list. In C++0x mode, this flag also applies to
1404 `>>' tokens, which are viewed as two consecutive `>' tokens when
1405 this flag is FALSE. */
1406 bool greater_than_is_operator_p
;
1408 /* TRUE if default arguments are allowed within a parameter list
1409 that starts at this point. FALSE if only a gnu extension makes
1410 them permissible. */
1411 bool default_arg_ok_p
;
1413 /* TRUE if we are parsing an integral constant-expression. See
1414 [expr.const] for a precise definition. */
1415 bool integral_constant_expression_p
;
1417 /* TRUE if we are parsing an integral constant-expression -- but a
1418 non-constant expression should be permitted as well. This flag
1419 is used when parsing an array bound so that GNU variable-length
1420 arrays are tolerated. */
1421 bool allow_non_integral_constant_expression_p
;
1423 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1424 been seen that makes the expression non-constant. */
1425 bool non_integral_constant_expression_p
;
1427 /* TRUE if local variable names and `this' are forbidden in the
1429 bool local_variables_forbidden_p
;
1431 /* TRUE if the declaration we are parsing is part of a
1432 linkage-specification of the form `extern string-literal
1434 bool in_unbraced_linkage_specification_p
;
1436 /* TRUE if we are presently parsing a declarator, after the
1437 direct-declarator. */
1438 bool in_declarator_p
;
1440 /* TRUE if we are presently parsing a template-argument-list. */
1441 bool in_template_argument_list_p
;
1443 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1444 to IN_OMP_BLOCK if parsing OpenMP structured block and
1445 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1446 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1447 iteration-statement, OpenMP block or loop within that switch. */
1448 #define IN_SWITCH_STMT 1
1449 #define IN_ITERATION_STMT 2
1450 #define IN_OMP_BLOCK 4
1451 #define IN_OMP_FOR 8
1452 #define IN_IF_STMT 16
1453 unsigned char in_statement
;
1455 /* TRUE if we are presently parsing the body of a switch statement.
1456 Note that this doesn't quite overlap with in_statement above.
1457 The difference relates to giving the right sets of error messages:
1458 "case not in switch" vs "break statement used with OpenMP...". */
1459 bool in_switch_statement_p
;
1461 /* TRUE if we are parsing a type-id in an expression context. In
1462 such a situation, both "type (expr)" and "type (type)" are valid
1464 bool in_type_id_in_expr_p
;
1466 /* TRUE if we are currently in a header file where declarations are
1467 implicitly extern "C". */
1468 bool implicit_extern_c
;
1470 /* TRUE if strings in expressions should be translated to the execution
1472 bool translate_strings_p
;
1474 /* TRUE if we are presently parsing the body of a function, but not
1476 bool in_function_body
;
1478 /* If non-NULL, then we are parsing a construct where new type
1479 definitions are not permitted. The string stored here will be
1480 issued as an error message if a type is defined. */
1481 const char *type_definition_forbidden_message
;
1483 /* A list of lists. The outer list is a stack, used for member
1484 functions of local classes. At each level there are two sub-list,
1485 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1486 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1487 TREE_VALUE's. The functions are chained in reverse declaration
1490 The TREE_PURPOSE sublist contains those functions with default
1491 arguments that need post processing, and the TREE_VALUE sublist
1492 contains those functions with definitions that need post
1495 These lists can only be processed once the outermost class being
1496 defined is complete. */
1497 tree unparsed_functions_queues
;
1499 /* The number of classes whose definitions are currently in
1501 unsigned num_classes_being_defined
;
1503 /* The number of template parameter lists that apply directly to the
1504 current declaration. */
1505 unsigned num_template_parameter_lists
;
1510 /* Constructors and destructors. */
1512 static cp_parser
*cp_parser_new
1515 /* Routines to parse various constructs.
1517 Those that return `tree' will return the error_mark_node (rather
1518 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1519 Sometimes, they will return an ordinary node if error-recovery was
1520 attempted, even though a parse error occurred. So, to check
1521 whether or not a parse error occurred, you should always use
1522 cp_parser_error_occurred. If the construct is optional (indicated
1523 either by an `_opt' in the name of the function that does the
1524 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1525 the construct is not present. */
1527 /* Lexical conventions [gram.lex] */
1529 static tree cp_parser_identifier
1531 static tree cp_parser_string_literal
1532 (cp_parser
*, bool, bool);
1534 /* Basic concepts [gram.basic] */
1536 static bool cp_parser_translation_unit
1539 /* Expressions [gram.expr] */
1541 static tree cp_parser_primary_expression
1542 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1543 static tree cp_parser_id_expression
1544 (cp_parser
*, bool, bool, bool *, bool, bool);
1545 static tree cp_parser_unqualified_id
1546 (cp_parser
*, bool, bool, bool, bool);
1547 static tree cp_parser_nested_name_specifier_opt
1548 (cp_parser
*, bool, bool, bool, bool);
1549 static tree cp_parser_nested_name_specifier
1550 (cp_parser
*, bool, bool, bool, bool);
1551 static tree cp_parser_class_or_namespace_name
1552 (cp_parser
*, bool, bool, bool, bool, bool);
1553 static tree cp_parser_postfix_expression
1554 (cp_parser
*, bool, bool);
1555 static tree cp_parser_postfix_open_square_expression
1556 (cp_parser
*, tree
, bool);
1557 static tree cp_parser_postfix_dot_deref_expression
1558 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*);
1559 static tree cp_parser_parenthesized_expression_list
1560 (cp_parser
*, bool, bool, bool, bool *);
1561 static void cp_parser_pseudo_destructor_name
1562 (cp_parser
*, tree
*, tree
*);
1563 static tree cp_parser_unary_expression
1564 (cp_parser
*, bool, bool);
1565 static enum tree_code cp_parser_unary_operator
1567 static tree cp_parser_new_expression
1569 static tree cp_parser_new_placement
1571 static tree cp_parser_new_type_id
1572 (cp_parser
*, tree
*);
1573 static cp_declarator
*cp_parser_new_declarator_opt
1575 static cp_declarator
*cp_parser_direct_new_declarator
1577 static tree cp_parser_new_initializer
1579 static tree cp_parser_delete_expression
1581 static tree cp_parser_cast_expression
1582 (cp_parser
*, bool, bool);
1583 static tree cp_parser_binary_expression
1584 (cp_parser
*, bool);
1585 static tree cp_parser_question_colon_clause
1586 (cp_parser
*, tree
);
1587 static tree cp_parser_assignment_expression
1588 (cp_parser
*, bool);
1589 static enum tree_code cp_parser_assignment_operator_opt
1591 static tree cp_parser_expression
1592 (cp_parser
*, bool);
1593 static tree cp_parser_constant_expression
1594 (cp_parser
*, bool, bool *);
1595 static tree cp_parser_builtin_offsetof
1598 /* Statements [gram.stmt.stmt] */
1600 static void cp_parser_statement
1601 (cp_parser
*, tree
, bool, bool *);
1602 static void cp_parser_label_for_labeled_statement
1604 static tree cp_parser_expression_statement
1605 (cp_parser
*, tree
);
1606 static tree cp_parser_compound_statement
1607 (cp_parser
*, tree
, bool);
1608 static void cp_parser_statement_seq_opt
1609 (cp_parser
*, tree
);
1610 static tree cp_parser_selection_statement
1611 (cp_parser
*, bool *);
1612 static tree cp_parser_condition
1614 static tree cp_parser_iteration_statement
1616 static void cp_parser_for_init_statement
1618 static tree cp_parser_jump_statement
1620 static void cp_parser_declaration_statement
1623 static tree cp_parser_implicitly_scoped_statement
1624 (cp_parser
*, bool *);
1625 static void cp_parser_already_scoped_statement
1628 /* Declarations [gram.dcl.dcl] */
1630 static void cp_parser_declaration_seq_opt
1632 static void cp_parser_declaration
1634 static void cp_parser_block_declaration
1635 (cp_parser
*, bool);
1636 static void cp_parser_simple_declaration
1637 (cp_parser
*, bool);
1638 static void cp_parser_decl_specifier_seq
1639 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1640 static tree cp_parser_storage_class_specifier_opt
1642 static tree cp_parser_function_specifier_opt
1643 (cp_parser
*, cp_decl_specifier_seq
*);
1644 static tree cp_parser_type_specifier
1645 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1647 static tree cp_parser_simple_type_specifier
1648 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1649 static tree cp_parser_type_name
1651 static tree cp_parser_elaborated_type_specifier
1652 (cp_parser
*, bool, bool);
1653 static tree cp_parser_enum_specifier
1655 static void cp_parser_enumerator_list
1656 (cp_parser
*, tree
);
1657 static void cp_parser_enumerator_definition
1658 (cp_parser
*, tree
);
1659 static tree cp_parser_namespace_name
1661 static void cp_parser_namespace_definition
1663 static void cp_parser_namespace_body
1665 static tree cp_parser_qualified_namespace_specifier
1667 static void cp_parser_namespace_alias_definition
1669 static bool cp_parser_using_declaration
1670 (cp_parser
*, bool);
1671 static void cp_parser_using_directive
1673 static void cp_parser_asm_definition
1675 static void cp_parser_linkage_specification
1677 static void cp_parser_static_assert
1678 (cp_parser
*, bool);
1680 /* Declarators [gram.dcl.decl] */
1682 static tree cp_parser_init_declarator
1683 (cp_parser
*, cp_decl_specifier_seq
*, VEC (deferred_access_check
,gc
)*, bool, bool, int, bool *);
1684 static cp_declarator
*cp_parser_declarator
1685 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1686 static cp_declarator
*cp_parser_direct_declarator
1687 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1688 static enum tree_code cp_parser_ptr_operator
1689 (cp_parser
*, tree
*, cp_cv_quals
*);
1690 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1692 static tree cp_parser_declarator_id
1693 (cp_parser
*, bool);
1694 static tree cp_parser_type_id
1696 static void cp_parser_type_specifier_seq
1697 (cp_parser
*, bool, cp_decl_specifier_seq
*);
1698 static cp_parameter_declarator
*cp_parser_parameter_declaration_clause
1700 static cp_parameter_declarator
*cp_parser_parameter_declaration_list
1701 (cp_parser
*, bool *);
1702 static cp_parameter_declarator
*cp_parser_parameter_declaration
1703 (cp_parser
*, bool, bool *);
1704 static void cp_parser_function_body
1706 static tree cp_parser_initializer
1707 (cp_parser
*, bool *, bool *);
1708 static tree cp_parser_initializer_clause
1709 (cp_parser
*, bool *);
1710 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1711 (cp_parser
*, bool *);
1713 static bool cp_parser_ctor_initializer_opt_and_function_body
1716 /* Classes [gram.class] */
1718 static tree cp_parser_class_name
1719 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1720 static tree cp_parser_class_specifier
1722 static tree cp_parser_class_head
1723 (cp_parser
*, bool *, tree
*, tree
*);
1724 static enum tag_types cp_parser_class_key
1726 static void cp_parser_member_specification_opt
1728 static void cp_parser_member_declaration
1730 static tree cp_parser_pure_specifier
1732 static tree cp_parser_constant_initializer
1735 /* Derived classes [gram.class.derived] */
1737 static tree cp_parser_base_clause
1739 static tree cp_parser_base_specifier
1742 /* Special member functions [gram.special] */
1744 static tree cp_parser_conversion_function_id
1746 static tree cp_parser_conversion_type_id
1748 static cp_declarator
*cp_parser_conversion_declarator_opt
1750 static bool cp_parser_ctor_initializer_opt
1752 static void cp_parser_mem_initializer_list
1754 static tree cp_parser_mem_initializer
1756 static tree cp_parser_mem_initializer_id
1759 /* Overloading [gram.over] */
1761 static tree cp_parser_operator_function_id
1763 static tree cp_parser_operator
1766 /* Templates [gram.temp] */
1768 static void cp_parser_template_declaration
1769 (cp_parser
*, bool);
1770 static tree cp_parser_template_parameter_list
1772 static tree cp_parser_template_parameter
1773 (cp_parser
*, bool *, bool *);
1774 static tree cp_parser_type_parameter
1775 (cp_parser
*, bool *);
1776 static tree cp_parser_template_id
1777 (cp_parser
*, bool, bool, bool);
1778 static tree cp_parser_template_name
1779 (cp_parser
*, bool, bool, bool, bool *);
1780 static tree cp_parser_template_argument_list
1782 static tree cp_parser_template_argument
1784 static void cp_parser_explicit_instantiation
1786 static void cp_parser_explicit_specialization
1789 /* Exception handling [gram.exception] */
1791 static tree cp_parser_try_block
1793 static bool cp_parser_function_try_block
1795 static void cp_parser_handler_seq
1797 static void cp_parser_handler
1799 static tree cp_parser_exception_declaration
1801 static tree cp_parser_throw_expression
1803 static tree cp_parser_exception_specification_opt
1805 static tree cp_parser_type_id_list
1808 /* GNU Extensions */
1810 static tree cp_parser_asm_specification_opt
1812 static tree cp_parser_asm_operand_list
1814 static tree cp_parser_asm_clobber_list
1816 static tree cp_parser_attributes_opt
1818 static tree cp_parser_attribute_list
1820 static bool cp_parser_extension_opt
1821 (cp_parser
*, int *);
1822 static void cp_parser_label_declaration
1825 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
1826 static bool cp_parser_pragma
1827 (cp_parser
*, enum pragma_context
);
1829 /* Objective-C++ Productions */
1831 static tree cp_parser_objc_message_receiver
1833 static tree cp_parser_objc_message_args
1835 static tree cp_parser_objc_message_expression
1837 static tree cp_parser_objc_encode_expression
1839 static tree cp_parser_objc_defs_expression
1841 static tree cp_parser_objc_protocol_expression
1843 static tree cp_parser_objc_selector_expression
1845 static tree cp_parser_objc_expression
1847 static bool cp_parser_objc_selector_p
1849 static tree cp_parser_objc_selector
1851 static tree cp_parser_objc_protocol_refs_opt
1853 static void cp_parser_objc_declaration
1855 static tree cp_parser_objc_statement
1858 /* Utility Routines */
1860 static tree cp_parser_lookup_name
1861 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*);
1862 static tree cp_parser_lookup_name_simple
1863 (cp_parser
*, tree
);
1864 static tree cp_parser_maybe_treat_template_as_class
1866 static bool cp_parser_check_declarator_template_parameters
1867 (cp_parser
*, cp_declarator
*);
1868 static bool cp_parser_check_template_parameters
1869 (cp_parser
*, unsigned);
1870 static tree cp_parser_simple_cast_expression
1872 static tree cp_parser_global_scope_opt
1873 (cp_parser
*, bool);
1874 static bool cp_parser_constructor_declarator_p
1875 (cp_parser
*, bool);
1876 static tree cp_parser_function_definition_from_specifiers_and_declarator
1877 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1878 static tree cp_parser_function_definition_after_declarator
1879 (cp_parser
*, bool);
1880 static void cp_parser_template_declaration_after_export
1881 (cp_parser
*, bool);
1882 static void cp_parser_perform_template_parameter_access_checks
1883 (VEC (deferred_access_check
,gc
)*);
1884 static tree cp_parser_single_declaration
1885 (cp_parser
*, VEC (deferred_access_check
,gc
)*, bool, bool *);
1886 static tree cp_parser_functional_cast
1887 (cp_parser
*, tree
);
1888 static tree cp_parser_save_member_function_body
1889 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1890 static tree cp_parser_enclosed_template_argument_list
1892 static void cp_parser_save_default_args
1893 (cp_parser
*, tree
);
1894 static void cp_parser_late_parsing_for_member
1895 (cp_parser
*, tree
);
1896 static void cp_parser_late_parsing_default_args
1897 (cp_parser
*, tree
);
1898 static tree cp_parser_sizeof_operand
1899 (cp_parser
*, enum rid
);
1900 static bool cp_parser_declares_only_class_p
1902 static void cp_parser_set_storage_class
1903 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
);
1904 static void cp_parser_set_decl_spec_type
1905 (cp_decl_specifier_seq
*, tree
, bool);
1906 static bool cp_parser_friend_p
1907 (const cp_decl_specifier_seq
*);
1908 static cp_token
*cp_parser_require
1909 (cp_parser
*, enum cpp_ttype
, const char *);
1910 static cp_token
*cp_parser_require_keyword
1911 (cp_parser
*, enum rid
, const char *);
1912 static bool cp_parser_token_starts_function_definition_p
1914 static bool cp_parser_next_token_starts_class_definition_p
1916 static bool cp_parser_next_token_ends_template_argument_p
1918 static bool cp_parser_nth_token_starts_template_argument_list_p
1919 (cp_parser
*, size_t);
1920 static enum tag_types cp_parser_token_is_class_key
1922 static void cp_parser_check_class_key
1923 (enum tag_types
, tree type
);
1924 static void cp_parser_check_access_in_redeclaration
1926 static bool cp_parser_optional_template_keyword
1928 static void cp_parser_pre_parsed_nested_name_specifier
1930 static void cp_parser_cache_group
1931 (cp_parser
*, enum cpp_ttype
, unsigned);
1932 static void cp_parser_parse_tentatively
1934 static void cp_parser_commit_to_tentative_parse
1936 static void cp_parser_abort_tentative_parse
1938 static bool cp_parser_parse_definitely
1940 static inline bool cp_parser_parsing_tentatively
1942 static bool cp_parser_uncommitted_to_tentative_parse_p
1944 static void cp_parser_error
1945 (cp_parser
*, const char *);
1946 static void cp_parser_name_lookup_error
1947 (cp_parser
*, tree
, tree
, const char *);
1948 static bool cp_parser_simulate_error
1950 static bool cp_parser_check_type_definition
1952 static void cp_parser_check_for_definition_in_return_type
1953 (cp_declarator
*, tree
);
1954 static void cp_parser_check_for_invalid_template_id
1955 (cp_parser
*, tree
);
1956 static bool cp_parser_non_integral_constant_expression
1957 (cp_parser
*, const char *);
1958 static void cp_parser_diagnose_invalid_type_name
1959 (cp_parser
*, tree
, tree
);
1960 static bool cp_parser_parse_and_diagnose_invalid_type_name
1962 static int cp_parser_skip_to_closing_parenthesis
1963 (cp_parser
*, bool, bool, bool);
1964 static void cp_parser_skip_to_end_of_statement
1966 static void cp_parser_consume_semicolon_at_end_of_statement
1968 static void cp_parser_skip_to_end_of_block_or_statement
1970 static void cp_parser_skip_to_closing_brace
1972 static void cp_parser_skip_to_end_of_template_parameter_list
1974 static void cp_parser_skip_to_pragma_eol
1975 (cp_parser
*, cp_token
*);
1976 static bool cp_parser_error_occurred
1978 static bool cp_parser_allow_gnu_extensions_p
1980 static bool cp_parser_is_string_literal
1982 static bool cp_parser_is_keyword
1983 (cp_token
*, enum rid
);
1984 static tree cp_parser_make_typename_type
1985 (cp_parser
*, tree
, tree
);
1987 /* Returns nonzero if we are parsing tentatively. */
1990 cp_parser_parsing_tentatively (cp_parser
* parser
)
1992 return parser
->context
->next
!= NULL
;
1995 /* Returns nonzero if TOKEN is a string literal. */
1998 cp_parser_is_string_literal (cp_token
* token
)
2000 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
2003 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2006 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2008 return token
->keyword
== keyword
;
2011 /* If not parsing tentatively, issue a diagnostic of the form
2012 FILE:LINE: MESSAGE before TOKEN
2013 where TOKEN is the next token in the input stream. MESSAGE
2014 (specified by the caller) is usually of the form "expected
2018 cp_parser_error (cp_parser
* parser
, const char* message
)
2020 if (!cp_parser_simulate_error (parser
))
2022 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2023 /* This diagnostic makes more sense if it is tagged to the line
2024 of the token we just peeked at. */
2025 cp_lexer_set_source_position_from_token (token
);
2027 if (token
->type
== CPP_PRAGMA
)
2029 error ("%<#pragma%> is not allowed here");
2030 cp_parser_skip_to_pragma_eol (parser
, token
);
2034 c_parse_error (message
,
2035 /* Because c_parser_error does not understand
2036 CPP_KEYWORD, keywords are treated like
2038 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2043 /* Issue an error about name-lookup failing. NAME is the
2044 IDENTIFIER_NODE DECL is the result of
2045 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2046 the thing that we hoped to find. */
2049 cp_parser_name_lookup_error (cp_parser
* parser
,
2052 const char* desired
)
2054 /* If name lookup completely failed, tell the user that NAME was not
2056 if (decl
== error_mark_node
)
2058 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2059 error ("%<%E::%E%> has not been declared",
2060 parser
->scope
, name
);
2061 else if (parser
->scope
== global_namespace
)
2062 error ("%<::%E%> has not been declared", name
);
2063 else if (parser
->object_scope
2064 && !CLASS_TYPE_P (parser
->object_scope
))
2065 error ("request for member %qE in non-class type %qT",
2066 name
, parser
->object_scope
);
2067 else if (parser
->object_scope
)
2068 error ("%<%T::%E%> has not been declared",
2069 parser
->object_scope
, name
);
2071 error ("%qE has not been declared", name
);
2073 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2074 error ("%<%E::%E%> %s", parser
->scope
, name
, desired
);
2075 else if (parser
->scope
== global_namespace
)
2076 error ("%<::%E%> %s", name
, desired
);
2078 error ("%qE %s", name
, desired
);
2081 /* If we are parsing tentatively, remember that an error has occurred
2082 during this tentative parse. Returns true if the error was
2083 simulated; false if a message should be issued by the caller. */
2086 cp_parser_simulate_error (cp_parser
* parser
)
2088 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2090 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2096 /* Check for repeated decl-specifiers. */
2099 cp_parser_check_decl_spec (cp_decl_specifier_seq
*decl_specs
)
2103 for (ds
= ds_first
; ds
!= ds_last
; ++ds
)
2105 unsigned count
= decl_specs
->specs
[(int)ds
];
2108 /* The "long" specifier is a special case because of "long long". */
2112 error ("%<long long long%> is too long for GCC");
2113 else if (pedantic
&& !in_system_header
&& warn_long_long
)
2114 pedwarn ("ISO C++ does not support %<long long%>");
2118 static const char *const decl_spec_names
[] = {
2134 error ("duplicate %qs", decl_spec_names
[(int)ds
]);
2139 /* This function is called when a type is defined. If type
2140 definitions are forbidden at this point, an error message is
2144 cp_parser_check_type_definition (cp_parser
* parser
)
2146 /* If types are forbidden here, issue a message. */
2147 if (parser
->type_definition_forbidden_message
)
2149 /* Use `%s' to print the string in case there are any escape
2150 characters in the message. */
2151 error ("%s", parser
->type_definition_forbidden_message
);
2157 /* This function is called when the DECLARATOR is processed. The TYPE
2158 was a type defined in the decl-specifiers. If it is invalid to
2159 define a type in the decl-specifiers for DECLARATOR, an error is
2163 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2166 /* [dcl.fct] forbids type definitions in return types.
2167 Unfortunately, it's not easy to know whether or not we are
2168 processing a return type until after the fact. */
2170 && (declarator
->kind
== cdk_pointer
2171 || declarator
->kind
== cdk_reference
2172 || declarator
->kind
== cdk_ptrmem
))
2173 declarator
= declarator
->declarator
;
2175 && declarator
->kind
== cdk_function
)
2177 error ("new types may not be defined in a return type");
2178 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2183 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2184 "<" in any valid C++ program. If the next token is indeed "<",
2185 issue a message warning the user about what appears to be an
2186 invalid attempt to form a template-id. */
2189 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2192 cp_token_position start
= 0;
2194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2197 error ("%qT is not a template", type
);
2198 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2199 error ("%qE is not a template", type
);
2201 error ("invalid template-id");
2202 /* Remember the location of the invalid "<". */
2203 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2204 start
= cp_lexer_token_position (parser
->lexer
, true);
2205 /* Consume the "<". */
2206 cp_lexer_consume_token (parser
->lexer
);
2207 /* Parse the template arguments. */
2208 cp_parser_enclosed_template_argument_list (parser
);
2209 /* Permanently remove the invalid template arguments so that
2210 this error message is not issued again. */
2212 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2216 /* If parsing an integral constant-expression, issue an error message
2217 about the fact that THING appeared and return true. Otherwise,
2218 return false. In either case, set
2219 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2222 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2225 parser
->non_integral_constant_expression_p
= true;
2226 if (parser
->integral_constant_expression_p
)
2228 if (!parser
->allow_non_integral_constant_expression_p
)
2230 error ("%s cannot appear in a constant-expression", thing
);
2237 /* Emit a diagnostic for an invalid type name. SCOPE is the
2238 qualifying scope (or NULL, if none) for ID. This function commits
2239 to the current active tentative parse, if any. (Otherwise, the
2240 problematic construct might be encountered again later, resulting
2241 in duplicate error messages.) */
2244 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree scope
, tree id
)
2246 tree decl
, old_scope
;
2247 /* Try to lookup the identifier. */
2248 old_scope
= parser
->scope
;
2249 parser
->scope
= scope
;
2250 decl
= cp_parser_lookup_name_simple (parser
, id
);
2251 parser
->scope
= old_scope
;
2252 /* If the lookup found a template-name, it means that the user forgot
2253 to specify an argument list. Emit a useful error message. */
2254 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2255 error ("invalid use of template-name %qE without an argument list", decl
);
2256 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2257 error ("invalid use of destructor %qD as a type", id
);
2258 else if (TREE_CODE (decl
) == TYPE_DECL
)
2259 /* Something like 'unsigned A a;' */
2260 error ("invalid combination of multiple type-specifiers");
2261 else if (!parser
->scope
)
2263 /* Issue an error message. */
2264 error ("%qE does not name a type", id
);
2265 /* If we're in a template class, it's possible that the user was
2266 referring to a type from a base class. For example:
2268 template <typename T> struct A { typedef T X; };
2269 template <typename T> struct B : public A<T> { X x; };
2271 The user should have said "typename A<T>::X". */
2272 if (processing_template_decl
&& current_class_type
2273 && TYPE_BINFO (current_class_type
))
2277 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2281 tree base_type
= BINFO_TYPE (b
);
2282 if (CLASS_TYPE_P (base_type
)
2283 && dependent_type_p (base_type
))
2286 /* Go from a particular instantiation of the
2287 template (which will have an empty TYPE_FIELDs),
2288 to the main version. */
2289 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2290 for (field
= TYPE_FIELDS (base_type
);
2292 field
= TREE_CHAIN (field
))
2293 if (TREE_CODE (field
) == TYPE_DECL
2294 && DECL_NAME (field
) == id
)
2296 inform ("(perhaps %<typename %T::%E%> was intended)",
2297 BINFO_TYPE (b
), id
);
2306 /* Here we diagnose qualified-ids where the scope is actually correct,
2307 but the identifier does not resolve to a valid type name. */
2308 else if (parser
->scope
!= error_mark_node
)
2310 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2311 error ("%qE in namespace %qE does not name a type",
2313 else if (TYPE_P (parser
->scope
))
2314 error ("%qE in class %qT does not name a type", id
, parser
->scope
);
2318 cp_parser_commit_to_tentative_parse (parser
);
2321 /* Check for a common situation where a type-name should be present,
2322 but is not, and issue a sensible error message. Returns true if an
2323 invalid type-name was detected.
2325 The situation handled by this function are variable declarations of the
2326 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2327 Usually, `ID' should name a type, but if we got here it means that it
2328 does not. We try to emit the best possible error message depending on
2329 how exactly the id-expression looks like. */
2332 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2336 cp_parser_parse_tentatively (parser
);
2337 id
= cp_parser_id_expression (parser
,
2338 /*template_keyword_p=*/false,
2339 /*check_dependency_p=*/true,
2340 /*template_p=*/NULL
,
2341 /*declarator_p=*/true,
2342 /*optional_p=*/false);
2343 /* After the id-expression, there should be a plain identifier,
2344 otherwise this is not a simple variable declaration. Also, if
2345 the scope is dependent, we cannot do much. */
2346 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2347 || (parser
->scope
&& TYPE_P (parser
->scope
)
2348 && dependent_type_p (parser
->scope
))
2349 || TREE_CODE (id
) == TYPE_DECL
)
2351 cp_parser_abort_tentative_parse (parser
);
2354 if (!cp_parser_parse_definitely (parser
))
2357 /* Emit a diagnostic for the invalid type. */
2358 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
, id
);
2359 /* Skip to the end of the declaration; there's no point in
2360 trying to process it. */
2361 cp_parser_skip_to_end_of_block_or_statement (parser
);
2365 /* Consume tokens up to, and including, the next non-nested closing `)'.
2366 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2367 are doing error recovery. Returns -1 if OR_COMMA is true and we
2368 found an unnested comma. */
2371 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2376 unsigned paren_depth
= 0;
2377 unsigned brace_depth
= 0;
2379 if (recovering
&& !or_comma
2380 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2385 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2387 switch (token
->type
)
2390 case CPP_PRAGMA_EOL
:
2391 /* If we've run out of tokens, then there is no closing `)'. */
2395 /* This matches the processing in skip_to_end_of_statement. */
2400 case CPP_OPEN_BRACE
:
2403 case CPP_CLOSE_BRACE
:
2409 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
)
2413 case CPP_OPEN_PAREN
:
2418 case CPP_CLOSE_PAREN
:
2419 if (!brace_depth
&& !paren_depth
--)
2422 cp_lexer_consume_token (parser
->lexer
);
2431 /* Consume the token. */
2432 cp_lexer_consume_token (parser
->lexer
);
2436 /* Consume tokens until we reach the end of the current statement.
2437 Normally, that will be just before consuming a `;'. However, if a
2438 non-nested `}' comes first, then we stop before consuming that. */
2441 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2443 unsigned nesting_depth
= 0;
2447 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2449 switch (token
->type
)
2452 case CPP_PRAGMA_EOL
:
2453 /* If we've run out of tokens, stop. */
2457 /* If the next token is a `;', we have reached the end of the
2463 case CPP_CLOSE_BRACE
:
2464 /* If this is a non-nested '}', stop before consuming it.
2465 That way, when confronted with something like:
2469 we stop before consuming the closing '}', even though we
2470 have not yet reached a `;'. */
2471 if (nesting_depth
== 0)
2474 /* If it is the closing '}' for a block that we have
2475 scanned, stop -- but only after consuming the token.
2481 we will stop after the body of the erroneously declared
2482 function, but before consuming the following `typedef'
2484 if (--nesting_depth
== 0)
2486 cp_lexer_consume_token (parser
->lexer
);
2490 case CPP_OPEN_BRACE
:
2498 /* Consume the token. */
2499 cp_lexer_consume_token (parser
->lexer
);
2503 /* This function is called at the end of a statement or declaration.
2504 If the next token is a semicolon, it is consumed; otherwise, error
2505 recovery is attempted. */
2508 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2510 /* Look for the trailing `;'. */
2511 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2513 /* If there is additional (erroneous) input, skip to the end of
2515 cp_parser_skip_to_end_of_statement (parser
);
2516 /* If the next token is now a `;', consume it. */
2517 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2518 cp_lexer_consume_token (parser
->lexer
);
2522 /* Skip tokens until we have consumed an entire block, or until we
2523 have consumed a non-nested `;'. */
2526 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2528 int nesting_depth
= 0;
2530 while (nesting_depth
>= 0)
2532 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2534 switch (token
->type
)
2537 case CPP_PRAGMA_EOL
:
2538 /* If we've run out of tokens, stop. */
2542 /* Stop if this is an unnested ';'. */
2547 case CPP_CLOSE_BRACE
:
2548 /* Stop if this is an unnested '}', or closes the outermost
2555 case CPP_OPEN_BRACE
:
2564 /* Consume the token. */
2565 cp_lexer_consume_token (parser
->lexer
);
2569 /* Skip tokens until a non-nested closing curly brace is the next
2573 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2575 unsigned nesting_depth
= 0;
2579 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2581 switch (token
->type
)
2584 case CPP_PRAGMA_EOL
:
2585 /* If we've run out of tokens, stop. */
2588 case CPP_CLOSE_BRACE
:
2589 /* If the next token is a non-nested `}', then we have reached
2590 the end of the current block. */
2591 if (nesting_depth
-- == 0)
2595 case CPP_OPEN_BRACE
:
2596 /* If it the next token is a `{', then we are entering a new
2597 block. Consume the entire block. */
2605 /* Consume the token. */
2606 cp_lexer_consume_token (parser
->lexer
);
2610 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2611 parameter is the PRAGMA token, allowing us to purge the entire pragma
2615 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
2619 parser
->lexer
->in_pragma
= false;
2622 token
= cp_lexer_consume_token (parser
->lexer
);
2623 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
2625 /* Ensure that the pragma is not parsed again. */
2626 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
2629 /* Require pragma end of line, resyncing with it as necessary. The
2630 arguments are as for cp_parser_skip_to_pragma_eol. */
2633 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
2635 parser
->lexer
->in_pragma
= false;
2636 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, "end of line"))
2637 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
2640 /* This is a simple wrapper around make_typename_type. When the id is
2641 an unresolved identifier node, we can provide a superior diagnostic
2642 using cp_parser_diagnose_invalid_type_name. */
2645 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
, tree id
)
2648 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2650 result
= make_typename_type (scope
, id
, typename_type
,
2651 /*complain=*/tf_none
);
2652 if (result
== error_mark_node
)
2653 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
);
2656 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2660 /* Create a new C++ parser. */
2663 cp_parser_new (void)
2669 /* cp_lexer_new_main is called before calling ggc_alloc because
2670 cp_lexer_new_main might load a PCH file. */
2671 lexer
= cp_lexer_new_main ();
2673 /* Initialize the binops_by_token so that we can get the tree
2674 directly from the token. */
2675 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2676 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2678 parser
= GGC_CNEW (cp_parser
);
2679 parser
->lexer
= lexer
;
2680 parser
->context
= cp_parser_context_new (NULL
);
2682 /* For now, we always accept GNU extensions. */
2683 parser
->allow_gnu_extensions_p
= 1;
2685 /* The `>' token is a greater-than operator, not the end of a
2687 parser
->greater_than_is_operator_p
= true;
2689 parser
->default_arg_ok_p
= true;
2691 /* We are not parsing a constant-expression. */
2692 parser
->integral_constant_expression_p
= false;
2693 parser
->allow_non_integral_constant_expression_p
= false;
2694 parser
->non_integral_constant_expression_p
= false;
2696 /* Local variable names are not forbidden. */
2697 parser
->local_variables_forbidden_p
= false;
2699 /* We are not processing an `extern "C"' declaration. */
2700 parser
->in_unbraced_linkage_specification_p
= false;
2702 /* We are not processing a declarator. */
2703 parser
->in_declarator_p
= false;
2705 /* We are not processing a template-argument-list. */
2706 parser
->in_template_argument_list_p
= false;
2708 /* We are not in an iteration statement. */
2709 parser
->in_statement
= 0;
2711 /* We are not in a switch statement. */
2712 parser
->in_switch_statement_p
= false;
2714 /* We are not parsing a type-id inside an expression. */
2715 parser
->in_type_id_in_expr_p
= false;
2717 /* Declarations aren't implicitly extern "C". */
2718 parser
->implicit_extern_c
= false;
2720 /* String literals should be translated to the execution character set. */
2721 parser
->translate_strings_p
= true;
2723 /* We are not parsing a function body. */
2724 parser
->in_function_body
= false;
2726 /* The unparsed function queue is empty. */
2727 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2729 /* There are no classes being defined. */
2730 parser
->num_classes_being_defined
= 0;
2732 /* No template parameters apply. */
2733 parser
->num_template_parameter_lists
= 0;
2738 /* Create a cp_lexer structure which will emit the tokens in CACHE
2739 and push it onto the parser's lexer stack. This is used for delayed
2740 parsing of in-class method bodies and default arguments, and should
2741 not be confused with tentative parsing. */
2743 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2745 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2746 lexer
->next
= parser
->lexer
;
2747 parser
->lexer
= lexer
;
2749 /* Move the current source position to that of the first token in the
2751 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2754 /* Pop the top lexer off the parser stack. This is never used for the
2755 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2757 cp_parser_pop_lexer (cp_parser
*parser
)
2759 cp_lexer
*lexer
= parser
->lexer
;
2760 parser
->lexer
= lexer
->next
;
2761 cp_lexer_destroy (lexer
);
2763 /* Put the current source position back where it was before this
2764 lexer was pushed. */
2765 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2768 /* Lexical conventions [gram.lex] */
2770 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2774 cp_parser_identifier (cp_parser
* parser
)
2778 /* Look for the identifier. */
2779 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2780 /* Return the value. */
2781 return token
? token
->u
.value
: error_mark_node
;
2784 /* Parse a sequence of adjacent string constants. Returns a
2785 TREE_STRING representing the combined, nul-terminated string
2786 constant. If TRANSLATE is true, translate the string to the
2787 execution character set. If WIDE_OK is true, a wide string is
2790 C++98 [lex.string] says that if a narrow string literal token is
2791 adjacent to a wide string literal token, the behavior is undefined.
2792 However, C99 6.4.5p4 says that this results in a wide string literal.
2793 We follow C99 here, for consistency with the C front end.
2795 This code is largely lifted from lex_string() in c-lex.c.
2797 FUTURE: ObjC++ will need to handle @-strings here. */
2799 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2804 struct obstack str_ob
;
2805 cpp_string str
, istr
, *strs
;
2808 tok
= cp_lexer_peek_token (parser
->lexer
);
2809 if (!cp_parser_is_string_literal (tok
))
2811 cp_parser_error (parser
, "expected string-literal");
2812 return error_mark_node
;
2815 /* Try to avoid the overhead of creating and destroying an obstack
2816 for the common case of just one string. */
2817 if (!cp_parser_is_string_literal
2818 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2820 cp_lexer_consume_token (parser
->lexer
);
2822 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
2823 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
2825 if (tok
->type
== CPP_WSTRING
)
2832 gcc_obstack_init (&str_ob
);
2837 cp_lexer_consume_token (parser
->lexer
);
2839 str
.text
= (unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
2840 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
2841 if (tok
->type
== CPP_WSTRING
)
2844 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
2846 tok
= cp_lexer_peek_token (parser
->lexer
);
2848 while (cp_parser_is_string_literal (tok
));
2850 strs
= (cpp_string
*) obstack_finish (&str_ob
);
2853 if (wide
&& !wide_ok
)
2855 cp_parser_error (parser
, "a wide string is invalid in this context");
2859 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
2860 (parse_in
, strs
, count
, &istr
, wide
))
2862 value
= build_string (istr
.len
, (char *)istr
.text
);
2863 free ((void *)istr
.text
);
2865 TREE_TYPE (value
) = wide
? wchar_array_type_node
: char_array_type_node
;
2866 value
= fix_string_type (value
);
2869 /* cpp_interpret_string has issued an error. */
2870 value
= error_mark_node
;
2873 obstack_free (&str_ob
, 0);
2879 /* Basic concepts [gram.basic] */
2881 /* Parse a translation-unit.
2884 declaration-seq [opt]
2886 Returns TRUE if all went well. */
2889 cp_parser_translation_unit (cp_parser
* parser
)
2891 /* The address of the first non-permanent object on the declarator
2893 static void *declarator_obstack_base
;
2897 /* Create the declarator obstack, if necessary. */
2898 if (!cp_error_declarator
)
2900 gcc_obstack_init (&declarator_obstack
);
2901 /* Create the error declarator. */
2902 cp_error_declarator
= make_declarator (cdk_error
);
2903 /* Create the empty parameter list. */
2904 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
2905 /* Remember where the base of the declarator obstack lies. */
2906 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
2909 cp_parser_declaration_seq_opt (parser
);
2911 /* If there are no tokens left then all went well. */
2912 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2914 /* Get rid of the token array; we don't need it any more. */
2915 cp_lexer_destroy (parser
->lexer
);
2916 parser
->lexer
= NULL
;
2918 /* This file might have been a context that's implicitly extern
2919 "C". If so, pop the lang context. (Only relevant for PCH.) */
2920 if (parser
->implicit_extern_c
)
2922 pop_lang_context ();
2923 parser
->implicit_extern_c
= false;
2927 finish_translation_unit ();
2933 cp_parser_error (parser
, "expected declaration");
2937 /* Make sure the declarator obstack was fully cleaned up. */
2938 gcc_assert (obstack_next_free (&declarator_obstack
)
2939 == declarator_obstack_base
);
2941 /* All went well. */
2945 /* Expressions [gram.expr] */
2947 /* Parse a primary-expression.
2958 ( compound-statement )
2959 __builtin_va_arg ( assignment-expression , type-id )
2960 __builtin_offsetof ( type-id , offsetof-expression )
2962 Objective-C++ Extension:
2970 ADDRESS_P is true iff this expression was immediately preceded by
2971 "&" and therefore might denote a pointer-to-member. CAST_P is true
2972 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2973 true iff this expression is a template argument.
2975 Returns a representation of the expression. Upon return, *IDK
2976 indicates what kind of id-expression (if any) was present. */
2979 cp_parser_primary_expression (cp_parser
*parser
,
2982 bool template_arg_p
,
2987 /* Assume the primary expression is not an id-expression. */
2988 *idk
= CP_ID_KIND_NONE
;
2990 /* Peek at the next token. */
2991 token
= cp_lexer_peek_token (parser
->lexer
);
2992 switch (token
->type
)
3003 token
= cp_lexer_consume_token (parser
->lexer
);
3004 /* Floating-point literals are only allowed in an integral
3005 constant expression if they are cast to an integral or
3006 enumeration type. */
3007 if (TREE_CODE (token
->u
.value
) == REAL_CST
3008 && parser
->integral_constant_expression_p
3011 /* CAST_P will be set even in invalid code like "int(2.7 +
3012 ...)". Therefore, we have to check that the next token
3013 is sure to end the cast. */
3016 cp_token
*next_token
;
3018 next_token
= cp_lexer_peek_token (parser
->lexer
);
3019 if (/* The comma at the end of an
3020 enumerator-definition. */
3021 next_token
->type
!= CPP_COMMA
3022 /* The curly brace at the end of an enum-specifier. */
3023 && next_token
->type
!= CPP_CLOSE_BRACE
3024 /* The end of a statement. */
3025 && next_token
->type
!= CPP_SEMICOLON
3026 /* The end of the cast-expression. */
3027 && next_token
->type
!= CPP_CLOSE_PAREN
3028 /* The end of an array bound. */
3029 && next_token
->type
!= CPP_CLOSE_SQUARE
3030 /* The closing ">" in a template-argument-list. */
3031 && (next_token
->type
!= CPP_GREATER
3032 || parser
->greater_than_is_operator_p
)
3033 /* C++0x only: A ">>" treated like two ">" tokens,
3034 in a template-argument-list. */
3035 && (next_token
->type
!= CPP_RSHIFT
3037 || parser
->greater_than_is_operator_p
))
3041 /* If we are within a cast, then the constraint that the
3042 cast is to an integral or enumeration type will be
3043 checked at that point. If we are not within a cast, then
3044 this code is invalid. */
3046 cp_parser_non_integral_constant_expression
3047 (parser
, "floating-point literal");
3049 return token
->u
.value
;
3053 /* ??? Should wide strings be allowed when parser->translate_strings_p
3054 is false (i.e. in attributes)? If not, we can kill the third
3055 argument to cp_parser_string_literal. */
3056 return cp_parser_string_literal (parser
,
3057 parser
->translate_strings_p
,
3060 case CPP_OPEN_PAREN
:
3063 bool saved_greater_than_is_operator_p
;
3065 /* Consume the `('. */
3066 cp_lexer_consume_token (parser
->lexer
);
3067 /* Within a parenthesized expression, a `>' token is always
3068 the greater-than operator. */
3069 saved_greater_than_is_operator_p
3070 = parser
->greater_than_is_operator_p
;
3071 parser
->greater_than_is_operator_p
= true;
3072 /* If we see `( { ' then we are looking at the beginning of
3073 a GNU statement-expression. */
3074 if (cp_parser_allow_gnu_extensions_p (parser
)
3075 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
3077 /* Statement-expressions are not allowed by the standard. */
3079 pedwarn ("ISO C++ forbids braced-groups within expressions");
3081 /* And they're not allowed outside of a function-body; you
3082 cannot, for example, write:
3084 int i = ({ int j = 3; j + 1; });
3086 at class or namespace scope. */
3087 if (!parser
->in_function_body
)
3089 error ("statement-expressions are allowed only inside functions");
3090 cp_parser_skip_to_end_of_block_or_statement (parser
);
3091 expr
= error_mark_node
;
3095 /* Start the statement-expression. */
3096 expr
= begin_stmt_expr ();
3097 /* Parse the compound-statement. */
3098 cp_parser_compound_statement (parser
, expr
, false);
3100 expr
= finish_stmt_expr (expr
, false);
3105 /* Parse the parenthesized expression. */
3106 expr
= cp_parser_expression (parser
, cast_p
);
3107 /* Let the front end know that this expression was
3108 enclosed in parentheses. This matters in case, for
3109 example, the expression is of the form `A::B', since
3110 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3112 finish_parenthesized_expr (expr
);
3114 /* The `>' token might be the end of a template-id or
3115 template-parameter-list now. */
3116 parser
->greater_than_is_operator_p
3117 = saved_greater_than_is_operator_p
;
3118 /* Consume the `)'. */
3119 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
3120 cp_parser_skip_to_end_of_statement (parser
);
3126 switch (token
->keyword
)
3128 /* These two are the boolean literals. */
3130 cp_lexer_consume_token (parser
->lexer
);
3131 return boolean_true_node
;
3133 cp_lexer_consume_token (parser
->lexer
);
3134 return boolean_false_node
;
3136 /* The `__null' literal. */
3138 cp_lexer_consume_token (parser
->lexer
);
3141 /* Recognize the `this' keyword. */
3143 cp_lexer_consume_token (parser
->lexer
);
3144 if (parser
->local_variables_forbidden_p
)
3146 error ("%<this%> may not be used in this context");
3147 return error_mark_node
;
3149 /* Pointers cannot appear in constant-expressions. */
3150 if (cp_parser_non_integral_constant_expression (parser
,
3152 return error_mark_node
;
3153 return finish_this_expr ();
3155 /* The `operator' keyword can be the beginning of an
3160 case RID_FUNCTION_NAME
:
3161 case RID_PRETTY_FUNCTION_NAME
:
3162 case RID_C99_FUNCTION_NAME
:
3163 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3164 __func__ are the names of variables -- but they are
3165 treated specially. Therefore, they are handled here,
3166 rather than relying on the generic id-expression logic
3167 below. Grammatically, these names are id-expressions.
3169 Consume the token. */
3170 token
= cp_lexer_consume_token (parser
->lexer
);
3171 /* Look up the name. */
3172 return finish_fname (token
->u
.value
);
3179 /* The `__builtin_va_arg' construct is used to handle
3180 `va_arg'. Consume the `__builtin_va_arg' token. */
3181 cp_lexer_consume_token (parser
->lexer
);
3182 /* Look for the opening `('. */
3183 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3184 /* Now, parse the assignment-expression. */
3185 expression
= cp_parser_assignment_expression (parser
,
3187 /* Look for the `,'. */
3188 cp_parser_require (parser
, CPP_COMMA
, "`,'");
3189 /* Parse the type-id. */
3190 type
= cp_parser_type_id (parser
);
3191 /* Look for the closing `)'. */
3192 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3193 /* Using `va_arg' in a constant-expression is not
3195 if (cp_parser_non_integral_constant_expression (parser
,
3197 return error_mark_node
;
3198 return build_x_va_arg (expression
, type
);
3202 return cp_parser_builtin_offsetof (parser
);
3204 /* Objective-C++ expressions. */
3206 case RID_AT_PROTOCOL
:
3207 case RID_AT_SELECTOR
:
3208 return cp_parser_objc_expression (parser
);
3211 cp_parser_error (parser
, "expected primary-expression");
3212 return error_mark_node
;
3215 /* An id-expression can start with either an identifier, a
3216 `::' as the beginning of a qualified-id, or the "operator"
3220 case CPP_TEMPLATE_ID
:
3221 case CPP_NESTED_NAME_SPECIFIER
:
3225 const char *error_msg
;
3230 /* Parse the id-expression. */
3232 = cp_parser_id_expression (parser
,
3233 /*template_keyword_p=*/false,
3234 /*check_dependency_p=*/true,
3236 /*declarator_p=*/false,
3237 /*optional_p=*/false);
3238 if (id_expression
== error_mark_node
)
3239 return error_mark_node
;
3240 token
= cp_lexer_peek_token (parser
->lexer
);
3241 done
= (token
->type
!= CPP_OPEN_SQUARE
3242 && token
->type
!= CPP_OPEN_PAREN
3243 && token
->type
!= CPP_DOT
3244 && token
->type
!= CPP_DEREF
3245 && token
->type
!= CPP_PLUS_PLUS
3246 && token
->type
!= CPP_MINUS_MINUS
);
3247 /* If we have a template-id, then no further lookup is
3248 required. If the template-id was for a template-class, we
3249 will sometimes have a TYPE_DECL at this point. */
3250 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
3251 || TREE_CODE (id_expression
) == TYPE_DECL
)
3252 decl
= id_expression
;
3253 /* Look up the name. */
3256 tree ambiguous_decls
;
3258 decl
= cp_parser_lookup_name (parser
, id_expression
,
3261 /*is_namespace=*/false,
3262 /*check_dependency=*/true,
3264 /* If the lookup was ambiguous, an error will already have
3266 if (ambiguous_decls
)
3267 return error_mark_node
;
3269 /* In Objective-C++, an instance variable (ivar) may be preferred
3270 to whatever cp_parser_lookup_name() found. */
3271 decl
= objc_lookup_ivar (decl
, id_expression
);
3273 /* If name lookup gives us a SCOPE_REF, then the
3274 qualifying scope was dependent. */
3275 if (TREE_CODE (decl
) == SCOPE_REF
)
3277 /* Check to see if DECL is a local variable in a context
3278 where that is forbidden. */
3279 if (parser
->local_variables_forbidden_p
3280 && local_variable_p (decl
))
3282 /* It might be that we only found DECL because we are
3283 trying to be generous with pre-ISO scoping rules.
3284 For example, consider:
3288 for (int i = 0; i < 10; ++i) {}
3289 extern void f(int j = i);
3292 Here, name look up will originally find the out
3293 of scope `i'. We need to issue a warning message,
3294 but then use the global `i'. */
3295 decl
= check_for_out_of_scope_variable (decl
);
3296 if (local_variable_p (decl
))
3298 error ("local variable %qD may not appear in this context",
3300 return error_mark_node
;
3305 decl
= (finish_id_expression
3306 (id_expression
, decl
, parser
->scope
,
3308 parser
->integral_constant_expression_p
,
3309 parser
->allow_non_integral_constant_expression_p
,
3310 &parser
->non_integral_constant_expression_p
,
3311 template_p
, done
, address_p
,
3315 cp_parser_error (parser
, error_msg
);
3319 /* Anything else is an error. */
3321 /* ...unless we have an Objective-C++ message or string literal,
3323 if (c_dialect_objc ()
3324 && (token
->type
== CPP_OPEN_SQUARE
3325 || token
->type
== CPP_OBJC_STRING
))
3326 return cp_parser_objc_expression (parser
);
3328 cp_parser_error (parser
, "expected primary-expression");
3329 return error_mark_node
;
3333 /* Parse an id-expression.
3340 :: [opt] nested-name-specifier template [opt] unqualified-id
3342 :: operator-function-id
3345 Return a representation of the unqualified portion of the
3346 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3347 a `::' or nested-name-specifier.
3349 Often, if the id-expression was a qualified-id, the caller will
3350 want to make a SCOPE_REF to represent the qualified-id. This
3351 function does not do this in order to avoid wastefully creating
3352 SCOPE_REFs when they are not required.
3354 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3357 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3358 uninstantiated templates.
3360 If *TEMPLATE_P is non-NULL, it is set to true iff the
3361 `template' keyword is used to explicitly indicate that the entity
3362 named is a template.
3364 If DECLARATOR_P is true, the id-expression is appearing as part of
3365 a declarator, rather than as part of an expression. */
3368 cp_parser_id_expression (cp_parser
*parser
,
3369 bool template_keyword_p
,
3370 bool check_dependency_p
,
3375 bool global_scope_p
;
3376 bool nested_name_specifier_p
;
3378 /* Assume the `template' keyword was not used. */
3380 *template_p
= template_keyword_p
;
3382 /* Look for the optional `::' operator. */
3384 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3386 /* Look for the optional nested-name-specifier. */
3387 nested_name_specifier_p
3388 = (cp_parser_nested_name_specifier_opt (parser
,
3389 /*typename_keyword_p=*/false,
3394 /* If there is a nested-name-specifier, then we are looking at
3395 the first qualified-id production. */
3396 if (nested_name_specifier_p
)
3399 tree saved_object_scope
;
3400 tree saved_qualifying_scope
;
3401 tree unqualified_id
;
3404 /* See if the next token is the `template' keyword. */
3406 template_p
= &is_template
;
3407 *template_p
= cp_parser_optional_template_keyword (parser
);
3408 /* Name lookup we do during the processing of the
3409 unqualified-id might obliterate SCOPE. */
3410 saved_scope
= parser
->scope
;
3411 saved_object_scope
= parser
->object_scope
;
3412 saved_qualifying_scope
= parser
->qualifying_scope
;
3413 /* Process the final unqualified-id. */
3414 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3417 /*optional_p=*/false);
3418 /* Restore the SAVED_SCOPE for our caller. */
3419 parser
->scope
= saved_scope
;
3420 parser
->object_scope
= saved_object_scope
;
3421 parser
->qualifying_scope
= saved_qualifying_scope
;
3423 return unqualified_id
;
3425 /* Otherwise, if we are in global scope, then we are looking at one
3426 of the other qualified-id productions. */
3427 else if (global_scope_p
)
3432 /* Peek at the next token. */
3433 token
= cp_lexer_peek_token (parser
->lexer
);
3435 /* If it's an identifier, and the next token is not a "<", then
3436 we can avoid the template-id case. This is an optimization
3437 for this common case. */
3438 if (token
->type
== CPP_NAME
3439 && !cp_parser_nth_token_starts_template_argument_list_p
3441 return cp_parser_identifier (parser
);
3443 cp_parser_parse_tentatively (parser
);
3444 /* Try a template-id. */
3445 id
= cp_parser_template_id (parser
,
3446 /*template_keyword_p=*/false,
3447 /*check_dependency_p=*/true,
3449 /* If that worked, we're done. */
3450 if (cp_parser_parse_definitely (parser
))
3453 /* Peek at the next token. (Changes in the token buffer may
3454 have invalidated the pointer obtained above.) */
3455 token
= cp_lexer_peek_token (parser
->lexer
);
3457 switch (token
->type
)
3460 return cp_parser_identifier (parser
);
3463 if (token
->keyword
== RID_OPERATOR
)
3464 return cp_parser_operator_function_id (parser
);
3468 cp_parser_error (parser
, "expected id-expression");
3469 return error_mark_node
;
3473 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3474 /*check_dependency_p=*/true,
3479 /* Parse an unqualified-id.
3483 operator-function-id
3484 conversion-function-id
3488 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3489 keyword, in a construct like `A::template ...'.
3491 Returns a representation of unqualified-id. For the `identifier'
3492 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3493 production a BIT_NOT_EXPR is returned; the operand of the
3494 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3495 other productions, see the documentation accompanying the
3496 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3497 names are looked up in uninstantiated templates. If DECLARATOR_P
3498 is true, the unqualified-id is appearing as part of a declarator,
3499 rather than as part of an expression. */
3502 cp_parser_unqualified_id (cp_parser
* parser
,
3503 bool template_keyword_p
,
3504 bool check_dependency_p
,
3510 /* Peek at the next token. */
3511 token
= cp_lexer_peek_token (parser
->lexer
);
3513 switch (token
->type
)
3519 /* We don't know yet whether or not this will be a
3521 cp_parser_parse_tentatively (parser
);
3522 /* Try a template-id. */
3523 id
= cp_parser_template_id (parser
, template_keyword_p
,
3526 /* If it worked, we're done. */
3527 if (cp_parser_parse_definitely (parser
))
3529 /* Otherwise, it's an ordinary identifier. */
3530 return cp_parser_identifier (parser
);
3533 case CPP_TEMPLATE_ID
:
3534 return cp_parser_template_id (parser
, template_keyword_p
,
3541 tree qualifying_scope
;
3546 /* Consume the `~' token. */
3547 cp_lexer_consume_token (parser
->lexer
);
3548 /* Parse the class-name. The standard, as written, seems to
3551 template <typename T> struct S { ~S (); };
3552 template <typename T> S<T>::~S() {}
3554 is invalid, since `~' must be followed by a class-name, but
3555 `S<T>' is dependent, and so not known to be a class.
3556 That's not right; we need to look in uninstantiated
3557 templates. A further complication arises from:
3559 template <typename T> void f(T t) {
3563 Here, it is not possible to look up `T' in the scope of `T'
3564 itself. We must look in both the current scope, and the
3565 scope of the containing complete expression.
3567 Yet another issue is:
3576 The standard does not seem to say that the `S' in `~S'
3577 should refer to the type `S' and not the data member
3580 /* DR 244 says that we look up the name after the "~" in the
3581 same scope as we looked up the qualifying name. That idea
3582 isn't fully worked out; it's more complicated than that. */
3583 scope
= parser
->scope
;
3584 object_scope
= parser
->object_scope
;
3585 qualifying_scope
= parser
->qualifying_scope
;
3587 /* Check for invalid scopes. */
3588 if (scope
== error_mark_node
)
3590 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3591 cp_lexer_consume_token (parser
->lexer
);
3592 return error_mark_node
;
3594 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
3596 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3597 error ("scope %qT before %<~%> is not a class-name", scope
);
3598 cp_parser_simulate_error (parser
);
3599 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3600 cp_lexer_consume_token (parser
->lexer
);
3601 return error_mark_node
;
3603 gcc_assert (!scope
|| TYPE_P (scope
));
3605 /* If the name is of the form "X::~X" it's OK. */
3606 token
= cp_lexer_peek_token (parser
->lexer
);
3608 && token
->type
== CPP_NAME
3609 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3611 && constructor_name_p (token
->u
.value
, scope
))
3613 cp_lexer_consume_token (parser
->lexer
);
3614 return build_nt (BIT_NOT_EXPR
, scope
);
3617 /* If there was an explicit qualification (S::~T), first look
3618 in the scope given by the qualification (i.e., S). */
3620 type_decl
= NULL_TREE
;
3623 cp_parser_parse_tentatively (parser
);
3624 type_decl
= cp_parser_class_name (parser
,
3625 /*typename_keyword_p=*/false,
3626 /*template_keyword_p=*/false,
3628 /*check_dependency=*/false,
3629 /*class_head_p=*/false,
3631 if (cp_parser_parse_definitely (parser
))
3634 /* In "N::S::~S", look in "N" as well. */
3635 if (!done
&& scope
&& qualifying_scope
)
3637 cp_parser_parse_tentatively (parser
);
3638 parser
->scope
= qualifying_scope
;
3639 parser
->object_scope
= NULL_TREE
;
3640 parser
->qualifying_scope
= NULL_TREE
;
3642 = cp_parser_class_name (parser
,
3643 /*typename_keyword_p=*/false,
3644 /*template_keyword_p=*/false,
3646 /*check_dependency=*/false,
3647 /*class_head_p=*/false,
3649 if (cp_parser_parse_definitely (parser
))
3652 /* In "p->S::~T", look in the scope given by "*p" as well. */
3653 else if (!done
&& object_scope
)
3655 cp_parser_parse_tentatively (parser
);
3656 parser
->scope
= object_scope
;
3657 parser
->object_scope
= NULL_TREE
;
3658 parser
->qualifying_scope
= NULL_TREE
;
3660 = cp_parser_class_name (parser
,
3661 /*typename_keyword_p=*/false,
3662 /*template_keyword_p=*/false,
3664 /*check_dependency=*/false,
3665 /*class_head_p=*/false,
3667 if (cp_parser_parse_definitely (parser
))
3670 /* Look in the surrounding context. */
3673 parser
->scope
= NULL_TREE
;
3674 parser
->object_scope
= NULL_TREE
;
3675 parser
->qualifying_scope
= NULL_TREE
;
3677 = cp_parser_class_name (parser
,
3678 /*typename_keyword_p=*/false,
3679 /*template_keyword_p=*/false,
3681 /*check_dependency=*/false,
3682 /*class_head_p=*/false,
3685 /* If an error occurred, assume that the name of the
3686 destructor is the same as the name of the qualifying
3687 class. That allows us to keep parsing after running
3688 into ill-formed destructor names. */
3689 if (type_decl
== error_mark_node
&& scope
)
3690 return build_nt (BIT_NOT_EXPR
, scope
);
3691 else if (type_decl
== error_mark_node
)
3692 return error_mark_node
;
3694 /* Check that destructor name and scope match. */
3695 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
3697 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3698 error ("declaration of %<~%T%> as member of %qT",
3700 cp_parser_simulate_error (parser
);
3701 return error_mark_node
;
3706 A typedef-name that names a class shall not be used as the
3707 identifier in the declarator for a destructor declaration. */
3709 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3710 && !DECL_SELF_REFERENCE_P (type_decl
)
3711 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
3712 error ("typedef-name %qD used as destructor declarator",
3715 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3719 if (token
->keyword
== RID_OPERATOR
)
3723 /* This could be a template-id, so we try that first. */
3724 cp_parser_parse_tentatively (parser
);
3725 /* Try a template-id. */
3726 id
= cp_parser_template_id (parser
, template_keyword_p
,
3727 /*check_dependency_p=*/true,
3729 /* If that worked, we're done. */
3730 if (cp_parser_parse_definitely (parser
))
3732 /* We still don't know whether we're looking at an
3733 operator-function-id or a conversion-function-id. */
3734 cp_parser_parse_tentatively (parser
);
3735 /* Try an operator-function-id. */
3736 id
= cp_parser_operator_function_id (parser
);
3737 /* If that didn't work, try a conversion-function-id. */
3738 if (!cp_parser_parse_definitely (parser
))
3739 id
= cp_parser_conversion_function_id (parser
);
3748 cp_parser_error (parser
, "expected unqualified-id");
3749 return error_mark_node
;
3753 /* Parse an (optional) nested-name-specifier.
3755 nested-name-specifier:
3756 class-or-namespace-name :: nested-name-specifier [opt]
3757 class-or-namespace-name :: template nested-name-specifier [opt]
3759 PARSER->SCOPE should be set appropriately before this function is
3760 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3761 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3764 Sets PARSER->SCOPE to the class (TYPE) or namespace
3765 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3766 it unchanged if there is no nested-name-specifier. Returns the new
3767 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3769 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3770 part of a declaration and/or decl-specifier. */
3773 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3774 bool typename_keyword_p
,
3775 bool check_dependency_p
,
3777 bool is_declaration
)
3779 bool success
= false;
3780 cp_token_position start
= 0;
3783 /* Remember where the nested-name-specifier starts. */
3784 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3786 start
= cp_lexer_token_position (parser
->lexer
, false);
3787 push_deferring_access_checks (dk_deferred
);
3794 tree saved_qualifying_scope
;
3795 bool template_keyword_p
;
3797 /* Spot cases that cannot be the beginning of a
3798 nested-name-specifier. */
3799 token
= cp_lexer_peek_token (parser
->lexer
);
3801 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3802 the already parsed nested-name-specifier. */
3803 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3805 /* Grab the nested-name-specifier and continue the loop. */
3806 cp_parser_pre_parsed_nested_name_specifier (parser
);
3807 /* If we originally encountered this nested-name-specifier
3808 with IS_DECLARATION set to false, we will not have
3809 resolved TYPENAME_TYPEs, so we must do so here. */
3811 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3813 new_scope
= resolve_typename_type (parser
->scope
,
3814 /*only_current_p=*/false);
3815 if (new_scope
!= error_mark_node
)
3816 parser
->scope
= new_scope
;
3822 /* Spot cases that cannot be the beginning of a
3823 nested-name-specifier. On the second and subsequent times
3824 through the loop, we look for the `template' keyword. */
3825 if (success
&& token
->keyword
== RID_TEMPLATE
)
3827 /* A template-id can start a nested-name-specifier. */
3828 else if (token
->type
== CPP_TEMPLATE_ID
)
3832 /* If the next token is not an identifier, then it is
3833 definitely not a class-or-namespace-name. */
3834 if (token
->type
!= CPP_NAME
)
3836 /* If the following token is neither a `<' (to begin a
3837 template-id), nor a `::', then we are not looking at a
3838 nested-name-specifier. */
3839 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3840 if (token
->type
!= CPP_SCOPE
3841 && !cp_parser_nth_token_starts_template_argument_list_p
3846 /* The nested-name-specifier is optional, so we parse
3848 cp_parser_parse_tentatively (parser
);
3850 /* Look for the optional `template' keyword, if this isn't the
3851 first time through the loop. */
3853 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3855 template_keyword_p
= false;
3857 /* Save the old scope since the name lookup we are about to do
3858 might destroy it. */
3859 old_scope
= parser
->scope
;
3860 saved_qualifying_scope
= parser
->qualifying_scope
;
3861 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3862 look up names in "X<T>::I" in order to determine that "Y" is
3863 a template. So, if we have a typename at this point, we make
3864 an effort to look through it. */
3866 && !typename_keyword_p
3868 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3869 parser
->scope
= resolve_typename_type (parser
->scope
,
3870 /*only_current_p=*/false);
3871 /* Parse the qualifying entity. */
3873 = cp_parser_class_or_namespace_name (parser
,
3879 /* Look for the `::' token. */
3880 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3882 /* If we found what we wanted, we keep going; otherwise, we're
3884 if (!cp_parser_parse_definitely (parser
))
3886 bool error_p
= false;
3888 /* Restore the OLD_SCOPE since it was valid before the
3889 failed attempt at finding the last
3890 class-or-namespace-name. */
3891 parser
->scope
= old_scope
;
3892 parser
->qualifying_scope
= saved_qualifying_scope
;
3893 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3895 /* If the next token is an identifier, and the one after
3896 that is a `::', then any valid interpretation would have
3897 found a class-or-namespace-name. */
3898 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3899 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3901 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3904 token
= cp_lexer_consume_token (parser
->lexer
);
3907 if (!token
->ambiguous_p
)
3910 tree ambiguous_decls
;
3912 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
3914 /*is_template=*/false,
3915 /*is_namespace=*/false,
3916 /*check_dependency=*/true,
3918 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3919 error ("%qD used without template parameters", decl
);
3920 else if (ambiguous_decls
)
3922 error ("reference to %qD is ambiguous",
3924 print_candidates (ambiguous_decls
);
3925 decl
= error_mark_node
;
3928 cp_parser_name_lookup_error
3929 (parser
, token
->u
.value
, decl
,
3930 "is not a class or namespace");
3932 parser
->scope
= error_mark_node
;
3934 /* Treat this as a successful nested-name-specifier
3939 If the name found is not a class-name (clause
3940 _class_) or namespace-name (_namespace.def_), the
3941 program is ill-formed. */
3944 cp_lexer_consume_token (parser
->lexer
);
3948 /* We've found one valid nested-name-specifier. */
3950 /* Name lookup always gives us a DECL. */
3951 if (TREE_CODE (new_scope
) == TYPE_DECL
)
3952 new_scope
= TREE_TYPE (new_scope
);
3953 /* Uses of "template" must be followed by actual templates. */
3954 if (template_keyword_p
3955 && !(CLASS_TYPE_P (new_scope
)
3956 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
3957 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
3958 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
3959 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
3960 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
3961 == TEMPLATE_ID_EXPR
)))
3962 pedwarn (TYPE_P (new_scope
)
3963 ? "%qT is not a template"
3964 : "%qD is not a template",
3966 /* If it is a class scope, try to complete it; we are about to
3967 be looking up names inside the class. */
3968 if (TYPE_P (new_scope
)
3969 /* Since checking types for dependency can be expensive,
3970 avoid doing it if the type is already complete. */
3971 && !COMPLETE_TYPE_P (new_scope
)
3972 /* Do not try to complete dependent types. */
3973 && !dependent_type_p (new_scope
))
3974 new_scope
= complete_type (new_scope
);
3975 /* Make sure we look in the right scope the next time through
3977 parser
->scope
= new_scope
;
3980 /* If parsing tentatively, replace the sequence of tokens that makes
3981 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3982 token. That way, should we re-parse the token stream, we will
3983 not have to repeat the effort required to do the parse, nor will
3984 we issue duplicate error messages. */
3985 if (success
&& start
)
3989 token
= cp_lexer_token_at (parser
->lexer
, start
);
3990 /* Reset the contents of the START token. */
3991 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3992 /* Retrieve any deferred checks. Do not pop this access checks yet
3993 so the memory will not be reclaimed during token replacing below. */
3994 token
->u
.tree_check_value
= GGC_CNEW (struct tree_check
);
3995 token
->u
.tree_check_value
->value
= parser
->scope
;
3996 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
3997 token
->u
.tree_check_value
->qualifying_scope
=
3998 parser
->qualifying_scope
;
3999 token
->keyword
= RID_MAX
;
4001 /* Purge all subsequent tokens. */
4002 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4006 pop_to_parent_deferring_access_checks ();
4008 return success
? parser
->scope
: NULL_TREE
;
4011 /* Parse a nested-name-specifier. See
4012 cp_parser_nested_name_specifier_opt for details. This function
4013 behaves identically, except that it will an issue an error if no
4014 nested-name-specifier is present. */
4017 cp_parser_nested_name_specifier (cp_parser
*parser
,
4018 bool typename_keyword_p
,
4019 bool check_dependency_p
,
4021 bool is_declaration
)
4025 /* Look for the nested-name-specifier. */
4026 scope
= cp_parser_nested_name_specifier_opt (parser
,
4031 /* If it was not present, issue an error message. */
4034 cp_parser_error (parser
, "expected nested-name-specifier");
4035 parser
->scope
= NULL_TREE
;
4041 /* Parse a class-or-namespace-name.
4043 class-or-namespace-name:
4047 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4048 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4049 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4050 TYPE_P is TRUE iff the next name should be taken as a class-name,
4051 even the same name is declared to be another entity in the same
4054 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4055 specified by the class-or-namespace-name. If neither is found the
4056 ERROR_MARK_NODE is returned. */
4059 cp_parser_class_or_namespace_name (cp_parser
*parser
,
4060 bool typename_keyword_p
,
4061 bool template_keyword_p
,
4062 bool check_dependency_p
,
4064 bool is_declaration
)
4067 tree saved_qualifying_scope
;
4068 tree saved_object_scope
;
4072 /* Before we try to parse the class-name, we must save away the
4073 current PARSER->SCOPE since cp_parser_class_name will destroy
4075 saved_scope
= parser
->scope
;
4076 saved_qualifying_scope
= parser
->qualifying_scope
;
4077 saved_object_scope
= parser
->object_scope
;
4078 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4079 there is no need to look for a namespace-name. */
4080 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
4082 cp_parser_parse_tentatively (parser
);
4083 scope
= cp_parser_class_name (parser
,
4086 type_p
? class_type
: none_type
,
4088 /*class_head_p=*/false,
4090 /* If that didn't work, try for a namespace-name. */
4091 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
4093 /* Restore the saved scope. */
4094 parser
->scope
= saved_scope
;
4095 parser
->qualifying_scope
= saved_qualifying_scope
;
4096 parser
->object_scope
= saved_object_scope
;
4097 /* If we are not looking at an identifier followed by the scope
4098 resolution operator, then this is not part of a
4099 nested-name-specifier. (Note that this function is only used
4100 to parse the components of a nested-name-specifier.) */
4101 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
4102 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
4103 return error_mark_node
;
4104 scope
= cp_parser_namespace_name (parser
);
4110 /* Parse a postfix-expression.
4114 postfix-expression [ expression ]
4115 postfix-expression ( expression-list [opt] )
4116 simple-type-specifier ( expression-list [opt] )
4117 typename :: [opt] nested-name-specifier identifier
4118 ( expression-list [opt] )
4119 typename :: [opt] nested-name-specifier template [opt] template-id
4120 ( expression-list [opt] )
4121 postfix-expression . template [opt] id-expression
4122 postfix-expression -> template [opt] id-expression
4123 postfix-expression . pseudo-destructor-name
4124 postfix-expression -> pseudo-destructor-name
4125 postfix-expression ++
4126 postfix-expression --
4127 dynamic_cast < type-id > ( expression )
4128 static_cast < type-id > ( expression )
4129 reinterpret_cast < type-id > ( expression )
4130 const_cast < type-id > ( expression )
4131 typeid ( expression )
4137 ( type-id ) { initializer-list , [opt] }
4139 This extension is a GNU version of the C99 compound-literal
4140 construct. (The C99 grammar uses `type-name' instead of `type-id',
4141 but they are essentially the same concept.)
4143 If ADDRESS_P is true, the postfix expression is the operand of the
4144 `&' operator. CAST_P is true if this expression is the target of a
4147 Returns a representation of the expression. */
4150 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
4154 cp_id_kind idk
= CP_ID_KIND_NONE
;
4155 tree postfix_expression
= NULL_TREE
;
4157 /* Peek at the next token. */
4158 token
= cp_lexer_peek_token (parser
->lexer
);
4159 /* Some of the productions are determined by keywords. */
4160 keyword
= token
->keyword
;
4170 const char *saved_message
;
4172 /* All of these can be handled in the same way from the point
4173 of view of parsing. Begin by consuming the token
4174 identifying the cast. */
4175 cp_lexer_consume_token (parser
->lexer
);
4177 /* New types cannot be defined in the cast. */
4178 saved_message
= parser
->type_definition_forbidden_message
;
4179 parser
->type_definition_forbidden_message
4180 = "types may not be defined in casts";
4182 /* Look for the opening `<'. */
4183 cp_parser_require (parser
, CPP_LESS
, "`<'");
4184 /* Parse the type to which we are casting. */
4185 type
= cp_parser_type_id (parser
);
4186 /* Look for the closing `>'. */
4187 cp_parser_require (parser
, CPP_GREATER
, "`>'");
4188 /* Restore the old message. */
4189 parser
->type_definition_forbidden_message
= saved_message
;
4191 /* And the expression which is being cast. */
4192 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4193 expression
= cp_parser_expression (parser
, /*cast_p=*/true);
4194 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4196 /* Only type conversions to integral or enumeration types
4197 can be used in constant-expressions. */
4198 if (!cast_valid_in_integral_constant_expression_p (type
)
4199 && (cp_parser_non_integral_constant_expression
4201 "a cast to a type other than an integral or "
4202 "enumeration type")))
4203 return error_mark_node
;
4209 = build_dynamic_cast (type
, expression
);
4213 = build_static_cast (type
, expression
);
4217 = build_reinterpret_cast (type
, expression
);
4221 = build_const_cast (type
, expression
);
4232 const char *saved_message
;
4233 bool saved_in_type_id_in_expr_p
;
4235 /* Consume the `typeid' token. */
4236 cp_lexer_consume_token (parser
->lexer
);
4237 /* Look for the `(' token. */
4238 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4239 /* Types cannot be defined in a `typeid' expression. */
4240 saved_message
= parser
->type_definition_forbidden_message
;
4241 parser
->type_definition_forbidden_message
4242 = "types may not be defined in a `typeid\' expression";
4243 /* We can't be sure yet whether we're looking at a type-id or an
4245 cp_parser_parse_tentatively (parser
);
4246 /* Try a type-id first. */
4247 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4248 parser
->in_type_id_in_expr_p
= true;
4249 type
= cp_parser_type_id (parser
);
4250 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4251 /* Look for the `)' token. Otherwise, we can't be sure that
4252 we're not looking at an expression: consider `typeid (int
4253 (3))', for example. */
4254 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4255 /* If all went well, simply lookup the type-id. */
4256 if (cp_parser_parse_definitely (parser
))
4257 postfix_expression
= get_typeid (type
);
4258 /* Otherwise, fall back to the expression variant. */
4263 /* Look for an expression. */
4264 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
4265 /* Compute its typeid. */
4266 postfix_expression
= build_typeid (expression
);
4267 /* Look for the `)' token. */
4268 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4270 /* Restore the saved message. */
4271 parser
->type_definition_forbidden_message
= saved_message
;
4272 /* `typeid' may not appear in an integral constant expression. */
4273 if (cp_parser_non_integral_constant_expression(parser
,
4274 "`typeid' operator"))
4275 return error_mark_node
;
4282 /* The syntax permitted here is the same permitted for an
4283 elaborated-type-specifier. */
4284 type
= cp_parser_elaborated_type_specifier (parser
,
4285 /*is_friend=*/false,
4286 /*is_declaration=*/false);
4287 postfix_expression
= cp_parser_functional_cast (parser
, type
);
4295 /* If the next thing is a simple-type-specifier, we may be
4296 looking at a functional cast. We could also be looking at
4297 an id-expression. So, we try the functional cast, and if
4298 that doesn't work we fall back to the primary-expression. */
4299 cp_parser_parse_tentatively (parser
);
4300 /* Look for the simple-type-specifier. */
4301 type
= cp_parser_simple_type_specifier (parser
,
4302 /*decl_specs=*/NULL
,
4303 CP_PARSER_FLAGS_NONE
);
4304 /* Parse the cast itself. */
4305 if (!cp_parser_error_occurred (parser
))
4307 = cp_parser_functional_cast (parser
, type
);
4308 /* If that worked, we're done. */
4309 if (cp_parser_parse_definitely (parser
))
4312 /* If the functional-cast didn't work out, try a
4313 compound-literal. */
4314 if (cp_parser_allow_gnu_extensions_p (parser
)
4315 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4317 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
4318 bool saved_in_type_id_in_expr_p
;
4320 cp_parser_parse_tentatively (parser
);
4321 /* Consume the `('. */
4322 cp_lexer_consume_token (parser
->lexer
);
4323 /* Parse the type. */
4324 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4325 parser
->in_type_id_in_expr_p
= true;
4326 type
= cp_parser_type_id (parser
);
4327 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4328 /* Look for the `)'. */
4329 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4330 /* Look for the `{'. */
4331 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
4332 /* If things aren't going well, there's no need to
4334 if (!cp_parser_error_occurred (parser
))
4336 bool non_constant_p
;
4337 /* Parse the initializer-list. */
4339 = cp_parser_initializer_list (parser
, &non_constant_p
);
4340 /* Allow a trailing `,'. */
4341 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
4342 cp_lexer_consume_token (parser
->lexer
);
4343 /* Look for the final `}'. */
4344 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
4346 /* If that worked, we're definitely looking at a
4347 compound-literal expression. */
4348 if (cp_parser_parse_definitely (parser
))
4350 /* Warn the user that a compound literal is not
4351 allowed in standard C++. */
4353 pedwarn ("ISO C++ forbids compound-literals");
4354 /* For simplicity, we disallow compound literals in
4355 constant-expressions. We could
4356 allow compound literals of integer type, whose
4357 initializer was a constant, in constant
4358 expressions. Permitting that usage, as a further
4359 extension, would not change the meaning of any
4360 currently accepted programs. (Of course, as
4361 compound literals are not part of ISO C++, the
4362 standard has nothing to say.) */
4363 if (cp_parser_non_integral_constant_expression
4364 (parser
, "non-constant compound literals"))
4366 postfix_expression
= error_mark_node
;
4369 /* Form the representation of the compound-literal. */
4371 = finish_compound_literal (type
, initializer_list
);
4376 /* It must be a primary-expression. */
4378 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
4379 /*template_arg_p=*/false,
4385 /* Keep looping until the postfix-expression is complete. */
4388 if (idk
== CP_ID_KIND_UNQUALIFIED
4389 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4390 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4391 /* It is not a Koenig lookup function call. */
4393 = unqualified_name_lookup_error (postfix_expression
);
4395 /* Peek at the next token. */
4396 token
= cp_lexer_peek_token (parser
->lexer
);
4398 switch (token
->type
)
4400 case CPP_OPEN_SQUARE
:
4402 = cp_parser_postfix_open_square_expression (parser
,
4405 idk
= CP_ID_KIND_NONE
;
4408 case CPP_OPEN_PAREN
:
4409 /* postfix-expression ( expression-list [opt] ) */
4412 bool is_builtin_constant_p
;
4413 bool saved_integral_constant_expression_p
= false;
4414 bool saved_non_integral_constant_expression_p
= false;
4417 is_builtin_constant_p
4418 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
4419 if (is_builtin_constant_p
)
4421 /* The whole point of __builtin_constant_p is to allow
4422 non-constant expressions to appear as arguments. */
4423 saved_integral_constant_expression_p
4424 = parser
->integral_constant_expression_p
;
4425 saved_non_integral_constant_expression_p
4426 = parser
->non_integral_constant_expression_p
;
4427 parser
->integral_constant_expression_p
= false;
4429 args
= (cp_parser_parenthesized_expression_list
4430 (parser
, /*is_attribute_list=*/false,
4431 /*cast_p=*/false, /*allow_expansion_p=*/true,
4432 /*non_constant_p=*/NULL
));
4433 if (is_builtin_constant_p
)
4435 parser
->integral_constant_expression_p
4436 = saved_integral_constant_expression_p
;
4437 parser
->non_integral_constant_expression_p
4438 = saved_non_integral_constant_expression_p
;
4441 if (args
== error_mark_node
)
4443 postfix_expression
= error_mark_node
;
4447 /* Function calls are not permitted in
4448 constant-expressions. */
4449 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
4450 && cp_parser_non_integral_constant_expression (parser
,
4453 postfix_expression
= error_mark_node
;
4458 if (idk
== CP_ID_KIND_UNQUALIFIED
)
4460 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
4466 = perform_koenig_lookup (postfix_expression
, args
);
4470 = unqualified_fn_lookup_error (postfix_expression
);
4472 /* We do not perform argument-dependent lookup if
4473 normal lookup finds a non-function, in accordance
4474 with the expected resolution of DR 218. */
4475 else if (args
&& is_overloaded_fn (postfix_expression
))
4477 tree fn
= get_first_fn (postfix_expression
);
4479 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4480 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
4482 /* Only do argument dependent lookup if regular
4483 lookup does not find a set of member functions.
4484 [basic.lookup.koenig]/2a */
4485 if (!DECL_FUNCTION_MEMBER_P (fn
))
4489 = perform_koenig_lookup (postfix_expression
, args
);
4494 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4496 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4497 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4499 if (processing_template_decl
4500 && (type_dependent_expression_p (instance
)
4501 || (!BASELINK_P (fn
)
4502 && TREE_CODE (fn
) != FIELD_DECL
)
4503 || type_dependent_expression_p (fn
)
4504 || any_type_dependent_arguments_p (args
)))
4507 = build_nt_call_list (postfix_expression
, args
);
4511 if (BASELINK_P (fn
))
4513 = (build_new_method_call
4514 (instance
, fn
, args
, NULL_TREE
,
4515 (idk
== CP_ID_KIND_QUALIFIED
4516 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
),
4520 = finish_call_expr (postfix_expression
, args
,
4521 /*disallow_virtual=*/false,
4522 /*koenig_p=*/false);
4524 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4525 || TREE_CODE (postfix_expression
) == MEMBER_REF
4526 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4527 postfix_expression
= (build_offset_ref_call_from_tree
4528 (postfix_expression
, args
));
4529 else if (idk
== CP_ID_KIND_QUALIFIED
)
4530 /* A call to a static class member, or a namespace-scope
4533 = finish_call_expr (postfix_expression
, args
,
4534 /*disallow_virtual=*/true,
4537 /* All other function calls. */
4539 = finish_call_expr (postfix_expression
, args
,
4540 /*disallow_virtual=*/false,
4543 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4544 idk
= CP_ID_KIND_NONE
;
4550 /* postfix-expression . template [opt] id-expression
4551 postfix-expression . pseudo-destructor-name
4552 postfix-expression -> template [opt] id-expression
4553 postfix-expression -> pseudo-destructor-name */
4555 /* Consume the `.' or `->' operator. */
4556 cp_lexer_consume_token (parser
->lexer
);
4559 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4565 /* postfix-expression ++ */
4566 /* Consume the `++' token. */
4567 cp_lexer_consume_token (parser
->lexer
);
4568 /* Generate a representation for the complete expression. */
4570 = finish_increment_expr (postfix_expression
,
4571 POSTINCREMENT_EXPR
);
4572 /* Increments may not appear in constant-expressions. */
4573 if (cp_parser_non_integral_constant_expression (parser
,
4575 postfix_expression
= error_mark_node
;
4576 idk
= CP_ID_KIND_NONE
;
4579 case CPP_MINUS_MINUS
:
4580 /* postfix-expression -- */
4581 /* Consume the `--' token. */
4582 cp_lexer_consume_token (parser
->lexer
);
4583 /* Generate a representation for the complete expression. */
4585 = finish_increment_expr (postfix_expression
,
4586 POSTDECREMENT_EXPR
);
4587 /* Decrements may not appear in constant-expressions. */
4588 if (cp_parser_non_integral_constant_expression (parser
,
4590 postfix_expression
= error_mark_node
;
4591 idk
= CP_ID_KIND_NONE
;
4595 return postfix_expression
;
4599 /* We should never get here. */
4601 return error_mark_node
;
4604 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4605 by cp_parser_builtin_offsetof. We're looking for
4607 postfix-expression [ expression ]
4609 FOR_OFFSETOF is set if we're being called in that context, which
4610 changes how we deal with integer constant expressions. */
4613 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
4614 tree postfix_expression
,
4619 /* Consume the `[' token. */
4620 cp_lexer_consume_token (parser
->lexer
);
4622 /* Parse the index expression. */
4623 /* ??? For offsetof, there is a question of what to allow here. If
4624 offsetof is not being used in an integral constant expression context,
4625 then we *could* get the right answer by computing the value at runtime.
4626 If we are in an integral constant expression context, then we might
4627 could accept any constant expression; hard to say without analysis.
4628 Rather than open the barn door too wide right away, allow only integer
4629 constant expressions here. */
4631 index
= cp_parser_constant_expression (parser
, false, NULL
);
4633 index
= cp_parser_expression (parser
, /*cast_p=*/false);
4635 /* Look for the closing `]'. */
4636 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4638 /* Build the ARRAY_REF. */
4639 postfix_expression
= grok_array_decl (postfix_expression
, index
);
4641 /* When not doing offsetof, array references are not permitted in
4642 constant-expressions. */
4644 && (cp_parser_non_integral_constant_expression
4645 (parser
, "an array reference")))
4646 postfix_expression
= error_mark_node
;
4648 return postfix_expression
;
4651 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4652 by cp_parser_builtin_offsetof. We're looking for
4654 postfix-expression . template [opt] id-expression
4655 postfix-expression . pseudo-destructor-name
4656 postfix-expression -> template [opt] id-expression
4657 postfix-expression -> pseudo-destructor-name
4659 FOR_OFFSETOF is set if we're being called in that context. That sorta
4660 limits what of the above we'll actually accept, but nevermind.
4661 TOKEN_TYPE is the "." or "->" token, which will already have been
4662 removed from the stream. */
4665 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
4666 enum cpp_ttype token_type
,
4667 tree postfix_expression
,
4668 bool for_offsetof
, cp_id_kind
*idk
)
4672 bool pseudo_destructor_p
;
4673 tree scope
= NULL_TREE
;
4675 /* If this is a `->' operator, dereference the pointer. */
4676 if (token_type
== CPP_DEREF
)
4677 postfix_expression
= build_x_arrow (postfix_expression
);
4678 /* Check to see whether or not the expression is type-dependent. */
4679 dependent_p
= type_dependent_expression_p (postfix_expression
);
4680 /* The identifier following the `->' or `.' is not qualified. */
4681 parser
->scope
= NULL_TREE
;
4682 parser
->qualifying_scope
= NULL_TREE
;
4683 parser
->object_scope
= NULL_TREE
;
4684 *idk
= CP_ID_KIND_NONE
;
4685 /* Enter the scope corresponding to the type of the object
4686 given by the POSTFIX_EXPRESSION. */
4687 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
4689 scope
= TREE_TYPE (postfix_expression
);
4690 /* According to the standard, no expression should ever have
4691 reference type. Unfortunately, we do not currently match
4692 the standard in this respect in that our internal representation
4693 of an expression may have reference type even when the standard
4694 says it does not. Therefore, we have to manually obtain the
4695 underlying type here. */
4696 scope
= non_reference (scope
);
4697 /* The type of the POSTFIX_EXPRESSION must be complete. */
4698 if (scope
== unknown_type_node
)
4700 error ("%qE does not have class type", postfix_expression
);
4704 scope
= complete_type_or_else (scope
, NULL_TREE
);
4705 /* Let the name lookup machinery know that we are processing a
4706 class member access expression. */
4707 parser
->context
->object_type
= scope
;
4708 /* If something went wrong, we want to be able to discern that case,
4709 as opposed to the case where there was no SCOPE due to the type
4710 of expression being dependent. */
4712 scope
= error_mark_node
;
4713 /* If the SCOPE was erroneous, make the various semantic analysis
4714 functions exit quickly -- and without issuing additional error
4716 if (scope
== error_mark_node
)
4717 postfix_expression
= error_mark_node
;
4720 /* Assume this expression is not a pseudo-destructor access. */
4721 pseudo_destructor_p
= false;
4723 /* If the SCOPE is a scalar type, then, if this is a valid program,
4724 we must be looking at a pseudo-destructor-name. */
4725 if (scope
&& SCALAR_TYPE_P (scope
))
4730 cp_parser_parse_tentatively (parser
);
4731 /* Parse the pseudo-destructor-name. */
4733 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4734 if (cp_parser_parse_definitely (parser
))
4736 pseudo_destructor_p
= true;
4738 = finish_pseudo_destructor_expr (postfix_expression
,
4739 s
, TREE_TYPE (type
));
4743 if (!pseudo_destructor_p
)
4745 /* If the SCOPE is not a scalar type, we are looking at an
4746 ordinary class member access expression, rather than a
4747 pseudo-destructor-name. */
4749 /* Parse the id-expression. */
4750 name
= (cp_parser_id_expression
4752 cp_parser_optional_template_keyword (parser
),
4753 /*check_dependency_p=*/true,
4755 /*declarator_p=*/false,
4756 /*optional_p=*/false));
4757 /* In general, build a SCOPE_REF if the member name is qualified.
4758 However, if the name was not dependent and has already been
4759 resolved; there is no need to build the SCOPE_REF. For example;
4761 struct X { void f(); };
4762 template <typename T> void f(T* t) { t->X::f(); }
4764 Even though "t" is dependent, "X::f" is not and has been resolved
4765 to a BASELINK; there is no need to include scope information. */
4767 /* But we do need to remember that there was an explicit scope for
4768 virtual function calls. */
4770 *idk
= CP_ID_KIND_QUALIFIED
;
4772 /* If the name is a template-id that names a type, we will get a
4773 TYPE_DECL here. That is invalid code. */
4774 if (TREE_CODE (name
) == TYPE_DECL
)
4776 error ("invalid use of %qD", name
);
4777 postfix_expression
= error_mark_node
;
4781 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
4783 name
= build_qualified_name (/*type=*/NULL_TREE
,
4787 parser
->scope
= NULL_TREE
;
4788 parser
->qualifying_scope
= NULL_TREE
;
4789 parser
->object_scope
= NULL_TREE
;
4791 if (scope
&& name
&& BASELINK_P (name
))
4792 adjust_result_of_qualified_name_lookup
4793 (name
, BINFO_TYPE (BASELINK_ACCESS_BINFO (name
)), scope
);
4795 = finish_class_member_access_expr (postfix_expression
, name
,
4800 /* We no longer need to look up names in the scope of the object on
4801 the left-hand side of the `.' or `->' operator. */
4802 parser
->context
->object_type
= NULL_TREE
;
4804 /* Outside of offsetof, these operators may not appear in
4805 constant-expressions. */
4807 && (cp_parser_non_integral_constant_expression
4808 (parser
, token_type
== CPP_DEREF
? "'->'" : "`.'")))
4809 postfix_expression
= error_mark_node
;
4811 return postfix_expression
;
4814 /* Parse a parenthesized expression-list.
4817 assignment-expression
4818 expression-list, assignment-expression
4823 identifier, expression-list
4825 CAST_P is true if this expression is the target of a cast.
4827 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4830 Returns a TREE_LIST. The TREE_VALUE of each node is a
4831 representation of an assignment-expression. Note that a TREE_LIST
4832 is returned even if there is only a single expression in the list.
4833 error_mark_node is returned if the ( and or ) are
4834 missing. NULL_TREE is returned on no expressions. The parentheses
4835 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4836 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4837 indicates whether or not all of the expressions in the list were
4841 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4842 bool is_attribute_list
,
4844 bool allow_expansion_p
,
4845 bool *non_constant_p
)
4847 tree expression_list
= NULL_TREE
;
4848 bool fold_expr_p
= is_attribute_list
;
4849 tree identifier
= NULL_TREE
;
4851 /* Assume all the expressions will be constant. */
4853 *non_constant_p
= false;
4855 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4856 return error_mark_node
;
4858 /* Consume expressions until there are no more. */
4859 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4864 /* At the beginning of attribute lists, check to see if the
4865 next token is an identifier. */
4866 if (is_attribute_list
4867 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4871 /* Consume the identifier. */
4872 token
= cp_lexer_consume_token (parser
->lexer
);
4873 /* Save the identifier. */
4874 identifier
= token
->u
.value
;
4878 /* Parse the next assignment-expression. */
4881 bool expr_non_constant_p
;
4882 expr
= (cp_parser_constant_expression
4883 (parser
, /*allow_non_constant_p=*/true,
4884 &expr_non_constant_p
));
4885 if (expr_non_constant_p
)
4886 *non_constant_p
= true;
4889 expr
= cp_parser_assignment_expression (parser
, cast_p
);
4892 expr
= fold_non_dependent_expr (expr
);
4894 /* If we have an ellipsis, then this is an expression
4896 if (allow_expansion_p
4897 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4899 /* Consume the `...'. */
4900 cp_lexer_consume_token (parser
->lexer
);
4902 /* Build the argument pack. */
4903 expr
= make_pack_expansion (expr
);
4906 /* Add it to the list. We add error_mark_node
4907 expressions to the list, so that we can still tell if
4908 the correct form for a parenthesized expression-list
4909 is found. That gives better errors. */
4910 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4912 if (expr
== error_mark_node
)
4916 /* After the first item, attribute lists look the same as
4917 expression lists. */
4918 is_attribute_list
= false;
4921 /* If the next token isn't a `,', then we are done. */
4922 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4925 /* Otherwise, consume the `,' and keep going. */
4926 cp_lexer_consume_token (parser
->lexer
);
4929 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4934 /* We try and resync to an unnested comma, as that will give the
4935 user better diagnostics. */
4936 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4937 /*recovering=*/true,
4939 /*consume_paren=*/true);
4943 return error_mark_node
;
4946 /* We built up the list in reverse order so we must reverse it now. */
4947 expression_list
= nreverse (expression_list
);
4949 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4951 return expression_list
;
4954 /* Parse a pseudo-destructor-name.
4956 pseudo-destructor-name:
4957 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4958 :: [opt] nested-name-specifier template template-id :: ~ type-name
4959 :: [opt] nested-name-specifier [opt] ~ type-name
4961 If either of the first two productions is used, sets *SCOPE to the
4962 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4963 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4964 or ERROR_MARK_NODE if the parse fails. */
4967 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4971 bool nested_name_specifier_p
;
4973 /* Assume that things will not work out. */
4974 *type
= error_mark_node
;
4976 /* Look for the optional `::' operator. */
4977 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4978 /* Look for the optional nested-name-specifier. */
4979 nested_name_specifier_p
4980 = (cp_parser_nested_name_specifier_opt (parser
,
4981 /*typename_keyword_p=*/false,
4982 /*check_dependency_p=*/true,
4984 /*is_declaration=*/true)
4986 /* Now, if we saw a nested-name-specifier, we might be doing the
4987 second production. */
4988 if (nested_name_specifier_p
4989 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4991 /* Consume the `template' keyword. */
4992 cp_lexer_consume_token (parser
->lexer
);
4993 /* Parse the template-id. */
4994 cp_parser_template_id (parser
,
4995 /*template_keyword_p=*/true,
4996 /*check_dependency_p=*/false,
4997 /*is_declaration=*/true);
4998 /* Look for the `::' token. */
4999 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
5001 /* If the next token is not a `~', then there might be some
5002 additional qualification. */
5003 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
5005 /* Look for the type-name. */
5006 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
5008 if (*scope
== error_mark_node
)
5011 /* If we don't have ::~, then something has gone wrong. Since
5012 the only caller of this function is looking for something
5013 after `.' or `->' after a scalar type, most likely the
5014 program is trying to get a member of a non-aggregate
5016 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
)
5017 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_COMPL
)
5019 cp_parser_error (parser
, "request for member of non-aggregate type");
5023 /* Look for the `::' token. */
5024 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
5029 /* Look for the `~'. */
5030 cp_parser_require (parser
, CPP_COMPL
, "`~'");
5031 /* Look for the type-name again. We are not responsible for
5032 checking that it matches the first type-name. */
5033 *type
= cp_parser_type_name (parser
);
5036 /* Parse a unary-expression.
5042 unary-operator cast-expression
5043 sizeof unary-expression
5051 __extension__ cast-expression
5052 __alignof__ unary-expression
5053 __alignof__ ( type-id )
5054 __real__ cast-expression
5055 __imag__ cast-expression
5058 ADDRESS_P is true iff the unary-expression is appearing as the
5059 operand of the `&' operator. CAST_P is true if this expression is
5060 the target of a cast.
5062 Returns a representation of the expression. */
5065 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5068 enum tree_code unary_operator
;
5070 /* Peek at the next token. */
5071 token
= cp_lexer_peek_token (parser
->lexer
);
5072 /* Some keywords give away the kind of expression. */
5073 if (token
->type
== CPP_KEYWORD
)
5075 enum rid keyword
= token
->keyword
;
5085 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
5086 /* Consume the token. */
5087 cp_lexer_consume_token (parser
->lexer
);
5088 /* Parse the operand. */
5089 operand
= cp_parser_sizeof_operand (parser
, keyword
);
5091 if (TYPE_P (operand
))
5092 return cxx_sizeof_or_alignof_type (operand
, op
, true);
5094 return cxx_sizeof_or_alignof_expr (operand
, op
);
5098 return cp_parser_new_expression (parser
);
5101 return cp_parser_delete_expression (parser
);
5105 /* The saved value of the PEDANTIC flag. */
5109 /* Save away the PEDANTIC flag. */
5110 cp_parser_extension_opt (parser
, &saved_pedantic
);
5111 /* Parse the cast-expression. */
5112 expr
= cp_parser_simple_cast_expression (parser
);
5113 /* Restore the PEDANTIC flag. */
5114 pedantic
= saved_pedantic
;
5124 /* Consume the `__real__' or `__imag__' token. */
5125 cp_lexer_consume_token (parser
->lexer
);
5126 /* Parse the cast-expression. */
5127 expression
= cp_parser_simple_cast_expression (parser
);
5128 /* Create the complete representation. */
5129 return build_x_unary_op ((keyword
== RID_REALPART
5130 ? REALPART_EXPR
: IMAGPART_EXPR
),
5140 /* Look for the `:: new' and `:: delete', which also signal the
5141 beginning of a new-expression, or delete-expression,
5142 respectively. If the next token is `::', then it might be one of
5144 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
5148 /* See if the token after the `::' is one of the keywords in
5149 which we're interested. */
5150 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
5151 /* If it's `new', we have a new-expression. */
5152 if (keyword
== RID_NEW
)
5153 return cp_parser_new_expression (parser
);
5154 /* Similarly, for `delete'. */
5155 else if (keyword
== RID_DELETE
)
5156 return cp_parser_delete_expression (parser
);
5159 /* Look for a unary operator. */
5160 unary_operator
= cp_parser_unary_operator (token
);
5161 /* The `++' and `--' operators can be handled similarly, even though
5162 they are not technically unary-operators in the grammar. */
5163 if (unary_operator
== ERROR_MARK
)
5165 if (token
->type
== CPP_PLUS_PLUS
)
5166 unary_operator
= PREINCREMENT_EXPR
;
5167 else if (token
->type
== CPP_MINUS_MINUS
)
5168 unary_operator
= PREDECREMENT_EXPR
;
5169 /* Handle the GNU address-of-label extension. */
5170 else if (cp_parser_allow_gnu_extensions_p (parser
)
5171 && token
->type
== CPP_AND_AND
)
5175 /* Consume the '&&' token. */
5176 cp_lexer_consume_token (parser
->lexer
);
5177 /* Look for the identifier. */
5178 identifier
= cp_parser_identifier (parser
);
5179 /* Create an expression representing the address. */
5180 return finish_label_address_expr (identifier
);
5183 if (unary_operator
!= ERROR_MARK
)
5185 tree cast_expression
;
5186 tree expression
= error_mark_node
;
5187 const char *non_constant_p
= NULL
;
5189 /* Consume the operator token. */
5190 token
= cp_lexer_consume_token (parser
->lexer
);
5191 /* Parse the cast-expression. */
5193 = cp_parser_cast_expression (parser
,
5194 unary_operator
== ADDR_EXPR
,
5196 /* Now, build an appropriate representation. */
5197 switch (unary_operator
)
5200 non_constant_p
= "`*'";
5201 expression
= build_x_indirect_ref (cast_expression
, "unary *");
5205 non_constant_p
= "`&'";
5208 expression
= build_x_unary_op (unary_operator
, cast_expression
);
5211 case PREINCREMENT_EXPR
:
5212 case PREDECREMENT_EXPR
:
5213 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
5216 case UNARY_PLUS_EXPR
:
5218 case TRUTH_NOT_EXPR
:
5219 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
5227 && cp_parser_non_integral_constant_expression (parser
,
5229 expression
= error_mark_node
;
5234 return cp_parser_postfix_expression (parser
, address_p
, cast_p
);
5237 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5238 unary-operator, the corresponding tree code is returned. */
5240 static enum tree_code
5241 cp_parser_unary_operator (cp_token
* token
)
5243 switch (token
->type
)
5246 return INDIRECT_REF
;
5252 return UNARY_PLUS_EXPR
;
5258 return TRUTH_NOT_EXPR
;
5261 return BIT_NOT_EXPR
;
5268 /* Parse a new-expression.
5271 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5272 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5274 Returns a representation of the expression. */
5277 cp_parser_new_expression (cp_parser
* parser
)
5279 bool global_scope_p
;
5285 /* Look for the optional `::' operator. */
5287 = (cp_parser_global_scope_opt (parser
,
5288 /*current_scope_valid_p=*/false)
5290 /* Look for the `new' operator. */
5291 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
5292 /* There's no easy way to tell a new-placement from the
5293 `( type-id )' construct. */
5294 cp_parser_parse_tentatively (parser
);
5295 /* Look for a new-placement. */
5296 placement
= cp_parser_new_placement (parser
);
5297 /* If that didn't work out, there's no new-placement. */
5298 if (!cp_parser_parse_definitely (parser
))
5299 placement
= NULL_TREE
;
5301 /* If the next token is a `(', then we have a parenthesized
5303 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5305 /* Consume the `('. */
5306 cp_lexer_consume_token (parser
->lexer
);
5307 /* Parse the type-id. */
5308 type
= cp_parser_type_id (parser
);
5309 /* Look for the closing `)'. */
5310 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5311 /* There should not be a direct-new-declarator in this production,
5312 but GCC used to allowed this, so we check and emit a sensible error
5313 message for this case. */
5314 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5316 error ("array bound forbidden after parenthesized type-id");
5317 inform ("try removing the parentheses around the type-id");
5318 cp_parser_direct_new_declarator (parser
);
5322 /* Otherwise, there must be a new-type-id. */
5324 type
= cp_parser_new_type_id (parser
, &nelts
);
5326 /* If the next token is a `(', then we have a new-initializer. */
5327 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5328 initializer
= cp_parser_new_initializer (parser
);
5330 initializer
= NULL_TREE
;
5332 /* A new-expression may not appear in an integral constant
5334 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
5335 return error_mark_node
;
5337 /* Create a representation of the new-expression. */
5338 return build_new (placement
, type
, nelts
, initializer
, global_scope_p
);
5341 /* Parse a new-placement.
5346 Returns the same representation as for an expression-list. */
5349 cp_parser_new_placement (cp_parser
* parser
)
5351 tree expression_list
;
5353 /* Parse the expression-list. */
5354 expression_list
= (cp_parser_parenthesized_expression_list
5355 (parser
, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5356 /*non_constant_p=*/NULL
));
5358 return expression_list
;
5361 /* Parse a new-type-id.
5364 type-specifier-seq new-declarator [opt]
5366 Returns the TYPE allocated. If the new-type-id indicates an array
5367 type, *NELTS is set to the number of elements in the last array
5368 bound; the TYPE will not include the last array bound. */
5371 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
5373 cp_decl_specifier_seq type_specifier_seq
;
5374 cp_declarator
*new_declarator
;
5375 cp_declarator
*declarator
;
5376 cp_declarator
*outer_declarator
;
5377 const char *saved_message
;
5380 /* The type-specifier sequence must not contain type definitions.
5381 (It cannot contain declarations of new types either, but if they
5382 are not definitions we will catch that because they are not
5384 saved_message
= parser
->type_definition_forbidden_message
;
5385 parser
->type_definition_forbidden_message
5386 = "types may not be defined in a new-type-id";
5387 /* Parse the type-specifier-seq. */
5388 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
5389 &type_specifier_seq
);
5390 /* Restore the old message. */
5391 parser
->type_definition_forbidden_message
= saved_message
;
5392 /* Parse the new-declarator. */
5393 new_declarator
= cp_parser_new_declarator_opt (parser
);
5395 /* Determine the number of elements in the last array dimension, if
5398 /* Skip down to the last array dimension. */
5399 declarator
= new_declarator
;
5400 outer_declarator
= NULL
;
5401 while (declarator
&& (declarator
->kind
== cdk_pointer
5402 || declarator
->kind
== cdk_ptrmem
))
5404 outer_declarator
= declarator
;
5405 declarator
= declarator
->declarator
;
5408 && declarator
->kind
== cdk_array
5409 && declarator
->declarator
5410 && declarator
->declarator
->kind
== cdk_array
)
5412 outer_declarator
= declarator
;
5413 declarator
= declarator
->declarator
;
5416 if (declarator
&& declarator
->kind
== cdk_array
)
5418 *nelts
= declarator
->u
.array
.bounds
;
5419 if (*nelts
== error_mark_node
)
5420 *nelts
= integer_one_node
;
5422 if (outer_declarator
)
5423 outer_declarator
->declarator
= declarator
->declarator
;
5425 new_declarator
= NULL
;
5428 type
= groktypename (&type_specifier_seq
, new_declarator
);
5429 if (TREE_CODE (type
) == ARRAY_TYPE
&& *nelts
== NULL_TREE
)
5431 *nelts
= array_type_nelts_top (type
);
5432 type
= TREE_TYPE (type
);
5437 /* Parse an (optional) new-declarator.
5440 ptr-operator new-declarator [opt]
5441 direct-new-declarator
5443 Returns the declarator. */
5445 static cp_declarator
*
5446 cp_parser_new_declarator_opt (cp_parser
* parser
)
5448 enum tree_code code
;
5450 cp_cv_quals cv_quals
;
5452 /* We don't know if there's a ptr-operator next, or not. */
5453 cp_parser_parse_tentatively (parser
);
5454 /* Look for a ptr-operator. */
5455 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
5456 /* If that worked, look for more new-declarators. */
5457 if (cp_parser_parse_definitely (parser
))
5459 cp_declarator
*declarator
;
5461 /* Parse another optional declarator. */
5462 declarator
= cp_parser_new_declarator_opt (parser
);
5464 /* Create the representation of the declarator. */
5466 declarator
= make_ptrmem_declarator (cv_quals
, type
, declarator
);
5467 else if (code
== INDIRECT_REF
)
5468 declarator
= make_pointer_declarator (cv_quals
, declarator
);
5470 declarator
= make_reference_declarator (cv_quals
, declarator
);
5475 /* If the next token is a `[', there is a direct-new-declarator. */
5476 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5477 return cp_parser_direct_new_declarator (parser
);
5482 /* Parse a direct-new-declarator.
5484 direct-new-declarator:
5486 direct-new-declarator [constant-expression]
5490 static cp_declarator
*
5491 cp_parser_direct_new_declarator (cp_parser
* parser
)
5493 cp_declarator
*declarator
= NULL
;
5499 /* Look for the opening `['. */
5500 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
5501 /* The first expression is not required to be constant. */
5504 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
5505 /* The standard requires that the expression have integral
5506 type. DR 74 adds enumeration types. We believe that the
5507 real intent is that these expressions be handled like the
5508 expression in a `switch' condition, which also allows
5509 classes with a single conversion to integral or
5510 enumeration type. */
5511 if (!processing_template_decl
)
5514 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5519 error ("expression in new-declarator must have integral "
5520 "or enumeration type");
5521 expression
= error_mark_node
;
5525 /* But all the other expressions must be. */
5528 = cp_parser_constant_expression (parser
,
5529 /*allow_non_constant=*/false,
5531 /* Look for the closing `]'. */
5532 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5534 /* Add this bound to the declarator. */
5535 declarator
= make_array_declarator (declarator
, expression
);
5537 /* If the next token is not a `[', then there are no more
5539 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
5546 /* Parse a new-initializer.
5549 ( expression-list [opt] )
5551 Returns a representation of the expression-list. If there is no
5552 expression-list, VOID_ZERO_NODE is returned. */
5555 cp_parser_new_initializer (cp_parser
* parser
)
5557 tree expression_list
;
5559 expression_list
= (cp_parser_parenthesized_expression_list
5560 (parser
, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5561 /*non_constant_p=*/NULL
));
5562 if (!expression_list
)
5563 expression_list
= void_zero_node
;
5565 return expression_list
;
5568 /* Parse a delete-expression.
5571 :: [opt] delete cast-expression
5572 :: [opt] delete [ ] cast-expression
5574 Returns a representation of the expression. */
5577 cp_parser_delete_expression (cp_parser
* parser
)
5579 bool global_scope_p
;
5583 /* Look for the optional `::' operator. */
5585 = (cp_parser_global_scope_opt (parser
,
5586 /*current_scope_valid_p=*/false)
5588 /* Look for the `delete' keyword. */
5589 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
5590 /* See if the array syntax is in use. */
5591 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5593 /* Consume the `[' token. */
5594 cp_lexer_consume_token (parser
->lexer
);
5595 /* Look for the `]' token. */
5596 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5597 /* Remember that this is the `[]' construct. */
5603 /* Parse the cast-expression. */
5604 expression
= cp_parser_simple_cast_expression (parser
);
5606 /* A delete-expression may not appear in an integral constant
5608 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
5609 return error_mark_node
;
5611 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
5614 /* Parse a cast-expression.
5618 ( type-id ) cast-expression
5620 ADDRESS_P is true iff the unary-expression is appearing as the
5621 operand of the `&' operator. CAST_P is true if this expression is
5622 the target of a cast.
5624 Returns a representation of the expression. */
5627 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5629 /* If it's a `(', then we might be looking at a cast. */
5630 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5632 tree type
= NULL_TREE
;
5633 tree expr
= NULL_TREE
;
5634 bool compound_literal_p
;
5635 const char *saved_message
;
5637 /* There's no way to know yet whether or not this is a cast.
5638 For example, `(int (3))' is a unary-expression, while `(int)
5639 3' is a cast. So, we resort to parsing tentatively. */
5640 cp_parser_parse_tentatively (parser
);
5641 /* Types may not be defined in a cast. */
5642 saved_message
= parser
->type_definition_forbidden_message
;
5643 parser
->type_definition_forbidden_message
5644 = "types may not be defined in casts";
5645 /* Consume the `('. */
5646 cp_lexer_consume_token (parser
->lexer
);
5647 /* A very tricky bit is that `(struct S) { 3 }' is a
5648 compound-literal (which we permit in C++ as an extension).
5649 But, that construct is not a cast-expression -- it is a
5650 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5651 is legal; if the compound-literal were a cast-expression,
5652 you'd need an extra set of parentheses.) But, if we parse
5653 the type-id, and it happens to be a class-specifier, then we
5654 will commit to the parse at that point, because we cannot
5655 undo the action that is done when creating a new class. So,
5656 then we cannot back up and do a postfix-expression.
5658 Therefore, we scan ahead to the closing `)', and check to see
5659 if the token after the `)' is a `{'. If so, we are not
5660 looking at a cast-expression.
5662 Save tokens so that we can put them back. */
5663 cp_lexer_save_tokens (parser
->lexer
);
5664 /* Skip tokens until the next token is a closing parenthesis.
5665 If we find the closing `)', and the next token is a `{', then
5666 we are looking at a compound-literal. */
5668 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5669 /*consume_paren=*/true)
5670 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5671 /* Roll back the tokens we skipped. */
5672 cp_lexer_rollback_tokens (parser
->lexer
);
5673 /* If we were looking at a compound-literal, simulate an error
5674 so that the call to cp_parser_parse_definitely below will
5676 if (compound_literal_p
)
5677 cp_parser_simulate_error (parser
);
5680 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5681 parser
->in_type_id_in_expr_p
= true;
5682 /* Look for the type-id. */
5683 type
= cp_parser_type_id (parser
);
5684 /* Look for the closing `)'. */
5685 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5686 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5689 /* Restore the saved message. */
5690 parser
->type_definition_forbidden_message
= saved_message
;
5692 /* If ok so far, parse the dependent expression. We cannot be
5693 sure it is a cast. Consider `(T ())'. It is a parenthesized
5694 ctor of T, but looks like a cast to function returning T
5695 without a dependent expression. */
5696 if (!cp_parser_error_occurred (parser
))
5697 expr
= cp_parser_cast_expression (parser
,
5698 /*address_p=*/false,
5701 if (cp_parser_parse_definitely (parser
))
5703 /* Warn about old-style casts, if so requested. */
5704 if (warn_old_style_cast
5705 && !in_system_header
5706 && !VOID_TYPE_P (type
)
5707 && current_lang_name
!= lang_name_c
)
5708 warning (OPT_Wold_style_cast
, "use of old-style cast");
5710 /* Only type conversions to integral or enumeration types
5711 can be used in constant-expressions. */
5712 if (!cast_valid_in_integral_constant_expression_p (type
)
5713 && (cp_parser_non_integral_constant_expression
5715 "a cast to a type other than an integral or "
5716 "enumeration type")))
5717 return error_mark_node
;
5719 /* Perform the cast. */
5720 expr
= build_c_cast (type
, expr
);
5725 /* If we get here, then it's not a cast, so it must be a
5726 unary-expression. */
5727 return cp_parser_unary_expression (parser
, address_p
, cast_p
);
5730 /* Parse a binary expression of the general form:
5734 pm-expression .* cast-expression
5735 pm-expression ->* cast-expression
5737 multiplicative-expression:
5739 multiplicative-expression * pm-expression
5740 multiplicative-expression / pm-expression
5741 multiplicative-expression % pm-expression
5743 additive-expression:
5744 multiplicative-expression
5745 additive-expression + multiplicative-expression
5746 additive-expression - multiplicative-expression
5750 shift-expression << additive-expression
5751 shift-expression >> additive-expression
5753 relational-expression:
5755 relational-expression < shift-expression
5756 relational-expression > shift-expression
5757 relational-expression <= shift-expression
5758 relational-expression >= shift-expression
5762 relational-expression:
5763 relational-expression <? shift-expression
5764 relational-expression >? shift-expression
5766 equality-expression:
5767 relational-expression
5768 equality-expression == relational-expression
5769 equality-expression != relational-expression
5773 and-expression & equality-expression
5775 exclusive-or-expression:
5777 exclusive-or-expression ^ and-expression
5779 inclusive-or-expression:
5780 exclusive-or-expression
5781 inclusive-or-expression | exclusive-or-expression
5783 logical-and-expression:
5784 inclusive-or-expression
5785 logical-and-expression && inclusive-or-expression
5787 logical-or-expression:
5788 logical-and-expression
5789 logical-or-expression || logical-and-expression
5791 All these are implemented with a single function like:
5794 simple-cast-expression
5795 binary-expression <token> binary-expression
5797 CAST_P is true if this expression is the target of a cast.
5799 The binops_by_token map is used to get the tree codes for each <token> type.
5800 binary-expressions are associated according to a precedence table. */
5802 #define TOKEN_PRECEDENCE(token) \
5803 (((token->type == CPP_GREATER \
5804 || (flag_cpp0x && token->type == CPP_RSHIFT)) \
5805 && !parser->greater_than_is_operator_p) \
5806 ? PREC_NOT_OPERATOR \
5807 : binops_by_token[token->type].prec)
5810 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
)
5812 cp_parser_expression_stack stack
;
5813 cp_parser_expression_stack_entry
*sp
= &stack
[0];
5816 enum tree_code tree_type
, lhs_type
, rhs_type
;
5817 enum cp_parser_prec prec
= PREC_NOT_OPERATOR
, new_prec
, lookahead_prec
;
5820 /* Parse the first expression. */
5821 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
);
5822 lhs_type
= ERROR_MARK
;
5826 /* Get an operator token. */
5827 token
= cp_lexer_peek_token (parser
->lexer
);
5829 if (warn_cxx0x_compat
5830 && token
->type
== CPP_RSHIFT
5831 && !parser
->greater_than_is_operator_p
)
5833 warning (OPT_Wc__0x_compat
,
5834 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5836 warning (OPT_Wc__0x_compat
,
5837 "suggest parentheses around %<>>%> expression");
5840 new_prec
= TOKEN_PRECEDENCE (token
);
5842 /* Popping an entry off the stack means we completed a subexpression:
5843 - either we found a token which is not an operator (`>' where it is not
5844 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5845 will happen repeatedly;
5846 - or, we found an operator which has lower priority. This is the case
5847 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5849 if (new_prec
<= prec
)
5858 tree_type
= binops_by_token
[token
->type
].tree_type
;
5860 /* We used the operator token. */
5861 cp_lexer_consume_token (parser
->lexer
);
5863 /* Extract another operand. It may be the RHS of this expression
5864 or the LHS of a new, higher priority expression. */
5865 rhs
= cp_parser_simple_cast_expression (parser
);
5866 rhs_type
= ERROR_MARK
;
5868 /* Get another operator token. Look up its precedence to avoid
5869 building a useless (immediately popped) stack entry for common
5870 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5871 token
= cp_lexer_peek_token (parser
->lexer
);
5872 lookahead_prec
= TOKEN_PRECEDENCE (token
);
5873 if (lookahead_prec
> new_prec
)
5875 /* ... and prepare to parse the RHS of the new, higher priority
5876 expression. Since precedence levels on the stack are
5877 monotonically increasing, we do not have to care about
5880 sp
->tree_type
= tree_type
;
5882 sp
->lhs_type
= lhs_type
;
5885 lhs_type
= rhs_type
;
5887 new_prec
= lookahead_prec
;
5891 /* If the stack is not empty, we have parsed into LHS the right side
5892 (`4' in the example above) of an expression we had suspended.
5893 We can use the information on the stack to recover the LHS (`3')
5894 from the stack together with the tree code (`MULT_EXPR'), and
5895 the precedence of the higher level subexpression
5896 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5897 which will be used to actually build the additive expression. */
5900 tree_type
= sp
->tree_type
;
5902 rhs_type
= lhs_type
;
5904 lhs_type
= sp
->lhs_type
;
5907 overloaded_p
= false;
5908 lhs
= build_x_binary_op (tree_type
, lhs
, lhs_type
, rhs
, rhs_type
,
5910 lhs_type
= tree_type
;
5912 /* If the binary operator required the use of an overloaded operator,
5913 then this expression cannot be an integral constant-expression.
5914 An overloaded operator can be used even if both operands are
5915 otherwise permissible in an integral constant-expression if at
5916 least one of the operands is of enumeration type. */
5919 && (cp_parser_non_integral_constant_expression
5920 (parser
, "calls to overloaded operators")))
5921 return error_mark_node
;
5928 /* Parse the `? expression : assignment-expression' part of a
5929 conditional-expression. The LOGICAL_OR_EXPR is the
5930 logical-or-expression that started the conditional-expression.
5931 Returns a representation of the entire conditional-expression.
5933 This routine is used by cp_parser_assignment_expression.
5935 ? expression : assignment-expression
5939 ? : assignment-expression */
5942 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5945 tree assignment_expr
;
5947 /* Consume the `?' token. */
5948 cp_lexer_consume_token (parser
->lexer
);
5949 if (cp_parser_allow_gnu_extensions_p (parser
)
5950 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5951 /* Implicit true clause. */
5954 /* Parse the expression. */
5955 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
5957 /* The next token should be a `:'. */
5958 cp_parser_require (parser
, CPP_COLON
, "`:'");
5959 /* Parse the assignment-expression. */
5960 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5962 /* Build the conditional-expression. */
5963 return build_x_conditional_expr (logical_or_expr
,
5968 /* Parse an assignment-expression.
5970 assignment-expression:
5971 conditional-expression
5972 logical-or-expression assignment-operator assignment_expression
5975 CAST_P is true if this expression is the target of a cast.
5977 Returns a representation for the expression. */
5980 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
)
5984 /* If the next token is the `throw' keyword, then we're looking at
5985 a throw-expression. */
5986 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5987 expr
= cp_parser_throw_expression (parser
);
5988 /* Otherwise, it must be that we are looking at a
5989 logical-or-expression. */
5992 /* Parse the binary expressions (logical-or-expression). */
5993 expr
= cp_parser_binary_expression (parser
, cast_p
);
5994 /* If the next token is a `?' then we're actually looking at a
5995 conditional-expression. */
5996 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5997 return cp_parser_question_colon_clause (parser
, expr
);
6000 enum tree_code assignment_operator
;
6002 /* If it's an assignment-operator, we're using the second
6005 = cp_parser_assignment_operator_opt (parser
);
6006 if (assignment_operator
!= ERROR_MARK
)
6010 /* Parse the right-hand side of the assignment. */
6011 rhs
= cp_parser_assignment_expression (parser
, cast_p
);
6012 /* An assignment may not appear in a
6013 constant-expression. */
6014 if (cp_parser_non_integral_constant_expression (parser
,
6016 return error_mark_node
;
6017 /* Build the assignment expression. */
6018 expr
= build_x_modify_expr (expr
,
6019 assignment_operator
,
6028 /* Parse an (optional) assignment-operator.
6030 assignment-operator: one of
6031 = *= /= %= += -= >>= <<= &= ^= |=
6035 assignment-operator: one of
6038 If the next token is an assignment operator, the corresponding tree
6039 code is returned, and the token is consumed. For example, for
6040 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6041 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6042 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6043 operator, ERROR_MARK is returned. */
6045 static enum tree_code
6046 cp_parser_assignment_operator_opt (cp_parser
* parser
)
6051 /* Peek at the next toen. */
6052 token
= cp_lexer_peek_token (parser
->lexer
);
6054 switch (token
->type
)
6065 op
= TRUNC_DIV_EXPR
;
6069 op
= TRUNC_MOD_EXPR
;
6101 /* Nothing else is an assignment operator. */
6105 /* If it was an assignment operator, consume it. */
6106 if (op
!= ERROR_MARK
)
6107 cp_lexer_consume_token (parser
->lexer
);
6112 /* Parse an expression.
6115 assignment-expression
6116 expression , assignment-expression
6118 CAST_P is true if this expression is the target of a cast.
6120 Returns a representation of the expression. */
6123 cp_parser_expression (cp_parser
* parser
, bool cast_p
)
6125 tree expression
= NULL_TREE
;
6129 tree assignment_expression
;
6131 /* Parse the next assignment-expression. */
6132 assignment_expression
6133 = cp_parser_assignment_expression (parser
, cast_p
);
6134 /* If this is the first assignment-expression, we can just
6137 expression
= assignment_expression
;
6139 expression
= build_x_compound_expr (expression
,
6140 assignment_expression
);
6141 /* If the next token is not a comma, then we are done with the
6143 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6145 /* Consume the `,'. */
6146 cp_lexer_consume_token (parser
->lexer
);
6147 /* A comma operator cannot appear in a constant-expression. */
6148 if (cp_parser_non_integral_constant_expression (parser
,
6149 "a comma operator"))
6150 expression
= error_mark_node
;
6156 /* Parse a constant-expression.
6158 constant-expression:
6159 conditional-expression
6161 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6162 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6163 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6164 is false, NON_CONSTANT_P should be NULL. */
6167 cp_parser_constant_expression (cp_parser
* parser
,
6168 bool allow_non_constant_p
,
6169 bool *non_constant_p
)
6171 bool saved_integral_constant_expression_p
;
6172 bool saved_allow_non_integral_constant_expression_p
;
6173 bool saved_non_integral_constant_expression_p
;
6176 /* It might seem that we could simply parse the
6177 conditional-expression, and then check to see if it were
6178 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6179 one that the compiler can figure out is constant, possibly after
6180 doing some simplifications or optimizations. The standard has a
6181 precise definition of constant-expression, and we must honor
6182 that, even though it is somewhat more restrictive.
6188 is not a legal declaration, because `(2, 3)' is not a
6189 constant-expression. The `,' operator is forbidden in a
6190 constant-expression. However, GCC's constant-folding machinery
6191 will fold this operation to an INTEGER_CST for `3'. */
6193 /* Save the old settings. */
6194 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
6195 saved_allow_non_integral_constant_expression_p
6196 = parser
->allow_non_integral_constant_expression_p
;
6197 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
6198 /* We are now parsing a constant-expression. */
6199 parser
->integral_constant_expression_p
= true;
6200 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
6201 parser
->non_integral_constant_expression_p
= false;
6202 /* Although the grammar says "conditional-expression", we parse an
6203 "assignment-expression", which also permits "throw-expression"
6204 and the use of assignment operators. In the case that
6205 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6206 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6207 actually essential that we look for an assignment-expression.
6208 For example, cp_parser_initializer_clauses uses this function to
6209 determine whether a particular assignment-expression is in fact
6211 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
6212 /* Restore the old settings. */
6213 parser
->integral_constant_expression_p
6214 = saved_integral_constant_expression_p
;
6215 parser
->allow_non_integral_constant_expression_p
6216 = saved_allow_non_integral_constant_expression_p
;
6217 if (allow_non_constant_p
)
6218 *non_constant_p
= parser
->non_integral_constant_expression_p
;
6219 else if (parser
->non_integral_constant_expression_p
)
6220 expression
= error_mark_node
;
6221 parser
->non_integral_constant_expression_p
6222 = saved_non_integral_constant_expression_p
;
6227 /* Parse __builtin_offsetof.
6229 offsetof-expression:
6230 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6232 offsetof-member-designator:
6234 | offsetof-member-designator "." id-expression
6235 | offsetof-member-designator "[" expression "]" */
6238 cp_parser_builtin_offsetof (cp_parser
*parser
)
6240 int save_ice_p
, save_non_ice_p
;
6244 /* We're about to accept non-integral-constant things, but will
6245 definitely yield an integral constant expression. Save and
6246 restore these values around our local parsing. */
6247 save_ice_p
= parser
->integral_constant_expression_p
;
6248 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
6250 /* Consume the "__builtin_offsetof" token. */
6251 cp_lexer_consume_token (parser
->lexer
);
6252 /* Consume the opening `('. */
6253 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6254 /* Parse the type-id. */
6255 type
= cp_parser_type_id (parser
);
6256 /* Look for the `,'. */
6257 cp_parser_require (parser
, CPP_COMMA
, "`,'");
6259 /* Build the (type *)null that begins the traditional offsetof macro. */
6260 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
);
6262 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6263 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
6267 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6268 switch (token
->type
)
6270 case CPP_OPEN_SQUARE
:
6271 /* offsetof-member-designator "[" expression "]" */
6272 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
6276 /* offsetof-member-designator "." identifier */
6277 cp_lexer_consume_token (parser
->lexer
);
6278 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
, expr
,
6282 case CPP_CLOSE_PAREN
:
6283 /* Consume the ")" token. */
6284 cp_lexer_consume_token (parser
->lexer
);
6288 /* Error. We know the following require will fail, but
6289 that gives the proper error message. */
6290 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6291 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
6292 expr
= error_mark_node
;
6298 /* If we're processing a template, we can't finish the semantics yet.
6299 Otherwise we can fold the entire expression now. */
6300 if (processing_template_decl
)
6301 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
6303 expr
= finish_offsetof (expr
);
6306 parser
->integral_constant_expression_p
= save_ice_p
;
6307 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
6312 /* Statements [gram.stmt.stmt] */
6314 /* Parse a statement.
6318 expression-statement
6323 declaration-statement
6326 IN_COMPOUND is true when the statement is nested inside a
6327 cp_parser_compound_statement; this matters for certain pragmas.
6329 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6330 is a (possibly labeled) if statement which is not enclosed in braces
6331 and has an else clause. This is used to implement -Wparentheses. */
6334 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
6335 bool in_compound
, bool *if_p
)
6339 location_t statement_location
;
6344 /* There is no statement yet. */
6345 statement
= NULL_TREE
;
6346 /* Peek at the next token. */
6347 token
= cp_lexer_peek_token (parser
->lexer
);
6348 /* Remember the location of the first token in the statement. */
6349 statement_location
= token
->location
;
6350 /* If this is a keyword, then that will often determine what kind of
6351 statement we have. */
6352 if (token
->type
== CPP_KEYWORD
)
6354 enum rid keyword
= token
->keyword
;
6360 /* Looks like a labeled-statement with a case label.
6361 Parse the label, and then use tail recursion to parse
6363 cp_parser_label_for_labeled_statement (parser
);
6368 statement
= cp_parser_selection_statement (parser
, if_p
);
6374 statement
= cp_parser_iteration_statement (parser
);
6381 statement
= cp_parser_jump_statement (parser
);
6384 /* Objective-C++ exception-handling constructs. */
6387 case RID_AT_FINALLY
:
6388 case RID_AT_SYNCHRONIZED
:
6390 statement
= cp_parser_objc_statement (parser
);
6394 statement
= cp_parser_try_block (parser
);
6398 /* This must be a namespace alias definition. */
6399 cp_parser_declaration_statement (parser
);
6403 /* It might be a keyword like `int' that can start a
6404 declaration-statement. */
6408 else if (token
->type
== CPP_NAME
)
6410 /* If the next token is a `:', then we are looking at a
6411 labeled-statement. */
6412 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6413 if (token
->type
== CPP_COLON
)
6415 /* Looks like a labeled-statement with an ordinary label.
6416 Parse the label, and then use tail recursion to parse
6418 cp_parser_label_for_labeled_statement (parser
);
6422 /* Anything that starts with a `{' must be a compound-statement. */
6423 else if (token
->type
== CPP_OPEN_BRACE
)
6424 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6425 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6426 a statement all its own. */
6427 else if (token
->type
== CPP_PRAGMA
)
6429 /* Only certain OpenMP pragmas are attached to statements, and thus
6430 are considered statements themselves. All others are not. In
6431 the context of a compound, accept the pragma as a "statement" and
6432 return so that we can check for a close brace. Otherwise we
6433 require a real statement and must go back and read one. */
6435 cp_parser_pragma (parser
, pragma_compound
);
6436 else if (!cp_parser_pragma (parser
, pragma_stmt
))
6440 else if (token
->type
== CPP_EOF
)
6442 cp_parser_error (parser
, "expected statement");
6446 /* Everything else must be a declaration-statement or an
6447 expression-statement. Try for the declaration-statement
6448 first, unless we are looking at a `;', in which case we know that
6449 we have an expression-statement. */
6452 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6454 cp_parser_parse_tentatively (parser
);
6455 /* Try to parse the declaration-statement. */
6456 cp_parser_declaration_statement (parser
);
6457 /* If that worked, we're done. */
6458 if (cp_parser_parse_definitely (parser
))
6461 /* Look for an expression-statement instead. */
6462 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
6465 /* Set the line number for the statement. */
6466 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
6467 SET_EXPR_LOCATION (statement
, statement_location
);
6470 /* Parse the label for a labeled-statement, i.e.
6473 case constant-expression :
6477 case constant-expression ... constant-expression : statement
6479 When a label is parsed without errors, the label is added to the
6480 parse tree by the finish_* functions, so this function doesn't
6481 have to return the label. */
6484 cp_parser_label_for_labeled_statement (cp_parser
* parser
)
6488 /* The next token should be an identifier. */
6489 token
= cp_lexer_peek_token (parser
->lexer
);
6490 if (token
->type
!= CPP_NAME
6491 && token
->type
!= CPP_KEYWORD
)
6493 cp_parser_error (parser
, "expected labeled-statement");
6497 switch (token
->keyword
)
6504 /* Consume the `case' token. */
6505 cp_lexer_consume_token (parser
->lexer
);
6506 /* Parse the constant-expression. */
6507 expr
= cp_parser_constant_expression (parser
,
6508 /*allow_non_constant_p=*/false,
6511 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
6512 if (ellipsis
->type
== CPP_ELLIPSIS
)
6514 /* Consume the `...' token. */
6515 cp_lexer_consume_token (parser
->lexer
);
6517 cp_parser_constant_expression (parser
,
6518 /*allow_non_constant_p=*/false,
6520 /* We don't need to emit warnings here, as the common code
6521 will do this for us. */
6524 expr_hi
= NULL_TREE
;
6526 if (parser
->in_switch_statement_p
)
6527 finish_case_label (expr
, expr_hi
);
6529 error ("case label %qE not within a switch statement", expr
);
6534 /* Consume the `default' token. */
6535 cp_lexer_consume_token (parser
->lexer
);
6537 if (parser
->in_switch_statement_p
)
6538 finish_case_label (NULL_TREE
, NULL_TREE
);
6540 error ("case label not within a switch statement");
6544 /* Anything else must be an ordinary label. */
6545 finish_label_stmt (cp_parser_identifier (parser
));
6549 /* Require the `:' token. */
6550 cp_parser_require (parser
, CPP_COLON
, "`:'");
6553 /* Parse an expression-statement.
6555 expression-statement:
6558 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6559 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6560 indicates whether this expression-statement is part of an
6561 expression statement. */
6564 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
6566 tree statement
= NULL_TREE
;
6568 /* If the next token is a ';', then there is no expression
6570 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6571 statement
= cp_parser_expression (parser
, /*cast_p=*/false);
6573 /* Consume the final `;'. */
6574 cp_parser_consume_semicolon_at_end_of_statement (parser
);
6576 if (in_statement_expr
6577 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
6578 /* This is the final expression statement of a statement
6580 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
6582 statement
= finish_expr_stmt (statement
);
6589 /* Parse a compound-statement.
6592 { statement-seq [opt] }
6594 Returns a tree representing the statement. */
6597 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
6602 /* Consume the `{'. */
6603 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
6604 return error_mark_node
;
6605 /* Begin the compound-statement. */
6606 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
6607 /* Parse an (optional) statement-seq. */
6608 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
6609 /* Finish the compound-statement. */
6610 finish_compound_stmt (compound_stmt
);
6611 /* Consume the `}'. */
6612 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6614 return compound_stmt
;
6617 /* Parse an (optional) statement-seq.
6621 statement-seq [opt] statement */
6624 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
6626 /* Scan statements until there aren't any more. */
6629 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6631 /* If we're looking at a `}', then we've run out of statements. */
6632 if (token
->type
== CPP_CLOSE_BRACE
6633 || token
->type
== CPP_EOF
6634 || token
->type
== CPP_PRAGMA_EOL
)
6637 /* If we are in a compound statement and find 'else' then
6638 something went wrong. */
6639 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
6641 if (parser
->in_statement
& IN_IF_STMT
)
6645 token
= cp_lexer_consume_token (parser
->lexer
);
6646 error ("%<else%> without a previous %<if%>");
6650 /* Parse the statement. */
6651 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
6655 /* Parse a selection-statement.
6657 selection-statement:
6658 if ( condition ) statement
6659 if ( condition ) statement else statement
6660 switch ( condition ) statement
6662 Returns the new IF_STMT or SWITCH_STMT.
6664 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6665 is a (possibly labeled) if statement which is not enclosed in
6666 braces and has an else clause. This is used to implement
6670 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
6678 /* Peek at the next token. */
6679 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
6681 /* See what kind of keyword it is. */
6682 keyword
= token
->keyword
;
6691 /* Look for the `('. */
6692 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
6694 cp_parser_skip_to_end_of_statement (parser
);
6695 return error_mark_node
;
6698 /* Begin the selection-statement. */
6699 if (keyword
== RID_IF
)
6700 statement
= begin_if_stmt ();
6702 statement
= begin_switch_stmt ();
6704 /* Parse the condition. */
6705 condition
= cp_parser_condition (parser
);
6706 /* Look for the `)'. */
6707 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
6708 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
6709 /*consume_paren=*/true);
6711 if (keyword
== RID_IF
)
6714 unsigned char in_statement
;
6716 /* Add the condition. */
6717 finish_if_stmt_cond (condition
, statement
);
6719 /* Parse the then-clause. */
6720 in_statement
= parser
->in_statement
;
6721 parser
->in_statement
|= IN_IF_STMT
;
6722 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
6723 parser
->in_statement
= in_statement
;
6725 finish_then_clause (statement
);
6727 /* If the next token is `else', parse the else-clause. */
6728 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
6731 /* Consume the `else' keyword. */
6732 cp_lexer_consume_token (parser
->lexer
);
6733 begin_else_clause (statement
);
6734 /* Parse the else-clause. */
6735 cp_parser_implicitly_scoped_statement (parser
, NULL
);
6736 finish_else_clause (statement
);
6738 /* If we are currently parsing a then-clause, then
6739 IF_P will not be NULL. We set it to true to
6740 indicate that this if statement has an else clause.
6741 This may trigger the Wparentheses warning below
6742 when we get back up to the parent if statement. */
6748 /* This if statement does not have an else clause. If
6749 NESTED_IF is true, then the then-clause is an if
6750 statement which does have an else clause. We warn
6751 about the potential ambiguity. */
6753 warning (OPT_Wparentheses
,
6754 ("%Hsuggest explicit braces "
6755 "to avoid ambiguous %<else%>"),
6756 EXPR_LOCUS (statement
));
6759 /* Now we're all done with the if-statement. */
6760 finish_if_stmt (statement
);
6764 bool in_switch_statement_p
;
6765 unsigned char in_statement
;
6767 /* Add the condition. */
6768 finish_switch_cond (condition
, statement
);
6770 /* Parse the body of the switch-statement. */
6771 in_switch_statement_p
= parser
->in_switch_statement_p
;
6772 in_statement
= parser
->in_statement
;
6773 parser
->in_switch_statement_p
= true;
6774 parser
->in_statement
|= IN_SWITCH_STMT
;
6775 cp_parser_implicitly_scoped_statement (parser
, NULL
);
6776 parser
->in_switch_statement_p
= in_switch_statement_p
;
6777 parser
->in_statement
= in_statement
;
6779 /* Now we're all done with the switch-statement. */
6780 finish_switch_stmt (statement
);
6788 cp_parser_error (parser
, "expected selection-statement");
6789 return error_mark_node
;
6793 /* Parse a condition.
6797 type-specifier-seq declarator = assignment-expression
6802 type-specifier-seq declarator asm-specification [opt]
6803 attributes [opt] = assignment-expression
6805 Returns the expression that should be tested. */
6808 cp_parser_condition (cp_parser
* parser
)
6810 cp_decl_specifier_seq type_specifiers
;
6811 const char *saved_message
;
6813 /* Try the declaration first. */
6814 cp_parser_parse_tentatively (parser
);
6815 /* New types are not allowed in the type-specifier-seq for a
6817 saved_message
= parser
->type_definition_forbidden_message
;
6818 parser
->type_definition_forbidden_message
6819 = "types may not be defined in conditions";
6820 /* Parse the type-specifier-seq. */
6821 cp_parser_type_specifier_seq (parser
, /*is_condition==*/true,
6823 /* Restore the saved message. */
6824 parser
->type_definition_forbidden_message
= saved_message
;
6825 /* If all is well, we might be looking at a declaration. */
6826 if (!cp_parser_error_occurred (parser
))
6829 tree asm_specification
;
6831 cp_declarator
*declarator
;
6832 tree initializer
= NULL_TREE
;
6834 /* Parse the declarator. */
6835 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
6836 /*ctor_dtor_or_conv_p=*/NULL
,
6837 /*parenthesized_p=*/NULL
,
6838 /*member_p=*/false);
6839 /* Parse the attributes. */
6840 attributes
= cp_parser_attributes_opt (parser
);
6841 /* Parse the asm-specification. */
6842 asm_specification
= cp_parser_asm_specification_opt (parser
);
6843 /* If the next token is not an `=', then we might still be
6844 looking at an expression. For example:
6848 looks like a decl-specifier-seq and a declarator -- but then
6849 there is no `=', so this is an expression. */
6850 cp_parser_require (parser
, CPP_EQ
, "`='");
6851 /* If we did see an `=', then we are looking at a declaration
6853 if (cp_parser_parse_definitely (parser
))
6856 bool non_constant_p
;
6858 /* Create the declaration. */
6859 decl
= start_decl (declarator
, &type_specifiers
,
6860 /*initialized_p=*/true,
6861 attributes
, /*prefix_attributes=*/NULL_TREE
,
6863 /* Parse the assignment-expression. */
6865 = cp_parser_constant_expression (parser
,
6866 /*allow_non_constant_p=*/true,
6868 if (!non_constant_p
)
6869 initializer
= fold_non_dependent_expr (initializer
);
6871 /* Process the initializer. */
6872 cp_finish_decl (decl
,
6873 initializer
, !non_constant_p
,
6875 LOOKUP_ONLYCONVERTING
);
6878 pop_scope (pushed_scope
);
6880 return convert_from_reference (decl
);
6883 /* If we didn't even get past the declarator successfully, we are
6884 definitely not looking at a declaration. */
6886 cp_parser_abort_tentative_parse (parser
);
6888 /* Otherwise, we are looking at an expression. */
6889 return cp_parser_expression (parser
, /*cast_p=*/false);
6892 /* Parse an iteration-statement.
6894 iteration-statement:
6895 while ( condition ) statement
6896 do statement while ( expression ) ;
6897 for ( for-init-statement condition [opt] ; expression [opt] )
6900 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6903 cp_parser_iteration_statement (cp_parser
* parser
)
6908 unsigned char in_statement
;
6910 /* Peek at the next token. */
6911 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
6913 return error_mark_node
;
6915 /* Remember whether or not we are already within an iteration
6917 in_statement
= parser
->in_statement
;
6919 /* See what kind of keyword it is. */
6920 keyword
= token
->keyword
;
6927 /* Begin the while-statement. */
6928 statement
= begin_while_stmt ();
6929 /* Look for the `('. */
6930 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6931 /* Parse the condition. */
6932 condition
= cp_parser_condition (parser
);
6933 finish_while_stmt_cond (condition
, statement
);
6934 /* Look for the `)'. */
6935 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6936 /* Parse the dependent statement. */
6937 parser
->in_statement
= IN_ITERATION_STMT
;
6938 cp_parser_already_scoped_statement (parser
);
6939 parser
->in_statement
= in_statement
;
6940 /* We're done with the while-statement. */
6941 finish_while_stmt (statement
);
6949 /* Begin the do-statement. */
6950 statement
= begin_do_stmt ();
6951 /* Parse the body of the do-statement. */
6952 parser
->in_statement
= IN_ITERATION_STMT
;
6953 cp_parser_implicitly_scoped_statement (parser
, NULL
);
6954 parser
->in_statement
= in_statement
;
6955 finish_do_body (statement
);
6956 /* Look for the `while' keyword. */
6957 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
6958 /* Look for the `('. */
6959 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6960 /* Parse the expression. */
6961 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6962 /* We're done with the do-statement. */
6963 finish_do_stmt (expression
, statement
);
6964 /* Look for the `)'. */
6965 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6966 /* Look for the `;'. */
6967 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6973 tree condition
= NULL_TREE
;
6974 tree expression
= NULL_TREE
;
6976 /* Begin the for-statement. */
6977 statement
= begin_for_stmt ();
6978 /* Look for the `('. */
6979 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6980 /* Parse the initialization. */
6981 cp_parser_for_init_statement (parser
);
6982 finish_for_init_stmt (statement
);
6984 /* If there's a condition, process it. */
6985 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6986 condition
= cp_parser_condition (parser
);
6987 finish_for_cond (condition
, statement
);
6988 /* Look for the `;'. */
6989 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6991 /* If there's an expression, process it. */
6992 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6993 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6994 finish_for_expr (expression
, statement
);
6995 /* Look for the `)'. */
6996 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6998 /* Parse the body of the for-statement. */
6999 parser
->in_statement
= IN_ITERATION_STMT
;
7000 cp_parser_already_scoped_statement (parser
);
7001 parser
->in_statement
= in_statement
;
7003 /* We're done with the for-statement. */
7004 finish_for_stmt (statement
);
7009 cp_parser_error (parser
, "expected iteration-statement");
7010 statement
= error_mark_node
;
7017 /* Parse a for-init-statement.
7020 expression-statement
7021 simple-declaration */
7024 cp_parser_for_init_statement (cp_parser
* parser
)
7026 /* If the next token is a `;', then we have an empty
7027 expression-statement. Grammatically, this is also a
7028 simple-declaration, but an invalid one, because it does not
7029 declare anything. Therefore, if we did not handle this case
7030 specially, we would issue an error message about an invalid
7032 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7034 /* We're going to speculatively look for a declaration, falling back
7035 to an expression, if necessary. */
7036 cp_parser_parse_tentatively (parser
);
7037 /* Parse the declaration. */
7038 cp_parser_simple_declaration (parser
,
7039 /*function_definition_allowed_p=*/false);
7040 /* If the tentative parse failed, then we shall need to look for an
7041 expression-statement. */
7042 if (cp_parser_parse_definitely (parser
))
7046 cp_parser_expression_statement (parser
, false);
7049 /* Parse a jump-statement.
7054 return expression [opt] ;
7062 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7065 cp_parser_jump_statement (cp_parser
* parser
)
7067 tree statement
= error_mark_node
;
7070 unsigned char in_statement
;
7072 /* Peek at the next token. */
7073 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
7075 return error_mark_node
;
7077 /* See what kind of keyword it is. */
7078 keyword
= token
->keyword
;
7082 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
7083 switch (in_statement
)
7086 error ("break statement not within loop or switch");
7089 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
7090 || in_statement
== IN_ITERATION_STMT
);
7091 statement
= finish_break_stmt ();
7094 error ("invalid exit from OpenMP structured block");
7097 error ("break statement used with OpenMP for loop");
7100 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7104 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
7107 error ("continue statement not within a loop");
7109 case IN_ITERATION_STMT
:
7111 statement
= finish_continue_stmt ();
7114 error ("invalid exit from OpenMP structured block");
7119 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7126 /* If the next token is a `;', then there is no
7128 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7129 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
7132 /* Build the return-statement. */
7133 statement
= finish_return_stmt (expr
);
7134 /* Look for the final `;'. */
7135 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7140 /* Create the goto-statement. */
7141 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
7143 /* Issue a warning about this use of a GNU extension. */
7145 pedwarn ("ISO C++ forbids computed gotos");
7146 /* Consume the '*' token. */
7147 cp_lexer_consume_token (parser
->lexer
);
7148 /* Parse the dependent expression. */
7149 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false));
7152 finish_goto_stmt (cp_parser_identifier (parser
));
7153 /* Look for the final `;'. */
7154 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7158 cp_parser_error (parser
, "expected jump-statement");
7165 /* Parse a declaration-statement.
7167 declaration-statement:
7168 block-declaration */
7171 cp_parser_declaration_statement (cp_parser
* parser
)
7175 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7176 p
= obstack_alloc (&declarator_obstack
, 0);
7178 /* Parse the block-declaration. */
7179 cp_parser_block_declaration (parser
, /*statement_p=*/true);
7181 /* Free any declarators allocated. */
7182 obstack_free (&declarator_obstack
, p
);
7184 /* Finish off the statement. */
7188 /* Some dependent statements (like `if (cond) statement'), are
7189 implicitly in their own scope. In other words, if the statement is
7190 a single statement (as opposed to a compound-statement), it is
7191 none-the-less treated as if it were enclosed in braces. Any
7192 declarations appearing in the dependent statement are out of scope
7193 after control passes that point. This function parses a statement,
7194 but ensures that is in its own scope, even if it is not a
7197 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7198 is a (possibly labeled) if statement which is not enclosed in
7199 braces and has an else clause. This is used to implement
7202 Returns the new statement. */
7205 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
7212 /* Mark if () ; with a special NOP_EXPR. */
7213 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7215 cp_lexer_consume_token (parser
->lexer
);
7216 statement
= add_stmt (build_empty_stmt ());
7218 /* if a compound is opened, we simply parse the statement directly. */
7219 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7220 statement
= cp_parser_compound_statement (parser
, NULL
, false);
7221 /* If the token is not a `{', then we must take special action. */
7224 /* Create a compound-statement. */
7225 statement
= begin_compound_stmt (0);
7226 /* Parse the dependent-statement. */
7227 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
7228 /* Finish the dummy compound-statement. */
7229 finish_compound_stmt (statement
);
7232 /* Return the statement. */
7236 /* For some dependent statements (like `while (cond) statement'), we
7237 have already created a scope. Therefore, even if the dependent
7238 statement is a compound-statement, we do not want to create another
7242 cp_parser_already_scoped_statement (cp_parser
* parser
)
7244 /* If the token is a `{', then we must take special action. */
7245 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
7246 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
7249 /* Avoid calling cp_parser_compound_statement, so that we
7250 don't create a new scope. Do everything else by hand. */
7251 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
7252 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
7253 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7257 /* Declarations [gram.dcl.dcl] */
7259 /* Parse an optional declaration-sequence.
7263 declaration-seq declaration */
7266 cp_parser_declaration_seq_opt (cp_parser
* parser
)
7272 token
= cp_lexer_peek_token (parser
->lexer
);
7274 if (token
->type
== CPP_CLOSE_BRACE
7275 || token
->type
== CPP_EOF
7276 || token
->type
== CPP_PRAGMA_EOL
)
7279 if (token
->type
== CPP_SEMICOLON
)
7281 /* A declaration consisting of a single semicolon is
7282 invalid. Allow it unless we're being pedantic. */
7283 cp_lexer_consume_token (parser
->lexer
);
7284 if (pedantic
&& !in_system_header
)
7285 pedwarn ("extra %<;%>");
7289 /* If we're entering or exiting a region that's implicitly
7290 extern "C", modify the lang context appropriately. */
7291 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
7293 push_lang_context (lang_name_c
);
7294 parser
->implicit_extern_c
= true;
7296 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
7298 pop_lang_context ();
7299 parser
->implicit_extern_c
= false;
7302 if (token
->type
== CPP_PRAGMA
)
7304 /* A top-level declaration can consist solely of a #pragma.
7305 A nested declaration cannot, so this is done here and not
7306 in cp_parser_declaration. (A #pragma at block scope is
7307 handled in cp_parser_statement.) */
7308 cp_parser_pragma (parser
, pragma_external
);
7312 /* Parse the declaration itself. */
7313 cp_parser_declaration (parser
);
7317 /* Parse a declaration.
7322 template-declaration
7323 explicit-instantiation
7324 explicit-specialization
7325 linkage-specification
7326 namespace-definition
7331 __extension__ declaration */
7334 cp_parser_declaration (cp_parser
* parser
)
7341 /* Check for the `__extension__' keyword. */
7342 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7344 /* Parse the qualified declaration. */
7345 cp_parser_declaration (parser
);
7346 /* Restore the PEDANTIC flag. */
7347 pedantic
= saved_pedantic
;
7352 /* Try to figure out what kind of declaration is present. */
7353 token1
= *cp_lexer_peek_token (parser
->lexer
);
7355 if (token1
.type
!= CPP_EOF
)
7356 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
7359 token2
.type
= CPP_EOF
;
7360 token2
.keyword
= RID_MAX
;
7363 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7364 p
= obstack_alloc (&declarator_obstack
, 0);
7366 /* If the next token is `extern' and the following token is a string
7367 literal, then we have a linkage specification. */
7368 if (token1
.keyword
== RID_EXTERN
7369 && cp_parser_is_string_literal (&token2
))
7370 cp_parser_linkage_specification (parser
);
7371 /* If the next token is `template', then we have either a template
7372 declaration, an explicit instantiation, or an explicit
7374 else if (token1
.keyword
== RID_TEMPLATE
)
7376 /* `template <>' indicates a template specialization. */
7377 if (token2
.type
== CPP_LESS
7378 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
7379 cp_parser_explicit_specialization (parser
);
7380 /* `template <' indicates a template declaration. */
7381 else if (token2
.type
== CPP_LESS
)
7382 cp_parser_template_declaration (parser
, /*member_p=*/false);
7383 /* Anything else must be an explicit instantiation. */
7385 cp_parser_explicit_instantiation (parser
);
7387 /* If the next token is `export', then we have a template
7389 else if (token1
.keyword
== RID_EXPORT
)
7390 cp_parser_template_declaration (parser
, /*member_p=*/false);
7391 /* If the next token is `extern', 'static' or 'inline' and the one
7392 after that is `template', we have a GNU extended explicit
7393 instantiation directive. */
7394 else if (cp_parser_allow_gnu_extensions_p (parser
)
7395 && (token1
.keyword
== RID_EXTERN
7396 || token1
.keyword
== RID_STATIC
7397 || token1
.keyword
== RID_INLINE
)
7398 && token2
.keyword
== RID_TEMPLATE
)
7399 cp_parser_explicit_instantiation (parser
);
7400 /* If the next token is `namespace', check for a named or unnamed
7401 namespace definition. */
7402 else if (token1
.keyword
== RID_NAMESPACE
7403 && (/* A named namespace definition. */
7404 (token2
.type
== CPP_NAME
7405 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
7407 /* An unnamed namespace definition. */
7408 || token2
.type
== CPP_OPEN_BRACE
7409 || token2
.keyword
== RID_ATTRIBUTE
))
7410 cp_parser_namespace_definition (parser
);
7411 /* Objective-C++ declaration/definition. */
7412 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
7413 cp_parser_objc_declaration (parser
);
7414 /* We must have either a block declaration or a function
7417 /* Try to parse a block-declaration, or a function-definition. */
7418 cp_parser_block_declaration (parser
, /*statement_p=*/false);
7420 /* Free any declarators allocated. */
7421 obstack_free (&declarator_obstack
, p
);
7424 /* Parse a block-declaration.
7429 namespace-alias-definition
7436 __extension__ block-declaration
7442 static_assert-declaration
7444 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7445 part of a declaration-statement. */
7448 cp_parser_block_declaration (cp_parser
*parser
,
7454 /* Check for the `__extension__' keyword. */
7455 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7457 /* Parse the qualified declaration. */
7458 cp_parser_block_declaration (parser
, statement_p
);
7459 /* Restore the PEDANTIC flag. */
7460 pedantic
= saved_pedantic
;
7465 /* Peek at the next token to figure out which kind of declaration is
7467 token1
= cp_lexer_peek_token (parser
->lexer
);
7469 /* If the next keyword is `asm', we have an asm-definition. */
7470 if (token1
->keyword
== RID_ASM
)
7473 cp_parser_commit_to_tentative_parse (parser
);
7474 cp_parser_asm_definition (parser
);
7476 /* If the next keyword is `namespace', we have a
7477 namespace-alias-definition. */
7478 else if (token1
->keyword
== RID_NAMESPACE
)
7479 cp_parser_namespace_alias_definition (parser
);
7480 /* If the next keyword is `using', we have either a
7481 using-declaration or a using-directive. */
7482 else if (token1
->keyword
== RID_USING
)
7487 cp_parser_commit_to_tentative_parse (parser
);
7488 /* If the token after `using' is `namespace', then we have a
7490 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7491 if (token2
->keyword
== RID_NAMESPACE
)
7492 cp_parser_using_directive (parser
);
7493 /* Otherwise, it's a using-declaration. */
7495 cp_parser_using_declaration (parser
,
7496 /*access_declaration_p=*/false);
7498 /* If the next keyword is `__label__' we have a label declaration. */
7499 else if (token1
->keyword
== RID_LABEL
)
7502 cp_parser_commit_to_tentative_parse (parser
);
7503 cp_parser_label_declaration (parser
);
7505 /* If the next token is `static_assert' we have a static assertion. */
7506 else if (token1
->keyword
== RID_STATIC_ASSERT
)
7507 cp_parser_static_assert (parser
, /*member_p=*/false);
7508 /* Anything else must be a simple-declaration. */
7510 cp_parser_simple_declaration (parser
, !statement_p
);
7513 /* Parse a simple-declaration.
7516 decl-specifier-seq [opt] init-declarator-list [opt] ;
7518 init-declarator-list:
7520 init-declarator-list , init-declarator
7522 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7523 function-definition as a simple-declaration. */
7526 cp_parser_simple_declaration (cp_parser
* parser
,
7527 bool function_definition_allowed_p
)
7529 cp_decl_specifier_seq decl_specifiers
;
7530 int declares_class_or_enum
;
7531 bool saw_declarator
;
7533 /* Defer access checks until we know what is being declared; the
7534 checks for names appearing in the decl-specifier-seq should be
7535 done as if we were in the scope of the thing being declared. */
7536 push_deferring_access_checks (dk_deferred
);
7538 /* Parse the decl-specifier-seq. We have to keep track of whether
7539 or not the decl-specifier-seq declares a named class or
7540 enumeration type, since that is the only case in which the
7541 init-declarator-list is allowed to be empty.
7545 In a simple-declaration, the optional init-declarator-list can be
7546 omitted only when declaring a class or enumeration, that is when
7547 the decl-specifier-seq contains either a class-specifier, an
7548 elaborated-type-specifier, or an enum-specifier. */
7549 cp_parser_decl_specifier_seq (parser
,
7550 CP_PARSER_FLAGS_OPTIONAL
,
7552 &declares_class_or_enum
);
7553 /* We no longer need to defer access checks. */
7554 stop_deferring_access_checks ();
7556 /* In a block scope, a valid declaration must always have a
7557 decl-specifier-seq. By not trying to parse declarators, we can
7558 resolve the declaration/expression ambiguity more quickly. */
7559 if (!function_definition_allowed_p
7560 && !decl_specifiers
.any_specifiers_p
)
7562 cp_parser_error (parser
, "expected declaration");
7566 /* If the next two tokens are both identifiers, the code is
7567 erroneous. The usual cause of this situation is code like:
7571 where "T" should name a type -- but does not. */
7572 if (!decl_specifiers
.type
7573 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
7575 /* If parsing tentatively, we should commit; we really are
7576 looking at a declaration. */
7577 cp_parser_commit_to_tentative_parse (parser
);
7582 /* If we have seen at least one decl-specifier, and the next token
7583 is not a parenthesis, then we must be looking at a declaration.
7584 (After "int (" we might be looking at a functional cast.) */
7585 if (decl_specifiers
.any_specifiers_p
7586 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7587 cp_parser_commit_to_tentative_parse (parser
);
7589 /* Keep going until we hit the `;' at the end of the simple
7591 saw_declarator
= false;
7592 while (cp_lexer_next_token_is_not (parser
->lexer
,
7596 bool function_definition_p
;
7601 /* If we are processing next declarator, coma is expected */
7602 token
= cp_lexer_peek_token (parser
->lexer
);
7603 gcc_assert (token
->type
== CPP_COMMA
);
7604 cp_lexer_consume_token (parser
->lexer
);
7607 saw_declarator
= true;
7609 /* Parse the init-declarator. */
7610 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
7612 function_definition_allowed_p
,
7614 declares_class_or_enum
,
7615 &function_definition_p
);
7616 /* If an error occurred while parsing tentatively, exit quickly.
7617 (That usually happens when in the body of a function; each
7618 statement is treated as a declaration-statement until proven
7620 if (cp_parser_error_occurred (parser
))
7622 /* Handle function definitions specially. */
7623 if (function_definition_p
)
7625 /* If the next token is a `,', then we are probably
7626 processing something like:
7630 which is erroneous. */
7631 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
7632 error ("mixing declarations and function-definitions is forbidden");
7633 /* Otherwise, we're done with the list of declarators. */
7636 pop_deferring_access_checks ();
7640 /* The next token should be either a `,' or a `;'. */
7641 token
= cp_lexer_peek_token (parser
->lexer
);
7642 /* If it's a `,', there are more declarators to come. */
7643 if (token
->type
== CPP_COMMA
)
7644 /* will be consumed next time around */;
7645 /* If it's a `;', we are done. */
7646 else if (token
->type
== CPP_SEMICOLON
)
7648 /* Anything else is an error. */
7651 /* If we have already issued an error message we don't need
7652 to issue another one. */
7653 if (decl
!= error_mark_node
7654 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
7655 cp_parser_error (parser
, "expected %<,%> or %<;%>");
7656 /* Skip tokens until we reach the end of the statement. */
7657 cp_parser_skip_to_end_of_statement (parser
);
7658 /* If the next token is now a `;', consume it. */
7659 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7660 cp_lexer_consume_token (parser
->lexer
);
7663 /* After the first time around, a function-definition is not
7664 allowed -- even if it was OK at first. For example:
7669 function_definition_allowed_p
= false;
7672 /* Issue an error message if no declarators are present, and the
7673 decl-specifier-seq does not itself declare a class or
7675 if (!saw_declarator
)
7677 if (cp_parser_declares_only_class_p (parser
))
7678 shadow_tag (&decl_specifiers
);
7679 /* Perform any deferred access checks. */
7680 perform_deferred_access_checks ();
7683 /* Consume the `;'. */
7684 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7687 pop_deferring_access_checks ();
7690 /* Parse a decl-specifier-seq.
7693 decl-specifier-seq [opt] decl-specifier
7696 storage-class-specifier
7707 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7709 The parser flags FLAGS is used to control type-specifier parsing.
7711 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7714 1: one of the decl-specifiers is an elaborated-type-specifier
7715 (i.e., a type declaration)
7716 2: one of the decl-specifiers is an enum-specifier or a
7717 class-specifier (i.e., a type definition)
7722 cp_parser_decl_specifier_seq (cp_parser
* parser
,
7723 cp_parser_flags flags
,
7724 cp_decl_specifier_seq
*decl_specs
,
7725 int* declares_class_or_enum
)
7727 bool constructor_possible_p
= !parser
->in_declarator_p
;
7729 /* Clear DECL_SPECS. */
7730 clear_decl_specs (decl_specs
);
7732 /* Assume no class or enumeration type is declared. */
7733 *declares_class_or_enum
= 0;
7735 /* Keep reading specifiers until there are no more to read. */
7739 bool found_decl_spec
;
7742 /* Peek at the next token. */
7743 token
= cp_lexer_peek_token (parser
->lexer
);
7744 /* Handle attributes. */
7745 if (token
->keyword
== RID_ATTRIBUTE
)
7747 /* Parse the attributes. */
7748 decl_specs
->attributes
7749 = chainon (decl_specs
->attributes
,
7750 cp_parser_attributes_opt (parser
));
7753 /* Assume we will find a decl-specifier keyword. */
7754 found_decl_spec
= true;
7755 /* If the next token is an appropriate keyword, we can simply
7756 add it to the list. */
7757 switch (token
->keyword
)
7762 if (!at_class_scope_p ())
7764 error ("%<friend%> used outside of class");
7765 cp_lexer_purge_token (parser
->lexer
);
7769 ++decl_specs
->specs
[(int) ds_friend
];
7770 /* Consume the token. */
7771 cp_lexer_consume_token (parser
->lexer
);
7775 /* function-specifier:
7782 cp_parser_function_specifier_opt (parser
, decl_specs
);
7788 ++decl_specs
->specs
[(int) ds_typedef
];
7789 /* Consume the token. */
7790 cp_lexer_consume_token (parser
->lexer
);
7791 /* A constructor declarator cannot appear in a typedef. */
7792 constructor_possible_p
= false;
7793 /* The "typedef" keyword can only occur in a declaration; we
7794 may as well commit at this point. */
7795 cp_parser_commit_to_tentative_parse (parser
);
7797 if (decl_specs
->storage_class
!= sc_none
)
7798 decl_specs
->conflicting_specifiers_p
= true;
7801 /* storage-class-specifier:
7815 /* Consume the token. */
7816 cp_lexer_consume_token (parser
->lexer
);
7817 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
);
7820 /* Consume the token. */
7821 cp_lexer_consume_token (parser
->lexer
);
7822 ++decl_specs
->specs
[(int) ds_thread
];
7826 /* We did not yet find a decl-specifier yet. */
7827 found_decl_spec
= false;
7831 /* Constructors are a special case. The `S' in `S()' is not a
7832 decl-specifier; it is the beginning of the declarator. */
7835 && constructor_possible_p
7836 && (cp_parser_constructor_declarator_p
7837 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
7839 /* If we don't have a DECL_SPEC yet, then we must be looking at
7840 a type-specifier. */
7841 if (!found_decl_spec
&& !constructor_p
)
7843 int decl_spec_declares_class_or_enum
;
7844 bool is_cv_qualifier
;
7848 = cp_parser_type_specifier (parser
, flags
,
7850 /*is_declaration=*/true,
7851 &decl_spec_declares_class_or_enum
,
7854 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
7856 /* If this type-specifier referenced a user-defined type
7857 (a typedef, class-name, etc.), then we can't allow any
7858 more such type-specifiers henceforth.
7862 The longest sequence of decl-specifiers that could
7863 possibly be a type name is taken as the
7864 decl-specifier-seq of a declaration. The sequence shall
7865 be self-consistent as described below.
7869 As a general rule, at most one type-specifier is allowed
7870 in the complete decl-specifier-seq of a declaration. The
7871 only exceptions are the following:
7873 -- const or volatile can be combined with any other
7876 -- signed or unsigned can be combined with char, long,
7884 void g (const int Pc);
7886 Here, Pc is *not* part of the decl-specifier seq; it's
7887 the declarator. Therefore, once we see a type-specifier
7888 (other than a cv-qualifier), we forbid any additional
7889 user-defined types. We *do* still allow things like `int
7890 int' to be considered a decl-specifier-seq, and issue the
7891 error message later. */
7892 if (type_spec
&& !is_cv_qualifier
)
7893 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
7894 /* A constructor declarator cannot follow a type-specifier. */
7897 constructor_possible_p
= false;
7898 found_decl_spec
= true;
7902 /* If we still do not have a DECL_SPEC, then there are no more
7904 if (!found_decl_spec
)
7907 decl_specs
->any_specifiers_p
= true;
7908 /* After we see one decl-specifier, further decl-specifiers are
7910 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
7913 cp_parser_check_decl_spec (decl_specs
);
7915 /* Don't allow a friend specifier with a class definition. */
7916 if (decl_specs
->specs
[(int) ds_friend
] != 0
7917 && (*declares_class_or_enum
& 2))
7918 error ("class definition may not be declared a friend");
7921 /* Parse an (optional) storage-class-specifier.
7923 storage-class-specifier:
7932 storage-class-specifier:
7935 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7938 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
7940 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7948 /* Consume the token. */
7949 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
7956 /* Parse an (optional) function-specifier.
7963 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7964 Updates DECL_SPECS, if it is non-NULL. */
7967 cp_parser_function_specifier_opt (cp_parser
* parser
,
7968 cp_decl_specifier_seq
*decl_specs
)
7970 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7974 ++decl_specs
->specs
[(int) ds_inline
];
7978 /* 14.5.2.3 [temp.mem]
7980 A member function template shall not be virtual. */
7981 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7982 error ("templates may not be %<virtual%>");
7983 else if (decl_specs
)
7984 ++decl_specs
->specs
[(int) ds_virtual
];
7989 ++decl_specs
->specs
[(int) ds_explicit
];
7996 /* Consume the token. */
7997 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
8000 /* Parse a linkage-specification.
8002 linkage-specification:
8003 extern string-literal { declaration-seq [opt] }
8004 extern string-literal declaration */
8007 cp_parser_linkage_specification (cp_parser
* parser
)
8011 /* Look for the `extern' keyword. */
8012 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
8014 /* Look for the string-literal. */
8015 linkage
= cp_parser_string_literal (parser
, false, false);
8017 /* Transform the literal into an identifier. If the literal is a
8018 wide-character string, or contains embedded NULs, then we can't
8019 handle it as the user wants. */
8020 if (strlen (TREE_STRING_POINTER (linkage
))
8021 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
8023 cp_parser_error (parser
, "invalid linkage-specification");
8024 /* Assume C++ linkage. */
8025 linkage
= lang_name_cplusplus
;
8028 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
8030 /* We're now using the new linkage. */
8031 push_lang_context (linkage
);
8033 /* If the next token is a `{', then we're using the first
8035 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8037 /* Consume the `{' token. */
8038 cp_lexer_consume_token (parser
->lexer
);
8039 /* Parse the declarations. */
8040 cp_parser_declaration_seq_opt (parser
);
8041 /* Look for the closing `}'. */
8042 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
8044 /* Otherwise, there's just one declaration. */
8047 bool saved_in_unbraced_linkage_specification_p
;
8049 saved_in_unbraced_linkage_specification_p
8050 = parser
->in_unbraced_linkage_specification_p
;
8051 parser
->in_unbraced_linkage_specification_p
= true;
8052 cp_parser_declaration (parser
);
8053 parser
->in_unbraced_linkage_specification_p
8054 = saved_in_unbraced_linkage_specification_p
;
8057 /* We're done with the linkage-specification. */
8058 pop_lang_context ();
8061 /* Parse a static_assert-declaration.
8063 static_assert-declaration:
8064 static_assert ( constant-expression , string-literal ) ;
8066 If MEMBER_P, this static_assert is a class member. */
8069 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
8074 location_t saved_loc
;
8076 /* Peek at the `static_assert' token so we can keep track of exactly
8077 where the static assertion started. */
8078 token
= cp_lexer_peek_token (parser
->lexer
);
8079 saved_loc
= token
->location
;
8081 /* Look for the `static_assert' keyword. */
8082 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
8086 /* We know we are in a static assertion; commit to any tentative
8088 if (cp_parser_parsing_tentatively (parser
))
8089 cp_parser_commit_to_tentative_parse (parser
);
8091 /* Parse the `(' starting the static assertion condition. */
8092 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
8094 /* Parse the constant-expression. */
8096 cp_parser_constant_expression (parser
,
8097 /*allow_non_constant_p=*/false,
8098 /*non_constant_p=*/NULL
);
8100 /* Parse the separating `,'. */
8101 cp_parser_require (parser
, CPP_COMMA
, "`,'");
8103 /* Parse the string-literal message. */
8104 message
= cp_parser_string_literal (parser
,
8105 /*translate=*/false,
8108 /* A `)' completes the static assertion. */
8109 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
8110 cp_parser_skip_to_closing_parenthesis (parser
,
8111 /*recovering=*/true,
8113 /*consume_paren=*/true);
8115 /* A semicolon terminates the declaration. */
8116 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
8118 /* Complete the static assertion, which may mean either processing
8119 the static assert now or saving it for template instantiation. */
8120 finish_static_assert (condition
, message
, saved_loc
, member_p
);
8123 /* Special member functions [gram.special] */
8125 /* Parse a conversion-function-id.
8127 conversion-function-id:
8128 operator conversion-type-id
8130 Returns an IDENTIFIER_NODE representing the operator. */
8133 cp_parser_conversion_function_id (cp_parser
* parser
)
8137 tree saved_qualifying_scope
;
8138 tree saved_object_scope
;
8139 tree pushed_scope
= NULL_TREE
;
8141 /* Look for the `operator' token. */
8142 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
8143 return error_mark_node
;
8144 /* When we parse the conversion-type-id, the current scope will be
8145 reset. However, we need that information in able to look up the
8146 conversion function later, so we save it here. */
8147 saved_scope
= parser
->scope
;
8148 saved_qualifying_scope
= parser
->qualifying_scope
;
8149 saved_object_scope
= parser
->object_scope
;
8150 /* We must enter the scope of the class so that the names of
8151 entities declared within the class are available in the
8152 conversion-type-id. For example, consider:
8159 S::operator I() { ... }
8161 In order to see that `I' is a type-name in the definition, we
8162 must be in the scope of `S'. */
8164 pushed_scope
= push_scope (saved_scope
);
8165 /* Parse the conversion-type-id. */
8166 type
= cp_parser_conversion_type_id (parser
);
8167 /* Leave the scope of the class, if any. */
8169 pop_scope (pushed_scope
);
8170 /* Restore the saved scope. */
8171 parser
->scope
= saved_scope
;
8172 parser
->qualifying_scope
= saved_qualifying_scope
;
8173 parser
->object_scope
= saved_object_scope
;
8174 /* If the TYPE is invalid, indicate failure. */
8175 if (type
== error_mark_node
)
8176 return error_mark_node
;
8177 return mangle_conv_op_name_for_type (type
);
8180 /* Parse a conversion-type-id:
8183 type-specifier-seq conversion-declarator [opt]
8185 Returns the TYPE specified. */
8188 cp_parser_conversion_type_id (cp_parser
* parser
)
8191 cp_decl_specifier_seq type_specifiers
;
8192 cp_declarator
*declarator
;
8193 tree type_specified
;
8195 /* Parse the attributes. */
8196 attributes
= cp_parser_attributes_opt (parser
);
8197 /* Parse the type-specifiers. */
8198 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
8200 /* If that didn't work, stop. */
8201 if (type_specifiers
.type
== error_mark_node
)
8202 return error_mark_node
;
8203 /* Parse the conversion-declarator. */
8204 declarator
= cp_parser_conversion_declarator_opt (parser
);
8206 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
8207 /*initialized=*/0, &attributes
);
8209 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
8210 return type_specified
;
8213 /* Parse an (optional) conversion-declarator.
8215 conversion-declarator:
8216 ptr-operator conversion-declarator [opt]
8220 static cp_declarator
*
8221 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
8223 enum tree_code code
;
8225 cp_cv_quals cv_quals
;
8227 /* We don't know if there's a ptr-operator next, or not. */
8228 cp_parser_parse_tentatively (parser
);
8229 /* Try the ptr-operator. */
8230 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
8231 /* If it worked, look for more conversion-declarators. */
8232 if (cp_parser_parse_definitely (parser
))
8234 cp_declarator
*declarator
;
8236 /* Parse another optional declarator. */
8237 declarator
= cp_parser_conversion_declarator_opt (parser
);
8239 /* Create the representation of the declarator. */
8241 declarator
= make_ptrmem_declarator (cv_quals
, class_type
,
8243 else if (code
== INDIRECT_REF
)
8244 declarator
= make_pointer_declarator (cv_quals
, declarator
);
8246 declarator
= make_reference_declarator (cv_quals
, declarator
);
8254 /* Parse an (optional) ctor-initializer.
8257 : mem-initializer-list
8259 Returns TRUE iff the ctor-initializer was actually present. */
8262 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
8264 /* If the next token is not a `:', then there is no
8265 ctor-initializer. */
8266 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
8268 /* Do default initialization of any bases and members. */
8269 if (DECL_CONSTRUCTOR_P (current_function_decl
))
8270 finish_mem_initializers (NULL_TREE
);
8275 /* Consume the `:' token. */
8276 cp_lexer_consume_token (parser
->lexer
);
8277 /* And the mem-initializer-list. */
8278 cp_parser_mem_initializer_list (parser
);
8283 /* Parse a mem-initializer-list.
8285 mem-initializer-list:
8286 mem-initializer ... [opt]
8287 mem-initializer ... [opt] , mem-initializer-list */
8290 cp_parser_mem_initializer_list (cp_parser
* parser
)
8292 tree mem_initializer_list
= NULL_TREE
;
8294 /* Let the semantic analysis code know that we are starting the
8295 mem-initializer-list. */
8296 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
8297 error ("only constructors take base initializers");
8299 /* Loop through the list. */
8302 tree mem_initializer
;
8304 /* Parse the mem-initializer. */
8305 mem_initializer
= cp_parser_mem_initializer (parser
);
8306 /* If the next token is a `...', we're expanding member initializers. */
8307 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8309 /* Consume the `...'. */
8310 cp_lexer_consume_token (parser
->lexer
);
8312 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8313 can be expanded but members cannot. */
8314 if (mem_initializer
!= error_mark_node
8315 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
8317 error ("cannot expand initializer for member %<%D%>",
8318 TREE_PURPOSE (mem_initializer
));
8319 mem_initializer
= error_mark_node
;
8322 /* Construct the pack expansion type. */
8323 if (mem_initializer
!= error_mark_node
)
8324 mem_initializer
= make_pack_expansion (mem_initializer
);
8326 /* Add it to the list, unless it was erroneous. */
8327 if (mem_initializer
!= error_mark_node
)
8329 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
8330 mem_initializer_list
= mem_initializer
;
8332 /* If the next token is not a `,', we're done. */
8333 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8335 /* Consume the `,' token. */
8336 cp_lexer_consume_token (parser
->lexer
);
8339 /* Perform semantic analysis. */
8340 if (DECL_CONSTRUCTOR_P (current_function_decl
))
8341 finish_mem_initializers (mem_initializer_list
);
8344 /* Parse a mem-initializer.
8347 mem-initializer-id ( expression-list [opt] )
8352 ( expression-list [opt] )
8354 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8355 class) or FIELD_DECL (for a non-static data member) to initialize;
8356 the TREE_VALUE is the expression-list. An empty initialization
8357 list is represented by void_list_node. */
8360 cp_parser_mem_initializer (cp_parser
* parser
)
8362 tree mem_initializer_id
;
8363 tree expression_list
;
8366 /* Find out what is being initialized. */
8367 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8369 pedwarn ("anachronistic old-style base class initializer");
8370 mem_initializer_id
= NULL_TREE
;
8373 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
8374 member
= expand_member_init (mem_initializer_id
);
8375 if (member
&& !DECL_P (member
))
8376 in_base_initializer
= 1;
8379 = cp_parser_parenthesized_expression_list (parser
, false,
8381 /*allow_expansion_p=*/true,
8382 /*non_constant_p=*/NULL
);
8383 if (expression_list
== error_mark_node
)
8384 return error_mark_node
;
8385 if (!expression_list
)
8386 expression_list
= void_type_node
;
8388 in_base_initializer
= 0;
8390 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
8393 /* Parse a mem-initializer-id.
8396 :: [opt] nested-name-specifier [opt] class-name
8399 Returns a TYPE indicating the class to be initializer for the first
8400 production. Returns an IDENTIFIER_NODE indicating the data member
8401 to be initialized for the second production. */
8404 cp_parser_mem_initializer_id (cp_parser
* parser
)
8406 bool global_scope_p
;
8407 bool nested_name_specifier_p
;
8408 bool template_p
= false;
8411 /* `typename' is not allowed in this context ([temp.res]). */
8412 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
8414 error ("keyword %<typename%> not allowed in this context (a qualified "
8415 "member initializer is implicitly a type)");
8416 cp_lexer_consume_token (parser
->lexer
);
8418 /* Look for the optional `::' operator. */
8420 = (cp_parser_global_scope_opt (parser
,
8421 /*current_scope_valid_p=*/false)
8423 /* Look for the optional nested-name-specifier. The simplest way to
8428 The keyword `typename' is not permitted in a base-specifier or
8429 mem-initializer; in these contexts a qualified name that
8430 depends on a template-parameter is implicitly assumed to be a
8433 is to assume that we have seen the `typename' keyword at this
8435 nested_name_specifier_p
8436 = (cp_parser_nested_name_specifier_opt (parser
,
8437 /*typename_keyword_p=*/true,
8438 /*check_dependency_p=*/true,
8440 /*is_declaration=*/true)
8442 if (nested_name_specifier_p
)
8443 template_p
= cp_parser_optional_template_keyword (parser
);
8444 /* If there is a `::' operator or a nested-name-specifier, then we
8445 are definitely looking for a class-name. */
8446 if (global_scope_p
|| nested_name_specifier_p
)
8447 return cp_parser_class_name (parser
,
8448 /*typename_keyword_p=*/true,
8449 /*template_keyword_p=*/template_p
,
8451 /*check_dependency_p=*/true,
8452 /*class_head_p=*/false,
8453 /*is_declaration=*/true);
8454 /* Otherwise, we could also be looking for an ordinary identifier. */
8455 cp_parser_parse_tentatively (parser
);
8456 /* Try a class-name. */
8457 id
= cp_parser_class_name (parser
,
8458 /*typename_keyword_p=*/true,
8459 /*template_keyword_p=*/false,
8461 /*check_dependency_p=*/true,
8462 /*class_head_p=*/false,
8463 /*is_declaration=*/true);
8464 /* If we found one, we're done. */
8465 if (cp_parser_parse_definitely (parser
))
8467 /* Otherwise, look for an ordinary identifier. */
8468 return cp_parser_identifier (parser
);
8471 /* Overloading [gram.over] */
8473 /* Parse an operator-function-id.
8475 operator-function-id:
8478 Returns an IDENTIFIER_NODE for the operator which is a
8479 human-readable spelling of the identifier, e.g., `operator +'. */
8482 cp_parser_operator_function_id (cp_parser
* parser
)
8484 /* Look for the `operator' keyword. */
8485 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
8486 return error_mark_node
;
8487 /* And then the name of the operator itself. */
8488 return cp_parser_operator (parser
);
8491 /* Parse an operator.
8494 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8495 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8496 || ++ -- , ->* -> () []
8503 Returns an IDENTIFIER_NODE for the operator which is a
8504 human-readable spelling of the identifier, e.g., `operator +'. */
8507 cp_parser_operator (cp_parser
* parser
)
8509 tree id
= NULL_TREE
;
8512 /* Peek at the next token. */
8513 token
= cp_lexer_peek_token (parser
->lexer
);
8514 /* Figure out which operator we have. */
8515 switch (token
->type
)
8521 /* The keyword should be either `new' or `delete'. */
8522 if (token
->keyword
== RID_NEW
)
8524 else if (token
->keyword
== RID_DELETE
)
8529 /* Consume the `new' or `delete' token. */
8530 cp_lexer_consume_token (parser
->lexer
);
8532 /* Peek at the next token. */
8533 token
= cp_lexer_peek_token (parser
->lexer
);
8534 /* If it's a `[' token then this is the array variant of the
8536 if (token
->type
== CPP_OPEN_SQUARE
)
8538 /* Consume the `[' token. */
8539 cp_lexer_consume_token (parser
->lexer
);
8540 /* Look for the `]' token. */
8541 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8542 id
= ansi_opname (op
== NEW_EXPR
8543 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
8545 /* Otherwise, we have the non-array variant. */
8547 id
= ansi_opname (op
);
8553 id
= ansi_opname (PLUS_EXPR
);
8557 id
= ansi_opname (MINUS_EXPR
);
8561 id
= ansi_opname (MULT_EXPR
);
8565 id
= ansi_opname (TRUNC_DIV_EXPR
);
8569 id
= ansi_opname (TRUNC_MOD_EXPR
);
8573 id
= ansi_opname (BIT_XOR_EXPR
);
8577 id
= ansi_opname (BIT_AND_EXPR
);
8581 id
= ansi_opname (BIT_IOR_EXPR
);
8585 id
= ansi_opname (BIT_NOT_EXPR
);
8589 id
= ansi_opname (TRUTH_NOT_EXPR
);
8593 id
= ansi_assopname (NOP_EXPR
);
8597 id
= ansi_opname (LT_EXPR
);
8601 id
= ansi_opname (GT_EXPR
);
8605 id
= ansi_assopname (PLUS_EXPR
);
8609 id
= ansi_assopname (MINUS_EXPR
);
8613 id
= ansi_assopname (MULT_EXPR
);
8617 id
= ansi_assopname (TRUNC_DIV_EXPR
);
8621 id
= ansi_assopname (TRUNC_MOD_EXPR
);
8625 id
= ansi_assopname (BIT_XOR_EXPR
);
8629 id
= ansi_assopname (BIT_AND_EXPR
);
8633 id
= ansi_assopname (BIT_IOR_EXPR
);
8637 id
= ansi_opname (LSHIFT_EXPR
);
8641 id
= ansi_opname (RSHIFT_EXPR
);
8645 id
= ansi_assopname (LSHIFT_EXPR
);
8649 id
= ansi_assopname (RSHIFT_EXPR
);
8653 id
= ansi_opname (EQ_EXPR
);
8657 id
= ansi_opname (NE_EXPR
);
8661 id
= ansi_opname (LE_EXPR
);
8664 case CPP_GREATER_EQ
:
8665 id
= ansi_opname (GE_EXPR
);
8669 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
8673 id
= ansi_opname (TRUTH_ORIF_EXPR
);
8677 id
= ansi_opname (POSTINCREMENT_EXPR
);
8680 case CPP_MINUS_MINUS
:
8681 id
= ansi_opname (PREDECREMENT_EXPR
);
8685 id
= ansi_opname (COMPOUND_EXPR
);
8688 case CPP_DEREF_STAR
:
8689 id
= ansi_opname (MEMBER_REF
);
8693 id
= ansi_opname (COMPONENT_REF
);
8696 case CPP_OPEN_PAREN
:
8697 /* Consume the `('. */
8698 cp_lexer_consume_token (parser
->lexer
);
8699 /* Look for the matching `)'. */
8700 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
8701 return ansi_opname (CALL_EXPR
);
8703 case CPP_OPEN_SQUARE
:
8704 /* Consume the `['. */
8705 cp_lexer_consume_token (parser
->lexer
);
8706 /* Look for the matching `]'. */
8707 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8708 return ansi_opname (ARRAY_REF
);
8711 /* Anything else is an error. */
8715 /* If we have selected an identifier, we need to consume the
8718 cp_lexer_consume_token (parser
->lexer
);
8719 /* Otherwise, no valid operator name was present. */
8722 cp_parser_error (parser
, "expected operator");
8723 id
= error_mark_node
;
8729 /* Parse a template-declaration.
8731 template-declaration:
8732 export [opt] template < template-parameter-list > declaration
8734 If MEMBER_P is TRUE, this template-declaration occurs within a
8737 The grammar rule given by the standard isn't correct. What
8740 template-declaration:
8741 export [opt] template-parameter-list-seq
8742 decl-specifier-seq [opt] init-declarator [opt] ;
8743 export [opt] template-parameter-list-seq
8746 template-parameter-list-seq:
8747 template-parameter-list-seq [opt]
8748 template < template-parameter-list > */
8751 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
8753 /* Check for `export'. */
8754 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
8756 /* Consume the `export' token. */
8757 cp_lexer_consume_token (parser
->lexer
);
8758 /* Warn that we do not support `export'. */
8759 warning (0, "keyword %<export%> not implemented, and will be ignored");
8762 cp_parser_template_declaration_after_export (parser
, member_p
);
8765 /* Parse a template-parameter-list.
8767 template-parameter-list:
8769 template-parameter-list , template-parameter
8771 Returns a TREE_LIST. Each node represents a template parameter.
8772 The nodes are connected via their TREE_CHAINs. */
8775 cp_parser_template_parameter_list (cp_parser
* parser
)
8777 tree parameter_list
= NULL_TREE
;
8779 begin_template_parm_list ();
8785 bool is_parameter_pack
;
8787 /* Parse the template-parameter. */
8788 parameter
= cp_parser_template_parameter (parser
,
8790 &is_parameter_pack
);
8791 /* Add it to the list. */
8792 if (parameter
!= error_mark_node
)
8793 parameter_list
= process_template_parm (parameter_list
,
8799 tree err_parm
= build_tree_list (parameter
, parameter
);
8800 TREE_VALUE (err_parm
) = error_mark_node
;
8801 parameter_list
= chainon (parameter_list
, err_parm
);
8804 /* Peek at the next token. */
8805 token
= cp_lexer_peek_token (parser
->lexer
);
8806 /* If it's not a `,', we're done. */
8807 if (token
->type
!= CPP_COMMA
)
8809 /* Otherwise, consume the `,' token. */
8810 cp_lexer_consume_token (parser
->lexer
);
8813 return end_template_parm_list (parameter_list
);
8816 /* Parse a template-parameter.
8820 parameter-declaration
8822 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8823 the parameter. The TREE_PURPOSE is the default value, if any.
8824 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8825 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
8826 set to true iff this parameter is a parameter pack. */
8829 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
8830 bool *is_parameter_pack
)
8833 cp_parameter_declarator
*parameter_declarator
;
8836 /* Assume it is a type parameter or a template parameter. */
8837 *is_non_type
= false;
8838 /* Assume it not a parameter pack. */
8839 *is_parameter_pack
= false;
8840 /* Peek at the next token. */
8841 token
= cp_lexer_peek_token (parser
->lexer
);
8842 /* If it is `class' or `template', we have a type-parameter. */
8843 if (token
->keyword
== RID_TEMPLATE
)
8844 return cp_parser_type_parameter (parser
, is_parameter_pack
);
8845 /* If it is `class' or `typename' we do not know yet whether it is a
8846 type parameter or a non-type parameter. Consider:
8848 template <typename T, typename T::X X> ...
8852 template <class C, class D*> ...
8854 Here, the first parameter is a type parameter, and the second is
8855 a non-type parameter. We can tell by looking at the token after
8856 the identifier -- if it is a `,', `=', or `>' then we have a type
8858 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
8860 /* Peek at the token after `class' or `typename'. */
8861 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8862 /* If it's an ellipsis, we have a template type parameter
8864 if (token
->type
== CPP_ELLIPSIS
)
8865 return cp_parser_type_parameter (parser
, is_parameter_pack
);
8866 /* If it's an identifier, skip it. */
8867 if (token
->type
== CPP_NAME
)
8868 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
8869 /* Now, see if the token looks like the end of a template
8871 if (token
->type
== CPP_COMMA
8872 || token
->type
== CPP_EQ
8873 || token
->type
== CPP_GREATER
)
8874 return cp_parser_type_parameter (parser
, is_parameter_pack
);
8877 /* Otherwise, it is a non-type parameter.
8881 When parsing a default template-argument for a non-type
8882 template-parameter, the first non-nested `>' is taken as the end
8883 of the template parameter-list rather than a greater-than
8885 *is_non_type
= true;
8886 parameter_declarator
8887 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
8888 /*parenthesized_p=*/NULL
);
8890 /* If the parameter declaration is marked as a parameter pack, set
8891 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
8892 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
8894 if (parameter_declarator
8895 && parameter_declarator
->declarator
8896 && parameter_declarator
->declarator
->parameter_pack_p
)
8898 *is_parameter_pack
= true;
8899 parameter_declarator
->declarator
->parameter_pack_p
= false;
8902 /* If the next token is an ellipsis, and we don't already have it
8903 marked as a parameter pack, then we have a parameter pack (that
8904 has no declarator); */
8905 if (!*is_parameter_pack
8906 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8909 /* Consume the `...'. */
8910 cp_lexer_consume_token (parser
->lexer
);
8911 maybe_warn_variadic_templates ();
8913 *is_parameter_pack
= true;
8916 parm
= grokdeclarator (parameter_declarator
->declarator
,
8917 ¶meter_declarator
->decl_specifiers
,
8918 PARM
, /*initialized=*/0,
8920 if (parm
== error_mark_node
)
8921 return error_mark_node
;
8923 return build_tree_list (parameter_declarator
->default_argument
, parm
);
8926 /* Parse a type-parameter.
8929 class identifier [opt]
8930 class identifier [opt] = type-id
8931 typename identifier [opt]
8932 typename identifier [opt] = type-id
8933 template < template-parameter-list > class identifier [opt]
8934 template < template-parameter-list > class identifier [opt]
8937 GNU Extension (variadic templates):
8940 class ... identifier [opt]
8941 typename ... identifier [opt]
8943 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8944 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8945 the declaration of the parameter.
8947 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
8950 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
8955 /* Look for a keyword to tell us what kind of parameter this is. */
8956 token
= cp_parser_require (parser
, CPP_KEYWORD
,
8957 "`class', `typename', or `template'");
8959 return error_mark_node
;
8961 switch (token
->keyword
)
8967 tree default_argument
;
8969 /* If the next token is an ellipsis, we have a template
8971 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8973 /* Consume the `...' token. */
8974 cp_lexer_consume_token (parser
->lexer
);
8975 maybe_warn_variadic_templates ();
8977 *is_parameter_pack
= true;
8980 /* If the next token is an identifier, then it names the
8982 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8983 identifier
= cp_parser_identifier (parser
);
8985 identifier
= NULL_TREE
;
8987 /* Create the parameter. */
8988 parameter
= finish_template_type_parm (class_type_node
, identifier
);
8990 /* If the next token is an `=', we have a default argument. */
8991 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8993 /* Consume the `=' token. */
8994 cp_lexer_consume_token (parser
->lexer
);
8995 /* Parse the default-argument. */
8996 push_deferring_access_checks (dk_no_deferred
);
8997 default_argument
= cp_parser_type_id (parser
);
8999 /* Template parameter packs cannot have default
9001 if (*is_parameter_pack
)
9004 error ("template parameter pack %qD cannot have a default argument",
9007 error ("template parameter packs cannot have default arguments");
9008 default_argument
= NULL_TREE
;
9010 pop_deferring_access_checks ();
9013 default_argument
= NULL_TREE
;
9015 /* Create the combined representation of the parameter and the
9016 default argument. */
9017 parameter
= build_tree_list (default_argument
, parameter
);
9023 tree parameter_list
;
9025 tree default_argument
;
9027 /* Look for the `<'. */
9028 cp_parser_require (parser
, CPP_LESS
, "`<'");
9029 /* Parse the template-parameter-list. */
9030 parameter_list
= cp_parser_template_parameter_list (parser
);
9031 /* Look for the `>'. */
9032 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9033 /* Look for the `class' keyword. */
9034 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
9035 /* If the next token is an ellipsis, we have a template
9037 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9039 /* Consume the `...' token. */
9040 cp_lexer_consume_token (parser
->lexer
);
9041 maybe_warn_variadic_templates ();
9043 *is_parameter_pack
= true;
9045 /* If the next token is an `=', then there is a
9046 default-argument. If the next token is a `>', we are at
9047 the end of the parameter-list. If the next token is a `,',
9048 then we are at the end of this parameter. */
9049 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
9050 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
9051 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
9053 identifier
= cp_parser_identifier (parser
);
9054 /* Treat invalid names as if the parameter were nameless. */
9055 if (identifier
== error_mark_node
)
9056 identifier
= NULL_TREE
;
9059 identifier
= NULL_TREE
;
9061 /* Create the template parameter. */
9062 parameter
= finish_template_template_parm (class_type_node
,
9065 /* If the next token is an `=', then there is a
9066 default-argument. */
9067 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9071 /* Consume the `='. */
9072 cp_lexer_consume_token (parser
->lexer
);
9073 /* Parse the id-expression. */
9074 push_deferring_access_checks (dk_no_deferred
);
9076 = cp_parser_id_expression (parser
,
9077 /*template_keyword_p=*/false,
9078 /*check_dependency_p=*/true,
9079 /*template_p=*/&is_template
,
9080 /*declarator_p=*/false,
9081 /*optional_p=*/false);
9082 if (TREE_CODE (default_argument
) == TYPE_DECL
)
9083 /* If the id-expression was a template-id that refers to
9084 a template-class, we already have the declaration here,
9085 so no further lookup is needed. */
9088 /* Look up the name. */
9090 = cp_parser_lookup_name (parser
, default_argument
,
9092 /*is_template=*/is_template
,
9093 /*is_namespace=*/false,
9094 /*check_dependency=*/true,
9095 /*ambiguous_decls=*/NULL
);
9096 /* See if the default argument is valid. */
9098 = check_template_template_default_arg (default_argument
);
9100 /* Template parameter packs cannot have default
9102 if (*is_parameter_pack
)
9105 error ("template parameter pack %qD cannot have a default argument",
9108 error ("template parameter packs cannot have default arguments");
9109 default_argument
= NULL_TREE
;
9111 pop_deferring_access_checks ();
9114 default_argument
= NULL_TREE
;
9116 /* Create the combined representation of the parameter and the
9117 default argument. */
9118 parameter
= build_tree_list (default_argument
, parameter
);
9130 /* Parse a template-id.
9133 template-name < template-argument-list [opt] >
9135 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9136 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9137 returned. Otherwise, if the template-name names a function, or set
9138 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9139 names a class, returns a TYPE_DECL for the specialization.
9141 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9142 uninstantiated templates. */
9145 cp_parser_template_id (cp_parser
*parser
,
9146 bool template_keyword_p
,
9147 bool check_dependency_p
,
9148 bool is_declaration
)
9154 cp_token_position start_of_id
= 0;
9155 deferred_access_check
*chk
;
9156 VEC (deferred_access_check
,gc
) *access_check
;
9157 cp_token
*next_token
, *next_token_2
;
9160 /* If the next token corresponds to a template-id, there is no need
9162 next_token
= cp_lexer_peek_token (parser
->lexer
);
9163 if (next_token
->type
== CPP_TEMPLATE_ID
)
9165 struct tree_check
*check_value
;
9167 /* Get the stored value. */
9168 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
9169 /* Perform any access checks that were deferred. */
9170 access_check
= check_value
->checks
;
9174 VEC_iterate (deferred_access_check
, access_check
, i
, chk
) ;
9177 perform_or_defer_access_check (chk
->binfo
,
9182 /* Return the stored value. */
9183 return check_value
->value
;
9186 /* Avoid performing name lookup if there is no possibility of
9187 finding a template-id. */
9188 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
9189 || (next_token
->type
== CPP_NAME
9190 && !cp_parser_nth_token_starts_template_argument_list_p
9193 cp_parser_error (parser
, "expected template-id");
9194 return error_mark_node
;
9197 /* Remember where the template-id starts. */
9198 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
9199 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
9201 push_deferring_access_checks (dk_deferred
);
9203 /* Parse the template-name. */
9204 is_identifier
= false;
9205 template = cp_parser_template_name (parser
, template_keyword_p
,
9209 if (template == error_mark_node
|| is_identifier
)
9211 pop_deferring_access_checks ();
9215 /* If we find the sequence `[:' after a template-name, it's probably
9216 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9217 parse correctly the argument list. */
9218 next_token
= cp_lexer_peek_token (parser
->lexer
);
9219 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9220 if (next_token
->type
== CPP_OPEN_SQUARE
9221 && next_token
->flags
& DIGRAPH
9222 && next_token_2
->type
== CPP_COLON
9223 && !(next_token_2
->flags
& PREV_WHITE
))
9225 cp_parser_parse_tentatively (parser
);
9226 /* Change `:' into `::'. */
9227 next_token_2
->type
= CPP_SCOPE
;
9228 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9230 cp_lexer_consume_token (parser
->lexer
);
9231 /* Parse the arguments. */
9232 arguments
= cp_parser_enclosed_template_argument_list (parser
);
9233 if (!cp_parser_parse_definitely (parser
))
9235 /* If we couldn't parse an argument list, then we revert our changes
9236 and return simply an error. Maybe this is not a template-id
9238 next_token_2
->type
= CPP_COLON
;
9239 cp_parser_error (parser
, "expected %<<%>");
9240 pop_deferring_access_checks ();
9241 return error_mark_node
;
9243 /* Otherwise, emit an error about the invalid digraph, but continue
9244 parsing because we got our argument list. */
9245 pedwarn ("%<<::%> cannot begin a template-argument list");
9246 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9247 "between %<<%> and %<::%>");
9248 if (!flag_permissive
)
9253 inform ("(if you use -fpermissive G++ will accept your code)");
9260 /* Look for the `<' that starts the template-argument-list. */
9261 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
9263 pop_deferring_access_checks ();
9264 return error_mark_node
;
9266 /* Parse the arguments. */
9267 arguments
= cp_parser_enclosed_template_argument_list (parser
);
9270 /* Build a representation of the specialization. */
9271 if (TREE_CODE (template) == IDENTIFIER_NODE
)
9272 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
9273 else if (DECL_CLASS_TEMPLATE_P (template)
9274 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9276 bool entering_scope
;
9277 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9278 template (rather than some instantiation thereof) only if
9279 is not nested within some other construct. For example, in
9280 "template <typename T> void f(T) { A<T>::", A<T> is just an
9281 instantiation of A. */
9282 entering_scope
= (template_parm_scope_p ()
9283 && cp_lexer_next_token_is (parser
->lexer
,
9286 = finish_template_type (template, arguments
, entering_scope
);
9290 /* If it's not a class-template or a template-template, it should be
9291 a function-template. */
9292 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9293 || TREE_CODE (template) == OVERLOAD
9294 || BASELINK_P (template)));
9296 template_id
= lookup_template_function (template, arguments
);
9299 /* If parsing tentatively, replace the sequence of tokens that makes
9300 up the template-id with a CPP_TEMPLATE_ID token. That way,
9301 should we re-parse the token stream, we will not have to repeat
9302 the effort required to do the parse, nor will we issue duplicate
9303 error messages about problems during instantiation of the
9307 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
9309 /* Reset the contents of the START_OF_ID token. */
9310 token
->type
= CPP_TEMPLATE_ID
;
9311 /* Retrieve any deferred checks. Do not pop this access checks yet
9312 so the memory will not be reclaimed during token replacing below. */
9313 token
->u
.tree_check_value
= GGC_CNEW (struct tree_check
);
9314 token
->u
.tree_check_value
->value
= template_id
;
9315 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
9316 token
->keyword
= RID_MAX
;
9318 /* Purge all subsequent tokens. */
9319 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
9321 /* ??? Can we actually assume that, if template_id ==
9322 error_mark_node, we will have issued a diagnostic to the
9323 user, as opposed to simply marking the tentative parse as
9325 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
9326 error ("parse error in template argument list");
9329 pop_deferring_access_checks ();
9333 /* Parse a template-name.
9338 The standard should actually say:
9342 operator-function-id
9344 A defect report has been filed about this issue.
9346 A conversion-function-id cannot be a template name because they cannot
9347 be part of a template-id. In fact, looking at this code:
9351 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9352 It is impossible to call a templated conversion-function-id with an
9353 explicit argument list, since the only allowed template parameter is
9354 the type to which it is converting.
9356 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9357 `template' keyword, in a construction like:
9361 In that case `f' is taken to be a template-name, even though there
9362 is no way of knowing for sure.
9364 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9365 name refers to a set of overloaded functions, at least one of which
9366 is a template, or an IDENTIFIER_NODE with the name of the template,
9367 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9368 names are looked up inside uninstantiated templates. */
9371 cp_parser_template_name (cp_parser
* parser
,
9372 bool template_keyword_p
,
9373 bool check_dependency_p
,
9374 bool is_declaration
,
9375 bool *is_identifier
)
9381 /* If the next token is `operator', then we have either an
9382 operator-function-id or a conversion-function-id. */
9383 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
9385 /* We don't know whether we're looking at an
9386 operator-function-id or a conversion-function-id. */
9387 cp_parser_parse_tentatively (parser
);
9388 /* Try an operator-function-id. */
9389 identifier
= cp_parser_operator_function_id (parser
);
9390 /* If that didn't work, try a conversion-function-id. */
9391 if (!cp_parser_parse_definitely (parser
))
9393 cp_parser_error (parser
, "expected template-name");
9394 return error_mark_node
;
9397 /* Look for the identifier. */
9399 identifier
= cp_parser_identifier (parser
);
9401 /* If we didn't find an identifier, we don't have a template-id. */
9402 if (identifier
== error_mark_node
)
9403 return error_mark_node
;
9405 /* If the name immediately followed the `template' keyword, then it
9406 is a template-name. However, if the next token is not `<', then
9407 we do not treat it as a template-name, since it is not being used
9408 as part of a template-id. This enables us to handle constructs
9411 template <typename T> struct S { S(); };
9412 template <typename T> S<T>::S();
9414 correctly. We would treat `S' as a template -- if it were `S<T>'
9415 -- but we do not if there is no `<'. */
9417 if (processing_template_decl
9418 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
9420 /* In a declaration, in a dependent context, we pretend that the
9421 "template" keyword was present in order to improve error
9422 recovery. For example, given:
9424 template <typename T> void f(T::X<int>);
9426 we want to treat "X<int>" as a template-id. */
9428 && !template_keyword_p
9429 && parser
->scope
&& TYPE_P (parser
->scope
)
9430 && check_dependency_p
9431 && dependent_type_p (parser
->scope
)
9432 /* Do not do this for dtors (or ctors), since they never
9433 need the template keyword before their name. */
9434 && !constructor_name_p (identifier
, parser
->scope
))
9436 cp_token_position start
= 0;
9438 /* Explain what went wrong. */
9439 error ("non-template %qD used as template", identifier
);
9440 inform ("use %<%T::template %D%> to indicate that it is a template",
9441 parser
->scope
, identifier
);
9442 /* If parsing tentatively, find the location of the "<" token. */
9443 if (cp_parser_simulate_error (parser
))
9444 start
= cp_lexer_token_position (parser
->lexer
, true);
9445 /* Parse the template arguments so that we can issue error
9446 messages about them. */
9447 cp_lexer_consume_token (parser
->lexer
);
9448 cp_parser_enclosed_template_argument_list (parser
);
9449 /* Skip tokens until we find a good place from which to
9450 continue parsing. */
9451 cp_parser_skip_to_closing_parenthesis (parser
,
9452 /*recovering=*/true,
9454 /*consume_paren=*/false);
9455 /* If parsing tentatively, permanently remove the
9456 template argument list. That will prevent duplicate
9457 error messages from being issued about the missing
9458 "template" keyword. */
9460 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
9462 *is_identifier
= true;
9466 /* If the "template" keyword is present, then there is generally
9467 no point in doing name-lookup, so we just return IDENTIFIER.
9468 But, if the qualifying scope is non-dependent then we can
9469 (and must) do name-lookup normally. */
9470 if (template_keyword_p
9472 || (TYPE_P (parser
->scope
)
9473 && dependent_type_p (parser
->scope
))))
9477 /* Look up the name. */
9478 decl
= cp_parser_lookup_name (parser
, identifier
,
9480 /*is_template=*/false,
9481 /*is_namespace=*/false,
9483 /*ambiguous_decls=*/NULL
);
9484 decl
= maybe_get_template_decl_from_type_decl (decl
);
9486 /* If DECL is a template, then the name was a template-name. */
9487 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9491 tree fn
= NULL_TREE
;
9493 /* The standard does not explicitly indicate whether a name that
9494 names a set of overloaded declarations, some of which are
9495 templates, is a template-name. However, such a name should
9496 be a template-name; otherwise, there is no way to form a
9497 template-id for the overloaded templates. */
9498 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
9499 if (TREE_CODE (fns
) == OVERLOAD
)
9500 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
9501 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
9506 /* The name does not name a template. */
9507 cp_parser_error (parser
, "expected template-name");
9508 return error_mark_node
;
9512 /* If DECL is dependent, and refers to a function, then just return
9513 its name; we will look it up again during template instantiation. */
9514 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
9516 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
9517 if (TYPE_P (scope
) && dependent_type_p (scope
))
9524 /* Parse a template-argument-list.
9526 template-argument-list:
9527 template-argument ... [opt]
9528 template-argument-list , template-argument ... [opt]
9530 Returns a TREE_VEC containing the arguments. */
9533 cp_parser_template_argument_list (cp_parser
* parser
)
9535 tree fixed_args
[10];
9536 unsigned n_args
= 0;
9537 unsigned alloced
= 10;
9538 tree
*arg_ary
= fixed_args
;
9540 bool saved_in_template_argument_list_p
;
9542 bool saved_non_ice_p
;
9544 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
9545 parser
->in_template_argument_list_p
= true;
9546 /* Even if the template-id appears in an integral
9547 constant-expression, the contents of the argument list do
9549 saved_ice_p
= parser
->integral_constant_expression_p
;
9550 parser
->integral_constant_expression_p
= false;
9551 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
9552 parser
->non_integral_constant_expression_p
= false;
9553 /* Parse the arguments. */
9559 /* Consume the comma. */
9560 cp_lexer_consume_token (parser
->lexer
);
9562 /* Parse the template-argument. */
9563 argument
= cp_parser_template_argument (parser
);
9565 /* If the next token is an ellipsis, we're expanding a template
9567 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9569 /* Consume the `...' token. */
9570 cp_lexer_consume_token (parser
->lexer
);
9572 /* Make the argument into a TYPE_PACK_EXPANSION or
9573 EXPR_PACK_EXPANSION. */
9574 argument
= make_pack_expansion (argument
);
9577 if (n_args
== alloced
)
9581 if (arg_ary
== fixed_args
)
9583 arg_ary
= XNEWVEC (tree
, alloced
);
9584 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
9587 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
9589 arg_ary
[n_args
++] = argument
;
9591 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
9593 vec
= make_tree_vec (n_args
);
9596 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
9598 if (arg_ary
!= fixed_args
)
9600 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
9601 parser
->integral_constant_expression_p
= saved_ice_p
;
9602 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
9606 /* Parse a template-argument.
9609 assignment-expression
9613 The representation is that of an assignment-expression, type-id, or
9614 id-expression -- except that the qualified id-expression is
9615 evaluated, so that the value returned is either a DECL or an
9618 Although the standard says "assignment-expression", it forbids
9619 throw-expressions or assignments in the template argument.
9620 Therefore, we use "conditional-expression" instead. */
9623 cp_parser_template_argument (cp_parser
* parser
)
9628 bool maybe_type_id
= false;
9632 /* There's really no way to know what we're looking at, so we just
9633 try each alternative in order.
9637 In a template-argument, an ambiguity between a type-id and an
9638 expression is resolved to a type-id, regardless of the form of
9639 the corresponding template-parameter.
9641 Therefore, we try a type-id first. */
9642 cp_parser_parse_tentatively (parser
);
9643 argument
= cp_parser_type_id (parser
);
9644 /* If there was no error parsing the type-id but the next token is a '>>',
9645 we probably found a typo for '> >'. But there are type-id which are
9646 also valid expressions. For instance:
9648 struct X { int operator >> (int); };
9649 template <int V> struct Foo {};
9652 Here 'X()' is a valid type-id of a function type, but the user just
9653 wanted to write the expression "X() >> 5". Thus, we remember that we
9654 found a valid type-id, but we still try to parse the argument as an
9655 expression to see what happens. */
9656 if (!cp_parser_error_occurred (parser
)
9657 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
9659 maybe_type_id
= true;
9660 cp_parser_abort_tentative_parse (parser
);
9664 /* If the next token isn't a `,' or a `>', then this argument wasn't
9665 really finished. This means that the argument is not a valid
9667 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9668 cp_parser_error (parser
, "expected template-argument");
9669 /* If that worked, we're done. */
9670 if (cp_parser_parse_definitely (parser
))
9673 /* We're still not sure what the argument will be. */
9674 cp_parser_parse_tentatively (parser
);
9675 /* Try a template. */
9676 argument
= cp_parser_id_expression (parser
,
9677 /*template_keyword_p=*/false,
9678 /*check_dependency_p=*/true,
9680 /*declarator_p=*/false,
9681 /*optional_p=*/false);
9682 /* If the next token isn't a `,' or a `>', then this argument wasn't
9684 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9685 cp_parser_error (parser
, "expected template-argument");
9686 if (!cp_parser_error_occurred (parser
))
9688 /* Figure out what is being referred to. If the id-expression
9689 was for a class template specialization, then we will have a
9690 TYPE_DECL at this point. There is no need to do name lookup
9691 at this point in that case. */
9692 if (TREE_CODE (argument
) != TYPE_DECL
)
9693 argument
= cp_parser_lookup_name (parser
, argument
,
9695 /*is_template=*/template_p
,
9696 /*is_namespace=*/false,
9697 /*check_dependency=*/true,
9698 /*ambiguous_decls=*/NULL
);
9699 if (TREE_CODE (argument
) != TEMPLATE_DECL
9700 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
9701 cp_parser_error (parser
, "expected template-name");
9703 if (cp_parser_parse_definitely (parser
))
9705 /* It must be a non-type argument. There permitted cases are given
9706 in [temp.arg.nontype]:
9708 -- an integral constant-expression of integral or enumeration
9711 -- the name of a non-type template-parameter; or
9713 -- the name of an object or function with external linkage...
9715 -- the address of an object or function with external linkage...
9717 -- a pointer to member... */
9718 /* Look for a non-type template parameter. */
9719 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9721 cp_parser_parse_tentatively (parser
);
9722 argument
= cp_parser_primary_expression (parser
,
9725 /*template_arg_p=*/true,
9727 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
9728 || !cp_parser_next_token_ends_template_argument_p (parser
))
9729 cp_parser_simulate_error (parser
);
9730 if (cp_parser_parse_definitely (parser
))
9734 /* If the next token is "&", the argument must be the address of an
9735 object or function with external linkage. */
9736 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
9738 cp_lexer_consume_token (parser
->lexer
);
9739 /* See if we might have an id-expression. */
9740 token
= cp_lexer_peek_token (parser
->lexer
);
9741 if (token
->type
== CPP_NAME
9742 || token
->keyword
== RID_OPERATOR
9743 || token
->type
== CPP_SCOPE
9744 || token
->type
== CPP_TEMPLATE_ID
9745 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
9747 cp_parser_parse_tentatively (parser
);
9748 argument
= cp_parser_primary_expression (parser
,
9751 /*template_arg_p=*/true,
9753 if (cp_parser_error_occurred (parser
)
9754 || !cp_parser_next_token_ends_template_argument_p (parser
))
9755 cp_parser_abort_tentative_parse (parser
);
9758 if (TREE_CODE (argument
) == INDIRECT_REF
)
9760 gcc_assert (REFERENCE_REF_P (argument
));
9761 argument
= TREE_OPERAND (argument
, 0);
9764 if (TREE_CODE (argument
) == VAR_DECL
)
9766 /* A variable without external linkage might still be a
9767 valid constant-expression, so no error is issued here
9768 if the external-linkage check fails. */
9769 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (argument
))
9770 cp_parser_simulate_error (parser
);
9772 else if (is_overloaded_fn (argument
))
9773 /* All overloaded functions are allowed; if the external
9774 linkage test does not pass, an error will be issued
9778 && (TREE_CODE (argument
) == OFFSET_REF
9779 || TREE_CODE (argument
) == SCOPE_REF
))
9780 /* A pointer-to-member. */
9782 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
9785 cp_parser_simulate_error (parser
);
9787 if (cp_parser_parse_definitely (parser
))
9790 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
9795 /* If the argument started with "&", there are no other valid
9796 alternatives at this point. */
9799 cp_parser_error (parser
, "invalid non-type template argument");
9800 return error_mark_node
;
9803 /* If the argument wasn't successfully parsed as a type-id followed
9804 by '>>', the argument can only be a constant expression now.
9805 Otherwise, we try parsing the constant-expression tentatively,
9806 because the argument could really be a type-id. */
9808 cp_parser_parse_tentatively (parser
);
9809 argument
= cp_parser_constant_expression (parser
,
9810 /*allow_non_constant_p=*/false,
9811 /*non_constant_p=*/NULL
);
9812 argument
= fold_non_dependent_expr (argument
);
9815 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9816 cp_parser_error (parser
, "expected template-argument");
9817 if (cp_parser_parse_definitely (parser
))
9819 /* We did our best to parse the argument as a non type-id, but that
9820 was the only alternative that matched (albeit with a '>' after
9821 it). We can assume it's just a typo from the user, and a
9822 diagnostic will then be issued. */
9823 return cp_parser_type_id (parser
);
9826 /* Parse an explicit-instantiation.
9828 explicit-instantiation:
9829 template declaration
9831 Although the standard says `declaration', what it really means is:
9833 explicit-instantiation:
9834 template decl-specifier-seq [opt] declarator [opt] ;
9836 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9837 supposed to be allowed. A defect report has been filed about this
9842 explicit-instantiation:
9843 storage-class-specifier template
9844 decl-specifier-seq [opt] declarator [opt] ;
9845 function-specifier template
9846 decl-specifier-seq [opt] declarator [opt] ; */
9849 cp_parser_explicit_instantiation (cp_parser
* parser
)
9851 int declares_class_or_enum
;
9852 cp_decl_specifier_seq decl_specifiers
;
9853 tree extension_specifier
= NULL_TREE
;
9855 /* Look for an (optional) storage-class-specifier or
9856 function-specifier. */
9857 if (cp_parser_allow_gnu_extensions_p (parser
))
9860 = cp_parser_storage_class_specifier_opt (parser
);
9861 if (!extension_specifier
)
9863 = cp_parser_function_specifier_opt (parser
,
9864 /*decl_specs=*/NULL
);
9867 /* Look for the `template' keyword. */
9868 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9869 /* Let the front end know that we are processing an explicit
9871 begin_explicit_instantiation ();
9872 /* [temp.explicit] says that we are supposed to ignore access
9873 control while processing explicit instantiation directives. */
9874 push_deferring_access_checks (dk_no_check
);
9875 /* Parse a decl-specifier-seq. */
9876 cp_parser_decl_specifier_seq (parser
,
9877 CP_PARSER_FLAGS_OPTIONAL
,
9879 &declares_class_or_enum
);
9880 /* If there was exactly one decl-specifier, and it declared a class,
9881 and there's no declarator, then we have an explicit type
9883 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
9887 type
= check_tag_decl (&decl_specifiers
);
9888 /* Turn access control back on for names used during
9889 template instantiation. */
9890 pop_deferring_access_checks ();
9892 do_type_instantiation (type
, extension_specifier
,
9893 /*complain=*/tf_error
);
9897 cp_declarator
*declarator
;
9900 /* Parse the declarator. */
9902 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9903 /*ctor_dtor_or_conv_p=*/NULL
,
9904 /*parenthesized_p=*/NULL
,
9905 /*member_p=*/false);
9906 if (declares_class_or_enum
& 2)
9907 cp_parser_check_for_definition_in_return_type (declarator
,
9908 decl_specifiers
.type
);
9909 if (declarator
!= cp_error_declarator
)
9911 decl
= grokdeclarator (declarator
, &decl_specifiers
,
9912 NORMAL
, 0, &decl_specifiers
.attributes
);
9913 /* Turn access control back on for names used during
9914 template instantiation. */
9915 pop_deferring_access_checks ();
9916 /* Do the explicit instantiation. */
9917 do_decl_instantiation (decl
, extension_specifier
);
9921 pop_deferring_access_checks ();
9922 /* Skip the body of the explicit instantiation. */
9923 cp_parser_skip_to_end_of_statement (parser
);
9926 /* We're done with the instantiation. */
9927 end_explicit_instantiation ();
9929 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9932 /* Parse an explicit-specialization.
9934 explicit-specialization:
9935 template < > declaration
9937 Although the standard says `declaration', what it really means is:
9939 explicit-specialization:
9940 template <> decl-specifier [opt] init-declarator [opt] ;
9941 template <> function-definition
9942 template <> explicit-specialization
9943 template <> template-declaration */
9946 cp_parser_explicit_specialization (cp_parser
* parser
)
9949 /* Look for the `template' keyword. */
9950 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9951 /* Look for the `<'. */
9952 cp_parser_require (parser
, CPP_LESS
, "`<'");
9953 /* Look for the `>'. */
9954 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9955 /* We have processed another parameter list. */
9956 ++parser
->num_template_parameter_lists
;
9959 A template ... explicit specialization ... shall not have C
9961 if (current_lang_name
== lang_name_c
)
9963 error ("template specialization with C linkage");
9964 /* Give it C++ linkage to avoid confusing other parts of the
9966 push_lang_context (lang_name_cplusplus
);
9967 need_lang_pop
= true;
9970 need_lang_pop
= false;
9971 /* Let the front end know that we are beginning a specialization. */
9972 if (!begin_specialization ())
9974 end_specialization ();
9975 cp_parser_skip_to_end_of_block_or_statement (parser
);
9979 /* If the next keyword is `template', we need to figure out whether
9980 or not we're looking a template-declaration. */
9981 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
9983 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
9984 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
9985 cp_parser_template_declaration_after_export (parser
,
9986 /*member_p=*/false);
9988 cp_parser_explicit_specialization (parser
);
9991 /* Parse the dependent declaration. */
9992 cp_parser_single_declaration (parser
,
9996 /* We're done with the specialization. */
9997 end_specialization ();
9998 /* For the erroneous case of a template with C linkage, we pushed an
9999 implicit C++ linkage scope; exit that scope now. */
10001 pop_lang_context ();
10002 /* We're done with this parameter list. */
10003 --parser
->num_template_parameter_lists
;
10006 /* Parse a type-specifier.
10009 simple-type-specifier
10012 elaborated-type-specifier
10020 Returns a representation of the type-specifier. For a
10021 class-specifier, enum-specifier, or elaborated-type-specifier, a
10022 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10024 The parser flags FLAGS is used to control type-specifier parsing.
10026 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10027 in a decl-specifier-seq.
10029 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10030 class-specifier, enum-specifier, or elaborated-type-specifier, then
10031 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10032 if a type is declared; 2 if it is defined. Otherwise, it is set to
10035 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10036 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10037 is set to FALSE. */
10040 cp_parser_type_specifier (cp_parser
* parser
,
10041 cp_parser_flags flags
,
10042 cp_decl_specifier_seq
*decl_specs
,
10043 bool is_declaration
,
10044 int* declares_class_or_enum
,
10045 bool* is_cv_qualifier
)
10047 tree type_spec
= NULL_TREE
;
10050 cp_decl_spec ds
= ds_last
;
10052 /* Assume this type-specifier does not declare a new type. */
10053 if (declares_class_or_enum
)
10054 *declares_class_or_enum
= 0;
10055 /* And that it does not specify a cv-qualifier. */
10056 if (is_cv_qualifier
)
10057 *is_cv_qualifier
= false;
10058 /* Peek at the next token. */
10059 token
= cp_lexer_peek_token (parser
->lexer
);
10061 /* If we're looking at a keyword, we can use that to guide the
10062 production we choose. */
10063 keyword
= token
->keyword
;
10067 /* Look for the enum-specifier. */
10068 type_spec
= cp_parser_enum_specifier (parser
);
10069 /* If that worked, we're done. */
10072 if (declares_class_or_enum
)
10073 *declares_class_or_enum
= 2;
10075 cp_parser_set_decl_spec_type (decl_specs
,
10077 /*user_defined_p=*/true);
10081 goto elaborated_type_specifier
;
10083 /* Any of these indicate either a class-specifier, or an
10084 elaborated-type-specifier. */
10088 /* Parse tentatively so that we can back up if we don't find a
10089 class-specifier. */
10090 cp_parser_parse_tentatively (parser
);
10091 /* Look for the class-specifier. */
10092 type_spec
= cp_parser_class_specifier (parser
);
10093 /* If that worked, we're done. */
10094 if (cp_parser_parse_definitely (parser
))
10096 if (declares_class_or_enum
)
10097 *declares_class_or_enum
= 2;
10099 cp_parser_set_decl_spec_type (decl_specs
,
10101 /*user_defined_p=*/true);
10105 /* Fall through. */
10106 elaborated_type_specifier
:
10107 /* We're declaring (not defining) a class or enum. */
10108 if (declares_class_or_enum
)
10109 *declares_class_or_enum
= 1;
10111 /* Fall through. */
10113 /* Look for an elaborated-type-specifier. */
10115 = (cp_parser_elaborated_type_specifier
10117 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
10120 cp_parser_set_decl_spec_type (decl_specs
,
10122 /*user_defined_p=*/true);
10127 if (is_cv_qualifier
)
10128 *is_cv_qualifier
= true;
10133 if (is_cv_qualifier
)
10134 *is_cv_qualifier
= true;
10139 if (is_cv_qualifier
)
10140 *is_cv_qualifier
= true;
10144 /* The `__complex__' keyword is a GNU extension. */
10152 /* Handle simple keywords. */
10157 ++decl_specs
->specs
[(int)ds
];
10158 decl_specs
->any_specifiers_p
= true;
10160 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
10163 /* If we do not already have a type-specifier, assume we are looking
10164 at a simple-type-specifier. */
10165 type_spec
= cp_parser_simple_type_specifier (parser
,
10169 /* If we didn't find a type-specifier, and a type-specifier was not
10170 optional in this context, issue an error message. */
10171 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
10173 cp_parser_error (parser
, "expected type specifier");
10174 return error_mark_node
;
10180 /* Parse a simple-type-specifier.
10182 simple-type-specifier:
10183 :: [opt] nested-name-specifier [opt] type-name
10184 :: [opt] nested-name-specifier template template-id
10199 simple-type-specifier:
10200 __typeof__ unary-expression
10201 __typeof__ ( type-id )
10203 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10204 appropriately updated. */
10207 cp_parser_simple_type_specifier (cp_parser
* parser
,
10208 cp_decl_specifier_seq
*decl_specs
,
10209 cp_parser_flags flags
)
10211 tree type
= NULL_TREE
;
10214 /* Peek at the next token. */
10215 token
= cp_lexer_peek_token (parser
->lexer
);
10217 /* If we're looking at a keyword, things are easy. */
10218 switch (token
->keyword
)
10222 decl_specs
->explicit_char_p
= true;
10223 type
= char_type_node
;
10226 type
= wchar_type_node
;
10229 type
= boolean_type_node
;
10233 ++decl_specs
->specs
[(int) ds_short
];
10234 type
= short_integer_type_node
;
10238 decl_specs
->explicit_int_p
= true;
10239 type
= integer_type_node
;
10243 ++decl_specs
->specs
[(int) ds_long
];
10244 type
= long_integer_type_node
;
10248 ++decl_specs
->specs
[(int) ds_signed
];
10249 type
= integer_type_node
;
10253 ++decl_specs
->specs
[(int) ds_unsigned
];
10254 type
= unsigned_type_node
;
10257 type
= float_type_node
;
10260 type
= double_type_node
;
10263 type
= void_type_node
;
10267 /* Consume the `typeof' token. */
10268 cp_lexer_consume_token (parser
->lexer
);
10269 /* Parse the operand to `typeof'. */
10270 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
10271 /* If it is not already a TYPE, take its type. */
10272 if (!TYPE_P (type
))
10273 type
= finish_typeof (type
);
10276 cp_parser_set_decl_spec_type (decl_specs
, type
,
10277 /*user_defined_p=*/true);
10285 /* If the type-specifier was for a built-in type, we're done. */
10290 /* Record the type. */
10292 && (token
->keyword
!= RID_SIGNED
10293 && token
->keyword
!= RID_UNSIGNED
10294 && token
->keyword
!= RID_SHORT
10295 && token
->keyword
!= RID_LONG
))
10296 cp_parser_set_decl_spec_type (decl_specs
,
10298 /*user_defined=*/false);
10300 decl_specs
->any_specifiers_p
= true;
10302 /* Consume the token. */
10303 id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
10305 /* There is no valid C++ program where a non-template type is
10306 followed by a "<". That usually indicates that the user thought
10307 that the type was a template. */
10308 cp_parser_check_for_invalid_template_id (parser
, type
);
10310 return TYPE_NAME (type
);
10313 /* The type-specifier must be a user-defined type. */
10314 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
10319 /* Don't gobble tokens or issue error messages if this is an
10320 optional type-specifier. */
10321 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
10322 cp_parser_parse_tentatively (parser
);
10324 /* Look for the optional `::' operator. */
10326 = (cp_parser_global_scope_opt (parser
,
10327 /*current_scope_valid_p=*/false)
10329 /* Look for the nested-name specifier. */
10331 = (cp_parser_nested_name_specifier_opt (parser
,
10332 /*typename_keyword_p=*/false,
10333 /*check_dependency_p=*/true,
10335 /*is_declaration=*/false)
10337 /* If we have seen a nested-name-specifier, and the next token
10338 is `template', then we are using the template-id production. */
10340 && cp_parser_optional_template_keyword (parser
))
10342 /* Look for the template-id. */
10343 type
= cp_parser_template_id (parser
,
10344 /*template_keyword_p=*/true,
10345 /*check_dependency_p=*/true,
10346 /*is_declaration=*/false);
10347 /* If the template-id did not name a type, we are out of
10349 if (TREE_CODE (type
) != TYPE_DECL
)
10351 cp_parser_error (parser
, "expected template-id for type");
10355 /* Otherwise, look for a type-name. */
10357 type
= cp_parser_type_name (parser
);
10358 /* Keep track of all name-lookups performed in class scopes. */
10362 && TREE_CODE (type
) == TYPE_DECL
10363 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
10364 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
10365 /* If it didn't work out, we don't have a TYPE. */
10366 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
10367 && !cp_parser_parse_definitely (parser
))
10369 if (type
&& decl_specs
)
10370 cp_parser_set_decl_spec_type (decl_specs
, type
,
10371 /*user_defined=*/true);
10374 /* If we didn't get a type-name, issue an error message. */
10375 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
10377 cp_parser_error (parser
, "expected type-name");
10378 return error_mark_node
;
10381 /* There is no valid C++ program where a non-template type is
10382 followed by a "<". That usually indicates that the user thought
10383 that the type was a template. */
10384 if (type
&& type
!= error_mark_node
)
10386 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10387 If it is, then the '<'...'>' enclose protocol names rather than
10388 template arguments, and so everything is fine. */
10389 if (c_dialect_objc ()
10390 && (objc_is_id (type
) || objc_is_class_name (type
)))
10392 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
10393 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
10395 /* Clobber the "unqualified" type previously entered into
10396 DECL_SPECS with the new, improved protocol-qualified version. */
10398 decl_specs
->type
= qual_type
;
10403 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
10409 /* Parse a type-name.
10422 Returns a TYPE_DECL for the type. */
10425 cp_parser_type_name (cp_parser
* parser
)
10430 /* We can't know yet whether it is a class-name or not. */
10431 cp_parser_parse_tentatively (parser
);
10432 /* Try a class-name. */
10433 type_decl
= cp_parser_class_name (parser
,
10434 /*typename_keyword_p=*/false,
10435 /*template_keyword_p=*/false,
10437 /*check_dependency_p=*/true,
10438 /*class_head_p=*/false,
10439 /*is_declaration=*/false);
10440 /* If it's not a class-name, keep looking. */
10441 if (!cp_parser_parse_definitely (parser
))
10443 /* It must be a typedef-name or an enum-name. */
10444 identifier
= cp_parser_identifier (parser
);
10445 if (identifier
== error_mark_node
)
10446 return error_mark_node
;
10448 /* Look up the type-name. */
10449 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10451 if (TREE_CODE (type_decl
) != TYPE_DECL
10452 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
10454 /* See if this is an Objective-C type. */
10455 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
10456 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
10458 type_decl
= TYPE_NAME (type
);
10461 /* Issue an error if we did not find a type-name. */
10462 if (TREE_CODE (type_decl
) != TYPE_DECL
)
10464 if (!cp_parser_simulate_error (parser
))
10465 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
10467 type_decl
= error_mark_node
;
10469 /* Remember that the name was used in the definition of the
10470 current class so that we can check later to see if the
10471 meaning would have been different after the class was
10472 entirely defined. */
10473 else if (type_decl
!= error_mark_node
10475 maybe_note_name_used_in_class (identifier
, type_decl
);
10482 /* Parse an elaborated-type-specifier. Note that the grammar given
10483 here incorporates the resolution to DR68.
10485 elaborated-type-specifier:
10486 class-key :: [opt] nested-name-specifier [opt] identifier
10487 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10488 enum :: [opt] nested-name-specifier [opt] identifier
10489 typename :: [opt] nested-name-specifier identifier
10490 typename :: [opt] nested-name-specifier template [opt]
10495 elaborated-type-specifier:
10496 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10497 class-key attributes :: [opt] nested-name-specifier [opt]
10498 template [opt] template-id
10499 enum attributes :: [opt] nested-name-specifier [opt] identifier
10501 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10502 declared `friend'. If IS_DECLARATION is TRUE, then this
10503 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10504 something is being declared.
10506 Returns the TYPE specified. */
10509 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
10511 bool is_declaration
)
10513 enum tag_types tag_type
;
10515 tree type
= NULL_TREE
;
10516 tree attributes
= NULL_TREE
;
10518 /* See if we're looking at the `enum' keyword. */
10519 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
10521 /* Consume the `enum' token. */
10522 cp_lexer_consume_token (parser
->lexer
);
10523 /* Remember that it's an enumeration type. */
10524 tag_type
= enum_type
;
10525 /* Parse the attributes. */
10526 attributes
= cp_parser_attributes_opt (parser
);
10528 /* Or, it might be `typename'. */
10529 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10532 /* Consume the `typename' token. */
10533 cp_lexer_consume_token (parser
->lexer
);
10534 /* Remember that it's a `typename' type. */
10535 tag_type
= typename_type
;
10536 /* The `typename' keyword is only allowed in templates. */
10537 if (!processing_template_decl
)
10538 pedwarn ("using %<typename%> outside of template");
10540 /* Otherwise it must be a class-key. */
10543 tag_type
= cp_parser_class_key (parser
);
10544 if (tag_type
== none_type
)
10545 return error_mark_node
;
10546 /* Parse the attributes. */
10547 attributes
= cp_parser_attributes_opt (parser
);
10550 /* Look for the `::' operator. */
10551 cp_parser_global_scope_opt (parser
,
10552 /*current_scope_valid_p=*/false);
10553 /* Look for the nested-name-specifier. */
10554 if (tag_type
== typename_type
)
10556 if (!cp_parser_nested_name_specifier (parser
,
10557 /*typename_keyword_p=*/true,
10558 /*check_dependency_p=*/true,
10561 return error_mark_node
;
10564 /* Even though `typename' is not present, the proposed resolution
10565 to Core Issue 180 says that in `class A<T>::B', `B' should be
10566 considered a type-name, even if `A<T>' is dependent. */
10567 cp_parser_nested_name_specifier_opt (parser
,
10568 /*typename_keyword_p=*/true,
10569 /*check_dependency_p=*/true,
10572 /* For everything but enumeration types, consider a template-id.
10573 For an enumeration type, consider only a plain identifier. */
10574 if (tag_type
!= enum_type
)
10576 bool template_p
= false;
10579 /* Allow the `template' keyword. */
10580 template_p
= cp_parser_optional_template_keyword (parser
);
10581 /* If we didn't see `template', we don't know if there's a
10582 template-id or not. */
10584 cp_parser_parse_tentatively (parser
);
10585 /* Parse the template-id. */
10586 decl
= cp_parser_template_id (parser
, template_p
,
10587 /*check_dependency_p=*/true,
10589 /* If we didn't find a template-id, look for an ordinary
10591 if (!template_p
&& !cp_parser_parse_definitely (parser
))
10593 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10594 in effect, then we must assume that, upon instantiation, the
10595 template will correspond to a class. */
10596 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
10597 && tag_type
== typename_type
)
10598 type
= make_typename_type (parser
->scope
, decl
,
10600 /*complain=*/tf_error
);
10602 type
= TREE_TYPE (decl
);
10607 identifier
= cp_parser_identifier (parser
);
10609 if (identifier
== error_mark_node
)
10611 parser
->scope
= NULL_TREE
;
10612 return error_mark_node
;
10615 /* For a `typename', we needn't call xref_tag. */
10616 if (tag_type
== typename_type
10617 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
10618 return cp_parser_make_typename_type (parser
, parser
->scope
,
10620 /* Look up a qualified name in the usual way. */
10625 decl
= cp_parser_lookup_name (parser
, identifier
,
10627 /*is_template=*/false,
10628 /*is_namespace=*/false,
10629 /*check_dependency=*/true,
10630 /*ambiguous_decls=*/NULL
);
10632 /* If we are parsing friend declaration, DECL may be a
10633 TEMPLATE_DECL tree node here. However, we need to check
10634 whether this TEMPLATE_DECL results in valid code. Consider
10635 the following example:
10638 template <class T> class C {};
10641 template <class T> friend class N::C; // #1, valid code
10643 template <class T> class Y {
10644 friend class N::C; // #2, invalid code
10647 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10648 name lookup of `N::C'. We see that friend declaration must
10649 be template for the code to be valid. Note that
10650 processing_template_decl does not work here since it is
10651 always 1 for the above two cases. */
10653 decl
= (cp_parser_maybe_treat_template_as_class
10654 (decl
, /*tag_name_p=*/is_friend
10655 && parser
->num_template_parameter_lists
));
10657 if (TREE_CODE (decl
) != TYPE_DECL
)
10659 cp_parser_diagnose_invalid_type_name (parser
,
10662 return error_mark_node
;
10665 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
10667 bool allow_template
= (parser
->num_template_parameter_lists
10668 || DECL_SELF_REFERENCE_P (decl
));
10669 type
= check_elaborated_type_specifier (tag_type
, decl
,
10672 if (type
== error_mark_node
)
10673 return error_mark_node
;
10676 type
= TREE_TYPE (decl
);
10680 /* An elaborated-type-specifier sometimes introduces a new type and
10681 sometimes names an existing type. Normally, the rule is that it
10682 introduces a new type only if there is not an existing type of
10683 the same name already in scope. For example, given:
10686 void f() { struct S s; }
10688 the `struct S' in the body of `f' is the same `struct S' as in
10689 the global scope; the existing definition is used. However, if
10690 there were no global declaration, this would introduce a new
10691 local class named `S'.
10693 An exception to this rule applies to the following code:
10695 namespace N { struct S; }
10697 Here, the elaborated-type-specifier names a new type
10698 unconditionally; even if there is already an `S' in the
10699 containing scope this declaration names a new type.
10700 This exception only applies if the elaborated-type-specifier
10701 forms the complete declaration:
10705 A declaration consisting solely of `class-key identifier ;' is
10706 either a redeclaration of the name in the current scope or a
10707 forward declaration of the identifier as a class name. It
10708 introduces the name into the current scope.
10710 We are in this situation precisely when the next token is a `;'.
10712 An exception to the exception is that a `friend' declaration does
10713 *not* name a new type; i.e., given:
10715 struct S { friend struct T; };
10717 `T' is not a new type in the scope of `S'.
10719 Also, `new struct S' or `sizeof (struct S)' never results in the
10720 definition of a new type; a new type can only be declared in a
10721 declaration context. */
10727 /* Friends have special name lookup rules. */
10728 ts
= ts_within_enclosing_non_class
;
10729 else if (is_declaration
10730 && cp_lexer_next_token_is (parser
->lexer
,
10732 /* This is a `class-key identifier ;' */
10738 (parser
->num_template_parameter_lists
10739 && (cp_parser_next_token_starts_class_definition_p (parser
)
10740 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
10741 /* An unqualified name was used to reference this type, so
10742 there were no qualifying templates. */
10743 if (!cp_parser_check_template_parameters (parser
,
10744 /*num_templates=*/0))
10745 return error_mark_node
;
10746 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
10750 if (type
== error_mark_node
)
10751 return error_mark_node
;
10753 /* Allow attributes on forward declarations of classes. */
10756 if (TREE_CODE (type
) == TYPENAME_TYPE
)
10757 warning (OPT_Wattributes
,
10758 "attributes ignored on uninstantiated type");
10759 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
10760 && ! processing_explicit_instantiation
)
10761 warning (OPT_Wattributes
,
10762 "attributes ignored on template instantiation");
10763 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
10764 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
10766 warning (OPT_Wattributes
,
10767 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10770 if (tag_type
!= enum_type
)
10771 cp_parser_check_class_key (tag_type
, type
);
10773 /* A "<" cannot follow an elaborated type specifier. If that
10774 happens, the user was probably trying to form a template-id. */
10775 cp_parser_check_for_invalid_template_id (parser
, type
);
10780 /* Parse an enum-specifier.
10783 enum identifier [opt] { enumerator-list [opt] }
10786 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10789 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10790 if the token stream isn't an enum-specifier after all. */
10793 cp_parser_enum_specifier (cp_parser
* parser
)
10799 /* Parse tentatively so that we can back up if we don't find a
10801 cp_parser_parse_tentatively (parser
);
10803 /* Caller guarantees that the current token is 'enum', an identifier
10804 possibly follows, and the token after that is an opening brace.
10805 If we don't have an identifier, fabricate an anonymous name for
10806 the enumeration being defined. */
10807 cp_lexer_consume_token (parser
->lexer
);
10809 attributes
= cp_parser_attributes_opt (parser
);
10811 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10812 identifier
= cp_parser_identifier (parser
);
10814 identifier
= make_anon_name ();
10816 /* Look for the `{' but don't consume it yet. */
10817 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10818 cp_parser_simulate_error (parser
);
10820 if (!cp_parser_parse_definitely (parser
))
10823 /* Issue an error message if type-definitions are forbidden here. */
10824 if (!cp_parser_check_type_definition (parser
))
10825 type
= error_mark_node
;
10827 /* Create the new type. We do this before consuming the opening
10828 brace so the enum will be recorded as being on the line of its
10829 tag (or the 'enum' keyword, if there is no tag). */
10830 type
= start_enum (identifier
);
10832 /* Consume the opening brace. */
10833 cp_lexer_consume_token (parser
->lexer
);
10835 if (type
== error_mark_node
)
10837 cp_parser_skip_to_end_of_block_or_statement (parser
);
10838 return error_mark_node
;
10841 /* If the next token is not '}', then there are some enumerators. */
10842 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
10843 cp_parser_enumerator_list (parser
, type
);
10845 /* Consume the final '}'. */
10846 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10848 /* Look for trailing attributes to apply to this enumeration, and
10849 apply them if appropriate. */
10850 if (cp_parser_allow_gnu_extensions_p (parser
))
10852 tree trailing_attr
= cp_parser_attributes_opt (parser
);
10853 cplus_decl_attributes (&type
,
10855 (int) ATTR_FLAG_TYPE_IN_PLACE
);
10858 /* Finish up the enumeration. */
10859 finish_enum (type
);
10864 /* Parse an enumerator-list. The enumerators all have the indicated
10868 enumerator-definition
10869 enumerator-list , enumerator-definition */
10872 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
10876 /* Parse an enumerator-definition. */
10877 cp_parser_enumerator_definition (parser
, type
);
10879 /* If the next token is not a ',', we've reached the end of
10881 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
10883 /* Otherwise, consume the `,' and keep going. */
10884 cp_lexer_consume_token (parser
->lexer
);
10885 /* If the next token is a `}', there is a trailing comma. */
10886 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10888 if (pedantic
&& !in_system_header
)
10889 pedwarn ("comma at end of enumerator list");
10895 /* Parse an enumerator-definition. The enumerator has the indicated
10898 enumerator-definition:
10900 enumerator = constant-expression
10906 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
10911 /* Look for the identifier. */
10912 identifier
= cp_parser_identifier (parser
);
10913 if (identifier
== error_mark_node
)
10916 /* If the next token is an '=', then there is an explicit value. */
10917 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10919 /* Consume the `=' token. */
10920 cp_lexer_consume_token (parser
->lexer
);
10921 /* Parse the value. */
10922 value
= cp_parser_constant_expression (parser
,
10923 /*allow_non_constant_p=*/false,
10929 /* Create the enumerator. */
10930 build_enumerator (identifier
, value
, type
);
10933 /* Parse a namespace-name.
10936 original-namespace-name
10939 Returns the NAMESPACE_DECL for the namespace. */
10942 cp_parser_namespace_name (cp_parser
* parser
)
10945 tree namespace_decl
;
10947 /* Get the name of the namespace. */
10948 identifier
= cp_parser_identifier (parser
);
10949 if (identifier
== error_mark_node
)
10950 return error_mark_node
;
10952 /* Look up the identifier in the currently active scope. Look only
10953 for namespaces, due to:
10955 [basic.lookup.udir]
10957 When looking up a namespace-name in a using-directive or alias
10958 definition, only namespace names are considered.
10962 [basic.lookup.qual]
10964 During the lookup of a name preceding the :: scope resolution
10965 operator, object, function, and enumerator names are ignored.
10967 (Note that cp_parser_class_or_namespace_name only calls this
10968 function if the token after the name is the scope resolution
10970 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
10972 /*is_template=*/false,
10973 /*is_namespace=*/true,
10974 /*check_dependency=*/true,
10975 /*ambiguous_decls=*/NULL
);
10976 /* If it's not a namespace, issue an error. */
10977 if (namespace_decl
== error_mark_node
10978 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
10980 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
10981 error ("%qD is not a namespace-name", identifier
);
10982 cp_parser_error (parser
, "expected namespace-name");
10983 namespace_decl
= error_mark_node
;
10986 return namespace_decl
;
10989 /* Parse a namespace-definition.
10991 namespace-definition:
10992 named-namespace-definition
10993 unnamed-namespace-definition
10995 named-namespace-definition:
10996 original-namespace-definition
10997 extension-namespace-definition
10999 original-namespace-definition:
11000 namespace identifier { namespace-body }
11002 extension-namespace-definition:
11003 namespace original-namespace-name { namespace-body }
11005 unnamed-namespace-definition:
11006 namespace { namespace-body } */
11009 cp_parser_namespace_definition (cp_parser
* parser
)
11011 tree identifier
, attribs
;
11013 /* Look for the `namespace' keyword. */
11014 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
11016 /* Get the name of the namespace. We do not attempt to distinguish
11017 between an original-namespace-definition and an
11018 extension-namespace-definition at this point. The semantic
11019 analysis routines are responsible for that. */
11020 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11021 identifier
= cp_parser_identifier (parser
);
11023 identifier
= NULL_TREE
;
11025 /* Parse any specified attributes. */
11026 attribs
= cp_parser_attributes_opt (parser
);
11028 /* Look for the `{' to start the namespace. */
11029 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
11030 /* Start the namespace. */
11031 push_namespace_with_attribs (identifier
, attribs
);
11032 /* Parse the body of the namespace. */
11033 cp_parser_namespace_body (parser
);
11034 /* Finish the namespace. */
11036 /* Look for the final `}'. */
11037 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11040 /* Parse a namespace-body.
11043 declaration-seq [opt] */
11046 cp_parser_namespace_body (cp_parser
* parser
)
11048 cp_parser_declaration_seq_opt (parser
);
11051 /* Parse a namespace-alias-definition.
11053 namespace-alias-definition:
11054 namespace identifier = qualified-namespace-specifier ; */
11057 cp_parser_namespace_alias_definition (cp_parser
* parser
)
11060 tree namespace_specifier
;
11062 /* Look for the `namespace' keyword. */
11063 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
11064 /* Look for the identifier. */
11065 identifier
= cp_parser_identifier (parser
);
11066 if (identifier
== error_mark_node
)
11068 /* Look for the `=' token. */
11069 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
11070 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11072 error ("%<namespace%> definition is not allowed here");
11073 /* Skip the definition. */
11074 cp_lexer_consume_token (parser
->lexer
);
11075 cp_parser_skip_to_closing_brace (parser
);
11076 cp_lexer_consume_token (parser
->lexer
);
11079 cp_parser_require (parser
, CPP_EQ
, "`='");
11080 /* Look for the qualified-namespace-specifier. */
11081 namespace_specifier
11082 = cp_parser_qualified_namespace_specifier (parser
);
11083 /* Look for the `;' token. */
11084 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11086 /* Register the alias in the symbol table. */
11087 do_namespace_alias (identifier
, namespace_specifier
);
11090 /* Parse a qualified-namespace-specifier.
11092 qualified-namespace-specifier:
11093 :: [opt] nested-name-specifier [opt] namespace-name
11095 Returns a NAMESPACE_DECL corresponding to the specified
11099 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
11101 /* Look for the optional `::'. */
11102 cp_parser_global_scope_opt (parser
,
11103 /*current_scope_valid_p=*/false);
11105 /* Look for the optional nested-name-specifier. */
11106 cp_parser_nested_name_specifier_opt (parser
,
11107 /*typename_keyword_p=*/false,
11108 /*check_dependency_p=*/true,
11110 /*is_declaration=*/true);
11112 return cp_parser_namespace_name (parser
);
11115 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11116 access declaration.
11119 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11120 using :: unqualified-id ;
11122 access-declaration:
11128 cp_parser_using_declaration (cp_parser
* parser
,
11129 bool access_declaration_p
)
11132 bool typename_p
= false;
11133 bool global_scope_p
;
11138 if (access_declaration_p
)
11139 cp_parser_parse_tentatively (parser
);
11142 /* Look for the `using' keyword. */
11143 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
11145 /* Peek at the next token. */
11146 token
= cp_lexer_peek_token (parser
->lexer
);
11147 /* See if it's `typename'. */
11148 if (token
->keyword
== RID_TYPENAME
)
11150 /* Remember that we've seen it. */
11152 /* Consume the `typename' token. */
11153 cp_lexer_consume_token (parser
->lexer
);
11157 /* Look for the optional global scope qualification. */
11159 = (cp_parser_global_scope_opt (parser
,
11160 /*current_scope_valid_p=*/false)
11163 /* If we saw `typename', or didn't see `::', then there must be a
11164 nested-name-specifier present. */
11165 if (typename_p
|| !global_scope_p
)
11166 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
11167 /*check_dependency_p=*/true,
11169 /*is_declaration=*/true);
11170 /* Otherwise, we could be in either of the two productions. In that
11171 case, treat the nested-name-specifier as optional. */
11173 qscope
= cp_parser_nested_name_specifier_opt (parser
,
11174 /*typename_keyword_p=*/false,
11175 /*check_dependency_p=*/true,
11177 /*is_declaration=*/true);
11179 qscope
= global_namespace
;
11181 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
11182 /* Something has already gone wrong; there's no need to parse
11183 further. Since an error has occurred, the return value of
11184 cp_parser_parse_definitely will be false, as required. */
11185 return cp_parser_parse_definitely (parser
);
11187 /* Parse the unqualified-id. */
11188 identifier
= cp_parser_unqualified_id (parser
,
11189 /*template_keyword_p=*/false,
11190 /*check_dependency_p=*/true,
11191 /*declarator_p=*/true,
11192 /*optional_p=*/false);
11194 if (access_declaration_p
)
11196 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11197 cp_parser_simulate_error (parser
);
11198 if (!cp_parser_parse_definitely (parser
))
11202 /* The function we call to handle a using-declaration is different
11203 depending on what scope we are in. */
11204 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
11206 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
11207 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
11208 /* [namespace.udecl]
11210 A using declaration shall not name a template-id. */
11211 error ("a template-id may not appear in a using-declaration");
11214 if (at_class_scope_p ())
11216 /* Create the USING_DECL. */
11217 decl
= do_class_using_decl (parser
->scope
, identifier
);
11218 /* Add it to the list of members in this class. */
11219 finish_member_declaration (decl
);
11223 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
11224 if (decl
== error_mark_node
)
11225 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
11226 else if (!at_namespace_scope_p ())
11227 do_local_using_decl (decl
, qscope
, identifier
);
11229 do_toplevel_using_decl (decl
, qscope
, identifier
);
11233 /* Look for the final `;'. */
11234 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11239 /* Parse a using-directive.
11242 using namespace :: [opt] nested-name-specifier [opt]
11243 namespace-name ; */
11246 cp_parser_using_directive (cp_parser
* parser
)
11248 tree namespace_decl
;
11251 /* Look for the `using' keyword. */
11252 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
11253 /* And the `namespace' keyword. */
11254 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
11255 /* Look for the optional `::' operator. */
11256 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
11257 /* And the optional nested-name-specifier. */
11258 cp_parser_nested_name_specifier_opt (parser
,
11259 /*typename_keyword_p=*/false,
11260 /*check_dependency_p=*/true,
11262 /*is_declaration=*/true);
11263 /* Get the namespace being used. */
11264 namespace_decl
= cp_parser_namespace_name (parser
);
11265 /* And any specified attributes. */
11266 attribs
= cp_parser_attributes_opt (parser
);
11267 /* Update the symbol table. */
11268 parse_using_directive (namespace_decl
, attribs
);
11269 /* Look for the final `;'. */
11270 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11273 /* Parse an asm-definition.
11276 asm ( string-literal ) ;
11281 asm volatile [opt] ( string-literal ) ;
11282 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11283 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11284 : asm-operand-list [opt] ) ;
11285 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11286 : asm-operand-list [opt]
11287 : asm-operand-list [opt] ) ; */
11290 cp_parser_asm_definition (cp_parser
* parser
)
11293 tree outputs
= NULL_TREE
;
11294 tree inputs
= NULL_TREE
;
11295 tree clobbers
= NULL_TREE
;
11297 bool volatile_p
= false;
11298 bool extended_p
= false;
11300 /* Look for the `asm' keyword. */
11301 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
11302 /* See if the next token is `volatile'. */
11303 if (cp_parser_allow_gnu_extensions_p (parser
)
11304 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
11306 /* Remember that we saw the `volatile' keyword. */
11308 /* Consume the token. */
11309 cp_lexer_consume_token (parser
->lexer
);
11311 /* Look for the opening `('. */
11312 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
11314 /* Look for the string. */
11315 string
= cp_parser_string_literal (parser
, false, false);
11316 if (string
== error_mark_node
)
11318 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11319 /*consume_paren=*/true);
11323 /* If we're allowing GNU extensions, check for the extended assembly
11324 syntax. Unfortunately, the `:' tokens need not be separated by
11325 a space in C, and so, for compatibility, we tolerate that here
11326 too. Doing that means that we have to treat the `::' operator as
11328 if (cp_parser_allow_gnu_extensions_p (parser
)
11329 && parser
->in_function_body
11330 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
11331 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
11333 bool inputs_p
= false;
11334 bool clobbers_p
= false;
11336 /* The extended syntax was used. */
11339 /* Look for outputs. */
11340 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11342 /* Consume the `:'. */
11343 cp_lexer_consume_token (parser
->lexer
);
11344 /* Parse the output-operands. */
11345 if (cp_lexer_next_token_is_not (parser
->lexer
,
11347 && cp_lexer_next_token_is_not (parser
->lexer
,
11349 && cp_lexer_next_token_is_not (parser
->lexer
,
11351 outputs
= cp_parser_asm_operand_list (parser
);
11353 /* If the next token is `::', there are no outputs, and the
11354 next token is the beginning of the inputs. */
11355 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11356 /* The inputs are coming next. */
11359 /* Look for inputs. */
11361 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11363 /* Consume the `:' or `::'. */
11364 cp_lexer_consume_token (parser
->lexer
);
11365 /* Parse the output-operands. */
11366 if (cp_lexer_next_token_is_not (parser
->lexer
,
11368 && cp_lexer_next_token_is_not (parser
->lexer
,
11370 inputs
= cp_parser_asm_operand_list (parser
);
11372 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11373 /* The clobbers are coming next. */
11376 /* Look for clobbers. */
11378 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11380 /* Consume the `:' or `::'. */
11381 cp_lexer_consume_token (parser
->lexer
);
11382 /* Parse the clobbers. */
11383 if (cp_lexer_next_token_is_not (parser
->lexer
,
11385 clobbers
= cp_parser_asm_clobber_list (parser
);
11388 /* Look for the closing `)'. */
11389 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11390 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11391 /*consume_paren=*/true);
11392 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11394 /* Create the ASM_EXPR. */
11395 if (parser
->in_function_body
)
11397 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
11399 /* If the extended syntax was not used, mark the ASM_EXPR. */
11402 tree temp
= asm_stmt
;
11403 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
11404 temp
= TREE_OPERAND (temp
, 0);
11406 ASM_INPUT_P (temp
) = 1;
11410 cgraph_add_asm_node (string
);
11413 /* Declarators [gram.dcl.decl] */
11415 /* Parse an init-declarator.
11418 declarator initializer [opt]
11423 declarator asm-specification [opt] attributes [opt] initializer [opt]
11425 function-definition:
11426 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11428 decl-specifier-seq [opt] declarator function-try-block
11432 function-definition:
11433 __extension__ function-definition
11435 The DECL_SPECIFIERS apply to this declarator. Returns a
11436 representation of the entity declared. If MEMBER_P is TRUE, then
11437 this declarator appears in a class scope. The new DECL created by
11438 this declarator is returned.
11440 The CHECKS are access checks that should be performed once we know
11441 what entity is being declared (and, therefore, what classes have
11444 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11445 for a function-definition here as well. If the declarator is a
11446 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11447 be TRUE upon return. By that point, the function-definition will
11448 have been completely parsed.
11450 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11454 cp_parser_init_declarator (cp_parser
* parser
,
11455 cp_decl_specifier_seq
*decl_specifiers
,
11456 VEC (deferred_access_check
,gc
)* checks
,
11457 bool function_definition_allowed_p
,
11459 int declares_class_or_enum
,
11460 bool* function_definition_p
)
11463 cp_declarator
*declarator
;
11464 tree prefix_attributes
;
11466 tree asm_specification
;
11468 tree decl
= NULL_TREE
;
11470 bool is_initialized
;
11471 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11472 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11474 enum cpp_ttype initialization_kind
;
11475 bool is_parenthesized_init
= false;
11476 bool is_non_constant_init
;
11477 int ctor_dtor_or_conv_p
;
11479 tree pushed_scope
= NULL
;
11481 /* Gather the attributes that were provided with the
11482 decl-specifiers. */
11483 prefix_attributes
= decl_specifiers
->attributes
;
11485 /* Assume that this is not the declarator for a function
11487 if (function_definition_p
)
11488 *function_definition_p
= false;
11490 /* Defer access checks while parsing the declarator; we cannot know
11491 what names are accessible until we know what is being
11493 resume_deferring_access_checks ();
11495 /* Parse the declarator. */
11497 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11498 &ctor_dtor_or_conv_p
,
11499 /*parenthesized_p=*/NULL
,
11500 /*member_p=*/false);
11501 /* Gather up the deferred checks. */
11502 stop_deferring_access_checks ();
11504 /* If the DECLARATOR was erroneous, there's no need to go
11506 if (declarator
== cp_error_declarator
)
11507 return error_mark_node
;
11509 /* Check that the number of template-parameter-lists is OK. */
11510 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
11511 return error_mark_node
;
11513 if (declares_class_or_enum
& 2)
11514 cp_parser_check_for_definition_in_return_type (declarator
,
11515 decl_specifiers
->type
);
11517 /* Figure out what scope the entity declared by the DECLARATOR is
11518 located in. `grokdeclarator' sometimes changes the scope, so
11519 we compute it now. */
11520 scope
= get_scope_of_declarator (declarator
);
11522 /* If we're allowing GNU extensions, look for an asm-specification
11524 if (cp_parser_allow_gnu_extensions_p (parser
))
11526 /* Look for an asm-specification. */
11527 asm_specification
= cp_parser_asm_specification_opt (parser
);
11528 /* And attributes. */
11529 attributes
= cp_parser_attributes_opt (parser
);
11533 asm_specification
= NULL_TREE
;
11534 attributes
= NULL_TREE
;
11537 /* Peek at the next token. */
11538 token
= cp_lexer_peek_token (parser
->lexer
);
11539 /* Check to see if the token indicates the start of a
11540 function-definition. */
11541 if (cp_parser_token_starts_function_definition_p (token
))
11543 if (!function_definition_allowed_p
)
11545 /* If a function-definition should not appear here, issue an
11547 cp_parser_error (parser
,
11548 "a function-definition is not allowed here");
11549 return error_mark_node
;
11553 /* Neither attributes nor an asm-specification are allowed
11554 on a function-definition. */
11555 if (asm_specification
)
11556 error ("an asm-specification is not allowed on a function-definition");
11558 error ("attributes are not allowed on a function-definition");
11559 /* This is a function-definition. */
11560 *function_definition_p
= true;
11562 /* Parse the function definition. */
11564 decl
= cp_parser_save_member_function_body (parser
,
11567 prefix_attributes
);
11570 = (cp_parser_function_definition_from_specifiers_and_declarator
11571 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
11579 Only in function declarations for constructors, destructors, and
11580 type conversions can the decl-specifier-seq be omitted.
11582 We explicitly postpone this check past the point where we handle
11583 function-definitions because we tolerate function-definitions
11584 that are missing their return types in some modes. */
11585 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
11587 cp_parser_error (parser
,
11588 "expected constructor, destructor, or type conversion");
11589 return error_mark_node
;
11592 /* An `=' or an `(' indicates an initializer. */
11593 if (token
->type
== CPP_EQ
11594 || token
->type
== CPP_OPEN_PAREN
)
11596 is_initialized
= true;
11597 initialization_kind
= token
->type
;
11601 /* If the init-declarator isn't initialized and isn't followed by a
11602 `,' or `;', it's not a valid init-declarator. */
11603 if (token
->type
!= CPP_COMMA
11604 && token
->type
!= CPP_SEMICOLON
)
11606 cp_parser_error (parser
, "expected initializer");
11607 return error_mark_node
;
11609 is_initialized
= false;
11610 initialization_kind
= CPP_EOF
;
11613 /* Because start_decl has side-effects, we should only call it if we
11614 know we're going ahead. By this point, we know that we cannot
11615 possibly be looking at any other construct. */
11616 cp_parser_commit_to_tentative_parse (parser
);
11618 /* If the decl specifiers were bad, issue an error now that we're
11619 sure this was intended to be a declarator. Then continue
11620 declaring the variable(s), as int, to try to cut down on further
11622 if (decl_specifiers
->any_specifiers_p
11623 && decl_specifiers
->type
== error_mark_node
)
11625 cp_parser_error (parser
, "invalid type in declaration");
11626 decl_specifiers
->type
= integer_type_node
;
11629 /* Check to see whether or not this declaration is a friend. */
11630 friend_p
= cp_parser_friend_p (decl_specifiers
);
11632 /* Enter the newly declared entry in the symbol table. If we're
11633 processing a declaration in a class-specifier, we wait until
11634 after processing the initializer. */
11637 if (parser
->in_unbraced_linkage_specification_p
)
11638 decl_specifiers
->storage_class
= sc_extern
;
11639 decl
= start_decl (declarator
, decl_specifiers
,
11640 is_initialized
, attributes
, prefix_attributes
,
11644 /* Enter the SCOPE. That way unqualified names appearing in the
11645 initializer will be looked up in SCOPE. */
11646 pushed_scope
= push_scope (scope
);
11648 /* Perform deferred access control checks, now that we know in which
11649 SCOPE the declared entity resides. */
11650 if (!member_p
&& decl
)
11652 tree saved_current_function_decl
= NULL_TREE
;
11654 /* If the entity being declared is a function, pretend that we
11655 are in its scope. If it is a `friend', it may have access to
11656 things that would not otherwise be accessible. */
11657 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11659 saved_current_function_decl
= current_function_decl
;
11660 current_function_decl
= decl
;
11663 /* Perform access checks for template parameters. */
11664 cp_parser_perform_template_parameter_access_checks (checks
);
11666 /* Perform the access control checks for the declarator and the
11667 the decl-specifiers. */
11668 perform_deferred_access_checks ();
11670 /* Restore the saved value. */
11671 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11672 current_function_decl
= saved_current_function_decl
;
11675 /* Parse the initializer. */
11676 initializer
= NULL_TREE
;
11677 is_parenthesized_init
= false;
11678 is_non_constant_init
= true;
11679 if (is_initialized
)
11681 if (function_declarator_p (declarator
))
11683 if (initialization_kind
== CPP_EQ
)
11684 initializer
= cp_parser_pure_specifier (parser
);
11687 /* If the declaration was erroneous, we don't really
11688 know what the user intended, so just silently
11689 consume the initializer. */
11690 if (decl
!= error_mark_node
)
11691 error ("initializer provided for function");
11692 cp_parser_skip_to_closing_parenthesis (parser
,
11693 /*recovering=*/true,
11694 /*or_comma=*/false,
11695 /*consume_paren=*/true);
11699 initializer
= cp_parser_initializer (parser
,
11700 &is_parenthesized_init
,
11701 &is_non_constant_init
);
11704 /* The old parser allows attributes to appear after a parenthesized
11705 initializer. Mark Mitchell proposed removing this functionality
11706 on the GCC mailing lists on 2002-08-13. This parser accepts the
11707 attributes -- but ignores them. */
11708 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
11709 if (cp_parser_attributes_opt (parser
))
11710 warning (OPT_Wattributes
,
11711 "attributes after parenthesized initializer ignored");
11713 /* For an in-class declaration, use `grokfield' to create the
11719 pop_scope (pushed_scope
);
11720 pushed_scope
= false;
11722 decl
= grokfield (declarator
, decl_specifiers
,
11723 initializer
, !is_non_constant_init
,
11724 /*asmspec=*/NULL_TREE
,
11725 prefix_attributes
);
11726 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
11727 cp_parser_save_default_args (parser
, decl
);
11730 /* Finish processing the declaration. But, skip friend
11732 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
11734 cp_finish_decl (decl
,
11735 initializer
, !is_non_constant_init
,
11737 /* If the initializer is in parentheses, then this is
11738 a direct-initialization, which means that an
11739 `explicit' constructor is OK. Otherwise, an
11740 `explicit' constructor cannot be used. */
11741 ((is_parenthesized_init
|| !is_initialized
)
11742 ? 0 : LOOKUP_ONLYCONVERTING
));
11744 else if (flag_cpp0x
&& friend_p
&& decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
11745 /* Core issue #226 (C++0x only): A default template-argument
11746 shall not be specified in a friend class template
11748 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/1,
11749 /*is_partial=*/0, /*is_friend_decl=*/1);
11751 if (!friend_p
&& pushed_scope
)
11752 pop_scope (pushed_scope
);
11757 /* Parse a declarator.
11761 ptr-operator declarator
11763 abstract-declarator:
11764 ptr-operator abstract-declarator [opt]
11765 direct-abstract-declarator
11770 attributes [opt] direct-declarator
11771 attributes [opt] ptr-operator declarator
11773 abstract-declarator:
11774 attributes [opt] ptr-operator abstract-declarator [opt]
11775 attributes [opt] direct-abstract-declarator
11777 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11778 detect constructor, destructor or conversion operators. It is set
11779 to -1 if the declarator is a name, and +1 if it is a
11780 function. Otherwise it is set to zero. Usually you just want to
11781 test for >0, but internally the negative value is used.
11783 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11784 a decl-specifier-seq unless it declares a constructor, destructor,
11785 or conversion. It might seem that we could check this condition in
11786 semantic analysis, rather than parsing, but that makes it difficult
11787 to handle something like `f()'. We want to notice that there are
11788 no decl-specifiers, and therefore realize that this is an
11789 expression, not a declaration.)
11791 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11792 the declarator is a direct-declarator of the form "(...)".
11794 MEMBER_P is true iff this declarator is a member-declarator. */
11796 static cp_declarator
*
11797 cp_parser_declarator (cp_parser
* parser
,
11798 cp_parser_declarator_kind dcl_kind
,
11799 int* ctor_dtor_or_conv_p
,
11800 bool* parenthesized_p
,
11804 cp_declarator
*declarator
;
11805 enum tree_code code
;
11806 cp_cv_quals cv_quals
;
11808 tree attributes
= NULL_TREE
;
11810 /* Assume this is not a constructor, destructor, or type-conversion
11812 if (ctor_dtor_or_conv_p
)
11813 *ctor_dtor_or_conv_p
= 0;
11815 if (cp_parser_allow_gnu_extensions_p (parser
))
11816 attributes
= cp_parser_attributes_opt (parser
);
11818 /* Peek at the next token. */
11819 token
= cp_lexer_peek_token (parser
->lexer
);
11821 /* Check for the ptr-operator production. */
11822 cp_parser_parse_tentatively (parser
);
11823 /* Parse the ptr-operator. */
11824 code
= cp_parser_ptr_operator (parser
,
11827 /* If that worked, then we have a ptr-operator. */
11828 if (cp_parser_parse_definitely (parser
))
11830 /* If a ptr-operator was found, then this declarator was not
11832 if (parenthesized_p
)
11833 *parenthesized_p
= true;
11834 /* The dependent declarator is optional if we are parsing an
11835 abstract-declarator. */
11836 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11837 cp_parser_parse_tentatively (parser
);
11839 /* Parse the dependent declarator. */
11840 declarator
= cp_parser_declarator (parser
, dcl_kind
,
11841 /*ctor_dtor_or_conv_p=*/NULL
,
11842 /*parenthesized_p=*/NULL
,
11843 /*member_p=*/false);
11845 /* If we are parsing an abstract-declarator, we must handle the
11846 case where the dependent declarator is absent. */
11847 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
11848 && !cp_parser_parse_definitely (parser
))
11851 /* Build the representation of the ptr-operator. */
11853 declarator
= make_ptrmem_declarator (cv_quals
,
11856 else if (code
== INDIRECT_REF
)
11857 declarator
= make_pointer_declarator (cv_quals
, declarator
);
11859 declarator
= make_reference_declarator (cv_quals
, declarator
);
11861 /* Everything else is a direct-declarator. */
11864 if (parenthesized_p
)
11865 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
11867 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
11868 ctor_dtor_or_conv_p
,
11872 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
11873 declarator
->attributes
= attributes
;
11878 /* Parse a direct-declarator or direct-abstract-declarator.
11882 direct-declarator ( parameter-declaration-clause )
11883 cv-qualifier-seq [opt]
11884 exception-specification [opt]
11885 direct-declarator [ constant-expression [opt] ]
11888 direct-abstract-declarator:
11889 direct-abstract-declarator [opt]
11890 ( parameter-declaration-clause )
11891 cv-qualifier-seq [opt]
11892 exception-specification [opt]
11893 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11894 ( abstract-declarator )
11896 Returns a representation of the declarator. DCL_KIND is
11897 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11898 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11899 we are parsing a direct-declarator. It is
11900 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11901 of ambiguity we prefer an abstract declarator, as per
11902 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11903 cp_parser_declarator. */
11905 static cp_declarator
*
11906 cp_parser_direct_declarator (cp_parser
* parser
,
11907 cp_parser_declarator_kind dcl_kind
,
11908 int* ctor_dtor_or_conv_p
,
11912 cp_declarator
*declarator
= NULL
;
11913 tree scope
= NULL_TREE
;
11914 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11915 bool saved_in_declarator_p
= parser
->in_declarator_p
;
11917 tree pushed_scope
= NULL_TREE
;
11921 /* Peek at the next token. */
11922 token
= cp_lexer_peek_token (parser
->lexer
);
11923 if (token
->type
== CPP_OPEN_PAREN
)
11925 /* This is either a parameter-declaration-clause, or a
11926 parenthesized declarator. When we know we are parsing a
11927 named declarator, it must be a parenthesized declarator
11928 if FIRST is true. For instance, `(int)' is a
11929 parameter-declaration-clause, with an omitted
11930 direct-abstract-declarator. But `((*))', is a
11931 parenthesized abstract declarator. Finally, when T is a
11932 template parameter `(T)' is a
11933 parameter-declaration-clause, and not a parenthesized
11936 We first try and parse a parameter-declaration-clause,
11937 and then try a nested declarator (if FIRST is true).
11939 It is not an error for it not to be a
11940 parameter-declaration-clause, even when FIRST is
11946 The first is the declaration of a function while the
11947 second is a the definition of a variable, including its
11950 Having seen only the parenthesis, we cannot know which of
11951 these two alternatives should be selected. Even more
11952 complex are examples like:
11957 The former is a function-declaration; the latter is a
11958 variable initialization.
11960 Thus again, we try a parameter-declaration-clause, and if
11961 that fails, we back out and return. */
11963 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11965 cp_parameter_declarator
*params
;
11966 unsigned saved_num_template_parameter_lists
;
11968 /* In a member-declarator, the only valid interpretation
11969 of a parenthesis is the start of a
11970 parameter-declaration-clause. (It is invalid to
11971 initialize a static data member with a parenthesized
11972 initializer; only the "=" form of initialization is
11975 cp_parser_parse_tentatively (parser
);
11977 /* Consume the `('. */
11978 cp_lexer_consume_token (parser
->lexer
);
11981 /* If this is going to be an abstract declarator, we're
11982 in a declarator and we can't have default args. */
11983 parser
->default_arg_ok_p
= false;
11984 parser
->in_declarator_p
= true;
11987 /* Inside the function parameter list, surrounding
11988 template-parameter-lists do not apply. */
11989 saved_num_template_parameter_lists
11990 = parser
->num_template_parameter_lists
;
11991 parser
->num_template_parameter_lists
= 0;
11993 /* Parse the parameter-declaration-clause. */
11994 params
= cp_parser_parameter_declaration_clause (parser
);
11996 parser
->num_template_parameter_lists
11997 = saved_num_template_parameter_lists
;
11999 /* If all went well, parse the cv-qualifier-seq and the
12000 exception-specification. */
12001 if (member_p
|| cp_parser_parse_definitely (parser
))
12003 cp_cv_quals cv_quals
;
12004 tree exception_specification
;
12006 if (ctor_dtor_or_conv_p
)
12007 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
12009 /* Consume the `)'. */
12010 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
12012 /* Parse the cv-qualifier-seq. */
12013 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
12014 /* And the exception-specification. */
12015 exception_specification
12016 = cp_parser_exception_specification_opt (parser
);
12018 /* Create the function-declarator. */
12019 declarator
= make_call_declarator (declarator
,
12022 exception_specification
);
12023 /* Any subsequent parameter lists are to do with
12024 return type, so are not those of the declared
12026 parser
->default_arg_ok_p
= false;
12028 /* Repeat the main loop. */
12033 /* If this is the first, we can try a parenthesized
12037 bool saved_in_type_id_in_expr_p
;
12039 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12040 parser
->in_declarator_p
= saved_in_declarator_p
;
12042 /* Consume the `('. */
12043 cp_lexer_consume_token (parser
->lexer
);
12044 /* Parse the nested declarator. */
12045 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
12046 parser
->in_type_id_in_expr_p
= true;
12048 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
12049 /*parenthesized_p=*/NULL
,
12051 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
12053 /* Expect a `)'. */
12054 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
12055 declarator
= cp_error_declarator
;
12056 if (declarator
== cp_error_declarator
)
12059 goto handle_declarator
;
12061 /* Otherwise, we must be done. */
12065 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
12066 && token
->type
== CPP_OPEN_SQUARE
)
12068 /* Parse an array-declarator. */
12071 if (ctor_dtor_or_conv_p
)
12072 *ctor_dtor_or_conv_p
= 0;
12075 parser
->default_arg_ok_p
= false;
12076 parser
->in_declarator_p
= true;
12077 /* Consume the `['. */
12078 cp_lexer_consume_token (parser
->lexer
);
12079 /* Peek at the next token. */
12080 token
= cp_lexer_peek_token (parser
->lexer
);
12081 /* If the next token is `]', then there is no
12082 constant-expression. */
12083 if (token
->type
!= CPP_CLOSE_SQUARE
)
12085 bool non_constant_p
;
12088 = cp_parser_constant_expression (parser
,
12089 /*allow_non_constant=*/true,
12091 if (!non_constant_p
)
12092 bounds
= fold_non_dependent_expr (bounds
);
12093 /* Normally, the array bound must be an integral constant
12094 expression. However, as an extension, we allow VLAs
12095 in function scopes. */
12096 else if (!parser
->in_function_body
)
12098 error ("array bound is not an integer constant");
12099 bounds
= error_mark_node
;
12103 bounds
= NULL_TREE
;
12104 /* Look for the closing `]'. */
12105 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
12107 declarator
= cp_error_declarator
;
12111 declarator
= make_array_declarator (declarator
, bounds
);
12113 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
12115 tree qualifying_scope
;
12116 tree unqualified_name
;
12117 special_function_kind sfk
;
12119 bool pack_expansion_p
= false;
12121 /* Parse a declarator-id */
12122 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
12125 cp_parser_parse_tentatively (parser
);
12127 /* If we see an ellipsis, we should be looking at a
12129 if (token
->type
== CPP_ELLIPSIS
)
12131 /* Consume the `...' */
12132 cp_lexer_consume_token (parser
->lexer
);
12134 pack_expansion_p
= true;
12139 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
12140 qualifying_scope
= parser
->scope
;
12145 if (!unqualified_name
&& pack_expansion_p
)
12147 /* Check whether an error occurred. */
12148 okay
= !cp_parser_error_occurred (parser
);
12150 /* We already consumed the ellipsis to mark a
12151 parameter pack, but we have no way to report it,
12152 so abort the tentative parse. We will be exiting
12153 immediately anyway. */
12154 cp_parser_abort_tentative_parse (parser
);
12157 okay
= cp_parser_parse_definitely (parser
);
12160 unqualified_name
= error_mark_node
;
12161 else if (unqualified_name
12162 && (qualifying_scope
12163 || (TREE_CODE (unqualified_name
)
12164 != IDENTIFIER_NODE
)))
12166 cp_parser_error (parser
, "expected unqualified-id");
12167 unqualified_name
= error_mark_node
;
12171 if (!unqualified_name
)
12173 if (unqualified_name
== error_mark_node
)
12175 declarator
= cp_error_declarator
;
12176 pack_expansion_p
= false;
12177 declarator
->parameter_pack_p
= false;
12181 if (qualifying_scope
&& at_namespace_scope_p ()
12182 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
12184 /* In the declaration of a member of a template class
12185 outside of the class itself, the SCOPE will sometimes
12186 be a TYPENAME_TYPE. For example, given:
12188 template <typename T>
12189 int S<T>::R::i = 3;
12191 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12192 this context, we must resolve S<T>::R to an ordinary
12193 type, rather than a typename type.
12195 The reason we normally avoid resolving TYPENAME_TYPEs
12196 is that a specialization of `S' might render
12197 `S<T>::R' not a type. However, if `S' is
12198 specialized, then this `i' will not be used, so there
12199 is no harm in resolving the types here. */
12202 /* Resolve the TYPENAME_TYPE. */
12203 type
= resolve_typename_type (qualifying_scope
,
12204 /*only_current_p=*/false);
12205 /* If that failed, the declarator is invalid. */
12206 if (type
== error_mark_node
)
12207 error ("%<%T::%E%> is not a type",
12208 TYPE_CONTEXT (qualifying_scope
),
12209 TYPE_IDENTIFIER (qualifying_scope
));
12210 qualifying_scope
= type
;
12215 if (unqualified_name
)
12219 if (qualifying_scope
12220 && CLASS_TYPE_P (qualifying_scope
))
12221 class_type
= qualifying_scope
;
12223 class_type
= current_class_type
;
12225 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
12227 tree name_type
= TREE_TYPE (unqualified_name
);
12228 if (class_type
&& same_type_p (name_type
, class_type
))
12230 if (qualifying_scope
12231 && CLASSTYPE_USE_TEMPLATE (name_type
))
12233 error ("invalid use of constructor as a template");
12234 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12235 "name the constructor in a qualified name",
12237 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
12238 class_type
, name_type
);
12239 declarator
= cp_error_declarator
;
12243 unqualified_name
= constructor_name (class_type
);
12247 /* We do not attempt to print the declarator
12248 here because we do not have enough
12249 information about its original syntactic
12251 cp_parser_error (parser
, "invalid declarator");
12252 declarator
= cp_error_declarator
;
12259 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
12260 sfk
= sfk_destructor
;
12261 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
12262 sfk
= sfk_conversion
;
12263 else if (/* There's no way to declare a constructor
12264 for an anonymous type, even if the type
12265 got a name for linkage purposes. */
12266 !TYPE_WAS_ANONYMOUS (class_type
)
12267 && constructor_name_p (unqualified_name
,
12270 unqualified_name
= constructor_name (class_type
);
12271 sfk
= sfk_constructor
;
12274 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
12275 *ctor_dtor_or_conv_p
= -1;
12278 declarator
= make_id_declarator (qualifying_scope
,
12281 declarator
->id_loc
= token
->location
;
12282 declarator
->parameter_pack_p
= pack_expansion_p
;
12284 if (pack_expansion_p
)
12285 maybe_warn_variadic_templates ();
12287 handle_declarator
:;
12288 scope
= get_scope_of_declarator (declarator
);
12290 /* Any names that appear after the declarator-id for a
12291 member are looked up in the containing scope. */
12292 pushed_scope
= push_scope (scope
);
12293 parser
->in_declarator_p
= true;
12294 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
12295 || (declarator
&& declarator
->kind
== cdk_id
))
12296 /* Default args are only allowed on function
12298 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12300 parser
->default_arg_ok_p
= false;
12309 /* For an abstract declarator, we might wind up with nothing at this
12310 point. That's an error; the declarator is not optional. */
12312 cp_parser_error (parser
, "expected declarator");
12314 /* If we entered a scope, we must exit it now. */
12316 pop_scope (pushed_scope
);
12318 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12319 parser
->in_declarator_p
= saved_in_declarator_p
;
12324 /* Parse a ptr-operator.
12327 * cv-qualifier-seq [opt]
12329 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12334 & cv-qualifier-seq [opt]
12336 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12337 Returns ADDR_EXPR if a reference was used. In the case of a
12338 pointer-to-member, *TYPE is filled in with the TYPE containing the
12339 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
12340 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
12341 ERROR_MARK if an error occurred. */
12343 static enum tree_code
12344 cp_parser_ptr_operator (cp_parser
* parser
,
12346 cp_cv_quals
*cv_quals
)
12348 enum tree_code code
= ERROR_MARK
;
12351 /* Assume that it's not a pointer-to-member. */
12353 /* And that there are no cv-qualifiers. */
12354 *cv_quals
= TYPE_UNQUALIFIED
;
12356 /* Peek at the next token. */
12357 token
= cp_lexer_peek_token (parser
->lexer
);
12358 /* If it's a `*' or `&' we have a pointer or reference. */
12359 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
12361 /* Remember which ptr-operator we were processing. */
12362 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
12364 /* Consume the `*' or `&'. */
12365 cp_lexer_consume_token (parser
->lexer
);
12367 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12368 `&', if we are allowing GNU extensions. (The only qualifier
12369 that can legally appear after `&' is `restrict', but that is
12370 enforced during semantic analysis. */
12371 if (code
== INDIRECT_REF
12372 || cp_parser_allow_gnu_extensions_p (parser
))
12373 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
12377 /* Try the pointer-to-member case. */
12378 cp_parser_parse_tentatively (parser
);
12379 /* Look for the optional `::' operator. */
12380 cp_parser_global_scope_opt (parser
,
12381 /*current_scope_valid_p=*/false);
12382 /* Look for the nested-name specifier. */
12383 cp_parser_nested_name_specifier (parser
,
12384 /*typename_keyword_p=*/false,
12385 /*check_dependency_p=*/true,
12387 /*is_declaration=*/false);
12388 /* If we found it, and the next token is a `*', then we are
12389 indeed looking at a pointer-to-member operator. */
12390 if (!cp_parser_error_occurred (parser
)
12391 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
12393 /* Indicate that the `*' operator was used. */
12394 code
= INDIRECT_REF
;
12396 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
12397 error ("%qD is a namespace", parser
->scope
);
12400 /* The type of which the member is a member is given by the
12402 *type
= parser
->scope
;
12403 /* The next name will not be qualified. */
12404 parser
->scope
= NULL_TREE
;
12405 parser
->qualifying_scope
= NULL_TREE
;
12406 parser
->object_scope
= NULL_TREE
;
12407 /* Look for the optional cv-qualifier-seq. */
12408 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
12411 /* If that didn't work we don't have a ptr-operator. */
12412 if (!cp_parser_parse_definitely (parser
))
12413 cp_parser_error (parser
, "expected ptr-operator");
12419 /* Parse an (optional) cv-qualifier-seq.
12422 cv-qualifier cv-qualifier-seq [opt]
12433 Returns a bitmask representing the cv-qualifiers. */
12436 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
12438 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
12443 cp_cv_quals cv_qualifier
;
12445 /* Peek at the next token. */
12446 token
= cp_lexer_peek_token (parser
->lexer
);
12447 /* See if it's a cv-qualifier. */
12448 switch (token
->keyword
)
12451 cv_qualifier
= TYPE_QUAL_CONST
;
12455 cv_qualifier
= TYPE_QUAL_VOLATILE
;
12459 cv_qualifier
= TYPE_QUAL_RESTRICT
;
12463 cv_qualifier
= TYPE_UNQUALIFIED
;
12470 if (cv_quals
& cv_qualifier
)
12472 error ("duplicate cv-qualifier");
12473 cp_lexer_purge_token (parser
->lexer
);
12477 cp_lexer_consume_token (parser
->lexer
);
12478 cv_quals
|= cv_qualifier
;
12485 /* Parse a declarator-id.
12489 :: [opt] nested-name-specifier [opt] type-name
12491 In the `id-expression' case, the value returned is as for
12492 cp_parser_id_expression if the id-expression was an unqualified-id.
12493 If the id-expression was a qualified-id, then a SCOPE_REF is
12494 returned. The first operand is the scope (either a NAMESPACE_DECL
12495 or TREE_TYPE), but the second is still just a representation of an
12499 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
12502 /* The expression must be an id-expression. Assume that qualified
12503 names are the names of types so that:
12506 int S<T>::R::i = 3;
12508 will work; we must treat `S<T>::R' as the name of a type.
12509 Similarly, assume that qualified names are templates, where
12513 int S<T>::R<T>::i = 3;
12516 id
= cp_parser_id_expression (parser
,
12517 /*template_keyword_p=*/false,
12518 /*check_dependency_p=*/false,
12519 /*template_p=*/NULL
,
12520 /*declarator_p=*/true,
12522 if (id
&& BASELINK_P (id
))
12523 id
= BASELINK_FUNCTIONS (id
);
12527 /* Parse a type-id.
12530 type-specifier-seq abstract-declarator [opt]
12532 Returns the TYPE specified. */
12535 cp_parser_type_id (cp_parser
* parser
)
12537 cp_decl_specifier_seq type_specifier_seq
;
12538 cp_declarator
*abstract_declarator
;
12540 /* Parse the type-specifier-seq. */
12541 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
12542 &type_specifier_seq
);
12543 if (type_specifier_seq
.type
== error_mark_node
)
12544 return error_mark_node
;
12546 /* There might or might not be an abstract declarator. */
12547 cp_parser_parse_tentatively (parser
);
12548 /* Look for the declarator. */
12549 abstract_declarator
12550 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
12551 /*parenthesized_p=*/NULL
,
12552 /*member_p=*/false);
12553 /* Check to see if there really was a declarator. */
12554 if (!cp_parser_parse_definitely (parser
))
12555 abstract_declarator
= NULL
;
12557 return groktypename (&type_specifier_seq
, abstract_declarator
);
12560 /* Parse a type-specifier-seq.
12562 type-specifier-seq:
12563 type-specifier type-specifier-seq [opt]
12567 type-specifier-seq:
12568 attributes type-specifier-seq [opt]
12570 If IS_CONDITION is true, we are at the start of a "condition",
12571 e.g., we've just seen "if (".
12573 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12576 cp_parser_type_specifier_seq (cp_parser
* parser
,
12578 cp_decl_specifier_seq
*type_specifier_seq
)
12580 bool seen_type_specifier
= false;
12581 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
12583 /* Clear the TYPE_SPECIFIER_SEQ. */
12584 clear_decl_specs (type_specifier_seq
);
12586 /* Parse the type-specifiers and attributes. */
12589 tree type_specifier
;
12590 bool is_cv_qualifier
;
12592 /* Check for attributes first. */
12593 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
12595 type_specifier_seq
->attributes
=
12596 chainon (type_specifier_seq
->attributes
,
12597 cp_parser_attributes_opt (parser
));
12601 /* Look for the type-specifier. */
12602 type_specifier
= cp_parser_type_specifier (parser
,
12604 type_specifier_seq
,
12605 /*is_declaration=*/false,
12608 if (!type_specifier
)
12610 /* If the first type-specifier could not be found, this is not a
12611 type-specifier-seq at all. */
12612 if (!seen_type_specifier
)
12614 cp_parser_error (parser
, "expected type-specifier");
12615 type_specifier_seq
->type
= error_mark_node
;
12618 /* If subsequent type-specifiers could not be found, the
12619 type-specifier-seq is complete. */
12623 seen_type_specifier
= true;
12624 /* The standard says that a condition can be:
12626 type-specifier-seq declarator = assignment-expression
12633 we should treat the "S" as a declarator, not as a
12634 type-specifier. The standard doesn't say that explicitly for
12635 type-specifier-seq, but it does say that for
12636 decl-specifier-seq in an ordinary declaration. Perhaps it
12637 would be clearer just to allow a decl-specifier-seq here, and
12638 then add a semantic restriction that if any decl-specifiers
12639 that are not type-specifiers appear, the program is invalid. */
12640 if (is_condition
&& !is_cv_qualifier
)
12641 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
12644 cp_parser_check_decl_spec (type_specifier_seq
);
12647 /* Parse a parameter-declaration-clause.
12649 parameter-declaration-clause:
12650 parameter-declaration-list [opt] ... [opt]
12651 parameter-declaration-list , ...
12653 Returns a representation for the parameter declarations. A return
12654 value of NULL indicates a parameter-declaration-clause consisting
12655 only of an ellipsis. */
12657 static cp_parameter_declarator
*
12658 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
12660 cp_parameter_declarator
*parameters
;
12665 /* Peek at the next token. */
12666 token
= cp_lexer_peek_token (parser
->lexer
);
12667 /* Check for trivial parameter-declaration-clauses. */
12668 if (token
->type
== CPP_ELLIPSIS
)
12670 /* Consume the `...' token. */
12671 cp_lexer_consume_token (parser
->lexer
);
12674 else if (token
->type
== CPP_CLOSE_PAREN
)
12675 /* There are no parameters. */
12677 #ifndef NO_IMPLICIT_EXTERN_C
12678 if (in_system_header
&& current_class_type
== NULL
12679 && current_lang_name
== lang_name_c
)
12683 return no_parameters
;
12685 /* Check for `(void)', too, which is a special case. */
12686 else if (token
->keyword
== RID_VOID
12687 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
12688 == CPP_CLOSE_PAREN
))
12690 /* Consume the `void' token. */
12691 cp_lexer_consume_token (parser
->lexer
);
12692 /* There are no parameters. */
12693 return no_parameters
;
12696 /* Parse the parameter-declaration-list. */
12697 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
12698 /* If a parse error occurred while parsing the
12699 parameter-declaration-list, then the entire
12700 parameter-declaration-clause is erroneous. */
12704 /* Peek at the next token. */
12705 token
= cp_lexer_peek_token (parser
->lexer
);
12706 /* If it's a `,', the clause should terminate with an ellipsis. */
12707 if (token
->type
== CPP_COMMA
)
12709 /* Consume the `,'. */
12710 cp_lexer_consume_token (parser
->lexer
);
12711 /* Expect an ellipsis. */
12713 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
12715 /* It might also be `...' if the optional trailing `,' was
12717 else if (token
->type
== CPP_ELLIPSIS
)
12719 /* Consume the `...' token. */
12720 cp_lexer_consume_token (parser
->lexer
);
12721 /* And remember that we saw it. */
12725 ellipsis_p
= false;
12727 /* Finish the parameter list. */
12728 if (parameters
&& ellipsis_p
)
12729 parameters
->ellipsis_p
= true;
12734 /* Parse a parameter-declaration-list.
12736 parameter-declaration-list:
12737 parameter-declaration
12738 parameter-declaration-list , parameter-declaration
12740 Returns a representation of the parameter-declaration-list, as for
12741 cp_parser_parameter_declaration_clause. However, the
12742 `void_list_node' is never appended to the list. Upon return,
12743 *IS_ERROR will be true iff an error occurred. */
12745 static cp_parameter_declarator
*
12746 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
12748 cp_parameter_declarator
*parameters
= NULL
;
12749 cp_parameter_declarator
**tail
= ¶meters
;
12750 bool saved_in_unbraced_linkage_specification_p
;
12752 /* Assume all will go well. */
12754 /* The special considerations that apply to a function within an
12755 unbraced linkage specifications do not apply to the parameters
12756 to the function. */
12757 saved_in_unbraced_linkage_specification_p
12758 = parser
->in_unbraced_linkage_specification_p
;
12759 parser
->in_unbraced_linkage_specification_p
= false;
12761 /* Look for more parameters. */
12764 cp_parameter_declarator
*parameter
;
12765 bool parenthesized_p
;
12766 /* Parse the parameter. */
12768 = cp_parser_parameter_declaration (parser
,
12769 /*template_parm_p=*/false,
12772 /* If a parse error occurred parsing the parameter declaration,
12773 then the entire parameter-declaration-list is erroneous. */
12780 /* Add the new parameter to the list. */
12782 tail
= ¶meter
->next
;
12784 /* Peek at the next token. */
12785 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
12786 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
12787 /* These are for Objective-C++ */
12788 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
12789 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12790 /* The parameter-declaration-list is complete. */
12792 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12796 /* Peek at the next token. */
12797 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12798 /* If it's an ellipsis, then the list is complete. */
12799 if (token
->type
== CPP_ELLIPSIS
)
12801 /* Otherwise, there must be more parameters. Consume the
12803 cp_lexer_consume_token (parser
->lexer
);
12804 /* When parsing something like:
12806 int i(float f, double d)
12808 we can tell after seeing the declaration for "f" that we
12809 are not looking at an initialization of a variable "i",
12810 but rather at the declaration of a function "i".
12812 Due to the fact that the parsing of template arguments
12813 (as specified to a template-id) requires backtracking we
12814 cannot use this technique when inside a template argument
12816 if (!parser
->in_template_argument_list_p
12817 && !parser
->in_type_id_in_expr_p
12818 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12819 /* However, a parameter-declaration of the form
12820 "foat(f)" (which is a valid declaration of a
12821 parameter "f") can also be interpreted as an
12822 expression (the conversion of "f" to "float"). */
12823 && !parenthesized_p
)
12824 cp_parser_commit_to_tentative_parse (parser
);
12828 cp_parser_error (parser
, "expected %<,%> or %<...%>");
12829 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
12830 cp_parser_skip_to_closing_parenthesis (parser
,
12831 /*recovering=*/true,
12832 /*or_comma=*/false,
12833 /*consume_paren=*/false);
12838 parser
->in_unbraced_linkage_specification_p
12839 = saved_in_unbraced_linkage_specification_p
;
12844 /* Parse a parameter declaration.
12846 parameter-declaration:
12847 decl-specifier-seq ... [opt] declarator
12848 decl-specifier-seq declarator = assignment-expression
12849 decl-specifier-seq ... [opt] abstract-declarator [opt]
12850 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12852 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12853 declares a template parameter. (In that case, a non-nested `>'
12854 token encountered during the parsing of the assignment-expression
12855 is not interpreted as a greater-than operator.)
12857 Returns a representation of the parameter, or NULL if an error
12858 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12859 true iff the declarator is of the form "(p)". */
12861 static cp_parameter_declarator
*
12862 cp_parser_parameter_declaration (cp_parser
*parser
,
12863 bool template_parm_p
,
12864 bool *parenthesized_p
)
12866 int declares_class_or_enum
;
12867 bool greater_than_is_operator_p
;
12868 cp_decl_specifier_seq decl_specifiers
;
12869 cp_declarator
*declarator
;
12870 tree default_argument
;
12872 const char *saved_message
;
12874 /* In a template parameter, `>' is not an operator.
12878 When parsing a default template-argument for a non-type
12879 template-parameter, the first non-nested `>' is taken as the end
12880 of the template parameter-list rather than a greater-than
12882 greater_than_is_operator_p
= !template_parm_p
;
12884 /* Type definitions may not appear in parameter types. */
12885 saved_message
= parser
->type_definition_forbidden_message
;
12886 parser
->type_definition_forbidden_message
12887 = "types may not be defined in parameter types";
12889 /* Parse the declaration-specifiers. */
12890 cp_parser_decl_specifier_seq (parser
,
12891 CP_PARSER_FLAGS_NONE
,
12893 &declares_class_or_enum
);
12894 /* If an error occurred, there's no reason to attempt to parse the
12895 rest of the declaration. */
12896 if (cp_parser_error_occurred (parser
))
12898 parser
->type_definition_forbidden_message
= saved_message
;
12902 /* Peek at the next token. */
12903 token
= cp_lexer_peek_token (parser
->lexer
);
12905 /* If the next token is a `)', `,', `=', `>', or `...', then there
12906 is no declarator. However, when variadic templates are enabled,
12907 there may be a declarator following `...'. */
12908 if (token
->type
== CPP_CLOSE_PAREN
12909 || token
->type
== CPP_COMMA
12910 || token
->type
== CPP_EQ
12911 || token
->type
== CPP_GREATER
)
12914 if (parenthesized_p
)
12915 *parenthesized_p
= false;
12917 /* Otherwise, there should be a declarator. */
12920 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
12921 parser
->default_arg_ok_p
= false;
12923 /* After seeing a decl-specifier-seq, if the next token is not a
12924 "(", there is no possibility that the code is a valid
12925 expression. Therefore, if parsing tentatively, we commit at
12927 if (!parser
->in_template_argument_list_p
12928 /* In an expression context, having seen:
12932 we cannot be sure whether we are looking at a
12933 function-type (taking a "char" as a parameter) or a cast
12934 of some object of type "char" to "int". */
12935 && !parser
->in_type_id_in_expr_p
12936 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12937 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
12938 cp_parser_commit_to_tentative_parse (parser
);
12939 /* Parse the declarator. */
12940 declarator
= cp_parser_declarator (parser
,
12941 CP_PARSER_DECLARATOR_EITHER
,
12942 /*ctor_dtor_or_conv_p=*/NULL
,
12944 /*member_p=*/false);
12945 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12946 /* After the declarator, allow more attributes. */
12947 decl_specifiers
.attributes
12948 = chainon (decl_specifiers
.attributes
,
12949 cp_parser_attributes_opt (parser
));
12952 /* If the next token is an ellipsis, and the type of the declarator
12953 contains parameter packs but it is not a TYPE_PACK_EXPANSION, then
12954 we actually have a parameter pack expansion expression. Otherwise,
12955 leave the ellipsis for a C-style variadic function. */
12956 token
= cp_lexer_peek_token (parser
->lexer
);
12957 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12959 tree type
= decl_specifiers
.type
;
12962 type
= TREE_TYPE (type
);
12964 if (TREE_CODE (type
) != TYPE_PACK_EXPANSION
12965 && (!declarator
|| !declarator
->parameter_pack_p
)
12966 && uses_parameter_packs (type
))
12968 /* Consume the `...'. */
12969 cp_lexer_consume_token (parser
->lexer
);
12970 maybe_warn_variadic_templates ();
12972 /* Build a pack expansion type */
12974 declarator
->parameter_pack_p
= true;
12976 decl_specifiers
.type
= make_pack_expansion (type
);
12980 /* The restriction on defining new types applies only to the type
12981 of the parameter, not to the default argument. */
12982 parser
->type_definition_forbidden_message
= saved_message
;
12984 /* If the next token is `=', then process a default argument. */
12985 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12987 bool saved_greater_than_is_operator_p
;
12988 /* Consume the `='. */
12989 cp_lexer_consume_token (parser
->lexer
);
12991 /* If we are defining a class, then the tokens that make up the
12992 default argument must be saved and processed later. */
12993 if (!template_parm_p
&& at_class_scope_p ()
12994 && TYPE_BEING_DEFINED (current_class_type
))
12996 unsigned depth
= 0;
12997 cp_token
*first_token
;
13000 /* Add tokens until we have processed the entire default
13001 argument. We add the range [first_token, token). */
13002 first_token
= cp_lexer_peek_token (parser
->lexer
);
13007 /* Peek at the next token. */
13008 token
= cp_lexer_peek_token (parser
->lexer
);
13009 /* What we do depends on what token we have. */
13010 switch (token
->type
)
13012 /* In valid code, a default argument must be
13013 immediately followed by a `,' `)', or `...'. */
13015 case CPP_CLOSE_PAREN
:
13017 /* If we run into a non-nested `;', `}', or `]',
13018 then the code is invalid -- but the default
13019 argument is certainly over. */
13020 case CPP_SEMICOLON
:
13021 case CPP_CLOSE_BRACE
:
13022 case CPP_CLOSE_SQUARE
:
13025 /* Update DEPTH, if necessary. */
13026 else if (token
->type
== CPP_CLOSE_PAREN
13027 || token
->type
== CPP_CLOSE_BRACE
13028 || token
->type
== CPP_CLOSE_SQUARE
)
13032 case CPP_OPEN_PAREN
:
13033 case CPP_OPEN_SQUARE
:
13034 case CPP_OPEN_BRACE
:
13041 /* Fall through for C++0x, which treats the `>>'
13042 operator like two `>' tokens in certain
13046 /* If we see a non-nested `>', and `>' is not an
13047 operator, then it marks the end of the default
13049 if (!depth
&& !greater_than_is_operator_p
)
13053 /* If we run out of tokens, issue an error message. */
13055 case CPP_PRAGMA_EOL
:
13056 error ("file ends in default argument");
13062 /* In these cases, we should look for template-ids.
13063 For example, if the default argument is
13064 `X<int, double>()', we need to do name lookup to
13065 figure out whether or not `X' is a template; if
13066 so, the `,' does not end the default argument.
13068 That is not yet done. */
13075 /* If we've reached the end, stop. */
13079 /* Add the token to the token block. */
13080 token
= cp_lexer_consume_token (parser
->lexer
);
13083 /* Create a DEFAULT_ARG to represented the unparsed default
13085 default_argument
= make_node (DEFAULT_ARG
);
13086 DEFARG_TOKENS (default_argument
)
13087 = cp_token_cache_new (first_token
, token
);
13088 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
13090 /* Outside of a class definition, we can just parse the
13091 assignment-expression. */
13094 bool saved_local_variables_forbidden_p
;
13096 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13098 saved_greater_than_is_operator_p
13099 = parser
->greater_than_is_operator_p
;
13100 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
13101 /* Local variable names (and the `this' keyword) may not
13102 appear in a default argument. */
13103 saved_local_variables_forbidden_p
13104 = parser
->local_variables_forbidden_p
;
13105 parser
->local_variables_forbidden_p
= true;
13106 /* The default argument expression may cause implicitly
13107 defined member functions to be synthesized, which will
13108 result in garbage collection. We must treat this
13109 situation as if we were within the body of function so as
13110 to avoid collecting live data on the stack. */
13112 /* Parse the assignment-expression. */
13113 if (template_parm_p
)
13114 push_deferring_access_checks (dk_no_deferred
);
13116 = cp_parser_assignment_expression (parser
, /*cast_p=*/false);
13117 if (template_parm_p
)
13118 pop_deferring_access_checks ();
13119 /* Restore saved state. */
13121 parser
->greater_than_is_operator_p
13122 = saved_greater_than_is_operator_p
;
13123 parser
->local_variables_forbidden_p
13124 = saved_local_variables_forbidden_p
;
13126 if (!parser
->default_arg_ok_p
)
13128 if (!flag_pedantic_errors
)
13129 warning (0, "deprecated use of default argument for parameter of non-function");
13132 error ("default arguments are only permitted for function parameters");
13133 default_argument
= NULL_TREE
;
13138 default_argument
= NULL_TREE
;
13140 return make_parameter_declarator (&decl_specifiers
,
13145 /* Parse a function-body.
13148 compound_statement */
13151 cp_parser_function_body (cp_parser
*parser
)
13153 cp_parser_compound_statement (parser
, NULL
, false);
13156 /* Parse a ctor-initializer-opt followed by a function-body. Return
13157 true if a ctor-initializer was present. */
13160 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
13163 bool ctor_initializer_p
;
13165 /* Begin the function body. */
13166 body
= begin_function_body ();
13167 /* Parse the optional ctor-initializer. */
13168 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
13169 /* Parse the function-body. */
13170 cp_parser_function_body (parser
);
13171 /* Finish the function body. */
13172 finish_function_body (body
);
13174 return ctor_initializer_p
;
13177 /* Parse an initializer.
13180 = initializer-clause
13181 ( expression-list )
13183 Returns an expression representing the initializer. If no
13184 initializer is present, NULL_TREE is returned.
13186 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13187 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13188 set to FALSE if there is no initializer present. If there is an
13189 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13190 is set to true; otherwise it is set to false. */
13193 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
13194 bool* non_constant_p
)
13199 /* Peek at the next token. */
13200 token
= cp_lexer_peek_token (parser
->lexer
);
13202 /* Let our caller know whether or not this initializer was
13204 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
13205 /* Assume that the initializer is constant. */
13206 *non_constant_p
= false;
13208 if (token
->type
== CPP_EQ
)
13210 /* Consume the `='. */
13211 cp_lexer_consume_token (parser
->lexer
);
13212 /* Parse the initializer-clause. */
13213 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
13215 else if (token
->type
== CPP_OPEN_PAREN
)
13216 init
= cp_parser_parenthesized_expression_list (parser
, false,
13218 /*allow_expansion_p=*/true,
13222 /* Anything else is an error. */
13223 cp_parser_error (parser
, "expected initializer");
13224 init
= error_mark_node
;
13230 /* Parse an initializer-clause.
13232 initializer-clause:
13233 assignment-expression
13234 { initializer-list , [opt] }
13237 Returns an expression representing the initializer.
13239 If the `assignment-expression' production is used the value
13240 returned is simply a representation for the expression.
13242 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13243 the elements of the initializer-list (or NULL, if the last
13244 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13245 NULL_TREE. There is no way to detect whether or not the optional
13246 trailing `,' was provided. NON_CONSTANT_P is as for
13247 cp_parser_initializer. */
13250 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
13254 /* Assume the expression is constant. */
13255 *non_constant_p
= false;
13257 /* If it is not a `{', then we are looking at an
13258 assignment-expression. */
13259 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13262 = cp_parser_constant_expression (parser
,
13263 /*allow_non_constant_p=*/true,
13265 if (!*non_constant_p
)
13266 initializer
= fold_non_dependent_expr (initializer
);
13270 /* Consume the `{' token. */
13271 cp_lexer_consume_token (parser
->lexer
);
13272 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13273 initializer
= make_node (CONSTRUCTOR
);
13274 /* If it's not a `}', then there is a non-trivial initializer. */
13275 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
13277 /* Parse the initializer list. */
13278 CONSTRUCTOR_ELTS (initializer
)
13279 = cp_parser_initializer_list (parser
, non_constant_p
);
13280 /* A trailing `,' token is allowed. */
13281 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13282 cp_lexer_consume_token (parser
->lexer
);
13284 /* Now, there should be a trailing `}'. */
13285 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13288 return initializer
;
13291 /* Parse an initializer-list.
13294 initializer-clause ... [opt]
13295 initializer-list , initializer-clause ... [opt]
13300 identifier : initializer-clause
13301 initializer-list, identifier : initializer-clause
13303 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13304 for the initializer. If the INDEX of the elt is non-NULL, it is the
13305 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13306 as for cp_parser_initializer. */
13308 static VEC(constructor_elt
,gc
) *
13309 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
13311 VEC(constructor_elt
,gc
) *v
= NULL
;
13313 /* Assume all of the expressions are constant. */
13314 *non_constant_p
= false;
13316 /* Parse the rest of the list. */
13322 bool clause_non_constant_p
;
13324 /* If the next token is an identifier and the following one is a
13325 colon, we are looking at the GNU designated-initializer
13327 if (cp_parser_allow_gnu_extensions_p (parser
)
13328 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
13329 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
13331 /* Warn the user that they are using an extension. */
13333 pedwarn ("ISO C++ does not allow designated initializers");
13334 /* Consume the identifier. */
13335 identifier
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13336 /* Consume the `:'. */
13337 cp_lexer_consume_token (parser
->lexer
);
13340 identifier
= NULL_TREE
;
13342 /* Parse the initializer. */
13343 initializer
= cp_parser_initializer_clause (parser
,
13344 &clause_non_constant_p
);
13345 /* If any clause is non-constant, so is the entire initializer. */
13346 if (clause_non_constant_p
)
13347 *non_constant_p
= true;
13349 /* If we have an ellipsis, this is an initializer pack
13351 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13353 /* Consume the `...'. */
13354 cp_lexer_consume_token (parser
->lexer
);
13356 /* Turn the initializer into an initializer expansion. */
13357 initializer
= make_pack_expansion (initializer
);
13360 /* Add it to the vector. */
13361 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
13363 /* If the next token is not a comma, we have reached the end of
13365 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13368 /* Peek at the next token. */
13369 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13370 /* If the next token is a `}', then we're still done. An
13371 initializer-clause can have a trailing `,' after the
13372 initializer-list and before the closing `}'. */
13373 if (token
->type
== CPP_CLOSE_BRACE
)
13376 /* Consume the `,' token. */
13377 cp_lexer_consume_token (parser
->lexer
);
13383 /* Classes [gram.class] */
13385 /* Parse a class-name.
13391 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13392 to indicate that names looked up in dependent types should be
13393 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13394 keyword has been used to indicate that the name that appears next
13395 is a template. TAG_TYPE indicates the explicit tag given before
13396 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13397 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13398 is the class being defined in a class-head.
13400 Returns the TYPE_DECL representing the class. */
13403 cp_parser_class_name (cp_parser
*parser
,
13404 bool typename_keyword_p
,
13405 bool template_keyword_p
,
13406 enum tag_types tag_type
,
13407 bool check_dependency_p
,
13409 bool is_declaration
)
13416 /* All class-names start with an identifier. */
13417 token
= cp_lexer_peek_token (parser
->lexer
);
13418 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
13420 cp_parser_error (parser
, "expected class-name");
13421 return error_mark_node
;
13424 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13425 to a template-id, so we save it here. */
13426 scope
= parser
->scope
;
13427 if (scope
== error_mark_node
)
13428 return error_mark_node
;
13430 /* Any name names a type if we're following the `typename' keyword
13431 in a qualified name where the enclosing scope is type-dependent. */
13432 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
13433 && dependent_type_p (scope
));
13434 /* Handle the common case (an identifier, but not a template-id)
13436 if (token
->type
== CPP_NAME
13437 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
13439 cp_token
*identifier_token
;
13443 /* Look for the identifier. */
13444 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
13445 ambiguous_p
= identifier_token
->ambiguous_p
;
13446 identifier
= cp_parser_identifier (parser
);
13447 /* If the next token isn't an identifier, we are certainly not
13448 looking at a class-name. */
13449 if (identifier
== error_mark_node
)
13450 decl
= error_mark_node
;
13451 /* If we know this is a type-name, there's no need to look it
13453 else if (typename_p
)
13457 tree ambiguous_decls
;
13458 /* If we already know that this lookup is ambiguous, then
13459 we've already issued an error message; there's no reason
13463 cp_parser_simulate_error (parser
);
13464 return error_mark_node
;
13466 /* If the next token is a `::', then the name must be a type
13469 [basic.lookup.qual]
13471 During the lookup for a name preceding the :: scope
13472 resolution operator, object, function, and enumerator
13473 names are ignored. */
13474 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13475 tag_type
= typename_type
;
13476 /* Look up the name. */
13477 decl
= cp_parser_lookup_name (parser
, identifier
,
13479 /*is_template=*/false,
13480 /*is_namespace=*/false,
13481 check_dependency_p
,
13483 if (ambiguous_decls
)
13485 error ("reference to %qD is ambiguous", identifier
);
13486 print_candidates (ambiguous_decls
);
13487 if (cp_parser_parsing_tentatively (parser
))
13489 identifier_token
->ambiguous_p
= true;
13490 cp_parser_simulate_error (parser
);
13492 return error_mark_node
;
13498 /* Try a template-id. */
13499 decl
= cp_parser_template_id (parser
, template_keyword_p
,
13500 check_dependency_p
,
13502 if (decl
== error_mark_node
)
13503 return error_mark_node
;
13506 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
13508 /* If this is a typename, create a TYPENAME_TYPE. */
13509 if (typename_p
&& decl
!= error_mark_node
)
13511 decl
= make_typename_type (scope
, decl
, typename_type
,
13512 /*complain=*/tf_error
);
13513 if (decl
!= error_mark_node
)
13514 decl
= TYPE_NAME (decl
);
13517 /* Check to see that it is really the name of a class. */
13518 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
13519 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
13520 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13521 /* Situations like this:
13523 template <typename T> struct A {
13524 typename T::template X<int>::I i;
13527 are problematic. Is `T::template X<int>' a class-name? The
13528 standard does not seem to be definitive, but there is no other
13529 valid interpretation of the following `::'. Therefore, those
13530 names are considered class-names. */
13532 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
13533 if (decl
!= error_mark_node
)
13534 decl
= TYPE_NAME (decl
);
13536 else if (TREE_CODE (decl
) != TYPE_DECL
13537 || TREE_TYPE (decl
) == error_mark_node
13538 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
13539 decl
= error_mark_node
;
13541 if (decl
== error_mark_node
)
13542 cp_parser_error (parser
, "expected class-name");
13547 /* Parse a class-specifier.
13550 class-head { member-specification [opt] }
13552 Returns the TREE_TYPE representing the class. */
13555 cp_parser_class_specifier (cp_parser
* parser
)
13559 tree attributes
= NULL_TREE
;
13560 int has_trailing_semicolon
;
13561 bool nested_name_specifier_p
;
13562 unsigned saved_num_template_parameter_lists
;
13563 bool saved_in_function_body
;
13564 tree old_scope
= NULL_TREE
;
13565 tree scope
= NULL_TREE
;
13568 push_deferring_access_checks (dk_no_deferred
);
13570 /* Parse the class-head. */
13571 type
= cp_parser_class_head (parser
,
13572 &nested_name_specifier_p
,
13575 /* If the class-head was a semantic disaster, skip the entire body
13579 cp_parser_skip_to_end_of_block_or_statement (parser
);
13580 pop_deferring_access_checks ();
13581 return error_mark_node
;
13584 /* Look for the `{'. */
13585 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
13587 pop_deferring_access_checks ();
13588 return error_mark_node
;
13591 /* Process the base classes. If they're invalid, skip the
13592 entire class body. */
13593 if (!xref_basetypes (type
, bases
))
13595 cp_parser_skip_to_closing_brace (parser
);
13597 /* Consuming the closing brace yields better error messages
13599 cp_lexer_consume_token (parser
->lexer
);
13600 pop_deferring_access_checks ();
13601 return error_mark_node
;
13604 /* Issue an error message if type-definitions are forbidden here. */
13605 cp_parser_check_type_definition (parser
);
13606 /* Remember that we are defining one more class. */
13607 ++parser
->num_classes_being_defined
;
13608 /* Inside the class, surrounding template-parameter-lists do not
13610 saved_num_template_parameter_lists
13611 = parser
->num_template_parameter_lists
;
13612 parser
->num_template_parameter_lists
= 0;
13613 /* We are not in a function body. */
13614 saved_in_function_body
= parser
->in_function_body
;
13615 parser
->in_function_body
= false;
13617 /* Start the class. */
13618 if (nested_name_specifier_p
)
13620 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
13621 old_scope
= push_inner_scope (scope
);
13623 type
= begin_class_definition (type
, attributes
);
13625 if (type
== error_mark_node
)
13626 /* If the type is erroneous, skip the entire body of the class. */
13627 cp_parser_skip_to_closing_brace (parser
);
13629 /* Parse the member-specification. */
13630 cp_parser_member_specification_opt (parser
);
13632 /* Look for the trailing `}'. */
13633 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13634 /* We get better error messages by noticing a common problem: a
13635 missing trailing `;'. */
13636 token
= cp_lexer_peek_token (parser
->lexer
);
13637 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
13638 /* Look for trailing attributes to apply to this class. */
13639 if (cp_parser_allow_gnu_extensions_p (parser
))
13640 attributes
= cp_parser_attributes_opt (parser
);
13641 if (type
!= error_mark_node
)
13642 type
= finish_struct (type
, attributes
);
13643 if (nested_name_specifier_p
)
13644 pop_inner_scope (old_scope
, scope
);
13645 /* If this class is not itself within the scope of another class,
13646 then we need to parse the bodies of all of the queued function
13647 definitions. Note that the queued functions defined in a class
13648 are not always processed immediately following the
13649 class-specifier for that class. Consider:
13652 struct B { void f() { sizeof (A); } };
13655 If `f' were processed before the processing of `A' were
13656 completed, there would be no way to compute the size of `A'.
13657 Note that the nesting we are interested in here is lexical --
13658 not the semantic nesting given by TYPE_CONTEXT. In particular,
13661 struct A { struct B; };
13662 struct A::B { void f() { } };
13664 there is no need to delay the parsing of `A::B::f'. */
13665 if (--parser
->num_classes_being_defined
== 0)
13669 tree class_type
= NULL_TREE
;
13670 tree pushed_scope
= NULL_TREE
;
13672 /* In a first pass, parse default arguments to the functions.
13673 Then, in a second pass, parse the bodies of the functions.
13674 This two-phased approach handles cases like:
13682 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
13683 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
13684 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
13685 TREE_PURPOSE (parser
->unparsed_functions_queues
)
13686 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
13688 fn
= TREE_VALUE (queue_entry
);
13689 /* If there are default arguments that have not yet been processed,
13690 take care of them now. */
13691 if (class_type
!= TREE_PURPOSE (queue_entry
))
13694 pop_scope (pushed_scope
);
13695 class_type
= TREE_PURPOSE (queue_entry
);
13696 pushed_scope
= push_scope (class_type
);
13698 /* Make sure that any template parameters are in scope. */
13699 maybe_begin_member_template_processing (fn
);
13700 /* Parse the default argument expressions. */
13701 cp_parser_late_parsing_default_args (parser
, fn
);
13702 /* Remove any template parameters from the symbol table. */
13703 maybe_end_member_template_processing ();
13706 pop_scope (pushed_scope
);
13707 /* Now parse the body of the functions. */
13708 for (TREE_VALUE (parser
->unparsed_functions_queues
)
13709 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
13710 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
13711 TREE_VALUE (parser
->unparsed_functions_queues
)
13712 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
13714 /* Figure out which function we need to process. */
13715 fn
= TREE_VALUE (queue_entry
);
13716 /* Parse the function. */
13717 cp_parser_late_parsing_for_member (parser
, fn
);
13721 /* Put back any saved access checks. */
13722 pop_deferring_access_checks ();
13724 /* Restore saved state. */
13725 parser
->in_function_body
= saved_in_function_body
;
13726 parser
->num_template_parameter_lists
13727 = saved_num_template_parameter_lists
;
13732 /* Parse a class-head.
13735 class-key identifier [opt] base-clause [opt]
13736 class-key nested-name-specifier identifier base-clause [opt]
13737 class-key nested-name-specifier [opt] template-id
13741 class-key attributes identifier [opt] base-clause [opt]
13742 class-key attributes nested-name-specifier identifier base-clause [opt]
13743 class-key attributes nested-name-specifier [opt] template-id
13746 Upon return BASES is initialized to the list of base classes (or
13747 NULL, if there are none) in the same form returned by
13748 cp_parser_base_clause.
13750 Returns the TYPE of the indicated class. Sets
13751 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13752 involving a nested-name-specifier was used, and FALSE otherwise.
13754 Returns error_mark_node if this is not a class-head.
13756 Returns NULL_TREE if the class-head is syntactically valid, but
13757 semantically invalid in a way that means we should skip the entire
13758 body of the class. */
13761 cp_parser_class_head (cp_parser
* parser
,
13762 bool* nested_name_specifier_p
,
13763 tree
*attributes_p
,
13766 tree nested_name_specifier
;
13767 enum tag_types class_key
;
13768 tree id
= NULL_TREE
;
13769 tree type
= NULL_TREE
;
13771 bool template_id_p
= false;
13772 bool qualified_p
= false;
13773 bool invalid_nested_name_p
= false;
13774 bool invalid_explicit_specialization_p
= false;
13775 tree pushed_scope
= NULL_TREE
;
13776 unsigned num_templates
;
13778 /* Assume no nested-name-specifier will be present. */
13779 *nested_name_specifier_p
= false;
13780 /* Assume no template parameter lists will be used in defining the
13784 *bases
= NULL_TREE
;
13786 /* Look for the class-key. */
13787 class_key
= cp_parser_class_key (parser
);
13788 if (class_key
== none_type
)
13789 return error_mark_node
;
13791 /* Parse the attributes. */
13792 attributes
= cp_parser_attributes_opt (parser
);
13794 /* If the next token is `::', that is invalid -- but sometimes
13795 people do try to write:
13799 Handle this gracefully by accepting the extra qualifier, and then
13800 issuing an error about it later if this really is a
13801 class-head. If it turns out just to be an elaborated type
13802 specifier, remain silent. */
13803 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
13804 qualified_p
= true;
13806 push_deferring_access_checks (dk_no_check
);
13808 /* Determine the name of the class. Begin by looking for an
13809 optional nested-name-specifier. */
13810 nested_name_specifier
13811 = cp_parser_nested_name_specifier_opt (parser
,
13812 /*typename_keyword_p=*/false,
13813 /*check_dependency_p=*/false,
13815 /*is_declaration=*/false);
13816 /* If there was a nested-name-specifier, then there *must* be an
13818 if (nested_name_specifier
)
13820 /* Although the grammar says `identifier', it really means
13821 `class-name' or `template-name'. You are only allowed to
13822 define a class that has already been declared with this
13825 The proposed resolution for Core Issue 180 says that wherever
13826 you see `class T::X' you should treat `X' as a type-name.
13828 It is OK to define an inaccessible class; for example:
13830 class A { class B; };
13833 We do not know if we will see a class-name, or a
13834 template-name. We look for a class-name first, in case the
13835 class-name is a template-id; if we looked for the
13836 template-name first we would stop after the template-name. */
13837 cp_parser_parse_tentatively (parser
);
13838 type
= cp_parser_class_name (parser
,
13839 /*typename_keyword_p=*/false,
13840 /*template_keyword_p=*/false,
13842 /*check_dependency_p=*/false,
13843 /*class_head_p=*/true,
13844 /*is_declaration=*/false);
13845 /* If that didn't work, ignore the nested-name-specifier. */
13846 if (!cp_parser_parse_definitely (parser
))
13848 invalid_nested_name_p
= true;
13849 id
= cp_parser_identifier (parser
);
13850 if (id
== error_mark_node
)
13853 /* If we could not find a corresponding TYPE, treat this
13854 declaration like an unqualified declaration. */
13855 if (type
== error_mark_node
)
13856 nested_name_specifier
= NULL_TREE
;
13857 /* Otherwise, count the number of templates used in TYPE and its
13858 containing scopes. */
13863 for (scope
= TREE_TYPE (type
);
13864 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
13865 scope
= (TYPE_P (scope
)
13866 ? TYPE_CONTEXT (scope
)
13867 : DECL_CONTEXT (scope
)))
13869 && CLASS_TYPE_P (scope
)
13870 && CLASSTYPE_TEMPLATE_INFO (scope
)
13871 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
13872 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
13876 /* Otherwise, the identifier is optional. */
13879 /* We don't know whether what comes next is a template-id,
13880 an identifier, or nothing at all. */
13881 cp_parser_parse_tentatively (parser
);
13882 /* Check for a template-id. */
13883 id
= cp_parser_template_id (parser
,
13884 /*template_keyword_p=*/false,
13885 /*check_dependency_p=*/true,
13886 /*is_declaration=*/true);
13887 /* If that didn't work, it could still be an identifier. */
13888 if (!cp_parser_parse_definitely (parser
))
13890 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13891 id
= cp_parser_identifier (parser
);
13897 template_id_p
= true;
13902 pop_deferring_access_checks ();
13905 cp_parser_check_for_invalid_template_id (parser
, id
);
13907 /* If it's not a `:' or a `{' then we can't really be looking at a
13908 class-head, since a class-head only appears as part of a
13909 class-specifier. We have to detect this situation before calling
13910 xref_tag, since that has irreversible side-effects. */
13911 if (!cp_parser_next_token_starts_class_definition_p (parser
))
13913 cp_parser_error (parser
, "expected %<{%> or %<:%>");
13914 return error_mark_node
;
13917 /* At this point, we're going ahead with the class-specifier, even
13918 if some other problem occurs. */
13919 cp_parser_commit_to_tentative_parse (parser
);
13920 /* Issue the error about the overly-qualified name now. */
13922 cp_parser_error (parser
,
13923 "global qualification of class name is invalid");
13924 else if (invalid_nested_name_p
)
13925 cp_parser_error (parser
,
13926 "qualified name does not name a class");
13927 else if (nested_name_specifier
)
13931 /* Reject typedef-names in class heads. */
13932 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
13934 error ("invalid class name in declaration of %qD", type
);
13939 /* Figure out in what scope the declaration is being placed. */
13940 scope
= current_scope ();
13941 /* If that scope does not contain the scope in which the
13942 class was originally declared, the program is invalid. */
13943 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
13945 error ("declaration of %qD in %qD which does not enclose %qD",
13946 type
, scope
, nested_name_specifier
);
13952 A declarator-id shall not be qualified exception of the
13953 definition of a ... nested class outside of its class
13954 ... [or] a the definition or explicit instantiation of a
13955 class member of a namespace outside of its namespace. */
13956 if (scope
== nested_name_specifier
)
13958 pedwarn ("extra qualification ignored");
13959 nested_name_specifier
= NULL_TREE
;
13963 /* An explicit-specialization must be preceded by "template <>". If
13964 it is not, try to recover gracefully. */
13965 if (at_namespace_scope_p ()
13966 && parser
->num_template_parameter_lists
== 0
13969 error ("an explicit specialization must be preceded by %<template <>%>");
13970 invalid_explicit_specialization_p
= true;
13971 /* Take the same action that would have been taken by
13972 cp_parser_explicit_specialization. */
13973 ++parser
->num_template_parameter_lists
;
13974 begin_specialization ();
13976 /* There must be no "return" statements between this point and the
13977 end of this function; set "type "to the correct return value and
13978 use "goto done;" to return. */
13979 /* Make sure that the right number of template parameters were
13981 if (!cp_parser_check_template_parameters (parser
, num_templates
))
13983 /* If something went wrong, there is no point in even trying to
13984 process the class-definition. */
13989 /* Look up the type. */
13992 type
= TREE_TYPE (id
);
13993 type
= maybe_process_partial_specialization (type
);
13994 if (nested_name_specifier
)
13995 pushed_scope
= push_scope (nested_name_specifier
);
13997 else if (nested_name_specifier
)
14003 template <typename T> struct S { struct T };
14004 template <typename T> struct S<T>::T { };
14006 we will get a TYPENAME_TYPE when processing the definition of
14007 `S::T'. We need to resolve it to the actual type before we
14008 try to define it. */
14009 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
14011 class_type
= resolve_typename_type (TREE_TYPE (type
),
14012 /*only_current_p=*/false);
14013 if (class_type
!= error_mark_node
)
14014 type
= TYPE_NAME (class_type
);
14017 cp_parser_error (parser
, "could not resolve typename type");
14018 type
= error_mark_node
;
14022 maybe_process_partial_specialization (TREE_TYPE (type
));
14023 class_type
= current_class_type
;
14024 /* Enter the scope indicated by the nested-name-specifier. */
14025 pushed_scope
= push_scope (nested_name_specifier
);
14026 /* Get the canonical version of this type. */
14027 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
14028 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14029 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
14031 type
= push_template_decl (type
);
14032 if (type
== error_mark_node
)
14039 type
= TREE_TYPE (type
);
14040 *nested_name_specifier_p
= true;
14042 else /* The name is not a nested name. */
14044 /* If the class was unnamed, create a dummy name. */
14046 id
= make_anon_name ();
14047 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
14048 parser
->num_template_parameter_lists
);
14051 /* Indicate whether this class was declared as a `class' or as a
14053 if (TREE_CODE (type
) == RECORD_TYPE
)
14054 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
14055 cp_parser_check_class_key (class_key
, type
);
14057 /* If this type was already complete, and we see another definition,
14058 that's an error. */
14059 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
14061 error ("redefinition of %q#T", type
);
14062 error ("previous definition of %q+#T", type
);
14066 else if (type
== error_mark_node
)
14069 /* We will have entered the scope containing the class; the names of
14070 base classes should be looked up in that context. For example:
14072 struct A { struct B {}; struct C; };
14073 struct A::C : B {};
14077 /* Get the list of base-classes, if there is one. */
14078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
14079 *bases
= cp_parser_base_clause (parser
);
14082 /* Leave the scope given by the nested-name-specifier. We will
14083 enter the class scope itself while processing the members. */
14085 pop_scope (pushed_scope
);
14087 if (invalid_explicit_specialization_p
)
14089 end_specialization ();
14090 --parser
->num_template_parameter_lists
;
14092 *attributes_p
= attributes
;
14096 /* Parse a class-key.
14103 Returns the kind of class-key specified, or none_type to indicate
14106 static enum tag_types
14107 cp_parser_class_key (cp_parser
* parser
)
14110 enum tag_types tag_type
;
14112 /* Look for the class-key. */
14113 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
14117 /* Check to see if the TOKEN is a class-key. */
14118 tag_type
= cp_parser_token_is_class_key (token
);
14120 cp_parser_error (parser
, "expected class-key");
14124 /* Parse an (optional) member-specification.
14126 member-specification:
14127 member-declaration member-specification [opt]
14128 access-specifier : member-specification [opt] */
14131 cp_parser_member_specification_opt (cp_parser
* parser
)
14138 /* Peek at the next token. */
14139 token
= cp_lexer_peek_token (parser
->lexer
);
14140 /* If it's a `}', or EOF then we've seen all the members. */
14141 if (token
->type
== CPP_CLOSE_BRACE
14142 || token
->type
== CPP_EOF
14143 || token
->type
== CPP_PRAGMA_EOL
)
14146 /* See if this token is a keyword. */
14147 keyword
= token
->keyword
;
14151 case RID_PROTECTED
:
14153 /* Consume the access-specifier. */
14154 cp_lexer_consume_token (parser
->lexer
);
14155 /* Remember which access-specifier is active. */
14156 current_access_specifier
= token
->u
.value
;
14157 /* Look for the `:'. */
14158 cp_parser_require (parser
, CPP_COLON
, "`:'");
14162 /* Accept #pragmas at class scope. */
14163 if (token
->type
== CPP_PRAGMA
)
14165 cp_parser_pragma (parser
, pragma_external
);
14169 /* Otherwise, the next construction must be a
14170 member-declaration. */
14171 cp_parser_member_declaration (parser
);
14176 /* Parse a member-declaration.
14178 member-declaration:
14179 decl-specifier-seq [opt] member-declarator-list [opt] ;
14180 function-definition ; [opt]
14181 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14183 template-declaration
14185 member-declarator-list:
14187 member-declarator-list , member-declarator
14190 declarator pure-specifier [opt]
14191 declarator constant-initializer [opt]
14192 identifier [opt] : constant-expression
14196 member-declaration:
14197 __extension__ member-declaration
14200 declarator attributes [opt] pure-specifier [opt]
14201 declarator attributes [opt] constant-initializer [opt]
14202 identifier [opt] attributes [opt] : constant-expression
14206 member-declaration:
14207 static_assert-declaration */
14210 cp_parser_member_declaration (cp_parser
* parser
)
14212 cp_decl_specifier_seq decl_specifiers
;
14213 tree prefix_attributes
;
14215 int declares_class_or_enum
;
14218 int saved_pedantic
;
14220 /* Check for the `__extension__' keyword. */
14221 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
14224 cp_parser_member_declaration (parser
);
14225 /* Restore the old value of the PEDANTIC flag. */
14226 pedantic
= saved_pedantic
;
14231 /* Check for a template-declaration. */
14232 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14234 /* An explicit specialization here is an error condition, and we
14235 expect the specialization handler to detect and report this. */
14236 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
14237 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
14238 cp_parser_explicit_specialization (parser
);
14240 cp_parser_template_declaration (parser
, /*member_p=*/true);
14245 /* Check for a using-declaration. */
14246 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
14248 /* Parse the using-declaration. */
14249 cp_parser_using_declaration (parser
,
14250 /*access_declaration_p=*/false);
14254 /* Check for @defs. */
14255 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
14258 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
14259 ivar
= ivar_chains
;
14263 ivar
= TREE_CHAIN (member
);
14264 TREE_CHAIN (member
) = NULL_TREE
;
14265 finish_member_declaration (member
);
14270 /* If the next token is `static_assert' we have a static assertion. */
14271 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
14273 cp_parser_static_assert (parser
, /*member_p=*/true);
14277 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
14280 /* Parse the decl-specifier-seq. */
14281 cp_parser_decl_specifier_seq (parser
,
14282 CP_PARSER_FLAGS_OPTIONAL
,
14284 &declares_class_or_enum
);
14285 prefix_attributes
= decl_specifiers
.attributes
;
14286 decl_specifiers
.attributes
= NULL_TREE
;
14287 /* Check for an invalid type-name. */
14288 if (!decl_specifiers
.type
14289 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
14291 /* If there is no declarator, then the decl-specifier-seq should
14293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14295 /* If there was no decl-specifier-seq, and the next token is a
14296 `;', then we have something like:
14302 Each member-declaration shall declare at least one member
14303 name of the class. */
14304 if (!decl_specifiers
.any_specifiers_p
)
14306 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14307 if (pedantic
&& !token
->in_system_header
)
14308 pedwarn ("%Hextra %<;%>", &token
->location
);
14314 /* See if this declaration is a friend. */
14315 friend_p
= cp_parser_friend_p (&decl_specifiers
);
14316 /* If there were decl-specifiers, check to see if there was
14317 a class-declaration. */
14318 type
= check_tag_decl (&decl_specifiers
);
14319 /* Nested classes have already been added to the class, but
14320 a `friend' needs to be explicitly registered. */
14323 /* If the `friend' keyword was present, the friend must
14324 be introduced with a class-key. */
14325 if (!declares_class_or_enum
)
14326 error ("a class-key must be used when declaring a friend");
14329 template <typename T> struct A {
14330 friend struct A<T>::B;
14333 A<T>::B will be represented by a TYPENAME_TYPE, and
14334 therefore not recognized by check_tag_decl. */
14336 && decl_specifiers
.type
14337 && TYPE_P (decl_specifiers
.type
))
14338 type
= decl_specifiers
.type
;
14339 if (!type
|| !TYPE_P (type
))
14340 error ("friend declaration does not name a class or "
14343 make_friend_class (current_class_type
, type
,
14344 /*complain=*/true);
14346 /* If there is no TYPE, an error message will already have
14348 else if (!type
|| type
== error_mark_node
)
14350 /* An anonymous aggregate has to be handled specially; such
14351 a declaration really declares a data member (with a
14352 particular type), as opposed to a nested class. */
14353 else if (ANON_AGGR_TYPE_P (type
))
14355 /* Remove constructors and such from TYPE, now that we
14356 know it is an anonymous aggregate. */
14357 fixup_anonymous_aggr (type
);
14358 /* And make the corresponding data member. */
14359 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
14360 /* Add it to the class. */
14361 finish_member_declaration (decl
);
14364 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
14369 /* See if these declarations will be friends. */
14370 friend_p
= cp_parser_friend_p (&decl_specifiers
);
14372 /* Keep going until we hit the `;' at the end of the
14374 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
14376 tree attributes
= NULL_TREE
;
14377 tree first_attribute
;
14379 /* Peek at the next token. */
14380 token
= cp_lexer_peek_token (parser
->lexer
);
14382 /* Check for a bitfield declaration. */
14383 if (token
->type
== CPP_COLON
14384 || (token
->type
== CPP_NAME
14385 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
14391 /* Get the name of the bitfield. Note that we cannot just
14392 check TOKEN here because it may have been invalidated by
14393 the call to cp_lexer_peek_nth_token above. */
14394 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
14395 identifier
= cp_parser_identifier (parser
);
14397 identifier
= NULL_TREE
;
14399 /* Consume the `:' token. */
14400 cp_lexer_consume_token (parser
->lexer
);
14401 /* Get the width of the bitfield. */
14403 = cp_parser_constant_expression (parser
,
14404 /*allow_non_constant=*/false,
14407 /* Look for attributes that apply to the bitfield. */
14408 attributes
= cp_parser_attributes_opt (parser
);
14409 /* Remember which attributes are prefix attributes and
14411 first_attribute
= attributes
;
14412 /* Combine the attributes. */
14413 attributes
= chainon (prefix_attributes
, attributes
);
14415 /* Create the bitfield declaration. */
14416 decl
= grokbitfield (identifier
14417 ? make_id_declarator (NULL_TREE
,
14423 /* Apply the attributes. */
14424 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
14428 cp_declarator
*declarator
;
14430 tree asm_specification
;
14431 int ctor_dtor_or_conv_p
;
14433 /* Parse the declarator. */
14435 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14436 &ctor_dtor_or_conv_p
,
14437 /*parenthesized_p=*/NULL
,
14438 /*member_p=*/true);
14440 /* If something went wrong parsing the declarator, make sure
14441 that we at least consume some tokens. */
14442 if (declarator
== cp_error_declarator
)
14444 /* Skip to the end of the statement. */
14445 cp_parser_skip_to_end_of_statement (parser
);
14446 /* If the next token is not a semicolon, that is
14447 probably because we just skipped over the body of
14448 a function. So, we consume a semicolon if
14449 present, but do not issue an error message if it
14451 if (cp_lexer_next_token_is (parser
->lexer
,
14453 cp_lexer_consume_token (parser
->lexer
);
14457 if (declares_class_or_enum
& 2)
14458 cp_parser_check_for_definition_in_return_type
14459 (declarator
, decl_specifiers
.type
);
14461 /* Look for an asm-specification. */
14462 asm_specification
= cp_parser_asm_specification_opt (parser
);
14463 /* Look for attributes that apply to the declaration. */
14464 attributes
= cp_parser_attributes_opt (parser
);
14465 /* Remember which attributes are prefix attributes and
14467 first_attribute
= attributes
;
14468 /* Combine the attributes. */
14469 attributes
= chainon (prefix_attributes
, attributes
);
14471 /* If it's an `=', then we have a constant-initializer or a
14472 pure-specifier. It is not correct to parse the
14473 initializer before registering the member declaration
14474 since the member declaration should be in scope while
14475 its initializer is processed. However, the rest of the
14476 front end does not yet provide an interface that allows
14477 us to handle this correctly. */
14478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
14482 A pure-specifier shall be used only in the declaration of
14483 a virtual function.
14485 A member-declarator can contain a constant-initializer
14486 only if it declares a static member of integral or
14489 Therefore, if the DECLARATOR is for a function, we look
14490 for a pure-specifier; otherwise, we look for a
14491 constant-initializer. When we call `grokfield', it will
14492 perform more stringent semantics checks. */
14493 if (function_declarator_p (declarator
))
14494 initializer
= cp_parser_pure_specifier (parser
);
14496 /* Parse the initializer. */
14497 initializer
= cp_parser_constant_initializer (parser
);
14499 /* Otherwise, there is no initializer. */
14501 initializer
= NULL_TREE
;
14503 /* See if we are probably looking at a function
14504 definition. We are certainly not looking at a
14505 member-declarator. Calling `grokfield' has
14506 side-effects, so we must not do it unless we are sure
14507 that we are looking at a member-declarator. */
14508 if (cp_parser_token_starts_function_definition_p
14509 (cp_lexer_peek_token (parser
->lexer
)))
14511 /* The grammar does not allow a pure-specifier to be
14512 used when a member function is defined. (It is
14513 possible that this fact is an oversight in the
14514 standard, since a pure function may be defined
14515 outside of the class-specifier. */
14517 error ("pure-specifier on function-definition");
14518 decl
= cp_parser_save_member_function_body (parser
,
14522 /* If the member was not a friend, declare it here. */
14524 finish_member_declaration (decl
);
14525 /* Peek at the next token. */
14526 token
= cp_lexer_peek_token (parser
->lexer
);
14527 /* If the next token is a semicolon, consume it. */
14528 if (token
->type
== CPP_SEMICOLON
)
14530 if (pedantic
&& !in_system_header
)
14531 pedwarn ("extra %<;%>");
14532 cp_lexer_consume_token (parser
->lexer
);
14537 /* Create the declaration. */
14538 decl
= grokfield (declarator
, &decl_specifiers
,
14539 initializer
, /*init_const_expr_p=*/true,
14544 /* Reset PREFIX_ATTRIBUTES. */
14545 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
14546 attributes
= TREE_CHAIN (attributes
);
14548 TREE_CHAIN (attributes
) = NULL_TREE
;
14550 /* If there is any qualification still in effect, clear it
14551 now; we will be starting fresh with the next declarator. */
14552 parser
->scope
= NULL_TREE
;
14553 parser
->qualifying_scope
= NULL_TREE
;
14554 parser
->object_scope
= NULL_TREE
;
14555 /* If it's a `,', then there are more declarators. */
14556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
14557 cp_lexer_consume_token (parser
->lexer
);
14558 /* If the next token isn't a `;', then we have a parse error. */
14559 else if (cp_lexer_next_token_is_not (parser
->lexer
,
14562 cp_parser_error (parser
, "expected %<;%>");
14563 /* Skip tokens until we find a `;'. */
14564 cp_parser_skip_to_end_of_statement (parser
);
14571 /* Add DECL to the list of members. */
14573 finish_member_declaration (decl
);
14575 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14576 cp_parser_save_default_args (parser
, decl
);
14581 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
14584 /* Parse a pure-specifier.
14589 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14590 Otherwise, ERROR_MARK_NODE is returned. */
14593 cp_parser_pure_specifier (cp_parser
* parser
)
14597 /* Look for the `=' token. */
14598 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
14599 return error_mark_node
;
14600 /* Look for the `0' token. */
14601 token
= cp_lexer_consume_token (parser
->lexer
);
14602 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14603 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
14605 cp_parser_error (parser
,
14606 "invalid pure specifier (only `= 0' is allowed)");
14607 cp_parser_skip_to_end_of_statement (parser
);
14608 return error_mark_node
;
14610 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14612 error ("templates may not be %<virtual%>");
14613 return error_mark_node
;
14616 return integer_zero_node
;
14619 /* Parse a constant-initializer.
14621 constant-initializer:
14622 = constant-expression
14624 Returns a representation of the constant-expression. */
14627 cp_parser_constant_initializer (cp_parser
* parser
)
14629 /* Look for the `=' token. */
14630 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
14631 return error_mark_node
;
14633 /* It is invalid to write:
14635 struct S { static const int i = { 7 }; };
14638 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14640 cp_parser_error (parser
,
14641 "a brace-enclosed initializer is not allowed here");
14642 /* Consume the opening brace. */
14643 cp_lexer_consume_token (parser
->lexer
);
14644 /* Skip the initializer. */
14645 cp_parser_skip_to_closing_brace (parser
);
14646 /* Look for the trailing `}'. */
14647 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
14649 return error_mark_node
;
14652 return cp_parser_constant_expression (parser
,
14653 /*allow_non_constant=*/false,
14657 /* Derived classes [gram.class.derived] */
14659 /* Parse a base-clause.
14662 : base-specifier-list
14664 base-specifier-list:
14665 base-specifier ... [opt]
14666 base-specifier-list , base-specifier ... [opt]
14668 Returns a TREE_LIST representing the base-classes, in the order in
14669 which they were declared. The representation of each node is as
14670 described by cp_parser_base_specifier.
14672 In the case that no bases are specified, this function will return
14673 NULL_TREE, not ERROR_MARK_NODE. */
14676 cp_parser_base_clause (cp_parser
* parser
)
14678 tree bases
= NULL_TREE
;
14680 /* Look for the `:' that begins the list. */
14681 cp_parser_require (parser
, CPP_COLON
, "`:'");
14683 /* Scan the base-specifier-list. */
14688 bool pack_expansion_p
= false;
14690 /* Look for the base-specifier. */
14691 base
= cp_parser_base_specifier (parser
);
14692 /* Look for the (optional) ellipsis. */
14693 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14695 /* Consume the `...'. */
14696 cp_lexer_consume_token (parser
->lexer
);
14698 pack_expansion_p
= true;
14701 /* Add BASE to the front of the list. */
14702 if (base
!= error_mark_node
)
14704 if (pack_expansion_p
)
14705 /* Make this a pack expansion type. */
14706 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
14708 check_for_bare_parameter_packs (TREE_VALUE (base
));
14710 TREE_CHAIN (base
) = bases
;
14713 /* Peek at the next token. */
14714 token
= cp_lexer_peek_token (parser
->lexer
);
14715 /* If it's not a comma, then the list is complete. */
14716 if (token
->type
!= CPP_COMMA
)
14718 /* Consume the `,'. */
14719 cp_lexer_consume_token (parser
->lexer
);
14722 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14723 base class had a qualified name. However, the next name that
14724 appears is certainly not qualified. */
14725 parser
->scope
= NULL_TREE
;
14726 parser
->qualifying_scope
= NULL_TREE
;
14727 parser
->object_scope
= NULL_TREE
;
14729 return nreverse (bases
);
14732 /* Parse a base-specifier.
14735 :: [opt] nested-name-specifier [opt] class-name
14736 virtual access-specifier [opt] :: [opt] nested-name-specifier
14738 access-specifier virtual [opt] :: [opt] nested-name-specifier
14741 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14742 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14743 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14744 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14747 cp_parser_base_specifier (cp_parser
* parser
)
14751 bool virtual_p
= false;
14752 bool duplicate_virtual_error_issued_p
= false;
14753 bool duplicate_access_error_issued_p
= false;
14754 bool class_scope_p
, template_p
;
14755 tree access
= access_default_node
;
14758 /* Process the optional `virtual' and `access-specifier'. */
14761 /* Peek at the next token. */
14762 token
= cp_lexer_peek_token (parser
->lexer
);
14763 /* Process `virtual'. */
14764 switch (token
->keyword
)
14767 /* If `virtual' appears more than once, issue an error. */
14768 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
14770 cp_parser_error (parser
,
14771 "%<virtual%> specified more than once in base-specified");
14772 duplicate_virtual_error_issued_p
= true;
14777 /* Consume the `virtual' token. */
14778 cp_lexer_consume_token (parser
->lexer
);
14783 case RID_PROTECTED
:
14785 /* If more than one access specifier appears, issue an
14787 if (access
!= access_default_node
14788 && !duplicate_access_error_issued_p
)
14790 cp_parser_error (parser
,
14791 "more than one access specifier in base-specified");
14792 duplicate_access_error_issued_p
= true;
14795 access
= ridpointers
[(int) token
->keyword
];
14797 /* Consume the access-specifier. */
14798 cp_lexer_consume_token (parser
->lexer
);
14807 /* It is not uncommon to see programs mechanically, erroneously, use
14808 the 'typename' keyword to denote (dependent) qualified types
14809 as base classes. */
14810 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14812 if (!processing_template_decl
)
14813 error ("keyword %<typename%> not allowed outside of templates");
14815 error ("keyword %<typename%> not allowed in this context "
14816 "(the base class is implicitly a type)");
14817 cp_lexer_consume_token (parser
->lexer
);
14820 /* Look for the optional `::' operator. */
14821 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
14822 /* Look for the nested-name-specifier. The simplest way to
14827 The keyword `typename' is not permitted in a base-specifier or
14828 mem-initializer; in these contexts a qualified name that
14829 depends on a template-parameter is implicitly assumed to be a
14832 is to pretend that we have seen the `typename' keyword at this
14834 cp_parser_nested_name_specifier_opt (parser
,
14835 /*typename_keyword_p=*/true,
14836 /*check_dependency_p=*/true,
14838 /*is_declaration=*/true);
14839 /* If the base class is given by a qualified name, assume that names
14840 we see are type names or templates, as appropriate. */
14841 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
14842 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
14844 /* Finally, look for the class-name. */
14845 type
= cp_parser_class_name (parser
,
14849 /*check_dependency_p=*/true,
14850 /*class_head_p=*/false,
14851 /*is_declaration=*/true);
14853 if (type
== error_mark_node
)
14854 return error_mark_node
;
14856 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
14859 /* Exception handling [gram.exception] */
14861 /* Parse an (optional) exception-specification.
14863 exception-specification:
14864 throw ( type-id-list [opt] )
14866 Returns a TREE_LIST representing the exception-specification. The
14867 TREE_VALUE of each node is a type. */
14870 cp_parser_exception_specification_opt (cp_parser
* parser
)
14875 /* Peek at the next token. */
14876 token
= cp_lexer_peek_token (parser
->lexer
);
14877 /* If it's not `throw', then there's no exception-specification. */
14878 if (!cp_parser_is_keyword (token
, RID_THROW
))
14881 /* Consume the `throw'. */
14882 cp_lexer_consume_token (parser
->lexer
);
14884 /* Look for the `('. */
14885 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14887 /* Peek at the next token. */
14888 token
= cp_lexer_peek_token (parser
->lexer
);
14889 /* If it's not a `)', then there is a type-id-list. */
14890 if (token
->type
!= CPP_CLOSE_PAREN
)
14892 const char *saved_message
;
14894 /* Types may not be defined in an exception-specification. */
14895 saved_message
= parser
->type_definition_forbidden_message
;
14896 parser
->type_definition_forbidden_message
14897 = "types may not be defined in an exception-specification";
14898 /* Parse the type-id-list. */
14899 type_id_list
= cp_parser_type_id_list (parser
);
14900 /* Restore the saved message. */
14901 parser
->type_definition_forbidden_message
= saved_message
;
14904 type_id_list
= empty_except_spec
;
14906 /* Look for the `)'. */
14907 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14909 return type_id_list
;
14912 /* Parse an (optional) type-id-list.
14916 type-id-list , type-id ... [opt]
14918 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14919 in the order that the types were presented. */
14922 cp_parser_type_id_list (cp_parser
* parser
)
14924 tree types
= NULL_TREE
;
14931 /* Get the next type-id. */
14932 type
= cp_parser_type_id (parser
);
14933 /* Parse the optional ellipsis. */
14934 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14936 /* Consume the `...'. */
14937 cp_lexer_consume_token (parser
->lexer
);
14939 /* Turn the type into a pack expansion expression. */
14940 type
= make_pack_expansion (type
);
14942 /* Add it to the list. */
14943 types
= add_exception_specifier (types
, type
, /*complain=*/1);
14944 /* Peek at the next token. */
14945 token
= cp_lexer_peek_token (parser
->lexer
);
14946 /* If it is not a `,', we are done. */
14947 if (token
->type
!= CPP_COMMA
)
14949 /* Consume the `,'. */
14950 cp_lexer_consume_token (parser
->lexer
);
14953 return nreverse (types
);
14956 /* Parse a try-block.
14959 try compound-statement handler-seq */
14962 cp_parser_try_block (cp_parser
* parser
)
14966 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
14967 try_block
= begin_try_block ();
14968 cp_parser_compound_statement (parser
, NULL
, true);
14969 finish_try_block (try_block
);
14970 cp_parser_handler_seq (parser
);
14971 finish_handler_sequence (try_block
);
14976 /* Parse a function-try-block.
14978 function-try-block:
14979 try ctor-initializer [opt] function-body handler-seq */
14982 cp_parser_function_try_block (cp_parser
* parser
)
14984 tree compound_stmt
;
14986 bool ctor_initializer_p
;
14988 /* Look for the `try' keyword. */
14989 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
14991 /* Let the rest of the front end know where we are. */
14992 try_block
= begin_function_try_block (&compound_stmt
);
14993 /* Parse the function-body. */
14995 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14996 /* We're done with the `try' part. */
14997 finish_function_try_block (try_block
);
14998 /* Parse the handlers. */
14999 cp_parser_handler_seq (parser
);
15000 /* We're done with the handlers. */
15001 finish_function_handler_sequence (try_block
, compound_stmt
);
15003 return ctor_initializer_p
;
15006 /* Parse a handler-seq.
15009 handler handler-seq [opt] */
15012 cp_parser_handler_seq (cp_parser
* parser
)
15018 /* Parse the handler. */
15019 cp_parser_handler (parser
);
15020 /* Peek at the next token. */
15021 token
= cp_lexer_peek_token (parser
->lexer
);
15022 /* If it's not `catch' then there are no more handlers. */
15023 if (!cp_parser_is_keyword (token
, RID_CATCH
))
15028 /* Parse a handler.
15031 catch ( exception-declaration ) compound-statement */
15034 cp_parser_handler (cp_parser
* parser
)
15039 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
15040 handler
= begin_handler ();
15041 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15042 declaration
= cp_parser_exception_declaration (parser
);
15043 finish_handler_parms (declaration
, handler
);
15044 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15045 cp_parser_compound_statement (parser
, NULL
, false);
15046 finish_handler (handler
);
15049 /* Parse an exception-declaration.
15051 exception-declaration:
15052 type-specifier-seq declarator
15053 type-specifier-seq abstract-declarator
15057 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15058 ellipsis variant is used. */
15061 cp_parser_exception_declaration (cp_parser
* parser
)
15063 cp_decl_specifier_seq type_specifiers
;
15064 cp_declarator
*declarator
;
15065 const char *saved_message
;
15067 /* If it's an ellipsis, it's easy to handle. */
15068 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15070 /* Consume the `...' token. */
15071 cp_lexer_consume_token (parser
->lexer
);
15075 /* Types may not be defined in exception-declarations. */
15076 saved_message
= parser
->type_definition_forbidden_message
;
15077 parser
->type_definition_forbidden_message
15078 = "types may not be defined in exception-declarations";
15080 /* Parse the type-specifier-seq. */
15081 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
15083 /* If it's a `)', then there is no declarator. */
15084 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
15087 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
15088 /*ctor_dtor_or_conv_p=*/NULL
,
15089 /*parenthesized_p=*/NULL
,
15090 /*member_p=*/false);
15092 /* Restore the saved message. */
15093 parser
->type_definition_forbidden_message
= saved_message
;
15095 if (!type_specifiers
.any_specifiers_p
)
15096 return error_mark_node
;
15098 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
15101 /* Parse a throw-expression.
15104 throw assignment-expression [opt]
15106 Returns a THROW_EXPR representing the throw-expression. */
15109 cp_parser_throw_expression (cp_parser
* parser
)
15114 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
15115 token
= cp_lexer_peek_token (parser
->lexer
);
15116 /* Figure out whether or not there is an assignment-expression
15117 following the "throw" keyword. */
15118 if (token
->type
== CPP_COMMA
15119 || token
->type
== CPP_SEMICOLON
15120 || token
->type
== CPP_CLOSE_PAREN
15121 || token
->type
== CPP_CLOSE_SQUARE
15122 || token
->type
== CPP_CLOSE_BRACE
15123 || token
->type
== CPP_COLON
)
15124 expression
= NULL_TREE
;
15126 expression
= cp_parser_assignment_expression (parser
,
15129 return build_throw (expression
);
15132 /* GNU Extensions */
15134 /* Parse an (optional) asm-specification.
15137 asm ( string-literal )
15139 If the asm-specification is present, returns a STRING_CST
15140 corresponding to the string-literal. Otherwise, returns
15144 cp_parser_asm_specification_opt (cp_parser
* parser
)
15147 tree asm_specification
;
15149 /* Peek at the next token. */
15150 token
= cp_lexer_peek_token (parser
->lexer
);
15151 /* If the next token isn't the `asm' keyword, then there's no
15152 asm-specification. */
15153 if (!cp_parser_is_keyword (token
, RID_ASM
))
15156 /* Consume the `asm' token. */
15157 cp_lexer_consume_token (parser
->lexer
);
15158 /* Look for the `('. */
15159 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15161 /* Look for the string-literal. */
15162 asm_specification
= cp_parser_string_literal (parser
, false, false);
15164 /* Look for the `)'. */
15165 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
15167 return asm_specification
;
15170 /* Parse an asm-operand-list.
15174 asm-operand-list , asm-operand
15177 string-literal ( expression )
15178 [ string-literal ] string-literal ( expression )
15180 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15181 each node is the expression. The TREE_PURPOSE is itself a
15182 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15183 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15184 is a STRING_CST for the string literal before the parenthesis. */
15187 cp_parser_asm_operand_list (cp_parser
* parser
)
15189 tree asm_operands
= NULL_TREE
;
15193 tree string_literal
;
15197 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
15199 /* Consume the `[' token. */
15200 cp_lexer_consume_token (parser
->lexer
);
15201 /* Read the operand name. */
15202 name
= cp_parser_identifier (parser
);
15203 if (name
!= error_mark_node
)
15204 name
= build_string (IDENTIFIER_LENGTH (name
),
15205 IDENTIFIER_POINTER (name
));
15206 /* Look for the closing `]'. */
15207 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
15211 /* Look for the string-literal. */
15212 string_literal
= cp_parser_string_literal (parser
, false, false);
15214 /* Look for the `('. */
15215 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15216 /* Parse the expression. */
15217 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
15218 /* Look for the `)'. */
15219 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15221 /* Add this operand to the list. */
15222 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
15225 /* If the next token is not a `,', there are no more
15227 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15229 /* Consume the `,'. */
15230 cp_lexer_consume_token (parser
->lexer
);
15233 return nreverse (asm_operands
);
15236 /* Parse an asm-clobber-list.
15240 asm-clobber-list , string-literal
15242 Returns a TREE_LIST, indicating the clobbers in the order that they
15243 appeared. The TREE_VALUE of each node is a STRING_CST. */
15246 cp_parser_asm_clobber_list (cp_parser
* parser
)
15248 tree clobbers
= NULL_TREE
;
15252 tree string_literal
;
15254 /* Look for the string literal. */
15255 string_literal
= cp_parser_string_literal (parser
, false, false);
15256 /* Add it to the list. */
15257 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
15258 /* If the next token is not a `,', then the list is
15260 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15262 /* Consume the `,' token. */
15263 cp_lexer_consume_token (parser
->lexer
);
15269 /* Parse an (optional) series of attributes.
15272 attributes attribute
15275 __attribute__ (( attribute-list [opt] ))
15277 The return value is as for cp_parser_attribute_list. */
15280 cp_parser_attributes_opt (cp_parser
* parser
)
15282 tree attributes
= NULL_TREE
;
15287 tree attribute_list
;
15289 /* Peek at the next token. */
15290 token
= cp_lexer_peek_token (parser
->lexer
);
15291 /* If it's not `__attribute__', then we're done. */
15292 if (token
->keyword
!= RID_ATTRIBUTE
)
15295 /* Consume the `__attribute__' keyword. */
15296 cp_lexer_consume_token (parser
->lexer
);
15297 /* Look for the two `(' tokens. */
15298 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15299 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15301 /* Peek at the next token. */
15302 token
= cp_lexer_peek_token (parser
->lexer
);
15303 if (token
->type
!= CPP_CLOSE_PAREN
)
15304 /* Parse the attribute-list. */
15305 attribute_list
= cp_parser_attribute_list (parser
);
15307 /* If the next token is a `)', then there is no attribute
15309 attribute_list
= NULL
;
15311 /* Look for the two `)' tokens. */
15312 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15313 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15315 /* Add these new attributes to the list. */
15316 attributes
= chainon (attributes
, attribute_list
);
15322 /* Parse an attribute-list.
15326 attribute-list , attribute
15330 identifier ( identifier )
15331 identifier ( identifier , expression-list )
15332 identifier ( expression-list )
15334 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15335 to an attribute. The TREE_PURPOSE of each node is the identifier
15336 indicating which attribute is in use. The TREE_VALUE represents
15337 the arguments, if any. */
15340 cp_parser_attribute_list (cp_parser
* parser
)
15342 tree attribute_list
= NULL_TREE
;
15343 bool save_translate_strings_p
= parser
->translate_strings_p
;
15345 parser
->translate_strings_p
= false;
15352 /* Look for the identifier. We also allow keywords here; for
15353 example `__attribute__ ((const))' is legal. */
15354 token
= cp_lexer_peek_token (parser
->lexer
);
15355 if (token
->type
== CPP_NAME
15356 || token
->type
== CPP_KEYWORD
)
15358 tree arguments
= NULL_TREE
;
15360 /* Consume the token. */
15361 token
= cp_lexer_consume_token (parser
->lexer
);
15363 /* Save away the identifier that indicates which attribute
15365 identifier
= token
->u
.value
;
15366 attribute
= build_tree_list (identifier
, NULL_TREE
);
15368 /* Peek at the next token. */
15369 token
= cp_lexer_peek_token (parser
->lexer
);
15370 /* If it's an `(', then parse the attribute arguments. */
15371 if (token
->type
== CPP_OPEN_PAREN
)
15373 arguments
= cp_parser_parenthesized_expression_list
15374 (parser
, true, /*cast_p=*/false,
15375 /*allow_expansion_p=*/false,
15376 /*non_constant_p=*/NULL
);
15377 /* Save the arguments away. */
15378 TREE_VALUE (attribute
) = arguments
;
15381 if (arguments
!= error_mark_node
)
15383 /* Add this attribute to the list. */
15384 TREE_CHAIN (attribute
) = attribute_list
;
15385 attribute_list
= attribute
;
15388 token
= cp_lexer_peek_token (parser
->lexer
);
15390 /* Now, look for more attributes. If the next token isn't a
15391 `,', we're done. */
15392 if (token
->type
!= CPP_COMMA
)
15395 /* Consume the comma and keep going. */
15396 cp_lexer_consume_token (parser
->lexer
);
15398 parser
->translate_strings_p
= save_translate_strings_p
;
15400 /* We built up the list in reverse order. */
15401 return nreverse (attribute_list
);
15404 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15405 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15406 current value of the PEDANTIC flag, regardless of whether or not
15407 the `__extension__' keyword is present. The caller is responsible
15408 for restoring the value of the PEDANTIC flag. */
15411 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
15413 /* Save the old value of the PEDANTIC flag. */
15414 *saved_pedantic
= pedantic
;
15416 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
15418 /* Consume the `__extension__' token. */
15419 cp_lexer_consume_token (parser
->lexer
);
15420 /* We're not being pedantic while the `__extension__' keyword is
15430 /* Parse a label declaration.
15433 __label__ label-declarator-seq ;
15435 label-declarator-seq:
15436 identifier , label-declarator-seq
15440 cp_parser_label_declaration (cp_parser
* parser
)
15442 /* Look for the `__label__' keyword. */
15443 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
15449 /* Look for an identifier. */
15450 identifier
= cp_parser_identifier (parser
);
15451 /* If we failed, stop. */
15452 if (identifier
== error_mark_node
)
15454 /* Declare it as a label. */
15455 finish_label_decl (identifier
);
15456 /* If the next token is a `;', stop. */
15457 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15459 /* Look for the `,' separating the label declarations. */
15460 cp_parser_require (parser
, CPP_COMMA
, "`,'");
15463 /* Look for the final `;'. */
15464 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
15467 /* Support Functions */
15469 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15470 NAME should have one of the representations used for an
15471 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15472 is returned. If PARSER->SCOPE is a dependent type, then a
15473 SCOPE_REF is returned.
15475 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15476 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15477 was formed. Abstractly, such entities should not be passed to this
15478 function, because they do not need to be looked up, but it is
15479 simpler to check for this special case here, rather than at the
15482 In cases not explicitly covered above, this function returns a
15483 DECL, OVERLOAD, or baselink representing the result of the lookup.
15484 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15487 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15488 (e.g., "struct") that was used. In that case bindings that do not
15489 refer to types are ignored.
15491 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15494 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15497 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15500 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15501 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15502 NULL_TREE otherwise. */
15505 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
15506 enum tag_types tag_type
,
15509 bool check_dependency
,
15510 tree
*ambiguous_decls
)
15514 tree object_type
= parser
->context
->object_type
;
15516 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
15517 flags
|= LOOKUP_COMPLAIN
;
15519 /* Assume that the lookup will be unambiguous. */
15520 if (ambiguous_decls
)
15521 *ambiguous_decls
= NULL_TREE
;
15523 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15524 no longer valid. Note that if we are parsing tentatively, and
15525 the parse fails, OBJECT_TYPE will be automatically restored. */
15526 parser
->context
->object_type
= NULL_TREE
;
15528 if (name
== error_mark_node
)
15529 return error_mark_node
;
15531 /* A template-id has already been resolved; there is no lookup to
15533 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
15535 if (BASELINK_P (name
))
15537 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
15538 == TEMPLATE_ID_EXPR
);
15542 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15543 it should already have been checked to make sure that the name
15544 used matches the type being destroyed. */
15545 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15549 /* Figure out to which type this destructor applies. */
15551 type
= parser
->scope
;
15552 else if (object_type
)
15553 type
= object_type
;
15555 type
= current_class_type
;
15556 /* If that's not a class type, there is no destructor. */
15557 if (!type
|| !CLASS_TYPE_P (type
))
15558 return error_mark_node
;
15559 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
15560 lazily_declare_fn (sfk_destructor
, type
);
15561 if (!CLASSTYPE_DESTRUCTORS (type
))
15562 return error_mark_node
;
15563 /* If it was a class type, return the destructor. */
15564 return CLASSTYPE_DESTRUCTORS (type
);
15567 /* By this point, the NAME should be an ordinary identifier. If
15568 the id-expression was a qualified name, the qualifying scope is
15569 stored in PARSER->SCOPE at this point. */
15570 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
15572 /* Perform the lookup. */
15577 if (parser
->scope
== error_mark_node
)
15578 return error_mark_node
;
15580 /* If the SCOPE is dependent, the lookup must be deferred until
15581 the template is instantiated -- unless we are explicitly
15582 looking up names in uninstantiated templates. Even then, we
15583 cannot look up the name if the scope is not a class type; it
15584 might, for example, be a template type parameter. */
15585 dependent_p
= (TYPE_P (parser
->scope
)
15586 && !(parser
->in_declarator_p
15587 && currently_open_class (parser
->scope
))
15588 && dependent_type_p (parser
->scope
));
15589 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
15596 /* The resolution to Core Issue 180 says that `struct
15597 A::B' should be considered a type-name, even if `A'
15599 type
= make_typename_type (parser
->scope
, name
, tag_type
,
15600 /*complain=*/tf_error
);
15601 decl
= TYPE_NAME (type
);
15603 else if (is_template
15604 && (cp_parser_next_token_ends_template_argument_p (parser
)
15605 || cp_lexer_next_token_is (parser
->lexer
,
15607 decl
= make_unbound_class_template (parser
->scope
,
15609 /*complain=*/tf_error
);
15611 decl
= build_qualified_name (/*type=*/NULL_TREE
,
15612 parser
->scope
, name
,
15617 tree pushed_scope
= NULL_TREE
;
15619 /* If PARSER->SCOPE is a dependent type, then it must be a
15620 class type, and we must not be checking dependencies;
15621 otherwise, we would have processed this lookup above. So
15622 that PARSER->SCOPE is not considered a dependent base by
15623 lookup_member, we must enter the scope here. */
15625 pushed_scope
= push_scope (parser
->scope
);
15626 /* If the PARSER->SCOPE is a template specialization, it
15627 may be instantiated during name lookup. In that case,
15628 errors may be issued. Even if we rollback the current
15629 tentative parse, those errors are valid. */
15630 decl
= lookup_qualified_name (parser
->scope
, name
,
15631 tag_type
!= none_type
,
15632 /*complain=*/true);
15634 pop_scope (pushed_scope
);
15636 parser
->qualifying_scope
= parser
->scope
;
15637 parser
->object_scope
= NULL_TREE
;
15639 else if (object_type
)
15641 tree object_decl
= NULL_TREE
;
15642 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15643 OBJECT_TYPE is not a class. */
15644 if (CLASS_TYPE_P (object_type
))
15645 /* If the OBJECT_TYPE is a template specialization, it may
15646 be instantiated during name lookup. In that case, errors
15647 may be issued. Even if we rollback the current tentative
15648 parse, those errors are valid. */
15649 object_decl
= lookup_member (object_type
,
15652 tag_type
!= none_type
);
15653 /* Look it up in the enclosing context, too. */
15654 decl
= lookup_name_real (name
, tag_type
!= none_type
,
15656 /*block_p=*/true, is_namespace
, flags
);
15657 parser
->object_scope
= object_type
;
15658 parser
->qualifying_scope
= NULL_TREE
;
15660 decl
= object_decl
;
15664 decl
= lookup_name_real (name
, tag_type
!= none_type
,
15666 /*block_p=*/true, is_namespace
, flags
);
15667 parser
->qualifying_scope
= NULL_TREE
;
15668 parser
->object_scope
= NULL_TREE
;
15671 /* If the lookup failed, let our caller know. */
15672 if (!decl
|| decl
== error_mark_node
)
15673 return error_mark_node
;
15675 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15676 if (TREE_CODE (decl
) == TREE_LIST
)
15678 if (ambiguous_decls
)
15679 *ambiguous_decls
= decl
;
15680 /* The error message we have to print is too complicated for
15681 cp_parser_error, so we incorporate its actions directly. */
15682 if (!cp_parser_simulate_error (parser
))
15684 error ("reference to %qD is ambiguous", name
);
15685 print_candidates (decl
);
15687 return error_mark_node
;
15690 gcc_assert (DECL_P (decl
)
15691 || TREE_CODE (decl
) == OVERLOAD
15692 || TREE_CODE (decl
) == SCOPE_REF
15693 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
15694 || BASELINK_P (decl
));
15696 /* If we have resolved the name of a member declaration, check to
15697 see if the declaration is accessible. When the name resolves to
15698 set of overloaded functions, accessibility is checked when
15699 overload resolution is done.
15701 During an explicit instantiation, access is not checked at all,
15702 as per [temp.explicit]. */
15704 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
15709 /* Like cp_parser_lookup_name, but for use in the typical case where
15710 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15711 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15714 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
15716 return cp_parser_lookup_name (parser
, name
,
15718 /*is_template=*/false,
15719 /*is_namespace=*/false,
15720 /*check_dependency=*/true,
15721 /*ambiguous_decls=*/NULL
);
15724 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15725 the current context, return the TYPE_DECL. If TAG_NAME_P is
15726 true, the DECL indicates the class being defined in a class-head,
15727 or declared in an elaborated-type-specifier.
15729 Otherwise, return DECL. */
15732 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
15734 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15735 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15738 template <typename T> struct B;
15741 template <typename T> struct A::B {};
15743 Similarly, in an elaborated-type-specifier:
15745 namespace N { struct X{}; }
15748 template <typename T> friend struct N::X;
15751 However, if the DECL refers to a class type, and we are in
15752 the scope of the class, then the name lookup automatically
15753 finds the TYPE_DECL created by build_self_reference rather
15754 than a TEMPLATE_DECL. For example, in:
15756 template <class T> struct S {
15760 there is no need to handle such case. */
15762 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
15763 return DECL_TEMPLATE_RESULT (decl
);
15768 /* If too many, or too few, template-parameter lists apply to the
15769 declarator, issue an error message. Returns TRUE if all went well,
15770 and FALSE otherwise. */
15773 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
15774 cp_declarator
*declarator
)
15776 unsigned num_templates
;
15778 /* We haven't seen any classes that involve template parameters yet. */
15781 switch (declarator
->kind
)
15784 if (declarator
->u
.id
.qualifying_scope
)
15789 scope
= declarator
->u
.id
.qualifying_scope
;
15790 member
= declarator
->u
.id
.unqualified_name
;
15792 while (scope
&& CLASS_TYPE_P (scope
))
15794 /* You're supposed to have one `template <...>'
15795 for every template class, but you don't need one
15796 for a full specialization. For example:
15798 template <class T> struct S{};
15799 template <> struct S<int> { void f(); };
15800 void S<int>::f () {}
15802 is correct; there shouldn't be a `template <>' for
15803 the definition of `S<int>::f'. */
15804 if (!CLASSTYPE_TEMPLATE_INFO (scope
))
15805 /* If SCOPE does not have template information of any
15806 kind, then it is not a template, nor is it nested
15807 within a template. */
15809 if (explicit_class_specialization_p (scope
))
15811 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
15814 scope
= TYPE_CONTEXT (scope
);
15817 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
15818 == TEMPLATE_ID_EXPR
)
15819 /* If the DECLARATOR has the form `X<y>' then it uses one
15820 additional level of template parameters. */
15823 return cp_parser_check_template_parameters (parser
,
15829 case cdk_reference
:
15831 return (cp_parser_check_declarator_template_parameters
15832 (parser
, declarator
->declarator
));
15838 gcc_unreachable ();
15843 /* NUM_TEMPLATES were used in the current declaration. If that is
15844 invalid, return FALSE and issue an error messages. Otherwise,
15848 cp_parser_check_template_parameters (cp_parser
* parser
,
15849 unsigned num_templates
)
15851 /* If there are more template classes than parameter lists, we have
15854 template <class T> void S<T>::R<T>::f (); */
15855 if (parser
->num_template_parameter_lists
< num_templates
)
15857 error ("too few template-parameter-lists");
15860 /* If there are the same number of template classes and parameter
15861 lists, that's OK. */
15862 if (parser
->num_template_parameter_lists
== num_templates
)
15864 /* If there are more, but only one more, then we are referring to a
15865 member template. That's OK too. */
15866 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
15868 /* Otherwise, there are too many template parameter lists. We have
15871 template <class T> template <class U> void S::f(); */
15872 error ("too many template-parameter-lists");
15876 /* Parse an optional `::' token indicating that the following name is
15877 from the global namespace. If so, PARSER->SCOPE is set to the
15878 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15879 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15880 Returns the new value of PARSER->SCOPE, if the `::' token is
15881 present, and NULL_TREE otherwise. */
15884 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
15888 /* Peek at the next token. */
15889 token
= cp_lexer_peek_token (parser
->lexer
);
15890 /* If we're looking at a `::' token then we're starting from the
15891 global namespace, not our current location. */
15892 if (token
->type
== CPP_SCOPE
)
15894 /* Consume the `::' token. */
15895 cp_lexer_consume_token (parser
->lexer
);
15896 /* Set the SCOPE so that we know where to start the lookup. */
15897 parser
->scope
= global_namespace
;
15898 parser
->qualifying_scope
= global_namespace
;
15899 parser
->object_scope
= NULL_TREE
;
15901 return parser
->scope
;
15903 else if (!current_scope_valid_p
)
15905 parser
->scope
= NULL_TREE
;
15906 parser
->qualifying_scope
= NULL_TREE
;
15907 parser
->object_scope
= NULL_TREE
;
15913 /* Returns TRUE if the upcoming token sequence is the start of a
15914 constructor declarator. If FRIEND_P is true, the declarator is
15915 preceded by the `friend' specifier. */
15918 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
15920 bool constructor_p
;
15921 tree type_decl
= NULL_TREE
;
15922 bool nested_name_p
;
15923 cp_token
*next_token
;
15925 /* The common case is that this is not a constructor declarator, so
15926 try to avoid doing lots of work if at all possible. It's not
15927 valid declare a constructor at function scope. */
15928 if (parser
->in_function_body
)
15930 /* And only certain tokens can begin a constructor declarator. */
15931 next_token
= cp_lexer_peek_token (parser
->lexer
);
15932 if (next_token
->type
!= CPP_NAME
15933 && next_token
->type
!= CPP_SCOPE
15934 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
15935 && next_token
->type
!= CPP_TEMPLATE_ID
)
15938 /* Parse tentatively; we are going to roll back all of the tokens
15940 cp_parser_parse_tentatively (parser
);
15941 /* Assume that we are looking at a constructor declarator. */
15942 constructor_p
= true;
15944 /* Look for the optional `::' operator. */
15945 cp_parser_global_scope_opt (parser
,
15946 /*current_scope_valid_p=*/false);
15947 /* Look for the nested-name-specifier. */
15949 = (cp_parser_nested_name_specifier_opt (parser
,
15950 /*typename_keyword_p=*/false,
15951 /*check_dependency_p=*/false,
15953 /*is_declaration=*/false)
15955 /* Outside of a class-specifier, there must be a
15956 nested-name-specifier. */
15957 if (!nested_name_p
&&
15958 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
15960 constructor_p
= false;
15961 /* If we still think that this might be a constructor-declarator,
15962 look for a class-name. */
15967 template <typename T> struct S { S(); };
15968 template <typename T> S<T>::S ();
15970 we must recognize that the nested `S' names a class.
15973 template <typename T> S<T>::S<T> ();
15975 we must recognize that the nested `S' names a template. */
15976 type_decl
= cp_parser_class_name (parser
,
15977 /*typename_keyword_p=*/false,
15978 /*template_keyword_p=*/false,
15980 /*check_dependency_p=*/false,
15981 /*class_head_p=*/false,
15982 /*is_declaration=*/false);
15983 /* If there was no class-name, then this is not a constructor. */
15984 constructor_p
= !cp_parser_error_occurred (parser
);
15987 /* If we're still considering a constructor, we have to see a `(',
15988 to begin the parameter-declaration-clause, followed by either a
15989 `)', an `...', or a decl-specifier. We need to check for a
15990 type-specifier to avoid being fooled into thinking that:
15994 is a constructor. (It is actually a function named `f' that
15995 takes one parameter (of type `int') and returns a value of type
15998 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
16000 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
16001 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
16002 /* A parameter declaration begins with a decl-specifier,
16003 which is either the "attribute" keyword, a storage class
16004 specifier, or (usually) a type-specifier. */
16005 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
16008 tree pushed_scope
= NULL_TREE
;
16009 unsigned saved_num_template_parameter_lists
;
16011 /* Names appearing in the type-specifier should be looked up
16012 in the scope of the class. */
16013 if (current_class_type
)
16017 type
= TREE_TYPE (type_decl
);
16018 if (TREE_CODE (type
) == TYPENAME_TYPE
)
16020 type
= resolve_typename_type (type
,
16021 /*only_current_p=*/false);
16022 if (type
== error_mark_node
)
16024 cp_parser_abort_tentative_parse (parser
);
16028 pushed_scope
= push_scope (type
);
16031 /* Inside the constructor parameter list, surrounding
16032 template-parameter-lists do not apply. */
16033 saved_num_template_parameter_lists
16034 = parser
->num_template_parameter_lists
;
16035 parser
->num_template_parameter_lists
= 0;
16037 /* Look for the type-specifier. */
16038 cp_parser_type_specifier (parser
,
16039 CP_PARSER_FLAGS_NONE
,
16040 /*decl_specs=*/NULL
,
16041 /*is_declarator=*/true,
16042 /*declares_class_or_enum=*/NULL
,
16043 /*is_cv_qualifier=*/NULL
);
16045 parser
->num_template_parameter_lists
16046 = saved_num_template_parameter_lists
;
16048 /* Leave the scope of the class. */
16050 pop_scope (pushed_scope
);
16052 constructor_p
= !cp_parser_error_occurred (parser
);
16056 constructor_p
= false;
16057 /* We did not really want to consume any tokens. */
16058 cp_parser_abort_tentative_parse (parser
);
16060 return constructor_p
;
16063 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16064 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16065 they must be performed once we are in the scope of the function.
16067 Returns the function defined. */
16070 cp_parser_function_definition_from_specifiers_and_declarator
16071 (cp_parser
* parser
,
16072 cp_decl_specifier_seq
*decl_specifiers
,
16074 const cp_declarator
*declarator
)
16079 /* Begin the function-definition. */
16080 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
16082 /* The things we're about to see are not directly qualified by any
16083 template headers we've seen thus far. */
16084 reset_specialization ();
16086 /* If there were names looked up in the decl-specifier-seq that we
16087 did not check, check them now. We must wait until we are in the
16088 scope of the function to perform the checks, since the function
16089 might be a friend. */
16090 perform_deferred_access_checks ();
16094 /* Skip the entire function. */
16095 cp_parser_skip_to_end_of_block_or_statement (parser
);
16096 fn
= error_mark_node
;
16098 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
16100 /* Seen already, skip it. An error message has already been output. */
16101 cp_parser_skip_to_end_of_block_or_statement (parser
);
16102 fn
= current_function_decl
;
16103 current_function_decl
= NULL_TREE
;
16104 /* If this is a function from a class, pop the nested class. */
16105 if (current_class_name
)
16106 pop_nested_class ();
16109 fn
= cp_parser_function_definition_after_declarator (parser
,
16110 /*inline_p=*/false);
16115 /* Parse the part of a function-definition that follows the
16116 declarator. INLINE_P is TRUE iff this function is an inline
16117 function defined with a class-specifier.
16119 Returns the function defined. */
16122 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
16126 bool ctor_initializer_p
= false;
16127 bool saved_in_unbraced_linkage_specification_p
;
16128 bool saved_in_function_body
;
16129 unsigned saved_num_template_parameter_lists
;
16131 saved_in_function_body
= parser
->in_function_body
;
16132 parser
->in_function_body
= true;
16133 /* If the next token is `return', then the code may be trying to
16134 make use of the "named return value" extension that G++ used to
16136 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
16138 /* Consume the `return' keyword. */
16139 cp_lexer_consume_token (parser
->lexer
);
16140 /* Look for the identifier that indicates what value is to be
16142 cp_parser_identifier (parser
);
16143 /* Issue an error message. */
16144 error ("named return values are no longer supported");
16145 /* Skip tokens until we reach the start of the function body. */
16148 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16149 if (token
->type
== CPP_OPEN_BRACE
16150 || token
->type
== CPP_EOF
16151 || token
->type
== CPP_PRAGMA_EOL
)
16153 cp_lexer_consume_token (parser
->lexer
);
16156 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16157 anything declared inside `f'. */
16158 saved_in_unbraced_linkage_specification_p
16159 = parser
->in_unbraced_linkage_specification_p
;
16160 parser
->in_unbraced_linkage_specification_p
= false;
16161 /* Inside the function, surrounding template-parameter-lists do not
16163 saved_num_template_parameter_lists
16164 = parser
->num_template_parameter_lists
;
16165 parser
->num_template_parameter_lists
= 0;
16166 /* If the next token is `try', then we are looking at a
16167 function-try-block. */
16168 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
16169 ctor_initializer_p
= cp_parser_function_try_block (parser
);
16170 /* A function-try-block includes the function-body, so we only do
16171 this next part if we're not processing a function-try-block. */
16174 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
16176 /* Finish the function. */
16177 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
16178 (inline_p
? 2 : 0));
16179 /* Generate code for it, if necessary. */
16180 expand_or_defer_fn (fn
);
16181 /* Restore the saved values. */
16182 parser
->in_unbraced_linkage_specification_p
16183 = saved_in_unbraced_linkage_specification_p
;
16184 parser
->num_template_parameter_lists
16185 = saved_num_template_parameter_lists
;
16186 parser
->in_function_body
= saved_in_function_body
;
16191 /* Parse a template-declaration, assuming that the `export' (and
16192 `extern') keywords, if present, has already been scanned. MEMBER_P
16193 is as for cp_parser_template_declaration. */
16196 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
16198 tree decl
= NULL_TREE
;
16199 VEC (deferred_access_check
,gc
) *checks
;
16200 tree parameter_list
;
16201 bool friend_p
= false;
16202 bool need_lang_pop
;
16204 /* Look for the `template' keyword. */
16205 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
16209 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
16211 if (at_class_scope_p () && current_function_decl
)
16213 /* 14.5.2.2 [temp.mem]
16215 A local class shall not have member templates. */
16216 error ("invalid declaration of member template in local class");
16217 cp_parser_skip_to_end_of_block_or_statement (parser
);
16222 A template ... shall not have C linkage. */
16223 if (current_lang_name
== lang_name_c
)
16225 error ("template with C linkage");
16226 /* Give it C++ linkage to avoid confusing other parts of the
16228 push_lang_context (lang_name_cplusplus
);
16229 need_lang_pop
= true;
16232 need_lang_pop
= false;
16234 /* We cannot perform access checks on the template parameter
16235 declarations until we know what is being declared, just as we
16236 cannot check the decl-specifier list. */
16237 push_deferring_access_checks (dk_deferred
);
16239 /* If the next token is `>', then we have an invalid
16240 specialization. Rather than complain about an invalid template
16241 parameter, issue an error message here. */
16242 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
16244 cp_parser_error (parser
, "invalid explicit specialization");
16245 begin_specialization ();
16246 parameter_list
= NULL_TREE
;
16249 /* Parse the template parameters. */
16250 parameter_list
= cp_parser_template_parameter_list (parser
);
16252 /* Get the deferred access checks from the parameter list. These
16253 will be checked once we know what is being declared, as for a
16254 member template the checks must be performed in the scope of the
16255 class containing the member. */
16256 checks
= get_deferred_access_checks ();
16258 /* Look for the `>'. */
16259 cp_parser_skip_to_end_of_template_parameter_list (parser
);
16260 /* We just processed one more parameter list. */
16261 ++parser
->num_template_parameter_lists
;
16262 /* If the next token is `template', there are more template
16264 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
16266 cp_parser_template_declaration_after_export (parser
, member_p
);
16269 /* There are no access checks when parsing a template, as we do not
16270 know if a specialization will be a friend. */
16271 push_deferring_access_checks (dk_no_check
);
16272 decl
= cp_parser_single_declaration (parser
,
16276 pop_deferring_access_checks ();
16278 /* If this is a member template declaration, let the front
16280 if (member_p
&& !friend_p
&& decl
)
16282 if (TREE_CODE (decl
) == TYPE_DECL
)
16283 cp_parser_check_access_in_redeclaration (decl
);
16285 decl
= finish_member_template_decl (decl
);
16287 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
16288 make_friend_class (current_class_type
, TREE_TYPE (decl
),
16289 /*complain=*/true);
16291 /* We are done with the current parameter list. */
16292 --parser
->num_template_parameter_lists
;
16294 pop_deferring_access_checks ();
16297 finish_template_decl (parameter_list
);
16299 /* Register member declarations. */
16300 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
16301 finish_member_declaration (decl
);
16302 /* For the erroneous case of a template with C linkage, we pushed an
16303 implicit C++ linkage scope; exit that scope now. */
16305 pop_lang_context ();
16306 /* If DECL is a function template, we must return to parse it later.
16307 (Even though there is no definition, there might be default
16308 arguments that need handling.) */
16309 if (member_p
&& decl
16310 && (TREE_CODE (decl
) == FUNCTION_DECL
16311 || DECL_FUNCTION_TEMPLATE_P (decl
)))
16312 TREE_VALUE (parser
->unparsed_functions_queues
)
16313 = tree_cons (NULL_TREE
, decl
,
16314 TREE_VALUE (parser
->unparsed_functions_queues
));
16317 /* Perform the deferred access checks from a template-parameter-list.
16318 CHECKS is a TREE_LIST of access checks, as returned by
16319 get_deferred_access_checks. */
16322 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check
,gc
)* checks
)
16324 ++processing_template_parmlist
;
16325 perform_access_checks (checks
);
16326 --processing_template_parmlist
;
16329 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16330 `function-definition' sequence. MEMBER_P is true, this declaration
16331 appears in a class scope.
16333 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16334 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16337 cp_parser_single_declaration (cp_parser
* parser
,
16338 VEC (deferred_access_check
,gc
)* checks
,
16342 int declares_class_or_enum
;
16343 tree decl
= NULL_TREE
;
16344 cp_decl_specifier_seq decl_specifiers
;
16345 bool function_definition_p
= false;
16347 /* This function is only used when processing a template
16349 gcc_assert (innermost_scope_kind () == sk_template_parms
16350 || innermost_scope_kind () == sk_template_spec
);
16352 /* Defer access checks until we know what is being declared. */
16353 push_deferring_access_checks (dk_deferred
);
16355 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16357 cp_parser_decl_specifier_seq (parser
,
16358 CP_PARSER_FLAGS_OPTIONAL
,
16360 &declares_class_or_enum
);
16362 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
16364 /* There are no template typedefs. */
16365 if (decl_specifiers
.specs
[(int) ds_typedef
])
16367 error ("template declaration of %qs", "typedef");
16368 decl
= error_mark_node
;
16371 /* Gather up the access checks that occurred the
16372 decl-specifier-seq. */
16373 stop_deferring_access_checks ();
16375 /* Check for the declaration of a template class. */
16376 if (declares_class_or_enum
)
16378 if (cp_parser_declares_only_class_p (parser
))
16380 decl
= shadow_tag (&decl_specifiers
);
16385 friend template <typename T> struct A<T>::B;
16388 A<T>::B will be represented by a TYPENAME_TYPE, and
16389 therefore not recognized by shadow_tag. */
16390 if (friend_p
&& *friend_p
16392 && decl_specifiers
.type
16393 && TYPE_P (decl_specifiers
.type
))
16394 decl
= decl_specifiers
.type
;
16396 if (decl
&& decl
!= error_mark_node
)
16397 decl
= TYPE_NAME (decl
);
16399 decl
= error_mark_node
;
16401 /* Perform access checks for template parameters. */
16402 cp_parser_perform_template_parameter_access_checks (checks
);
16405 /* If it's not a template class, try for a template function. If
16406 the next token is a `;', then this declaration does not declare
16407 anything. But, if there were errors in the decl-specifiers, then
16408 the error might well have come from an attempted class-specifier.
16409 In that case, there's no need to warn about a missing declarator. */
16411 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
16412 || decl_specifiers
.type
!= error_mark_node
))
16413 decl
= cp_parser_init_declarator (parser
,
16416 /*function_definition_allowed_p=*/true,
16418 declares_class_or_enum
,
16419 &function_definition_p
);
16421 pop_deferring_access_checks ();
16423 /* Clear any current qualification; whatever comes next is the start
16424 of something new. */
16425 parser
->scope
= NULL_TREE
;
16426 parser
->qualifying_scope
= NULL_TREE
;
16427 parser
->object_scope
= NULL_TREE
;
16428 /* Look for a trailing `;' after the declaration. */
16429 if (!function_definition_p
16430 && (decl
== error_mark_node
16431 || !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")))
16432 cp_parser_skip_to_end_of_block_or_statement (parser
);
16437 /* Parse a cast-expression that is not the operand of a unary "&". */
16440 cp_parser_simple_cast_expression (cp_parser
*parser
)
16442 return cp_parser_cast_expression (parser
, /*address_p=*/false,
16446 /* Parse a functional cast to TYPE. Returns an expression
16447 representing the cast. */
16450 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
16452 tree expression_list
;
16456 = cp_parser_parenthesized_expression_list (parser
, false,
16458 /*allow_expansion_p=*/true,
16459 /*non_constant_p=*/NULL
);
16461 cast
= build_functional_cast (type
, expression_list
);
16462 /* [expr.const]/1: In an integral constant expression "only type
16463 conversions to integral or enumeration type can be used". */
16464 if (TREE_CODE (type
) == TYPE_DECL
)
16465 type
= TREE_TYPE (type
);
16466 if (cast
!= error_mark_node
16467 && !cast_valid_in_integral_constant_expression_p (type
)
16468 && (cp_parser_non_integral_constant_expression
16469 (parser
, "a call to a constructor")))
16470 return error_mark_node
;
16474 /* Save the tokens that make up the body of a member function defined
16475 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16476 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16477 specifiers applied to the declaration. Returns the FUNCTION_DECL
16478 for the member function. */
16481 cp_parser_save_member_function_body (cp_parser
* parser
,
16482 cp_decl_specifier_seq
*decl_specifiers
,
16483 cp_declarator
*declarator
,
16490 /* Create the function-declaration. */
16491 fn
= start_method (decl_specifiers
, declarator
, attributes
);
16492 /* If something went badly wrong, bail out now. */
16493 if (fn
== error_mark_node
)
16495 /* If there's a function-body, skip it. */
16496 if (cp_parser_token_starts_function_definition_p
16497 (cp_lexer_peek_token (parser
->lexer
)))
16498 cp_parser_skip_to_end_of_block_or_statement (parser
);
16499 return error_mark_node
;
16502 /* Remember it, if there default args to post process. */
16503 cp_parser_save_default_args (parser
, fn
);
16505 /* Save away the tokens that make up the body of the
16507 first
= parser
->lexer
->next_token
;
16508 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
16509 /* Handle function try blocks. */
16510 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
16511 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
16512 last
= parser
->lexer
->next_token
;
16514 /* Save away the inline definition; we will process it when the
16515 class is complete. */
16516 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
16517 DECL_PENDING_INLINE_P (fn
) = 1;
16519 /* We need to know that this was defined in the class, so that
16520 friend templates are handled correctly. */
16521 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
16523 /* We're done with the inline definition. */
16524 finish_method (fn
);
16526 /* Add FN to the queue of functions to be parsed later. */
16527 TREE_VALUE (parser
->unparsed_functions_queues
)
16528 = tree_cons (NULL_TREE
, fn
,
16529 TREE_VALUE (parser
->unparsed_functions_queues
));
16534 /* Parse a template-argument-list, as well as the trailing ">" (but
16535 not the opening ">"). See cp_parser_template_argument_list for the
16539 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
16543 tree saved_qualifying_scope
;
16544 tree saved_object_scope
;
16545 bool saved_greater_than_is_operator_p
;
16546 bool saved_skip_evaluation
;
16550 When parsing a template-id, the first non-nested `>' is taken as
16551 the end of the template-argument-list rather than a greater-than
16553 saved_greater_than_is_operator_p
16554 = parser
->greater_than_is_operator_p
;
16555 parser
->greater_than_is_operator_p
= false;
16556 /* Parsing the argument list may modify SCOPE, so we save it
16558 saved_scope
= parser
->scope
;
16559 saved_qualifying_scope
= parser
->qualifying_scope
;
16560 saved_object_scope
= parser
->object_scope
;
16561 /* We need to evaluate the template arguments, even though this
16562 template-id may be nested within a "sizeof". */
16563 saved_skip_evaluation
= skip_evaluation
;
16564 skip_evaluation
= false;
16565 /* Parse the template-argument-list itself. */
16566 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
16567 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16568 arguments
= NULL_TREE
;
16570 arguments
= cp_parser_template_argument_list (parser
);
16571 /* Look for the `>' that ends the template-argument-list. If we find
16572 a '>>' instead, it's probably just a typo. */
16573 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16577 /* In C++0x, a `>>' in a template argument list or cast
16578 expression is considered to be two separate `>'
16579 tokens. So, change the current token to a `>', but don't
16580 consume it: it will be consumed later when the outer
16581 template argument list (or cast expression) is parsed.
16582 Note that this replacement of `>' for `>>' is necessary
16583 even if we are parsing tentatively: in the tentative
16584 case, after calling
16585 cp_parser_enclosed_template_argument_list we will always
16586 throw away all of the template arguments and the first
16587 closing `>', either because the template argument list
16588 was erroneous or because we are replacing those tokens
16589 with a CPP_TEMPLATE_ID token. The second `>' (which will
16590 not have been thrown away) is needed either to close an
16591 outer template argument list or to complete a new-style
16593 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16594 token
->type
= CPP_GREATER
;
16596 else if (!saved_greater_than_is_operator_p
)
16598 /* If we're in a nested template argument list, the '>>' has
16599 to be a typo for '> >'. We emit the error message, but we
16600 continue parsing and we push a '>' as next token, so that
16601 the argument list will be parsed correctly. Note that the
16602 global source location is still on the token before the
16603 '>>', so we need to say explicitly where we want it. */
16604 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16605 error ("%H%<>>%> should be %<> >%> "
16606 "within a nested template argument list",
16609 token
->type
= CPP_GREATER
;
16613 /* If this is not a nested template argument list, the '>>'
16614 is a typo for '>'. Emit an error message and continue.
16615 Same deal about the token location, but here we can get it
16616 right by consuming the '>>' before issuing the diagnostic. */
16617 cp_lexer_consume_token (parser
->lexer
);
16618 error ("spurious %<>>%>, use %<>%> to terminate "
16619 "a template argument list");
16623 cp_parser_skip_to_end_of_template_parameter_list (parser
);
16624 /* The `>' token might be a greater-than operator again now. */
16625 parser
->greater_than_is_operator_p
16626 = saved_greater_than_is_operator_p
;
16627 /* Restore the SAVED_SCOPE. */
16628 parser
->scope
= saved_scope
;
16629 parser
->qualifying_scope
= saved_qualifying_scope
;
16630 parser
->object_scope
= saved_object_scope
;
16631 skip_evaluation
= saved_skip_evaluation
;
16636 /* MEMBER_FUNCTION is a member function, or a friend. If default
16637 arguments, or the body of the function have not yet been parsed,
16641 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
16643 /* If this member is a template, get the underlying
16645 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
16646 member_function
= DECL_TEMPLATE_RESULT (member_function
);
16648 /* There should not be any class definitions in progress at this
16649 point; the bodies of members are only parsed outside of all class
16651 gcc_assert (parser
->num_classes_being_defined
== 0);
16652 /* While we're parsing the member functions we might encounter more
16653 classes. We want to handle them right away, but we don't want
16654 them getting mixed up with functions that are currently in the
16656 parser
->unparsed_functions_queues
16657 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
16659 /* Make sure that any template parameters are in scope. */
16660 maybe_begin_member_template_processing (member_function
);
16662 /* If the body of the function has not yet been parsed, parse it
16664 if (DECL_PENDING_INLINE_P (member_function
))
16666 tree function_scope
;
16667 cp_token_cache
*tokens
;
16669 /* The function is no longer pending; we are processing it. */
16670 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
16671 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
16672 DECL_PENDING_INLINE_P (member_function
) = 0;
16674 /* If this is a local class, enter the scope of the containing
16676 function_scope
= current_function_decl
;
16677 if (function_scope
)
16678 push_function_context_to (function_scope
);
16681 /* Push the body of the function onto the lexer stack. */
16682 cp_parser_push_lexer_for_tokens (parser
, tokens
);
16684 /* Let the front end know that we going to be defining this
16686 start_preparsed_function (member_function
, NULL_TREE
,
16687 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
16689 /* Don't do access checking if it is a templated function. */
16690 if (processing_template_decl
)
16691 push_deferring_access_checks (dk_no_check
);
16693 /* Now, parse the body of the function. */
16694 cp_parser_function_definition_after_declarator (parser
,
16695 /*inline_p=*/true);
16697 if (processing_template_decl
)
16698 pop_deferring_access_checks ();
16700 /* Leave the scope of the containing function. */
16701 if (function_scope
)
16702 pop_function_context_from (function_scope
);
16703 cp_parser_pop_lexer (parser
);
16706 /* Remove any template parameters from the symbol table. */
16707 maybe_end_member_template_processing ();
16709 /* Restore the queue. */
16710 parser
->unparsed_functions_queues
16711 = TREE_CHAIN (parser
->unparsed_functions_queues
);
16714 /* If DECL contains any default args, remember it on the unparsed
16715 functions queue. */
16718 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
16722 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
16724 probe
= TREE_CHAIN (probe
))
16725 if (TREE_PURPOSE (probe
))
16727 TREE_PURPOSE (parser
->unparsed_functions_queues
)
16728 = tree_cons (current_class_type
, decl
,
16729 TREE_PURPOSE (parser
->unparsed_functions_queues
));
16734 /* FN is a FUNCTION_DECL which may contains a parameter with an
16735 unparsed DEFAULT_ARG. Parse the default args now. This function
16736 assumes that the current scope is the scope in which the default
16737 argument should be processed. */
16740 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
16742 bool saved_local_variables_forbidden_p
;
16745 /* While we're parsing the default args, we might (due to the
16746 statement expression extension) encounter more classes. We want
16747 to handle them right away, but we don't want them getting mixed
16748 up with default args that are currently in the queue. */
16749 parser
->unparsed_functions_queues
16750 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
16752 /* Local variable names (and the `this' keyword) may not appear
16753 in a default argument. */
16754 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
16755 parser
->local_variables_forbidden_p
= true;
16757 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
16759 parm
= TREE_CHAIN (parm
))
16761 cp_token_cache
*tokens
;
16762 tree default_arg
= TREE_PURPOSE (parm
);
16764 VEC(tree
,gc
) *insts
;
16771 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
16772 /* This can happen for a friend declaration for a function
16773 already declared with default arguments. */
16776 /* Push the saved tokens for the default argument onto the parser's
16778 tokens
= DEFARG_TOKENS (default_arg
);
16779 cp_parser_push_lexer_for_tokens (parser
, tokens
);
16781 /* Parse the assignment-expression. */
16782 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
16784 if (!processing_template_decl
)
16785 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
16787 TREE_PURPOSE (parm
) = parsed_arg
;
16789 /* Update any instantiations we've already created. */
16790 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
16791 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
16792 TREE_PURPOSE (copy
) = parsed_arg
;
16794 /* If the token stream has not been completely used up, then
16795 there was extra junk after the end of the default
16797 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
16798 cp_parser_error (parser
, "expected %<,%>");
16800 /* Revert to the main lexer. */
16801 cp_parser_pop_lexer (parser
);
16804 /* Make sure no default arg is missing. */
16805 check_default_args (fn
);
16807 /* Restore the state of local_variables_forbidden_p. */
16808 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
16810 /* Restore the queue. */
16811 parser
->unparsed_functions_queues
16812 = TREE_CHAIN (parser
->unparsed_functions_queues
);
16815 /* Parse the operand of `sizeof' (or a similar operator). Returns
16816 either a TYPE or an expression, depending on the form of the
16817 input. The KEYWORD indicates which kind of expression we have
16821 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
16823 static const char *format
;
16824 tree expr
= NULL_TREE
;
16825 const char *saved_message
;
16826 bool saved_integral_constant_expression_p
;
16827 bool saved_non_integral_constant_expression_p
;
16828 bool pack_expansion_p
= false;
16830 /* Initialize FORMAT the first time we get here. */
16832 format
= "types may not be defined in '%s' expressions";
16834 /* Types cannot be defined in a `sizeof' expression. Save away the
16836 saved_message
= parser
->type_definition_forbidden_message
;
16837 /* And create the new one. */
16838 parser
->type_definition_forbidden_message
16839 = XNEWVEC (const char, strlen (format
)
16840 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
16842 sprintf ((char *) parser
->type_definition_forbidden_message
,
16843 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
16845 /* The restrictions on constant-expressions do not apply inside
16846 sizeof expressions. */
16847 saved_integral_constant_expression_p
16848 = parser
->integral_constant_expression_p
;
16849 saved_non_integral_constant_expression_p
16850 = parser
->non_integral_constant_expression_p
;
16851 parser
->integral_constant_expression_p
= false;
16853 /* If it's a `...', then we are computing the length of a parameter
16855 if (keyword
== RID_SIZEOF
16856 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16858 /* Consume the `...'. */
16859 cp_lexer_consume_token (parser
->lexer
);
16860 maybe_warn_variadic_templates ();
16862 /* Note that this is an expansion. */
16863 pack_expansion_p
= true;
16866 /* Do not actually evaluate the expression. */
16868 /* If it's a `(', then we might be looking at the type-id
16870 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16873 bool saved_in_type_id_in_expr_p
;
16875 /* We can't be sure yet whether we're looking at a type-id or an
16877 cp_parser_parse_tentatively (parser
);
16878 /* Consume the `('. */
16879 cp_lexer_consume_token (parser
->lexer
);
16880 /* Parse the type-id. */
16881 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
16882 parser
->in_type_id_in_expr_p
= true;
16883 type
= cp_parser_type_id (parser
);
16884 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
16885 /* Now, look for the trailing `)'. */
16886 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
16887 /* If all went well, then we're done. */
16888 if (cp_parser_parse_definitely (parser
))
16890 cp_decl_specifier_seq decl_specs
;
16892 /* Build a trivial decl-specifier-seq. */
16893 clear_decl_specs (&decl_specs
);
16894 decl_specs
.type
= type
;
16896 /* Call grokdeclarator to figure out what type this is. */
16897 expr
= grokdeclarator (NULL
,
16901 /*attrlist=*/NULL
);
16905 /* If the type-id production did not work out, then we must be
16906 looking at the unary-expression production. */
16908 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
16911 if (pack_expansion_p
)
16912 /* Build a pack expansion. */
16913 expr
= make_pack_expansion (expr
);
16915 /* Go back to evaluating expressions. */
16918 /* Free the message we created. */
16919 free ((char *) parser
->type_definition_forbidden_message
);
16920 /* And restore the old one. */
16921 parser
->type_definition_forbidden_message
= saved_message
;
16922 parser
->integral_constant_expression_p
16923 = saved_integral_constant_expression_p
;
16924 parser
->non_integral_constant_expression_p
16925 = saved_non_integral_constant_expression_p
;
16930 /* If the current declaration has no declarator, return true. */
16933 cp_parser_declares_only_class_p (cp_parser
*parser
)
16935 /* If the next token is a `;' or a `,' then there is no
16937 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
16938 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16941 /* Update the DECL_SPECS to reflect the storage class indicated by
16945 cp_parser_set_storage_class (cp_parser
*parser
,
16946 cp_decl_specifier_seq
*decl_specs
,
16949 cp_storage_class storage_class
;
16951 if (parser
->in_unbraced_linkage_specification_p
)
16953 error ("invalid use of %qD in linkage specification",
16954 ridpointers
[keyword
]);
16957 else if (decl_specs
->storage_class
!= sc_none
)
16959 decl_specs
->conflicting_specifiers_p
= true;
16963 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
16964 && decl_specs
->specs
[(int) ds_thread
])
16966 error ("%<__thread%> before %qD", ridpointers
[keyword
]);
16967 decl_specs
->specs
[(int) ds_thread
] = 0;
16973 storage_class
= sc_auto
;
16976 storage_class
= sc_register
;
16979 storage_class
= sc_static
;
16982 storage_class
= sc_extern
;
16985 storage_class
= sc_mutable
;
16988 gcc_unreachable ();
16990 decl_specs
->storage_class
= storage_class
;
16992 /* A storage class specifier cannot be applied alongside a typedef
16993 specifier. If there is a typedef specifier present then set
16994 conflicting_specifiers_p which will trigger an error later
16995 on in grokdeclarator. */
16996 if (decl_specs
->specs
[(int)ds_typedef
])
16997 decl_specs
->conflicting_specifiers_p
= true;
17000 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17001 is true, the type is a user-defined type; otherwise it is a
17002 built-in type specified by a keyword. */
17005 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
17007 bool user_defined_p
)
17009 decl_specs
->any_specifiers_p
= true;
17011 /* If the user tries to redeclare bool or wchar_t (with, for
17012 example, in "typedef int wchar_t;") we remember that this is what
17013 happened. In system headers, we ignore these declarations so
17014 that G++ can work with system headers that are not C++-safe. */
17015 if (decl_specs
->specs
[(int) ds_typedef
]
17017 && (type_spec
== boolean_type_node
17018 || type_spec
== wchar_type_node
)
17019 && (decl_specs
->type
17020 || decl_specs
->specs
[(int) ds_long
]
17021 || decl_specs
->specs
[(int) ds_short
]
17022 || decl_specs
->specs
[(int) ds_unsigned
]
17023 || decl_specs
->specs
[(int) ds_signed
]))
17025 decl_specs
->redefined_builtin_type
= type_spec
;
17026 if (!decl_specs
->type
)
17028 decl_specs
->type
= type_spec
;
17029 decl_specs
->user_defined_type_p
= false;
17032 else if (decl_specs
->type
)
17033 decl_specs
->multiple_types_p
= true;
17036 decl_specs
->type
= type_spec
;
17037 decl_specs
->user_defined_type_p
= user_defined_p
;
17038 decl_specs
->redefined_builtin_type
= NULL_TREE
;
17042 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17043 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17046 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
17048 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
17051 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17052 issue an error message indicating that TOKEN_DESC was expected.
17054 Returns the token consumed, if the token had the appropriate type.
17055 Otherwise, returns NULL. */
17058 cp_parser_require (cp_parser
* parser
,
17059 enum cpp_ttype type
,
17060 const char* token_desc
)
17062 if (cp_lexer_next_token_is (parser
->lexer
, type
))
17063 return cp_lexer_consume_token (parser
->lexer
);
17066 /* Output the MESSAGE -- unless we're parsing tentatively. */
17067 if (!cp_parser_simulate_error (parser
))
17069 char *message
= concat ("expected ", token_desc
, NULL
);
17070 cp_parser_error (parser
, message
);
17077 /* An error message is produced if the next token is not '>'.
17078 All further tokens are skipped until the desired token is
17079 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17082 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
17084 /* Current level of '< ... >'. */
17085 unsigned level
= 0;
17086 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17087 unsigned nesting_depth
= 0;
17089 /* Are we ready, yet? If not, issue error message. */
17090 if (cp_parser_require (parser
, CPP_GREATER
, "%<>%>"))
17093 /* Skip tokens until the desired token is found. */
17096 /* Peek at the next token. */
17097 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
17100 if (!nesting_depth
)
17106 /* C++0x views the `>>' operator as two `>' tokens, but
17109 else if (!nesting_depth
&& level
-- == 0)
17111 /* We've hit a `>>' where the first `>' closes the
17112 template argument list, and the second `>' is
17113 spurious. Just consume the `>>' and stop; we've
17114 already produced at least one error. */
17115 cp_lexer_consume_token (parser
->lexer
);
17118 /* Fall through for C++0x, so we handle the second `>' in
17122 if (!nesting_depth
&& level
-- == 0)
17124 /* We've reached the token we want, consume it and stop. */
17125 cp_lexer_consume_token (parser
->lexer
);
17130 case CPP_OPEN_PAREN
:
17131 case CPP_OPEN_SQUARE
:
17135 case CPP_CLOSE_PAREN
:
17136 case CPP_CLOSE_SQUARE
:
17137 if (nesting_depth
-- == 0)
17142 case CPP_PRAGMA_EOL
:
17143 case CPP_SEMICOLON
:
17144 case CPP_OPEN_BRACE
:
17145 case CPP_CLOSE_BRACE
:
17146 /* The '>' was probably forgotten, don't look further. */
17153 /* Consume this token. */
17154 cp_lexer_consume_token (parser
->lexer
);
17158 /* If the next token is the indicated keyword, consume it. Otherwise,
17159 issue an error message indicating that TOKEN_DESC was expected.
17161 Returns the token consumed, if the token had the appropriate type.
17162 Otherwise, returns NULL. */
17165 cp_parser_require_keyword (cp_parser
* parser
,
17167 const char* token_desc
)
17169 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
17171 if (token
&& token
->keyword
!= keyword
)
17173 dyn_string_t error_msg
;
17175 /* Format the error message. */
17176 error_msg
= dyn_string_new (0);
17177 dyn_string_append_cstr (error_msg
, "expected ");
17178 dyn_string_append_cstr (error_msg
, token_desc
);
17179 cp_parser_error (parser
, error_msg
->s
);
17180 dyn_string_delete (error_msg
);
17187 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17188 function-definition. */
17191 cp_parser_token_starts_function_definition_p (cp_token
* token
)
17193 return (/* An ordinary function-body begins with an `{'. */
17194 token
->type
== CPP_OPEN_BRACE
17195 /* A ctor-initializer begins with a `:'. */
17196 || token
->type
== CPP_COLON
17197 /* A function-try-block begins with `try'. */
17198 || token
->keyword
== RID_TRY
17199 /* The named return value extension begins with `return'. */
17200 || token
->keyword
== RID_RETURN
);
17203 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17207 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
17211 token
= cp_lexer_peek_token (parser
->lexer
);
17212 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
17215 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17216 C++0x) ending a template-argument. */
17219 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
17223 token
= cp_lexer_peek_token (parser
->lexer
);
17224 return (token
->type
== CPP_COMMA
17225 || token
->type
== CPP_GREATER
17226 || token
->type
== CPP_ELLIPSIS
17227 || (flag_cpp0x
&& token
->type
== CPP_RSHIFT
));
17230 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17231 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17234 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
17239 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
17240 if (token
->type
== CPP_LESS
)
17242 /* Check for the sequence `<::' in the original code. It would be lexed as
17243 `[:', where `[' is a digraph, and there is no whitespace before
17245 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
17248 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
17249 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
17255 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17256 or none_type otherwise. */
17258 static enum tag_types
17259 cp_parser_token_is_class_key (cp_token
* token
)
17261 switch (token
->keyword
)
17266 return record_type
;
17275 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17278 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
17280 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
17281 pedwarn ("%qs tag used in naming %q#T",
17282 class_key
== union_type
? "union"
17283 : class_key
== record_type
? "struct" : "class",
17287 /* Issue an error message if DECL is redeclared with different
17288 access than its original declaration [class.access.spec/3].
17289 This applies to nested classes and nested class templates.
17293 cp_parser_check_access_in_redeclaration (tree decl
)
17295 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
17298 if ((TREE_PRIVATE (decl
)
17299 != (current_access_specifier
== access_private_node
))
17300 || (TREE_PROTECTED (decl
)
17301 != (current_access_specifier
== access_protected_node
)))
17302 error ("%qD redeclared with different access", decl
);
17305 /* Look for the `template' keyword, as a syntactic disambiguator.
17306 Return TRUE iff it is present, in which case it will be
17310 cp_parser_optional_template_keyword (cp_parser
*parser
)
17312 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17314 /* The `template' keyword can only be used within templates;
17315 outside templates the parser can always figure out what is a
17316 template and what is not. */
17317 if (!processing_template_decl
)
17319 error ("%<template%> (as a disambiguator) is only allowed "
17320 "within templates");
17321 /* If this part of the token stream is rescanned, the same
17322 error message would be generated. So, we purge the token
17323 from the stream. */
17324 cp_lexer_purge_token (parser
->lexer
);
17329 /* Consume the `template' keyword. */
17330 cp_lexer_consume_token (parser
->lexer
);
17338 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17339 set PARSER->SCOPE, and perform other related actions. */
17342 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
17345 struct tree_check
*check_value
;
17346 deferred_access_check
*chk
;
17347 VEC (deferred_access_check
,gc
) *checks
;
17349 /* Get the stored value. */
17350 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
17351 /* Perform any access checks that were deferred. */
17352 checks
= check_value
->checks
;
17356 VEC_iterate (deferred_access_check
, checks
, i
, chk
) ;
17359 perform_or_defer_access_check (chk
->binfo
,
17364 /* Set the scope from the stored value. */
17365 parser
->scope
= check_value
->value
;
17366 parser
->qualifying_scope
= check_value
->qualifying_scope
;
17367 parser
->object_scope
= NULL_TREE
;
17370 /* Consume tokens up through a non-nested END token. */
17373 cp_parser_cache_group (cp_parser
*parser
,
17374 enum cpp_ttype end
,
17381 /* Abort a parenthesized expression if we encounter a brace. */
17382 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
17383 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17385 /* If we've reached the end of the file, stop. */
17386 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
)
17387 || (end
!= CPP_PRAGMA_EOL
17388 && cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)))
17390 /* Consume the next token. */
17391 token
= cp_lexer_consume_token (parser
->lexer
);
17392 /* See if it starts a new group. */
17393 if (token
->type
== CPP_OPEN_BRACE
)
17395 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
17399 else if (token
->type
== CPP_OPEN_PAREN
)
17400 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
17401 else if (token
->type
== CPP_PRAGMA
)
17402 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
17403 else if (token
->type
== end
)
17408 /* Begin parsing tentatively. We always save tokens while parsing
17409 tentatively so that if the tentative parsing fails we can restore the
17413 cp_parser_parse_tentatively (cp_parser
* parser
)
17415 /* Enter a new parsing context. */
17416 parser
->context
= cp_parser_context_new (parser
->context
);
17417 /* Begin saving tokens. */
17418 cp_lexer_save_tokens (parser
->lexer
);
17419 /* In order to avoid repetitive access control error messages,
17420 access checks are queued up until we are no longer parsing
17422 push_deferring_access_checks (dk_deferred
);
17425 /* Commit to the currently active tentative parse. */
17428 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
17430 cp_parser_context
*context
;
17433 /* Mark all of the levels as committed. */
17434 lexer
= parser
->lexer
;
17435 for (context
= parser
->context
; context
->next
; context
= context
->next
)
17437 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
17439 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
17440 while (!cp_lexer_saving_tokens (lexer
))
17441 lexer
= lexer
->next
;
17442 cp_lexer_commit_tokens (lexer
);
17446 /* Abort the currently active tentative parse. All consumed tokens
17447 will be rolled back, and no diagnostics will be issued. */
17450 cp_parser_abort_tentative_parse (cp_parser
* parser
)
17452 cp_parser_simulate_error (parser
);
17453 /* Now, pretend that we want to see if the construct was
17454 successfully parsed. */
17455 cp_parser_parse_definitely (parser
);
17458 /* Stop parsing tentatively. If a parse error has occurred, restore the
17459 token stream. Otherwise, commit to the tokens we have consumed.
17460 Returns true if no error occurred; false otherwise. */
17463 cp_parser_parse_definitely (cp_parser
* parser
)
17465 bool error_occurred
;
17466 cp_parser_context
*context
;
17468 /* Remember whether or not an error occurred, since we are about to
17469 destroy that information. */
17470 error_occurred
= cp_parser_error_occurred (parser
);
17471 /* Remove the topmost context from the stack. */
17472 context
= parser
->context
;
17473 parser
->context
= context
->next
;
17474 /* If no parse errors occurred, commit to the tentative parse. */
17475 if (!error_occurred
)
17477 /* Commit to the tokens read tentatively, unless that was
17479 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
17480 cp_lexer_commit_tokens (parser
->lexer
);
17482 pop_to_parent_deferring_access_checks ();
17484 /* Otherwise, if errors occurred, roll back our state so that things
17485 are just as they were before we began the tentative parse. */
17488 cp_lexer_rollback_tokens (parser
->lexer
);
17489 pop_deferring_access_checks ();
17491 /* Add the context to the front of the free list. */
17492 context
->next
= cp_parser_context_free_list
;
17493 cp_parser_context_free_list
= context
;
17495 return !error_occurred
;
17498 /* Returns true if we are parsing tentatively and are not committed to
17499 this tentative parse. */
17502 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
17504 return (cp_parser_parsing_tentatively (parser
)
17505 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
17508 /* Returns nonzero iff an error has occurred during the most recent
17509 tentative parse. */
17512 cp_parser_error_occurred (cp_parser
* parser
)
17514 return (cp_parser_parsing_tentatively (parser
)
17515 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
17518 /* Returns nonzero if GNU extensions are allowed. */
17521 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
17523 return parser
->allow_gnu_extensions_p
;
17526 /* Objective-C++ Productions */
17529 /* Parse an Objective-C expression, which feeds into a primary-expression
17533 objc-message-expression
17534 objc-string-literal
17535 objc-encode-expression
17536 objc-protocol-expression
17537 objc-selector-expression
17539 Returns a tree representation of the expression. */
17542 cp_parser_objc_expression (cp_parser
* parser
)
17544 /* Try to figure out what kind of declaration is present. */
17545 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17549 case CPP_OPEN_SQUARE
:
17550 return cp_parser_objc_message_expression (parser
);
17552 case CPP_OBJC_STRING
:
17553 kwd
= cp_lexer_consume_token (parser
->lexer
);
17554 return objc_build_string_object (kwd
->u
.value
);
17557 switch (kwd
->keyword
)
17559 case RID_AT_ENCODE
:
17560 return cp_parser_objc_encode_expression (parser
);
17562 case RID_AT_PROTOCOL
:
17563 return cp_parser_objc_protocol_expression (parser
);
17565 case RID_AT_SELECTOR
:
17566 return cp_parser_objc_selector_expression (parser
);
17572 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->u
.value
);
17573 cp_parser_skip_to_end_of_block_or_statement (parser
);
17576 return error_mark_node
;
17579 /* Parse an Objective-C message expression.
17581 objc-message-expression:
17582 [ objc-message-receiver objc-message-args ]
17584 Returns a representation of an Objective-C message. */
17587 cp_parser_objc_message_expression (cp_parser
* parser
)
17589 tree receiver
, messageargs
;
17591 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
17592 receiver
= cp_parser_objc_message_receiver (parser
);
17593 messageargs
= cp_parser_objc_message_args (parser
);
17594 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
17596 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
17599 /* Parse an objc-message-receiver.
17601 objc-message-receiver:
17603 simple-type-specifier
17605 Returns a representation of the type or expression. */
17608 cp_parser_objc_message_receiver (cp_parser
* parser
)
17612 /* An Objective-C message receiver may be either (1) a type
17613 or (2) an expression. */
17614 cp_parser_parse_tentatively (parser
);
17615 rcv
= cp_parser_expression (parser
, false);
17617 if (cp_parser_parse_definitely (parser
))
17620 rcv
= cp_parser_simple_type_specifier (parser
,
17621 /*decl_specs=*/NULL
,
17622 CP_PARSER_FLAGS_NONE
);
17624 return objc_get_class_reference (rcv
);
17627 /* Parse the arguments and selectors comprising an Objective-C message.
17632 objc-selector-args , objc-comma-args
17634 objc-selector-args:
17635 objc-selector [opt] : assignment-expression
17636 objc-selector-args objc-selector [opt] : assignment-expression
17639 assignment-expression
17640 objc-comma-args , assignment-expression
17642 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17643 selector arguments and TREE_VALUE containing a list of comma
17647 cp_parser_objc_message_args (cp_parser
* parser
)
17649 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
17650 bool maybe_unary_selector_p
= true;
17651 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17653 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
17655 tree selector
= NULL_TREE
, arg
;
17657 if (token
->type
!= CPP_COLON
)
17658 selector
= cp_parser_objc_selector (parser
);
17660 /* Detect if we have a unary selector. */
17661 if (maybe_unary_selector_p
17662 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
17663 return build_tree_list (selector
, NULL_TREE
);
17665 maybe_unary_selector_p
= false;
17666 cp_parser_require (parser
, CPP_COLON
, "`:'");
17667 arg
= cp_parser_assignment_expression (parser
, false);
17670 = chainon (sel_args
,
17671 build_tree_list (selector
, arg
));
17673 token
= cp_lexer_peek_token (parser
->lexer
);
17676 /* Handle non-selector arguments, if any. */
17677 while (token
->type
== CPP_COMMA
)
17681 cp_lexer_consume_token (parser
->lexer
);
17682 arg
= cp_parser_assignment_expression (parser
, false);
17685 = chainon (addl_args
,
17686 build_tree_list (NULL_TREE
, arg
));
17688 token
= cp_lexer_peek_token (parser
->lexer
);
17691 return build_tree_list (sel_args
, addl_args
);
17694 /* Parse an Objective-C encode expression.
17696 objc-encode-expression:
17697 @encode objc-typename
17699 Returns an encoded representation of the type argument. */
17702 cp_parser_objc_encode_expression (cp_parser
* parser
)
17706 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
17707 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17708 type
= complete_type (cp_parser_type_id (parser
));
17709 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17713 error ("%<@encode%> must specify a type as an argument");
17714 return error_mark_node
;
17717 return objc_build_encode_expr (type
);
17720 /* Parse an Objective-C @defs expression. */
17723 cp_parser_objc_defs_expression (cp_parser
*parser
)
17727 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
17728 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17729 name
= cp_parser_identifier (parser
);
17730 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17732 return objc_get_class_ivars (name
);
17735 /* Parse an Objective-C protocol expression.
17737 objc-protocol-expression:
17738 @protocol ( identifier )
17740 Returns a representation of the protocol expression. */
17743 cp_parser_objc_protocol_expression (cp_parser
* parser
)
17747 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
17748 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17749 proto
= cp_parser_identifier (parser
);
17750 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17752 return objc_build_protocol_expr (proto
);
17755 /* Parse an Objective-C selector expression.
17757 objc-selector-expression:
17758 @selector ( objc-method-signature )
17760 objc-method-signature:
17766 objc-selector-seq objc-selector :
17768 Returns a representation of the method selector. */
17771 cp_parser_objc_selector_expression (cp_parser
* parser
)
17773 tree sel_seq
= NULL_TREE
;
17774 bool maybe_unary_selector_p
= true;
17777 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
17778 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17779 token
= cp_lexer_peek_token (parser
->lexer
);
17781 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
17782 || token
->type
== CPP_SCOPE
)
17784 tree selector
= NULL_TREE
;
17786 if (token
->type
!= CPP_COLON
17787 || token
->type
== CPP_SCOPE
)
17788 selector
= cp_parser_objc_selector (parser
);
17790 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
17791 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
17793 /* Detect if we have a unary selector. */
17794 if (maybe_unary_selector_p
)
17796 sel_seq
= selector
;
17797 goto finish_selector
;
17801 cp_parser_error (parser
, "expected %<:%>");
17804 maybe_unary_selector_p
= false;
17805 token
= cp_lexer_consume_token (parser
->lexer
);
17807 if (token
->type
== CPP_SCOPE
)
17810 = chainon (sel_seq
,
17811 build_tree_list (selector
, NULL_TREE
));
17813 = chainon (sel_seq
,
17814 build_tree_list (NULL_TREE
, NULL_TREE
));
17818 = chainon (sel_seq
,
17819 build_tree_list (selector
, NULL_TREE
));
17821 token
= cp_lexer_peek_token (parser
->lexer
);
17825 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17827 return objc_build_selector_expr (sel_seq
);
17830 /* Parse a list of identifiers.
17832 objc-identifier-list:
17834 objc-identifier-list , identifier
17836 Returns a TREE_LIST of identifier nodes. */
17839 cp_parser_objc_identifier_list (cp_parser
* parser
)
17841 tree list
= build_tree_list (NULL_TREE
, cp_parser_identifier (parser
));
17842 cp_token
*sep
= cp_lexer_peek_token (parser
->lexer
);
17844 while (sep
->type
== CPP_COMMA
)
17846 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17847 list
= chainon (list
,
17848 build_tree_list (NULL_TREE
,
17849 cp_parser_identifier (parser
)));
17850 sep
= cp_lexer_peek_token (parser
->lexer
);
17856 /* Parse an Objective-C alias declaration.
17858 objc-alias-declaration:
17859 @compatibility_alias identifier identifier ;
17861 This function registers the alias mapping with the Objective-C front end.
17862 It returns nothing. */
17865 cp_parser_objc_alias_declaration (cp_parser
* parser
)
17869 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
17870 alias
= cp_parser_identifier (parser
);
17871 orig
= cp_parser_identifier (parser
);
17872 objc_declare_alias (alias
, orig
);
17873 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17876 /* Parse an Objective-C class forward-declaration.
17878 objc-class-declaration:
17879 @class objc-identifier-list ;
17881 The function registers the forward declarations with the Objective-C
17882 front end. It returns nothing. */
17885 cp_parser_objc_class_declaration (cp_parser
* parser
)
17887 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
17888 objc_declare_class (cp_parser_objc_identifier_list (parser
));
17889 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17892 /* Parse a list of Objective-C protocol references.
17894 objc-protocol-refs-opt:
17895 objc-protocol-refs [opt]
17897 objc-protocol-refs:
17898 < objc-identifier-list >
17900 Returns a TREE_LIST of identifiers, if any. */
17903 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
17905 tree protorefs
= NULL_TREE
;
17907 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
17909 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
17910 protorefs
= cp_parser_objc_identifier_list (parser
);
17911 cp_parser_require (parser
, CPP_GREATER
, "`>'");
17917 /* Parse a Objective-C visibility specification. */
17920 cp_parser_objc_visibility_spec (cp_parser
* parser
)
17922 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
17924 switch (vis
->keyword
)
17926 case RID_AT_PRIVATE
:
17927 objc_set_visibility (2);
17929 case RID_AT_PROTECTED
:
17930 objc_set_visibility (0);
17932 case RID_AT_PUBLIC
:
17933 objc_set_visibility (1);
17939 /* Eat '@private'/'@protected'/'@public'. */
17940 cp_lexer_consume_token (parser
->lexer
);
17943 /* Parse an Objective-C method type. */
17946 cp_parser_objc_method_type (cp_parser
* parser
)
17948 objc_set_method_type
17949 (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
17954 /* Parse an Objective-C protocol qualifier. */
17957 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
17959 tree quals
= NULL_TREE
, node
;
17960 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17962 node
= token
->u
.value
;
17964 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
17965 && (node
== ridpointers
[(int) RID_IN
]
17966 || node
== ridpointers
[(int) RID_OUT
]
17967 || node
== ridpointers
[(int) RID_INOUT
]
17968 || node
== ridpointers
[(int) RID_BYCOPY
]
17969 || node
== ridpointers
[(int) RID_BYREF
]
17970 || node
== ridpointers
[(int) RID_ONEWAY
]))
17972 quals
= tree_cons (NULL_TREE
, node
, quals
);
17973 cp_lexer_consume_token (parser
->lexer
);
17974 token
= cp_lexer_peek_token (parser
->lexer
);
17975 node
= token
->u
.value
;
17981 /* Parse an Objective-C typename. */
17984 cp_parser_objc_typename (cp_parser
* parser
)
17986 tree typename
= NULL_TREE
;
17988 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17990 tree proto_quals
, cp_type
= NULL_TREE
;
17992 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17993 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
17995 /* An ObjC type name may consist of just protocol qualifiers, in which
17996 case the type shall default to 'id'. */
17997 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
17998 cp_type
= cp_parser_type_id (parser
);
18000 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18001 typename
= build_tree_list (proto_quals
, cp_type
);
18007 /* Check to see if TYPE refers to an Objective-C selector name. */
18010 cp_parser_objc_selector_p (enum cpp_ttype type
)
18012 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
18013 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
18014 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
18015 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
18016 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
18019 /* Parse an Objective-C selector. */
18022 cp_parser_objc_selector (cp_parser
* parser
)
18024 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
18026 if (!cp_parser_objc_selector_p (token
->type
))
18028 error ("invalid Objective-C++ selector name");
18029 return error_mark_node
;
18032 /* C++ operator names are allowed to appear in ObjC selectors. */
18033 switch (token
->type
)
18035 case CPP_AND_AND
: return get_identifier ("and");
18036 case CPP_AND_EQ
: return get_identifier ("and_eq");
18037 case CPP_AND
: return get_identifier ("bitand");
18038 case CPP_OR
: return get_identifier ("bitor");
18039 case CPP_COMPL
: return get_identifier ("compl");
18040 case CPP_NOT
: return get_identifier ("not");
18041 case CPP_NOT_EQ
: return get_identifier ("not_eq");
18042 case CPP_OR_OR
: return get_identifier ("or");
18043 case CPP_OR_EQ
: return get_identifier ("or_eq");
18044 case CPP_XOR
: return get_identifier ("xor");
18045 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
18046 default: return token
->u
.value
;
18050 /* Parse an Objective-C params list. */
18053 cp_parser_objc_method_keyword_params (cp_parser
* parser
)
18055 tree params
= NULL_TREE
;
18056 bool maybe_unary_selector_p
= true;
18057 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18059 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
18061 tree selector
= NULL_TREE
, typename
, identifier
;
18063 if (token
->type
!= CPP_COLON
)
18064 selector
= cp_parser_objc_selector (parser
);
18066 /* Detect if we have a unary selector. */
18067 if (maybe_unary_selector_p
18068 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
18071 maybe_unary_selector_p
= false;
18072 cp_parser_require (parser
, CPP_COLON
, "`:'");
18073 typename
= cp_parser_objc_typename (parser
);
18074 identifier
= cp_parser_identifier (parser
);
18078 objc_build_keyword_decl (selector
,
18082 token
= cp_lexer_peek_token (parser
->lexer
);
18088 /* Parse the non-keyword Objective-C params. */
18091 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
)
18093 tree params
= make_node (TREE_LIST
);
18094 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18095 *ellipsisp
= false; /* Initially, assume no ellipsis. */
18097 while (token
->type
== CPP_COMMA
)
18099 cp_parameter_declarator
*parmdecl
;
18102 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
18103 token
= cp_lexer_peek_token (parser
->lexer
);
18105 if (token
->type
== CPP_ELLIPSIS
)
18107 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
18112 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
18113 parm
= grokdeclarator (parmdecl
->declarator
,
18114 &parmdecl
->decl_specifiers
,
18115 PARM
, /*initialized=*/0,
18116 /*attrlist=*/NULL
);
18118 chainon (params
, build_tree_list (NULL_TREE
, parm
));
18119 token
= cp_lexer_peek_token (parser
->lexer
);
18125 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18128 cp_parser_objc_interstitial_code (cp_parser
* parser
)
18130 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18132 /* If the next token is `extern' and the following token is a string
18133 literal, then we have a linkage specification. */
18134 if (token
->keyword
== RID_EXTERN
18135 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
18136 cp_parser_linkage_specification (parser
);
18137 /* Handle #pragma, if any. */
18138 else if (token
->type
== CPP_PRAGMA
)
18139 cp_parser_pragma (parser
, pragma_external
);
18140 /* Allow stray semicolons. */
18141 else if (token
->type
== CPP_SEMICOLON
)
18142 cp_lexer_consume_token (parser
->lexer
);
18143 /* Finally, try to parse a block-declaration, or a function-definition. */
18145 cp_parser_block_declaration (parser
, /*statement_p=*/false);
18148 /* Parse a method signature. */
18151 cp_parser_objc_method_signature (cp_parser
* parser
)
18153 tree rettype
, kwdparms
, optparms
;
18154 bool ellipsis
= false;
18156 cp_parser_objc_method_type (parser
);
18157 rettype
= cp_parser_objc_typename (parser
);
18158 kwdparms
= cp_parser_objc_method_keyword_params (parser
);
18159 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
);
18161 return objc_build_method_signature (rettype
, kwdparms
, optparms
, ellipsis
);
18164 /* Pars an Objective-C method prototype list. */
18167 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
18169 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18171 while (token
->keyword
!= RID_AT_END
)
18173 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
18175 objc_add_method_declaration
18176 (cp_parser_objc_method_signature (parser
));
18177 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18180 /* Allow for interspersed non-ObjC++ code. */
18181 cp_parser_objc_interstitial_code (parser
);
18183 token
= cp_lexer_peek_token (parser
->lexer
);
18186 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
18187 objc_finish_interface ();
18190 /* Parse an Objective-C method definition list. */
18193 cp_parser_objc_method_definition_list (cp_parser
* parser
)
18195 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18197 while (token
->keyword
!= RID_AT_END
)
18201 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
18203 push_deferring_access_checks (dk_deferred
);
18204 objc_start_method_definition
18205 (cp_parser_objc_method_signature (parser
));
18207 /* For historical reasons, we accept an optional semicolon. */
18208 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18209 cp_lexer_consume_token (parser
->lexer
);
18211 perform_deferred_access_checks ();
18212 stop_deferring_access_checks ();
18213 meth
= cp_parser_function_definition_after_declarator (parser
,
18215 pop_deferring_access_checks ();
18216 objc_finish_method_definition (meth
);
18219 /* Allow for interspersed non-ObjC++ code. */
18220 cp_parser_objc_interstitial_code (parser
);
18222 token
= cp_lexer_peek_token (parser
->lexer
);
18225 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
18226 objc_finish_implementation ();
18229 /* Parse Objective-C ivars. */
18232 cp_parser_objc_class_ivars (cp_parser
* parser
)
18234 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18236 if (token
->type
!= CPP_OPEN_BRACE
)
18237 return; /* No ivars specified. */
18239 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
18240 token
= cp_lexer_peek_token (parser
->lexer
);
18242 while (token
->type
!= CPP_CLOSE_BRACE
)
18244 cp_decl_specifier_seq declspecs
;
18245 int decl_class_or_enum_p
;
18246 tree prefix_attributes
;
18248 cp_parser_objc_visibility_spec (parser
);
18250 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18253 cp_parser_decl_specifier_seq (parser
,
18254 CP_PARSER_FLAGS_OPTIONAL
,
18256 &decl_class_or_enum_p
);
18257 prefix_attributes
= declspecs
.attributes
;
18258 declspecs
.attributes
= NULL_TREE
;
18260 /* Keep going until we hit the `;' at the end of the
18262 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18264 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
18265 cp_declarator
*declarator
= NULL
;
18266 int ctor_dtor_or_conv_p
;
18268 /* Check for a (possibly unnamed) bitfield declaration. */
18269 token
= cp_lexer_peek_token (parser
->lexer
);
18270 if (token
->type
== CPP_COLON
)
18273 if (token
->type
== CPP_NAME
18274 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
18277 /* Get the name of the bitfield. */
18278 declarator
= make_id_declarator (NULL_TREE
,
18279 cp_parser_identifier (parser
),
18283 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
18284 /* Get the width of the bitfield. */
18286 = cp_parser_constant_expression (parser
,
18287 /*allow_non_constant=*/false,
18292 /* Parse the declarator. */
18294 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
18295 &ctor_dtor_or_conv_p
,
18296 /*parenthesized_p=*/NULL
,
18297 /*member_p=*/false);
18300 /* Look for attributes that apply to the ivar. */
18301 attributes
= cp_parser_attributes_opt (parser
);
18302 /* Remember which attributes are prefix attributes and
18304 first_attribute
= attributes
;
18305 /* Combine the attributes. */
18306 attributes
= chainon (prefix_attributes
, attributes
);
18310 /* Create the bitfield declaration. */
18311 decl
= grokbitfield (declarator
, &declspecs
, width
);
18312 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
18315 decl
= grokfield (declarator
, &declspecs
,
18316 NULL_TREE
, /*init_const_expr_p=*/false,
18317 NULL_TREE
, attributes
);
18319 /* Add the instance variable. */
18320 objc_add_instance_variable (decl
);
18322 /* Reset PREFIX_ATTRIBUTES. */
18323 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
18324 attributes
= TREE_CHAIN (attributes
);
18326 TREE_CHAIN (attributes
) = NULL_TREE
;
18328 token
= cp_lexer_peek_token (parser
->lexer
);
18330 if (token
->type
== CPP_COMMA
)
18332 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
18338 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18339 token
= cp_lexer_peek_token (parser
->lexer
);
18342 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
18343 /* For historical reasons, we accept an optional semicolon. */
18344 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18345 cp_lexer_consume_token (parser
->lexer
);
18348 /* Parse an Objective-C protocol declaration. */
18351 cp_parser_objc_protocol_declaration (cp_parser
* parser
)
18353 tree proto
, protorefs
;
18356 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
18357 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
18359 error ("identifier expected after %<@protocol%>");
18363 /* See if we have a forward declaration or a definition. */
18364 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18366 /* Try a forward declaration first. */
18367 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
18369 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
18371 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18374 /* Ok, we got a full-fledged definition (or at least should). */
18377 proto
= cp_parser_identifier (parser
);
18378 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
18379 objc_start_protocol (proto
, protorefs
);
18380 cp_parser_objc_method_prototype_list (parser
);
18384 /* Parse an Objective-C superclass or category. */
18387 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
18390 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
18392 *super
= *categ
= NULL_TREE
;
18393 if (next
->type
== CPP_COLON
)
18395 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
18396 *super
= cp_parser_identifier (parser
);
18398 else if (next
->type
== CPP_OPEN_PAREN
)
18400 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
18401 *categ
= cp_parser_identifier (parser
);
18402 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18406 /* Parse an Objective-C class interface. */
18409 cp_parser_objc_class_interface (cp_parser
* parser
)
18411 tree name
, super
, categ
, protos
;
18413 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
18414 name
= cp_parser_identifier (parser
);
18415 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
18416 protos
= cp_parser_objc_protocol_refs_opt (parser
);
18418 /* We have either a class or a category on our hands. */
18420 objc_start_category_interface (name
, categ
, protos
);
18423 objc_start_class_interface (name
, super
, protos
);
18424 /* Handle instance variable declarations, if any. */
18425 cp_parser_objc_class_ivars (parser
);
18426 objc_continue_interface ();
18429 cp_parser_objc_method_prototype_list (parser
);
18432 /* Parse an Objective-C class implementation. */
18435 cp_parser_objc_class_implementation (cp_parser
* parser
)
18437 tree name
, super
, categ
;
18439 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
18440 name
= cp_parser_identifier (parser
);
18441 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
18443 /* We have either a class or a category on our hands. */
18445 objc_start_category_implementation (name
, categ
);
18448 objc_start_class_implementation (name
, super
);
18449 /* Handle instance variable declarations, if any. */
18450 cp_parser_objc_class_ivars (parser
);
18451 objc_continue_implementation ();
18454 cp_parser_objc_method_definition_list (parser
);
18457 /* Consume the @end token and finish off the implementation. */
18460 cp_parser_objc_end_implementation (cp_parser
* parser
)
18462 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
18463 objc_finish_implementation ();
18466 /* Parse an Objective-C declaration. */
18469 cp_parser_objc_declaration (cp_parser
* parser
)
18471 /* Try to figure out what kind of declaration is present. */
18472 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
18474 switch (kwd
->keyword
)
18477 cp_parser_objc_alias_declaration (parser
);
18480 cp_parser_objc_class_declaration (parser
);
18482 case RID_AT_PROTOCOL
:
18483 cp_parser_objc_protocol_declaration (parser
);
18485 case RID_AT_INTERFACE
:
18486 cp_parser_objc_class_interface (parser
);
18488 case RID_AT_IMPLEMENTATION
:
18489 cp_parser_objc_class_implementation (parser
);
18492 cp_parser_objc_end_implementation (parser
);
18495 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->u
.value
);
18496 cp_parser_skip_to_end_of_block_or_statement (parser
);
18500 /* Parse an Objective-C try-catch-finally statement.
18502 objc-try-catch-finally-stmt:
18503 @try compound-statement objc-catch-clause-seq [opt]
18504 objc-finally-clause [opt]
18506 objc-catch-clause-seq:
18507 objc-catch-clause objc-catch-clause-seq [opt]
18510 @catch ( exception-declaration ) compound-statement
18512 objc-finally-clause
18513 @finally compound-statement
18515 Returns NULL_TREE. */
18518 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
18519 location_t location
;
18522 cp_parser_require_keyword (parser
, RID_AT_TRY
, "`@try'");
18523 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18524 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18525 node, lest it get absorbed into the surrounding block. */
18526 stmt
= push_stmt_list ();
18527 cp_parser_compound_statement (parser
, NULL
, false);
18528 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
18530 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
18532 cp_parameter_declarator
*parmdecl
;
18535 cp_lexer_consume_token (parser
->lexer
);
18536 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
18537 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
18538 parm
= grokdeclarator (parmdecl
->declarator
,
18539 &parmdecl
->decl_specifiers
,
18540 PARM
, /*initialized=*/0,
18541 /*attrlist=*/NULL
);
18542 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18543 objc_begin_catch_clause (parm
);
18544 cp_parser_compound_statement (parser
, NULL
, false);
18545 objc_finish_catch_clause ();
18548 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
18550 cp_lexer_consume_token (parser
->lexer
);
18551 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18552 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18553 node, lest it get absorbed into the surrounding block. */
18554 stmt
= push_stmt_list ();
18555 cp_parser_compound_statement (parser
, NULL
, false);
18556 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
18559 return objc_finish_try_stmt ();
18562 /* Parse an Objective-C synchronized statement.
18564 objc-synchronized-stmt:
18565 @synchronized ( expression ) compound-statement
18567 Returns NULL_TREE. */
18570 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
18571 location_t location
;
18574 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, "`@synchronized'");
18576 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18577 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
18578 lock
= cp_parser_expression (parser
, false);
18579 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18581 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18582 node, lest it get absorbed into the surrounding block. */
18583 stmt
= push_stmt_list ();
18584 cp_parser_compound_statement (parser
, NULL
, false);
18586 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
18589 /* Parse an Objective-C throw statement.
18592 @throw assignment-expression [opt] ;
18594 Returns a constructed '@throw' statement. */
18597 cp_parser_objc_throw_statement (cp_parser
*parser
) {
18598 tree expr
= NULL_TREE
;
18600 cp_parser_require_keyword (parser
, RID_AT_THROW
, "`@throw'");
18602 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18603 expr
= cp_parser_assignment_expression (parser
, false);
18605 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18607 return objc_build_throw_stmt (expr
);
18610 /* Parse an Objective-C statement. */
18613 cp_parser_objc_statement (cp_parser
* parser
) {
18614 /* Try to figure out what kind of declaration is present. */
18615 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
18617 switch (kwd
->keyword
)
18620 return cp_parser_objc_try_catch_finally_statement (parser
);
18621 case RID_AT_SYNCHRONIZED
:
18622 return cp_parser_objc_synchronized_statement (parser
);
18624 return cp_parser_objc_throw_statement (parser
);
18626 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->u
.value
);
18627 cp_parser_skip_to_end_of_block_or_statement (parser
);
18630 return error_mark_node
;
18633 /* OpenMP 2.5 parsing routines. */
18635 /* Returns name of the next clause.
18636 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18637 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18638 returned and the token is consumed. */
18640 static pragma_omp_clause
18641 cp_parser_omp_clause_name (cp_parser
*parser
)
18643 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
18645 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
18646 result
= PRAGMA_OMP_CLAUSE_IF
;
18647 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
18648 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
18649 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
18650 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
18651 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18653 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
18654 const char *p
= IDENTIFIER_POINTER (id
);
18659 if (!strcmp ("copyin", p
))
18660 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
18661 else if (!strcmp ("copyprivate", p
))
18662 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
18665 if (!strcmp ("firstprivate", p
))
18666 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
18669 if (!strcmp ("lastprivate", p
))
18670 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
18673 if (!strcmp ("nowait", p
))
18674 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
18675 else if (!strcmp ("num_threads", p
))
18676 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
18679 if (!strcmp ("ordered", p
))
18680 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
18683 if (!strcmp ("reduction", p
))
18684 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
18687 if (!strcmp ("schedule", p
))
18688 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
18689 else if (!strcmp ("shared", p
))
18690 result
= PRAGMA_OMP_CLAUSE_SHARED
;
18695 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
18696 cp_lexer_consume_token (parser
->lexer
);
18701 /* Validate that a clause of the given type does not already exist. */
18704 check_no_duplicate_clause (tree clauses
, enum tree_code code
, const char *name
)
18708 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
18709 if (OMP_CLAUSE_CODE (c
) == code
)
18711 error ("too many %qs clauses", name
);
18719 variable-list , identifier
18721 In addition, we match a closing parenthesis. An opening parenthesis
18722 will have been consumed by the caller.
18724 If KIND is nonzero, create the appropriate node and install the decl
18725 in OMP_CLAUSE_DECL and add the node to the head of the list.
18727 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18728 return the list created. */
18731 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
18738 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
18739 /*check_dependency_p=*/true,
18740 /*template_p=*/NULL
,
18741 /*declarator_p=*/false,
18742 /*optional_p=*/false);
18743 if (name
== error_mark_node
)
18746 decl
= cp_parser_lookup_name_simple (parser
, name
);
18747 if (decl
== error_mark_node
)
18748 cp_parser_name_lookup_error (parser
, name
, decl
, NULL
);
18749 else if (kind
!= 0)
18751 tree u
= build_omp_clause (kind
);
18752 OMP_CLAUSE_DECL (u
) = decl
;
18753 OMP_CLAUSE_CHAIN (u
) = list
;
18757 list
= tree_cons (decl
, NULL_TREE
, list
);
18760 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18762 cp_lexer_consume_token (parser
->lexer
);
18765 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18769 /* Try to resync to an unnested comma. Copied from
18770 cp_parser_parenthesized_expression_list. */
18772 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
18773 /*recovering=*/true,
18775 /*consume_paren=*/true);
18783 /* Similarly, but expect leading and trailing parenthesis. This is a very
18784 common case for omp clauses. */
18787 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
18789 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18790 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
18795 default ( shared | none ) */
18798 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
)
18800 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
18803 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18805 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18807 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
18808 const char *p
= IDENTIFIER_POINTER (id
);
18813 if (strcmp ("none", p
) != 0)
18815 kind
= OMP_CLAUSE_DEFAULT_NONE
;
18819 if (strcmp ("shared", p
) != 0)
18821 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
18828 cp_lexer_consume_token (parser
->lexer
);
18833 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
18836 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18837 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18838 /*or_comma=*/false,
18839 /*consume_paren=*/true);
18841 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
18844 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default");
18845 c
= build_omp_clause (OMP_CLAUSE_DEFAULT
);
18846 OMP_CLAUSE_CHAIN (c
) = list
;
18847 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
18853 if ( expression ) */
18856 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
)
18860 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18863 t
= cp_parser_condition (parser
);
18865 if (t
== error_mark_node
18866 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18867 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18868 /*or_comma=*/false,
18869 /*consume_paren=*/true);
18871 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if");
18873 c
= build_omp_clause (OMP_CLAUSE_IF
);
18874 OMP_CLAUSE_IF_EXPR (c
) = t
;
18875 OMP_CLAUSE_CHAIN (c
) = list
;
18884 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18888 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait");
18890 c
= build_omp_clause (OMP_CLAUSE_NOWAIT
);
18891 OMP_CLAUSE_CHAIN (c
) = list
;
18896 num_threads ( expression ) */
18899 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
)
18903 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18906 t
= cp_parser_expression (parser
, false);
18908 if (t
== error_mark_node
18909 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18910 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18911 /*or_comma=*/false,
18912 /*consume_paren=*/true);
18914 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
, "num_threads");
18916 c
= build_omp_clause (OMP_CLAUSE_NUM_THREADS
);
18917 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
18918 OMP_CLAUSE_CHAIN (c
) = list
;
18927 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18931 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
, "ordered");
18933 c
= build_omp_clause (OMP_CLAUSE_ORDERED
);
18934 OMP_CLAUSE_CHAIN (c
) = list
;
18939 reduction ( reduction-operator : variable-list )
18941 reduction-operator:
18942 One of: + * - & ^ | && || */
18945 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
18947 enum tree_code code
;
18950 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18953 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18965 code
= BIT_AND_EXPR
;
18968 code
= BIT_XOR_EXPR
;
18971 code
= BIT_IOR_EXPR
;
18974 code
= TRUTH_ANDIF_EXPR
;
18977 code
= TRUTH_ORIF_EXPR
;
18980 cp_parser_error (parser
, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18982 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18983 /*or_comma=*/false,
18984 /*consume_paren=*/true);
18987 cp_lexer_consume_token (parser
->lexer
);
18989 if (!cp_parser_require (parser
, CPP_COLON
, "`:'"))
18992 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
18993 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
18994 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
19000 schedule ( schedule-kind )
19001 schedule ( schedule-kind , expression )
19004 static | dynamic | guided | runtime */
19007 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
)
19011 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
19014 c
= build_omp_clause (OMP_CLAUSE_SCHEDULE
);
19016 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19018 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
19019 const char *p
= IDENTIFIER_POINTER (id
);
19024 if (strcmp ("dynamic", p
) != 0)
19026 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
19030 if (strcmp ("guided", p
) != 0)
19032 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
19036 if (strcmp ("runtime", p
) != 0)
19038 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
19045 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
19046 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
19049 cp_lexer_consume_token (parser
->lexer
);
19051 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19053 cp_lexer_consume_token (parser
->lexer
);
19055 t
= cp_parser_assignment_expression (parser
, false);
19057 if (t
== error_mark_node
)
19059 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
19060 error ("schedule %<runtime%> does not take "
19061 "a %<chunk_size%> parameter");
19063 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
19065 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
19068 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`,' or `)'"))
19071 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule");
19072 OMP_CLAUSE_CHAIN (c
) = list
;
19076 cp_parser_error (parser
, "invalid schedule kind");
19078 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19079 /*or_comma=*/false,
19080 /*consume_paren=*/true);
19084 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19085 is a bitmask in MASK. Return the list of clauses found; the result
19086 of clause default goes in *pdefault. */
19089 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
19090 const char *where
, cp_token
*pragma_tok
)
19092 tree clauses
= NULL
;
19094 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
19096 pragma_omp_clause c_kind
= cp_parser_omp_clause_name (parser
);
19097 const char *c_name
;
19098 tree prev
= clauses
;
19102 case PRAGMA_OMP_CLAUSE_COPYIN
:
19103 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
19106 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
19107 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
19109 c_name
= "copyprivate";
19111 case PRAGMA_OMP_CLAUSE_DEFAULT
:
19112 clauses
= cp_parser_omp_clause_default (parser
, clauses
);
19113 c_name
= "default";
19115 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
19116 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
19118 c_name
= "firstprivate";
19120 case PRAGMA_OMP_CLAUSE_IF
:
19121 clauses
= cp_parser_omp_clause_if (parser
, clauses
);
19124 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
19125 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
19127 c_name
= "lastprivate";
19129 case PRAGMA_OMP_CLAUSE_NOWAIT
:
19130 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
);
19133 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
19134 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
);
19135 c_name
= "num_threads";
19137 case PRAGMA_OMP_CLAUSE_ORDERED
:
19138 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
);
19139 c_name
= "ordered";
19141 case PRAGMA_OMP_CLAUSE_PRIVATE
:
19142 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
19144 c_name
= "private";
19146 case PRAGMA_OMP_CLAUSE_REDUCTION
:
19147 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
19148 c_name
= "reduction";
19150 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
19151 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
);
19152 c_name
= "schedule";
19154 case PRAGMA_OMP_CLAUSE_SHARED
:
19155 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
19160 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
19164 if (((mask
>> c_kind
) & 1) == 0)
19166 /* Remove the invalid clause(s) from the list to avoid
19167 confusing the rest of the compiler. */
19169 error ("%qs is not valid for %qs", c_name
, where
);
19173 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
19174 return finish_omp_clauses (clauses
);
19181 In practice, we're also interested in adding the statement to an
19182 outer node. So it is convenient if we work around the fact that
19183 cp_parser_statement calls add_stmt. */
19186 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
19188 unsigned save
= parser
->in_statement
;
19190 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19191 This preserves the "not within loop or switch" style error messages
19192 for nonsense cases like
19198 if (parser
->in_statement
)
19199 parser
->in_statement
= IN_OMP_BLOCK
;
19205 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
19207 parser
->in_statement
= save
;
19211 cp_parser_omp_structured_block (cp_parser
*parser
)
19213 tree stmt
= begin_omp_structured_block ();
19214 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
19216 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
19218 cp_parser_end_omp_structured_block (parser
, save
);
19219 return finish_omp_structured_block (stmt
);
19223 # pragma omp atomic new-line
19227 x binop= expr | x++ | ++x | x-- | --x
19229 +, *, -, /, &, ^, |, <<, >>
19231 where x is an lvalue expression with scalar type. */
19234 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
19237 enum tree_code code
;
19239 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19241 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
19243 switch (TREE_CODE (lhs
))
19248 case PREINCREMENT_EXPR
:
19249 case POSTINCREMENT_EXPR
:
19250 lhs
= TREE_OPERAND (lhs
, 0);
19252 rhs
= integer_one_node
;
19255 case PREDECREMENT_EXPR
:
19256 case POSTDECREMENT_EXPR
:
19257 lhs
= TREE_OPERAND (lhs
, 0);
19259 rhs
= integer_one_node
;
19263 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
19269 code
= TRUNC_DIV_EXPR
;
19277 case CPP_LSHIFT_EQ
:
19278 code
= LSHIFT_EXPR
;
19280 case CPP_RSHIFT_EQ
:
19281 code
= RSHIFT_EXPR
;
19284 code
= BIT_AND_EXPR
;
19287 code
= BIT_IOR_EXPR
;
19290 code
= BIT_XOR_EXPR
;
19293 cp_parser_error (parser
,
19294 "invalid operator for %<#pragma omp atomic%>");
19297 cp_lexer_consume_token (parser
->lexer
);
19299 rhs
= cp_parser_expression (parser
, false);
19300 if (rhs
== error_mark_node
)
19304 finish_omp_atomic (code
, lhs
, rhs
);
19305 cp_parser_consume_semicolon_at_end_of_statement (parser
);
19309 cp_parser_skip_to_end_of_block_or_statement (parser
);
19314 # pragma omp barrier new-line */
19317 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
19319 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19320 finish_omp_barrier ();
19324 # pragma omp critical [(name)] new-line
19325 structured-block */
19328 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
19330 tree stmt
, name
= NULL
;
19332 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
19334 cp_lexer_consume_token (parser
->lexer
);
19336 name
= cp_parser_identifier (parser
);
19338 if (name
== error_mark_node
19339 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
19340 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19341 /*or_comma=*/false,
19342 /*consume_paren=*/true);
19343 if (name
== error_mark_node
)
19346 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19348 stmt
= cp_parser_omp_structured_block (parser
);
19349 return c_finish_omp_critical (stmt
, name
);
19353 # pragma omp flush flush-vars[opt] new-line
19356 ( variable-list ) */
19359 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
19361 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
19362 (void) cp_parser_omp_var_list (parser
, 0, NULL
);
19363 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19365 finish_omp_flush ();
19368 /* Parse the restricted form of the for statment allowed by OpenMP. */
19371 cp_parser_omp_for_loop (cp_parser
*parser
)
19373 tree init
, cond
, incr
, body
, decl
, pre_body
;
19376 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
19378 cp_parser_error (parser
, "for statement expected");
19381 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
19382 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
19385 init
= decl
= NULL
;
19386 pre_body
= push_stmt_list ();
19387 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19389 cp_decl_specifier_seq type_specifiers
;
19391 /* First, try to parse as an initialized declaration. See
19392 cp_parser_condition, from whence the bulk of this is copied. */
19394 cp_parser_parse_tentatively (parser
);
19395 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
19397 if (!cp_parser_error_occurred (parser
))
19399 tree asm_specification
, attributes
;
19400 cp_declarator
*declarator
;
19402 declarator
= cp_parser_declarator (parser
,
19403 CP_PARSER_DECLARATOR_NAMED
,
19404 /*ctor_dtor_or_conv_p=*/NULL
,
19405 /*parenthesized_p=*/NULL
,
19406 /*member_p=*/false);
19407 attributes
= cp_parser_attributes_opt (parser
);
19408 asm_specification
= cp_parser_asm_specification_opt (parser
);
19410 cp_parser_require (parser
, CPP_EQ
, "`='");
19411 if (cp_parser_parse_definitely (parser
))
19415 decl
= start_decl (declarator
, &type_specifiers
,
19416 /*initialized_p=*/false, attributes
,
19417 /*prefix_attributes=*/NULL_TREE
,
19420 init
= cp_parser_assignment_expression (parser
, false);
19422 cp_finish_decl (decl
, NULL_TREE
, /*init_const_expr_p=*/false,
19423 asm_specification
, LOOKUP_ONLYCONVERTING
);
19426 pop_scope (pushed_scope
);
19430 cp_parser_abort_tentative_parse (parser
);
19432 /* If parsing as an initialized declaration failed, try again as
19433 a simple expression. */
19435 init
= cp_parser_expression (parser
, false);
19437 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
19438 pre_body
= pop_stmt_list (pre_body
);
19441 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19442 cond
= cp_parser_condition (parser
);
19443 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
19446 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
19447 incr
= cp_parser_expression (parser
, false);
19449 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
19450 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19451 /*or_comma=*/false,
19452 /*consume_paren=*/true);
19454 /* Note that we saved the original contents of this flag when we entered
19455 the structured block, and so we don't need to re-save it here. */
19456 parser
->in_statement
= IN_OMP_FOR
;
19458 /* Note that the grammar doesn't call for a structured block here,
19459 though the loop as a whole is a structured block. */
19460 body
= push_stmt_list ();
19461 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
19462 body
= pop_stmt_list (body
);
19464 return finish_omp_for (loc
, decl
, init
, cond
, incr
, body
, pre_body
);
19468 #pragma omp for for-clause[optseq] new-line
19471 #define OMP_FOR_CLAUSE_MASK \
19472 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19473 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19474 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19475 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19476 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
19477 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
19478 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19481 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
19483 tree clauses
, sb
, ret
;
19486 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
19487 "#pragma omp for", pragma_tok
);
19489 sb
= begin_omp_structured_block ();
19490 save
= cp_parser_begin_omp_structured_block (parser
);
19492 ret
= cp_parser_omp_for_loop (parser
);
19494 OMP_FOR_CLAUSES (ret
) = clauses
;
19496 cp_parser_end_omp_structured_block (parser
, save
);
19497 add_stmt (finish_omp_structured_block (sb
));
19503 # pragma omp master new-line
19504 structured-block */
19507 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
19509 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19510 return c_finish_omp_master (cp_parser_omp_structured_block (parser
));
19514 # pragma omp ordered new-line
19515 structured-block */
19518 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
19520 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19521 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser
));
19527 { section-sequence }
19530 section-directive[opt] structured-block
19531 section-sequence section-directive structured-block */
19534 cp_parser_omp_sections_scope (cp_parser
*parser
)
19536 tree stmt
, substmt
;
19537 bool error_suppress
= false;
19540 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
19543 stmt
= push_stmt_list ();
19545 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
19549 substmt
= begin_omp_structured_block ();
19550 save
= cp_parser_begin_omp_structured_block (parser
);
19554 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
19556 tok
= cp_lexer_peek_token (parser
->lexer
);
19557 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
19559 if (tok
->type
== CPP_CLOSE_BRACE
)
19561 if (tok
->type
== CPP_EOF
)
19565 cp_parser_end_omp_structured_block (parser
, save
);
19566 substmt
= finish_omp_structured_block (substmt
);
19567 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
19568 add_stmt (substmt
);
19573 tok
= cp_lexer_peek_token (parser
->lexer
);
19574 if (tok
->type
== CPP_CLOSE_BRACE
)
19576 if (tok
->type
== CPP_EOF
)
19579 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
19581 cp_lexer_consume_token (parser
->lexer
);
19582 cp_parser_require_pragma_eol (parser
, tok
);
19583 error_suppress
= false;
19585 else if (!error_suppress
)
19587 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
19588 error_suppress
= true;
19591 substmt
= cp_parser_omp_structured_block (parser
);
19592 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
19593 add_stmt (substmt
);
19595 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
19597 substmt
= pop_stmt_list (stmt
);
19599 stmt
= make_node (OMP_SECTIONS
);
19600 TREE_TYPE (stmt
) = void_type_node
;
19601 OMP_SECTIONS_BODY (stmt
) = substmt
;
19608 # pragma omp sections sections-clause[optseq] newline
19611 #define OMP_SECTIONS_CLAUSE_MASK \
19612 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19613 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19614 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19615 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19616 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19619 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
19623 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
19624 "#pragma omp sections", pragma_tok
);
19626 ret
= cp_parser_omp_sections_scope (parser
);
19628 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
19634 # pragma parallel parallel-clause new-line
19635 # pragma parallel for parallel-for-clause new-line
19636 # pragma parallel sections parallel-sections-clause new-line */
19638 #define OMP_PARALLEL_CLAUSE_MASK \
19639 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19640 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19641 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19642 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19643 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19644 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19645 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19646 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19649 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
19651 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
19652 const char *p_name
= "#pragma omp parallel";
19653 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
19654 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
19657 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
19659 cp_lexer_consume_token (parser
->lexer
);
19660 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
19661 p_name
= "#pragma omp parallel for";
19662 mask
|= OMP_FOR_CLAUSE_MASK
;
19663 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
19665 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19667 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
19668 const char *p
= IDENTIFIER_POINTER (id
);
19669 if (strcmp (p
, "sections") == 0)
19671 cp_lexer_consume_token (parser
->lexer
);
19672 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
19673 p_name
= "#pragma omp parallel sections";
19674 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
19675 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
19679 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
19680 block
= begin_omp_parallel ();
19681 save
= cp_parser_begin_omp_structured_block (parser
);
19685 case PRAGMA_OMP_PARALLEL
:
19686 cp_parser_already_scoped_statement (parser
);
19687 par_clause
= clauses
;
19690 case PRAGMA_OMP_PARALLEL_FOR
:
19691 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
19692 stmt
= cp_parser_omp_for_loop (parser
);
19694 OMP_FOR_CLAUSES (stmt
) = ws_clause
;
19697 case PRAGMA_OMP_PARALLEL_SECTIONS
:
19698 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
19699 stmt
= cp_parser_omp_sections_scope (parser
);
19701 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
19705 gcc_unreachable ();
19708 cp_parser_end_omp_structured_block (parser
, save
);
19709 stmt
= finish_omp_parallel (par_clause
, block
);
19710 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
19711 OMP_PARALLEL_COMBINED (stmt
) = 1;
19716 # pragma omp single single-clause[optseq] new-line
19717 structured-block */
19719 #define OMP_SINGLE_CLAUSE_MASK \
19720 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19721 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19722 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19723 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19726 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
19728 tree stmt
= make_node (OMP_SINGLE
);
19729 TREE_TYPE (stmt
) = void_type_node
;
19731 OMP_SINGLE_CLAUSES (stmt
)
19732 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
19733 "#pragma omp single", pragma_tok
);
19734 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
19736 return add_stmt (stmt
);
19740 # pragma omp threadprivate (variable-list) */
19743 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
19747 vars
= cp_parser_omp_var_list (parser
, 0, NULL
);
19748 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19750 finish_omp_threadprivate (vars
);
19753 /* Main entry point to OpenMP statement pragmas. */
19756 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
19760 switch (pragma_tok
->pragma_kind
)
19762 case PRAGMA_OMP_ATOMIC
:
19763 cp_parser_omp_atomic (parser
, pragma_tok
);
19765 case PRAGMA_OMP_CRITICAL
:
19766 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
19768 case PRAGMA_OMP_FOR
:
19769 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
19771 case PRAGMA_OMP_MASTER
:
19772 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
19774 case PRAGMA_OMP_ORDERED
:
19775 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
19777 case PRAGMA_OMP_PARALLEL
:
19778 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
19780 case PRAGMA_OMP_SECTIONS
:
19781 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
19783 case PRAGMA_OMP_SINGLE
:
19784 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
19787 gcc_unreachable ();
19791 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
19796 static GTY (()) cp_parser
*the_parser
;
19799 /* Special handling for the first token or line in the file. The first
19800 thing in the file might be #pragma GCC pch_preprocess, which loads a
19801 PCH file, which is a GC collection point. So we need to handle this
19802 first pragma without benefit of an existing lexer structure.
19804 Always returns one token to the caller in *FIRST_TOKEN. This is
19805 either the true first token of the file, or the first token after
19806 the initial pragma. */
19809 cp_parser_initial_pragma (cp_token
*first_token
)
19813 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19814 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
19817 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19818 if (first_token
->type
== CPP_STRING
)
19820 name
= first_token
->u
.value
;
19822 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19823 if (first_token
->type
!= CPP_PRAGMA_EOL
)
19824 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19827 error ("expected string literal");
19829 /* Skip to the end of the pragma. */
19830 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
19831 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19833 /* Now actually load the PCH file. */
19835 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
19837 /* Read one more token to return to our caller. We have to do this
19838 after reading the PCH file in, since its pointers have to be
19840 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19843 /* Normal parsing of a pragma token. Here we can (and must) use the
19847 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
19849 cp_token
*pragma_tok
;
19852 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
19853 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
19854 parser
->lexer
->in_pragma
= true;
19856 id
= pragma_tok
->pragma_kind
;
19859 case PRAGMA_GCC_PCH_PREPROCESS
:
19860 error ("%<#pragma GCC pch_preprocess%> must be first");
19863 case PRAGMA_OMP_BARRIER
:
19866 case pragma_compound
:
19867 cp_parser_omp_barrier (parser
, pragma_tok
);
19870 error ("%<#pragma omp barrier%> may only be "
19871 "used in compound statements");
19878 case PRAGMA_OMP_FLUSH
:
19881 case pragma_compound
:
19882 cp_parser_omp_flush (parser
, pragma_tok
);
19885 error ("%<#pragma omp flush%> may only be "
19886 "used in compound statements");
19893 case PRAGMA_OMP_THREADPRIVATE
:
19894 cp_parser_omp_threadprivate (parser
, pragma_tok
);
19897 case PRAGMA_OMP_ATOMIC
:
19898 case PRAGMA_OMP_CRITICAL
:
19899 case PRAGMA_OMP_FOR
:
19900 case PRAGMA_OMP_MASTER
:
19901 case PRAGMA_OMP_ORDERED
:
19902 case PRAGMA_OMP_PARALLEL
:
19903 case PRAGMA_OMP_SECTIONS
:
19904 case PRAGMA_OMP_SINGLE
:
19905 if (context
== pragma_external
)
19907 cp_parser_omp_construct (parser
, pragma_tok
);
19910 case PRAGMA_OMP_SECTION
:
19911 error ("%<#pragma omp section%> may only be used in "
19912 "%<#pragma omp sections%> construct");
19916 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
19917 c_invoke_pragma_handler (id
);
19921 cp_parser_error (parser
, "expected declaration specifiers");
19925 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
19929 /* The interface the pragma parsers have to the lexer. */
19932 pragma_lex (tree
*value
)
19935 enum cpp_ttype ret
;
19937 tok
= cp_lexer_peek_token (the_parser
->lexer
);
19940 *value
= tok
->u
.value
;
19942 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
19944 else if (ret
== CPP_STRING
)
19945 *value
= cp_parser_string_literal (the_parser
, false, false);
19948 cp_lexer_consume_token (the_parser
->lexer
);
19949 if (ret
== CPP_KEYWORD
)
19957 /* External interface. */
19959 /* Parse one entire translation unit. */
19962 c_parse_file (void)
19964 bool error_occurred
;
19965 static bool already_called
= false;
19967 if (already_called
)
19969 sorry ("inter-module optimizations not implemented for C++");
19972 already_called
= true;
19974 the_parser
= cp_parser_new ();
19975 push_deferring_access_checks (flag_access_control
19976 ? dk_no_deferred
: dk_no_check
);
19977 error_occurred
= cp_parser_translation_unit (the_parser
);
19981 #include "gt-cp-parser.h"