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. */
1404 bool greater_than_is_operator_p
;
1406 /* TRUE if default arguments are allowed within a parameter list
1407 that starts at this point. FALSE if only a gnu extension makes
1408 them permissible. */
1409 bool default_arg_ok_p
;
1411 /* TRUE if we are parsing an integral constant-expression. See
1412 [expr.const] for a precise definition. */
1413 bool integral_constant_expression_p
;
1415 /* TRUE if we are parsing an integral constant-expression -- but a
1416 non-constant expression should be permitted as well. This flag
1417 is used when parsing an array bound so that GNU variable-length
1418 arrays are tolerated. */
1419 bool allow_non_integral_constant_expression_p
;
1421 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1422 been seen that makes the expression non-constant. */
1423 bool non_integral_constant_expression_p
;
1425 /* TRUE if local variable names and `this' are forbidden in the
1427 bool local_variables_forbidden_p
;
1429 /* TRUE if the declaration we are parsing is part of a
1430 linkage-specification of the form `extern string-literal
1432 bool in_unbraced_linkage_specification_p
;
1434 /* TRUE if we are presently parsing a declarator, after the
1435 direct-declarator. */
1436 bool in_declarator_p
;
1438 /* TRUE if we are presently parsing a template-argument-list. */
1439 bool in_template_argument_list_p
;
1441 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1442 to IN_OMP_BLOCK if parsing OpenMP structured block and
1443 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1444 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1445 iteration-statement, OpenMP block or loop within that switch. */
1446 #define IN_SWITCH_STMT 1
1447 #define IN_ITERATION_STMT 2
1448 #define IN_OMP_BLOCK 4
1449 #define IN_OMP_FOR 8
1450 #define IN_IF_STMT 16
1451 unsigned char in_statement
;
1453 /* TRUE if we are presently parsing the body of a switch statement.
1454 Note that this doesn't quite overlap with in_statement above.
1455 The difference relates to giving the right sets of error messages:
1456 "case not in switch" vs "break statement used with OpenMP...". */
1457 bool in_switch_statement_p
;
1459 /* TRUE if we are parsing a type-id in an expression context. In
1460 such a situation, both "type (expr)" and "type (type)" are valid
1462 bool in_type_id_in_expr_p
;
1464 /* TRUE if we are currently in a header file where declarations are
1465 implicitly extern "C". */
1466 bool implicit_extern_c
;
1468 /* TRUE if strings in expressions should be translated to the execution
1470 bool translate_strings_p
;
1472 /* TRUE if we are presently parsing the body of a function, but not
1474 bool in_function_body
;
1476 /* If non-NULL, then we are parsing a construct where new type
1477 definitions are not permitted. The string stored here will be
1478 issued as an error message if a type is defined. */
1479 const char *type_definition_forbidden_message
;
1481 /* A list of lists. The outer list is a stack, used for member
1482 functions of local classes. At each level there are two sub-list,
1483 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1484 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1485 TREE_VALUE's. The functions are chained in reverse declaration
1488 The TREE_PURPOSE sublist contains those functions with default
1489 arguments that need post processing, and the TREE_VALUE sublist
1490 contains those functions with definitions that need post
1493 These lists can only be processed once the outermost class being
1494 defined is complete. */
1495 tree unparsed_functions_queues
;
1497 /* The number of classes whose definitions are currently in
1499 unsigned num_classes_being_defined
;
1501 /* The number of template parameter lists that apply directly to the
1502 current declaration. */
1503 unsigned num_template_parameter_lists
;
1508 /* Constructors and destructors. */
1510 static cp_parser
*cp_parser_new
1513 /* Routines to parse various constructs.
1515 Those that return `tree' will return the error_mark_node (rather
1516 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1517 Sometimes, they will return an ordinary node if error-recovery was
1518 attempted, even though a parse error occurred. So, to check
1519 whether or not a parse error occurred, you should always use
1520 cp_parser_error_occurred. If the construct is optional (indicated
1521 either by an `_opt' in the name of the function that does the
1522 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1523 the construct is not present. */
1525 /* Lexical conventions [gram.lex] */
1527 static tree cp_parser_identifier
1529 static tree cp_parser_string_literal
1530 (cp_parser
*, bool, bool);
1532 /* Basic concepts [gram.basic] */
1534 static bool cp_parser_translation_unit
1537 /* Expressions [gram.expr] */
1539 static tree cp_parser_primary_expression
1540 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1541 static tree cp_parser_id_expression
1542 (cp_parser
*, bool, bool, bool *, bool, bool);
1543 static tree cp_parser_unqualified_id
1544 (cp_parser
*, bool, bool, bool, bool);
1545 static tree cp_parser_nested_name_specifier_opt
1546 (cp_parser
*, bool, bool, bool, bool);
1547 static tree cp_parser_nested_name_specifier
1548 (cp_parser
*, bool, bool, bool, bool);
1549 static tree cp_parser_class_or_namespace_name
1550 (cp_parser
*, bool, bool, bool, bool, bool);
1551 static tree cp_parser_postfix_expression
1552 (cp_parser
*, bool, bool);
1553 static tree cp_parser_postfix_open_square_expression
1554 (cp_parser
*, tree
, bool);
1555 static tree cp_parser_postfix_dot_deref_expression
1556 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*);
1557 static tree cp_parser_parenthesized_expression_list
1558 (cp_parser
*, bool, bool, bool, bool *);
1559 static void cp_parser_pseudo_destructor_name
1560 (cp_parser
*, tree
*, tree
*);
1561 static tree cp_parser_unary_expression
1562 (cp_parser
*, bool, bool);
1563 static enum tree_code cp_parser_unary_operator
1565 static tree cp_parser_new_expression
1567 static tree cp_parser_new_placement
1569 static tree cp_parser_new_type_id
1570 (cp_parser
*, tree
*);
1571 static cp_declarator
*cp_parser_new_declarator_opt
1573 static cp_declarator
*cp_parser_direct_new_declarator
1575 static tree cp_parser_new_initializer
1577 static tree cp_parser_delete_expression
1579 static tree cp_parser_cast_expression
1580 (cp_parser
*, bool, bool);
1581 static tree cp_parser_binary_expression
1582 (cp_parser
*, bool);
1583 static tree cp_parser_question_colon_clause
1584 (cp_parser
*, tree
);
1585 static tree cp_parser_assignment_expression
1586 (cp_parser
*, bool);
1587 static enum tree_code cp_parser_assignment_operator_opt
1589 static tree cp_parser_expression
1590 (cp_parser
*, bool);
1591 static tree cp_parser_constant_expression
1592 (cp_parser
*, bool, bool *);
1593 static tree cp_parser_builtin_offsetof
1596 /* Statements [gram.stmt.stmt] */
1598 static void cp_parser_statement
1599 (cp_parser
*, tree
, bool, bool *);
1600 static void cp_parser_label_for_labeled_statement
1602 static tree cp_parser_expression_statement
1603 (cp_parser
*, tree
);
1604 static tree cp_parser_compound_statement
1605 (cp_parser
*, tree
, bool);
1606 static void cp_parser_statement_seq_opt
1607 (cp_parser
*, tree
);
1608 static tree cp_parser_selection_statement
1609 (cp_parser
*, bool *);
1610 static tree cp_parser_condition
1612 static tree cp_parser_iteration_statement
1614 static void cp_parser_for_init_statement
1616 static tree cp_parser_jump_statement
1618 static void cp_parser_declaration_statement
1621 static tree cp_parser_implicitly_scoped_statement
1622 (cp_parser
*, bool *);
1623 static void cp_parser_already_scoped_statement
1626 /* Declarations [gram.dcl.dcl] */
1628 static void cp_parser_declaration_seq_opt
1630 static void cp_parser_declaration
1632 static void cp_parser_block_declaration
1633 (cp_parser
*, bool);
1634 static void cp_parser_simple_declaration
1635 (cp_parser
*, bool);
1636 static void cp_parser_decl_specifier_seq
1637 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1638 static tree cp_parser_storage_class_specifier_opt
1640 static tree cp_parser_function_specifier_opt
1641 (cp_parser
*, cp_decl_specifier_seq
*);
1642 static tree cp_parser_type_specifier
1643 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1645 static tree cp_parser_simple_type_specifier
1646 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1647 static tree cp_parser_type_name
1649 static tree cp_parser_elaborated_type_specifier
1650 (cp_parser
*, bool, bool);
1651 static tree cp_parser_enum_specifier
1653 static void cp_parser_enumerator_list
1654 (cp_parser
*, tree
);
1655 static void cp_parser_enumerator_definition
1656 (cp_parser
*, tree
);
1657 static tree cp_parser_namespace_name
1659 static void cp_parser_namespace_definition
1661 static void cp_parser_namespace_body
1663 static tree cp_parser_qualified_namespace_specifier
1665 static void cp_parser_namespace_alias_definition
1667 static bool cp_parser_using_declaration
1668 (cp_parser
*, bool);
1669 static void cp_parser_using_directive
1671 static void cp_parser_asm_definition
1673 static void cp_parser_linkage_specification
1675 static void cp_parser_static_assert
1676 (cp_parser
*, bool);
1678 /* Declarators [gram.dcl.decl] */
1680 static tree cp_parser_init_declarator
1681 (cp_parser
*, cp_decl_specifier_seq
*, VEC (deferred_access_check
,gc
)*, bool, bool, int, bool *);
1682 static cp_declarator
*cp_parser_declarator
1683 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1684 static cp_declarator
*cp_parser_direct_declarator
1685 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1686 static enum tree_code cp_parser_ptr_operator
1687 (cp_parser
*, tree
*, cp_cv_quals
*);
1688 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1690 static tree cp_parser_declarator_id
1691 (cp_parser
*, bool);
1692 static tree cp_parser_type_id
1694 static void cp_parser_type_specifier_seq
1695 (cp_parser
*, bool, cp_decl_specifier_seq
*);
1696 static cp_parameter_declarator
*cp_parser_parameter_declaration_clause
1698 static cp_parameter_declarator
*cp_parser_parameter_declaration_list
1699 (cp_parser
*, bool *);
1700 static cp_parameter_declarator
*cp_parser_parameter_declaration
1701 (cp_parser
*, bool, bool *);
1702 static void cp_parser_function_body
1704 static tree cp_parser_initializer
1705 (cp_parser
*, bool *, bool *);
1706 static tree cp_parser_initializer_clause
1707 (cp_parser
*, bool *);
1708 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1709 (cp_parser
*, bool *);
1711 static bool cp_parser_ctor_initializer_opt_and_function_body
1714 /* Classes [gram.class] */
1716 static tree cp_parser_class_name
1717 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1718 static tree cp_parser_class_specifier
1720 static tree cp_parser_class_head
1721 (cp_parser
*, bool *, tree
*, tree
*);
1722 static enum tag_types cp_parser_class_key
1724 static void cp_parser_member_specification_opt
1726 static void cp_parser_member_declaration
1728 static tree cp_parser_pure_specifier
1730 static tree cp_parser_constant_initializer
1733 /* Derived classes [gram.class.derived] */
1735 static tree cp_parser_base_clause
1737 static tree cp_parser_base_specifier
1740 /* Special member functions [gram.special] */
1742 static tree cp_parser_conversion_function_id
1744 static tree cp_parser_conversion_type_id
1746 static cp_declarator
*cp_parser_conversion_declarator_opt
1748 static bool cp_parser_ctor_initializer_opt
1750 static void cp_parser_mem_initializer_list
1752 static tree cp_parser_mem_initializer
1754 static tree cp_parser_mem_initializer_id
1757 /* Overloading [gram.over] */
1759 static tree cp_parser_operator_function_id
1761 static tree cp_parser_operator
1764 /* Templates [gram.temp] */
1766 static void cp_parser_template_declaration
1767 (cp_parser
*, bool);
1768 static tree cp_parser_template_parameter_list
1770 static tree cp_parser_template_parameter
1771 (cp_parser
*, bool *, bool *);
1772 static tree cp_parser_type_parameter
1773 (cp_parser
*, bool *);
1774 static tree cp_parser_template_id
1775 (cp_parser
*, bool, bool, bool);
1776 static tree cp_parser_template_name
1777 (cp_parser
*, bool, bool, bool, bool *);
1778 static tree cp_parser_template_argument_list
1780 static tree cp_parser_template_argument
1782 static void cp_parser_explicit_instantiation
1784 static void cp_parser_explicit_specialization
1787 /* Exception handling [gram.exception] */
1789 static tree cp_parser_try_block
1791 static bool cp_parser_function_try_block
1793 static void cp_parser_handler_seq
1795 static void cp_parser_handler
1797 static tree cp_parser_exception_declaration
1799 static tree cp_parser_throw_expression
1801 static tree cp_parser_exception_specification_opt
1803 static tree cp_parser_type_id_list
1806 /* GNU Extensions */
1808 static tree cp_parser_asm_specification_opt
1810 static tree cp_parser_asm_operand_list
1812 static tree cp_parser_asm_clobber_list
1814 static tree cp_parser_attributes_opt
1816 static tree cp_parser_attribute_list
1818 static bool cp_parser_extension_opt
1819 (cp_parser
*, int *);
1820 static void cp_parser_label_declaration
1823 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
1824 static bool cp_parser_pragma
1825 (cp_parser
*, enum pragma_context
);
1827 /* Objective-C++ Productions */
1829 static tree cp_parser_objc_message_receiver
1831 static tree cp_parser_objc_message_args
1833 static tree cp_parser_objc_message_expression
1835 static tree cp_parser_objc_encode_expression
1837 static tree cp_parser_objc_defs_expression
1839 static tree cp_parser_objc_protocol_expression
1841 static tree cp_parser_objc_selector_expression
1843 static tree cp_parser_objc_expression
1845 static bool cp_parser_objc_selector_p
1847 static tree cp_parser_objc_selector
1849 static tree cp_parser_objc_protocol_refs_opt
1851 static void cp_parser_objc_declaration
1853 static tree cp_parser_objc_statement
1856 /* Utility Routines */
1858 static tree cp_parser_lookup_name
1859 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*);
1860 static tree cp_parser_lookup_name_simple
1861 (cp_parser
*, tree
);
1862 static tree cp_parser_maybe_treat_template_as_class
1864 static bool cp_parser_check_declarator_template_parameters
1865 (cp_parser
*, cp_declarator
*);
1866 static bool cp_parser_check_template_parameters
1867 (cp_parser
*, unsigned);
1868 static tree cp_parser_simple_cast_expression
1870 static tree cp_parser_global_scope_opt
1871 (cp_parser
*, bool);
1872 static bool cp_parser_constructor_declarator_p
1873 (cp_parser
*, bool);
1874 static tree cp_parser_function_definition_from_specifiers_and_declarator
1875 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
1876 static tree cp_parser_function_definition_after_declarator
1877 (cp_parser
*, bool);
1878 static void cp_parser_template_declaration_after_export
1879 (cp_parser
*, bool);
1880 static void cp_parser_perform_template_parameter_access_checks
1881 (VEC (deferred_access_check
,gc
)*);
1882 static tree cp_parser_single_declaration
1883 (cp_parser
*, VEC (deferred_access_check
,gc
)*, bool, bool *);
1884 static tree cp_parser_functional_cast
1885 (cp_parser
*, tree
);
1886 static tree cp_parser_save_member_function_body
1887 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1888 static tree cp_parser_enclosed_template_argument_list
1890 static void cp_parser_save_default_args
1891 (cp_parser
*, tree
);
1892 static void cp_parser_late_parsing_for_member
1893 (cp_parser
*, tree
);
1894 static void cp_parser_late_parsing_default_args
1895 (cp_parser
*, tree
);
1896 static tree cp_parser_sizeof_operand
1897 (cp_parser
*, enum rid
);
1898 static bool cp_parser_declares_only_class_p
1900 static void cp_parser_set_storage_class
1901 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
);
1902 static void cp_parser_set_decl_spec_type
1903 (cp_decl_specifier_seq
*, tree
, bool);
1904 static bool cp_parser_friend_p
1905 (const cp_decl_specifier_seq
*);
1906 static cp_token
*cp_parser_require
1907 (cp_parser
*, enum cpp_ttype
, const char *);
1908 static cp_token
*cp_parser_require_keyword
1909 (cp_parser
*, enum rid
, const char *);
1910 static bool cp_parser_token_starts_function_definition_p
1912 static bool cp_parser_next_token_starts_class_definition_p
1914 static bool cp_parser_next_token_ends_template_argument_p
1916 static bool cp_parser_nth_token_starts_template_argument_list_p
1917 (cp_parser
*, size_t);
1918 static enum tag_types cp_parser_token_is_class_key
1920 static void cp_parser_check_class_key
1921 (enum tag_types
, tree type
);
1922 static void cp_parser_check_access_in_redeclaration
1924 static bool cp_parser_optional_template_keyword
1926 static void cp_parser_pre_parsed_nested_name_specifier
1928 static void cp_parser_cache_group
1929 (cp_parser
*, enum cpp_ttype
, unsigned);
1930 static void cp_parser_parse_tentatively
1932 static void cp_parser_commit_to_tentative_parse
1934 static void cp_parser_abort_tentative_parse
1936 static bool cp_parser_parse_definitely
1938 static inline bool cp_parser_parsing_tentatively
1940 static bool cp_parser_uncommitted_to_tentative_parse_p
1942 static void cp_parser_error
1943 (cp_parser
*, const char *);
1944 static void cp_parser_name_lookup_error
1945 (cp_parser
*, tree
, tree
, const char *);
1946 static bool cp_parser_simulate_error
1948 static bool cp_parser_check_type_definition
1950 static void cp_parser_check_for_definition_in_return_type
1951 (cp_declarator
*, tree
);
1952 static void cp_parser_check_for_invalid_template_id
1953 (cp_parser
*, tree
);
1954 static bool cp_parser_non_integral_constant_expression
1955 (cp_parser
*, const char *);
1956 static void cp_parser_diagnose_invalid_type_name
1957 (cp_parser
*, tree
, tree
);
1958 static bool cp_parser_parse_and_diagnose_invalid_type_name
1960 static int cp_parser_skip_to_closing_parenthesis
1961 (cp_parser
*, bool, bool, bool);
1962 static void cp_parser_skip_to_end_of_statement
1964 static void cp_parser_consume_semicolon_at_end_of_statement
1966 static void cp_parser_skip_to_end_of_block_or_statement
1968 static void cp_parser_skip_to_closing_brace
1970 static void cp_parser_skip_to_end_of_template_parameter_list
1972 static void cp_parser_skip_to_pragma_eol
1973 (cp_parser
*, cp_token
*);
1974 static bool cp_parser_error_occurred
1976 static bool cp_parser_allow_gnu_extensions_p
1978 static bool cp_parser_is_string_literal
1980 static bool cp_parser_is_keyword
1981 (cp_token
*, enum rid
);
1982 static tree cp_parser_make_typename_type
1983 (cp_parser
*, tree
, tree
);
1985 /* Returns nonzero if we are parsing tentatively. */
1988 cp_parser_parsing_tentatively (cp_parser
* parser
)
1990 return parser
->context
->next
!= NULL
;
1993 /* Returns nonzero if TOKEN is a string literal. */
1996 cp_parser_is_string_literal (cp_token
* token
)
1998 return (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
);
2001 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2004 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2006 return token
->keyword
== keyword
;
2009 /* If not parsing tentatively, issue a diagnostic of the form
2010 FILE:LINE: MESSAGE before TOKEN
2011 where TOKEN is the next token in the input stream. MESSAGE
2012 (specified by the caller) is usually of the form "expected
2016 cp_parser_error (cp_parser
* parser
, const char* message
)
2018 if (!cp_parser_simulate_error (parser
))
2020 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2021 /* This diagnostic makes more sense if it is tagged to the line
2022 of the token we just peeked at. */
2023 cp_lexer_set_source_position_from_token (token
);
2025 if (token
->type
== CPP_PRAGMA
)
2027 error ("%<#pragma%> is not allowed here");
2028 cp_parser_skip_to_pragma_eol (parser
, token
);
2032 c_parse_error (message
,
2033 /* Because c_parser_error does not understand
2034 CPP_KEYWORD, keywords are treated like
2036 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2041 /* Issue an error about name-lookup failing. NAME is the
2042 IDENTIFIER_NODE DECL is the result of
2043 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2044 the thing that we hoped to find. */
2047 cp_parser_name_lookup_error (cp_parser
* parser
,
2050 const char* desired
)
2052 /* If name lookup completely failed, tell the user that NAME was not
2054 if (decl
== error_mark_node
)
2056 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2057 error ("%<%E::%E%> has not been declared",
2058 parser
->scope
, name
);
2059 else if (parser
->scope
== global_namespace
)
2060 error ("%<::%E%> has not been declared", name
);
2061 else if (parser
->object_scope
2062 && !CLASS_TYPE_P (parser
->object_scope
))
2063 error ("request for member %qE in non-class type %qT",
2064 name
, parser
->object_scope
);
2065 else if (parser
->object_scope
)
2066 error ("%<%T::%E%> has not been declared",
2067 parser
->object_scope
, name
);
2069 error ("%qE has not been declared", name
);
2071 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2072 error ("%<%E::%E%> %s", parser
->scope
, name
, desired
);
2073 else if (parser
->scope
== global_namespace
)
2074 error ("%<::%E%> %s", name
, desired
);
2076 error ("%qE %s", name
, desired
);
2079 /* If we are parsing tentatively, remember that an error has occurred
2080 during this tentative parse. Returns true if the error was
2081 simulated; false if a message should be issued by the caller. */
2084 cp_parser_simulate_error (cp_parser
* parser
)
2086 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2088 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2094 /* Check for repeated decl-specifiers. */
2097 cp_parser_check_decl_spec (cp_decl_specifier_seq
*decl_specs
)
2101 for (ds
= ds_first
; ds
!= ds_last
; ++ds
)
2103 unsigned count
= decl_specs
->specs
[(int)ds
];
2106 /* The "long" specifier is a special case because of "long long". */
2110 error ("%<long long long%> is too long for GCC");
2111 else if (pedantic
&& !in_system_header
&& warn_long_long
)
2112 pedwarn ("ISO C++ does not support %<long long%>");
2116 static const char *const decl_spec_names
[] = {
2132 error ("duplicate %qs", decl_spec_names
[(int)ds
]);
2137 /* This function is called when a type is defined. If type
2138 definitions are forbidden at this point, an error message is
2142 cp_parser_check_type_definition (cp_parser
* parser
)
2144 /* If types are forbidden here, issue a message. */
2145 if (parser
->type_definition_forbidden_message
)
2147 /* Use `%s' to print the string in case there are any escape
2148 characters in the message. */
2149 error ("%s", parser
->type_definition_forbidden_message
);
2155 /* This function is called when the DECLARATOR is processed. The TYPE
2156 was a type defined in the decl-specifiers. If it is invalid to
2157 define a type in the decl-specifiers for DECLARATOR, an error is
2161 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2164 /* [dcl.fct] forbids type definitions in return types.
2165 Unfortunately, it's not easy to know whether or not we are
2166 processing a return type until after the fact. */
2168 && (declarator
->kind
== cdk_pointer
2169 || declarator
->kind
== cdk_reference
2170 || declarator
->kind
== cdk_ptrmem
))
2171 declarator
= declarator
->declarator
;
2173 && declarator
->kind
== cdk_function
)
2175 error ("new types may not be defined in a return type");
2176 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2181 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2182 "<" in any valid C++ program. If the next token is indeed "<",
2183 issue a message warning the user about what appears to be an
2184 invalid attempt to form a template-id. */
2187 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2190 cp_token_position start
= 0;
2192 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2195 error ("%qT is not a template", type
);
2196 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2197 error ("%qE is not a template", type
);
2199 error ("invalid template-id");
2200 /* Remember the location of the invalid "<". */
2201 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2202 start
= cp_lexer_token_position (parser
->lexer
, true);
2203 /* Consume the "<". */
2204 cp_lexer_consume_token (parser
->lexer
);
2205 /* Parse the template arguments. */
2206 cp_parser_enclosed_template_argument_list (parser
);
2207 /* Permanently remove the invalid template arguments so that
2208 this error message is not issued again. */
2210 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2214 /* If parsing an integral constant-expression, issue an error message
2215 about the fact that THING appeared and return true. Otherwise,
2216 return false. In either case, set
2217 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2220 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2223 parser
->non_integral_constant_expression_p
= true;
2224 if (parser
->integral_constant_expression_p
)
2226 if (!parser
->allow_non_integral_constant_expression_p
)
2228 error ("%s cannot appear in a constant-expression", thing
);
2235 /* Emit a diagnostic for an invalid type name. SCOPE is the
2236 qualifying scope (or NULL, if none) for ID. This function commits
2237 to the current active tentative parse, if any. (Otherwise, the
2238 problematic construct might be encountered again later, resulting
2239 in duplicate error messages.) */
2242 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree scope
, tree id
)
2244 tree decl
, old_scope
;
2245 /* Try to lookup the identifier. */
2246 old_scope
= parser
->scope
;
2247 parser
->scope
= scope
;
2248 decl
= cp_parser_lookup_name_simple (parser
, id
);
2249 parser
->scope
= old_scope
;
2250 /* If the lookup found a template-name, it means that the user forgot
2251 to specify an argument list. Emit a useful error message. */
2252 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2253 error ("invalid use of template-name %qE without an argument list", decl
);
2254 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2255 error ("invalid use of destructor %qD as a type", id
);
2256 else if (TREE_CODE (decl
) == TYPE_DECL
)
2257 /* Something like 'unsigned A a;' */
2258 error ("invalid combination of multiple type-specifiers");
2259 else if (!parser
->scope
)
2261 /* Issue an error message. */
2262 error ("%qE does not name a type", id
);
2263 /* If we're in a template class, it's possible that the user was
2264 referring to a type from a base class. For example:
2266 template <typename T> struct A { typedef T X; };
2267 template <typename T> struct B : public A<T> { X x; };
2269 The user should have said "typename A<T>::X". */
2270 if (processing_template_decl
&& current_class_type
2271 && TYPE_BINFO (current_class_type
))
2275 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2279 tree base_type
= BINFO_TYPE (b
);
2280 if (CLASS_TYPE_P (base_type
)
2281 && dependent_type_p (base_type
))
2284 /* Go from a particular instantiation of the
2285 template (which will have an empty TYPE_FIELDs),
2286 to the main version. */
2287 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2288 for (field
= TYPE_FIELDS (base_type
);
2290 field
= TREE_CHAIN (field
))
2291 if (TREE_CODE (field
) == TYPE_DECL
2292 && DECL_NAME (field
) == id
)
2294 inform ("(perhaps %<typename %T::%E%> was intended)",
2295 BINFO_TYPE (b
), id
);
2304 /* Here we diagnose qualified-ids where the scope is actually correct,
2305 but the identifier does not resolve to a valid type name. */
2306 else if (parser
->scope
!= error_mark_node
)
2308 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2309 error ("%qE in namespace %qE does not name a type",
2311 else if (TYPE_P (parser
->scope
))
2312 error ("%qE in class %qT does not name a type", id
, parser
->scope
);
2316 cp_parser_commit_to_tentative_parse (parser
);
2319 /* Check for a common situation where a type-name should be present,
2320 but is not, and issue a sensible error message. Returns true if an
2321 invalid type-name was detected.
2323 The situation handled by this function are variable declarations of the
2324 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2325 Usually, `ID' should name a type, but if we got here it means that it
2326 does not. We try to emit the best possible error message depending on
2327 how exactly the id-expression looks like. */
2330 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2334 cp_parser_parse_tentatively (parser
);
2335 id
= cp_parser_id_expression (parser
,
2336 /*template_keyword_p=*/false,
2337 /*check_dependency_p=*/true,
2338 /*template_p=*/NULL
,
2339 /*declarator_p=*/true,
2340 /*optional_p=*/false);
2341 /* After the id-expression, there should be a plain identifier,
2342 otherwise this is not a simple variable declaration. Also, if
2343 the scope is dependent, we cannot do much. */
2344 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
2345 || (parser
->scope
&& TYPE_P (parser
->scope
)
2346 && dependent_type_p (parser
->scope
)))
2348 cp_parser_abort_tentative_parse (parser
);
2351 if (!cp_parser_parse_definitely (parser
) || TREE_CODE (id
) == TYPE_DECL
)
2354 /* Emit a diagnostic for the invalid type. */
2355 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
, id
);
2356 /* Skip to the end of the declaration; there's no point in
2357 trying to process it. */
2358 cp_parser_skip_to_end_of_block_or_statement (parser
);
2362 /* Consume tokens up to, and including, the next non-nested closing `)'.
2363 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2364 are doing error recovery. Returns -1 if OR_COMMA is true and we
2365 found an unnested comma. */
2368 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2373 unsigned paren_depth
= 0;
2374 unsigned brace_depth
= 0;
2376 if (recovering
&& !or_comma
2377 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2382 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2384 switch (token
->type
)
2387 case CPP_PRAGMA_EOL
:
2388 /* If we've run out of tokens, then there is no closing `)'. */
2392 /* This matches the processing in skip_to_end_of_statement. */
2397 case CPP_OPEN_BRACE
:
2400 case CPP_CLOSE_BRACE
:
2406 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
)
2410 case CPP_OPEN_PAREN
:
2415 case CPP_CLOSE_PAREN
:
2416 if (!brace_depth
&& !paren_depth
--)
2419 cp_lexer_consume_token (parser
->lexer
);
2428 /* Consume the token. */
2429 cp_lexer_consume_token (parser
->lexer
);
2433 /* Consume tokens until we reach the end of the current statement.
2434 Normally, that will be just before consuming a `;'. However, if a
2435 non-nested `}' comes first, then we stop before consuming that. */
2438 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2440 unsigned nesting_depth
= 0;
2444 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2446 switch (token
->type
)
2449 case CPP_PRAGMA_EOL
:
2450 /* If we've run out of tokens, stop. */
2454 /* If the next token is a `;', we have reached the end of the
2460 case CPP_CLOSE_BRACE
:
2461 /* If this is a non-nested '}', stop before consuming it.
2462 That way, when confronted with something like:
2466 we stop before consuming the closing '}', even though we
2467 have not yet reached a `;'. */
2468 if (nesting_depth
== 0)
2471 /* If it is the closing '}' for a block that we have
2472 scanned, stop -- but only after consuming the token.
2478 we will stop after the body of the erroneously declared
2479 function, but before consuming the following `typedef'
2481 if (--nesting_depth
== 0)
2483 cp_lexer_consume_token (parser
->lexer
);
2487 case CPP_OPEN_BRACE
:
2495 /* Consume the token. */
2496 cp_lexer_consume_token (parser
->lexer
);
2500 /* This function is called at the end of a statement or declaration.
2501 If the next token is a semicolon, it is consumed; otherwise, error
2502 recovery is attempted. */
2505 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2507 /* Look for the trailing `;'. */
2508 if (!cp_parser_require (parser
, CPP_SEMICOLON
, "`;'"))
2510 /* If there is additional (erroneous) input, skip to the end of
2512 cp_parser_skip_to_end_of_statement (parser
);
2513 /* If the next token is now a `;', consume it. */
2514 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2515 cp_lexer_consume_token (parser
->lexer
);
2519 /* Skip tokens until we have consumed an entire block, or until we
2520 have consumed a non-nested `;'. */
2523 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2525 int nesting_depth
= 0;
2527 while (nesting_depth
>= 0)
2529 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2531 switch (token
->type
)
2534 case CPP_PRAGMA_EOL
:
2535 /* If we've run out of tokens, stop. */
2539 /* Stop if this is an unnested ';'. */
2544 case CPP_CLOSE_BRACE
:
2545 /* Stop if this is an unnested '}', or closes the outermost
2552 case CPP_OPEN_BRACE
:
2561 /* Consume the token. */
2562 cp_lexer_consume_token (parser
->lexer
);
2566 /* Skip tokens until a non-nested closing curly brace is the next
2570 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
2572 unsigned nesting_depth
= 0;
2576 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2578 switch (token
->type
)
2581 case CPP_PRAGMA_EOL
:
2582 /* If we've run out of tokens, stop. */
2585 case CPP_CLOSE_BRACE
:
2586 /* If the next token is a non-nested `}', then we have reached
2587 the end of the current block. */
2588 if (nesting_depth
-- == 0)
2592 case CPP_OPEN_BRACE
:
2593 /* If it the next token is a `{', then we are entering a new
2594 block. Consume the entire block. */
2602 /* Consume the token. */
2603 cp_lexer_consume_token (parser
->lexer
);
2607 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2608 parameter is the PRAGMA token, allowing us to purge the entire pragma
2612 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
2616 parser
->lexer
->in_pragma
= false;
2619 token
= cp_lexer_consume_token (parser
->lexer
);
2620 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
2622 /* Ensure that the pragma is not parsed again. */
2623 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
2626 /* Require pragma end of line, resyncing with it as necessary. The
2627 arguments are as for cp_parser_skip_to_pragma_eol. */
2630 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
2632 parser
->lexer
->in_pragma
= false;
2633 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, "end of line"))
2634 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
2637 /* This is a simple wrapper around make_typename_type. When the id is
2638 an unresolved identifier node, we can provide a superior diagnostic
2639 using cp_parser_diagnose_invalid_type_name. */
2642 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
, tree id
)
2645 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
2647 result
= make_typename_type (scope
, id
, typename_type
,
2648 /*complain=*/tf_none
);
2649 if (result
== error_mark_node
)
2650 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
);
2653 return make_typename_type (scope
, id
, typename_type
, tf_error
);
2657 /* Create a new C++ parser. */
2660 cp_parser_new (void)
2666 /* cp_lexer_new_main is called before calling ggc_alloc because
2667 cp_lexer_new_main might load a PCH file. */
2668 lexer
= cp_lexer_new_main ();
2670 /* Initialize the binops_by_token so that we can get the tree
2671 directly from the token. */
2672 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
2673 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
2675 parser
= GGC_CNEW (cp_parser
);
2676 parser
->lexer
= lexer
;
2677 parser
->context
= cp_parser_context_new (NULL
);
2679 /* For now, we always accept GNU extensions. */
2680 parser
->allow_gnu_extensions_p
= 1;
2682 /* The `>' token is a greater-than operator, not the end of a
2684 parser
->greater_than_is_operator_p
= true;
2686 parser
->default_arg_ok_p
= true;
2688 /* We are not parsing a constant-expression. */
2689 parser
->integral_constant_expression_p
= false;
2690 parser
->allow_non_integral_constant_expression_p
= false;
2691 parser
->non_integral_constant_expression_p
= false;
2693 /* Local variable names are not forbidden. */
2694 parser
->local_variables_forbidden_p
= false;
2696 /* We are not processing an `extern "C"' declaration. */
2697 parser
->in_unbraced_linkage_specification_p
= false;
2699 /* We are not processing a declarator. */
2700 parser
->in_declarator_p
= false;
2702 /* We are not processing a template-argument-list. */
2703 parser
->in_template_argument_list_p
= false;
2705 /* We are not in an iteration statement. */
2706 parser
->in_statement
= 0;
2708 /* We are not in a switch statement. */
2709 parser
->in_switch_statement_p
= false;
2711 /* We are not parsing a type-id inside an expression. */
2712 parser
->in_type_id_in_expr_p
= false;
2714 /* Declarations aren't implicitly extern "C". */
2715 parser
->implicit_extern_c
= false;
2717 /* String literals should be translated to the execution character set. */
2718 parser
->translate_strings_p
= true;
2720 /* We are not parsing a function body. */
2721 parser
->in_function_body
= false;
2723 /* The unparsed function queue is empty. */
2724 parser
->unparsed_functions_queues
= build_tree_list (NULL_TREE
, NULL_TREE
);
2726 /* There are no classes being defined. */
2727 parser
->num_classes_being_defined
= 0;
2729 /* No template parameters apply. */
2730 parser
->num_template_parameter_lists
= 0;
2735 /* Create a cp_lexer structure which will emit the tokens in CACHE
2736 and push it onto the parser's lexer stack. This is used for delayed
2737 parsing of in-class method bodies and default arguments, and should
2738 not be confused with tentative parsing. */
2740 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
2742 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
2743 lexer
->next
= parser
->lexer
;
2744 parser
->lexer
= lexer
;
2746 /* Move the current source position to that of the first token in the
2748 cp_lexer_set_source_position_from_token (lexer
->next_token
);
2751 /* Pop the top lexer off the parser stack. This is never used for the
2752 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2754 cp_parser_pop_lexer (cp_parser
*parser
)
2756 cp_lexer
*lexer
= parser
->lexer
;
2757 parser
->lexer
= lexer
->next
;
2758 cp_lexer_destroy (lexer
);
2760 /* Put the current source position back where it was before this
2761 lexer was pushed. */
2762 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
2765 /* Lexical conventions [gram.lex] */
2767 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2771 cp_parser_identifier (cp_parser
* parser
)
2775 /* Look for the identifier. */
2776 token
= cp_parser_require (parser
, CPP_NAME
, "identifier");
2777 /* Return the value. */
2778 return token
? token
->u
.value
: error_mark_node
;
2781 /* Parse a sequence of adjacent string constants. Returns a
2782 TREE_STRING representing the combined, nul-terminated string
2783 constant. If TRANSLATE is true, translate the string to the
2784 execution character set. If WIDE_OK is true, a wide string is
2787 C++98 [lex.string] says that if a narrow string literal token is
2788 adjacent to a wide string literal token, the behavior is undefined.
2789 However, C99 6.4.5p4 says that this results in a wide string literal.
2790 We follow C99 here, for consistency with the C front end.
2792 This code is largely lifted from lex_string() in c-lex.c.
2794 FUTURE: ObjC++ will need to handle @-strings here. */
2796 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
2801 struct obstack str_ob
;
2802 cpp_string str
, istr
, *strs
;
2805 tok
= cp_lexer_peek_token (parser
->lexer
);
2806 if (!cp_parser_is_string_literal (tok
))
2808 cp_parser_error (parser
, "expected string-literal");
2809 return error_mark_node
;
2812 /* Try to avoid the overhead of creating and destroying an obstack
2813 for the common case of just one string. */
2814 if (!cp_parser_is_string_literal
2815 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
2817 cp_lexer_consume_token (parser
->lexer
);
2819 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
2820 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
2822 if (tok
->type
== CPP_WSTRING
)
2829 gcc_obstack_init (&str_ob
);
2834 cp_lexer_consume_token (parser
->lexer
);
2836 str
.text
= (unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
2837 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
2838 if (tok
->type
== CPP_WSTRING
)
2841 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
2843 tok
= cp_lexer_peek_token (parser
->lexer
);
2845 while (cp_parser_is_string_literal (tok
));
2847 strs
= (cpp_string
*) obstack_finish (&str_ob
);
2850 if (wide
&& !wide_ok
)
2852 cp_parser_error (parser
, "a wide string is invalid in this context");
2856 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
2857 (parse_in
, strs
, count
, &istr
, wide
))
2859 value
= build_string (istr
.len
, (char *)istr
.text
);
2860 free ((void *)istr
.text
);
2862 TREE_TYPE (value
) = wide
? wchar_array_type_node
: char_array_type_node
;
2863 value
= fix_string_type (value
);
2866 /* cpp_interpret_string has issued an error. */
2867 value
= error_mark_node
;
2870 obstack_free (&str_ob
, 0);
2876 /* Basic concepts [gram.basic] */
2878 /* Parse a translation-unit.
2881 declaration-seq [opt]
2883 Returns TRUE if all went well. */
2886 cp_parser_translation_unit (cp_parser
* parser
)
2888 /* The address of the first non-permanent object on the declarator
2890 static void *declarator_obstack_base
;
2894 /* Create the declarator obstack, if necessary. */
2895 if (!cp_error_declarator
)
2897 gcc_obstack_init (&declarator_obstack
);
2898 /* Create the error declarator. */
2899 cp_error_declarator
= make_declarator (cdk_error
);
2900 /* Create the empty parameter list. */
2901 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
2902 /* Remember where the base of the declarator obstack lies. */
2903 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
2906 cp_parser_declaration_seq_opt (parser
);
2908 /* If there are no tokens left then all went well. */
2909 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
2911 /* Get rid of the token array; we don't need it any more. */
2912 cp_lexer_destroy (parser
->lexer
);
2913 parser
->lexer
= NULL
;
2915 /* This file might have been a context that's implicitly extern
2916 "C". If so, pop the lang context. (Only relevant for PCH.) */
2917 if (parser
->implicit_extern_c
)
2919 pop_lang_context ();
2920 parser
->implicit_extern_c
= false;
2924 finish_translation_unit ();
2930 cp_parser_error (parser
, "expected declaration");
2934 /* Make sure the declarator obstack was fully cleaned up. */
2935 gcc_assert (obstack_next_free (&declarator_obstack
)
2936 == declarator_obstack_base
);
2938 /* All went well. */
2942 /* Expressions [gram.expr] */
2944 /* Parse a primary-expression.
2955 ( compound-statement )
2956 __builtin_va_arg ( assignment-expression , type-id )
2957 __builtin_offsetof ( type-id , offsetof-expression )
2959 Objective-C++ Extension:
2967 ADDRESS_P is true iff this expression was immediately preceded by
2968 "&" and therefore might denote a pointer-to-member. CAST_P is true
2969 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2970 true iff this expression is a template argument.
2972 Returns a representation of the expression. Upon return, *IDK
2973 indicates what kind of id-expression (if any) was present. */
2976 cp_parser_primary_expression (cp_parser
*parser
,
2979 bool template_arg_p
,
2984 /* Assume the primary expression is not an id-expression. */
2985 *idk
= CP_ID_KIND_NONE
;
2987 /* Peek at the next token. */
2988 token
= cp_lexer_peek_token (parser
->lexer
);
2989 switch (token
->type
)
3000 token
= cp_lexer_consume_token (parser
->lexer
);
3001 /* Floating-point literals are only allowed in an integral
3002 constant expression if they are cast to an integral or
3003 enumeration type. */
3004 if (TREE_CODE (token
->u
.value
) == REAL_CST
3005 && parser
->integral_constant_expression_p
3008 /* CAST_P will be set even in invalid code like "int(2.7 +
3009 ...)". Therefore, we have to check that the next token
3010 is sure to end the cast. */
3013 cp_token
*next_token
;
3015 next_token
= cp_lexer_peek_token (parser
->lexer
);
3016 if (/* The comma at the end of an
3017 enumerator-definition. */
3018 next_token
->type
!= CPP_COMMA
3019 /* The curly brace at the end of an enum-specifier. */
3020 && next_token
->type
!= CPP_CLOSE_BRACE
3021 /* The end of a statement. */
3022 && next_token
->type
!= CPP_SEMICOLON
3023 /* The end of the cast-expression. */
3024 && next_token
->type
!= CPP_CLOSE_PAREN
3025 /* The end of an array bound. */
3026 && next_token
->type
!= CPP_CLOSE_SQUARE
3027 /* The closing ">" in a template-argument-list. */
3028 && (next_token
->type
!= CPP_GREATER
3029 || parser
->greater_than_is_operator_p
))
3033 /* If we are within a cast, then the constraint that the
3034 cast is to an integral or enumeration type will be
3035 checked at that point. If we are not within a cast, then
3036 this code is invalid. */
3038 cp_parser_non_integral_constant_expression
3039 (parser
, "floating-point literal");
3041 return token
->u
.value
;
3045 /* ??? Should wide strings be allowed when parser->translate_strings_p
3046 is false (i.e. in attributes)? If not, we can kill the third
3047 argument to cp_parser_string_literal. */
3048 return cp_parser_string_literal (parser
,
3049 parser
->translate_strings_p
,
3052 case CPP_OPEN_PAREN
:
3055 bool saved_greater_than_is_operator_p
;
3057 /* Consume the `('. */
3058 cp_lexer_consume_token (parser
->lexer
);
3059 /* Within a parenthesized expression, a `>' token is always
3060 the greater-than operator. */
3061 saved_greater_than_is_operator_p
3062 = parser
->greater_than_is_operator_p
;
3063 parser
->greater_than_is_operator_p
= true;
3064 /* If we see `( { ' then we are looking at the beginning of
3065 a GNU statement-expression. */
3066 if (cp_parser_allow_gnu_extensions_p (parser
)
3067 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
3069 /* Statement-expressions are not allowed by the standard. */
3071 pedwarn ("ISO C++ forbids braced-groups within expressions");
3073 /* And they're not allowed outside of a function-body; you
3074 cannot, for example, write:
3076 int i = ({ int j = 3; j + 1; });
3078 at class or namespace scope. */
3079 if (!parser
->in_function_body
)
3081 error ("statement-expressions are allowed only inside functions");
3082 cp_parser_skip_to_end_of_block_or_statement (parser
);
3083 expr
= error_mark_node
;
3087 /* Start the statement-expression. */
3088 expr
= begin_stmt_expr ();
3089 /* Parse the compound-statement. */
3090 cp_parser_compound_statement (parser
, expr
, false);
3092 expr
= finish_stmt_expr (expr
, false);
3097 /* Parse the parenthesized expression. */
3098 expr
= cp_parser_expression (parser
, cast_p
);
3099 /* Let the front end know that this expression was
3100 enclosed in parentheses. This matters in case, for
3101 example, the expression is of the form `A::B', since
3102 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3104 finish_parenthesized_expr (expr
);
3106 /* The `>' token might be the end of a template-id or
3107 template-parameter-list now. */
3108 parser
->greater_than_is_operator_p
3109 = saved_greater_than_is_operator_p
;
3110 /* Consume the `)'. */
3111 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
3112 cp_parser_skip_to_end_of_statement (parser
);
3118 switch (token
->keyword
)
3120 /* These two are the boolean literals. */
3122 cp_lexer_consume_token (parser
->lexer
);
3123 return boolean_true_node
;
3125 cp_lexer_consume_token (parser
->lexer
);
3126 return boolean_false_node
;
3128 /* The `__null' literal. */
3130 cp_lexer_consume_token (parser
->lexer
);
3133 /* Recognize the `this' keyword. */
3135 cp_lexer_consume_token (parser
->lexer
);
3136 if (parser
->local_variables_forbidden_p
)
3138 error ("%<this%> may not be used in this context");
3139 return error_mark_node
;
3141 /* Pointers cannot appear in constant-expressions. */
3142 if (cp_parser_non_integral_constant_expression (parser
,
3144 return error_mark_node
;
3145 return finish_this_expr ();
3147 /* The `operator' keyword can be the beginning of an
3152 case RID_FUNCTION_NAME
:
3153 case RID_PRETTY_FUNCTION_NAME
:
3154 case RID_C99_FUNCTION_NAME
:
3155 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3156 __func__ are the names of variables -- but they are
3157 treated specially. Therefore, they are handled here,
3158 rather than relying on the generic id-expression logic
3159 below. Grammatically, these names are id-expressions.
3161 Consume the token. */
3162 token
= cp_lexer_consume_token (parser
->lexer
);
3163 /* Look up the name. */
3164 return finish_fname (token
->u
.value
);
3171 /* The `__builtin_va_arg' construct is used to handle
3172 `va_arg'. Consume the `__builtin_va_arg' token. */
3173 cp_lexer_consume_token (parser
->lexer
);
3174 /* Look for the opening `('. */
3175 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
3176 /* Now, parse the assignment-expression. */
3177 expression
= cp_parser_assignment_expression (parser
,
3179 /* Look for the `,'. */
3180 cp_parser_require (parser
, CPP_COMMA
, "`,'");
3181 /* Parse the type-id. */
3182 type
= cp_parser_type_id (parser
);
3183 /* Look for the closing `)'. */
3184 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
3185 /* Using `va_arg' in a constant-expression is not
3187 if (cp_parser_non_integral_constant_expression (parser
,
3189 return error_mark_node
;
3190 return build_x_va_arg (expression
, type
);
3194 return cp_parser_builtin_offsetof (parser
);
3196 /* Objective-C++ expressions. */
3198 case RID_AT_PROTOCOL
:
3199 case RID_AT_SELECTOR
:
3200 return cp_parser_objc_expression (parser
);
3203 cp_parser_error (parser
, "expected primary-expression");
3204 return error_mark_node
;
3207 /* An id-expression can start with either an identifier, a
3208 `::' as the beginning of a qualified-id, or the "operator"
3212 case CPP_TEMPLATE_ID
:
3213 case CPP_NESTED_NAME_SPECIFIER
:
3217 const char *error_msg
;
3222 /* Parse the id-expression. */
3224 = cp_parser_id_expression (parser
,
3225 /*template_keyword_p=*/false,
3226 /*check_dependency_p=*/true,
3228 /*declarator_p=*/false,
3229 /*optional_p=*/false);
3230 if (id_expression
== error_mark_node
)
3231 return error_mark_node
;
3232 token
= cp_lexer_peek_token (parser
->lexer
);
3233 done
= (token
->type
!= CPP_OPEN_SQUARE
3234 && token
->type
!= CPP_OPEN_PAREN
3235 && token
->type
!= CPP_DOT
3236 && token
->type
!= CPP_DEREF
3237 && token
->type
!= CPP_PLUS_PLUS
3238 && token
->type
!= CPP_MINUS_MINUS
);
3239 /* If we have a template-id, then no further lookup is
3240 required. If the template-id was for a template-class, we
3241 will sometimes have a TYPE_DECL at this point. */
3242 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
3243 || TREE_CODE (id_expression
) == TYPE_DECL
)
3244 decl
= id_expression
;
3245 /* Look up the name. */
3248 tree ambiguous_decls
;
3250 decl
= cp_parser_lookup_name (parser
, id_expression
,
3253 /*is_namespace=*/false,
3254 /*check_dependency=*/true,
3256 /* If the lookup was ambiguous, an error will already have
3258 if (ambiguous_decls
)
3259 return error_mark_node
;
3261 /* In Objective-C++, an instance variable (ivar) may be preferred
3262 to whatever cp_parser_lookup_name() found. */
3263 decl
= objc_lookup_ivar (decl
, id_expression
);
3265 /* If name lookup gives us a SCOPE_REF, then the
3266 qualifying scope was dependent. */
3267 if (TREE_CODE (decl
) == SCOPE_REF
)
3269 /* Check to see if DECL is a local variable in a context
3270 where that is forbidden. */
3271 if (parser
->local_variables_forbidden_p
3272 && local_variable_p (decl
))
3274 /* It might be that we only found DECL because we are
3275 trying to be generous with pre-ISO scoping rules.
3276 For example, consider:
3280 for (int i = 0; i < 10; ++i) {}
3281 extern void f(int j = i);
3284 Here, name look up will originally find the out
3285 of scope `i'. We need to issue a warning message,
3286 but then use the global `i'. */
3287 decl
= check_for_out_of_scope_variable (decl
);
3288 if (local_variable_p (decl
))
3290 error ("local variable %qD may not appear in this context",
3292 return error_mark_node
;
3297 decl
= (finish_id_expression
3298 (id_expression
, decl
, parser
->scope
,
3300 parser
->integral_constant_expression_p
,
3301 parser
->allow_non_integral_constant_expression_p
,
3302 &parser
->non_integral_constant_expression_p
,
3303 template_p
, done
, address_p
,
3307 cp_parser_error (parser
, error_msg
);
3311 /* Anything else is an error. */
3313 /* ...unless we have an Objective-C++ message or string literal,
3315 if (c_dialect_objc ()
3316 && (token
->type
== CPP_OPEN_SQUARE
3317 || token
->type
== CPP_OBJC_STRING
))
3318 return cp_parser_objc_expression (parser
);
3320 cp_parser_error (parser
, "expected primary-expression");
3321 return error_mark_node
;
3325 /* Parse an id-expression.
3332 :: [opt] nested-name-specifier template [opt] unqualified-id
3334 :: operator-function-id
3337 Return a representation of the unqualified portion of the
3338 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3339 a `::' or nested-name-specifier.
3341 Often, if the id-expression was a qualified-id, the caller will
3342 want to make a SCOPE_REF to represent the qualified-id. This
3343 function does not do this in order to avoid wastefully creating
3344 SCOPE_REFs when they are not required.
3346 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3349 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3350 uninstantiated templates.
3352 If *TEMPLATE_P is non-NULL, it is set to true iff the
3353 `template' keyword is used to explicitly indicate that the entity
3354 named is a template.
3356 If DECLARATOR_P is true, the id-expression is appearing as part of
3357 a declarator, rather than as part of an expression. */
3360 cp_parser_id_expression (cp_parser
*parser
,
3361 bool template_keyword_p
,
3362 bool check_dependency_p
,
3367 bool global_scope_p
;
3368 bool nested_name_specifier_p
;
3370 /* Assume the `template' keyword was not used. */
3372 *template_p
= template_keyword_p
;
3374 /* Look for the optional `::' operator. */
3376 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
3378 /* Look for the optional nested-name-specifier. */
3379 nested_name_specifier_p
3380 = (cp_parser_nested_name_specifier_opt (parser
,
3381 /*typename_keyword_p=*/false,
3386 /* If there is a nested-name-specifier, then we are looking at
3387 the first qualified-id production. */
3388 if (nested_name_specifier_p
)
3391 tree saved_object_scope
;
3392 tree saved_qualifying_scope
;
3393 tree unqualified_id
;
3396 /* See if the next token is the `template' keyword. */
3398 template_p
= &is_template
;
3399 *template_p
= cp_parser_optional_template_keyword (parser
);
3400 /* Name lookup we do during the processing of the
3401 unqualified-id might obliterate SCOPE. */
3402 saved_scope
= parser
->scope
;
3403 saved_object_scope
= parser
->object_scope
;
3404 saved_qualifying_scope
= parser
->qualifying_scope
;
3405 /* Process the final unqualified-id. */
3406 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
3409 /*optional_p=*/false);
3410 /* Restore the SAVED_SCOPE for our caller. */
3411 parser
->scope
= saved_scope
;
3412 parser
->object_scope
= saved_object_scope
;
3413 parser
->qualifying_scope
= saved_qualifying_scope
;
3415 return unqualified_id
;
3417 /* Otherwise, if we are in global scope, then we are looking at one
3418 of the other qualified-id productions. */
3419 else if (global_scope_p
)
3424 /* Peek at the next token. */
3425 token
= cp_lexer_peek_token (parser
->lexer
);
3427 /* If it's an identifier, and the next token is not a "<", then
3428 we can avoid the template-id case. This is an optimization
3429 for this common case. */
3430 if (token
->type
== CPP_NAME
3431 && !cp_parser_nth_token_starts_template_argument_list_p
3433 return cp_parser_identifier (parser
);
3435 cp_parser_parse_tentatively (parser
);
3436 /* Try a template-id. */
3437 id
= cp_parser_template_id (parser
,
3438 /*template_keyword_p=*/false,
3439 /*check_dependency_p=*/true,
3441 /* If that worked, we're done. */
3442 if (cp_parser_parse_definitely (parser
))
3445 /* Peek at the next token. (Changes in the token buffer may
3446 have invalidated the pointer obtained above.) */
3447 token
= cp_lexer_peek_token (parser
->lexer
);
3449 switch (token
->type
)
3452 return cp_parser_identifier (parser
);
3455 if (token
->keyword
== RID_OPERATOR
)
3456 return cp_parser_operator_function_id (parser
);
3460 cp_parser_error (parser
, "expected id-expression");
3461 return error_mark_node
;
3465 return cp_parser_unqualified_id (parser
, template_keyword_p
,
3466 /*check_dependency_p=*/true,
3471 /* Parse an unqualified-id.
3475 operator-function-id
3476 conversion-function-id
3480 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3481 keyword, in a construct like `A::template ...'.
3483 Returns a representation of unqualified-id. For the `identifier'
3484 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3485 production a BIT_NOT_EXPR is returned; the operand of the
3486 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3487 other productions, see the documentation accompanying the
3488 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3489 names are looked up in uninstantiated templates. If DECLARATOR_P
3490 is true, the unqualified-id is appearing as part of a declarator,
3491 rather than as part of an expression. */
3494 cp_parser_unqualified_id (cp_parser
* parser
,
3495 bool template_keyword_p
,
3496 bool check_dependency_p
,
3502 /* Peek at the next token. */
3503 token
= cp_lexer_peek_token (parser
->lexer
);
3505 switch (token
->type
)
3511 /* We don't know yet whether or not this will be a
3513 cp_parser_parse_tentatively (parser
);
3514 /* Try a template-id. */
3515 id
= cp_parser_template_id (parser
, template_keyword_p
,
3518 /* If it worked, we're done. */
3519 if (cp_parser_parse_definitely (parser
))
3521 /* Otherwise, it's an ordinary identifier. */
3522 return cp_parser_identifier (parser
);
3525 case CPP_TEMPLATE_ID
:
3526 return cp_parser_template_id (parser
, template_keyword_p
,
3533 tree qualifying_scope
;
3538 /* Consume the `~' token. */
3539 cp_lexer_consume_token (parser
->lexer
);
3540 /* Parse the class-name. The standard, as written, seems to
3543 template <typename T> struct S { ~S (); };
3544 template <typename T> S<T>::~S() {}
3546 is invalid, since `~' must be followed by a class-name, but
3547 `S<T>' is dependent, and so not known to be a class.
3548 That's not right; we need to look in uninstantiated
3549 templates. A further complication arises from:
3551 template <typename T> void f(T t) {
3555 Here, it is not possible to look up `T' in the scope of `T'
3556 itself. We must look in both the current scope, and the
3557 scope of the containing complete expression.
3559 Yet another issue is:
3568 The standard does not seem to say that the `S' in `~S'
3569 should refer to the type `S' and not the data member
3572 /* DR 244 says that we look up the name after the "~" in the
3573 same scope as we looked up the qualifying name. That idea
3574 isn't fully worked out; it's more complicated than that. */
3575 scope
= parser
->scope
;
3576 object_scope
= parser
->object_scope
;
3577 qualifying_scope
= parser
->qualifying_scope
;
3579 /* Check for invalid scopes. */
3580 if (scope
== error_mark_node
)
3582 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3583 cp_lexer_consume_token (parser
->lexer
);
3584 return error_mark_node
;
3586 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
3588 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3589 error ("scope %qT before %<~%> is not a class-name", scope
);
3590 cp_parser_simulate_error (parser
);
3591 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
3592 cp_lexer_consume_token (parser
->lexer
);
3593 return error_mark_node
;
3595 gcc_assert (!scope
|| TYPE_P (scope
));
3597 /* If the name is of the form "X::~X" it's OK. */
3598 token
= cp_lexer_peek_token (parser
->lexer
);
3600 && token
->type
== CPP_NAME
3601 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3603 && constructor_name_p (token
->u
.value
, scope
))
3605 cp_lexer_consume_token (parser
->lexer
);
3606 return build_nt (BIT_NOT_EXPR
, scope
);
3609 /* If there was an explicit qualification (S::~T), first look
3610 in the scope given by the qualification (i.e., S). */
3612 type_decl
= NULL_TREE
;
3615 cp_parser_parse_tentatively (parser
);
3616 type_decl
= cp_parser_class_name (parser
,
3617 /*typename_keyword_p=*/false,
3618 /*template_keyword_p=*/false,
3620 /*check_dependency=*/false,
3621 /*class_head_p=*/false,
3623 if (cp_parser_parse_definitely (parser
))
3626 /* In "N::S::~S", look in "N" as well. */
3627 if (!done
&& scope
&& qualifying_scope
)
3629 cp_parser_parse_tentatively (parser
);
3630 parser
->scope
= qualifying_scope
;
3631 parser
->object_scope
= NULL_TREE
;
3632 parser
->qualifying_scope
= NULL_TREE
;
3634 = cp_parser_class_name (parser
,
3635 /*typename_keyword_p=*/false,
3636 /*template_keyword_p=*/false,
3638 /*check_dependency=*/false,
3639 /*class_head_p=*/false,
3641 if (cp_parser_parse_definitely (parser
))
3644 /* In "p->S::~T", look in the scope given by "*p" as well. */
3645 else if (!done
&& object_scope
)
3647 cp_parser_parse_tentatively (parser
);
3648 parser
->scope
= object_scope
;
3649 parser
->object_scope
= NULL_TREE
;
3650 parser
->qualifying_scope
= NULL_TREE
;
3652 = cp_parser_class_name (parser
,
3653 /*typename_keyword_p=*/false,
3654 /*template_keyword_p=*/false,
3656 /*check_dependency=*/false,
3657 /*class_head_p=*/false,
3659 if (cp_parser_parse_definitely (parser
))
3662 /* Look in the surrounding context. */
3665 parser
->scope
= NULL_TREE
;
3666 parser
->object_scope
= NULL_TREE
;
3667 parser
->qualifying_scope
= NULL_TREE
;
3669 = cp_parser_class_name (parser
,
3670 /*typename_keyword_p=*/false,
3671 /*template_keyword_p=*/false,
3673 /*check_dependency=*/false,
3674 /*class_head_p=*/false,
3677 /* If an error occurred, assume that the name of the
3678 destructor is the same as the name of the qualifying
3679 class. That allows us to keep parsing after running
3680 into ill-formed destructor names. */
3681 if (type_decl
== error_mark_node
&& scope
)
3682 return build_nt (BIT_NOT_EXPR
, scope
);
3683 else if (type_decl
== error_mark_node
)
3684 return error_mark_node
;
3686 /* Check that destructor name and scope match. */
3687 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
3689 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
3690 error ("declaration of %<~%T%> as member of %qT",
3692 cp_parser_simulate_error (parser
);
3693 return error_mark_node
;
3698 A typedef-name that names a class shall not be used as the
3699 identifier in the declarator for a destructor declaration. */
3701 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
3702 && !DECL_SELF_REFERENCE_P (type_decl
)
3703 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
3704 error ("typedef-name %qD used as destructor declarator",
3707 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
3711 if (token
->keyword
== RID_OPERATOR
)
3715 /* This could be a template-id, so we try that first. */
3716 cp_parser_parse_tentatively (parser
);
3717 /* Try a template-id. */
3718 id
= cp_parser_template_id (parser
, template_keyword_p
,
3719 /*check_dependency_p=*/true,
3721 /* If that worked, we're done. */
3722 if (cp_parser_parse_definitely (parser
))
3724 /* We still don't know whether we're looking at an
3725 operator-function-id or a conversion-function-id. */
3726 cp_parser_parse_tentatively (parser
);
3727 /* Try an operator-function-id. */
3728 id
= cp_parser_operator_function_id (parser
);
3729 /* If that didn't work, try a conversion-function-id. */
3730 if (!cp_parser_parse_definitely (parser
))
3731 id
= cp_parser_conversion_function_id (parser
);
3740 cp_parser_error (parser
, "expected unqualified-id");
3741 return error_mark_node
;
3745 /* Parse an (optional) nested-name-specifier.
3747 nested-name-specifier:
3748 class-or-namespace-name :: nested-name-specifier [opt]
3749 class-or-namespace-name :: template nested-name-specifier [opt]
3751 PARSER->SCOPE should be set appropriately before this function is
3752 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3753 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3756 Sets PARSER->SCOPE to the class (TYPE) or namespace
3757 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3758 it unchanged if there is no nested-name-specifier. Returns the new
3759 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3761 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3762 part of a declaration and/or decl-specifier. */
3765 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
3766 bool typename_keyword_p
,
3767 bool check_dependency_p
,
3769 bool is_declaration
)
3771 bool success
= false;
3772 cp_token_position start
= 0;
3775 /* Remember where the nested-name-specifier starts. */
3776 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3778 start
= cp_lexer_token_position (parser
->lexer
, false);
3779 push_deferring_access_checks (dk_deferred
);
3786 tree saved_qualifying_scope
;
3787 bool template_keyword_p
;
3789 /* Spot cases that cannot be the beginning of a
3790 nested-name-specifier. */
3791 token
= cp_lexer_peek_token (parser
->lexer
);
3793 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3794 the already parsed nested-name-specifier. */
3795 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3797 /* Grab the nested-name-specifier and continue the loop. */
3798 cp_parser_pre_parsed_nested_name_specifier (parser
);
3799 /* If we originally encountered this nested-name-specifier
3800 with IS_DECLARATION set to false, we will not have
3801 resolved TYPENAME_TYPEs, so we must do so here. */
3803 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3805 new_scope
= resolve_typename_type (parser
->scope
,
3806 /*only_current_p=*/false);
3807 if (new_scope
!= error_mark_node
)
3808 parser
->scope
= new_scope
;
3814 /* Spot cases that cannot be the beginning of a
3815 nested-name-specifier. On the second and subsequent times
3816 through the loop, we look for the `template' keyword. */
3817 if (success
&& token
->keyword
== RID_TEMPLATE
)
3819 /* A template-id can start a nested-name-specifier. */
3820 else if (token
->type
== CPP_TEMPLATE_ID
)
3824 /* If the next token is not an identifier, then it is
3825 definitely not a class-or-namespace-name. */
3826 if (token
->type
!= CPP_NAME
)
3828 /* If the following token is neither a `<' (to begin a
3829 template-id), nor a `::', then we are not looking at a
3830 nested-name-specifier. */
3831 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3832 if (token
->type
!= CPP_SCOPE
3833 && !cp_parser_nth_token_starts_template_argument_list_p
3838 /* The nested-name-specifier is optional, so we parse
3840 cp_parser_parse_tentatively (parser
);
3842 /* Look for the optional `template' keyword, if this isn't the
3843 first time through the loop. */
3845 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
3847 template_keyword_p
= false;
3849 /* Save the old scope since the name lookup we are about to do
3850 might destroy it. */
3851 old_scope
= parser
->scope
;
3852 saved_qualifying_scope
= parser
->qualifying_scope
;
3853 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3854 look up names in "X<T>::I" in order to determine that "Y" is
3855 a template. So, if we have a typename at this point, we make
3856 an effort to look through it. */
3858 && !typename_keyword_p
3860 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3861 parser
->scope
= resolve_typename_type (parser
->scope
,
3862 /*only_current_p=*/false);
3863 /* Parse the qualifying entity. */
3865 = cp_parser_class_or_namespace_name (parser
,
3871 /* Look for the `::' token. */
3872 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
3874 /* If we found what we wanted, we keep going; otherwise, we're
3876 if (!cp_parser_parse_definitely (parser
))
3878 bool error_p
= false;
3880 /* Restore the OLD_SCOPE since it was valid before the
3881 failed attempt at finding the last
3882 class-or-namespace-name. */
3883 parser
->scope
= old_scope
;
3884 parser
->qualifying_scope
= saved_qualifying_scope
;
3885 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3887 /* If the next token is an identifier, and the one after
3888 that is a `::', then any valid interpretation would have
3889 found a class-or-namespace-name. */
3890 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
3891 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3893 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
3896 token
= cp_lexer_consume_token (parser
->lexer
);
3899 if (!token
->ambiguous_p
)
3902 tree ambiguous_decls
;
3904 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
3906 /*is_template=*/false,
3907 /*is_namespace=*/false,
3908 /*check_dependency=*/true,
3910 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3911 error ("%qD used without template parameters", decl
);
3912 else if (ambiguous_decls
)
3914 error ("reference to %qD is ambiguous",
3916 print_candidates (ambiguous_decls
);
3917 decl
= error_mark_node
;
3920 cp_parser_name_lookup_error
3921 (parser
, token
->u
.value
, decl
,
3922 "is not a class or namespace");
3924 parser
->scope
= error_mark_node
;
3926 /* Treat this as a successful nested-name-specifier
3931 If the name found is not a class-name (clause
3932 _class_) or namespace-name (_namespace.def_), the
3933 program is ill-formed. */
3936 cp_lexer_consume_token (parser
->lexer
);
3940 /* We've found one valid nested-name-specifier. */
3942 /* Name lookup always gives us a DECL. */
3943 if (TREE_CODE (new_scope
) == TYPE_DECL
)
3944 new_scope
= TREE_TYPE (new_scope
);
3945 /* Uses of "template" must be followed by actual templates. */
3946 if (template_keyword_p
3947 && !(CLASS_TYPE_P (new_scope
)
3948 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
3949 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
3950 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
3951 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
3952 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
3953 == TEMPLATE_ID_EXPR
)))
3954 pedwarn (TYPE_P (new_scope
)
3955 ? "%qT is not a template"
3956 : "%qD is not a template",
3958 /* If it is a class scope, try to complete it; we are about to
3959 be looking up names inside the class. */
3960 if (TYPE_P (new_scope
)
3961 /* Since checking types for dependency can be expensive,
3962 avoid doing it if the type is already complete. */
3963 && !COMPLETE_TYPE_P (new_scope
)
3964 /* Do not try to complete dependent types. */
3965 && !dependent_type_p (new_scope
))
3966 new_scope
= complete_type (new_scope
);
3967 /* Make sure we look in the right scope the next time through
3969 parser
->scope
= new_scope
;
3972 /* If parsing tentatively, replace the sequence of tokens that makes
3973 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3974 token. That way, should we re-parse the token stream, we will
3975 not have to repeat the effort required to do the parse, nor will
3976 we issue duplicate error messages. */
3977 if (success
&& start
)
3981 token
= cp_lexer_token_at (parser
->lexer
, start
);
3982 /* Reset the contents of the START token. */
3983 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
3984 /* Retrieve any deferred checks. Do not pop this access checks yet
3985 so the memory will not be reclaimed during token replacing below. */
3986 token
->u
.tree_check_value
= GGC_CNEW (struct tree_check
);
3987 token
->u
.tree_check_value
->value
= parser
->scope
;
3988 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
3989 token
->u
.tree_check_value
->qualifying_scope
=
3990 parser
->qualifying_scope
;
3991 token
->keyword
= RID_MAX
;
3993 /* Purge all subsequent tokens. */
3994 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3998 pop_to_parent_deferring_access_checks ();
4000 return success
? parser
->scope
: NULL_TREE
;
4003 /* Parse a nested-name-specifier. See
4004 cp_parser_nested_name_specifier_opt for details. This function
4005 behaves identically, except that it will an issue an error if no
4006 nested-name-specifier is present. */
4009 cp_parser_nested_name_specifier (cp_parser
*parser
,
4010 bool typename_keyword_p
,
4011 bool check_dependency_p
,
4013 bool is_declaration
)
4017 /* Look for the nested-name-specifier. */
4018 scope
= cp_parser_nested_name_specifier_opt (parser
,
4023 /* If it was not present, issue an error message. */
4026 cp_parser_error (parser
, "expected nested-name-specifier");
4027 parser
->scope
= NULL_TREE
;
4033 /* Parse a class-or-namespace-name.
4035 class-or-namespace-name:
4039 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4040 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4041 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4042 TYPE_P is TRUE iff the next name should be taken as a class-name,
4043 even the same name is declared to be another entity in the same
4046 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4047 specified by the class-or-namespace-name. If neither is found the
4048 ERROR_MARK_NODE is returned. */
4051 cp_parser_class_or_namespace_name (cp_parser
*parser
,
4052 bool typename_keyword_p
,
4053 bool template_keyword_p
,
4054 bool check_dependency_p
,
4056 bool is_declaration
)
4059 tree saved_qualifying_scope
;
4060 tree saved_object_scope
;
4064 /* Before we try to parse the class-name, we must save away the
4065 current PARSER->SCOPE since cp_parser_class_name will destroy
4067 saved_scope
= parser
->scope
;
4068 saved_qualifying_scope
= parser
->qualifying_scope
;
4069 saved_object_scope
= parser
->object_scope
;
4070 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4071 there is no need to look for a namespace-name. */
4072 only_class_p
= template_keyword_p
|| (saved_scope
&& TYPE_P (saved_scope
));
4074 cp_parser_parse_tentatively (parser
);
4075 scope
= cp_parser_class_name (parser
,
4078 type_p
? class_type
: none_type
,
4080 /*class_head_p=*/false,
4082 /* If that didn't work, try for a namespace-name. */
4083 if (!only_class_p
&& !cp_parser_parse_definitely (parser
))
4085 /* Restore the saved scope. */
4086 parser
->scope
= saved_scope
;
4087 parser
->qualifying_scope
= saved_qualifying_scope
;
4088 parser
->object_scope
= saved_object_scope
;
4089 /* If we are not looking at an identifier followed by the scope
4090 resolution operator, then this is not part of a
4091 nested-name-specifier. (Note that this function is only used
4092 to parse the components of a nested-name-specifier.) */
4093 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
4094 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
4095 return error_mark_node
;
4096 scope
= cp_parser_namespace_name (parser
);
4102 /* Parse a postfix-expression.
4106 postfix-expression [ expression ]
4107 postfix-expression ( expression-list [opt] )
4108 simple-type-specifier ( expression-list [opt] )
4109 typename :: [opt] nested-name-specifier identifier
4110 ( expression-list [opt] )
4111 typename :: [opt] nested-name-specifier template [opt] template-id
4112 ( expression-list [opt] )
4113 postfix-expression . template [opt] id-expression
4114 postfix-expression -> template [opt] id-expression
4115 postfix-expression . pseudo-destructor-name
4116 postfix-expression -> pseudo-destructor-name
4117 postfix-expression ++
4118 postfix-expression --
4119 dynamic_cast < type-id > ( expression )
4120 static_cast < type-id > ( expression )
4121 reinterpret_cast < type-id > ( expression )
4122 const_cast < type-id > ( expression )
4123 typeid ( expression )
4129 ( type-id ) { initializer-list , [opt] }
4131 This extension is a GNU version of the C99 compound-literal
4132 construct. (The C99 grammar uses `type-name' instead of `type-id',
4133 but they are essentially the same concept.)
4135 If ADDRESS_P is true, the postfix expression is the operand of the
4136 `&' operator. CAST_P is true if this expression is the target of a
4139 Returns a representation of the expression. */
4142 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
4146 cp_id_kind idk
= CP_ID_KIND_NONE
;
4147 tree postfix_expression
= NULL_TREE
;
4149 /* Peek at the next token. */
4150 token
= cp_lexer_peek_token (parser
->lexer
);
4151 /* Some of the productions are determined by keywords. */
4152 keyword
= token
->keyword
;
4162 const char *saved_message
;
4164 /* All of these can be handled in the same way from the point
4165 of view of parsing. Begin by consuming the token
4166 identifying the cast. */
4167 cp_lexer_consume_token (parser
->lexer
);
4169 /* New types cannot be defined in the cast. */
4170 saved_message
= parser
->type_definition_forbidden_message
;
4171 parser
->type_definition_forbidden_message
4172 = "types may not be defined in casts";
4174 /* Look for the opening `<'. */
4175 cp_parser_require (parser
, CPP_LESS
, "`<'");
4176 /* Parse the type to which we are casting. */
4177 type
= cp_parser_type_id (parser
);
4178 /* Look for the closing `>'. */
4179 cp_parser_require (parser
, CPP_GREATER
, "`>'");
4180 /* Restore the old message. */
4181 parser
->type_definition_forbidden_message
= saved_message
;
4183 /* And the expression which is being cast. */
4184 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4185 expression
= cp_parser_expression (parser
, /*cast_p=*/true);
4186 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4188 /* Only type conversions to integral or enumeration types
4189 can be used in constant-expressions. */
4190 if (!cast_valid_in_integral_constant_expression_p (type
)
4191 && (cp_parser_non_integral_constant_expression
4193 "a cast to a type other than an integral or "
4194 "enumeration type")))
4195 return error_mark_node
;
4201 = build_dynamic_cast (type
, expression
);
4205 = build_static_cast (type
, expression
);
4209 = build_reinterpret_cast (type
, expression
);
4213 = build_const_cast (type
, expression
);
4224 const char *saved_message
;
4225 bool saved_in_type_id_in_expr_p
;
4227 /* Consume the `typeid' token. */
4228 cp_lexer_consume_token (parser
->lexer
);
4229 /* Look for the `(' token. */
4230 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
4231 /* Types cannot be defined in a `typeid' expression. */
4232 saved_message
= parser
->type_definition_forbidden_message
;
4233 parser
->type_definition_forbidden_message
4234 = "types may not be defined in a `typeid\' expression";
4235 /* We can't be sure yet whether we're looking at a type-id or an
4237 cp_parser_parse_tentatively (parser
);
4238 /* Try a type-id first. */
4239 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4240 parser
->in_type_id_in_expr_p
= true;
4241 type
= cp_parser_type_id (parser
);
4242 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4243 /* Look for the `)' token. Otherwise, we can't be sure that
4244 we're not looking at an expression: consider `typeid (int
4245 (3))', for example. */
4246 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4247 /* If all went well, simply lookup the type-id. */
4248 if (cp_parser_parse_definitely (parser
))
4249 postfix_expression
= get_typeid (type
);
4250 /* Otherwise, fall back to the expression variant. */
4255 /* Look for an expression. */
4256 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
4257 /* Compute its typeid. */
4258 postfix_expression
= build_typeid (expression
);
4259 /* Look for the `)' token. */
4260 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4262 /* Restore the saved message. */
4263 parser
->type_definition_forbidden_message
= saved_message
;
4264 /* `typeid' may not appear in an integral constant expression. */
4265 if (cp_parser_non_integral_constant_expression(parser
,
4266 "`typeid' operator"))
4267 return error_mark_node
;
4274 /* The syntax permitted here is the same permitted for an
4275 elaborated-type-specifier. */
4276 type
= cp_parser_elaborated_type_specifier (parser
,
4277 /*is_friend=*/false,
4278 /*is_declaration=*/false);
4279 postfix_expression
= cp_parser_functional_cast (parser
, type
);
4287 /* If the next thing is a simple-type-specifier, we may be
4288 looking at a functional cast. We could also be looking at
4289 an id-expression. So, we try the functional cast, and if
4290 that doesn't work we fall back to the primary-expression. */
4291 cp_parser_parse_tentatively (parser
);
4292 /* Look for the simple-type-specifier. */
4293 type
= cp_parser_simple_type_specifier (parser
,
4294 /*decl_specs=*/NULL
,
4295 CP_PARSER_FLAGS_NONE
);
4296 /* Parse the cast itself. */
4297 if (!cp_parser_error_occurred (parser
))
4299 = cp_parser_functional_cast (parser
, type
);
4300 /* If that worked, we're done. */
4301 if (cp_parser_parse_definitely (parser
))
4304 /* If the functional-cast didn't work out, try a
4305 compound-literal. */
4306 if (cp_parser_allow_gnu_extensions_p (parser
)
4307 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
4309 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
4310 bool saved_in_type_id_in_expr_p
;
4312 cp_parser_parse_tentatively (parser
);
4313 /* Consume the `('. */
4314 cp_lexer_consume_token (parser
->lexer
);
4315 /* Parse the type. */
4316 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4317 parser
->in_type_id_in_expr_p
= true;
4318 type
= cp_parser_type_id (parser
);
4319 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4320 /* Look for the `)'. */
4321 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
4322 /* Look for the `{'. */
4323 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
4324 /* If things aren't going well, there's no need to
4326 if (!cp_parser_error_occurred (parser
))
4328 bool non_constant_p
;
4329 /* Parse the initializer-list. */
4331 = cp_parser_initializer_list (parser
, &non_constant_p
);
4332 /* Allow a trailing `,'. */
4333 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
4334 cp_lexer_consume_token (parser
->lexer
);
4335 /* Look for the final `}'. */
4336 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
4338 /* If that worked, we're definitely looking at a
4339 compound-literal expression. */
4340 if (cp_parser_parse_definitely (parser
))
4342 /* Warn the user that a compound literal is not
4343 allowed in standard C++. */
4345 pedwarn ("ISO C++ forbids compound-literals");
4346 /* For simplicity, we disallow compound literals in
4347 constant-expressions. We could
4348 allow compound literals of integer type, whose
4349 initializer was a constant, in constant
4350 expressions. Permitting that usage, as a further
4351 extension, would not change the meaning of any
4352 currently accepted programs. (Of course, as
4353 compound literals are not part of ISO C++, the
4354 standard has nothing to say.) */
4355 if (cp_parser_non_integral_constant_expression
4356 (parser
, "non-constant compound literals"))
4358 postfix_expression
= error_mark_node
;
4361 /* Form the representation of the compound-literal. */
4363 = finish_compound_literal (type
, initializer_list
);
4368 /* It must be a primary-expression. */
4370 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
4371 /*template_arg_p=*/false,
4377 /* Keep looping until the postfix-expression is complete. */
4380 if (idk
== CP_ID_KIND_UNQUALIFIED
4381 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
4382 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
4383 /* It is not a Koenig lookup function call. */
4385 = unqualified_name_lookup_error (postfix_expression
);
4387 /* Peek at the next token. */
4388 token
= cp_lexer_peek_token (parser
->lexer
);
4390 switch (token
->type
)
4392 case CPP_OPEN_SQUARE
:
4394 = cp_parser_postfix_open_square_expression (parser
,
4397 idk
= CP_ID_KIND_NONE
;
4400 case CPP_OPEN_PAREN
:
4401 /* postfix-expression ( expression-list [opt] ) */
4404 bool is_builtin_constant_p
;
4405 bool saved_integral_constant_expression_p
= false;
4406 bool saved_non_integral_constant_expression_p
= false;
4409 is_builtin_constant_p
4410 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
4411 if (is_builtin_constant_p
)
4413 /* The whole point of __builtin_constant_p is to allow
4414 non-constant expressions to appear as arguments. */
4415 saved_integral_constant_expression_p
4416 = parser
->integral_constant_expression_p
;
4417 saved_non_integral_constant_expression_p
4418 = parser
->non_integral_constant_expression_p
;
4419 parser
->integral_constant_expression_p
= false;
4421 args
= (cp_parser_parenthesized_expression_list
4422 (parser
, /*is_attribute_list=*/false,
4423 /*cast_p=*/false, /*allow_expansion_p=*/true,
4424 /*non_constant_p=*/NULL
));
4425 if (is_builtin_constant_p
)
4427 parser
->integral_constant_expression_p
4428 = saved_integral_constant_expression_p
;
4429 parser
->non_integral_constant_expression_p
4430 = saved_non_integral_constant_expression_p
;
4433 if (args
== error_mark_node
)
4435 postfix_expression
= error_mark_node
;
4439 /* Function calls are not permitted in
4440 constant-expressions. */
4441 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
4442 && cp_parser_non_integral_constant_expression (parser
,
4445 postfix_expression
= error_mark_node
;
4450 if (idk
== CP_ID_KIND_UNQUALIFIED
)
4452 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
4458 = perform_koenig_lookup (postfix_expression
, args
);
4462 = unqualified_fn_lookup_error (postfix_expression
);
4464 /* We do not perform argument-dependent lookup if
4465 normal lookup finds a non-function, in accordance
4466 with the expected resolution of DR 218. */
4467 else if (args
&& is_overloaded_fn (postfix_expression
))
4469 tree fn
= get_first_fn (postfix_expression
);
4471 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4472 fn
= OVL_CURRENT (TREE_OPERAND (fn
, 0));
4474 /* Only do argument dependent lookup if regular
4475 lookup does not find a set of member functions.
4476 [basic.lookup.koenig]/2a */
4477 if (!DECL_FUNCTION_MEMBER_P (fn
))
4481 = perform_koenig_lookup (postfix_expression
, args
);
4486 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
4488 tree instance
= TREE_OPERAND (postfix_expression
, 0);
4489 tree fn
= TREE_OPERAND (postfix_expression
, 1);
4491 if (processing_template_decl
4492 && (type_dependent_expression_p (instance
)
4493 || (!BASELINK_P (fn
)
4494 && TREE_CODE (fn
) != FIELD_DECL
)
4495 || type_dependent_expression_p (fn
)
4496 || any_type_dependent_arguments_p (args
)))
4499 = build_nt_call_list (postfix_expression
, args
);
4503 if (BASELINK_P (fn
))
4505 = (build_new_method_call
4506 (instance
, fn
, args
, NULL_TREE
,
4507 (idk
== CP_ID_KIND_QUALIFIED
4508 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
),
4512 = finish_call_expr (postfix_expression
, args
,
4513 /*disallow_virtual=*/false,
4514 /*koenig_p=*/false);
4516 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
4517 || TREE_CODE (postfix_expression
) == MEMBER_REF
4518 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
4519 postfix_expression
= (build_offset_ref_call_from_tree
4520 (postfix_expression
, args
));
4521 else if (idk
== CP_ID_KIND_QUALIFIED
)
4522 /* A call to a static class member, or a namespace-scope
4525 = finish_call_expr (postfix_expression
, args
,
4526 /*disallow_virtual=*/true,
4529 /* All other function calls. */
4531 = finish_call_expr (postfix_expression
, args
,
4532 /*disallow_virtual=*/false,
4535 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4536 idk
= CP_ID_KIND_NONE
;
4542 /* postfix-expression . template [opt] id-expression
4543 postfix-expression . pseudo-destructor-name
4544 postfix-expression -> template [opt] id-expression
4545 postfix-expression -> pseudo-destructor-name */
4547 /* Consume the `.' or `->' operator. */
4548 cp_lexer_consume_token (parser
->lexer
);
4551 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
4557 /* postfix-expression ++ */
4558 /* Consume the `++' token. */
4559 cp_lexer_consume_token (parser
->lexer
);
4560 /* Generate a representation for the complete expression. */
4562 = finish_increment_expr (postfix_expression
,
4563 POSTINCREMENT_EXPR
);
4564 /* Increments may not appear in constant-expressions. */
4565 if (cp_parser_non_integral_constant_expression (parser
,
4567 postfix_expression
= error_mark_node
;
4568 idk
= CP_ID_KIND_NONE
;
4571 case CPP_MINUS_MINUS
:
4572 /* postfix-expression -- */
4573 /* Consume the `--' token. */
4574 cp_lexer_consume_token (parser
->lexer
);
4575 /* Generate a representation for the complete expression. */
4577 = finish_increment_expr (postfix_expression
,
4578 POSTDECREMENT_EXPR
);
4579 /* Decrements may not appear in constant-expressions. */
4580 if (cp_parser_non_integral_constant_expression (parser
,
4582 postfix_expression
= error_mark_node
;
4583 idk
= CP_ID_KIND_NONE
;
4587 return postfix_expression
;
4591 /* We should never get here. */
4593 return error_mark_node
;
4596 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4597 by cp_parser_builtin_offsetof. We're looking for
4599 postfix-expression [ expression ]
4601 FOR_OFFSETOF is set if we're being called in that context, which
4602 changes how we deal with integer constant expressions. */
4605 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
4606 tree postfix_expression
,
4611 /* Consume the `[' token. */
4612 cp_lexer_consume_token (parser
->lexer
);
4614 /* Parse the index expression. */
4615 /* ??? For offsetof, there is a question of what to allow here. If
4616 offsetof is not being used in an integral constant expression context,
4617 then we *could* get the right answer by computing the value at runtime.
4618 If we are in an integral constant expression context, then we might
4619 could accept any constant expression; hard to say without analysis.
4620 Rather than open the barn door too wide right away, allow only integer
4621 constant expressions here. */
4623 index
= cp_parser_constant_expression (parser
, false, NULL
);
4625 index
= cp_parser_expression (parser
, /*cast_p=*/false);
4627 /* Look for the closing `]'. */
4628 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
4630 /* Build the ARRAY_REF. */
4631 postfix_expression
= grok_array_decl (postfix_expression
, index
);
4633 /* When not doing offsetof, array references are not permitted in
4634 constant-expressions. */
4636 && (cp_parser_non_integral_constant_expression
4637 (parser
, "an array reference")))
4638 postfix_expression
= error_mark_node
;
4640 return postfix_expression
;
4643 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4644 by cp_parser_builtin_offsetof. We're looking for
4646 postfix-expression . template [opt] id-expression
4647 postfix-expression . pseudo-destructor-name
4648 postfix-expression -> template [opt] id-expression
4649 postfix-expression -> pseudo-destructor-name
4651 FOR_OFFSETOF is set if we're being called in that context. That sorta
4652 limits what of the above we'll actually accept, but nevermind.
4653 TOKEN_TYPE is the "." or "->" token, which will already have been
4654 removed from the stream. */
4657 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
4658 enum cpp_ttype token_type
,
4659 tree postfix_expression
,
4660 bool for_offsetof
, cp_id_kind
*idk
)
4664 bool pseudo_destructor_p
;
4665 tree scope
= NULL_TREE
;
4667 /* If this is a `->' operator, dereference the pointer. */
4668 if (token_type
== CPP_DEREF
)
4669 postfix_expression
= build_x_arrow (postfix_expression
);
4670 /* Check to see whether or not the expression is type-dependent. */
4671 dependent_p
= type_dependent_expression_p (postfix_expression
);
4672 /* The identifier following the `->' or `.' is not qualified. */
4673 parser
->scope
= NULL_TREE
;
4674 parser
->qualifying_scope
= NULL_TREE
;
4675 parser
->object_scope
= NULL_TREE
;
4676 *idk
= CP_ID_KIND_NONE
;
4677 /* Enter the scope corresponding to the type of the object
4678 given by the POSTFIX_EXPRESSION. */
4679 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
4681 scope
= TREE_TYPE (postfix_expression
);
4682 /* According to the standard, no expression should ever have
4683 reference type. Unfortunately, we do not currently match
4684 the standard in this respect in that our internal representation
4685 of an expression may have reference type even when the standard
4686 says it does not. Therefore, we have to manually obtain the
4687 underlying type here. */
4688 scope
= non_reference (scope
);
4689 /* The type of the POSTFIX_EXPRESSION must be complete. */
4690 if (scope
== unknown_type_node
)
4692 error ("%qE does not have class type", postfix_expression
);
4696 scope
= complete_type_or_else (scope
, NULL_TREE
);
4697 /* Let the name lookup machinery know that we are processing a
4698 class member access expression. */
4699 parser
->context
->object_type
= scope
;
4700 /* If something went wrong, we want to be able to discern that case,
4701 as opposed to the case where there was no SCOPE due to the type
4702 of expression being dependent. */
4704 scope
= error_mark_node
;
4705 /* If the SCOPE was erroneous, make the various semantic analysis
4706 functions exit quickly -- and without issuing additional error
4708 if (scope
== error_mark_node
)
4709 postfix_expression
= error_mark_node
;
4712 /* Assume this expression is not a pseudo-destructor access. */
4713 pseudo_destructor_p
= false;
4715 /* If the SCOPE is a scalar type, then, if this is a valid program,
4716 we must be looking at a pseudo-destructor-name. */
4717 if (scope
&& SCALAR_TYPE_P (scope
))
4722 cp_parser_parse_tentatively (parser
);
4723 /* Parse the pseudo-destructor-name. */
4725 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
4726 if (cp_parser_parse_definitely (parser
))
4728 pseudo_destructor_p
= true;
4730 = finish_pseudo_destructor_expr (postfix_expression
,
4731 s
, TREE_TYPE (type
));
4735 if (!pseudo_destructor_p
)
4737 /* If the SCOPE is not a scalar type, we are looking at an
4738 ordinary class member access expression, rather than a
4739 pseudo-destructor-name. */
4741 /* Parse the id-expression. */
4742 name
= (cp_parser_id_expression
4744 cp_parser_optional_template_keyword (parser
),
4745 /*check_dependency_p=*/true,
4747 /*declarator_p=*/false,
4748 /*optional_p=*/false));
4749 /* In general, build a SCOPE_REF if the member name is qualified.
4750 However, if the name was not dependent and has already been
4751 resolved; there is no need to build the SCOPE_REF. For example;
4753 struct X { void f(); };
4754 template <typename T> void f(T* t) { t->X::f(); }
4756 Even though "t" is dependent, "X::f" is not and has been resolved
4757 to a BASELINK; there is no need to include scope information. */
4759 /* But we do need to remember that there was an explicit scope for
4760 virtual function calls. */
4762 *idk
= CP_ID_KIND_QUALIFIED
;
4764 /* If the name is a template-id that names a type, we will get a
4765 TYPE_DECL here. That is invalid code. */
4766 if (TREE_CODE (name
) == TYPE_DECL
)
4768 error ("invalid use of %qD", name
);
4769 postfix_expression
= error_mark_node
;
4773 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
4775 name
= build_qualified_name (/*type=*/NULL_TREE
,
4779 parser
->scope
= NULL_TREE
;
4780 parser
->qualifying_scope
= NULL_TREE
;
4781 parser
->object_scope
= NULL_TREE
;
4783 if (scope
&& name
&& BASELINK_P (name
))
4784 adjust_result_of_qualified_name_lookup
4785 (name
, BINFO_TYPE (BASELINK_ACCESS_BINFO (name
)), scope
);
4787 = finish_class_member_access_expr (postfix_expression
, name
,
4792 /* We no longer need to look up names in the scope of the object on
4793 the left-hand side of the `.' or `->' operator. */
4794 parser
->context
->object_type
= NULL_TREE
;
4796 /* Outside of offsetof, these operators may not appear in
4797 constant-expressions. */
4799 && (cp_parser_non_integral_constant_expression
4800 (parser
, token_type
== CPP_DEREF
? "'->'" : "`.'")))
4801 postfix_expression
= error_mark_node
;
4803 return postfix_expression
;
4806 /* Parse a parenthesized expression-list.
4809 assignment-expression
4810 expression-list, assignment-expression
4815 identifier, expression-list
4817 CAST_P is true if this expression is the target of a cast.
4819 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4822 Returns a TREE_LIST. The TREE_VALUE of each node is a
4823 representation of an assignment-expression. Note that a TREE_LIST
4824 is returned even if there is only a single expression in the list.
4825 error_mark_node is returned if the ( and or ) are
4826 missing. NULL_TREE is returned on no expressions. The parentheses
4827 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4828 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4829 indicates whether or not all of the expressions in the list were
4833 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
4834 bool is_attribute_list
,
4836 bool allow_expansion_p
,
4837 bool *non_constant_p
)
4839 tree expression_list
= NULL_TREE
;
4840 bool fold_expr_p
= is_attribute_list
;
4841 tree identifier
= NULL_TREE
;
4843 /* Assume all the expressions will be constant. */
4845 *non_constant_p
= false;
4847 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
4848 return error_mark_node
;
4850 /* Consume expressions until there are no more. */
4851 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
4856 /* At the beginning of attribute lists, check to see if the
4857 next token is an identifier. */
4858 if (is_attribute_list
4859 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
4863 /* Consume the identifier. */
4864 token
= cp_lexer_consume_token (parser
->lexer
);
4865 /* Save the identifier. */
4866 identifier
= token
->u
.value
;
4870 /* Parse the next assignment-expression. */
4873 bool expr_non_constant_p
;
4874 expr
= (cp_parser_constant_expression
4875 (parser
, /*allow_non_constant_p=*/true,
4876 &expr_non_constant_p
));
4877 if (expr_non_constant_p
)
4878 *non_constant_p
= true;
4881 expr
= cp_parser_assignment_expression (parser
, cast_p
);
4884 expr
= fold_non_dependent_expr (expr
);
4886 /* If we have an ellipsis, then this is an expression
4888 if (allow_expansion_p
4889 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4891 /* Consume the `...'. */
4892 cp_lexer_consume_token (parser
->lexer
);
4894 /* Build the argument pack. */
4895 expr
= make_pack_expansion (expr
);
4898 /* Add it to the list. We add error_mark_node
4899 expressions to the list, so that we can still tell if
4900 the correct form for a parenthesized expression-list
4901 is found. That gives better errors. */
4902 expression_list
= tree_cons (NULL_TREE
, expr
, expression_list
);
4904 if (expr
== error_mark_node
)
4908 /* After the first item, attribute lists look the same as
4909 expression lists. */
4910 is_attribute_list
= false;
4913 /* If the next token isn't a `,', then we are done. */
4914 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
4917 /* Otherwise, consume the `,' and keep going. */
4918 cp_lexer_consume_token (parser
->lexer
);
4921 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
4926 /* We try and resync to an unnested comma, as that will give the
4927 user better diagnostics. */
4928 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
4929 /*recovering=*/true,
4931 /*consume_paren=*/true);
4935 return error_mark_node
;
4938 /* We built up the list in reverse order so we must reverse it now. */
4939 expression_list
= nreverse (expression_list
);
4941 expression_list
= tree_cons (NULL_TREE
, identifier
, expression_list
);
4943 return expression_list
;
4946 /* Parse a pseudo-destructor-name.
4948 pseudo-destructor-name:
4949 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4950 :: [opt] nested-name-specifier template template-id :: ~ type-name
4951 :: [opt] nested-name-specifier [opt] ~ type-name
4953 If either of the first two productions is used, sets *SCOPE to the
4954 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4955 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4956 or ERROR_MARK_NODE if the parse fails. */
4959 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
4963 bool nested_name_specifier_p
;
4965 /* Assume that things will not work out. */
4966 *type
= error_mark_node
;
4968 /* Look for the optional `::' operator. */
4969 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
4970 /* Look for the optional nested-name-specifier. */
4971 nested_name_specifier_p
4972 = (cp_parser_nested_name_specifier_opt (parser
,
4973 /*typename_keyword_p=*/false,
4974 /*check_dependency_p=*/true,
4976 /*is_declaration=*/true)
4978 /* Now, if we saw a nested-name-specifier, we might be doing the
4979 second production. */
4980 if (nested_name_specifier_p
4981 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
4983 /* Consume the `template' keyword. */
4984 cp_lexer_consume_token (parser
->lexer
);
4985 /* Parse the template-id. */
4986 cp_parser_template_id (parser
,
4987 /*template_keyword_p=*/true,
4988 /*check_dependency_p=*/false,
4989 /*is_declaration=*/true);
4990 /* Look for the `::' token. */
4991 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
4993 /* If the next token is not a `~', then there might be some
4994 additional qualification. */
4995 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
4997 /* Look for the type-name. */
4998 *scope
= TREE_TYPE (cp_parser_type_name (parser
));
5000 if (*scope
== error_mark_node
)
5003 /* If we don't have ::~, then something has gone wrong. Since
5004 the only caller of this function is looking for something
5005 after `.' or `->' after a scalar type, most likely the
5006 program is trying to get a member of a non-aggregate
5008 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
)
5009 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_COMPL
)
5011 cp_parser_error (parser
, "request for member of non-aggregate type");
5015 /* Look for the `::' token. */
5016 cp_parser_require (parser
, CPP_SCOPE
, "`::'");
5021 /* Look for the `~'. */
5022 cp_parser_require (parser
, CPP_COMPL
, "`~'");
5023 /* Look for the type-name again. We are not responsible for
5024 checking that it matches the first type-name. */
5025 *type
= cp_parser_type_name (parser
);
5028 /* Parse a unary-expression.
5034 unary-operator cast-expression
5035 sizeof unary-expression
5043 __extension__ cast-expression
5044 __alignof__ unary-expression
5045 __alignof__ ( type-id )
5046 __real__ cast-expression
5047 __imag__ cast-expression
5050 ADDRESS_P is true iff the unary-expression is appearing as the
5051 operand of the `&' operator. CAST_P is true if this expression is
5052 the target of a cast.
5054 Returns a representation of the expression. */
5057 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5060 enum tree_code unary_operator
;
5062 /* Peek at the next token. */
5063 token
= cp_lexer_peek_token (parser
->lexer
);
5064 /* Some keywords give away the kind of expression. */
5065 if (token
->type
== CPP_KEYWORD
)
5067 enum rid keyword
= token
->keyword
;
5077 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
5078 /* Consume the token. */
5079 cp_lexer_consume_token (parser
->lexer
);
5080 /* Parse the operand. */
5081 operand
= cp_parser_sizeof_operand (parser
, keyword
);
5083 if (TYPE_P (operand
))
5084 return cxx_sizeof_or_alignof_type (operand
, op
, true);
5086 return cxx_sizeof_or_alignof_expr (operand
, op
);
5090 return cp_parser_new_expression (parser
);
5093 return cp_parser_delete_expression (parser
);
5097 /* The saved value of the PEDANTIC flag. */
5101 /* Save away the PEDANTIC flag. */
5102 cp_parser_extension_opt (parser
, &saved_pedantic
);
5103 /* Parse the cast-expression. */
5104 expr
= cp_parser_simple_cast_expression (parser
);
5105 /* Restore the PEDANTIC flag. */
5106 pedantic
= saved_pedantic
;
5116 /* Consume the `__real__' or `__imag__' token. */
5117 cp_lexer_consume_token (parser
->lexer
);
5118 /* Parse the cast-expression. */
5119 expression
= cp_parser_simple_cast_expression (parser
);
5120 /* Create the complete representation. */
5121 return build_x_unary_op ((keyword
== RID_REALPART
5122 ? REALPART_EXPR
: IMAGPART_EXPR
),
5132 /* Look for the `:: new' and `:: delete', which also signal the
5133 beginning of a new-expression, or delete-expression,
5134 respectively. If the next token is `::', then it might be one of
5136 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
5140 /* See if the token after the `::' is one of the keywords in
5141 which we're interested. */
5142 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
5143 /* If it's `new', we have a new-expression. */
5144 if (keyword
== RID_NEW
)
5145 return cp_parser_new_expression (parser
);
5146 /* Similarly, for `delete'. */
5147 else if (keyword
== RID_DELETE
)
5148 return cp_parser_delete_expression (parser
);
5151 /* Look for a unary operator. */
5152 unary_operator
= cp_parser_unary_operator (token
);
5153 /* The `++' and `--' operators can be handled similarly, even though
5154 they are not technically unary-operators in the grammar. */
5155 if (unary_operator
== ERROR_MARK
)
5157 if (token
->type
== CPP_PLUS_PLUS
)
5158 unary_operator
= PREINCREMENT_EXPR
;
5159 else if (token
->type
== CPP_MINUS_MINUS
)
5160 unary_operator
= PREDECREMENT_EXPR
;
5161 /* Handle the GNU address-of-label extension. */
5162 else if (cp_parser_allow_gnu_extensions_p (parser
)
5163 && token
->type
== CPP_AND_AND
)
5167 /* Consume the '&&' token. */
5168 cp_lexer_consume_token (parser
->lexer
);
5169 /* Look for the identifier. */
5170 identifier
= cp_parser_identifier (parser
);
5171 /* Create an expression representing the address. */
5172 return finish_label_address_expr (identifier
);
5175 if (unary_operator
!= ERROR_MARK
)
5177 tree cast_expression
;
5178 tree expression
= error_mark_node
;
5179 const char *non_constant_p
= NULL
;
5181 /* Consume the operator token. */
5182 token
= cp_lexer_consume_token (parser
->lexer
);
5183 /* Parse the cast-expression. */
5185 = cp_parser_cast_expression (parser
,
5186 unary_operator
== ADDR_EXPR
,
5188 /* Now, build an appropriate representation. */
5189 switch (unary_operator
)
5192 non_constant_p
= "`*'";
5193 expression
= build_x_indirect_ref (cast_expression
, "unary *");
5197 non_constant_p
= "`&'";
5200 expression
= build_x_unary_op (unary_operator
, cast_expression
);
5203 case PREINCREMENT_EXPR
:
5204 case PREDECREMENT_EXPR
:
5205 non_constant_p
= (unary_operator
== PREINCREMENT_EXPR
5208 case UNARY_PLUS_EXPR
:
5210 case TRUTH_NOT_EXPR
:
5211 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
5219 && cp_parser_non_integral_constant_expression (parser
,
5221 expression
= error_mark_node
;
5226 return cp_parser_postfix_expression (parser
, address_p
, cast_p
);
5229 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5230 unary-operator, the corresponding tree code is returned. */
5232 static enum tree_code
5233 cp_parser_unary_operator (cp_token
* token
)
5235 switch (token
->type
)
5238 return INDIRECT_REF
;
5244 return UNARY_PLUS_EXPR
;
5250 return TRUTH_NOT_EXPR
;
5253 return BIT_NOT_EXPR
;
5260 /* Parse a new-expression.
5263 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5264 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5266 Returns a representation of the expression. */
5269 cp_parser_new_expression (cp_parser
* parser
)
5271 bool global_scope_p
;
5277 /* Look for the optional `::' operator. */
5279 = (cp_parser_global_scope_opt (parser
,
5280 /*current_scope_valid_p=*/false)
5282 /* Look for the `new' operator. */
5283 cp_parser_require_keyword (parser
, RID_NEW
, "`new'");
5284 /* There's no easy way to tell a new-placement from the
5285 `( type-id )' construct. */
5286 cp_parser_parse_tentatively (parser
);
5287 /* Look for a new-placement. */
5288 placement
= cp_parser_new_placement (parser
);
5289 /* If that didn't work out, there's no new-placement. */
5290 if (!cp_parser_parse_definitely (parser
))
5291 placement
= NULL_TREE
;
5293 /* If the next token is a `(', then we have a parenthesized
5295 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5297 /* Consume the `('. */
5298 cp_lexer_consume_token (parser
->lexer
);
5299 /* Parse the type-id. */
5300 type
= cp_parser_type_id (parser
);
5301 /* Look for the closing `)'. */
5302 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5303 /* There should not be a direct-new-declarator in this production,
5304 but GCC used to allowed this, so we check and emit a sensible error
5305 message for this case. */
5306 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5308 error ("array bound forbidden after parenthesized type-id");
5309 inform ("try removing the parentheses around the type-id");
5310 cp_parser_direct_new_declarator (parser
);
5314 /* Otherwise, there must be a new-type-id. */
5316 type
= cp_parser_new_type_id (parser
, &nelts
);
5318 /* If the next token is a `(', then we have a new-initializer. */
5319 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5320 initializer
= cp_parser_new_initializer (parser
);
5322 initializer
= NULL_TREE
;
5324 /* A new-expression may not appear in an integral constant
5326 if (cp_parser_non_integral_constant_expression (parser
, "`new'"))
5327 return error_mark_node
;
5329 /* Create a representation of the new-expression. */
5330 return build_new (placement
, type
, nelts
, initializer
, global_scope_p
);
5333 /* Parse a new-placement.
5338 Returns the same representation as for an expression-list. */
5341 cp_parser_new_placement (cp_parser
* parser
)
5343 tree expression_list
;
5345 /* Parse the expression-list. */
5346 expression_list
= (cp_parser_parenthesized_expression_list
5347 (parser
, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5348 /*non_constant_p=*/NULL
));
5350 return expression_list
;
5353 /* Parse a new-type-id.
5356 type-specifier-seq new-declarator [opt]
5358 Returns the TYPE allocated. If the new-type-id indicates an array
5359 type, *NELTS is set to the number of elements in the last array
5360 bound; the TYPE will not include the last array bound. */
5363 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
5365 cp_decl_specifier_seq type_specifier_seq
;
5366 cp_declarator
*new_declarator
;
5367 cp_declarator
*declarator
;
5368 cp_declarator
*outer_declarator
;
5369 const char *saved_message
;
5372 /* The type-specifier sequence must not contain type definitions.
5373 (It cannot contain declarations of new types either, but if they
5374 are not definitions we will catch that because they are not
5376 saved_message
= parser
->type_definition_forbidden_message
;
5377 parser
->type_definition_forbidden_message
5378 = "types may not be defined in a new-type-id";
5379 /* Parse the type-specifier-seq. */
5380 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
5381 &type_specifier_seq
);
5382 /* Restore the old message. */
5383 parser
->type_definition_forbidden_message
= saved_message
;
5384 /* Parse the new-declarator. */
5385 new_declarator
= cp_parser_new_declarator_opt (parser
);
5387 /* Determine the number of elements in the last array dimension, if
5390 /* Skip down to the last array dimension. */
5391 declarator
= new_declarator
;
5392 outer_declarator
= NULL
;
5393 while (declarator
&& (declarator
->kind
== cdk_pointer
5394 || declarator
->kind
== cdk_ptrmem
))
5396 outer_declarator
= declarator
;
5397 declarator
= declarator
->declarator
;
5400 && declarator
->kind
== cdk_array
5401 && declarator
->declarator
5402 && declarator
->declarator
->kind
== cdk_array
)
5404 outer_declarator
= declarator
;
5405 declarator
= declarator
->declarator
;
5408 if (declarator
&& declarator
->kind
== cdk_array
)
5410 *nelts
= declarator
->u
.array
.bounds
;
5411 if (*nelts
== error_mark_node
)
5412 *nelts
= integer_one_node
;
5414 if (outer_declarator
)
5415 outer_declarator
->declarator
= declarator
->declarator
;
5417 new_declarator
= NULL
;
5420 type
= groktypename (&type_specifier_seq
, new_declarator
);
5421 if (TREE_CODE (type
) == ARRAY_TYPE
&& *nelts
== NULL_TREE
)
5423 *nelts
= array_type_nelts_top (type
);
5424 type
= TREE_TYPE (type
);
5429 /* Parse an (optional) new-declarator.
5432 ptr-operator new-declarator [opt]
5433 direct-new-declarator
5435 Returns the declarator. */
5437 static cp_declarator
*
5438 cp_parser_new_declarator_opt (cp_parser
* parser
)
5440 enum tree_code code
;
5442 cp_cv_quals cv_quals
;
5444 /* We don't know if there's a ptr-operator next, or not. */
5445 cp_parser_parse_tentatively (parser
);
5446 /* Look for a ptr-operator. */
5447 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
5448 /* If that worked, look for more new-declarators. */
5449 if (cp_parser_parse_definitely (parser
))
5451 cp_declarator
*declarator
;
5453 /* Parse another optional declarator. */
5454 declarator
= cp_parser_new_declarator_opt (parser
);
5456 /* Create the representation of the declarator. */
5458 declarator
= make_ptrmem_declarator (cv_quals
, type
, declarator
);
5459 else if (code
== INDIRECT_REF
)
5460 declarator
= make_pointer_declarator (cv_quals
, declarator
);
5462 declarator
= make_reference_declarator (cv_quals
, declarator
);
5467 /* If the next token is a `[', there is a direct-new-declarator. */
5468 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5469 return cp_parser_direct_new_declarator (parser
);
5474 /* Parse a direct-new-declarator.
5476 direct-new-declarator:
5478 direct-new-declarator [constant-expression]
5482 static cp_declarator
*
5483 cp_parser_direct_new_declarator (cp_parser
* parser
)
5485 cp_declarator
*declarator
= NULL
;
5491 /* Look for the opening `['. */
5492 cp_parser_require (parser
, CPP_OPEN_SQUARE
, "`['");
5493 /* The first expression is not required to be constant. */
5496 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
5497 /* The standard requires that the expression have integral
5498 type. DR 74 adds enumeration types. We believe that the
5499 real intent is that these expressions be handled like the
5500 expression in a `switch' condition, which also allows
5501 classes with a single conversion to integral or
5502 enumeration type. */
5503 if (!processing_template_decl
)
5506 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
5511 error ("expression in new-declarator must have integral "
5512 "or enumeration type");
5513 expression
= error_mark_node
;
5517 /* But all the other expressions must be. */
5520 = cp_parser_constant_expression (parser
,
5521 /*allow_non_constant=*/false,
5523 /* Look for the closing `]'. */
5524 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5526 /* Add this bound to the declarator. */
5527 declarator
= make_array_declarator (declarator
, expression
);
5529 /* If the next token is not a `[', then there are no more
5531 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
5538 /* Parse a new-initializer.
5541 ( expression-list [opt] )
5543 Returns a representation of the expression-list. If there is no
5544 expression-list, VOID_ZERO_NODE is returned. */
5547 cp_parser_new_initializer (cp_parser
* parser
)
5549 tree expression_list
;
5551 expression_list
= (cp_parser_parenthesized_expression_list
5552 (parser
, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5553 /*non_constant_p=*/NULL
));
5554 if (!expression_list
)
5555 expression_list
= void_zero_node
;
5557 return expression_list
;
5560 /* Parse a delete-expression.
5563 :: [opt] delete cast-expression
5564 :: [opt] delete [ ] cast-expression
5566 Returns a representation of the expression. */
5569 cp_parser_delete_expression (cp_parser
* parser
)
5571 bool global_scope_p
;
5575 /* Look for the optional `::' operator. */
5577 = (cp_parser_global_scope_opt (parser
,
5578 /*current_scope_valid_p=*/false)
5580 /* Look for the `delete' keyword. */
5581 cp_parser_require_keyword (parser
, RID_DELETE
, "`delete'");
5582 /* See if the array syntax is in use. */
5583 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
5585 /* Consume the `[' token. */
5586 cp_lexer_consume_token (parser
->lexer
);
5587 /* Look for the `]' token. */
5588 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
5589 /* Remember that this is the `[]' construct. */
5595 /* Parse the cast-expression. */
5596 expression
= cp_parser_simple_cast_expression (parser
);
5598 /* A delete-expression may not appear in an integral constant
5600 if (cp_parser_non_integral_constant_expression (parser
, "`delete'"))
5601 return error_mark_node
;
5603 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
5606 /* Parse a cast-expression.
5610 ( type-id ) cast-expression
5612 ADDRESS_P is true iff the unary-expression is appearing as the
5613 operand of the `&' operator. CAST_P is true if this expression is
5614 the target of a cast.
5616 Returns a representation of the expression. */
5619 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
)
5621 /* If it's a `(', then we might be looking at a cast. */
5622 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5624 tree type
= NULL_TREE
;
5625 tree expr
= NULL_TREE
;
5626 bool compound_literal_p
;
5627 const char *saved_message
;
5629 /* There's no way to know yet whether or not this is a cast.
5630 For example, `(int (3))' is a unary-expression, while `(int)
5631 3' is a cast. So, we resort to parsing tentatively. */
5632 cp_parser_parse_tentatively (parser
);
5633 /* Types may not be defined in a cast. */
5634 saved_message
= parser
->type_definition_forbidden_message
;
5635 parser
->type_definition_forbidden_message
5636 = "types may not be defined in casts";
5637 /* Consume the `('. */
5638 cp_lexer_consume_token (parser
->lexer
);
5639 /* A very tricky bit is that `(struct S) { 3 }' is a
5640 compound-literal (which we permit in C++ as an extension).
5641 But, that construct is not a cast-expression -- it is a
5642 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5643 is legal; if the compound-literal were a cast-expression,
5644 you'd need an extra set of parentheses.) But, if we parse
5645 the type-id, and it happens to be a class-specifier, then we
5646 will commit to the parse at that point, because we cannot
5647 undo the action that is done when creating a new class. So,
5648 then we cannot back up and do a postfix-expression.
5650 Therefore, we scan ahead to the closing `)', and check to see
5651 if the token after the `)' is a `{'. If so, we are not
5652 looking at a cast-expression.
5654 Save tokens so that we can put them back. */
5655 cp_lexer_save_tokens (parser
->lexer
);
5656 /* Skip tokens until the next token is a closing parenthesis.
5657 If we find the closing `)', and the next token is a `{', then
5658 we are looking at a compound-literal. */
5660 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5661 /*consume_paren=*/true)
5662 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5663 /* Roll back the tokens we skipped. */
5664 cp_lexer_rollback_tokens (parser
->lexer
);
5665 /* If we were looking at a compound-literal, simulate an error
5666 so that the call to cp_parser_parse_definitely below will
5668 if (compound_literal_p
)
5669 cp_parser_simulate_error (parser
);
5672 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5673 parser
->in_type_id_in_expr_p
= true;
5674 /* Look for the type-id. */
5675 type
= cp_parser_type_id (parser
);
5676 /* Look for the closing `)'. */
5677 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
5678 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5681 /* Restore the saved message. */
5682 parser
->type_definition_forbidden_message
= saved_message
;
5684 /* If ok so far, parse the dependent expression. We cannot be
5685 sure it is a cast. Consider `(T ())'. It is a parenthesized
5686 ctor of T, but looks like a cast to function returning T
5687 without a dependent expression. */
5688 if (!cp_parser_error_occurred (parser
))
5689 expr
= cp_parser_cast_expression (parser
,
5690 /*address_p=*/false,
5693 if (cp_parser_parse_definitely (parser
))
5695 /* Warn about old-style casts, if so requested. */
5696 if (warn_old_style_cast
5697 && !in_system_header
5698 && !VOID_TYPE_P (type
)
5699 && current_lang_name
!= lang_name_c
)
5700 warning (OPT_Wold_style_cast
, "use of old-style cast");
5702 /* Only type conversions to integral or enumeration types
5703 can be used in constant-expressions. */
5704 if (!cast_valid_in_integral_constant_expression_p (type
)
5705 && (cp_parser_non_integral_constant_expression
5707 "a cast to a type other than an integral or "
5708 "enumeration type")))
5709 return error_mark_node
;
5711 /* Perform the cast. */
5712 expr
= build_c_cast (type
, expr
);
5717 /* If we get here, then it's not a cast, so it must be a
5718 unary-expression. */
5719 return cp_parser_unary_expression (parser
, address_p
, cast_p
);
5722 /* Parse a binary expression of the general form:
5726 pm-expression .* cast-expression
5727 pm-expression ->* cast-expression
5729 multiplicative-expression:
5731 multiplicative-expression * pm-expression
5732 multiplicative-expression / pm-expression
5733 multiplicative-expression % pm-expression
5735 additive-expression:
5736 multiplicative-expression
5737 additive-expression + multiplicative-expression
5738 additive-expression - multiplicative-expression
5742 shift-expression << additive-expression
5743 shift-expression >> additive-expression
5745 relational-expression:
5747 relational-expression < shift-expression
5748 relational-expression > shift-expression
5749 relational-expression <= shift-expression
5750 relational-expression >= shift-expression
5754 relational-expression:
5755 relational-expression <? shift-expression
5756 relational-expression >? shift-expression
5758 equality-expression:
5759 relational-expression
5760 equality-expression == relational-expression
5761 equality-expression != relational-expression
5765 and-expression & equality-expression
5767 exclusive-or-expression:
5769 exclusive-or-expression ^ and-expression
5771 inclusive-or-expression:
5772 exclusive-or-expression
5773 inclusive-or-expression | exclusive-or-expression
5775 logical-and-expression:
5776 inclusive-or-expression
5777 logical-and-expression && inclusive-or-expression
5779 logical-or-expression:
5780 logical-and-expression
5781 logical-or-expression || logical-and-expression
5783 All these are implemented with a single function like:
5786 simple-cast-expression
5787 binary-expression <token> binary-expression
5789 CAST_P is true if this expression is the target of a cast.
5791 The binops_by_token map is used to get the tree codes for each <token> type.
5792 binary-expressions are associated according to a precedence table. */
5794 #define TOKEN_PRECEDENCE(token) \
5795 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5796 ? PREC_NOT_OPERATOR \
5797 : binops_by_token[token->type].prec)
5800 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
)
5802 cp_parser_expression_stack stack
;
5803 cp_parser_expression_stack_entry
*sp
= &stack
[0];
5806 enum tree_code tree_type
, lhs_type
, rhs_type
;
5807 enum cp_parser_prec prec
= PREC_NOT_OPERATOR
, new_prec
, lookahead_prec
;
5810 /* Parse the first expression. */
5811 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
);
5812 lhs_type
= ERROR_MARK
;
5816 /* Get an operator token. */
5817 token
= cp_lexer_peek_token (parser
->lexer
);
5819 new_prec
= TOKEN_PRECEDENCE (token
);
5821 /* Popping an entry off the stack means we completed a subexpression:
5822 - either we found a token which is not an operator (`>' where it is not
5823 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5824 will happen repeatedly;
5825 - or, we found an operator which has lower priority. This is the case
5826 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5828 if (new_prec
<= prec
)
5837 tree_type
= binops_by_token
[token
->type
].tree_type
;
5839 /* We used the operator token. */
5840 cp_lexer_consume_token (parser
->lexer
);
5842 /* Extract another operand. It may be the RHS of this expression
5843 or the LHS of a new, higher priority expression. */
5844 rhs
= cp_parser_simple_cast_expression (parser
);
5845 rhs_type
= ERROR_MARK
;
5847 /* Get another operator token. Look up its precedence to avoid
5848 building a useless (immediately popped) stack entry for common
5849 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5850 token
= cp_lexer_peek_token (parser
->lexer
);
5851 lookahead_prec
= TOKEN_PRECEDENCE (token
);
5852 if (lookahead_prec
> new_prec
)
5854 /* ... and prepare to parse the RHS of the new, higher priority
5855 expression. Since precedence levels on the stack are
5856 monotonically increasing, we do not have to care about
5859 sp
->tree_type
= tree_type
;
5861 sp
->lhs_type
= lhs_type
;
5864 lhs_type
= rhs_type
;
5866 new_prec
= lookahead_prec
;
5870 /* If the stack is not empty, we have parsed into LHS the right side
5871 (`4' in the example above) of an expression we had suspended.
5872 We can use the information on the stack to recover the LHS (`3')
5873 from the stack together with the tree code (`MULT_EXPR'), and
5874 the precedence of the higher level subexpression
5875 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5876 which will be used to actually build the additive expression. */
5879 tree_type
= sp
->tree_type
;
5881 rhs_type
= lhs_type
;
5883 lhs_type
= sp
->lhs_type
;
5886 overloaded_p
= false;
5887 lhs
= build_x_binary_op (tree_type
, lhs
, lhs_type
, rhs
, rhs_type
,
5889 lhs_type
= tree_type
;
5891 /* If the binary operator required the use of an overloaded operator,
5892 then this expression cannot be an integral constant-expression.
5893 An overloaded operator can be used even if both operands are
5894 otherwise permissible in an integral constant-expression if at
5895 least one of the operands is of enumeration type. */
5898 && (cp_parser_non_integral_constant_expression
5899 (parser
, "calls to overloaded operators")))
5900 return error_mark_node
;
5907 /* Parse the `? expression : assignment-expression' part of a
5908 conditional-expression. The LOGICAL_OR_EXPR is the
5909 logical-or-expression that started the conditional-expression.
5910 Returns a representation of the entire conditional-expression.
5912 This routine is used by cp_parser_assignment_expression.
5914 ? expression : assignment-expression
5918 ? : assignment-expression */
5921 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
5924 tree assignment_expr
;
5926 /* Consume the `?' token. */
5927 cp_lexer_consume_token (parser
->lexer
);
5928 if (cp_parser_allow_gnu_extensions_p (parser
)
5929 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
5930 /* Implicit true clause. */
5933 /* Parse the expression. */
5934 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
5936 /* The next token should be a `:'. */
5937 cp_parser_require (parser
, CPP_COLON
, "`:'");
5938 /* Parse the assignment-expression. */
5939 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
5941 /* Build the conditional-expression. */
5942 return build_x_conditional_expr (logical_or_expr
,
5947 /* Parse an assignment-expression.
5949 assignment-expression:
5950 conditional-expression
5951 logical-or-expression assignment-operator assignment_expression
5954 CAST_P is true if this expression is the target of a cast.
5956 Returns a representation for the expression. */
5959 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
)
5963 /* If the next token is the `throw' keyword, then we're looking at
5964 a throw-expression. */
5965 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
5966 expr
= cp_parser_throw_expression (parser
);
5967 /* Otherwise, it must be that we are looking at a
5968 logical-or-expression. */
5971 /* Parse the binary expressions (logical-or-expression). */
5972 expr
= cp_parser_binary_expression (parser
, cast_p
);
5973 /* If the next token is a `?' then we're actually looking at a
5974 conditional-expression. */
5975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
5976 return cp_parser_question_colon_clause (parser
, expr
);
5979 enum tree_code assignment_operator
;
5981 /* If it's an assignment-operator, we're using the second
5984 = cp_parser_assignment_operator_opt (parser
);
5985 if (assignment_operator
!= ERROR_MARK
)
5989 /* Parse the right-hand side of the assignment. */
5990 rhs
= cp_parser_assignment_expression (parser
, cast_p
);
5991 /* An assignment may not appear in a
5992 constant-expression. */
5993 if (cp_parser_non_integral_constant_expression (parser
,
5995 return error_mark_node
;
5996 /* Build the assignment expression. */
5997 expr
= build_x_modify_expr (expr
,
5998 assignment_operator
,
6007 /* Parse an (optional) assignment-operator.
6009 assignment-operator: one of
6010 = *= /= %= += -= >>= <<= &= ^= |=
6014 assignment-operator: one of
6017 If the next token is an assignment operator, the corresponding tree
6018 code is returned, and the token is consumed. For example, for
6019 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6020 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6021 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6022 operator, ERROR_MARK is returned. */
6024 static enum tree_code
6025 cp_parser_assignment_operator_opt (cp_parser
* parser
)
6030 /* Peek at the next toen. */
6031 token
= cp_lexer_peek_token (parser
->lexer
);
6033 switch (token
->type
)
6044 op
= TRUNC_DIV_EXPR
;
6048 op
= TRUNC_MOD_EXPR
;
6080 /* Nothing else is an assignment operator. */
6084 /* If it was an assignment operator, consume it. */
6085 if (op
!= ERROR_MARK
)
6086 cp_lexer_consume_token (parser
->lexer
);
6091 /* Parse an expression.
6094 assignment-expression
6095 expression , assignment-expression
6097 CAST_P is true if this expression is the target of a cast.
6099 Returns a representation of the expression. */
6102 cp_parser_expression (cp_parser
* parser
, bool cast_p
)
6104 tree expression
= NULL_TREE
;
6108 tree assignment_expression
;
6110 /* Parse the next assignment-expression. */
6111 assignment_expression
6112 = cp_parser_assignment_expression (parser
, cast_p
);
6113 /* If this is the first assignment-expression, we can just
6116 expression
= assignment_expression
;
6118 expression
= build_x_compound_expr (expression
,
6119 assignment_expression
);
6120 /* If the next token is not a comma, then we are done with the
6122 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6124 /* Consume the `,'. */
6125 cp_lexer_consume_token (parser
->lexer
);
6126 /* A comma operator cannot appear in a constant-expression. */
6127 if (cp_parser_non_integral_constant_expression (parser
,
6128 "a comma operator"))
6129 expression
= error_mark_node
;
6135 /* Parse a constant-expression.
6137 constant-expression:
6138 conditional-expression
6140 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6141 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6142 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6143 is false, NON_CONSTANT_P should be NULL. */
6146 cp_parser_constant_expression (cp_parser
* parser
,
6147 bool allow_non_constant_p
,
6148 bool *non_constant_p
)
6150 bool saved_integral_constant_expression_p
;
6151 bool saved_allow_non_integral_constant_expression_p
;
6152 bool saved_non_integral_constant_expression_p
;
6155 /* It might seem that we could simply parse the
6156 conditional-expression, and then check to see if it were
6157 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6158 one that the compiler can figure out is constant, possibly after
6159 doing some simplifications or optimizations. The standard has a
6160 precise definition of constant-expression, and we must honor
6161 that, even though it is somewhat more restrictive.
6167 is not a legal declaration, because `(2, 3)' is not a
6168 constant-expression. The `,' operator is forbidden in a
6169 constant-expression. However, GCC's constant-folding machinery
6170 will fold this operation to an INTEGER_CST for `3'. */
6172 /* Save the old settings. */
6173 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
6174 saved_allow_non_integral_constant_expression_p
6175 = parser
->allow_non_integral_constant_expression_p
;
6176 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
6177 /* We are now parsing a constant-expression. */
6178 parser
->integral_constant_expression_p
= true;
6179 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
6180 parser
->non_integral_constant_expression_p
= false;
6181 /* Although the grammar says "conditional-expression", we parse an
6182 "assignment-expression", which also permits "throw-expression"
6183 and the use of assignment operators. In the case that
6184 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6185 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6186 actually essential that we look for an assignment-expression.
6187 For example, cp_parser_initializer_clauses uses this function to
6188 determine whether a particular assignment-expression is in fact
6190 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
6191 /* Restore the old settings. */
6192 parser
->integral_constant_expression_p
6193 = saved_integral_constant_expression_p
;
6194 parser
->allow_non_integral_constant_expression_p
6195 = saved_allow_non_integral_constant_expression_p
;
6196 if (allow_non_constant_p
)
6197 *non_constant_p
= parser
->non_integral_constant_expression_p
;
6198 else if (parser
->non_integral_constant_expression_p
)
6199 expression
= error_mark_node
;
6200 parser
->non_integral_constant_expression_p
6201 = saved_non_integral_constant_expression_p
;
6206 /* Parse __builtin_offsetof.
6208 offsetof-expression:
6209 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6211 offsetof-member-designator:
6213 | offsetof-member-designator "." id-expression
6214 | offsetof-member-designator "[" expression "]" */
6217 cp_parser_builtin_offsetof (cp_parser
*parser
)
6219 int save_ice_p
, save_non_ice_p
;
6223 /* We're about to accept non-integral-constant things, but will
6224 definitely yield an integral constant expression. Save and
6225 restore these values around our local parsing. */
6226 save_ice_p
= parser
->integral_constant_expression_p
;
6227 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
6229 /* Consume the "__builtin_offsetof" token. */
6230 cp_lexer_consume_token (parser
->lexer
);
6231 /* Consume the opening `('. */
6232 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6233 /* Parse the type-id. */
6234 type
= cp_parser_type_id (parser
);
6235 /* Look for the `,'. */
6236 cp_parser_require (parser
, CPP_COMMA
, "`,'");
6238 /* Build the (type *)null that begins the traditional offsetof macro. */
6239 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
);
6241 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6242 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
6246 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6247 switch (token
->type
)
6249 case CPP_OPEN_SQUARE
:
6250 /* offsetof-member-designator "[" expression "]" */
6251 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
6255 /* offsetof-member-designator "." identifier */
6256 cp_lexer_consume_token (parser
->lexer
);
6257 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
, expr
,
6261 case CPP_CLOSE_PAREN
:
6262 /* Consume the ")" token. */
6263 cp_lexer_consume_token (parser
->lexer
);
6267 /* Error. We know the following require will fail, but
6268 that gives the proper error message. */
6269 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6270 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
6271 expr
= error_mark_node
;
6277 /* If we're processing a template, we can't finish the semantics yet.
6278 Otherwise we can fold the entire expression now. */
6279 if (processing_template_decl
)
6280 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
6282 expr
= finish_offsetof (expr
);
6285 parser
->integral_constant_expression_p
= save_ice_p
;
6286 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
6291 /* Statements [gram.stmt.stmt] */
6293 /* Parse a statement.
6297 expression-statement
6302 declaration-statement
6305 IN_COMPOUND is true when the statement is nested inside a
6306 cp_parser_compound_statement; this matters for certain pragmas.
6308 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6309 is a (possibly labeled) if statement which is not enclosed in braces
6310 and has an else clause. This is used to implement -Wparentheses. */
6313 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
6314 bool in_compound
, bool *if_p
)
6318 location_t statement_location
;
6323 /* There is no statement yet. */
6324 statement
= NULL_TREE
;
6325 /* Peek at the next token. */
6326 token
= cp_lexer_peek_token (parser
->lexer
);
6327 /* Remember the location of the first token in the statement. */
6328 statement_location
= token
->location
;
6329 /* If this is a keyword, then that will often determine what kind of
6330 statement we have. */
6331 if (token
->type
== CPP_KEYWORD
)
6333 enum rid keyword
= token
->keyword
;
6339 /* Looks like a labeled-statement with a case label.
6340 Parse the label, and then use tail recursion to parse
6342 cp_parser_label_for_labeled_statement (parser
);
6347 statement
= cp_parser_selection_statement (parser
, if_p
);
6353 statement
= cp_parser_iteration_statement (parser
);
6360 statement
= cp_parser_jump_statement (parser
);
6363 /* Objective-C++ exception-handling constructs. */
6366 case RID_AT_FINALLY
:
6367 case RID_AT_SYNCHRONIZED
:
6369 statement
= cp_parser_objc_statement (parser
);
6373 statement
= cp_parser_try_block (parser
);
6377 /* This must be a namespace alias definition. */
6378 cp_parser_declaration_statement (parser
);
6382 /* It might be a keyword like `int' that can start a
6383 declaration-statement. */
6387 else if (token
->type
== CPP_NAME
)
6389 /* If the next token is a `:', then we are looking at a
6390 labeled-statement. */
6391 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6392 if (token
->type
== CPP_COLON
)
6394 /* Looks like a labeled-statement with an ordinary label.
6395 Parse the label, and then use tail recursion to parse
6397 cp_parser_label_for_labeled_statement (parser
);
6401 /* Anything that starts with a `{' must be a compound-statement. */
6402 else if (token
->type
== CPP_OPEN_BRACE
)
6403 statement
= cp_parser_compound_statement (parser
, NULL
, false);
6404 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6405 a statement all its own. */
6406 else if (token
->type
== CPP_PRAGMA
)
6408 /* Only certain OpenMP pragmas are attached to statements, and thus
6409 are considered statements themselves. All others are not. In
6410 the context of a compound, accept the pragma as a "statement" and
6411 return so that we can check for a close brace. Otherwise we
6412 require a real statement and must go back and read one. */
6414 cp_parser_pragma (parser
, pragma_compound
);
6415 else if (!cp_parser_pragma (parser
, pragma_stmt
))
6419 else if (token
->type
== CPP_EOF
)
6421 cp_parser_error (parser
, "expected statement");
6425 /* Everything else must be a declaration-statement or an
6426 expression-statement. Try for the declaration-statement
6427 first, unless we are looking at a `;', in which case we know that
6428 we have an expression-statement. */
6431 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6433 cp_parser_parse_tentatively (parser
);
6434 /* Try to parse the declaration-statement. */
6435 cp_parser_declaration_statement (parser
);
6436 /* If that worked, we're done. */
6437 if (cp_parser_parse_definitely (parser
))
6440 /* Look for an expression-statement instead. */
6441 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
6444 /* Set the line number for the statement. */
6445 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
6446 SET_EXPR_LOCATION (statement
, statement_location
);
6449 /* Parse the label for a labeled-statement, i.e.
6452 case constant-expression :
6456 case constant-expression ... constant-expression : statement
6458 When a label is parsed without errors, the label is added to the
6459 parse tree by the finish_* functions, so this function doesn't
6460 have to return the label. */
6463 cp_parser_label_for_labeled_statement (cp_parser
* parser
)
6467 /* The next token should be an identifier. */
6468 token
= cp_lexer_peek_token (parser
->lexer
);
6469 if (token
->type
!= CPP_NAME
6470 && token
->type
!= CPP_KEYWORD
)
6472 cp_parser_error (parser
, "expected labeled-statement");
6476 switch (token
->keyword
)
6483 /* Consume the `case' token. */
6484 cp_lexer_consume_token (parser
->lexer
);
6485 /* Parse the constant-expression. */
6486 expr
= cp_parser_constant_expression (parser
,
6487 /*allow_non_constant_p=*/false,
6490 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
6491 if (ellipsis
->type
== CPP_ELLIPSIS
)
6493 /* Consume the `...' token. */
6494 cp_lexer_consume_token (parser
->lexer
);
6496 cp_parser_constant_expression (parser
,
6497 /*allow_non_constant_p=*/false,
6499 /* We don't need to emit warnings here, as the common code
6500 will do this for us. */
6503 expr_hi
= NULL_TREE
;
6505 if (parser
->in_switch_statement_p
)
6506 finish_case_label (expr
, expr_hi
);
6508 error ("case label %qE not within a switch statement", expr
);
6513 /* Consume the `default' token. */
6514 cp_lexer_consume_token (parser
->lexer
);
6516 if (parser
->in_switch_statement_p
)
6517 finish_case_label (NULL_TREE
, NULL_TREE
);
6519 error ("case label not within a switch statement");
6523 /* Anything else must be an ordinary label. */
6524 finish_label_stmt (cp_parser_identifier (parser
));
6528 /* Require the `:' token. */
6529 cp_parser_require (parser
, CPP_COLON
, "`:'");
6532 /* Parse an expression-statement.
6534 expression-statement:
6537 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6538 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6539 indicates whether this expression-statement is part of an
6540 expression statement. */
6543 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
6545 tree statement
= NULL_TREE
;
6547 /* If the next token is a ';', then there is no expression
6549 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6550 statement
= cp_parser_expression (parser
, /*cast_p=*/false);
6552 /* Consume the final `;'. */
6553 cp_parser_consume_semicolon_at_end_of_statement (parser
);
6555 if (in_statement_expr
6556 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
6557 /* This is the final expression statement of a statement
6559 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
6561 statement
= finish_expr_stmt (statement
);
6568 /* Parse a compound-statement.
6571 { statement-seq [opt] }
6573 Returns a tree representing the statement. */
6576 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
6581 /* Consume the `{'. */
6582 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
6583 return error_mark_node
;
6584 /* Begin the compound-statement. */
6585 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
6586 /* Parse an (optional) statement-seq. */
6587 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
6588 /* Finish the compound-statement. */
6589 finish_compound_stmt (compound_stmt
);
6590 /* Consume the `}'. */
6591 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
6593 return compound_stmt
;
6596 /* Parse an (optional) statement-seq.
6600 statement-seq [opt] statement */
6603 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
6605 /* Scan statements until there aren't any more. */
6608 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6610 /* If we're looking at a `}', then we've run out of statements. */
6611 if (token
->type
== CPP_CLOSE_BRACE
6612 || token
->type
== CPP_EOF
6613 || token
->type
== CPP_PRAGMA_EOL
)
6616 /* If we are in a compound statement and find 'else' then
6617 something went wrong. */
6618 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
6620 if (parser
->in_statement
& IN_IF_STMT
)
6624 token
= cp_lexer_consume_token (parser
->lexer
);
6625 error ("%<else%> without a previous %<if%>");
6629 /* Parse the statement. */
6630 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
6634 /* Parse a selection-statement.
6636 selection-statement:
6637 if ( condition ) statement
6638 if ( condition ) statement else statement
6639 switch ( condition ) statement
6641 Returns the new IF_STMT or SWITCH_STMT.
6643 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6644 is a (possibly labeled) if statement which is not enclosed in
6645 braces and has an else clause. This is used to implement
6649 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
6657 /* Peek at the next token. */
6658 token
= cp_parser_require (parser
, CPP_KEYWORD
, "selection-statement");
6660 /* See what kind of keyword it is. */
6661 keyword
= token
->keyword
;
6670 /* Look for the `('. */
6671 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
6673 cp_parser_skip_to_end_of_statement (parser
);
6674 return error_mark_node
;
6677 /* Begin the selection-statement. */
6678 if (keyword
== RID_IF
)
6679 statement
= begin_if_stmt ();
6681 statement
= begin_switch_stmt ();
6683 /* Parse the condition. */
6684 condition
= cp_parser_condition (parser
);
6685 /* Look for the `)'. */
6686 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
6687 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
6688 /*consume_paren=*/true);
6690 if (keyword
== RID_IF
)
6693 unsigned char in_statement
;
6695 /* Add the condition. */
6696 finish_if_stmt_cond (condition
, statement
);
6698 /* Parse the then-clause. */
6699 in_statement
= parser
->in_statement
;
6700 parser
->in_statement
|= IN_IF_STMT
;
6701 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
6702 parser
->in_statement
= in_statement
;
6704 finish_then_clause (statement
);
6706 /* If the next token is `else', parse the else-clause. */
6707 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
6710 /* Consume the `else' keyword. */
6711 cp_lexer_consume_token (parser
->lexer
);
6712 begin_else_clause (statement
);
6713 /* Parse the else-clause. */
6714 cp_parser_implicitly_scoped_statement (parser
, NULL
);
6715 finish_else_clause (statement
);
6717 /* If we are currently parsing a then-clause, then
6718 IF_P will not be NULL. We set it to true to
6719 indicate that this if statement has an else clause.
6720 This may trigger the Wparentheses warning below
6721 when we get back up to the parent if statement. */
6727 /* This if statement does not have an else clause. If
6728 NESTED_IF is true, then the then-clause is an if
6729 statement which does have an else clause. We warn
6730 about the potential ambiguity. */
6732 warning (OPT_Wparentheses
,
6733 ("%Hsuggest explicit braces "
6734 "to avoid ambiguous %<else%>"),
6735 EXPR_LOCUS (statement
));
6738 /* Now we're all done with the if-statement. */
6739 finish_if_stmt (statement
);
6743 bool in_switch_statement_p
;
6744 unsigned char in_statement
;
6746 /* Add the condition. */
6747 finish_switch_cond (condition
, statement
);
6749 /* Parse the body of the switch-statement. */
6750 in_switch_statement_p
= parser
->in_switch_statement_p
;
6751 in_statement
= parser
->in_statement
;
6752 parser
->in_switch_statement_p
= true;
6753 parser
->in_statement
|= IN_SWITCH_STMT
;
6754 cp_parser_implicitly_scoped_statement (parser
, NULL
);
6755 parser
->in_switch_statement_p
= in_switch_statement_p
;
6756 parser
->in_statement
= in_statement
;
6758 /* Now we're all done with the switch-statement. */
6759 finish_switch_stmt (statement
);
6767 cp_parser_error (parser
, "expected selection-statement");
6768 return error_mark_node
;
6772 /* Parse a condition.
6776 type-specifier-seq declarator = assignment-expression
6781 type-specifier-seq declarator asm-specification [opt]
6782 attributes [opt] = assignment-expression
6784 Returns the expression that should be tested. */
6787 cp_parser_condition (cp_parser
* parser
)
6789 cp_decl_specifier_seq type_specifiers
;
6790 const char *saved_message
;
6792 /* Try the declaration first. */
6793 cp_parser_parse_tentatively (parser
);
6794 /* New types are not allowed in the type-specifier-seq for a
6796 saved_message
= parser
->type_definition_forbidden_message
;
6797 parser
->type_definition_forbidden_message
6798 = "types may not be defined in conditions";
6799 /* Parse the type-specifier-seq. */
6800 cp_parser_type_specifier_seq (parser
, /*is_condition==*/true,
6802 /* Restore the saved message. */
6803 parser
->type_definition_forbidden_message
= saved_message
;
6804 /* If all is well, we might be looking at a declaration. */
6805 if (!cp_parser_error_occurred (parser
))
6808 tree asm_specification
;
6810 cp_declarator
*declarator
;
6811 tree initializer
= NULL_TREE
;
6813 /* Parse the declarator. */
6814 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
6815 /*ctor_dtor_or_conv_p=*/NULL
,
6816 /*parenthesized_p=*/NULL
,
6817 /*member_p=*/false);
6818 /* Parse the attributes. */
6819 attributes
= cp_parser_attributes_opt (parser
);
6820 /* Parse the asm-specification. */
6821 asm_specification
= cp_parser_asm_specification_opt (parser
);
6822 /* If the next token is not an `=', then we might still be
6823 looking at an expression. For example:
6827 looks like a decl-specifier-seq and a declarator -- but then
6828 there is no `=', so this is an expression. */
6829 cp_parser_require (parser
, CPP_EQ
, "`='");
6830 /* If we did see an `=', then we are looking at a declaration
6832 if (cp_parser_parse_definitely (parser
))
6835 bool non_constant_p
;
6837 /* Create the declaration. */
6838 decl
= start_decl (declarator
, &type_specifiers
,
6839 /*initialized_p=*/true,
6840 attributes
, /*prefix_attributes=*/NULL_TREE
,
6842 /* Parse the assignment-expression. */
6844 = cp_parser_constant_expression (parser
,
6845 /*allow_non_constant_p=*/true,
6847 if (!non_constant_p
)
6848 initializer
= fold_non_dependent_expr (initializer
);
6850 /* Process the initializer. */
6851 cp_finish_decl (decl
,
6852 initializer
, !non_constant_p
,
6854 LOOKUP_ONLYCONVERTING
);
6857 pop_scope (pushed_scope
);
6859 return convert_from_reference (decl
);
6862 /* If we didn't even get past the declarator successfully, we are
6863 definitely not looking at a declaration. */
6865 cp_parser_abort_tentative_parse (parser
);
6867 /* Otherwise, we are looking at an expression. */
6868 return cp_parser_expression (parser
, /*cast_p=*/false);
6871 /* Parse an iteration-statement.
6873 iteration-statement:
6874 while ( condition ) statement
6875 do statement while ( expression ) ;
6876 for ( for-init-statement condition [opt] ; expression [opt] )
6879 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6882 cp_parser_iteration_statement (cp_parser
* parser
)
6887 unsigned char in_statement
;
6889 /* Peek at the next token. */
6890 token
= cp_parser_require (parser
, CPP_KEYWORD
, "iteration-statement");
6892 return error_mark_node
;
6894 /* Remember whether or not we are already within an iteration
6896 in_statement
= parser
->in_statement
;
6898 /* See what kind of keyword it is. */
6899 keyword
= token
->keyword
;
6906 /* Begin the while-statement. */
6907 statement
= begin_while_stmt ();
6908 /* Look for the `('. */
6909 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6910 /* Parse the condition. */
6911 condition
= cp_parser_condition (parser
);
6912 finish_while_stmt_cond (condition
, statement
);
6913 /* Look for the `)'. */
6914 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6915 /* Parse the dependent statement. */
6916 parser
->in_statement
= IN_ITERATION_STMT
;
6917 cp_parser_already_scoped_statement (parser
);
6918 parser
->in_statement
= in_statement
;
6919 /* We're done with the while-statement. */
6920 finish_while_stmt (statement
);
6928 /* Begin the do-statement. */
6929 statement
= begin_do_stmt ();
6930 /* Parse the body of the do-statement. */
6931 parser
->in_statement
= IN_ITERATION_STMT
;
6932 cp_parser_implicitly_scoped_statement (parser
, NULL
);
6933 parser
->in_statement
= in_statement
;
6934 finish_do_body (statement
);
6935 /* Look for the `while' keyword. */
6936 cp_parser_require_keyword (parser
, RID_WHILE
, "`while'");
6937 /* Look for the `('. */
6938 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6939 /* Parse the expression. */
6940 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6941 /* We're done with the do-statement. */
6942 finish_do_stmt (expression
, statement
);
6943 /* Look for the `)'. */
6944 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6945 /* Look for the `;'. */
6946 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6952 tree condition
= NULL_TREE
;
6953 tree expression
= NULL_TREE
;
6955 /* Begin the for-statement. */
6956 statement
= begin_for_stmt ();
6957 /* Look for the `('. */
6958 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
6959 /* Parse the initialization. */
6960 cp_parser_for_init_statement (parser
);
6961 finish_for_init_stmt (statement
);
6963 /* If there's a condition, process it. */
6964 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
6965 condition
= cp_parser_condition (parser
);
6966 finish_for_cond (condition
, statement
);
6967 /* Look for the `;'. */
6968 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
6970 /* If there's an expression, process it. */
6971 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6972 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
6973 finish_for_expr (expression
, statement
);
6974 /* Look for the `)'. */
6975 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
6977 /* Parse the body of the for-statement. */
6978 parser
->in_statement
= IN_ITERATION_STMT
;
6979 cp_parser_already_scoped_statement (parser
);
6980 parser
->in_statement
= in_statement
;
6982 /* We're done with the for-statement. */
6983 finish_for_stmt (statement
);
6988 cp_parser_error (parser
, "expected iteration-statement");
6989 statement
= error_mark_node
;
6996 /* Parse a for-init-statement.
6999 expression-statement
7000 simple-declaration */
7003 cp_parser_for_init_statement (cp_parser
* parser
)
7005 /* If the next token is a `;', then we have an empty
7006 expression-statement. Grammatically, this is also a
7007 simple-declaration, but an invalid one, because it does not
7008 declare anything. Therefore, if we did not handle this case
7009 specially, we would issue an error message about an invalid
7011 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7013 /* We're going to speculatively look for a declaration, falling back
7014 to an expression, if necessary. */
7015 cp_parser_parse_tentatively (parser
);
7016 /* Parse the declaration. */
7017 cp_parser_simple_declaration (parser
,
7018 /*function_definition_allowed_p=*/false);
7019 /* If the tentative parse failed, then we shall need to look for an
7020 expression-statement. */
7021 if (cp_parser_parse_definitely (parser
))
7025 cp_parser_expression_statement (parser
, false);
7028 /* Parse a jump-statement.
7033 return expression [opt] ;
7041 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7044 cp_parser_jump_statement (cp_parser
* parser
)
7046 tree statement
= error_mark_node
;
7049 unsigned char in_statement
;
7051 /* Peek at the next token. */
7052 token
= cp_parser_require (parser
, CPP_KEYWORD
, "jump-statement");
7054 return error_mark_node
;
7056 /* See what kind of keyword it is. */
7057 keyword
= token
->keyword
;
7061 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
7062 switch (in_statement
)
7065 error ("break statement not within loop or switch");
7068 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
7069 || in_statement
== IN_ITERATION_STMT
);
7070 statement
= finish_break_stmt ();
7073 error ("invalid exit from OpenMP structured block");
7076 error ("break statement used with OpenMP for loop");
7079 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7083 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
7086 error ("continue statement not within a loop");
7088 case IN_ITERATION_STMT
:
7090 statement
= finish_continue_stmt ();
7093 error ("invalid exit from OpenMP structured block");
7098 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7105 /* If the next token is a `;', then there is no
7107 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
7108 expr
= cp_parser_expression (parser
, /*cast_p=*/false);
7111 /* Build the return-statement. */
7112 statement
= finish_return_stmt (expr
);
7113 /* Look for the final `;'. */
7114 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7119 /* Create the goto-statement. */
7120 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
7122 /* Issue a warning about this use of a GNU extension. */
7124 pedwarn ("ISO C++ forbids computed gotos");
7125 /* Consume the '*' token. */
7126 cp_lexer_consume_token (parser
->lexer
);
7127 /* Parse the dependent expression. */
7128 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false));
7131 finish_goto_stmt (cp_parser_identifier (parser
));
7132 /* Look for the final `;'. */
7133 cp_parser_require (parser
, CPP_SEMICOLON
, "%<;%>");
7137 cp_parser_error (parser
, "expected jump-statement");
7144 /* Parse a declaration-statement.
7146 declaration-statement:
7147 block-declaration */
7150 cp_parser_declaration_statement (cp_parser
* parser
)
7154 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7155 p
= obstack_alloc (&declarator_obstack
, 0);
7157 /* Parse the block-declaration. */
7158 cp_parser_block_declaration (parser
, /*statement_p=*/true);
7160 /* Free any declarators allocated. */
7161 obstack_free (&declarator_obstack
, p
);
7163 /* Finish off the statement. */
7167 /* Some dependent statements (like `if (cond) statement'), are
7168 implicitly in their own scope. In other words, if the statement is
7169 a single statement (as opposed to a compound-statement), it is
7170 none-the-less treated as if it were enclosed in braces. Any
7171 declarations appearing in the dependent statement are out of scope
7172 after control passes that point. This function parses a statement,
7173 but ensures that is in its own scope, even if it is not a
7176 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7177 is a (possibly labeled) if statement which is not enclosed in
7178 braces and has an else clause. This is used to implement
7181 Returns the new statement. */
7184 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
7191 /* Mark if () ; with a special NOP_EXPR. */
7192 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7194 cp_lexer_consume_token (parser
->lexer
);
7195 statement
= add_stmt (build_empty_stmt ());
7197 /* if a compound is opened, we simply parse the statement directly. */
7198 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7199 statement
= cp_parser_compound_statement (parser
, NULL
, false);
7200 /* If the token is not a `{', then we must take special action. */
7203 /* Create a compound-statement. */
7204 statement
= begin_compound_stmt (0);
7205 /* Parse the dependent-statement. */
7206 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
7207 /* Finish the dummy compound-statement. */
7208 finish_compound_stmt (statement
);
7211 /* Return the statement. */
7215 /* For some dependent statements (like `while (cond) statement'), we
7216 have already created a scope. Therefore, even if the dependent
7217 statement is a compound-statement, we do not want to create another
7221 cp_parser_already_scoped_statement (cp_parser
* parser
)
7223 /* If the token is a `{', then we must take special action. */
7224 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
7225 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
7228 /* Avoid calling cp_parser_compound_statement, so that we
7229 don't create a new scope. Do everything else by hand. */
7230 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
7231 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
7232 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
7236 /* Declarations [gram.dcl.dcl] */
7238 /* Parse an optional declaration-sequence.
7242 declaration-seq declaration */
7245 cp_parser_declaration_seq_opt (cp_parser
* parser
)
7251 token
= cp_lexer_peek_token (parser
->lexer
);
7253 if (token
->type
== CPP_CLOSE_BRACE
7254 || token
->type
== CPP_EOF
7255 || token
->type
== CPP_PRAGMA_EOL
)
7258 if (token
->type
== CPP_SEMICOLON
)
7260 /* A declaration consisting of a single semicolon is
7261 invalid. Allow it unless we're being pedantic. */
7262 cp_lexer_consume_token (parser
->lexer
);
7263 if (pedantic
&& !in_system_header
)
7264 pedwarn ("extra %<;%>");
7268 /* If we're entering or exiting a region that's implicitly
7269 extern "C", modify the lang context appropriately. */
7270 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
7272 push_lang_context (lang_name_c
);
7273 parser
->implicit_extern_c
= true;
7275 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
7277 pop_lang_context ();
7278 parser
->implicit_extern_c
= false;
7281 if (token
->type
== CPP_PRAGMA
)
7283 /* A top-level declaration can consist solely of a #pragma.
7284 A nested declaration cannot, so this is done here and not
7285 in cp_parser_declaration. (A #pragma at block scope is
7286 handled in cp_parser_statement.) */
7287 cp_parser_pragma (parser
, pragma_external
);
7291 /* Parse the declaration itself. */
7292 cp_parser_declaration (parser
);
7296 /* Parse a declaration.
7301 template-declaration
7302 explicit-instantiation
7303 explicit-specialization
7304 linkage-specification
7305 namespace-definition
7310 __extension__ declaration */
7313 cp_parser_declaration (cp_parser
* parser
)
7320 /* Check for the `__extension__' keyword. */
7321 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7323 /* Parse the qualified declaration. */
7324 cp_parser_declaration (parser
);
7325 /* Restore the PEDANTIC flag. */
7326 pedantic
= saved_pedantic
;
7331 /* Try to figure out what kind of declaration is present. */
7332 token1
= *cp_lexer_peek_token (parser
->lexer
);
7334 if (token1
.type
!= CPP_EOF
)
7335 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
7338 token2
.type
= CPP_EOF
;
7339 token2
.keyword
= RID_MAX
;
7342 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7343 p
= obstack_alloc (&declarator_obstack
, 0);
7345 /* If the next token is `extern' and the following token is a string
7346 literal, then we have a linkage specification. */
7347 if (token1
.keyword
== RID_EXTERN
7348 && cp_parser_is_string_literal (&token2
))
7349 cp_parser_linkage_specification (parser
);
7350 /* If the next token is `template', then we have either a template
7351 declaration, an explicit instantiation, or an explicit
7353 else if (token1
.keyword
== RID_TEMPLATE
)
7355 /* `template <>' indicates a template specialization. */
7356 if (token2
.type
== CPP_LESS
7357 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
7358 cp_parser_explicit_specialization (parser
);
7359 /* `template <' indicates a template declaration. */
7360 else if (token2
.type
== CPP_LESS
)
7361 cp_parser_template_declaration (parser
, /*member_p=*/false);
7362 /* Anything else must be an explicit instantiation. */
7364 cp_parser_explicit_instantiation (parser
);
7366 /* If the next token is `export', then we have a template
7368 else if (token1
.keyword
== RID_EXPORT
)
7369 cp_parser_template_declaration (parser
, /*member_p=*/false);
7370 /* If the next token is `extern', 'static' or 'inline' and the one
7371 after that is `template', we have a GNU extended explicit
7372 instantiation directive. */
7373 else if (cp_parser_allow_gnu_extensions_p (parser
)
7374 && (token1
.keyword
== RID_EXTERN
7375 || token1
.keyword
== RID_STATIC
7376 || token1
.keyword
== RID_INLINE
)
7377 && token2
.keyword
== RID_TEMPLATE
)
7378 cp_parser_explicit_instantiation (parser
);
7379 /* If the next token is `namespace', check for a named or unnamed
7380 namespace definition. */
7381 else if (token1
.keyword
== RID_NAMESPACE
7382 && (/* A named namespace definition. */
7383 (token2
.type
== CPP_NAME
7384 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
7386 /* An unnamed namespace definition. */
7387 || token2
.type
== CPP_OPEN_BRACE
7388 || token2
.keyword
== RID_ATTRIBUTE
))
7389 cp_parser_namespace_definition (parser
);
7390 /* Objective-C++ declaration/definition. */
7391 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
7392 cp_parser_objc_declaration (parser
);
7393 /* We must have either a block declaration or a function
7396 /* Try to parse a block-declaration, or a function-definition. */
7397 cp_parser_block_declaration (parser
, /*statement_p=*/false);
7399 /* Free any declarators allocated. */
7400 obstack_free (&declarator_obstack
, p
);
7403 /* Parse a block-declaration.
7408 namespace-alias-definition
7415 __extension__ block-declaration
7421 static_assert-declaration
7423 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7424 part of a declaration-statement. */
7427 cp_parser_block_declaration (cp_parser
*parser
,
7433 /* Check for the `__extension__' keyword. */
7434 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
7436 /* Parse the qualified declaration. */
7437 cp_parser_block_declaration (parser
, statement_p
);
7438 /* Restore the PEDANTIC flag. */
7439 pedantic
= saved_pedantic
;
7444 /* Peek at the next token to figure out which kind of declaration is
7446 token1
= cp_lexer_peek_token (parser
->lexer
);
7448 /* If the next keyword is `asm', we have an asm-definition. */
7449 if (token1
->keyword
== RID_ASM
)
7452 cp_parser_commit_to_tentative_parse (parser
);
7453 cp_parser_asm_definition (parser
);
7455 /* If the next keyword is `namespace', we have a
7456 namespace-alias-definition. */
7457 else if (token1
->keyword
== RID_NAMESPACE
)
7458 cp_parser_namespace_alias_definition (parser
);
7459 /* If the next keyword is `using', we have either a
7460 using-declaration or a using-directive. */
7461 else if (token1
->keyword
== RID_USING
)
7466 cp_parser_commit_to_tentative_parse (parser
);
7467 /* If the token after `using' is `namespace', then we have a
7469 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
7470 if (token2
->keyword
== RID_NAMESPACE
)
7471 cp_parser_using_directive (parser
);
7472 /* Otherwise, it's a using-declaration. */
7474 cp_parser_using_declaration (parser
,
7475 /*access_declaration_p=*/false);
7477 /* If the next keyword is `__label__' we have a label declaration. */
7478 else if (token1
->keyword
== RID_LABEL
)
7481 cp_parser_commit_to_tentative_parse (parser
);
7482 cp_parser_label_declaration (parser
);
7484 /* If the next token is `static_assert' we have a static assertion. */
7485 else if (token1
->keyword
== RID_STATIC_ASSERT
)
7486 cp_parser_static_assert (parser
, /*member_p=*/false);
7487 /* Anything else must be a simple-declaration. */
7489 cp_parser_simple_declaration (parser
, !statement_p
);
7492 /* Parse a simple-declaration.
7495 decl-specifier-seq [opt] init-declarator-list [opt] ;
7497 init-declarator-list:
7499 init-declarator-list , init-declarator
7501 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7502 function-definition as a simple-declaration. */
7505 cp_parser_simple_declaration (cp_parser
* parser
,
7506 bool function_definition_allowed_p
)
7508 cp_decl_specifier_seq decl_specifiers
;
7509 int declares_class_or_enum
;
7510 bool saw_declarator
;
7512 /* Defer access checks until we know what is being declared; the
7513 checks for names appearing in the decl-specifier-seq should be
7514 done as if we were in the scope of the thing being declared. */
7515 push_deferring_access_checks (dk_deferred
);
7517 /* Parse the decl-specifier-seq. We have to keep track of whether
7518 or not the decl-specifier-seq declares a named class or
7519 enumeration type, since that is the only case in which the
7520 init-declarator-list is allowed to be empty.
7524 In a simple-declaration, the optional init-declarator-list can be
7525 omitted only when declaring a class or enumeration, that is when
7526 the decl-specifier-seq contains either a class-specifier, an
7527 elaborated-type-specifier, or an enum-specifier. */
7528 cp_parser_decl_specifier_seq (parser
,
7529 CP_PARSER_FLAGS_OPTIONAL
,
7531 &declares_class_or_enum
);
7532 /* We no longer need to defer access checks. */
7533 stop_deferring_access_checks ();
7535 /* In a block scope, a valid declaration must always have a
7536 decl-specifier-seq. By not trying to parse declarators, we can
7537 resolve the declaration/expression ambiguity more quickly. */
7538 if (!function_definition_allowed_p
7539 && !decl_specifiers
.any_specifiers_p
)
7541 cp_parser_error (parser
, "expected declaration");
7545 /* If the next two tokens are both identifiers, the code is
7546 erroneous. The usual cause of this situation is code like:
7550 where "T" should name a type -- but does not. */
7551 if (!decl_specifiers
.type
7552 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
7554 /* If parsing tentatively, we should commit; we really are
7555 looking at a declaration. */
7556 cp_parser_commit_to_tentative_parse (parser
);
7561 /* If we have seen at least one decl-specifier, and the next token
7562 is not a parenthesis, then we must be looking at a declaration.
7563 (After "int (" we might be looking at a functional cast.) */
7564 if (decl_specifiers
.any_specifiers_p
7565 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7566 cp_parser_commit_to_tentative_parse (parser
);
7568 /* Keep going until we hit the `;' at the end of the simple
7570 saw_declarator
= false;
7571 while (cp_lexer_next_token_is_not (parser
->lexer
,
7575 bool function_definition_p
;
7580 /* If we are processing next declarator, coma is expected */
7581 token
= cp_lexer_peek_token (parser
->lexer
);
7582 gcc_assert (token
->type
== CPP_COMMA
);
7583 cp_lexer_consume_token (parser
->lexer
);
7586 saw_declarator
= true;
7588 /* Parse the init-declarator. */
7589 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
7591 function_definition_allowed_p
,
7593 declares_class_or_enum
,
7594 &function_definition_p
);
7595 /* If an error occurred while parsing tentatively, exit quickly.
7596 (That usually happens when in the body of a function; each
7597 statement is treated as a declaration-statement until proven
7599 if (cp_parser_error_occurred (parser
))
7601 /* Handle function definitions specially. */
7602 if (function_definition_p
)
7604 /* If the next token is a `,', then we are probably
7605 processing something like:
7609 which is erroneous. */
7610 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
7611 error ("mixing declarations and function-definitions is forbidden");
7612 /* Otherwise, we're done with the list of declarators. */
7615 pop_deferring_access_checks ();
7619 /* The next token should be either a `,' or a `;'. */
7620 token
= cp_lexer_peek_token (parser
->lexer
);
7621 /* If it's a `,', there are more declarators to come. */
7622 if (token
->type
== CPP_COMMA
)
7623 /* will be consumed next time around */;
7624 /* If it's a `;', we are done. */
7625 else if (token
->type
== CPP_SEMICOLON
)
7627 /* Anything else is an error. */
7630 /* If we have already issued an error message we don't need
7631 to issue another one. */
7632 if (decl
!= error_mark_node
7633 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
7634 cp_parser_error (parser
, "expected %<,%> or %<;%>");
7635 /* Skip tokens until we reach the end of the statement. */
7636 cp_parser_skip_to_end_of_statement (parser
);
7637 /* If the next token is now a `;', consume it. */
7638 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
7639 cp_lexer_consume_token (parser
->lexer
);
7642 /* After the first time around, a function-definition is not
7643 allowed -- even if it was OK at first. For example:
7648 function_definition_allowed_p
= false;
7651 /* Issue an error message if no declarators are present, and the
7652 decl-specifier-seq does not itself declare a class or
7654 if (!saw_declarator
)
7656 if (cp_parser_declares_only_class_p (parser
))
7657 shadow_tag (&decl_specifiers
);
7658 /* Perform any deferred access checks. */
7659 perform_deferred_access_checks ();
7662 /* Consume the `;'. */
7663 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
7666 pop_deferring_access_checks ();
7669 /* Parse a decl-specifier-seq.
7672 decl-specifier-seq [opt] decl-specifier
7675 storage-class-specifier
7686 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7688 The parser flags FLAGS is used to control type-specifier parsing.
7690 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7693 1: one of the decl-specifiers is an elaborated-type-specifier
7694 (i.e., a type declaration)
7695 2: one of the decl-specifiers is an enum-specifier or a
7696 class-specifier (i.e., a type definition)
7701 cp_parser_decl_specifier_seq (cp_parser
* parser
,
7702 cp_parser_flags flags
,
7703 cp_decl_specifier_seq
*decl_specs
,
7704 int* declares_class_or_enum
)
7706 bool constructor_possible_p
= !parser
->in_declarator_p
;
7708 /* Clear DECL_SPECS. */
7709 clear_decl_specs (decl_specs
);
7711 /* Assume no class or enumeration type is declared. */
7712 *declares_class_or_enum
= 0;
7714 /* Keep reading specifiers until there are no more to read. */
7718 bool found_decl_spec
;
7721 /* Peek at the next token. */
7722 token
= cp_lexer_peek_token (parser
->lexer
);
7723 /* Handle attributes. */
7724 if (token
->keyword
== RID_ATTRIBUTE
)
7726 /* Parse the attributes. */
7727 decl_specs
->attributes
7728 = chainon (decl_specs
->attributes
,
7729 cp_parser_attributes_opt (parser
));
7732 /* Assume we will find a decl-specifier keyword. */
7733 found_decl_spec
= true;
7734 /* If the next token is an appropriate keyword, we can simply
7735 add it to the list. */
7736 switch (token
->keyword
)
7741 if (!at_class_scope_p ())
7743 error ("%<friend%> used outside of class");
7744 cp_lexer_purge_token (parser
->lexer
);
7748 ++decl_specs
->specs
[(int) ds_friend
];
7749 /* Consume the token. */
7750 cp_lexer_consume_token (parser
->lexer
);
7754 /* function-specifier:
7761 cp_parser_function_specifier_opt (parser
, decl_specs
);
7767 ++decl_specs
->specs
[(int) ds_typedef
];
7768 /* Consume the token. */
7769 cp_lexer_consume_token (parser
->lexer
);
7770 /* A constructor declarator cannot appear in a typedef. */
7771 constructor_possible_p
= false;
7772 /* The "typedef" keyword can only occur in a declaration; we
7773 may as well commit at this point. */
7774 cp_parser_commit_to_tentative_parse (parser
);
7776 if (decl_specs
->storage_class
!= sc_none
)
7777 decl_specs
->conflicting_specifiers_p
= true;
7780 /* storage-class-specifier:
7794 /* Consume the token. */
7795 cp_lexer_consume_token (parser
->lexer
);
7796 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
);
7799 /* Consume the token. */
7800 cp_lexer_consume_token (parser
->lexer
);
7801 ++decl_specs
->specs
[(int) ds_thread
];
7805 /* We did not yet find a decl-specifier yet. */
7806 found_decl_spec
= false;
7810 /* Constructors are a special case. The `S' in `S()' is not a
7811 decl-specifier; it is the beginning of the declarator. */
7814 && constructor_possible_p
7815 && (cp_parser_constructor_declarator_p
7816 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
7818 /* If we don't have a DECL_SPEC yet, then we must be looking at
7819 a type-specifier. */
7820 if (!found_decl_spec
&& !constructor_p
)
7822 int decl_spec_declares_class_or_enum
;
7823 bool is_cv_qualifier
;
7827 = cp_parser_type_specifier (parser
, flags
,
7829 /*is_declaration=*/true,
7830 &decl_spec_declares_class_or_enum
,
7833 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
7835 /* If this type-specifier referenced a user-defined type
7836 (a typedef, class-name, etc.), then we can't allow any
7837 more such type-specifiers henceforth.
7841 The longest sequence of decl-specifiers that could
7842 possibly be a type name is taken as the
7843 decl-specifier-seq of a declaration. The sequence shall
7844 be self-consistent as described below.
7848 As a general rule, at most one type-specifier is allowed
7849 in the complete decl-specifier-seq of a declaration. The
7850 only exceptions are the following:
7852 -- const or volatile can be combined with any other
7855 -- signed or unsigned can be combined with char, long,
7863 void g (const int Pc);
7865 Here, Pc is *not* part of the decl-specifier seq; it's
7866 the declarator. Therefore, once we see a type-specifier
7867 (other than a cv-qualifier), we forbid any additional
7868 user-defined types. We *do* still allow things like `int
7869 int' to be considered a decl-specifier-seq, and issue the
7870 error message later. */
7871 if (type_spec
&& !is_cv_qualifier
)
7872 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
7873 /* A constructor declarator cannot follow a type-specifier. */
7876 constructor_possible_p
= false;
7877 found_decl_spec
= true;
7881 /* If we still do not have a DECL_SPEC, then there are no more
7883 if (!found_decl_spec
)
7886 decl_specs
->any_specifiers_p
= true;
7887 /* After we see one decl-specifier, further decl-specifiers are
7889 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
7892 cp_parser_check_decl_spec (decl_specs
);
7894 /* Don't allow a friend specifier with a class definition. */
7895 if (decl_specs
->specs
[(int) ds_friend
] != 0
7896 && (*declares_class_or_enum
& 2))
7897 error ("class definition may not be declared a friend");
7900 /* Parse an (optional) storage-class-specifier.
7902 storage-class-specifier:
7911 storage-class-specifier:
7914 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7917 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
7919 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7927 /* Consume the token. */
7928 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
7935 /* Parse an (optional) function-specifier.
7942 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7943 Updates DECL_SPECS, if it is non-NULL. */
7946 cp_parser_function_specifier_opt (cp_parser
* parser
,
7947 cp_decl_specifier_seq
*decl_specs
)
7949 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
7953 ++decl_specs
->specs
[(int) ds_inline
];
7957 /* 14.5.2.3 [temp.mem]
7959 A member function template shall not be virtual. */
7960 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7961 error ("templates may not be %<virtual%>");
7962 else if (decl_specs
)
7963 ++decl_specs
->specs
[(int) ds_virtual
];
7968 ++decl_specs
->specs
[(int) ds_explicit
];
7975 /* Consume the token. */
7976 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
7979 /* Parse a linkage-specification.
7981 linkage-specification:
7982 extern string-literal { declaration-seq [opt] }
7983 extern string-literal declaration */
7986 cp_parser_linkage_specification (cp_parser
* parser
)
7990 /* Look for the `extern' keyword. */
7991 cp_parser_require_keyword (parser
, RID_EXTERN
, "`extern'");
7993 /* Look for the string-literal. */
7994 linkage
= cp_parser_string_literal (parser
, false, false);
7996 /* Transform the literal into an identifier. If the literal is a
7997 wide-character string, or contains embedded NULs, then we can't
7998 handle it as the user wants. */
7999 if (strlen (TREE_STRING_POINTER (linkage
))
8000 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
8002 cp_parser_error (parser
, "invalid linkage-specification");
8003 /* Assume C++ linkage. */
8004 linkage
= lang_name_cplusplus
;
8007 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
8009 /* We're now using the new linkage. */
8010 push_lang_context (linkage
);
8012 /* If the next token is a `{', then we're using the first
8014 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8016 /* Consume the `{' token. */
8017 cp_lexer_consume_token (parser
->lexer
);
8018 /* Parse the declarations. */
8019 cp_parser_declaration_seq_opt (parser
);
8020 /* Look for the closing `}'. */
8021 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
8023 /* Otherwise, there's just one declaration. */
8026 bool saved_in_unbraced_linkage_specification_p
;
8028 saved_in_unbraced_linkage_specification_p
8029 = parser
->in_unbraced_linkage_specification_p
;
8030 parser
->in_unbraced_linkage_specification_p
= true;
8031 cp_parser_declaration (parser
);
8032 parser
->in_unbraced_linkage_specification_p
8033 = saved_in_unbraced_linkage_specification_p
;
8036 /* We're done with the linkage-specification. */
8037 pop_lang_context ();
8040 /* Parse a static_assert-declaration.
8042 static_assert-declaration:
8043 static_assert ( constant-expression , string-literal ) ;
8045 If MEMBER_P, this static_assert is a class member. */
8048 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
8053 location_t saved_loc
;
8055 /* Peek at the `static_assert' token so we can keep track of exactly
8056 where the static assertion started. */
8057 token
= cp_lexer_peek_token (parser
->lexer
);
8058 saved_loc
= token
->location
;
8060 /* Look for the `static_assert' keyword. */
8061 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
8065 /* We know we are in a static assertion; commit to any tentative
8067 if (cp_parser_parsing_tentatively (parser
))
8068 cp_parser_commit_to_tentative_parse (parser
);
8070 /* Parse the `(' starting the static assertion condition. */
8071 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
8073 /* Parse the constant-expression. */
8075 cp_parser_constant_expression (parser
,
8076 /*allow_non_constant_p=*/false,
8077 /*non_constant_p=*/NULL
);
8079 /* Parse the separating `,'. */
8080 cp_parser_require (parser
, CPP_COMMA
, "`,'");
8082 /* Parse the string-literal message. */
8083 message
= cp_parser_string_literal (parser
,
8084 /*translate=*/false,
8087 /* A `)' completes the static assertion. */
8088 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
8089 cp_parser_skip_to_closing_parenthesis (parser
,
8090 /*recovering=*/true,
8092 /*consume_paren=*/true);
8094 /* A semicolon terminates the declaration. */
8095 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
8097 /* Complete the static assertion, which may mean either processing
8098 the static assert now or saving it for template instantiation. */
8099 finish_static_assert (condition
, message
, saved_loc
, member_p
);
8102 /* Special member functions [gram.special] */
8104 /* Parse a conversion-function-id.
8106 conversion-function-id:
8107 operator conversion-type-id
8109 Returns an IDENTIFIER_NODE representing the operator. */
8112 cp_parser_conversion_function_id (cp_parser
* parser
)
8116 tree saved_qualifying_scope
;
8117 tree saved_object_scope
;
8118 tree pushed_scope
= NULL_TREE
;
8120 /* Look for the `operator' token. */
8121 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
8122 return error_mark_node
;
8123 /* When we parse the conversion-type-id, the current scope will be
8124 reset. However, we need that information in able to look up the
8125 conversion function later, so we save it here. */
8126 saved_scope
= parser
->scope
;
8127 saved_qualifying_scope
= parser
->qualifying_scope
;
8128 saved_object_scope
= parser
->object_scope
;
8129 /* We must enter the scope of the class so that the names of
8130 entities declared within the class are available in the
8131 conversion-type-id. For example, consider:
8138 S::operator I() { ... }
8140 In order to see that `I' is a type-name in the definition, we
8141 must be in the scope of `S'. */
8143 pushed_scope
= push_scope (saved_scope
);
8144 /* Parse the conversion-type-id. */
8145 type
= cp_parser_conversion_type_id (parser
);
8146 /* Leave the scope of the class, if any. */
8148 pop_scope (pushed_scope
);
8149 /* Restore the saved scope. */
8150 parser
->scope
= saved_scope
;
8151 parser
->qualifying_scope
= saved_qualifying_scope
;
8152 parser
->object_scope
= saved_object_scope
;
8153 /* If the TYPE is invalid, indicate failure. */
8154 if (type
== error_mark_node
)
8155 return error_mark_node
;
8156 return mangle_conv_op_name_for_type (type
);
8159 /* Parse a conversion-type-id:
8162 type-specifier-seq conversion-declarator [opt]
8164 Returns the TYPE specified. */
8167 cp_parser_conversion_type_id (cp_parser
* parser
)
8170 cp_decl_specifier_seq type_specifiers
;
8171 cp_declarator
*declarator
;
8172 tree type_specified
;
8174 /* Parse the attributes. */
8175 attributes
= cp_parser_attributes_opt (parser
);
8176 /* Parse the type-specifiers. */
8177 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
8179 /* If that didn't work, stop. */
8180 if (type_specifiers
.type
== error_mark_node
)
8181 return error_mark_node
;
8182 /* Parse the conversion-declarator. */
8183 declarator
= cp_parser_conversion_declarator_opt (parser
);
8185 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
8186 /*initialized=*/0, &attributes
);
8188 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
8189 return type_specified
;
8192 /* Parse an (optional) conversion-declarator.
8194 conversion-declarator:
8195 ptr-operator conversion-declarator [opt]
8199 static cp_declarator
*
8200 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
8202 enum tree_code code
;
8204 cp_cv_quals cv_quals
;
8206 /* We don't know if there's a ptr-operator next, or not. */
8207 cp_parser_parse_tentatively (parser
);
8208 /* Try the ptr-operator. */
8209 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
8210 /* If it worked, look for more conversion-declarators. */
8211 if (cp_parser_parse_definitely (parser
))
8213 cp_declarator
*declarator
;
8215 /* Parse another optional declarator. */
8216 declarator
= cp_parser_conversion_declarator_opt (parser
);
8218 /* Create the representation of the declarator. */
8220 declarator
= make_ptrmem_declarator (cv_quals
, class_type
,
8222 else if (code
== INDIRECT_REF
)
8223 declarator
= make_pointer_declarator (cv_quals
, declarator
);
8225 declarator
= make_reference_declarator (cv_quals
, declarator
);
8233 /* Parse an (optional) ctor-initializer.
8236 : mem-initializer-list
8238 Returns TRUE iff the ctor-initializer was actually present. */
8241 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
8243 /* If the next token is not a `:', then there is no
8244 ctor-initializer. */
8245 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
8247 /* Do default initialization of any bases and members. */
8248 if (DECL_CONSTRUCTOR_P (current_function_decl
))
8249 finish_mem_initializers (NULL_TREE
);
8254 /* Consume the `:' token. */
8255 cp_lexer_consume_token (parser
->lexer
);
8256 /* And the mem-initializer-list. */
8257 cp_parser_mem_initializer_list (parser
);
8262 /* Parse a mem-initializer-list.
8264 mem-initializer-list:
8265 mem-initializer ... [opt]
8266 mem-initializer ... [opt] , mem-initializer-list */
8269 cp_parser_mem_initializer_list (cp_parser
* parser
)
8271 tree mem_initializer_list
= NULL_TREE
;
8273 /* Let the semantic analysis code know that we are starting the
8274 mem-initializer-list. */
8275 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
8276 error ("only constructors take base initializers");
8278 /* Loop through the list. */
8281 tree mem_initializer
;
8283 /* Parse the mem-initializer. */
8284 mem_initializer
= cp_parser_mem_initializer (parser
);
8285 /* If the next token is a `...', we're expanding member initializers. */
8286 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8288 /* Consume the `...'. */
8289 cp_lexer_consume_token (parser
->lexer
);
8291 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8292 can be expanded but members cannot. */
8293 if (mem_initializer
!= error_mark_node
8294 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
8296 error ("cannot expand initializer for member %<%D%>",
8297 TREE_PURPOSE (mem_initializer
));
8298 mem_initializer
= error_mark_node
;
8301 /* Construct the pack expansion type. */
8302 if (mem_initializer
!= error_mark_node
)
8303 mem_initializer
= make_pack_expansion (mem_initializer
);
8305 /* Add it to the list, unless it was erroneous. */
8306 if (mem_initializer
!= error_mark_node
)
8308 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
8309 mem_initializer_list
= mem_initializer
;
8311 /* If the next token is not a `,', we're done. */
8312 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8314 /* Consume the `,' token. */
8315 cp_lexer_consume_token (parser
->lexer
);
8318 /* Perform semantic analysis. */
8319 if (DECL_CONSTRUCTOR_P (current_function_decl
))
8320 finish_mem_initializers (mem_initializer_list
);
8323 /* Parse a mem-initializer.
8326 mem-initializer-id ( expression-list [opt] )
8331 ( expression-list [opt] )
8333 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8334 class) or FIELD_DECL (for a non-static data member) to initialize;
8335 the TREE_VALUE is the expression-list. An empty initialization
8336 list is represented by void_list_node. */
8339 cp_parser_mem_initializer (cp_parser
* parser
)
8341 tree mem_initializer_id
;
8342 tree expression_list
;
8345 /* Find out what is being initialized. */
8346 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8348 pedwarn ("anachronistic old-style base class initializer");
8349 mem_initializer_id
= NULL_TREE
;
8352 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
8353 member
= expand_member_init (mem_initializer_id
);
8354 if (member
&& !DECL_P (member
))
8355 in_base_initializer
= 1;
8358 = cp_parser_parenthesized_expression_list (parser
, false,
8360 /*allow_expansion_p=*/true,
8361 /*non_constant_p=*/NULL
);
8362 if (expression_list
== error_mark_node
)
8363 return error_mark_node
;
8364 if (!expression_list
)
8365 expression_list
= void_type_node
;
8367 in_base_initializer
= 0;
8369 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
8372 /* Parse a mem-initializer-id.
8375 :: [opt] nested-name-specifier [opt] class-name
8378 Returns a TYPE indicating the class to be initializer for the first
8379 production. Returns an IDENTIFIER_NODE indicating the data member
8380 to be initialized for the second production. */
8383 cp_parser_mem_initializer_id (cp_parser
* parser
)
8385 bool global_scope_p
;
8386 bool nested_name_specifier_p
;
8387 bool template_p
= false;
8390 /* `typename' is not allowed in this context ([temp.res]). */
8391 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
8393 error ("keyword %<typename%> not allowed in this context (a qualified "
8394 "member initializer is implicitly a type)");
8395 cp_lexer_consume_token (parser
->lexer
);
8397 /* Look for the optional `::' operator. */
8399 = (cp_parser_global_scope_opt (parser
,
8400 /*current_scope_valid_p=*/false)
8402 /* Look for the optional nested-name-specifier. The simplest way to
8407 The keyword `typename' is not permitted in a base-specifier or
8408 mem-initializer; in these contexts a qualified name that
8409 depends on a template-parameter is implicitly assumed to be a
8412 is to assume that we have seen the `typename' keyword at this
8414 nested_name_specifier_p
8415 = (cp_parser_nested_name_specifier_opt (parser
,
8416 /*typename_keyword_p=*/true,
8417 /*check_dependency_p=*/true,
8419 /*is_declaration=*/true)
8421 if (nested_name_specifier_p
)
8422 template_p
= cp_parser_optional_template_keyword (parser
);
8423 /* If there is a `::' operator or a nested-name-specifier, then we
8424 are definitely looking for a class-name. */
8425 if (global_scope_p
|| nested_name_specifier_p
)
8426 return cp_parser_class_name (parser
,
8427 /*typename_keyword_p=*/true,
8428 /*template_keyword_p=*/template_p
,
8430 /*check_dependency_p=*/true,
8431 /*class_head_p=*/false,
8432 /*is_declaration=*/true);
8433 /* Otherwise, we could also be looking for an ordinary identifier. */
8434 cp_parser_parse_tentatively (parser
);
8435 /* Try a class-name. */
8436 id
= cp_parser_class_name (parser
,
8437 /*typename_keyword_p=*/true,
8438 /*template_keyword_p=*/false,
8440 /*check_dependency_p=*/true,
8441 /*class_head_p=*/false,
8442 /*is_declaration=*/true);
8443 /* If we found one, we're done. */
8444 if (cp_parser_parse_definitely (parser
))
8446 /* Otherwise, look for an ordinary identifier. */
8447 return cp_parser_identifier (parser
);
8450 /* Overloading [gram.over] */
8452 /* Parse an operator-function-id.
8454 operator-function-id:
8457 Returns an IDENTIFIER_NODE for the operator which is a
8458 human-readable spelling of the identifier, e.g., `operator +'. */
8461 cp_parser_operator_function_id (cp_parser
* parser
)
8463 /* Look for the `operator' keyword. */
8464 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, "`operator'"))
8465 return error_mark_node
;
8466 /* And then the name of the operator itself. */
8467 return cp_parser_operator (parser
);
8470 /* Parse an operator.
8473 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8474 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8475 || ++ -- , ->* -> () []
8482 Returns an IDENTIFIER_NODE for the operator which is a
8483 human-readable spelling of the identifier, e.g., `operator +'. */
8486 cp_parser_operator (cp_parser
* parser
)
8488 tree id
= NULL_TREE
;
8491 /* Peek at the next token. */
8492 token
= cp_lexer_peek_token (parser
->lexer
);
8493 /* Figure out which operator we have. */
8494 switch (token
->type
)
8500 /* The keyword should be either `new' or `delete'. */
8501 if (token
->keyword
== RID_NEW
)
8503 else if (token
->keyword
== RID_DELETE
)
8508 /* Consume the `new' or `delete' token. */
8509 cp_lexer_consume_token (parser
->lexer
);
8511 /* Peek at the next token. */
8512 token
= cp_lexer_peek_token (parser
->lexer
);
8513 /* If it's a `[' token then this is the array variant of the
8515 if (token
->type
== CPP_OPEN_SQUARE
)
8517 /* Consume the `[' token. */
8518 cp_lexer_consume_token (parser
->lexer
);
8519 /* Look for the `]' token. */
8520 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8521 id
= ansi_opname (op
== NEW_EXPR
8522 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
8524 /* Otherwise, we have the non-array variant. */
8526 id
= ansi_opname (op
);
8532 id
= ansi_opname (PLUS_EXPR
);
8536 id
= ansi_opname (MINUS_EXPR
);
8540 id
= ansi_opname (MULT_EXPR
);
8544 id
= ansi_opname (TRUNC_DIV_EXPR
);
8548 id
= ansi_opname (TRUNC_MOD_EXPR
);
8552 id
= ansi_opname (BIT_XOR_EXPR
);
8556 id
= ansi_opname (BIT_AND_EXPR
);
8560 id
= ansi_opname (BIT_IOR_EXPR
);
8564 id
= ansi_opname (BIT_NOT_EXPR
);
8568 id
= ansi_opname (TRUTH_NOT_EXPR
);
8572 id
= ansi_assopname (NOP_EXPR
);
8576 id
= ansi_opname (LT_EXPR
);
8580 id
= ansi_opname (GT_EXPR
);
8584 id
= ansi_assopname (PLUS_EXPR
);
8588 id
= ansi_assopname (MINUS_EXPR
);
8592 id
= ansi_assopname (MULT_EXPR
);
8596 id
= ansi_assopname (TRUNC_DIV_EXPR
);
8600 id
= ansi_assopname (TRUNC_MOD_EXPR
);
8604 id
= ansi_assopname (BIT_XOR_EXPR
);
8608 id
= ansi_assopname (BIT_AND_EXPR
);
8612 id
= ansi_assopname (BIT_IOR_EXPR
);
8616 id
= ansi_opname (LSHIFT_EXPR
);
8620 id
= ansi_opname (RSHIFT_EXPR
);
8624 id
= ansi_assopname (LSHIFT_EXPR
);
8628 id
= ansi_assopname (RSHIFT_EXPR
);
8632 id
= ansi_opname (EQ_EXPR
);
8636 id
= ansi_opname (NE_EXPR
);
8640 id
= ansi_opname (LE_EXPR
);
8643 case CPP_GREATER_EQ
:
8644 id
= ansi_opname (GE_EXPR
);
8648 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
8652 id
= ansi_opname (TRUTH_ORIF_EXPR
);
8656 id
= ansi_opname (POSTINCREMENT_EXPR
);
8659 case CPP_MINUS_MINUS
:
8660 id
= ansi_opname (PREDECREMENT_EXPR
);
8664 id
= ansi_opname (COMPOUND_EXPR
);
8667 case CPP_DEREF_STAR
:
8668 id
= ansi_opname (MEMBER_REF
);
8672 id
= ansi_opname (COMPONENT_REF
);
8675 case CPP_OPEN_PAREN
:
8676 /* Consume the `('. */
8677 cp_lexer_consume_token (parser
->lexer
);
8678 /* Look for the matching `)'. */
8679 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
8680 return ansi_opname (CALL_EXPR
);
8682 case CPP_OPEN_SQUARE
:
8683 /* Consume the `['. */
8684 cp_lexer_consume_token (parser
->lexer
);
8685 /* Look for the matching `]'. */
8686 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
8687 return ansi_opname (ARRAY_REF
);
8690 /* Anything else is an error. */
8694 /* If we have selected an identifier, we need to consume the
8697 cp_lexer_consume_token (parser
->lexer
);
8698 /* Otherwise, no valid operator name was present. */
8701 cp_parser_error (parser
, "expected operator");
8702 id
= error_mark_node
;
8708 /* Parse a template-declaration.
8710 template-declaration:
8711 export [opt] template < template-parameter-list > declaration
8713 If MEMBER_P is TRUE, this template-declaration occurs within a
8716 The grammar rule given by the standard isn't correct. What
8719 template-declaration:
8720 export [opt] template-parameter-list-seq
8721 decl-specifier-seq [opt] init-declarator [opt] ;
8722 export [opt] template-parameter-list-seq
8725 template-parameter-list-seq:
8726 template-parameter-list-seq [opt]
8727 template < template-parameter-list > */
8730 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
8732 /* Check for `export'. */
8733 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
8735 /* Consume the `export' token. */
8736 cp_lexer_consume_token (parser
->lexer
);
8737 /* Warn that we do not support `export'. */
8738 warning (0, "keyword %<export%> not implemented, and will be ignored");
8741 cp_parser_template_declaration_after_export (parser
, member_p
);
8744 /* Parse a template-parameter-list.
8746 template-parameter-list:
8748 template-parameter-list , template-parameter
8750 Returns a TREE_LIST. Each node represents a template parameter.
8751 The nodes are connected via their TREE_CHAINs. */
8754 cp_parser_template_parameter_list (cp_parser
* parser
)
8756 tree parameter_list
= NULL_TREE
;
8758 begin_template_parm_list ();
8764 bool is_parameter_pack
;
8766 /* Parse the template-parameter. */
8767 parameter
= cp_parser_template_parameter (parser
,
8769 &is_parameter_pack
);
8770 /* Add it to the list. */
8771 if (parameter
!= error_mark_node
)
8772 parameter_list
= process_template_parm (parameter_list
,
8778 tree err_parm
= build_tree_list (parameter
, parameter
);
8779 TREE_VALUE (err_parm
) = error_mark_node
;
8780 parameter_list
= chainon (parameter_list
, err_parm
);
8783 /* Peek at the next token. */
8784 token
= cp_lexer_peek_token (parser
->lexer
);
8785 /* If it's not a `,', we're done. */
8786 if (token
->type
!= CPP_COMMA
)
8788 /* Otherwise, consume the `,' token. */
8789 cp_lexer_consume_token (parser
->lexer
);
8792 return end_template_parm_list (parameter_list
);
8795 /* Parse a template-parameter.
8799 parameter-declaration
8801 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8802 the parameter. The TREE_PURPOSE is the default value, if any.
8803 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8804 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
8805 set to true iff this parameter is a parameter pack. */
8808 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
8809 bool *is_parameter_pack
)
8812 cp_parameter_declarator
*parameter_declarator
;
8815 /* Assume it is a type parameter or a template parameter. */
8816 *is_non_type
= false;
8817 /* Assume it not a parameter pack. */
8818 *is_parameter_pack
= false;
8819 /* Peek at the next token. */
8820 token
= cp_lexer_peek_token (parser
->lexer
);
8821 /* If it is `class' or `template', we have a type-parameter. */
8822 if (token
->keyword
== RID_TEMPLATE
)
8823 return cp_parser_type_parameter (parser
, is_parameter_pack
);
8824 /* If it is `class' or `typename' we do not know yet whether it is a
8825 type parameter or a non-type parameter. Consider:
8827 template <typename T, typename T::X X> ...
8831 template <class C, class D*> ...
8833 Here, the first parameter is a type parameter, and the second is
8834 a non-type parameter. We can tell by looking at the token after
8835 the identifier -- if it is a `,', `=', or `>' then we have a type
8837 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
8839 /* Peek at the token after `class' or `typename'. */
8840 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8841 /* If it's an ellipsis, we have a template type parameter
8843 if (token
->type
== CPP_ELLIPSIS
)
8844 return cp_parser_type_parameter (parser
, is_parameter_pack
);
8845 /* If it's an identifier, skip it. */
8846 if (token
->type
== CPP_NAME
)
8847 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
8848 /* Now, see if the token looks like the end of a template
8850 if (token
->type
== CPP_COMMA
8851 || token
->type
== CPP_EQ
8852 || token
->type
== CPP_GREATER
)
8853 return cp_parser_type_parameter (parser
, is_parameter_pack
);
8856 /* Otherwise, it is a non-type parameter.
8860 When parsing a default template-argument for a non-type
8861 template-parameter, the first non-nested `>' is taken as the end
8862 of the template parameter-list rather than a greater-than
8864 *is_non_type
= true;
8865 parameter_declarator
8866 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
8867 /*parenthesized_p=*/NULL
);
8869 /* If the parameter declaration is marked as a parameter pack, set
8870 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
8871 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
8873 if (parameter_declarator
8874 && parameter_declarator
->declarator
8875 && parameter_declarator
->declarator
->parameter_pack_p
)
8877 *is_parameter_pack
= true;
8878 parameter_declarator
->declarator
->parameter_pack_p
= false;
8881 /* If the next token is an ellipsis, and we don't already have it
8882 marked as a parameter pack, then we have a parameter pack (that
8883 has no declarator); */
8884 if (!*is_parameter_pack
8885 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8888 /* Consume the `...'. */
8889 cp_lexer_consume_token (parser
->lexer
);
8890 maybe_warn_variadic_templates ();
8892 *is_parameter_pack
= true;
8895 parm
= grokdeclarator (parameter_declarator
->declarator
,
8896 ¶meter_declarator
->decl_specifiers
,
8897 PARM
, /*initialized=*/0,
8899 if (parm
== error_mark_node
)
8900 return error_mark_node
;
8902 return build_tree_list (parameter_declarator
->default_argument
, parm
);
8905 /* Parse a type-parameter.
8908 class identifier [opt]
8909 class identifier [opt] = type-id
8910 typename identifier [opt]
8911 typename identifier [opt] = type-id
8912 template < template-parameter-list > class identifier [opt]
8913 template < template-parameter-list > class identifier [opt]
8916 GNU Extension (variadic templates):
8919 class ... identifier [opt]
8920 typename ... identifier [opt]
8922 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8923 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8924 the declaration of the parameter.
8926 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
8929 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
8934 /* Look for a keyword to tell us what kind of parameter this is. */
8935 token
= cp_parser_require (parser
, CPP_KEYWORD
,
8936 "`class', `typename', or `template'");
8938 return error_mark_node
;
8940 switch (token
->keyword
)
8946 tree default_argument
;
8948 /* If the next token is an ellipsis, we have a template
8950 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8952 /* Consume the `...' token. */
8953 cp_lexer_consume_token (parser
->lexer
);
8954 maybe_warn_variadic_templates ();
8956 *is_parameter_pack
= true;
8959 /* If the next token is an identifier, then it names the
8961 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
8962 identifier
= cp_parser_identifier (parser
);
8964 identifier
= NULL_TREE
;
8966 /* Create the parameter. */
8967 parameter
= finish_template_type_parm (class_type_node
, identifier
);
8969 /* If the next token is an `=', we have a default argument. */
8970 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8972 /* Consume the `=' token. */
8973 cp_lexer_consume_token (parser
->lexer
);
8974 /* Parse the default-argument. */
8975 push_deferring_access_checks (dk_no_deferred
);
8976 default_argument
= cp_parser_type_id (parser
);
8978 /* Template parameter packs cannot have default
8980 if (*is_parameter_pack
)
8983 error ("template parameter pack %qD cannot have a default argument",
8986 error ("template parameter packs cannot have default arguments");
8987 default_argument
= NULL_TREE
;
8989 pop_deferring_access_checks ();
8992 default_argument
= NULL_TREE
;
8994 /* Create the combined representation of the parameter and the
8995 default argument. */
8996 parameter
= build_tree_list (default_argument
, parameter
);
9002 tree parameter_list
;
9004 tree default_argument
;
9006 /* Look for the `<'. */
9007 cp_parser_require (parser
, CPP_LESS
, "`<'");
9008 /* Parse the template-parameter-list. */
9009 parameter_list
= cp_parser_template_parameter_list (parser
);
9010 /* Look for the `>'. */
9011 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9012 /* Look for the `class' keyword. */
9013 cp_parser_require_keyword (parser
, RID_CLASS
, "`class'");
9014 /* If the next token is an ellipsis, we have a template
9016 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9018 /* Consume the `...' token. */
9019 cp_lexer_consume_token (parser
->lexer
);
9020 maybe_warn_variadic_templates ();
9022 *is_parameter_pack
= true;
9024 /* If the next token is an `=', then there is a
9025 default-argument. If the next token is a `>', we are at
9026 the end of the parameter-list. If the next token is a `,',
9027 then we are at the end of this parameter. */
9028 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
9029 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
9030 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
9032 identifier
= cp_parser_identifier (parser
);
9033 /* Treat invalid names as if the parameter were nameless. */
9034 if (identifier
== error_mark_node
)
9035 identifier
= NULL_TREE
;
9038 identifier
= NULL_TREE
;
9040 /* Create the template parameter. */
9041 parameter
= finish_template_template_parm (class_type_node
,
9044 /* If the next token is an `=', then there is a
9045 default-argument. */
9046 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9050 /* Consume the `='. */
9051 cp_lexer_consume_token (parser
->lexer
);
9052 /* Parse the id-expression. */
9053 push_deferring_access_checks (dk_no_deferred
);
9055 = cp_parser_id_expression (parser
,
9056 /*template_keyword_p=*/false,
9057 /*check_dependency_p=*/true,
9058 /*template_p=*/&is_template
,
9059 /*declarator_p=*/false,
9060 /*optional_p=*/false);
9061 if (TREE_CODE (default_argument
) == TYPE_DECL
)
9062 /* If the id-expression was a template-id that refers to
9063 a template-class, we already have the declaration here,
9064 so no further lookup is needed. */
9067 /* Look up the name. */
9069 = cp_parser_lookup_name (parser
, default_argument
,
9071 /*is_template=*/is_template
,
9072 /*is_namespace=*/false,
9073 /*check_dependency=*/true,
9074 /*ambiguous_decls=*/NULL
);
9075 /* See if the default argument is valid. */
9077 = check_template_template_default_arg (default_argument
);
9079 /* Template parameter packs cannot have default
9081 if (*is_parameter_pack
)
9084 error ("template parameter pack %qD cannot have a default argument",
9087 error ("template parameter packs cannot have default arguments");
9088 default_argument
= NULL_TREE
;
9090 pop_deferring_access_checks ();
9093 default_argument
= NULL_TREE
;
9095 /* Create the combined representation of the parameter and the
9096 default argument. */
9097 parameter
= build_tree_list (default_argument
, parameter
);
9109 /* Parse a template-id.
9112 template-name < template-argument-list [opt] >
9114 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9115 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9116 returned. Otherwise, if the template-name names a function, or set
9117 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9118 names a class, returns a TYPE_DECL for the specialization.
9120 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9121 uninstantiated templates. */
9124 cp_parser_template_id (cp_parser
*parser
,
9125 bool template_keyword_p
,
9126 bool check_dependency_p
,
9127 bool is_declaration
)
9133 cp_token_position start_of_id
= 0;
9134 deferred_access_check
*chk
;
9135 VEC (deferred_access_check
,gc
) *access_check
;
9136 cp_token
*next_token
, *next_token_2
;
9139 /* If the next token corresponds to a template-id, there is no need
9141 next_token
= cp_lexer_peek_token (parser
->lexer
);
9142 if (next_token
->type
== CPP_TEMPLATE_ID
)
9144 struct tree_check
*check_value
;
9146 /* Get the stored value. */
9147 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
9148 /* Perform any access checks that were deferred. */
9149 access_check
= check_value
->checks
;
9153 VEC_iterate (deferred_access_check
, access_check
, i
, chk
) ;
9156 perform_or_defer_access_check (chk
->binfo
,
9161 /* Return the stored value. */
9162 return check_value
->value
;
9165 /* Avoid performing name lookup if there is no possibility of
9166 finding a template-id. */
9167 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
9168 || (next_token
->type
== CPP_NAME
9169 && !cp_parser_nth_token_starts_template_argument_list_p
9172 cp_parser_error (parser
, "expected template-id");
9173 return error_mark_node
;
9176 /* Remember where the template-id starts. */
9177 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
9178 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
9180 push_deferring_access_checks (dk_deferred
);
9182 /* Parse the template-name. */
9183 is_identifier
= false;
9184 template = cp_parser_template_name (parser
, template_keyword_p
,
9188 if (template == error_mark_node
|| is_identifier
)
9190 pop_deferring_access_checks ();
9194 /* If we find the sequence `[:' after a template-name, it's probably
9195 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9196 parse correctly the argument list. */
9197 next_token
= cp_lexer_peek_token (parser
->lexer
);
9198 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9199 if (next_token
->type
== CPP_OPEN_SQUARE
9200 && next_token
->flags
& DIGRAPH
9201 && next_token_2
->type
== CPP_COLON
9202 && !(next_token_2
->flags
& PREV_WHITE
))
9204 cp_parser_parse_tentatively (parser
);
9205 /* Change `:' into `::'. */
9206 next_token_2
->type
= CPP_SCOPE
;
9207 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9209 cp_lexer_consume_token (parser
->lexer
);
9210 /* Parse the arguments. */
9211 arguments
= cp_parser_enclosed_template_argument_list (parser
);
9212 if (!cp_parser_parse_definitely (parser
))
9214 /* If we couldn't parse an argument list, then we revert our changes
9215 and return simply an error. Maybe this is not a template-id
9217 next_token_2
->type
= CPP_COLON
;
9218 cp_parser_error (parser
, "expected %<<%>");
9219 pop_deferring_access_checks ();
9220 return error_mark_node
;
9222 /* Otherwise, emit an error about the invalid digraph, but continue
9223 parsing because we got our argument list. */
9224 pedwarn ("%<<::%> cannot begin a template-argument list");
9225 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9226 "between %<<%> and %<::%>");
9227 if (!flag_permissive
)
9232 inform ("(if you use -fpermissive G++ will accept your code)");
9239 /* Look for the `<' that starts the template-argument-list. */
9240 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
9242 pop_deferring_access_checks ();
9243 return error_mark_node
;
9245 /* Parse the arguments. */
9246 arguments
= cp_parser_enclosed_template_argument_list (parser
);
9249 /* Build a representation of the specialization. */
9250 if (TREE_CODE (template) == IDENTIFIER_NODE
)
9251 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, template, arguments
);
9252 else if (DECL_CLASS_TEMPLATE_P (template)
9253 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9255 bool entering_scope
;
9256 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9257 template (rather than some instantiation thereof) only if
9258 is not nested within some other construct. For example, in
9259 "template <typename T> void f(T) { A<T>::", A<T> is just an
9260 instantiation of A. */
9261 entering_scope
= (template_parm_scope_p ()
9262 && cp_lexer_next_token_is (parser
->lexer
,
9265 = finish_template_type (template, arguments
, entering_scope
);
9269 /* If it's not a class-template or a template-template, it should be
9270 a function-template. */
9271 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9272 || TREE_CODE (template) == OVERLOAD
9273 || BASELINK_P (template)));
9275 template_id
= lookup_template_function (template, arguments
);
9278 /* If parsing tentatively, replace the sequence of tokens that makes
9279 up the template-id with a CPP_TEMPLATE_ID token. That way,
9280 should we re-parse the token stream, we will not have to repeat
9281 the effort required to do the parse, nor will we issue duplicate
9282 error messages about problems during instantiation of the
9286 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
9288 /* Reset the contents of the START_OF_ID token. */
9289 token
->type
= CPP_TEMPLATE_ID
;
9290 /* Retrieve any deferred checks. Do not pop this access checks yet
9291 so the memory will not be reclaimed during token replacing below. */
9292 token
->u
.tree_check_value
= GGC_CNEW (struct tree_check
);
9293 token
->u
.tree_check_value
->value
= template_id
;
9294 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
9295 token
->keyword
= RID_MAX
;
9297 /* Purge all subsequent tokens. */
9298 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
9300 /* ??? Can we actually assume that, if template_id ==
9301 error_mark_node, we will have issued a diagnostic to the
9302 user, as opposed to simply marking the tentative parse as
9304 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
9305 error ("parse error in template argument list");
9308 pop_deferring_access_checks ();
9312 /* Parse a template-name.
9317 The standard should actually say:
9321 operator-function-id
9323 A defect report has been filed about this issue.
9325 A conversion-function-id cannot be a template name because they cannot
9326 be part of a template-id. In fact, looking at this code:
9330 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9331 It is impossible to call a templated conversion-function-id with an
9332 explicit argument list, since the only allowed template parameter is
9333 the type to which it is converting.
9335 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9336 `template' keyword, in a construction like:
9340 In that case `f' is taken to be a template-name, even though there
9341 is no way of knowing for sure.
9343 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9344 name refers to a set of overloaded functions, at least one of which
9345 is a template, or an IDENTIFIER_NODE with the name of the template,
9346 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9347 names are looked up inside uninstantiated templates. */
9350 cp_parser_template_name (cp_parser
* parser
,
9351 bool template_keyword_p
,
9352 bool check_dependency_p
,
9353 bool is_declaration
,
9354 bool *is_identifier
)
9360 /* If the next token is `operator', then we have either an
9361 operator-function-id or a conversion-function-id. */
9362 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
9364 /* We don't know whether we're looking at an
9365 operator-function-id or a conversion-function-id. */
9366 cp_parser_parse_tentatively (parser
);
9367 /* Try an operator-function-id. */
9368 identifier
= cp_parser_operator_function_id (parser
);
9369 /* If that didn't work, try a conversion-function-id. */
9370 if (!cp_parser_parse_definitely (parser
))
9372 cp_parser_error (parser
, "expected template-name");
9373 return error_mark_node
;
9376 /* Look for the identifier. */
9378 identifier
= cp_parser_identifier (parser
);
9380 /* If we didn't find an identifier, we don't have a template-id. */
9381 if (identifier
== error_mark_node
)
9382 return error_mark_node
;
9384 /* If the name immediately followed the `template' keyword, then it
9385 is a template-name. However, if the next token is not `<', then
9386 we do not treat it as a template-name, since it is not being used
9387 as part of a template-id. This enables us to handle constructs
9390 template <typename T> struct S { S(); };
9391 template <typename T> S<T>::S();
9393 correctly. We would treat `S' as a template -- if it were `S<T>'
9394 -- but we do not if there is no `<'. */
9396 if (processing_template_decl
9397 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
9399 /* In a declaration, in a dependent context, we pretend that the
9400 "template" keyword was present in order to improve error
9401 recovery. For example, given:
9403 template <typename T> void f(T::X<int>);
9405 we want to treat "X<int>" as a template-id. */
9407 && !template_keyword_p
9408 && parser
->scope
&& TYPE_P (parser
->scope
)
9409 && check_dependency_p
9410 && dependent_type_p (parser
->scope
)
9411 /* Do not do this for dtors (or ctors), since they never
9412 need the template keyword before their name. */
9413 && !constructor_name_p (identifier
, parser
->scope
))
9415 cp_token_position start
= 0;
9417 /* Explain what went wrong. */
9418 error ("non-template %qD used as template", identifier
);
9419 inform ("use %<%T::template %D%> to indicate that it is a template",
9420 parser
->scope
, identifier
);
9421 /* If parsing tentatively, find the location of the "<" token. */
9422 if (cp_parser_simulate_error (parser
))
9423 start
= cp_lexer_token_position (parser
->lexer
, true);
9424 /* Parse the template arguments so that we can issue error
9425 messages about them. */
9426 cp_lexer_consume_token (parser
->lexer
);
9427 cp_parser_enclosed_template_argument_list (parser
);
9428 /* Skip tokens until we find a good place from which to
9429 continue parsing. */
9430 cp_parser_skip_to_closing_parenthesis (parser
,
9431 /*recovering=*/true,
9433 /*consume_paren=*/false);
9434 /* If parsing tentatively, permanently remove the
9435 template argument list. That will prevent duplicate
9436 error messages from being issued about the missing
9437 "template" keyword. */
9439 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
9441 *is_identifier
= true;
9445 /* If the "template" keyword is present, then there is generally
9446 no point in doing name-lookup, so we just return IDENTIFIER.
9447 But, if the qualifying scope is non-dependent then we can
9448 (and must) do name-lookup normally. */
9449 if (template_keyword_p
9451 || (TYPE_P (parser
->scope
)
9452 && dependent_type_p (parser
->scope
))))
9456 /* Look up the name. */
9457 decl
= cp_parser_lookup_name (parser
, identifier
,
9459 /*is_template=*/false,
9460 /*is_namespace=*/false,
9462 /*ambiguous_decls=*/NULL
);
9463 decl
= maybe_get_template_decl_from_type_decl (decl
);
9465 /* If DECL is a template, then the name was a template-name. */
9466 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9470 tree fn
= NULL_TREE
;
9472 /* The standard does not explicitly indicate whether a name that
9473 names a set of overloaded declarations, some of which are
9474 templates, is a template-name. However, such a name should
9475 be a template-name; otherwise, there is no way to form a
9476 template-id for the overloaded templates. */
9477 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
9478 if (TREE_CODE (fns
) == OVERLOAD
)
9479 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
9480 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
9485 /* The name does not name a template. */
9486 cp_parser_error (parser
, "expected template-name");
9487 return error_mark_node
;
9491 /* If DECL is dependent, and refers to a function, then just return
9492 its name; we will look it up again during template instantiation. */
9493 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
9495 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
9496 if (TYPE_P (scope
) && dependent_type_p (scope
))
9503 /* Parse a template-argument-list.
9505 template-argument-list:
9506 template-argument ... [opt]
9507 template-argument-list , template-argument ... [opt]
9509 Returns a TREE_VEC containing the arguments. */
9512 cp_parser_template_argument_list (cp_parser
* parser
)
9514 tree fixed_args
[10];
9515 unsigned n_args
= 0;
9516 unsigned alloced
= 10;
9517 tree
*arg_ary
= fixed_args
;
9519 bool saved_in_template_argument_list_p
;
9521 bool saved_non_ice_p
;
9523 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
9524 parser
->in_template_argument_list_p
= true;
9525 /* Even if the template-id appears in an integral
9526 constant-expression, the contents of the argument list do
9528 saved_ice_p
= parser
->integral_constant_expression_p
;
9529 parser
->integral_constant_expression_p
= false;
9530 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
9531 parser
->non_integral_constant_expression_p
= false;
9532 /* Parse the arguments. */
9538 /* Consume the comma. */
9539 cp_lexer_consume_token (parser
->lexer
);
9541 /* Parse the template-argument. */
9542 argument
= cp_parser_template_argument (parser
);
9544 /* If the next token is an ellipsis, we're expanding a template
9546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9548 /* Consume the `...' token. */
9549 cp_lexer_consume_token (parser
->lexer
);
9551 /* Make the argument into a TYPE_PACK_EXPANSION or
9552 EXPR_PACK_EXPANSION. */
9553 argument
= make_pack_expansion (argument
);
9556 if (n_args
== alloced
)
9560 if (arg_ary
== fixed_args
)
9562 arg_ary
= XNEWVEC (tree
, alloced
);
9563 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
9566 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
9568 arg_ary
[n_args
++] = argument
;
9570 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
9572 vec
= make_tree_vec (n_args
);
9575 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
9577 if (arg_ary
!= fixed_args
)
9579 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
9580 parser
->integral_constant_expression_p
= saved_ice_p
;
9581 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
9585 /* Parse a template-argument.
9588 assignment-expression
9592 The representation is that of an assignment-expression, type-id, or
9593 id-expression -- except that the qualified id-expression is
9594 evaluated, so that the value returned is either a DECL or an
9597 Although the standard says "assignment-expression", it forbids
9598 throw-expressions or assignments in the template argument.
9599 Therefore, we use "conditional-expression" instead. */
9602 cp_parser_template_argument (cp_parser
* parser
)
9607 bool maybe_type_id
= false;
9611 /* There's really no way to know what we're looking at, so we just
9612 try each alternative in order.
9616 In a template-argument, an ambiguity between a type-id and an
9617 expression is resolved to a type-id, regardless of the form of
9618 the corresponding template-parameter.
9620 Therefore, we try a type-id first. */
9621 cp_parser_parse_tentatively (parser
);
9622 argument
= cp_parser_type_id (parser
);
9623 /* If there was no error parsing the type-id but the next token is a '>>',
9624 we probably found a typo for '> >'. But there are type-id which are
9625 also valid expressions. For instance:
9627 struct X { int operator >> (int); };
9628 template <int V> struct Foo {};
9631 Here 'X()' is a valid type-id of a function type, but the user just
9632 wanted to write the expression "X() >> 5". Thus, we remember that we
9633 found a valid type-id, but we still try to parse the argument as an
9634 expression to see what happens. */
9635 if (!cp_parser_error_occurred (parser
)
9636 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
9638 maybe_type_id
= true;
9639 cp_parser_abort_tentative_parse (parser
);
9643 /* If the next token isn't a `,' or a `>', then this argument wasn't
9644 really finished. This means that the argument is not a valid
9646 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9647 cp_parser_error (parser
, "expected template-argument");
9648 /* If that worked, we're done. */
9649 if (cp_parser_parse_definitely (parser
))
9652 /* We're still not sure what the argument will be. */
9653 cp_parser_parse_tentatively (parser
);
9654 /* Try a template. */
9655 argument
= cp_parser_id_expression (parser
,
9656 /*template_keyword_p=*/false,
9657 /*check_dependency_p=*/true,
9659 /*declarator_p=*/false,
9660 /*optional_p=*/false);
9661 /* If the next token isn't a `,' or a `>', then this argument wasn't
9663 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9664 cp_parser_error (parser
, "expected template-argument");
9665 if (!cp_parser_error_occurred (parser
))
9667 /* Figure out what is being referred to. If the id-expression
9668 was for a class template specialization, then we will have a
9669 TYPE_DECL at this point. There is no need to do name lookup
9670 at this point in that case. */
9671 if (TREE_CODE (argument
) != TYPE_DECL
)
9672 argument
= cp_parser_lookup_name (parser
, argument
,
9674 /*is_template=*/template_p
,
9675 /*is_namespace=*/false,
9676 /*check_dependency=*/true,
9677 /*ambiguous_decls=*/NULL
);
9678 if (TREE_CODE (argument
) != TEMPLATE_DECL
9679 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
9680 cp_parser_error (parser
, "expected template-name");
9682 if (cp_parser_parse_definitely (parser
))
9684 /* It must be a non-type argument. There permitted cases are given
9685 in [temp.arg.nontype]:
9687 -- an integral constant-expression of integral or enumeration
9690 -- the name of a non-type template-parameter; or
9692 -- the name of an object or function with external linkage...
9694 -- the address of an object or function with external linkage...
9696 -- a pointer to member... */
9697 /* Look for a non-type template parameter. */
9698 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
9700 cp_parser_parse_tentatively (parser
);
9701 argument
= cp_parser_primary_expression (parser
,
9704 /*template_arg_p=*/true,
9706 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
9707 || !cp_parser_next_token_ends_template_argument_p (parser
))
9708 cp_parser_simulate_error (parser
);
9709 if (cp_parser_parse_definitely (parser
))
9713 /* If the next token is "&", the argument must be the address of an
9714 object or function with external linkage. */
9715 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
9717 cp_lexer_consume_token (parser
->lexer
);
9718 /* See if we might have an id-expression. */
9719 token
= cp_lexer_peek_token (parser
->lexer
);
9720 if (token
->type
== CPP_NAME
9721 || token
->keyword
== RID_OPERATOR
9722 || token
->type
== CPP_SCOPE
9723 || token
->type
== CPP_TEMPLATE_ID
9724 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
9726 cp_parser_parse_tentatively (parser
);
9727 argument
= cp_parser_primary_expression (parser
,
9730 /*template_arg_p=*/true,
9732 if (cp_parser_error_occurred (parser
)
9733 || !cp_parser_next_token_ends_template_argument_p (parser
))
9734 cp_parser_abort_tentative_parse (parser
);
9737 if (TREE_CODE (argument
) == INDIRECT_REF
)
9739 gcc_assert (REFERENCE_REF_P (argument
));
9740 argument
= TREE_OPERAND (argument
, 0);
9743 if (TREE_CODE (argument
) == VAR_DECL
)
9745 /* A variable without external linkage might still be a
9746 valid constant-expression, so no error is issued here
9747 if the external-linkage check fails. */
9748 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (argument
))
9749 cp_parser_simulate_error (parser
);
9751 else if (is_overloaded_fn (argument
))
9752 /* All overloaded functions are allowed; if the external
9753 linkage test does not pass, an error will be issued
9757 && (TREE_CODE (argument
) == OFFSET_REF
9758 || TREE_CODE (argument
) == SCOPE_REF
))
9759 /* A pointer-to-member. */
9761 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
9764 cp_parser_simulate_error (parser
);
9766 if (cp_parser_parse_definitely (parser
))
9769 argument
= build_x_unary_op (ADDR_EXPR
, argument
);
9774 /* If the argument started with "&", there are no other valid
9775 alternatives at this point. */
9778 cp_parser_error (parser
, "invalid non-type template argument");
9779 return error_mark_node
;
9782 /* If the argument wasn't successfully parsed as a type-id followed
9783 by '>>', the argument can only be a constant expression now.
9784 Otherwise, we try parsing the constant-expression tentatively,
9785 because the argument could really be a type-id. */
9787 cp_parser_parse_tentatively (parser
);
9788 argument
= cp_parser_constant_expression (parser
,
9789 /*allow_non_constant_p=*/false,
9790 /*non_constant_p=*/NULL
);
9791 argument
= fold_non_dependent_expr (argument
);
9794 if (!cp_parser_next_token_ends_template_argument_p (parser
))
9795 cp_parser_error (parser
, "expected template-argument");
9796 if (cp_parser_parse_definitely (parser
))
9798 /* We did our best to parse the argument as a non type-id, but that
9799 was the only alternative that matched (albeit with a '>' after
9800 it). We can assume it's just a typo from the user, and a
9801 diagnostic will then be issued. */
9802 return cp_parser_type_id (parser
);
9805 /* Parse an explicit-instantiation.
9807 explicit-instantiation:
9808 template declaration
9810 Although the standard says `declaration', what it really means is:
9812 explicit-instantiation:
9813 template decl-specifier-seq [opt] declarator [opt] ;
9815 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9816 supposed to be allowed. A defect report has been filed about this
9821 explicit-instantiation:
9822 storage-class-specifier template
9823 decl-specifier-seq [opt] declarator [opt] ;
9824 function-specifier template
9825 decl-specifier-seq [opt] declarator [opt] ; */
9828 cp_parser_explicit_instantiation (cp_parser
* parser
)
9830 int declares_class_or_enum
;
9831 cp_decl_specifier_seq decl_specifiers
;
9832 tree extension_specifier
= NULL_TREE
;
9834 /* Look for an (optional) storage-class-specifier or
9835 function-specifier. */
9836 if (cp_parser_allow_gnu_extensions_p (parser
))
9839 = cp_parser_storage_class_specifier_opt (parser
);
9840 if (!extension_specifier
)
9842 = cp_parser_function_specifier_opt (parser
,
9843 /*decl_specs=*/NULL
);
9846 /* Look for the `template' keyword. */
9847 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9848 /* Let the front end know that we are processing an explicit
9850 begin_explicit_instantiation ();
9851 /* [temp.explicit] says that we are supposed to ignore access
9852 control while processing explicit instantiation directives. */
9853 push_deferring_access_checks (dk_no_check
);
9854 /* Parse a decl-specifier-seq. */
9855 cp_parser_decl_specifier_seq (parser
,
9856 CP_PARSER_FLAGS_OPTIONAL
,
9858 &declares_class_or_enum
);
9859 /* If there was exactly one decl-specifier, and it declared a class,
9860 and there's no declarator, then we have an explicit type
9862 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
9866 type
= check_tag_decl (&decl_specifiers
);
9867 /* Turn access control back on for names used during
9868 template instantiation. */
9869 pop_deferring_access_checks ();
9871 do_type_instantiation (type
, extension_specifier
,
9872 /*complain=*/tf_error
);
9876 cp_declarator
*declarator
;
9879 /* Parse the declarator. */
9881 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9882 /*ctor_dtor_or_conv_p=*/NULL
,
9883 /*parenthesized_p=*/NULL
,
9884 /*member_p=*/false);
9885 if (declares_class_or_enum
& 2)
9886 cp_parser_check_for_definition_in_return_type (declarator
,
9887 decl_specifiers
.type
);
9888 if (declarator
!= cp_error_declarator
)
9890 decl
= grokdeclarator (declarator
, &decl_specifiers
,
9891 NORMAL
, 0, &decl_specifiers
.attributes
);
9892 /* Turn access control back on for names used during
9893 template instantiation. */
9894 pop_deferring_access_checks ();
9895 /* Do the explicit instantiation. */
9896 do_decl_instantiation (decl
, extension_specifier
);
9900 pop_deferring_access_checks ();
9901 /* Skip the body of the explicit instantiation. */
9902 cp_parser_skip_to_end_of_statement (parser
);
9905 /* We're done with the instantiation. */
9906 end_explicit_instantiation ();
9908 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9911 /* Parse an explicit-specialization.
9913 explicit-specialization:
9914 template < > declaration
9916 Although the standard says `declaration', what it really means is:
9918 explicit-specialization:
9919 template <> decl-specifier [opt] init-declarator [opt] ;
9920 template <> function-definition
9921 template <> explicit-specialization
9922 template <> template-declaration */
9925 cp_parser_explicit_specialization (cp_parser
* parser
)
9928 /* Look for the `template' keyword. */
9929 cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'");
9930 /* Look for the `<'. */
9931 cp_parser_require (parser
, CPP_LESS
, "`<'");
9932 /* Look for the `>'. */
9933 cp_parser_require (parser
, CPP_GREATER
, "`>'");
9934 /* We have processed another parameter list. */
9935 ++parser
->num_template_parameter_lists
;
9938 A template ... explicit specialization ... shall not have C
9940 if (current_lang_name
== lang_name_c
)
9942 error ("template specialization with C linkage");
9943 /* Give it C++ linkage to avoid confusing other parts of the
9945 push_lang_context (lang_name_cplusplus
);
9946 need_lang_pop
= true;
9949 need_lang_pop
= false;
9950 /* Let the front end know that we are beginning a specialization. */
9951 if (!begin_specialization ())
9953 end_specialization ();
9954 cp_parser_skip_to_end_of_block_or_statement (parser
);
9958 /* If the next keyword is `template', we need to figure out whether
9959 or not we're looking a template-declaration. */
9960 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
9962 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
9963 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
9964 cp_parser_template_declaration_after_export (parser
,
9965 /*member_p=*/false);
9967 cp_parser_explicit_specialization (parser
);
9970 /* Parse the dependent declaration. */
9971 cp_parser_single_declaration (parser
,
9975 /* We're done with the specialization. */
9976 end_specialization ();
9977 /* For the erroneous case of a template with C linkage, we pushed an
9978 implicit C++ linkage scope; exit that scope now. */
9980 pop_lang_context ();
9981 /* We're done with this parameter list. */
9982 --parser
->num_template_parameter_lists
;
9985 /* Parse a type-specifier.
9988 simple-type-specifier
9991 elaborated-type-specifier
9999 Returns a representation of the type-specifier. For a
10000 class-specifier, enum-specifier, or elaborated-type-specifier, a
10001 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10003 The parser flags FLAGS is used to control type-specifier parsing.
10005 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10006 in a decl-specifier-seq.
10008 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10009 class-specifier, enum-specifier, or elaborated-type-specifier, then
10010 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10011 if a type is declared; 2 if it is defined. Otherwise, it is set to
10014 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10015 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10016 is set to FALSE. */
10019 cp_parser_type_specifier (cp_parser
* parser
,
10020 cp_parser_flags flags
,
10021 cp_decl_specifier_seq
*decl_specs
,
10022 bool is_declaration
,
10023 int* declares_class_or_enum
,
10024 bool* is_cv_qualifier
)
10026 tree type_spec
= NULL_TREE
;
10029 cp_decl_spec ds
= ds_last
;
10031 /* Assume this type-specifier does not declare a new type. */
10032 if (declares_class_or_enum
)
10033 *declares_class_or_enum
= 0;
10034 /* And that it does not specify a cv-qualifier. */
10035 if (is_cv_qualifier
)
10036 *is_cv_qualifier
= false;
10037 /* Peek at the next token. */
10038 token
= cp_lexer_peek_token (parser
->lexer
);
10040 /* If we're looking at a keyword, we can use that to guide the
10041 production we choose. */
10042 keyword
= token
->keyword
;
10046 /* Look for the enum-specifier. */
10047 type_spec
= cp_parser_enum_specifier (parser
);
10048 /* If that worked, we're done. */
10051 if (declares_class_or_enum
)
10052 *declares_class_or_enum
= 2;
10054 cp_parser_set_decl_spec_type (decl_specs
,
10056 /*user_defined_p=*/true);
10060 goto elaborated_type_specifier
;
10062 /* Any of these indicate either a class-specifier, or an
10063 elaborated-type-specifier. */
10067 /* Parse tentatively so that we can back up if we don't find a
10068 class-specifier. */
10069 cp_parser_parse_tentatively (parser
);
10070 /* Look for the class-specifier. */
10071 type_spec
= cp_parser_class_specifier (parser
);
10072 /* If that worked, we're done. */
10073 if (cp_parser_parse_definitely (parser
))
10075 if (declares_class_or_enum
)
10076 *declares_class_or_enum
= 2;
10078 cp_parser_set_decl_spec_type (decl_specs
,
10080 /*user_defined_p=*/true);
10084 /* Fall through. */
10085 elaborated_type_specifier
:
10086 /* We're declaring (not defining) a class or enum. */
10087 if (declares_class_or_enum
)
10088 *declares_class_or_enum
= 1;
10090 /* Fall through. */
10092 /* Look for an elaborated-type-specifier. */
10094 = (cp_parser_elaborated_type_specifier
10096 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
10099 cp_parser_set_decl_spec_type (decl_specs
,
10101 /*user_defined_p=*/true);
10106 if (is_cv_qualifier
)
10107 *is_cv_qualifier
= true;
10112 if (is_cv_qualifier
)
10113 *is_cv_qualifier
= true;
10118 if (is_cv_qualifier
)
10119 *is_cv_qualifier
= true;
10123 /* The `__complex__' keyword is a GNU extension. */
10131 /* Handle simple keywords. */
10136 ++decl_specs
->specs
[(int)ds
];
10137 decl_specs
->any_specifiers_p
= true;
10139 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
10142 /* If we do not already have a type-specifier, assume we are looking
10143 at a simple-type-specifier. */
10144 type_spec
= cp_parser_simple_type_specifier (parser
,
10148 /* If we didn't find a type-specifier, and a type-specifier was not
10149 optional in this context, issue an error message. */
10150 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
10152 cp_parser_error (parser
, "expected type specifier");
10153 return error_mark_node
;
10159 /* Parse a simple-type-specifier.
10161 simple-type-specifier:
10162 :: [opt] nested-name-specifier [opt] type-name
10163 :: [opt] nested-name-specifier template template-id
10178 simple-type-specifier:
10179 __typeof__ unary-expression
10180 __typeof__ ( type-id )
10182 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10183 appropriately updated. */
10186 cp_parser_simple_type_specifier (cp_parser
* parser
,
10187 cp_decl_specifier_seq
*decl_specs
,
10188 cp_parser_flags flags
)
10190 tree type
= NULL_TREE
;
10193 /* Peek at the next token. */
10194 token
= cp_lexer_peek_token (parser
->lexer
);
10196 /* If we're looking at a keyword, things are easy. */
10197 switch (token
->keyword
)
10201 decl_specs
->explicit_char_p
= true;
10202 type
= char_type_node
;
10205 type
= wchar_type_node
;
10208 type
= boolean_type_node
;
10212 ++decl_specs
->specs
[(int) ds_short
];
10213 type
= short_integer_type_node
;
10217 decl_specs
->explicit_int_p
= true;
10218 type
= integer_type_node
;
10222 ++decl_specs
->specs
[(int) ds_long
];
10223 type
= long_integer_type_node
;
10227 ++decl_specs
->specs
[(int) ds_signed
];
10228 type
= integer_type_node
;
10232 ++decl_specs
->specs
[(int) ds_unsigned
];
10233 type
= unsigned_type_node
;
10236 type
= float_type_node
;
10239 type
= double_type_node
;
10242 type
= void_type_node
;
10246 /* Consume the `typeof' token. */
10247 cp_lexer_consume_token (parser
->lexer
);
10248 /* Parse the operand to `typeof'. */
10249 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
10250 /* If it is not already a TYPE, take its type. */
10251 if (!TYPE_P (type
))
10252 type
= finish_typeof (type
);
10255 cp_parser_set_decl_spec_type (decl_specs
, type
,
10256 /*user_defined_p=*/true);
10264 /* If the type-specifier was for a built-in type, we're done. */
10269 /* Record the type. */
10271 && (token
->keyword
!= RID_SIGNED
10272 && token
->keyword
!= RID_UNSIGNED
10273 && token
->keyword
!= RID_SHORT
10274 && token
->keyword
!= RID_LONG
))
10275 cp_parser_set_decl_spec_type (decl_specs
,
10277 /*user_defined=*/false);
10279 decl_specs
->any_specifiers_p
= true;
10281 /* Consume the token. */
10282 id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
10284 /* There is no valid C++ program where a non-template type is
10285 followed by a "<". That usually indicates that the user thought
10286 that the type was a template. */
10287 cp_parser_check_for_invalid_template_id (parser
, type
);
10289 return TYPE_NAME (type
);
10292 /* The type-specifier must be a user-defined type. */
10293 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
10298 /* Don't gobble tokens or issue error messages if this is an
10299 optional type-specifier. */
10300 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
10301 cp_parser_parse_tentatively (parser
);
10303 /* Look for the optional `::' operator. */
10305 = (cp_parser_global_scope_opt (parser
,
10306 /*current_scope_valid_p=*/false)
10308 /* Look for the nested-name specifier. */
10310 = (cp_parser_nested_name_specifier_opt (parser
,
10311 /*typename_keyword_p=*/false,
10312 /*check_dependency_p=*/true,
10314 /*is_declaration=*/false)
10316 /* If we have seen a nested-name-specifier, and the next token
10317 is `template', then we are using the template-id production. */
10319 && cp_parser_optional_template_keyword (parser
))
10321 /* Look for the template-id. */
10322 type
= cp_parser_template_id (parser
,
10323 /*template_keyword_p=*/true,
10324 /*check_dependency_p=*/true,
10325 /*is_declaration=*/false);
10326 /* If the template-id did not name a type, we are out of
10328 if (TREE_CODE (type
) != TYPE_DECL
)
10330 cp_parser_error (parser
, "expected template-id for type");
10334 /* Otherwise, look for a type-name. */
10336 type
= cp_parser_type_name (parser
);
10337 /* Keep track of all name-lookups performed in class scopes. */
10341 && TREE_CODE (type
) == TYPE_DECL
10342 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
10343 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
10344 /* If it didn't work out, we don't have a TYPE. */
10345 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
10346 && !cp_parser_parse_definitely (parser
))
10348 if (type
&& decl_specs
)
10349 cp_parser_set_decl_spec_type (decl_specs
, type
,
10350 /*user_defined=*/true);
10353 /* If we didn't get a type-name, issue an error message. */
10354 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
10356 cp_parser_error (parser
, "expected type-name");
10357 return error_mark_node
;
10360 /* There is no valid C++ program where a non-template type is
10361 followed by a "<". That usually indicates that the user thought
10362 that the type was a template. */
10363 if (type
&& type
!= error_mark_node
)
10365 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10366 If it is, then the '<'...'>' enclose protocol names rather than
10367 template arguments, and so everything is fine. */
10368 if (c_dialect_objc ()
10369 && (objc_is_id (type
) || objc_is_class_name (type
)))
10371 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
10372 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
10374 /* Clobber the "unqualified" type previously entered into
10375 DECL_SPECS with the new, improved protocol-qualified version. */
10377 decl_specs
->type
= qual_type
;
10382 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
));
10388 /* Parse a type-name.
10401 Returns a TYPE_DECL for the type. */
10404 cp_parser_type_name (cp_parser
* parser
)
10409 /* We can't know yet whether it is a class-name or not. */
10410 cp_parser_parse_tentatively (parser
);
10411 /* Try a class-name. */
10412 type_decl
= cp_parser_class_name (parser
,
10413 /*typename_keyword_p=*/false,
10414 /*template_keyword_p=*/false,
10416 /*check_dependency_p=*/true,
10417 /*class_head_p=*/false,
10418 /*is_declaration=*/false);
10419 /* If it's not a class-name, keep looking. */
10420 if (!cp_parser_parse_definitely (parser
))
10422 /* It must be a typedef-name or an enum-name. */
10423 identifier
= cp_parser_identifier (parser
);
10424 if (identifier
== error_mark_node
)
10425 return error_mark_node
;
10427 /* Look up the type-name. */
10428 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
);
10430 if (TREE_CODE (type_decl
) != TYPE_DECL
10431 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
10433 /* See if this is an Objective-C type. */
10434 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
10435 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
10437 type_decl
= TYPE_NAME (type
);
10440 /* Issue an error if we did not find a type-name. */
10441 if (TREE_CODE (type_decl
) != TYPE_DECL
)
10443 if (!cp_parser_simulate_error (parser
))
10444 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
10446 type_decl
= error_mark_node
;
10448 /* Remember that the name was used in the definition of the
10449 current class so that we can check later to see if the
10450 meaning would have been different after the class was
10451 entirely defined. */
10452 else if (type_decl
!= error_mark_node
10454 maybe_note_name_used_in_class (identifier
, type_decl
);
10461 /* Parse an elaborated-type-specifier. Note that the grammar given
10462 here incorporates the resolution to DR68.
10464 elaborated-type-specifier:
10465 class-key :: [opt] nested-name-specifier [opt] identifier
10466 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10467 enum :: [opt] nested-name-specifier [opt] identifier
10468 typename :: [opt] nested-name-specifier identifier
10469 typename :: [opt] nested-name-specifier template [opt]
10474 elaborated-type-specifier:
10475 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10476 class-key attributes :: [opt] nested-name-specifier [opt]
10477 template [opt] template-id
10478 enum attributes :: [opt] nested-name-specifier [opt] identifier
10480 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10481 declared `friend'. If IS_DECLARATION is TRUE, then this
10482 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10483 something is being declared.
10485 Returns the TYPE specified. */
10488 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
10490 bool is_declaration
)
10492 enum tag_types tag_type
;
10494 tree type
= NULL_TREE
;
10495 tree attributes
= NULL_TREE
;
10497 /* See if we're looking at the `enum' keyword. */
10498 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
10500 /* Consume the `enum' token. */
10501 cp_lexer_consume_token (parser
->lexer
);
10502 /* Remember that it's an enumeration type. */
10503 tag_type
= enum_type
;
10504 /* Parse the attributes. */
10505 attributes
= cp_parser_attributes_opt (parser
);
10507 /* Or, it might be `typename'. */
10508 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10511 /* Consume the `typename' token. */
10512 cp_lexer_consume_token (parser
->lexer
);
10513 /* Remember that it's a `typename' type. */
10514 tag_type
= typename_type
;
10515 /* The `typename' keyword is only allowed in templates. */
10516 if (!processing_template_decl
)
10517 pedwarn ("using %<typename%> outside of template");
10519 /* Otherwise it must be a class-key. */
10522 tag_type
= cp_parser_class_key (parser
);
10523 if (tag_type
== none_type
)
10524 return error_mark_node
;
10525 /* Parse the attributes. */
10526 attributes
= cp_parser_attributes_opt (parser
);
10529 /* Look for the `::' operator. */
10530 cp_parser_global_scope_opt (parser
,
10531 /*current_scope_valid_p=*/false);
10532 /* Look for the nested-name-specifier. */
10533 if (tag_type
== typename_type
)
10535 if (!cp_parser_nested_name_specifier (parser
,
10536 /*typename_keyword_p=*/true,
10537 /*check_dependency_p=*/true,
10540 return error_mark_node
;
10543 /* Even though `typename' is not present, the proposed resolution
10544 to Core Issue 180 says that in `class A<T>::B', `B' should be
10545 considered a type-name, even if `A<T>' is dependent. */
10546 cp_parser_nested_name_specifier_opt (parser
,
10547 /*typename_keyword_p=*/true,
10548 /*check_dependency_p=*/true,
10551 /* For everything but enumeration types, consider a template-id.
10552 For an enumeration type, consider only a plain identifier. */
10553 if (tag_type
!= enum_type
)
10555 bool template_p
= false;
10558 /* Allow the `template' keyword. */
10559 template_p
= cp_parser_optional_template_keyword (parser
);
10560 /* If we didn't see `template', we don't know if there's a
10561 template-id or not. */
10563 cp_parser_parse_tentatively (parser
);
10564 /* Parse the template-id. */
10565 decl
= cp_parser_template_id (parser
, template_p
,
10566 /*check_dependency_p=*/true,
10568 /* If we didn't find a template-id, look for an ordinary
10570 if (!template_p
&& !cp_parser_parse_definitely (parser
))
10572 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10573 in effect, then we must assume that, upon instantiation, the
10574 template will correspond to a class. */
10575 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
10576 && tag_type
== typename_type
)
10577 type
= make_typename_type (parser
->scope
, decl
,
10579 /*complain=*/tf_error
);
10581 type
= TREE_TYPE (decl
);
10586 identifier
= cp_parser_identifier (parser
);
10588 if (identifier
== error_mark_node
)
10590 parser
->scope
= NULL_TREE
;
10591 return error_mark_node
;
10594 /* For a `typename', we needn't call xref_tag. */
10595 if (tag_type
== typename_type
10596 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
10597 return cp_parser_make_typename_type (parser
, parser
->scope
,
10599 /* Look up a qualified name in the usual way. */
10604 decl
= cp_parser_lookup_name (parser
, identifier
,
10606 /*is_template=*/false,
10607 /*is_namespace=*/false,
10608 /*check_dependency=*/true,
10609 /*ambiguous_decls=*/NULL
);
10611 /* If we are parsing friend declaration, DECL may be a
10612 TEMPLATE_DECL tree node here. However, we need to check
10613 whether this TEMPLATE_DECL results in valid code. Consider
10614 the following example:
10617 template <class T> class C {};
10620 template <class T> friend class N::C; // #1, valid code
10622 template <class T> class Y {
10623 friend class N::C; // #2, invalid code
10626 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10627 name lookup of `N::C'. We see that friend declaration must
10628 be template for the code to be valid. Note that
10629 processing_template_decl does not work here since it is
10630 always 1 for the above two cases. */
10632 decl
= (cp_parser_maybe_treat_template_as_class
10633 (decl
, /*tag_name_p=*/is_friend
10634 && parser
->num_template_parameter_lists
));
10636 if (TREE_CODE (decl
) != TYPE_DECL
)
10638 cp_parser_diagnose_invalid_type_name (parser
,
10641 return error_mark_node
;
10644 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
10646 bool allow_template
= (parser
->num_template_parameter_lists
10647 || DECL_SELF_REFERENCE_P (decl
));
10648 type
= check_elaborated_type_specifier (tag_type
, decl
,
10651 if (type
== error_mark_node
)
10652 return error_mark_node
;
10655 type
= TREE_TYPE (decl
);
10659 /* An elaborated-type-specifier sometimes introduces a new type and
10660 sometimes names an existing type. Normally, the rule is that it
10661 introduces a new type only if there is not an existing type of
10662 the same name already in scope. For example, given:
10665 void f() { struct S s; }
10667 the `struct S' in the body of `f' is the same `struct S' as in
10668 the global scope; the existing definition is used. However, if
10669 there were no global declaration, this would introduce a new
10670 local class named `S'.
10672 An exception to this rule applies to the following code:
10674 namespace N { struct S; }
10676 Here, the elaborated-type-specifier names a new type
10677 unconditionally; even if there is already an `S' in the
10678 containing scope this declaration names a new type.
10679 This exception only applies if the elaborated-type-specifier
10680 forms the complete declaration:
10684 A declaration consisting solely of `class-key identifier ;' is
10685 either a redeclaration of the name in the current scope or a
10686 forward declaration of the identifier as a class name. It
10687 introduces the name into the current scope.
10689 We are in this situation precisely when the next token is a `;'.
10691 An exception to the exception is that a `friend' declaration does
10692 *not* name a new type; i.e., given:
10694 struct S { friend struct T; };
10696 `T' is not a new type in the scope of `S'.
10698 Also, `new struct S' or `sizeof (struct S)' never results in the
10699 definition of a new type; a new type can only be declared in a
10700 declaration context. */
10706 /* Friends have special name lookup rules. */
10707 ts
= ts_within_enclosing_non_class
;
10708 else if (is_declaration
10709 && cp_lexer_next_token_is (parser
->lexer
,
10711 /* This is a `class-key identifier ;' */
10717 (parser
->num_template_parameter_lists
10718 && (cp_parser_next_token_starts_class_definition_p (parser
)
10719 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
10720 /* An unqualified name was used to reference this type, so
10721 there were no qualifying templates. */
10722 if (!cp_parser_check_template_parameters (parser
,
10723 /*num_templates=*/0))
10724 return error_mark_node
;
10725 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
10729 if (type
== error_mark_node
)
10730 return error_mark_node
;
10732 /* Allow attributes on forward declarations of classes. */
10735 if (TREE_CODE (type
) == TYPENAME_TYPE
)
10736 warning (OPT_Wattributes
,
10737 "attributes ignored on uninstantiated type");
10738 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
10739 && ! processing_explicit_instantiation
)
10740 warning (OPT_Wattributes
,
10741 "attributes ignored on template instantiation");
10742 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
10743 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
10745 warning (OPT_Wattributes
,
10746 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10749 if (tag_type
!= enum_type
)
10750 cp_parser_check_class_key (tag_type
, type
);
10752 /* A "<" cannot follow an elaborated type specifier. If that
10753 happens, the user was probably trying to form a template-id. */
10754 cp_parser_check_for_invalid_template_id (parser
, type
);
10759 /* Parse an enum-specifier.
10762 enum identifier [opt] { enumerator-list [opt] }
10765 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10768 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10769 if the token stream isn't an enum-specifier after all. */
10772 cp_parser_enum_specifier (cp_parser
* parser
)
10778 /* Parse tentatively so that we can back up if we don't find a
10780 cp_parser_parse_tentatively (parser
);
10782 /* Caller guarantees that the current token is 'enum', an identifier
10783 possibly follows, and the token after that is an opening brace.
10784 If we don't have an identifier, fabricate an anonymous name for
10785 the enumeration being defined. */
10786 cp_lexer_consume_token (parser
->lexer
);
10788 attributes
= cp_parser_attributes_opt (parser
);
10790 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
10791 identifier
= cp_parser_identifier (parser
);
10793 identifier
= make_anon_name ();
10795 /* Look for the `{' but don't consume it yet. */
10796 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10797 cp_parser_simulate_error (parser
);
10799 if (!cp_parser_parse_definitely (parser
))
10802 /* Issue an error message if type-definitions are forbidden here. */
10803 if (!cp_parser_check_type_definition (parser
))
10804 type
= error_mark_node
;
10806 /* Create the new type. We do this before consuming the opening
10807 brace so the enum will be recorded as being on the line of its
10808 tag (or the 'enum' keyword, if there is no tag). */
10809 type
= start_enum (identifier
);
10811 /* Consume the opening brace. */
10812 cp_lexer_consume_token (parser
->lexer
);
10814 if (type
== error_mark_node
)
10816 cp_parser_skip_to_end_of_block_or_statement (parser
);
10817 return error_mark_node
;
10820 /* If the next token is not '}', then there are some enumerators. */
10821 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
10822 cp_parser_enumerator_list (parser
, type
);
10824 /* Consume the final '}'. */
10825 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
10827 /* Look for trailing attributes to apply to this enumeration, and
10828 apply them if appropriate. */
10829 if (cp_parser_allow_gnu_extensions_p (parser
))
10831 tree trailing_attr
= cp_parser_attributes_opt (parser
);
10832 cplus_decl_attributes (&type
,
10834 (int) ATTR_FLAG_TYPE_IN_PLACE
);
10837 /* Finish up the enumeration. */
10838 finish_enum (type
);
10843 /* Parse an enumerator-list. The enumerators all have the indicated
10847 enumerator-definition
10848 enumerator-list , enumerator-definition */
10851 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
10855 /* Parse an enumerator-definition. */
10856 cp_parser_enumerator_definition (parser
, type
);
10858 /* If the next token is not a ',', we've reached the end of
10860 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
10862 /* Otherwise, consume the `,' and keep going. */
10863 cp_lexer_consume_token (parser
->lexer
);
10864 /* If the next token is a `}', there is a trailing comma. */
10865 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10867 if (pedantic
&& !in_system_header
)
10868 pedwarn ("comma at end of enumerator list");
10874 /* Parse an enumerator-definition. The enumerator has the indicated
10877 enumerator-definition:
10879 enumerator = constant-expression
10885 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
10890 /* Look for the identifier. */
10891 identifier
= cp_parser_identifier (parser
);
10892 if (identifier
== error_mark_node
)
10895 /* If the next token is an '=', then there is an explicit value. */
10896 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10898 /* Consume the `=' token. */
10899 cp_lexer_consume_token (parser
->lexer
);
10900 /* Parse the value. */
10901 value
= cp_parser_constant_expression (parser
,
10902 /*allow_non_constant_p=*/false,
10908 /* Create the enumerator. */
10909 build_enumerator (identifier
, value
, type
);
10912 /* Parse a namespace-name.
10915 original-namespace-name
10918 Returns the NAMESPACE_DECL for the namespace. */
10921 cp_parser_namespace_name (cp_parser
* parser
)
10924 tree namespace_decl
;
10926 /* Get the name of the namespace. */
10927 identifier
= cp_parser_identifier (parser
);
10928 if (identifier
== error_mark_node
)
10929 return error_mark_node
;
10931 /* Look up the identifier in the currently active scope. Look only
10932 for namespaces, due to:
10934 [basic.lookup.udir]
10936 When looking up a namespace-name in a using-directive or alias
10937 definition, only namespace names are considered.
10941 [basic.lookup.qual]
10943 During the lookup of a name preceding the :: scope resolution
10944 operator, object, function, and enumerator names are ignored.
10946 (Note that cp_parser_class_or_namespace_name only calls this
10947 function if the token after the name is the scope resolution
10949 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
10951 /*is_template=*/false,
10952 /*is_namespace=*/true,
10953 /*check_dependency=*/true,
10954 /*ambiguous_decls=*/NULL
);
10955 /* If it's not a namespace, issue an error. */
10956 if (namespace_decl
== error_mark_node
10957 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
10959 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
10960 error ("%qD is not a namespace-name", identifier
);
10961 cp_parser_error (parser
, "expected namespace-name");
10962 namespace_decl
= error_mark_node
;
10965 return namespace_decl
;
10968 /* Parse a namespace-definition.
10970 namespace-definition:
10971 named-namespace-definition
10972 unnamed-namespace-definition
10974 named-namespace-definition:
10975 original-namespace-definition
10976 extension-namespace-definition
10978 original-namespace-definition:
10979 namespace identifier { namespace-body }
10981 extension-namespace-definition:
10982 namespace original-namespace-name { namespace-body }
10984 unnamed-namespace-definition:
10985 namespace { namespace-body } */
10988 cp_parser_namespace_definition (cp_parser
* parser
)
10990 tree identifier
, attribs
;
10992 /* Look for the `namespace' keyword. */
10993 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
10995 /* Get the name of the namespace. We do not attempt to distinguish
10996 between an original-namespace-definition and an
10997 extension-namespace-definition at this point. The semantic
10998 analysis routines are responsible for that. */
10999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11000 identifier
= cp_parser_identifier (parser
);
11002 identifier
= NULL_TREE
;
11004 /* Parse any specified attributes. */
11005 attribs
= cp_parser_attributes_opt (parser
);
11007 /* Look for the `{' to start the namespace. */
11008 cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'");
11009 /* Start the namespace. */
11010 push_namespace_with_attribs (identifier
, attribs
);
11011 /* Parse the body of the namespace. */
11012 cp_parser_namespace_body (parser
);
11013 /* Finish the namespace. */
11015 /* Look for the final `}'. */
11016 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
11019 /* Parse a namespace-body.
11022 declaration-seq [opt] */
11025 cp_parser_namespace_body (cp_parser
* parser
)
11027 cp_parser_declaration_seq_opt (parser
);
11030 /* Parse a namespace-alias-definition.
11032 namespace-alias-definition:
11033 namespace identifier = qualified-namespace-specifier ; */
11036 cp_parser_namespace_alias_definition (cp_parser
* parser
)
11039 tree namespace_specifier
;
11041 /* Look for the `namespace' keyword. */
11042 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
11043 /* Look for the identifier. */
11044 identifier
= cp_parser_identifier (parser
);
11045 if (identifier
== error_mark_node
)
11047 /* Look for the `=' token. */
11048 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
11049 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11051 error ("%<namespace%> definition is not allowed here");
11052 /* Skip the definition. */
11053 cp_lexer_consume_token (parser
->lexer
);
11054 cp_parser_skip_to_closing_brace (parser
);
11055 cp_lexer_consume_token (parser
->lexer
);
11058 cp_parser_require (parser
, CPP_EQ
, "`='");
11059 /* Look for the qualified-namespace-specifier. */
11060 namespace_specifier
11061 = cp_parser_qualified_namespace_specifier (parser
);
11062 /* Look for the `;' token. */
11063 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11065 /* Register the alias in the symbol table. */
11066 do_namespace_alias (identifier
, namespace_specifier
);
11069 /* Parse a qualified-namespace-specifier.
11071 qualified-namespace-specifier:
11072 :: [opt] nested-name-specifier [opt] namespace-name
11074 Returns a NAMESPACE_DECL corresponding to the specified
11078 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
11080 /* Look for the optional `::'. */
11081 cp_parser_global_scope_opt (parser
,
11082 /*current_scope_valid_p=*/false);
11084 /* Look for the optional nested-name-specifier. */
11085 cp_parser_nested_name_specifier_opt (parser
,
11086 /*typename_keyword_p=*/false,
11087 /*check_dependency_p=*/true,
11089 /*is_declaration=*/true);
11091 return cp_parser_namespace_name (parser
);
11094 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11095 access declaration.
11098 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11099 using :: unqualified-id ;
11101 access-declaration:
11107 cp_parser_using_declaration (cp_parser
* parser
,
11108 bool access_declaration_p
)
11111 bool typename_p
= false;
11112 bool global_scope_p
;
11117 if (access_declaration_p
)
11118 cp_parser_parse_tentatively (parser
);
11121 /* Look for the `using' keyword. */
11122 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
11124 /* Peek at the next token. */
11125 token
= cp_lexer_peek_token (parser
->lexer
);
11126 /* See if it's `typename'. */
11127 if (token
->keyword
== RID_TYPENAME
)
11129 /* Remember that we've seen it. */
11131 /* Consume the `typename' token. */
11132 cp_lexer_consume_token (parser
->lexer
);
11136 /* Look for the optional global scope qualification. */
11138 = (cp_parser_global_scope_opt (parser
,
11139 /*current_scope_valid_p=*/false)
11142 /* If we saw `typename', or didn't see `::', then there must be a
11143 nested-name-specifier present. */
11144 if (typename_p
|| !global_scope_p
)
11145 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
11146 /*check_dependency_p=*/true,
11148 /*is_declaration=*/true);
11149 /* Otherwise, we could be in either of the two productions. In that
11150 case, treat the nested-name-specifier as optional. */
11152 qscope
= cp_parser_nested_name_specifier_opt (parser
,
11153 /*typename_keyword_p=*/false,
11154 /*check_dependency_p=*/true,
11156 /*is_declaration=*/true);
11158 qscope
= global_namespace
;
11160 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
11161 /* Something has already gone wrong; there's no need to parse
11162 further. Since an error has occurred, the return value of
11163 cp_parser_parse_definitely will be false, as required. */
11164 return cp_parser_parse_definitely (parser
);
11166 /* Parse the unqualified-id. */
11167 identifier
= cp_parser_unqualified_id (parser
,
11168 /*template_keyword_p=*/false,
11169 /*check_dependency_p=*/true,
11170 /*declarator_p=*/true,
11171 /*optional_p=*/false);
11173 if (access_declaration_p
)
11175 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11176 cp_parser_simulate_error (parser
);
11177 if (!cp_parser_parse_definitely (parser
))
11181 /* The function we call to handle a using-declaration is different
11182 depending on what scope we are in. */
11183 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
11185 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
11186 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
11187 /* [namespace.udecl]
11189 A using declaration shall not name a template-id. */
11190 error ("a template-id may not appear in a using-declaration");
11193 if (at_class_scope_p ())
11195 /* Create the USING_DECL. */
11196 decl
= do_class_using_decl (parser
->scope
, identifier
);
11197 /* Add it to the list of members in this class. */
11198 finish_member_declaration (decl
);
11202 decl
= cp_parser_lookup_name_simple (parser
, identifier
);
11203 if (decl
== error_mark_node
)
11204 cp_parser_name_lookup_error (parser
, identifier
, decl
, NULL
);
11205 else if (!at_namespace_scope_p ())
11206 do_local_using_decl (decl
, qscope
, identifier
);
11208 do_toplevel_using_decl (decl
, qscope
, identifier
);
11212 /* Look for the final `;'. */
11213 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11218 /* Parse a using-directive.
11221 using namespace :: [opt] nested-name-specifier [opt]
11222 namespace-name ; */
11225 cp_parser_using_directive (cp_parser
* parser
)
11227 tree namespace_decl
;
11230 /* Look for the `using' keyword. */
11231 cp_parser_require_keyword (parser
, RID_USING
, "`using'");
11232 /* And the `namespace' keyword. */
11233 cp_parser_require_keyword (parser
, RID_NAMESPACE
, "`namespace'");
11234 /* Look for the optional `::' operator. */
11235 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
11236 /* And the optional nested-name-specifier. */
11237 cp_parser_nested_name_specifier_opt (parser
,
11238 /*typename_keyword_p=*/false,
11239 /*check_dependency_p=*/true,
11241 /*is_declaration=*/true);
11242 /* Get the namespace being used. */
11243 namespace_decl
= cp_parser_namespace_name (parser
);
11244 /* And any specified attributes. */
11245 attribs
= cp_parser_attributes_opt (parser
);
11246 /* Update the symbol table. */
11247 parse_using_directive (namespace_decl
, attribs
);
11248 /* Look for the final `;'. */
11249 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11252 /* Parse an asm-definition.
11255 asm ( string-literal ) ;
11260 asm volatile [opt] ( string-literal ) ;
11261 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11262 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11263 : asm-operand-list [opt] ) ;
11264 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11265 : asm-operand-list [opt]
11266 : asm-operand-list [opt] ) ; */
11269 cp_parser_asm_definition (cp_parser
* parser
)
11272 tree outputs
= NULL_TREE
;
11273 tree inputs
= NULL_TREE
;
11274 tree clobbers
= NULL_TREE
;
11276 bool volatile_p
= false;
11277 bool extended_p
= false;
11279 /* Look for the `asm' keyword. */
11280 cp_parser_require_keyword (parser
, RID_ASM
, "`asm'");
11281 /* See if the next token is `volatile'. */
11282 if (cp_parser_allow_gnu_extensions_p (parser
)
11283 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
11285 /* Remember that we saw the `volatile' keyword. */
11287 /* Consume the token. */
11288 cp_lexer_consume_token (parser
->lexer
);
11290 /* Look for the opening `('. */
11291 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
11293 /* Look for the string. */
11294 string
= cp_parser_string_literal (parser
, false, false);
11295 if (string
== error_mark_node
)
11297 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11298 /*consume_paren=*/true);
11302 /* If we're allowing GNU extensions, check for the extended assembly
11303 syntax. Unfortunately, the `:' tokens need not be separated by
11304 a space in C, and so, for compatibility, we tolerate that here
11305 too. Doing that means that we have to treat the `::' operator as
11307 if (cp_parser_allow_gnu_extensions_p (parser
)
11308 && parser
->in_function_body
11309 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
11310 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
11312 bool inputs_p
= false;
11313 bool clobbers_p
= false;
11315 /* The extended syntax was used. */
11318 /* Look for outputs. */
11319 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11321 /* Consume the `:'. */
11322 cp_lexer_consume_token (parser
->lexer
);
11323 /* Parse the output-operands. */
11324 if (cp_lexer_next_token_is_not (parser
->lexer
,
11326 && cp_lexer_next_token_is_not (parser
->lexer
,
11328 && cp_lexer_next_token_is_not (parser
->lexer
,
11330 outputs
= cp_parser_asm_operand_list (parser
);
11332 /* If the next token is `::', there are no outputs, and the
11333 next token is the beginning of the inputs. */
11334 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11335 /* The inputs are coming next. */
11338 /* Look for inputs. */
11340 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11342 /* Consume the `:' or `::'. */
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 inputs
= cp_parser_asm_operand_list (parser
);
11351 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
11352 /* The clobbers are coming next. */
11355 /* Look for clobbers. */
11357 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11359 /* Consume the `:' or `::'. */
11360 cp_lexer_consume_token (parser
->lexer
);
11361 /* Parse the clobbers. */
11362 if (cp_lexer_next_token_is_not (parser
->lexer
,
11364 clobbers
= cp_parser_asm_clobber_list (parser
);
11367 /* Look for the closing `)'. */
11368 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
11369 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11370 /*consume_paren=*/true);
11371 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
11373 /* Create the ASM_EXPR. */
11374 if (parser
->in_function_body
)
11376 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
11378 /* If the extended syntax was not used, mark the ASM_EXPR. */
11381 tree temp
= asm_stmt
;
11382 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
11383 temp
= TREE_OPERAND (temp
, 0);
11385 ASM_INPUT_P (temp
) = 1;
11389 cgraph_add_asm_node (string
);
11392 /* Declarators [gram.dcl.decl] */
11394 /* Parse an init-declarator.
11397 declarator initializer [opt]
11402 declarator asm-specification [opt] attributes [opt] initializer [opt]
11404 function-definition:
11405 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11407 decl-specifier-seq [opt] declarator function-try-block
11411 function-definition:
11412 __extension__ function-definition
11414 The DECL_SPECIFIERS apply to this declarator. Returns a
11415 representation of the entity declared. If MEMBER_P is TRUE, then
11416 this declarator appears in a class scope. The new DECL created by
11417 this declarator is returned.
11419 The CHECKS are access checks that should be performed once we know
11420 what entity is being declared (and, therefore, what classes have
11423 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11424 for a function-definition here as well. If the declarator is a
11425 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11426 be TRUE upon return. By that point, the function-definition will
11427 have been completely parsed.
11429 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11433 cp_parser_init_declarator (cp_parser
* parser
,
11434 cp_decl_specifier_seq
*decl_specifiers
,
11435 VEC (deferred_access_check
,gc
)* checks
,
11436 bool function_definition_allowed_p
,
11438 int declares_class_or_enum
,
11439 bool* function_definition_p
)
11442 cp_declarator
*declarator
;
11443 tree prefix_attributes
;
11445 tree asm_specification
;
11447 tree decl
= NULL_TREE
;
11449 bool is_initialized
;
11450 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11451 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11453 enum cpp_ttype initialization_kind
;
11454 bool is_parenthesized_init
= false;
11455 bool is_non_constant_init
;
11456 int ctor_dtor_or_conv_p
;
11458 tree pushed_scope
= NULL
;
11460 /* Gather the attributes that were provided with the
11461 decl-specifiers. */
11462 prefix_attributes
= decl_specifiers
->attributes
;
11464 /* Assume that this is not the declarator for a function
11466 if (function_definition_p
)
11467 *function_definition_p
= false;
11469 /* Defer access checks while parsing the declarator; we cannot know
11470 what names are accessible until we know what is being
11472 resume_deferring_access_checks ();
11474 /* Parse the declarator. */
11476 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11477 &ctor_dtor_or_conv_p
,
11478 /*parenthesized_p=*/NULL
,
11479 /*member_p=*/false);
11480 /* Gather up the deferred checks. */
11481 stop_deferring_access_checks ();
11483 /* If the DECLARATOR was erroneous, there's no need to go
11485 if (declarator
== cp_error_declarator
)
11486 return error_mark_node
;
11488 /* Check that the number of template-parameter-lists is OK. */
11489 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
))
11490 return error_mark_node
;
11492 if (declares_class_or_enum
& 2)
11493 cp_parser_check_for_definition_in_return_type (declarator
,
11494 decl_specifiers
->type
);
11496 /* Figure out what scope the entity declared by the DECLARATOR is
11497 located in. `grokdeclarator' sometimes changes the scope, so
11498 we compute it now. */
11499 scope
= get_scope_of_declarator (declarator
);
11501 /* If we're allowing GNU extensions, look for an asm-specification
11503 if (cp_parser_allow_gnu_extensions_p (parser
))
11505 /* Look for an asm-specification. */
11506 asm_specification
= cp_parser_asm_specification_opt (parser
);
11507 /* And attributes. */
11508 attributes
= cp_parser_attributes_opt (parser
);
11512 asm_specification
= NULL_TREE
;
11513 attributes
= NULL_TREE
;
11516 /* Peek at the next token. */
11517 token
= cp_lexer_peek_token (parser
->lexer
);
11518 /* Check to see if the token indicates the start of a
11519 function-definition. */
11520 if (cp_parser_token_starts_function_definition_p (token
))
11522 if (!function_definition_allowed_p
)
11524 /* If a function-definition should not appear here, issue an
11526 cp_parser_error (parser
,
11527 "a function-definition is not allowed here");
11528 return error_mark_node
;
11532 /* Neither attributes nor an asm-specification are allowed
11533 on a function-definition. */
11534 if (asm_specification
)
11535 error ("an asm-specification is not allowed on a function-definition");
11537 error ("attributes are not allowed on a function-definition");
11538 /* This is a function-definition. */
11539 *function_definition_p
= true;
11541 /* Parse the function definition. */
11543 decl
= cp_parser_save_member_function_body (parser
,
11546 prefix_attributes
);
11549 = (cp_parser_function_definition_from_specifiers_and_declarator
11550 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
11558 Only in function declarations for constructors, destructors, and
11559 type conversions can the decl-specifier-seq be omitted.
11561 We explicitly postpone this check past the point where we handle
11562 function-definitions because we tolerate function-definitions
11563 that are missing their return types in some modes. */
11564 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
11566 cp_parser_error (parser
,
11567 "expected constructor, destructor, or type conversion");
11568 return error_mark_node
;
11571 /* An `=' or an `(' indicates an initializer. */
11572 if (token
->type
== CPP_EQ
11573 || token
->type
== CPP_OPEN_PAREN
)
11575 is_initialized
= true;
11576 initialization_kind
= token
->type
;
11580 /* If the init-declarator isn't initialized and isn't followed by a
11581 `,' or `;', it's not a valid init-declarator. */
11582 if (token
->type
!= CPP_COMMA
11583 && token
->type
!= CPP_SEMICOLON
)
11585 cp_parser_error (parser
, "expected initializer");
11586 return error_mark_node
;
11588 is_initialized
= false;
11589 initialization_kind
= CPP_EOF
;
11592 /* Because start_decl has side-effects, we should only call it if we
11593 know we're going ahead. By this point, we know that we cannot
11594 possibly be looking at any other construct. */
11595 cp_parser_commit_to_tentative_parse (parser
);
11597 /* If the decl specifiers were bad, issue an error now that we're
11598 sure this was intended to be a declarator. Then continue
11599 declaring the variable(s), as int, to try to cut down on further
11601 if (decl_specifiers
->any_specifiers_p
11602 && decl_specifiers
->type
== error_mark_node
)
11604 cp_parser_error (parser
, "invalid type in declaration");
11605 decl_specifiers
->type
= integer_type_node
;
11608 /* Check to see whether or not this declaration is a friend. */
11609 friend_p
= cp_parser_friend_p (decl_specifiers
);
11611 /* Enter the newly declared entry in the symbol table. If we're
11612 processing a declaration in a class-specifier, we wait until
11613 after processing the initializer. */
11616 if (parser
->in_unbraced_linkage_specification_p
)
11617 decl_specifiers
->storage_class
= sc_extern
;
11618 decl
= start_decl (declarator
, decl_specifiers
,
11619 is_initialized
, attributes
, prefix_attributes
,
11623 /* Enter the SCOPE. That way unqualified names appearing in the
11624 initializer will be looked up in SCOPE. */
11625 pushed_scope
= push_scope (scope
);
11627 /* Perform deferred access control checks, now that we know in which
11628 SCOPE the declared entity resides. */
11629 if (!member_p
&& decl
)
11631 tree saved_current_function_decl
= NULL_TREE
;
11633 /* If the entity being declared is a function, pretend that we
11634 are in its scope. If it is a `friend', it may have access to
11635 things that would not otherwise be accessible. */
11636 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11638 saved_current_function_decl
= current_function_decl
;
11639 current_function_decl
= decl
;
11642 /* Perform access checks for template parameters. */
11643 cp_parser_perform_template_parameter_access_checks (checks
);
11645 /* Perform the access control checks for the declarator and the
11646 the decl-specifiers. */
11647 perform_deferred_access_checks ();
11649 /* Restore the saved value. */
11650 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11651 current_function_decl
= saved_current_function_decl
;
11654 /* Parse the initializer. */
11655 initializer
= NULL_TREE
;
11656 is_parenthesized_init
= false;
11657 is_non_constant_init
= true;
11658 if (is_initialized
)
11660 if (function_declarator_p (declarator
))
11662 if (initialization_kind
== CPP_EQ
)
11663 initializer
= cp_parser_pure_specifier (parser
);
11666 /* If the declaration was erroneous, we don't really
11667 know what the user intended, so just silently
11668 consume the initializer. */
11669 if (decl
!= error_mark_node
)
11670 error ("initializer provided for function");
11671 cp_parser_skip_to_closing_parenthesis (parser
,
11672 /*recovering=*/true,
11673 /*or_comma=*/false,
11674 /*consume_paren=*/true);
11678 initializer
= cp_parser_initializer (parser
,
11679 &is_parenthesized_init
,
11680 &is_non_constant_init
);
11683 /* The old parser allows attributes to appear after a parenthesized
11684 initializer. Mark Mitchell proposed removing this functionality
11685 on the GCC mailing lists on 2002-08-13. This parser accepts the
11686 attributes -- but ignores them. */
11687 if (cp_parser_allow_gnu_extensions_p (parser
) && is_parenthesized_init
)
11688 if (cp_parser_attributes_opt (parser
))
11689 warning (OPT_Wattributes
,
11690 "attributes after parenthesized initializer ignored");
11692 /* For an in-class declaration, use `grokfield' to create the
11698 pop_scope (pushed_scope
);
11699 pushed_scope
= false;
11701 decl
= grokfield (declarator
, decl_specifiers
,
11702 initializer
, !is_non_constant_init
,
11703 /*asmspec=*/NULL_TREE
,
11704 prefix_attributes
);
11705 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
11706 cp_parser_save_default_args (parser
, decl
);
11709 /* Finish processing the declaration. But, skip friend
11711 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
11713 cp_finish_decl (decl
,
11714 initializer
, !is_non_constant_init
,
11716 /* If the initializer is in parentheses, then this is
11717 a direct-initialization, which means that an
11718 `explicit' constructor is OK. Otherwise, an
11719 `explicit' constructor cannot be used. */
11720 ((is_parenthesized_init
|| !is_initialized
)
11721 ? 0 : LOOKUP_ONLYCONVERTING
));
11723 if (!friend_p
&& pushed_scope
)
11724 pop_scope (pushed_scope
);
11729 /* Parse a declarator.
11733 ptr-operator declarator
11735 abstract-declarator:
11736 ptr-operator abstract-declarator [opt]
11737 direct-abstract-declarator
11742 attributes [opt] direct-declarator
11743 attributes [opt] ptr-operator declarator
11745 abstract-declarator:
11746 attributes [opt] ptr-operator abstract-declarator [opt]
11747 attributes [opt] direct-abstract-declarator
11749 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11750 detect constructor, destructor or conversion operators. It is set
11751 to -1 if the declarator is a name, and +1 if it is a
11752 function. Otherwise it is set to zero. Usually you just want to
11753 test for >0, but internally the negative value is used.
11755 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11756 a decl-specifier-seq unless it declares a constructor, destructor,
11757 or conversion. It might seem that we could check this condition in
11758 semantic analysis, rather than parsing, but that makes it difficult
11759 to handle something like `f()'. We want to notice that there are
11760 no decl-specifiers, and therefore realize that this is an
11761 expression, not a declaration.)
11763 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11764 the declarator is a direct-declarator of the form "(...)".
11766 MEMBER_P is true iff this declarator is a member-declarator. */
11768 static cp_declarator
*
11769 cp_parser_declarator (cp_parser
* parser
,
11770 cp_parser_declarator_kind dcl_kind
,
11771 int* ctor_dtor_or_conv_p
,
11772 bool* parenthesized_p
,
11776 cp_declarator
*declarator
;
11777 enum tree_code code
;
11778 cp_cv_quals cv_quals
;
11780 tree attributes
= NULL_TREE
;
11782 /* Assume this is not a constructor, destructor, or type-conversion
11784 if (ctor_dtor_or_conv_p
)
11785 *ctor_dtor_or_conv_p
= 0;
11787 if (cp_parser_allow_gnu_extensions_p (parser
))
11788 attributes
= cp_parser_attributes_opt (parser
);
11790 /* Peek at the next token. */
11791 token
= cp_lexer_peek_token (parser
->lexer
);
11793 /* Check for the ptr-operator production. */
11794 cp_parser_parse_tentatively (parser
);
11795 /* Parse the ptr-operator. */
11796 code
= cp_parser_ptr_operator (parser
,
11799 /* If that worked, then we have a ptr-operator. */
11800 if (cp_parser_parse_definitely (parser
))
11802 /* If a ptr-operator was found, then this declarator was not
11804 if (parenthesized_p
)
11805 *parenthesized_p
= true;
11806 /* The dependent declarator is optional if we are parsing an
11807 abstract-declarator. */
11808 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11809 cp_parser_parse_tentatively (parser
);
11811 /* Parse the dependent declarator. */
11812 declarator
= cp_parser_declarator (parser
, dcl_kind
,
11813 /*ctor_dtor_or_conv_p=*/NULL
,
11814 /*parenthesized_p=*/NULL
,
11815 /*member_p=*/false);
11817 /* If we are parsing an abstract-declarator, we must handle the
11818 case where the dependent declarator is absent. */
11819 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
11820 && !cp_parser_parse_definitely (parser
))
11823 /* Build the representation of the ptr-operator. */
11825 declarator
= make_ptrmem_declarator (cv_quals
,
11828 else if (code
== INDIRECT_REF
)
11829 declarator
= make_pointer_declarator (cv_quals
, declarator
);
11831 declarator
= make_reference_declarator (cv_quals
, declarator
);
11833 /* Everything else is a direct-declarator. */
11836 if (parenthesized_p
)
11837 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
11839 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
11840 ctor_dtor_or_conv_p
,
11844 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
11845 declarator
->attributes
= attributes
;
11850 /* Parse a direct-declarator or direct-abstract-declarator.
11854 direct-declarator ( parameter-declaration-clause )
11855 cv-qualifier-seq [opt]
11856 exception-specification [opt]
11857 direct-declarator [ constant-expression [opt] ]
11860 direct-abstract-declarator:
11861 direct-abstract-declarator [opt]
11862 ( parameter-declaration-clause )
11863 cv-qualifier-seq [opt]
11864 exception-specification [opt]
11865 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11866 ( abstract-declarator )
11868 Returns a representation of the declarator. DCL_KIND is
11869 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11870 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11871 we are parsing a direct-declarator. It is
11872 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11873 of ambiguity we prefer an abstract declarator, as per
11874 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11875 cp_parser_declarator. */
11877 static cp_declarator
*
11878 cp_parser_direct_declarator (cp_parser
* parser
,
11879 cp_parser_declarator_kind dcl_kind
,
11880 int* ctor_dtor_or_conv_p
,
11884 cp_declarator
*declarator
= NULL
;
11885 tree scope
= NULL_TREE
;
11886 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
11887 bool saved_in_declarator_p
= parser
->in_declarator_p
;
11889 tree pushed_scope
= NULL_TREE
;
11893 /* Peek at the next token. */
11894 token
= cp_lexer_peek_token (parser
->lexer
);
11895 if (token
->type
== CPP_OPEN_PAREN
)
11897 /* This is either a parameter-declaration-clause, or a
11898 parenthesized declarator. When we know we are parsing a
11899 named declarator, it must be a parenthesized declarator
11900 if FIRST is true. For instance, `(int)' is a
11901 parameter-declaration-clause, with an omitted
11902 direct-abstract-declarator. But `((*))', is a
11903 parenthesized abstract declarator. Finally, when T is a
11904 template parameter `(T)' is a
11905 parameter-declaration-clause, and not a parenthesized
11908 We first try and parse a parameter-declaration-clause,
11909 and then try a nested declarator (if FIRST is true).
11911 It is not an error for it not to be a
11912 parameter-declaration-clause, even when FIRST is
11918 The first is the declaration of a function while the
11919 second is a the definition of a variable, including its
11922 Having seen only the parenthesis, we cannot know which of
11923 these two alternatives should be selected. Even more
11924 complex are examples like:
11929 The former is a function-declaration; the latter is a
11930 variable initialization.
11932 Thus again, we try a parameter-declaration-clause, and if
11933 that fails, we back out and return. */
11935 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
11937 cp_parameter_declarator
*params
;
11938 unsigned saved_num_template_parameter_lists
;
11940 /* In a member-declarator, the only valid interpretation
11941 of a parenthesis is the start of a
11942 parameter-declaration-clause. (It is invalid to
11943 initialize a static data member with a parenthesized
11944 initializer; only the "=" form of initialization is
11947 cp_parser_parse_tentatively (parser
);
11949 /* Consume the `('. */
11950 cp_lexer_consume_token (parser
->lexer
);
11953 /* If this is going to be an abstract declarator, we're
11954 in a declarator and we can't have default args. */
11955 parser
->default_arg_ok_p
= false;
11956 parser
->in_declarator_p
= true;
11959 /* Inside the function parameter list, surrounding
11960 template-parameter-lists do not apply. */
11961 saved_num_template_parameter_lists
11962 = parser
->num_template_parameter_lists
;
11963 parser
->num_template_parameter_lists
= 0;
11965 /* Parse the parameter-declaration-clause. */
11966 params
= cp_parser_parameter_declaration_clause (parser
);
11968 parser
->num_template_parameter_lists
11969 = saved_num_template_parameter_lists
;
11971 /* If all went well, parse the cv-qualifier-seq and the
11972 exception-specification. */
11973 if (member_p
|| cp_parser_parse_definitely (parser
))
11975 cp_cv_quals cv_quals
;
11976 tree exception_specification
;
11978 if (ctor_dtor_or_conv_p
)
11979 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
11981 /* Consume the `)'. */
11982 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
11984 /* Parse the cv-qualifier-seq. */
11985 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
11986 /* And the exception-specification. */
11987 exception_specification
11988 = cp_parser_exception_specification_opt (parser
);
11990 /* Create the function-declarator. */
11991 declarator
= make_call_declarator (declarator
,
11994 exception_specification
);
11995 /* Any subsequent parameter lists are to do with
11996 return type, so are not those of the declared
11998 parser
->default_arg_ok_p
= false;
12000 /* Repeat the main loop. */
12005 /* If this is the first, we can try a parenthesized
12009 bool saved_in_type_id_in_expr_p
;
12011 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12012 parser
->in_declarator_p
= saved_in_declarator_p
;
12014 /* Consume the `('. */
12015 cp_lexer_consume_token (parser
->lexer
);
12016 /* Parse the nested declarator. */
12017 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
12018 parser
->in_type_id_in_expr_p
= true;
12020 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
12021 /*parenthesized_p=*/NULL
,
12023 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
12025 /* Expect a `)'. */
12026 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
12027 declarator
= cp_error_declarator
;
12028 if (declarator
== cp_error_declarator
)
12031 goto handle_declarator
;
12033 /* Otherwise, we must be done. */
12037 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
12038 && token
->type
== CPP_OPEN_SQUARE
)
12040 /* Parse an array-declarator. */
12043 if (ctor_dtor_or_conv_p
)
12044 *ctor_dtor_or_conv_p
= 0;
12047 parser
->default_arg_ok_p
= false;
12048 parser
->in_declarator_p
= true;
12049 /* Consume the `['. */
12050 cp_lexer_consume_token (parser
->lexer
);
12051 /* Peek at the next token. */
12052 token
= cp_lexer_peek_token (parser
->lexer
);
12053 /* If the next token is `]', then there is no
12054 constant-expression. */
12055 if (token
->type
!= CPP_CLOSE_SQUARE
)
12057 bool non_constant_p
;
12060 = cp_parser_constant_expression (parser
,
12061 /*allow_non_constant=*/true,
12063 if (!non_constant_p
)
12064 bounds
= fold_non_dependent_expr (bounds
);
12065 /* Normally, the array bound must be an integral constant
12066 expression. However, as an extension, we allow VLAs
12067 in function scopes. */
12068 else if (!parser
->in_function_body
)
12070 error ("array bound is not an integer constant");
12071 bounds
= error_mark_node
;
12075 bounds
= NULL_TREE
;
12076 /* Look for the closing `]'. */
12077 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'"))
12079 declarator
= cp_error_declarator
;
12083 declarator
= make_array_declarator (declarator
, bounds
);
12085 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
12087 tree qualifying_scope
;
12088 tree unqualified_name
;
12089 special_function_kind sfk
;
12091 bool pack_expansion_p
= false;
12093 /* Parse a declarator-id */
12094 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
12097 cp_parser_parse_tentatively (parser
);
12099 /* If we see an ellipsis, we should be looking at a
12101 if (token
->type
== CPP_ELLIPSIS
)
12103 /* Consume the `...' */
12104 cp_lexer_consume_token (parser
->lexer
);
12106 pack_expansion_p
= true;
12111 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
12112 qualifying_scope
= parser
->scope
;
12117 if (!unqualified_name
&& pack_expansion_p
)
12119 /* Check whether an error occurred. */
12120 okay
= !cp_parser_error_occurred (parser
);
12122 /* We already consumed the ellipsis to mark a
12123 parameter pack, but we have no way to report it,
12124 so abort the tentative parse. We will be exiting
12125 immediately anyway. */
12126 cp_parser_abort_tentative_parse (parser
);
12129 okay
= cp_parser_parse_definitely (parser
);
12132 unqualified_name
= error_mark_node
;
12133 else if (unqualified_name
12134 && (qualifying_scope
12135 || (TREE_CODE (unqualified_name
)
12136 != IDENTIFIER_NODE
)))
12138 cp_parser_error (parser
, "expected unqualified-id");
12139 unqualified_name
= error_mark_node
;
12143 if (!unqualified_name
)
12145 if (unqualified_name
== error_mark_node
)
12147 declarator
= cp_error_declarator
;
12148 pack_expansion_p
= false;
12149 declarator
->parameter_pack_p
= false;
12153 if (qualifying_scope
&& at_namespace_scope_p ()
12154 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
12156 /* In the declaration of a member of a template class
12157 outside of the class itself, the SCOPE will sometimes
12158 be a TYPENAME_TYPE. For example, given:
12160 template <typename T>
12161 int S<T>::R::i = 3;
12163 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12164 this context, we must resolve S<T>::R to an ordinary
12165 type, rather than a typename type.
12167 The reason we normally avoid resolving TYPENAME_TYPEs
12168 is that a specialization of `S' might render
12169 `S<T>::R' not a type. However, if `S' is
12170 specialized, then this `i' will not be used, so there
12171 is no harm in resolving the types here. */
12174 /* Resolve the TYPENAME_TYPE. */
12175 type
= resolve_typename_type (qualifying_scope
,
12176 /*only_current_p=*/false);
12177 /* If that failed, the declarator is invalid. */
12178 if (type
== error_mark_node
)
12179 error ("%<%T::%E%> is not a type",
12180 TYPE_CONTEXT (qualifying_scope
),
12181 TYPE_IDENTIFIER (qualifying_scope
));
12182 qualifying_scope
= type
;
12187 if (unqualified_name
)
12191 if (qualifying_scope
12192 && CLASS_TYPE_P (qualifying_scope
))
12193 class_type
= qualifying_scope
;
12195 class_type
= current_class_type
;
12197 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
12199 tree name_type
= TREE_TYPE (unqualified_name
);
12200 if (class_type
&& same_type_p (name_type
, class_type
))
12202 if (qualifying_scope
12203 && CLASSTYPE_USE_TEMPLATE (name_type
))
12205 error ("invalid use of constructor as a template");
12206 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12207 "name the constructor in a qualified name",
12209 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
12210 class_type
, name_type
);
12211 declarator
= cp_error_declarator
;
12215 unqualified_name
= constructor_name (class_type
);
12219 /* We do not attempt to print the declarator
12220 here because we do not have enough
12221 information about its original syntactic
12223 cp_parser_error (parser
, "invalid declarator");
12224 declarator
= cp_error_declarator
;
12231 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
12232 sfk
= sfk_destructor
;
12233 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
12234 sfk
= sfk_conversion
;
12235 else if (/* There's no way to declare a constructor
12236 for an anonymous type, even if the type
12237 got a name for linkage purposes. */
12238 !TYPE_WAS_ANONYMOUS (class_type
)
12239 && constructor_name_p (unqualified_name
,
12242 unqualified_name
= constructor_name (class_type
);
12243 sfk
= sfk_constructor
;
12246 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
12247 *ctor_dtor_or_conv_p
= -1;
12250 declarator
= make_id_declarator (qualifying_scope
,
12253 declarator
->id_loc
= token
->location
;
12254 declarator
->parameter_pack_p
= pack_expansion_p
;
12256 if (pack_expansion_p
)
12257 maybe_warn_variadic_templates ();
12259 handle_declarator
:;
12260 scope
= get_scope_of_declarator (declarator
);
12262 /* Any names that appear after the declarator-id for a
12263 member are looked up in the containing scope. */
12264 pushed_scope
= push_scope (scope
);
12265 parser
->in_declarator_p
= true;
12266 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
12267 || (declarator
&& declarator
->kind
== cdk_id
))
12268 /* Default args are only allowed on function
12270 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12272 parser
->default_arg_ok_p
= false;
12281 /* For an abstract declarator, we might wind up with nothing at this
12282 point. That's an error; the declarator is not optional. */
12284 cp_parser_error (parser
, "expected declarator");
12286 /* If we entered a scope, we must exit it now. */
12288 pop_scope (pushed_scope
);
12290 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12291 parser
->in_declarator_p
= saved_in_declarator_p
;
12296 /* Parse a ptr-operator.
12299 * cv-qualifier-seq [opt]
12301 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12306 & cv-qualifier-seq [opt]
12308 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12309 Returns ADDR_EXPR if a reference was used. In the case of a
12310 pointer-to-member, *TYPE is filled in with the TYPE containing the
12311 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
12312 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
12313 ERROR_MARK if an error occurred. */
12315 static enum tree_code
12316 cp_parser_ptr_operator (cp_parser
* parser
,
12318 cp_cv_quals
*cv_quals
)
12320 enum tree_code code
= ERROR_MARK
;
12323 /* Assume that it's not a pointer-to-member. */
12325 /* And that there are no cv-qualifiers. */
12326 *cv_quals
= TYPE_UNQUALIFIED
;
12328 /* Peek at the next token. */
12329 token
= cp_lexer_peek_token (parser
->lexer
);
12330 /* If it's a `*' or `&' we have a pointer or reference. */
12331 if (token
->type
== CPP_MULT
|| token
->type
== CPP_AND
)
12333 /* Remember which ptr-operator we were processing. */
12334 code
= (token
->type
== CPP_AND
? ADDR_EXPR
: INDIRECT_REF
);
12336 /* Consume the `*' or `&'. */
12337 cp_lexer_consume_token (parser
->lexer
);
12339 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12340 `&', if we are allowing GNU extensions. (The only qualifier
12341 that can legally appear after `&' is `restrict', but that is
12342 enforced during semantic analysis. */
12343 if (code
== INDIRECT_REF
12344 || cp_parser_allow_gnu_extensions_p (parser
))
12345 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
12349 /* Try the pointer-to-member case. */
12350 cp_parser_parse_tentatively (parser
);
12351 /* Look for the optional `::' operator. */
12352 cp_parser_global_scope_opt (parser
,
12353 /*current_scope_valid_p=*/false);
12354 /* Look for the nested-name specifier. */
12355 cp_parser_nested_name_specifier (parser
,
12356 /*typename_keyword_p=*/false,
12357 /*check_dependency_p=*/true,
12359 /*is_declaration=*/false);
12360 /* If we found it, and the next token is a `*', then we are
12361 indeed looking at a pointer-to-member operator. */
12362 if (!cp_parser_error_occurred (parser
)
12363 && cp_parser_require (parser
, CPP_MULT
, "`*'"))
12365 /* Indicate that the `*' operator was used. */
12366 code
= INDIRECT_REF
;
12368 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
12369 error ("%qD is a namespace", parser
->scope
);
12372 /* The type of which the member is a member is given by the
12374 *type
= parser
->scope
;
12375 /* The next name will not be qualified. */
12376 parser
->scope
= NULL_TREE
;
12377 parser
->qualifying_scope
= NULL_TREE
;
12378 parser
->object_scope
= NULL_TREE
;
12379 /* Look for the optional cv-qualifier-seq. */
12380 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
12383 /* If that didn't work we don't have a ptr-operator. */
12384 if (!cp_parser_parse_definitely (parser
))
12385 cp_parser_error (parser
, "expected ptr-operator");
12391 /* Parse an (optional) cv-qualifier-seq.
12394 cv-qualifier cv-qualifier-seq [opt]
12405 Returns a bitmask representing the cv-qualifiers. */
12408 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
12410 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
12415 cp_cv_quals cv_qualifier
;
12417 /* Peek at the next token. */
12418 token
= cp_lexer_peek_token (parser
->lexer
);
12419 /* See if it's a cv-qualifier. */
12420 switch (token
->keyword
)
12423 cv_qualifier
= TYPE_QUAL_CONST
;
12427 cv_qualifier
= TYPE_QUAL_VOLATILE
;
12431 cv_qualifier
= TYPE_QUAL_RESTRICT
;
12435 cv_qualifier
= TYPE_UNQUALIFIED
;
12442 if (cv_quals
& cv_qualifier
)
12444 error ("duplicate cv-qualifier");
12445 cp_lexer_purge_token (parser
->lexer
);
12449 cp_lexer_consume_token (parser
->lexer
);
12450 cv_quals
|= cv_qualifier
;
12457 /* Parse a declarator-id.
12461 :: [opt] nested-name-specifier [opt] type-name
12463 In the `id-expression' case, the value returned is as for
12464 cp_parser_id_expression if the id-expression was an unqualified-id.
12465 If the id-expression was a qualified-id, then a SCOPE_REF is
12466 returned. The first operand is the scope (either a NAMESPACE_DECL
12467 or TREE_TYPE), but the second is still just a representation of an
12471 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
12474 /* The expression must be an id-expression. Assume that qualified
12475 names are the names of types so that:
12478 int S<T>::R::i = 3;
12480 will work; we must treat `S<T>::R' as the name of a type.
12481 Similarly, assume that qualified names are templates, where
12485 int S<T>::R<T>::i = 3;
12488 id
= cp_parser_id_expression (parser
,
12489 /*template_keyword_p=*/false,
12490 /*check_dependency_p=*/false,
12491 /*template_p=*/NULL
,
12492 /*declarator_p=*/true,
12494 if (id
&& BASELINK_P (id
))
12495 id
= BASELINK_FUNCTIONS (id
);
12499 /* Parse a type-id.
12502 type-specifier-seq abstract-declarator [opt]
12504 Returns the TYPE specified. */
12507 cp_parser_type_id (cp_parser
* parser
)
12509 cp_decl_specifier_seq type_specifier_seq
;
12510 cp_declarator
*abstract_declarator
;
12512 /* Parse the type-specifier-seq. */
12513 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
12514 &type_specifier_seq
);
12515 if (type_specifier_seq
.type
== error_mark_node
)
12516 return error_mark_node
;
12518 /* There might or might not be an abstract declarator. */
12519 cp_parser_parse_tentatively (parser
);
12520 /* Look for the declarator. */
12521 abstract_declarator
12522 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
12523 /*parenthesized_p=*/NULL
,
12524 /*member_p=*/false);
12525 /* Check to see if there really was a declarator. */
12526 if (!cp_parser_parse_definitely (parser
))
12527 abstract_declarator
= NULL
;
12529 return groktypename (&type_specifier_seq
, abstract_declarator
);
12532 /* Parse a type-specifier-seq.
12534 type-specifier-seq:
12535 type-specifier type-specifier-seq [opt]
12539 type-specifier-seq:
12540 attributes type-specifier-seq [opt]
12542 If IS_CONDITION is true, we are at the start of a "condition",
12543 e.g., we've just seen "if (".
12545 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12548 cp_parser_type_specifier_seq (cp_parser
* parser
,
12550 cp_decl_specifier_seq
*type_specifier_seq
)
12552 bool seen_type_specifier
= false;
12553 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
12555 /* Clear the TYPE_SPECIFIER_SEQ. */
12556 clear_decl_specs (type_specifier_seq
);
12558 /* Parse the type-specifiers and attributes. */
12561 tree type_specifier
;
12562 bool is_cv_qualifier
;
12564 /* Check for attributes first. */
12565 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
12567 type_specifier_seq
->attributes
=
12568 chainon (type_specifier_seq
->attributes
,
12569 cp_parser_attributes_opt (parser
));
12573 /* Look for the type-specifier. */
12574 type_specifier
= cp_parser_type_specifier (parser
,
12576 type_specifier_seq
,
12577 /*is_declaration=*/false,
12580 if (!type_specifier
)
12582 /* If the first type-specifier could not be found, this is not a
12583 type-specifier-seq at all. */
12584 if (!seen_type_specifier
)
12586 cp_parser_error (parser
, "expected type-specifier");
12587 type_specifier_seq
->type
= error_mark_node
;
12590 /* If subsequent type-specifiers could not be found, the
12591 type-specifier-seq is complete. */
12595 seen_type_specifier
= true;
12596 /* The standard says that a condition can be:
12598 type-specifier-seq declarator = assignment-expression
12605 we should treat the "S" as a declarator, not as a
12606 type-specifier. The standard doesn't say that explicitly for
12607 type-specifier-seq, but it does say that for
12608 decl-specifier-seq in an ordinary declaration. Perhaps it
12609 would be clearer just to allow a decl-specifier-seq here, and
12610 then add a semantic restriction that if any decl-specifiers
12611 that are not type-specifiers appear, the program is invalid. */
12612 if (is_condition
&& !is_cv_qualifier
)
12613 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
12616 cp_parser_check_decl_spec (type_specifier_seq
);
12619 /* Parse a parameter-declaration-clause.
12621 parameter-declaration-clause:
12622 parameter-declaration-list [opt] ... [opt]
12623 parameter-declaration-list , ...
12625 Returns a representation for the parameter declarations. A return
12626 value of NULL indicates a parameter-declaration-clause consisting
12627 only of an ellipsis. */
12629 static cp_parameter_declarator
*
12630 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
12632 cp_parameter_declarator
*parameters
;
12637 /* Peek at the next token. */
12638 token
= cp_lexer_peek_token (parser
->lexer
);
12639 /* Check for trivial parameter-declaration-clauses. */
12640 if (token
->type
== CPP_ELLIPSIS
)
12642 /* Consume the `...' token. */
12643 cp_lexer_consume_token (parser
->lexer
);
12646 else if (token
->type
== CPP_CLOSE_PAREN
)
12647 /* There are no parameters. */
12649 #ifndef NO_IMPLICIT_EXTERN_C
12650 if (in_system_header
&& current_class_type
== NULL
12651 && current_lang_name
== lang_name_c
)
12655 return no_parameters
;
12657 /* Check for `(void)', too, which is a special case. */
12658 else if (token
->keyword
== RID_VOID
12659 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
12660 == CPP_CLOSE_PAREN
))
12662 /* Consume the `void' token. */
12663 cp_lexer_consume_token (parser
->lexer
);
12664 /* There are no parameters. */
12665 return no_parameters
;
12668 /* Parse the parameter-declaration-list. */
12669 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
12670 /* If a parse error occurred while parsing the
12671 parameter-declaration-list, then the entire
12672 parameter-declaration-clause is erroneous. */
12676 /* Peek at the next token. */
12677 token
= cp_lexer_peek_token (parser
->lexer
);
12678 /* If it's a `,', the clause should terminate with an ellipsis. */
12679 if (token
->type
== CPP_COMMA
)
12681 /* Consume the `,'. */
12682 cp_lexer_consume_token (parser
->lexer
);
12683 /* Expect an ellipsis. */
12685 = (cp_parser_require (parser
, CPP_ELLIPSIS
, "`...'") != NULL
);
12687 /* It might also be `...' if the optional trailing `,' was
12689 else if (token
->type
== CPP_ELLIPSIS
)
12691 /* Consume the `...' token. */
12692 cp_lexer_consume_token (parser
->lexer
);
12693 /* And remember that we saw it. */
12697 ellipsis_p
= false;
12699 /* Finish the parameter list. */
12700 if (parameters
&& ellipsis_p
)
12701 parameters
->ellipsis_p
= true;
12706 /* Parse a parameter-declaration-list.
12708 parameter-declaration-list:
12709 parameter-declaration
12710 parameter-declaration-list , parameter-declaration
12712 Returns a representation of the parameter-declaration-list, as for
12713 cp_parser_parameter_declaration_clause. However, the
12714 `void_list_node' is never appended to the list. Upon return,
12715 *IS_ERROR will be true iff an error occurred. */
12717 static cp_parameter_declarator
*
12718 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
12720 cp_parameter_declarator
*parameters
= NULL
;
12721 cp_parameter_declarator
**tail
= ¶meters
;
12722 bool saved_in_unbraced_linkage_specification_p
;
12724 /* Assume all will go well. */
12726 /* The special considerations that apply to a function within an
12727 unbraced linkage specifications do not apply to the parameters
12728 to the function. */
12729 saved_in_unbraced_linkage_specification_p
12730 = parser
->in_unbraced_linkage_specification_p
;
12731 parser
->in_unbraced_linkage_specification_p
= false;
12733 /* Look for more parameters. */
12736 cp_parameter_declarator
*parameter
;
12737 bool parenthesized_p
;
12738 /* Parse the parameter. */
12740 = cp_parser_parameter_declaration (parser
,
12741 /*template_parm_p=*/false,
12744 /* If a parse error occurred parsing the parameter declaration,
12745 then the entire parameter-declaration-list is erroneous. */
12752 /* Add the new parameter to the list. */
12754 tail
= ¶meter
->next
;
12756 /* Peek at the next token. */
12757 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
12758 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
12759 /* These are for Objective-C++ */
12760 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
12761 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12762 /* The parameter-declaration-list is complete. */
12764 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12768 /* Peek at the next token. */
12769 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12770 /* If it's an ellipsis, then the list is complete. */
12771 if (token
->type
== CPP_ELLIPSIS
)
12773 /* Otherwise, there must be more parameters. Consume the
12775 cp_lexer_consume_token (parser
->lexer
);
12776 /* When parsing something like:
12778 int i(float f, double d)
12780 we can tell after seeing the declaration for "f" that we
12781 are not looking at an initialization of a variable "i",
12782 but rather at the declaration of a function "i".
12784 Due to the fact that the parsing of template arguments
12785 (as specified to a template-id) requires backtracking we
12786 cannot use this technique when inside a template argument
12788 if (!parser
->in_template_argument_list_p
12789 && !parser
->in_type_id_in_expr_p
12790 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12791 /* However, a parameter-declaration of the form
12792 "foat(f)" (which is a valid declaration of a
12793 parameter "f") can also be interpreted as an
12794 expression (the conversion of "f" to "float"). */
12795 && !parenthesized_p
)
12796 cp_parser_commit_to_tentative_parse (parser
);
12800 cp_parser_error (parser
, "expected %<,%> or %<...%>");
12801 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
12802 cp_parser_skip_to_closing_parenthesis (parser
,
12803 /*recovering=*/true,
12804 /*or_comma=*/false,
12805 /*consume_paren=*/false);
12810 parser
->in_unbraced_linkage_specification_p
12811 = saved_in_unbraced_linkage_specification_p
;
12816 /* Parse a parameter declaration.
12818 parameter-declaration:
12819 decl-specifier-seq ... [opt] declarator
12820 decl-specifier-seq declarator = assignment-expression
12821 decl-specifier-seq ... [opt] abstract-declarator [opt]
12822 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12824 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12825 declares a template parameter. (In that case, a non-nested `>'
12826 token encountered during the parsing of the assignment-expression
12827 is not interpreted as a greater-than operator.)
12829 Returns a representation of the parameter, or NULL if an error
12830 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12831 true iff the declarator is of the form "(p)". */
12833 static cp_parameter_declarator
*
12834 cp_parser_parameter_declaration (cp_parser
*parser
,
12835 bool template_parm_p
,
12836 bool *parenthesized_p
)
12838 int declares_class_or_enum
;
12839 bool greater_than_is_operator_p
;
12840 cp_decl_specifier_seq decl_specifiers
;
12841 cp_declarator
*declarator
;
12842 tree default_argument
;
12844 const char *saved_message
;
12846 /* In a template parameter, `>' is not an operator.
12850 When parsing a default template-argument for a non-type
12851 template-parameter, the first non-nested `>' is taken as the end
12852 of the template parameter-list rather than a greater-than
12854 greater_than_is_operator_p
= !template_parm_p
;
12856 /* Type definitions may not appear in parameter types. */
12857 saved_message
= parser
->type_definition_forbidden_message
;
12858 parser
->type_definition_forbidden_message
12859 = "types may not be defined in parameter types";
12861 /* Parse the declaration-specifiers. */
12862 cp_parser_decl_specifier_seq (parser
,
12863 CP_PARSER_FLAGS_NONE
,
12865 &declares_class_or_enum
);
12866 /* If an error occurred, there's no reason to attempt to parse the
12867 rest of the declaration. */
12868 if (cp_parser_error_occurred (parser
))
12870 parser
->type_definition_forbidden_message
= saved_message
;
12874 /* Peek at the next token. */
12875 token
= cp_lexer_peek_token (parser
->lexer
);
12877 /* If the next token is a `)', `,', `=', `>', or `...', then there
12878 is no declarator. However, when variadic templates are enabled,
12879 there may be a declarator following `...'. */
12880 if (token
->type
== CPP_CLOSE_PAREN
12881 || token
->type
== CPP_COMMA
12882 || token
->type
== CPP_EQ
12883 || token
->type
== CPP_GREATER
)
12886 if (parenthesized_p
)
12887 *parenthesized_p
= false;
12889 /* Otherwise, there should be a declarator. */
12892 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
12893 parser
->default_arg_ok_p
= false;
12895 /* After seeing a decl-specifier-seq, if the next token is not a
12896 "(", there is no possibility that the code is a valid
12897 expression. Therefore, if parsing tentatively, we commit at
12899 if (!parser
->in_template_argument_list_p
12900 /* In an expression context, having seen:
12904 we cannot be sure whether we are looking at a
12905 function-type (taking a "char" as a parameter) or a cast
12906 of some object of type "char" to "int". */
12907 && !parser
->in_type_id_in_expr_p
12908 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
12909 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
12910 cp_parser_commit_to_tentative_parse (parser
);
12911 /* Parse the declarator. */
12912 declarator
= cp_parser_declarator (parser
,
12913 CP_PARSER_DECLARATOR_EITHER
,
12914 /*ctor_dtor_or_conv_p=*/NULL
,
12916 /*member_p=*/false);
12917 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
12918 /* After the declarator, allow more attributes. */
12919 decl_specifiers
.attributes
12920 = chainon (decl_specifiers
.attributes
,
12921 cp_parser_attributes_opt (parser
));
12924 /* If the next token is an ellipsis, and the type of the declarator
12925 contains parameter packs but it is not a TYPE_PACK_EXPANSION, then
12926 we actually have a parameter pack expansion expression. Otherwise,
12927 leave the ellipsis for a C-style variadic function. */
12928 token
= cp_lexer_peek_token (parser
->lexer
);
12929 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12931 tree type
= decl_specifiers
.type
;
12934 type
= TREE_TYPE (type
);
12936 if (TREE_CODE (type
) != TYPE_PACK_EXPANSION
12937 && (!declarator
|| !declarator
->parameter_pack_p
)
12938 && uses_parameter_packs (type
))
12940 /* Consume the `...'. */
12941 cp_lexer_consume_token (parser
->lexer
);
12942 maybe_warn_variadic_templates ();
12944 /* Build a pack expansion type */
12946 declarator
->parameter_pack_p
= true;
12948 decl_specifiers
.type
= make_pack_expansion (type
);
12952 /* The restriction on defining new types applies only to the type
12953 of the parameter, not to the default argument. */
12954 parser
->type_definition_forbidden_message
= saved_message
;
12956 /* If the next token is `=', then process a default argument. */
12957 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12959 bool saved_greater_than_is_operator_p
;
12960 /* Consume the `='. */
12961 cp_lexer_consume_token (parser
->lexer
);
12963 /* If we are defining a class, then the tokens that make up the
12964 default argument must be saved and processed later. */
12965 if (!template_parm_p
&& at_class_scope_p ()
12966 && TYPE_BEING_DEFINED (current_class_type
))
12968 unsigned depth
= 0;
12969 cp_token
*first_token
;
12972 /* Add tokens until we have processed the entire default
12973 argument. We add the range [first_token, token). */
12974 first_token
= cp_lexer_peek_token (parser
->lexer
);
12979 /* Peek at the next token. */
12980 token
= cp_lexer_peek_token (parser
->lexer
);
12981 /* What we do depends on what token we have. */
12982 switch (token
->type
)
12984 /* In valid code, a default argument must be
12985 immediately followed by a `,' `)', or `...'. */
12987 case CPP_CLOSE_PAREN
:
12989 /* If we run into a non-nested `;', `}', or `]',
12990 then the code is invalid -- but the default
12991 argument is certainly over. */
12992 case CPP_SEMICOLON
:
12993 case CPP_CLOSE_BRACE
:
12994 case CPP_CLOSE_SQUARE
:
12997 /* Update DEPTH, if necessary. */
12998 else if (token
->type
== CPP_CLOSE_PAREN
12999 || token
->type
== CPP_CLOSE_BRACE
13000 || token
->type
== CPP_CLOSE_SQUARE
)
13004 case CPP_OPEN_PAREN
:
13005 case CPP_OPEN_SQUARE
:
13006 case CPP_OPEN_BRACE
:
13011 /* If we see a non-nested `>', and `>' is not an
13012 operator, then it marks the end of the default
13014 if (!depth
&& !greater_than_is_operator_p
)
13018 /* If we run out of tokens, issue an error message. */
13020 case CPP_PRAGMA_EOL
:
13021 error ("file ends in default argument");
13027 /* In these cases, we should look for template-ids.
13028 For example, if the default argument is
13029 `X<int, double>()', we need to do name lookup to
13030 figure out whether or not `X' is a template; if
13031 so, the `,' does not end the default argument.
13033 That is not yet done. */
13040 /* If we've reached the end, stop. */
13044 /* Add the token to the token block. */
13045 token
= cp_lexer_consume_token (parser
->lexer
);
13048 /* Create a DEFAULT_ARG to represented the unparsed default
13050 default_argument
= make_node (DEFAULT_ARG
);
13051 DEFARG_TOKENS (default_argument
)
13052 = cp_token_cache_new (first_token
, token
);
13053 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
13055 /* Outside of a class definition, we can just parse the
13056 assignment-expression. */
13059 bool saved_local_variables_forbidden_p
;
13061 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13063 saved_greater_than_is_operator_p
13064 = parser
->greater_than_is_operator_p
;
13065 parser
->greater_than_is_operator_p
= greater_than_is_operator_p
;
13066 /* Local variable names (and the `this' keyword) may not
13067 appear in a default argument. */
13068 saved_local_variables_forbidden_p
13069 = parser
->local_variables_forbidden_p
;
13070 parser
->local_variables_forbidden_p
= true;
13071 /* The default argument expression may cause implicitly
13072 defined member functions to be synthesized, which will
13073 result in garbage collection. We must treat this
13074 situation as if we were within the body of function so as
13075 to avoid collecting live data on the stack. */
13077 /* Parse the assignment-expression. */
13078 if (template_parm_p
)
13079 push_deferring_access_checks (dk_no_deferred
);
13081 = cp_parser_assignment_expression (parser
, /*cast_p=*/false);
13082 if (template_parm_p
)
13083 pop_deferring_access_checks ();
13084 /* Restore saved state. */
13086 parser
->greater_than_is_operator_p
13087 = saved_greater_than_is_operator_p
;
13088 parser
->local_variables_forbidden_p
13089 = saved_local_variables_forbidden_p
;
13091 if (!parser
->default_arg_ok_p
)
13093 if (!flag_pedantic_errors
)
13094 warning (0, "deprecated use of default argument for parameter of non-function");
13097 error ("default arguments are only permitted for function parameters");
13098 default_argument
= NULL_TREE
;
13103 default_argument
= NULL_TREE
;
13105 return make_parameter_declarator (&decl_specifiers
,
13110 /* Parse a function-body.
13113 compound_statement */
13116 cp_parser_function_body (cp_parser
*parser
)
13118 cp_parser_compound_statement (parser
, NULL
, false);
13121 /* Parse a ctor-initializer-opt followed by a function-body. Return
13122 true if a ctor-initializer was present. */
13125 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
13128 bool ctor_initializer_p
;
13130 /* Begin the function body. */
13131 body
= begin_function_body ();
13132 /* Parse the optional ctor-initializer. */
13133 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
13134 /* Parse the function-body. */
13135 cp_parser_function_body (parser
);
13136 /* Finish the function body. */
13137 finish_function_body (body
);
13139 return ctor_initializer_p
;
13142 /* Parse an initializer.
13145 = initializer-clause
13146 ( expression-list )
13148 Returns an expression representing the initializer. If no
13149 initializer is present, NULL_TREE is returned.
13151 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13152 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13153 set to FALSE if there is no initializer present. If there is an
13154 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13155 is set to true; otherwise it is set to false. */
13158 cp_parser_initializer (cp_parser
* parser
, bool* is_parenthesized_init
,
13159 bool* non_constant_p
)
13164 /* Peek at the next token. */
13165 token
= cp_lexer_peek_token (parser
->lexer
);
13167 /* Let our caller know whether or not this initializer was
13169 *is_parenthesized_init
= (token
->type
== CPP_OPEN_PAREN
);
13170 /* Assume that the initializer is constant. */
13171 *non_constant_p
= false;
13173 if (token
->type
== CPP_EQ
)
13175 /* Consume the `='. */
13176 cp_lexer_consume_token (parser
->lexer
);
13177 /* Parse the initializer-clause. */
13178 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
13180 else if (token
->type
== CPP_OPEN_PAREN
)
13181 init
= cp_parser_parenthesized_expression_list (parser
, false,
13183 /*allow_expansion_p=*/true,
13187 /* Anything else is an error. */
13188 cp_parser_error (parser
, "expected initializer");
13189 init
= error_mark_node
;
13195 /* Parse an initializer-clause.
13197 initializer-clause:
13198 assignment-expression
13199 { initializer-list , [opt] }
13202 Returns an expression representing the initializer.
13204 If the `assignment-expression' production is used the value
13205 returned is simply a representation for the expression.
13207 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13208 the elements of the initializer-list (or NULL, if the last
13209 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13210 NULL_TREE. There is no way to detect whether or not the optional
13211 trailing `,' was provided. NON_CONSTANT_P is as for
13212 cp_parser_initializer. */
13215 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
13219 /* Assume the expression is constant. */
13220 *non_constant_p
= false;
13222 /* If it is not a `{', then we are looking at an
13223 assignment-expression. */
13224 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13227 = cp_parser_constant_expression (parser
,
13228 /*allow_non_constant_p=*/true,
13230 if (!*non_constant_p
)
13231 initializer
= fold_non_dependent_expr (initializer
);
13235 /* Consume the `{' token. */
13236 cp_lexer_consume_token (parser
->lexer
);
13237 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13238 initializer
= make_node (CONSTRUCTOR
);
13239 /* If it's not a `}', then there is a non-trivial initializer. */
13240 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
13242 /* Parse the initializer list. */
13243 CONSTRUCTOR_ELTS (initializer
)
13244 = cp_parser_initializer_list (parser
, non_constant_p
);
13245 /* A trailing `,' token is allowed. */
13246 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13247 cp_lexer_consume_token (parser
->lexer
);
13249 /* Now, there should be a trailing `}'. */
13250 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13253 return initializer
;
13256 /* Parse an initializer-list.
13259 initializer-clause ... [opt]
13260 initializer-list , initializer-clause ... [opt]
13265 identifier : initializer-clause
13266 initializer-list, identifier : initializer-clause
13268 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13269 for the initializer. If the INDEX of the elt is non-NULL, it is the
13270 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13271 as for cp_parser_initializer. */
13273 static VEC(constructor_elt
,gc
) *
13274 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
13276 VEC(constructor_elt
,gc
) *v
= NULL
;
13278 /* Assume all of the expressions are constant. */
13279 *non_constant_p
= false;
13281 /* Parse the rest of the list. */
13287 bool clause_non_constant_p
;
13289 /* If the next token is an identifier and the following one is a
13290 colon, we are looking at the GNU designated-initializer
13292 if (cp_parser_allow_gnu_extensions_p (parser
)
13293 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
13294 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
13296 /* Warn the user that they are using an extension. */
13298 pedwarn ("ISO C++ does not allow designated initializers");
13299 /* Consume the identifier. */
13300 identifier
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13301 /* Consume the `:'. */
13302 cp_lexer_consume_token (parser
->lexer
);
13305 identifier
= NULL_TREE
;
13307 /* Parse the initializer. */
13308 initializer
= cp_parser_initializer_clause (parser
,
13309 &clause_non_constant_p
);
13310 /* If any clause is non-constant, so is the entire initializer. */
13311 if (clause_non_constant_p
)
13312 *non_constant_p
= true;
13314 /* If we have an ellipsis, this is an initializer pack
13316 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13318 /* Consume the `...'. */
13319 cp_lexer_consume_token (parser
->lexer
);
13321 /* Turn the initializer into an initializer expansion. */
13322 initializer
= make_pack_expansion (initializer
);
13325 /* Add it to the vector. */
13326 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
13328 /* If the next token is not a comma, we have reached the end of
13330 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13333 /* Peek at the next token. */
13334 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13335 /* If the next token is a `}', then we're still done. An
13336 initializer-clause can have a trailing `,' after the
13337 initializer-list and before the closing `}'. */
13338 if (token
->type
== CPP_CLOSE_BRACE
)
13341 /* Consume the `,' token. */
13342 cp_lexer_consume_token (parser
->lexer
);
13348 /* Classes [gram.class] */
13350 /* Parse a class-name.
13356 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13357 to indicate that names looked up in dependent types should be
13358 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13359 keyword has been used to indicate that the name that appears next
13360 is a template. TAG_TYPE indicates the explicit tag given before
13361 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13362 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13363 is the class being defined in a class-head.
13365 Returns the TYPE_DECL representing the class. */
13368 cp_parser_class_name (cp_parser
*parser
,
13369 bool typename_keyword_p
,
13370 bool template_keyword_p
,
13371 enum tag_types tag_type
,
13372 bool check_dependency_p
,
13374 bool is_declaration
)
13381 /* All class-names start with an identifier. */
13382 token
= cp_lexer_peek_token (parser
->lexer
);
13383 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
13385 cp_parser_error (parser
, "expected class-name");
13386 return error_mark_node
;
13389 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13390 to a template-id, so we save it here. */
13391 scope
= parser
->scope
;
13392 if (scope
== error_mark_node
)
13393 return error_mark_node
;
13395 /* Any name names a type if we're following the `typename' keyword
13396 in a qualified name where the enclosing scope is type-dependent. */
13397 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
13398 && dependent_type_p (scope
));
13399 /* Handle the common case (an identifier, but not a template-id)
13401 if (token
->type
== CPP_NAME
13402 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
13404 cp_token
*identifier_token
;
13408 /* Look for the identifier. */
13409 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
13410 ambiguous_p
= identifier_token
->ambiguous_p
;
13411 identifier
= cp_parser_identifier (parser
);
13412 /* If the next token isn't an identifier, we are certainly not
13413 looking at a class-name. */
13414 if (identifier
== error_mark_node
)
13415 decl
= error_mark_node
;
13416 /* If we know this is a type-name, there's no need to look it
13418 else if (typename_p
)
13422 tree ambiguous_decls
;
13423 /* If we already know that this lookup is ambiguous, then
13424 we've already issued an error message; there's no reason
13428 cp_parser_simulate_error (parser
);
13429 return error_mark_node
;
13431 /* If the next token is a `::', then the name must be a type
13434 [basic.lookup.qual]
13436 During the lookup for a name preceding the :: scope
13437 resolution operator, object, function, and enumerator
13438 names are ignored. */
13439 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13440 tag_type
= typename_type
;
13441 /* Look up the name. */
13442 decl
= cp_parser_lookup_name (parser
, identifier
,
13444 /*is_template=*/false,
13445 /*is_namespace=*/false,
13446 check_dependency_p
,
13448 if (ambiguous_decls
)
13450 error ("reference to %qD is ambiguous", identifier
);
13451 print_candidates (ambiguous_decls
);
13452 if (cp_parser_parsing_tentatively (parser
))
13454 identifier_token
->ambiguous_p
= true;
13455 cp_parser_simulate_error (parser
);
13457 return error_mark_node
;
13463 /* Try a template-id. */
13464 decl
= cp_parser_template_id (parser
, template_keyword_p
,
13465 check_dependency_p
,
13467 if (decl
== error_mark_node
)
13468 return error_mark_node
;
13471 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
13473 /* If this is a typename, create a TYPENAME_TYPE. */
13474 if (typename_p
&& decl
!= error_mark_node
)
13476 decl
= make_typename_type (scope
, decl
, typename_type
,
13477 /*complain=*/tf_error
);
13478 if (decl
!= error_mark_node
)
13479 decl
= TYPE_NAME (decl
);
13482 /* Check to see that it is really the name of a class. */
13483 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
13484 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
13485 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13486 /* Situations like this:
13488 template <typename T> struct A {
13489 typename T::template X<int>::I i;
13492 are problematic. Is `T::template X<int>' a class-name? The
13493 standard does not seem to be definitive, but there is no other
13494 valid interpretation of the following `::'. Therefore, those
13495 names are considered class-names. */
13497 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
13498 if (decl
!= error_mark_node
)
13499 decl
= TYPE_NAME (decl
);
13501 else if (TREE_CODE (decl
) != TYPE_DECL
13502 || TREE_TYPE (decl
) == error_mark_node
13503 || !IS_AGGR_TYPE (TREE_TYPE (decl
)))
13504 decl
= error_mark_node
;
13506 if (decl
== error_mark_node
)
13507 cp_parser_error (parser
, "expected class-name");
13512 /* Parse a class-specifier.
13515 class-head { member-specification [opt] }
13517 Returns the TREE_TYPE representing the class. */
13520 cp_parser_class_specifier (cp_parser
* parser
)
13524 tree attributes
= NULL_TREE
;
13525 int has_trailing_semicolon
;
13526 bool nested_name_specifier_p
;
13527 unsigned saved_num_template_parameter_lists
;
13528 bool saved_in_function_body
;
13529 tree old_scope
= NULL_TREE
;
13530 tree scope
= NULL_TREE
;
13533 push_deferring_access_checks (dk_no_deferred
);
13535 /* Parse the class-head. */
13536 type
= cp_parser_class_head (parser
,
13537 &nested_name_specifier_p
,
13540 /* If the class-head was a semantic disaster, skip the entire body
13544 cp_parser_skip_to_end_of_block_or_statement (parser
);
13545 pop_deferring_access_checks ();
13546 return error_mark_node
;
13549 /* Look for the `{'. */
13550 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
13552 pop_deferring_access_checks ();
13553 return error_mark_node
;
13556 /* Process the base classes. If they're invalid, skip the
13557 entire class body. */
13558 if (!xref_basetypes (type
, bases
))
13560 cp_parser_skip_to_closing_brace (parser
);
13562 /* Consuming the closing brace yields better error messages
13564 cp_lexer_consume_token (parser
->lexer
);
13565 pop_deferring_access_checks ();
13566 return error_mark_node
;
13569 /* Issue an error message if type-definitions are forbidden here. */
13570 cp_parser_check_type_definition (parser
);
13571 /* Remember that we are defining one more class. */
13572 ++parser
->num_classes_being_defined
;
13573 /* Inside the class, surrounding template-parameter-lists do not
13575 saved_num_template_parameter_lists
13576 = parser
->num_template_parameter_lists
;
13577 parser
->num_template_parameter_lists
= 0;
13578 /* We are not in a function body. */
13579 saved_in_function_body
= parser
->in_function_body
;
13580 parser
->in_function_body
= false;
13582 /* Start the class. */
13583 if (nested_name_specifier_p
)
13585 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
13586 old_scope
= push_inner_scope (scope
);
13588 type
= begin_class_definition (type
, attributes
);
13590 if (type
== error_mark_node
)
13591 /* If the type is erroneous, skip the entire body of the class. */
13592 cp_parser_skip_to_closing_brace (parser
);
13594 /* Parse the member-specification. */
13595 cp_parser_member_specification_opt (parser
);
13597 /* Look for the trailing `}'. */
13598 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
13599 /* We get better error messages by noticing a common problem: a
13600 missing trailing `;'. */
13601 token
= cp_lexer_peek_token (parser
->lexer
);
13602 has_trailing_semicolon
= (token
->type
== CPP_SEMICOLON
);
13603 /* Look for trailing attributes to apply to this class. */
13604 if (cp_parser_allow_gnu_extensions_p (parser
))
13605 attributes
= cp_parser_attributes_opt (parser
);
13606 if (type
!= error_mark_node
)
13607 type
= finish_struct (type
, attributes
);
13608 if (nested_name_specifier_p
)
13609 pop_inner_scope (old_scope
, scope
);
13610 /* If this class is not itself within the scope of another class,
13611 then we need to parse the bodies of all of the queued function
13612 definitions. Note that the queued functions defined in a class
13613 are not always processed immediately following the
13614 class-specifier for that class. Consider:
13617 struct B { void f() { sizeof (A); } };
13620 If `f' were processed before the processing of `A' were
13621 completed, there would be no way to compute the size of `A'.
13622 Note that the nesting we are interested in here is lexical --
13623 not the semantic nesting given by TYPE_CONTEXT. In particular,
13626 struct A { struct B; };
13627 struct A::B { void f() { } };
13629 there is no need to delay the parsing of `A::B::f'. */
13630 if (--parser
->num_classes_being_defined
== 0)
13634 tree class_type
= NULL_TREE
;
13635 tree pushed_scope
= NULL_TREE
;
13637 /* In a first pass, parse default arguments to the functions.
13638 Then, in a second pass, parse the bodies of the functions.
13639 This two-phased approach handles cases like:
13647 for (TREE_PURPOSE (parser
->unparsed_functions_queues
)
13648 = nreverse (TREE_PURPOSE (parser
->unparsed_functions_queues
));
13649 (queue_entry
= TREE_PURPOSE (parser
->unparsed_functions_queues
));
13650 TREE_PURPOSE (parser
->unparsed_functions_queues
)
13651 = TREE_CHAIN (TREE_PURPOSE (parser
->unparsed_functions_queues
)))
13653 fn
= TREE_VALUE (queue_entry
);
13654 /* If there are default arguments that have not yet been processed,
13655 take care of them now. */
13656 if (class_type
!= TREE_PURPOSE (queue_entry
))
13659 pop_scope (pushed_scope
);
13660 class_type
= TREE_PURPOSE (queue_entry
);
13661 pushed_scope
= push_scope (class_type
);
13663 /* Make sure that any template parameters are in scope. */
13664 maybe_begin_member_template_processing (fn
);
13665 /* Parse the default argument expressions. */
13666 cp_parser_late_parsing_default_args (parser
, fn
);
13667 /* Remove any template parameters from the symbol table. */
13668 maybe_end_member_template_processing ();
13671 pop_scope (pushed_scope
);
13672 /* Now parse the body of the functions. */
13673 for (TREE_VALUE (parser
->unparsed_functions_queues
)
13674 = nreverse (TREE_VALUE (parser
->unparsed_functions_queues
));
13675 (queue_entry
= TREE_VALUE (parser
->unparsed_functions_queues
));
13676 TREE_VALUE (parser
->unparsed_functions_queues
)
13677 = TREE_CHAIN (TREE_VALUE (parser
->unparsed_functions_queues
)))
13679 /* Figure out which function we need to process. */
13680 fn
= TREE_VALUE (queue_entry
);
13681 /* Parse the function. */
13682 cp_parser_late_parsing_for_member (parser
, fn
);
13686 /* Put back any saved access checks. */
13687 pop_deferring_access_checks ();
13689 /* Restore saved state. */
13690 parser
->in_function_body
= saved_in_function_body
;
13691 parser
->num_template_parameter_lists
13692 = saved_num_template_parameter_lists
;
13697 /* Parse a class-head.
13700 class-key identifier [opt] base-clause [opt]
13701 class-key nested-name-specifier identifier base-clause [opt]
13702 class-key nested-name-specifier [opt] template-id
13706 class-key attributes identifier [opt] base-clause [opt]
13707 class-key attributes nested-name-specifier identifier base-clause [opt]
13708 class-key attributes nested-name-specifier [opt] template-id
13711 Upon return BASES is initialized to the list of base classes (or
13712 NULL, if there are none) in the same form returned by
13713 cp_parser_base_clause.
13715 Returns the TYPE of the indicated class. Sets
13716 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13717 involving a nested-name-specifier was used, and FALSE otherwise.
13719 Returns error_mark_node if this is not a class-head.
13721 Returns NULL_TREE if the class-head is syntactically valid, but
13722 semantically invalid in a way that means we should skip the entire
13723 body of the class. */
13726 cp_parser_class_head (cp_parser
* parser
,
13727 bool* nested_name_specifier_p
,
13728 tree
*attributes_p
,
13731 tree nested_name_specifier
;
13732 enum tag_types class_key
;
13733 tree id
= NULL_TREE
;
13734 tree type
= NULL_TREE
;
13736 bool template_id_p
= false;
13737 bool qualified_p
= false;
13738 bool invalid_nested_name_p
= false;
13739 bool invalid_explicit_specialization_p
= false;
13740 tree pushed_scope
= NULL_TREE
;
13741 unsigned num_templates
;
13743 /* Assume no nested-name-specifier will be present. */
13744 *nested_name_specifier_p
= false;
13745 /* Assume no template parameter lists will be used in defining the
13749 *bases
= NULL_TREE
;
13751 /* Look for the class-key. */
13752 class_key
= cp_parser_class_key (parser
);
13753 if (class_key
== none_type
)
13754 return error_mark_node
;
13756 /* Parse the attributes. */
13757 attributes
= cp_parser_attributes_opt (parser
);
13759 /* If the next token is `::', that is invalid -- but sometimes
13760 people do try to write:
13764 Handle this gracefully by accepting the extra qualifier, and then
13765 issuing an error about it later if this really is a
13766 class-head. If it turns out just to be an elaborated type
13767 specifier, remain silent. */
13768 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
13769 qualified_p
= true;
13771 push_deferring_access_checks (dk_no_check
);
13773 /* Determine the name of the class. Begin by looking for an
13774 optional nested-name-specifier. */
13775 nested_name_specifier
13776 = cp_parser_nested_name_specifier_opt (parser
,
13777 /*typename_keyword_p=*/false,
13778 /*check_dependency_p=*/false,
13780 /*is_declaration=*/false);
13781 /* If there was a nested-name-specifier, then there *must* be an
13783 if (nested_name_specifier
)
13785 /* Although the grammar says `identifier', it really means
13786 `class-name' or `template-name'. You are only allowed to
13787 define a class that has already been declared with this
13790 The proposed resolution for Core Issue 180 says that wherever
13791 you see `class T::X' you should treat `X' as a type-name.
13793 It is OK to define an inaccessible class; for example:
13795 class A { class B; };
13798 We do not know if we will see a class-name, or a
13799 template-name. We look for a class-name first, in case the
13800 class-name is a template-id; if we looked for the
13801 template-name first we would stop after the template-name. */
13802 cp_parser_parse_tentatively (parser
);
13803 type
= cp_parser_class_name (parser
,
13804 /*typename_keyword_p=*/false,
13805 /*template_keyword_p=*/false,
13807 /*check_dependency_p=*/false,
13808 /*class_head_p=*/true,
13809 /*is_declaration=*/false);
13810 /* If that didn't work, ignore the nested-name-specifier. */
13811 if (!cp_parser_parse_definitely (parser
))
13813 invalid_nested_name_p
= true;
13814 id
= cp_parser_identifier (parser
);
13815 if (id
== error_mark_node
)
13818 /* If we could not find a corresponding TYPE, treat this
13819 declaration like an unqualified declaration. */
13820 if (type
== error_mark_node
)
13821 nested_name_specifier
= NULL_TREE
;
13822 /* Otherwise, count the number of templates used in TYPE and its
13823 containing scopes. */
13828 for (scope
= TREE_TYPE (type
);
13829 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
13830 scope
= (TYPE_P (scope
)
13831 ? TYPE_CONTEXT (scope
)
13832 : DECL_CONTEXT (scope
)))
13834 && CLASS_TYPE_P (scope
)
13835 && CLASSTYPE_TEMPLATE_INFO (scope
)
13836 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
13837 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
13841 /* Otherwise, the identifier is optional. */
13844 /* We don't know whether what comes next is a template-id,
13845 an identifier, or nothing at all. */
13846 cp_parser_parse_tentatively (parser
);
13847 /* Check for a template-id. */
13848 id
= cp_parser_template_id (parser
,
13849 /*template_keyword_p=*/false,
13850 /*check_dependency_p=*/true,
13851 /*is_declaration=*/true);
13852 /* If that didn't work, it could still be an identifier. */
13853 if (!cp_parser_parse_definitely (parser
))
13855 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13856 id
= cp_parser_identifier (parser
);
13862 template_id_p
= true;
13867 pop_deferring_access_checks ();
13870 cp_parser_check_for_invalid_template_id (parser
, id
);
13872 /* If it's not a `:' or a `{' then we can't really be looking at a
13873 class-head, since a class-head only appears as part of a
13874 class-specifier. We have to detect this situation before calling
13875 xref_tag, since that has irreversible side-effects. */
13876 if (!cp_parser_next_token_starts_class_definition_p (parser
))
13878 cp_parser_error (parser
, "expected %<{%> or %<:%>");
13879 return error_mark_node
;
13882 /* At this point, we're going ahead with the class-specifier, even
13883 if some other problem occurs. */
13884 cp_parser_commit_to_tentative_parse (parser
);
13885 /* Issue the error about the overly-qualified name now. */
13887 cp_parser_error (parser
,
13888 "global qualification of class name is invalid");
13889 else if (invalid_nested_name_p
)
13890 cp_parser_error (parser
,
13891 "qualified name does not name a class");
13892 else if (nested_name_specifier
)
13896 /* Reject typedef-names in class heads. */
13897 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
13899 error ("invalid class name in declaration of %qD", type
);
13904 /* Figure out in what scope the declaration is being placed. */
13905 scope
= current_scope ();
13906 /* If that scope does not contain the scope in which the
13907 class was originally declared, the program is invalid. */
13908 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
13910 error ("declaration of %qD in %qD which does not enclose %qD",
13911 type
, scope
, nested_name_specifier
);
13917 A declarator-id shall not be qualified exception of the
13918 definition of a ... nested class outside of its class
13919 ... [or] a the definition or explicit instantiation of a
13920 class member of a namespace outside of its namespace. */
13921 if (scope
== nested_name_specifier
)
13923 pedwarn ("extra qualification ignored");
13924 nested_name_specifier
= NULL_TREE
;
13928 /* An explicit-specialization must be preceded by "template <>". If
13929 it is not, try to recover gracefully. */
13930 if (at_namespace_scope_p ()
13931 && parser
->num_template_parameter_lists
== 0
13934 error ("an explicit specialization must be preceded by %<template <>%>");
13935 invalid_explicit_specialization_p
= true;
13936 /* Take the same action that would have been taken by
13937 cp_parser_explicit_specialization. */
13938 ++parser
->num_template_parameter_lists
;
13939 begin_specialization ();
13941 /* There must be no "return" statements between this point and the
13942 end of this function; set "type "to the correct return value and
13943 use "goto done;" to return. */
13944 /* Make sure that the right number of template parameters were
13946 if (!cp_parser_check_template_parameters (parser
, num_templates
))
13948 /* If something went wrong, there is no point in even trying to
13949 process the class-definition. */
13954 /* Look up the type. */
13957 type
= TREE_TYPE (id
);
13958 type
= maybe_process_partial_specialization (type
);
13959 if (nested_name_specifier
)
13960 pushed_scope
= push_scope (nested_name_specifier
);
13962 else if (nested_name_specifier
)
13968 template <typename T> struct S { struct T };
13969 template <typename T> struct S<T>::T { };
13971 we will get a TYPENAME_TYPE when processing the definition of
13972 `S::T'. We need to resolve it to the actual type before we
13973 try to define it. */
13974 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
13976 class_type
= resolve_typename_type (TREE_TYPE (type
),
13977 /*only_current_p=*/false);
13978 if (class_type
!= error_mark_node
)
13979 type
= TYPE_NAME (class_type
);
13982 cp_parser_error (parser
, "could not resolve typename type");
13983 type
= error_mark_node
;
13987 maybe_process_partial_specialization (TREE_TYPE (type
));
13988 class_type
= current_class_type
;
13989 /* Enter the scope indicated by the nested-name-specifier. */
13990 pushed_scope
= push_scope (nested_name_specifier
);
13991 /* Get the canonical version of this type. */
13992 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
13993 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13994 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
13996 type
= push_template_decl (type
);
13997 if (type
== error_mark_node
)
14004 type
= TREE_TYPE (type
);
14005 *nested_name_specifier_p
= true;
14007 else /* The name is not a nested name. */
14009 /* If the class was unnamed, create a dummy name. */
14011 id
= make_anon_name ();
14012 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
14013 parser
->num_template_parameter_lists
);
14016 /* Indicate whether this class was declared as a `class' or as a
14018 if (TREE_CODE (type
) == RECORD_TYPE
)
14019 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
14020 cp_parser_check_class_key (class_key
, type
);
14022 /* If this type was already complete, and we see another definition,
14023 that's an error. */
14024 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
14026 error ("redefinition of %q#T", type
);
14027 error ("previous definition of %q+#T", type
);
14031 else if (type
== error_mark_node
)
14034 /* We will have entered the scope containing the class; the names of
14035 base classes should be looked up in that context. For example:
14037 struct A { struct B {}; struct C; };
14038 struct A::C : B {};
14042 /* Get the list of base-classes, if there is one. */
14043 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
14044 *bases
= cp_parser_base_clause (parser
);
14047 /* Leave the scope given by the nested-name-specifier. We will
14048 enter the class scope itself while processing the members. */
14050 pop_scope (pushed_scope
);
14052 if (invalid_explicit_specialization_p
)
14054 end_specialization ();
14055 --parser
->num_template_parameter_lists
;
14057 *attributes_p
= attributes
;
14061 /* Parse a class-key.
14068 Returns the kind of class-key specified, or none_type to indicate
14071 static enum tag_types
14072 cp_parser_class_key (cp_parser
* parser
)
14075 enum tag_types tag_type
;
14077 /* Look for the class-key. */
14078 token
= cp_parser_require (parser
, CPP_KEYWORD
, "class-key");
14082 /* Check to see if the TOKEN is a class-key. */
14083 tag_type
= cp_parser_token_is_class_key (token
);
14085 cp_parser_error (parser
, "expected class-key");
14089 /* Parse an (optional) member-specification.
14091 member-specification:
14092 member-declaration member-specification [opt]
14093 access-specifier : member-specification [opt] */
14096 cp_parser_member_specification_opt (cp_parser
* parser
)
14103 /* Peek at the next token. */
14104 token
= cp_lexer_peek_token (parser
->lexer
);
14105 /* If it's a `}', or EOF then we've seen all the members. */
14106 if (token
->type
== CPP_CLOSE_BRACE
14107 || token
->type
== CPP_EOF
14108 || token
->type
== CPP_PRAGMA_EOL
)
14111 /* See if this token is a keyword. */
14112 keyword
= token
->keyword
;
14116 case RID_PROTECTED
:
14118 /* Consume the access-specifier. */
14119 cp_lexer_consume_token (parser
->lexer
);
14120 /* Remember which access-specifier is active. */
14121 current_access_specifier
= token
->u
.value
;
14122 /* Look for the `:'. */
14123 cp_parser_require (parser
, CPP_COLON
, "`:'");
14127 /* Accept #pragmas at class scope. */
14128 if (token
->type
== CPP_PRAGMA
)
14130 cp_parser_pragma (parser
, pragma_external
);
14134 /* Otherwise, the next construction must be a
14135 member-declaration. */
14136 cp_parser_member_declaration (parser
);
14141 /* Parse a member-declaration.
14143 member-declaration:
14144 decl-specifier-seq [opt] member-declarator-list [opt] ;
14145 function-definition ; [opt]
14146 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14148 template-declaration
14150 member-declarator-list:
14152 member-declarator-list , member-declarator
14155 declarator pure-specifier [opt]
14156 declarator constant-initializer [opt]
14157 identifier [opt] : constant-expression
14161 member-declaration:
14162 __extension__ member-declaration
14165 declarator attributes [opt] pure-specifier [opt]
14166 declarator attributes [opt] constant-initializer [opt]
14167 identifier [opt] attributes [opt] : constant-expression
14171 member-declaration:
14172 static_assert-declaration */
14175 cp_parser_member_declaration (cp_parser
* parser
)
14177 cp_decl_specifier_seq decl_specifiers
;
14178 tree prefix_attributes
;
14180 int declares_class_or_enum
;
14183 int saved_pedantic
;
14185 /* Check for the `__extension__' keyword. */
14186 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
14189 cp_parser_member_declaration (parser
);
14190 /* Restore the old value of the PEDANTIC flag. */
14191 pedantic
= saved_pedantic
;
14196 /* Check for a template-declaration. */
14197 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14199 /* An explicit specialization here is an error condition, and we
14200 expect the specialization handler to detect and report this. */
14201 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
14202 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
14203 cp_parser_explicit_specialization (parser
);
14205 cp_parser_template_declaration (parser
, /*member_p=*/true);
14210 /* Check for a using-declaration. */
14211 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
14213 /* Parse the using-declaration. */
14214 cp_parser_using_declaration (parser
,
14215 /*access_declaration_p=*/false);
14219 /* Check for @defs. */
14220 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
14223 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
14224 ivar
= ivar_chains
;
14228 ivar
= TREE_CHAIN (member
);
14229 TREE_CHAIN (member
) = NULL_TREE
;
14230 finish_member_declaration (member
);
14235 /* If the next token is `static_assert' we have a static assertion. */
14236 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
14238 cp_parser_static_assert (parser
, /*member_p=*/true);
14242 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
14245 /* Parse the decl-specifier-seq. */
14246 cp_parser_decl_specifier_seq (parser
,
14247 CP_PARSER_FLAGS_OPTIONAL
,
14249 &declares_class_or_enum
);
14250 prefix_attributes
= decl_specifiers
.attributes
;
14251 decl_specifiers
.attributes
= NULL_TREE
;
14252 /* Check for an invalid type-name. */
14253 if (!decl_specifiers
.type
14254 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
14256 /* If there is no declarator, then the decl-specifier-seq should
14258 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14260 /* If there was no decl-specifier-seq, and the next token is a
14261 `;', then we have something like:
14267 Each member-declaration shall declare at least one member
14268 name of the class. */
14269 if (!decl_specifiers
.any_specifiers_p
)
14271 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14272 if (pedantic
&& !token
->in_system_header
)
14273 pedwarn ("%Hextra %<;%>", &token
->location
);
14279 /* See if this declaration is a friend. */
14280 friend_p
= cp_parser_friend_p (&decl_specifiers
);
14281 /* If there were decl-specifiers, check to see if there was
14282 a class-declaration. */
14283 type
= check_tag_decl (&decl_specifiers
);
14284 /* Nested classes have already been added to the class, but
14285 a `friend' needs to be explicitly registered. */
14288 /* If the `friend' keyword was present, the friend must
14289 be introduced with a class-key. */
14290 if (!declares_class_or_enum
)
14291 error ("a class-key must be used when declaring a friend");
14294 template <typename T> struct A {
14295 friend struct A<T>::B;
14298 A<T>::B will be represented by a TYPENAME_TYPE, and
14299 therefore not recognized by check_tag_decl. */
14301 && decl_specifiers
.type
14302 && TYPE_P (decl_specifiers
.type
))
14303 type
= decl_specifiers
.type
;
14304 if (!type
|| !TYPE_P (type
))
14305 error ("friend declaration does not name a class or "
14308 make_friend_class (current_class_type
, type
,
14309 /*complain=*/true);
14311 /* If there is no TYPE, an error message will already have
14313 else if (!type
|| type
== error_mark_node
)
14315 /* An anonymous aggregate has to be handled specially; such
14316 a declaration really declares a data member (with a
14317 particular type), as opposed to a nested class. */
14318 else if (ANON_AGGR_TYPE_P (type
))
14320 /* Remove constructors and such from TYPE, now that we
14321 know it is an anonymous aggregate. */
14322 fixup_anonymous_aggr (type
);
14323 /* And make the corresponding data member. */
14324 decl
= build_decl (FIELD_DECL
, NULL_TREE
, type
);
14325 /* Add it to the class. */
14326 finish_member_declaration (decl
);
14329 cp_parser_check_access_in_redeclaration (TYPE_NAME (type
));
14334 /* See if these declarations will be friends. */
14335 friend_p
= cp_parser_friend_p (&decl_specifiers
);
14337 /* Keep going until we hit the `;' at the end of the
14339 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
14341 tree attributes
= NULL_TREE
;
14342 tree first_attribute
;
14344 /* Peek at the next token. */
14345 token
= cp_lexer_peek_token (parser
->lexer
);
14347 /* Check for a bitfield declaration. */
14348 if (token
->type
== CPP_COLON
14349 || (token
->type
== CPP_NAME
14350 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
14356 /* Get the name of the bitfield. Note that we cannot just
14357 check TOKEN here because it may have been invalidated by
14358 the call to cp_lexer_peek_nth_token above. */
14359 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
14360 identifier
= cp_parser_identifier (parser
);
14362 identifier
= NULL_TREE
;
14364 /* Consume the `:' token. */
14365 cp_lexer_consume_token (parser
->lexer
);
14366 /* Get the width of the bitfield. */
14368 = cp_parser_constant_expression (parser
,
14369 /*allow_non_constant=*/false,
14372 /* Look for attributes that apply to the bitfield. */
14373 attributes
= cp_parser_attributes_opt (parser
);
14374 /* Remember which attributes are prefix attributes and
14376 first_attribute
= attributes
;
14377 /* Combine the attributes. */
14378 attributes
= chainon (prefix_attributes
, attributes
);
14380 /* Create the bitfield declaration. */
14381 decl
= grokbitfield (identifier
14382 ? make_id_declarator (NULL_TREE
,
14388 /* Apply the attributes. */
14389 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
14393 cp_declarator
*declarator
;
14395 tree asm_specification
;
14396 int ctor_dtor_or_conv_p
;
14398 /* Parse the declarator. */
14400 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14401 &ctor_dtor_or_conv_p
,
14402 /*parenthesized_p=*/NULL
,
14403 /*member_p=*/true);
14405 /* If something went wrong parsing the declarator, make sure
14406 that we at least consume some tokens. */
14407 if (declarator
== cp_error_declarator
)
14409 /* Skip to the end of the statement. */
14410 cp_parser_skip_to_end_of_statement (parser
);
14411 /* If the next token is not a semicolon, that is
14412 probably because we just skipped over the body of
14413 a function. So, we consume a semicolon if
14414 present, but do not issue an error message if it
14416 if (cp_lexer_next_token_is (parser
->lexer
,
14418 cp_lexer_consume_token (parser
->lexer
);
14422 if (declares_class_or_enum
& 2)
14423 cp_parser_check_for_definition_in_return_type
14424 (declarator
, decl_specifiers
.type
);
14426 /* Look for an asm-specification. */
14427 asm_specification
= cp_parser_asm_specification_opt (parser
);
14428 /* Look for attributes that apply to the declaration. */
14429 attributes
= cp_parser_attributes_opt (parser
);
14430 /* Remember which attributes are prefix attributes and
14432 first_attribute
= attributes
;
14433 /* Combine the attributes. */
14434 attributes
= chainon (prefix_attributes
, attributes
);
14436 /* If it's an `=', then we have a constant-initializer or a
14437 pure-specifier. It is not correct to parse the
14438 initializer before registering the member declaration
14439 since the member declaration should be in scope while
14440 its initializer is processed. However, the rest of the
14441 front end does not yet provide an interface that allows
14442 us to handle this correctly. */
14443 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
14447 A pure-specifier shall be used only in the declaration of
14448 a virtual function.
14450 A member-declarator can contain a constant-initializer
14451 only if it declares a static member of integral or
14454 Therefore, if the DECLARATOR is for a function, we look
14455 for a pure-specifier; otherwise, we look for a
14456 constant-initializer. When we call `grokfield', it will
14457 perform more stringent semantics checks. */
14458 if (function_declarator_p (declarator
))
14459 initializer
= cp_parser_pure_specifier (parser
);
14461 /* Parse the initializer. */
14462 initializer
= cp_parser_constant_initializer (parser
);
14464 /* Otherwise, there is no initializer. */
14466 initializer
= NULL_TREE
;
14468 /* See if we are probably looking at a function
14469 definition. We are certainly not looking at a
14470 member-declarator. Calling `grokfield' has
14471 side-effects, so we must not do it unless we are sure
14472 that we are looking at a member-declarator. */
14473 if (cp_parser_token_starts_function_definition_p
14474 (cp_lexer_peek_token (parser
->lexer
)))
14476 /* The grammar does not allow a pure-specifier to be
14477 used when a member function is defined. (It is
14478 possible that this fact is an oversight in the
14479 standard, since a pure function may be defined
14480 outside of the class-specifier. */
14482 error ("pure-specifier on function-definition");
14483 decl
= cp_parser_save_member_function_body (parser
,
14487 /* If the member was not a friend, declare it here. */
14489 finish_member_declaration (decl
);
14490 /* Peek at the next token. */
14491 token
= cp_lexer_peek_token (parser
->lexer
);
14492 /* If the next token is a semicolon, consume it. */
14493 if (token
->type
== CPP_SEMICOLON
)
14494 cp_lexer_consume_token (parser
->lexer
);
14498 /* Create the declaration. */
14499 decl
= grokfield (declarator
, &decl_specifiers
,
14500 initializer
, /*init_const_expr_p=*/true,
14505 /* Reset PREFIX_ATTRIBUTES. */
14506 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
14507 attributes
= TREE_CHAIN (attributes
);
14509 TREE_CHAIN (attributes
) = NULL_TREE
;
14511 /* If there is any qualification still in effect, clear it
14512 now; we will be starting fresh with the next declarator. */
14513 parser
->scope
= NULL_TREE
;
14514 parser
->qualifying_scope
= NULL_TREE
;
14515 parser
->object_scope
= NULL_TREE
;
14516 /* If it's a `,', then there are more declarators. */
14517 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
14518 cp_lexer_consume_token (parser
->lexer
);
14519 /* If the next token isn't a `;', then we have a parse error. */
14520 else if (cp_lexer_next_token_is_not (parser
->lexer
,
14523 cp_parser_error (parser
, "expected %<;%>");
14524 /* Skip tokens until we find a `;'. */
14525 cp_parser_skip_to_end_of_statement (parser
);
14532 /* Add DECL to the list of members. */
14534 finish_member_declaration (decl
);
14536 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14537 cp_parser_save_default_args (parser
, decl
);
14542 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
14545 /* Parse a pure-specifier.
14550 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14551 Otherwise, ERROR_MARK_NODE is returned. */
14554 cp_parser_pure_specifier (cp_parser
* parser
)
14558 /* Look for the `=' token. */
14559 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
14560 return error_mark_node
;
14561 /* Look for the `0' token. */
14562 token
= cp_lexer_consume_token (parser
->lexer
);
14563 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14564 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
14566 cp_parser_error (parser
,
14567 "invalid pure specifier (only `= 0' is allowed)");
14568 cp_parser_skip_to_end_of_statement (parser
);
14569 return error_mark_node
;
14571 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14573 error ("templates may not be %<virtual%>");
14574 return error_mark_node
;
14577 return integer_zero_node
;
14580 /* Parse a constant-initializer.
14582 constant-initializer:
14583 = constant-expression
14585 Returns a representation of the constant-expression. */
14588 cp_parser_constant_initializer (cp_parser
* parser
)
14590 /* Look for the `=' token. */
14591 if (!cp_parser_require (parser
, CPP_EQ
, "`='"))
14592 return error_mark_node
;
14594 /* It is invalid to write:
14596 struct S { static const int i = { 7 }; };
14599 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14601 cp_parser_error (parser
,
14602 "a brace-enclosed initializer is not allowed here");
14603 /* Consume the opening brace. */
14604 cp_lexer_consume_token (parser
->lexer
);
14605 /* Skip the initializer. */
14606 cp_parser_skip_to_closing_brace (parser
);
14607 /* Look for the trailing `}'. */
14608 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
14610 return error_mark_node
;
14613 return cp_parser_constant_expression (parser
,
14614 /*allow_non_constant=*/false,
14618 /* Derived classes [gram.class.derived] */
14620 /* Parse a base-clause.
14623 : base-specifier-list
14625 base-specifier-list:
14626 base-specifier ... [opt]
14627 base-specifier-list , base-specifier ... [opt]
14629 Returns a TREE_LIST representing the base-classes, in the order in
14630 which they were declared. The representation of each node is as
14631 described by cp_parser_base_specifier.
14633 In the case that no bases are specified, this function will return
14634 NULL_TREE, not ERROR_MARK_NODE. */
14637 cp_parser_base_clause (cp_parser
* parser
)
14639 tree bases
= NULL_TREE
;
14641 /* Look for the `:' that begins the list. */
14642 cp_parser_require (parser
, CPP_COLON
, "`:'");
14644 /* Scan the base-specifier-list. */
14649 bool pack_expansion_p
= false;
14651 /* Look for the base-specifier. */
14652 base
= cp_parser_base_specifier (parser
);
14653 /* Look for the (optional) ellipsis. */
14654 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14656 /* Consume the `...'. */
14657 cp_lexer_consume_token (parser
->lexer
);
14659 pack_expansion_p
= true;
14662 /* Add BASE to the front of the list. */
14663 if (base
!= error_mark_node
)
14665 if (pack_expansion_p
)
14666 /* Make this a pack expansion type. */
14667 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
14669 check_for_bare_parameter_packs (TREE_VALUE (base
));
14671 TREE_CHAIN (base
) = bases
;
14674 /* Peek at the next token. */
14675 token
= cp_lexer_peek_token (parser
->lexer
);
14676 /* If it's not a comma, then the list is complete. */
14677 if (token
->type
!= CPP_COMMA
)
14679 /* Consume the `,'. */
14680 cp_lexer_consume_token (parser
->lexer
);
14683 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14684 base class had a qualified name. However, the next name that
14685 appears is certainly not qualified. */
14686 parser
->scope
= NULL_TREE
;
14687 parser
->qualifying_scope
= NULL_TREE
;
14688 parser
->object_scope
= NULL_TREE
;
14690 return nreverse (bases
);
14693 /* Parse a base-specifier.
14696 :: [opt] nested-name-specifier [opt] class-name
14697 virtual access-specifier [opt] :: [opt] nested-name-specifier
14699 access-specifier virtual [opt] :: [opt] nested-name-specifier
14702 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14703 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14704 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14705 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14708 cp_parser_base_specifier (cp_parser
* parser
)
14712 bool virtual_p
= false;
14713 bool duplicate_virtual_error_issued_p
= false;
14714 bool duplicate_access_error_issued_p
= false;
14715 bool class_scope_p
, template_p
;
14716 tree access
= access_default_node
;
14719 /* Process the optional `virtual' and `access-specifier'. */
14722 /* Peek at the next token. */
14723 token
= cp_lexer_peek_token (parser
->lexer
);
14724 /* Process `virtual'. */
14725 switch (token
->keyword
)
14728 /* If `virtual' appears more than once, issue an error. */
14729 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
14731 cp_parser_error (parser
,
14732 "%<virtual%> specified more than once in base-specified");
14733 duplicate_virtual_error_issued_p
= true;
14738 /* Consume the `virtual' token. */
14739 cp_lexer_consume_token (parser
->lexer
);
14744 case RID_PROTECTED
:
14746 /* If more than one access specifier appears, issue an
14748 if (access
!= access_default_node
14749 && !duplicate_access_error_issued_p
)
14751 cp_parser_error (parser
,
14752 "more than one access specifier in base-specified");
14753 duplicate_access_error_issued_p
= true;
14756 access
= ridpointers
[(int) token
->keyword
];
14758 /* Consume the access-specifier. */
14759 cp_lexer_consume_token (parser
->lexer
);
14768 /* It is not uncommon to see programs mechanically, erroneously, use
14769 the 'typename' keyword to denote (dependent) qualified types
14770 as base classes. */
14771 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14773 if (!processing_template_decl
)
14774 error ("keyword %<typename%> not allowed outside of templates");
14776 error ("keyword %<typename%> not allowed in this context "
14777 "(the base class is implicitly a type)");
14778 cp_lexer_consume_token (parser
->lexer
);
14781 /* Look for the optional `::' operator. */
14782 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
14783 /* Look for the nested-name-specifier. The simplest way to
14788 The keyword `typename' is not permitted in a base-specifier or
14789 mem-initializer; in these contexts a qualified name that
14790 depends on a template-parameter is implicitly assumed to be a
14793 is to pretend that we have seen the `typename' keyword at this
14795 cp_parser_nested_name_specifier_opt (parser
,
14796 /*typename_keyword_p=*/true,
14797 /*check_dependency_p=*/true,
14799 /*is_declaration=*/true);
14800 /* If the base class is given by a qualified name, assume that names
14801 we see are type names or templates, as appropriate. */
14802 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
14803 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
14805 /* Finally, look for the class-name. */
14806 type
= cp_parser_class_name (parser
,
14810 /*check_dependency_p=*/true,
14811 /*class_head_p=*/false,
14812 /*is_declaration=*/true);
14814 if (type
== error_mark_node
)
14815 return error_mark_node
;
14817 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
14820 /* Exception handling [gram.exception] */
14822 /* Parse an (optional) exception-specification.
14824 exception-specification:
14825 throw ( type-id-list [opt] )
14827 Returns a TREE_LIST representing the exception-specification. The
14828 TREE_VALUE of each node is a type. */
14831 cp_parser_exception_specification_opt (cp_parser
* parser
)
14836 /* Peek at the next token. */
14837 token
= cp_lexer_peek_token (parser
->lexer
);
14838 /* If it's not `throw', then there's no exception-specification. */
14839 if (!cp_parser_is_keyword (token
, RID_THROW
))
14842 /* Consume the `throw'. */
14843 cp_lexer_consume_token (parser
->lexer
);
14845 /* Look for the `('. */
14846 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
14848 /* Peek at the next token. */
14849 token
= cp_lexer_peek_token (parser
->lexer
);
14850 /* If it's not a `)', then there is a type-id-list. */
14851 if (token
->type
!= CPP_CLOSE_PAREN
)
14853 const char *saved_message
;
14855 /* Types may not be defined in an exception-specification. */
14856 saved_message
= parser
->type_definition_forbidden_message
;
14857 parser
->type_definition_forbidden_message
14858 = "types may not be defined in an exception-specification";
14859 /* Parse the type-id-list. */
14860 type_id_list
= cp_parser_type_id_list (parser
);
14861 /* Restore the saved message. */
14862 parser
->type_definition_forbidden_message
= saved_message
;
14865 type_id_list
= empty_except_spec
;
14867 /* Look for the `)'. */
14868 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
14870 return type_id_list
;
14873 /* Parse an (optional) type-id-list.
14877 type-id-list , type-id ... [opt]
14879 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14880 in the order that the types were presented. */
14883 cp_parser_type_id_list (cp_parser
* parser
)
14885 tree types
= NULL_TREE
;
14892 /* Get the next type-id. */
14893 type
= cp_parser_type_id (parser
);
14894 /* Parse the optional ellipsis. */
14895 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14897 /* Consume the `...'. */
14898 cp_lexer_consume_token (parser
->lexer
);
14900 /* Turn the type into a pack expansion expression. */
14901 type
= make_pack_expansion (type
);
14903 /* Add it to the list. */
14904 types
= add_exception_specifier (types
, type
, /*complain=*/1);
14905 /* Peek at the next token. */
14906 token
= cp_lexer_peek_token (parser
->lexer
);
14907 /* If it is not a `,', we are done. */
14908 if (token
->type
!= CPP_COMMA
)
14910 /* Consume the `,'. */
14911 cp_lexer_consume_token (parser
->lexer
);
14914 return nreverse (types
);
14917 /* Parse a try-block.
14920 try compound-statement handler-seq */
14923 cp_parser_try_block (cp_parser
* parser
)
14927 cp_parser_require_keyword (parser
, RID_TRY
, "`try'");
14928 try_block
= begin_try_block ();
14929 cp_parser_compound_statement (parser
, NULL
, true);
14930 finish_try_block (try_block
);
14931 cp_parser_handler_seq (parser
);
14932 finish_handler_sequence (try_block
);
14937 /* Parse a function-try-block.
14939 function-try-block:
14940 try ctor-initializer [opt] function-body handler-seq */
14943 cp_parser_function_try_block (cp_parser
* parser
)
14945 tree compound_stmt
;
14947 bool ctor_initializer_p
;
14949 /* Look for the `try' keyword. */
14950 if (!cp_parser_require_keyword (parser
, RID_TRY
, "`try'"))
14952 /* Let the rest of the front end know where we are. */
14953 try_block
= begin_function_try_block (&compound_stmt
);
14954 /* Parse the function-body. */
14956 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
14957 /* We're done with the `try' part. */
14958 finish_function_try_block (try_block
);
14959 /* Parse the handlers. */
14960 cp_parser_handler_seq (parser
);
14961 /* We're done with the handlers. */
14962 finish_function_handler_sequence (try_block
, compound_stmt
);
14964 return ctor_initializer_p
;
14967 /* Parse a handler-seq.
14970 handler handler-seq [opt] */
14973 cp_parser_handler_seq (cp_parser
* parser
)
14979 /* Parse the handler. */
14980 cp_parser_handler (parser
);
14981 /* Peek at the next token. */
14982 token
= cp_lexer_peek_token (parser
->lexer
);
14983 /* If it's not `catch' then there are no more handlers. */
14984 if (!cp_parser_is_keyword (token
, RID_CATCH
))
14989 /* Parse a handler.
14992 catch ( exception-declaration ) compound-statement */
14995 cp_parser_handler (cp_parser
* parser
)
15000 cp_parser_require_keyword (parser
, RID_CATCH
, "`catch'");
15001 handler
= begin_handler ();
15002 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15003 declaration
= cp_parser_exception_declaration (parser
);
15004 finish_handler_parms (declaration
, handler
);
15005 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15006 cp_parser_compound_statement (parser
, NULL
, false);
15007 finish_handler (handler
);
15010 /* Parse an exception-declaration.
15012 exception-declaration:
15013 type-specifier-seq declarator
15014 type-specifier-seq abstract-declarator
15018 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15019 ellipsis variant is used. */
15022 cp_parser_exception_declaration (cp_parser
* parser
)
15024 cp_decl_specifier_seq type_specifiers
;
15025 cp_declarator
*declarator
;
15026 const char *saved_message
;
15028 /* If it's an ellipsis, it's easy to handle. */
15029 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15031 /* Consume the `...' token. */
15032 cp_lexer_consume_token (parser
->lexer
);
15036 /* Types may not be defined in exception-declarations. */
15037 saved_message
= parser
->type_definition_forbidden_message
;
15038 parser
->type_definition_forbidden_message
15039 = "types may not be defined in exception-declarations";
15041 /* Parse the type-specifier-seq. */
15042 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
15044 /* If it's a `)', then there is no declarator. */
15045 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
15048 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
15049 /*ctor_dtor_or_conv_p=*/NULL
,
15050 /*parenthesized_p=*/NULL
,
15051 /*member_p=*/false);
15053 /* Restore the saved message. */
15054 parser
->type_definition_forbidden_message
= saved_message
;
15056 if (!type_specifiers
.any_specifiers_p
)
15057 return error_mark_node
;
15059 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
15062 /* Parse a throw-expression.
15065 throw assignment-expression [opt]
15067 Returns a THROW_EXPR representing the throw-expression. */
15070 cp_parser_throw_expression (cp_parser
* parser
)
15075 cp_parser_require_keyword (parser
, RID_THROW
, "`throw'");
15076 token
= cp_lexer_peek_token (parser
->lexer
);
15077 /* Figure out whether or not there is an assignment-expression
15078 following the "throw" keyword. */
15079 if (token
->type
== CPP_COMMA
15080 || token
->type
== CPP_SEMICOLON
15081 || token
->type
== CPP_CLOSE_PAREN
15082 || token
->type
== CPP_CLOSE_SQUARE
15083 || token
->type
== CPP_CLOSE_BRACE
15084 || token
->type
== CPP_COLON
)
15085 expression
= NULL_TREE
;
15087 expression
= cp_parser_assignment_expression (parser
,
15090 return build_throw (expression
);
15093 /* GNU Extensions */
15095 /* Parse an (optional) asm-specification.
15098 asm ( string-literal )
15100 If the asm-specification is present, returns a STRING_CST
15101 corresponding to the string-literal. Otherwise, returns
15105 cp_parser_asm_specification_opt (cp_parser
* parser
)
15108 tree asm_specification
;
15110 /* Peek at the next token. */
15111 token
= cp_lexer_peek_token (parser
->lexer
);
15112 /* If the next token isn't the `asm' keyword, then there's no
15113 asm-specification. */
15114 if (!cp_parser_is_keyword (token
, RID_ASM
))
15117 /* Consume the `asm' token. */
15118 cp_lexer_consume_token (parser
->lexer
);
15119 /* Look for the `('. */
15120 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15122 /* Look for the string-literal. */
15123 asm_specification
= cp_parser_string_literal (parser
, false, false);
15125 /* Look for the `)'. */
15126 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`('");
15128 return asm_specification
;
15131 /* Parse an asm-operand-list.
15135 asm-operand-list , asm-operand
15138 string-literal ( expression )
15139 [ string-literal ] string-literal ( expression )
15141 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15142 each node is the expression. The TREE_PURPOSE is itself a
15143 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15144 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15145 is a STRING_CST for the string literal before the parenthesis. */
15148 cp_parser_asm_operand_list (cp_parser
* parser
)
15150 tree asm_operands
= NULL_TREE
;
15154 tree string_literal
;
15158 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
15160 /* Consume the `[' token. */
15161 cp_lexer_consume_token (parser
->lexer
);
15162 /* Read the operand name. */
15163 name
= cp_parser_identifier (parser
);
15164 if (name
!= error_mark_node
)
15165 name
= build_string (IDENTIFIER_LENGTH (name
),
15166 IDENTIFIER_POINTER (name
));
15167 /* Look for the closing `]'. */
15168 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
15172 /* Look for the string-literal. */
15173 string_literal
= cp_parser_string_literal (parser
, false, false);
15175 /* Look for the `('. */
15176 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15177 /* Parse the expression. */
15178 expression
= cp_parser_expression (parser
, /*cast_p=*/false);
15179 /* Look for the `)'. */
15180 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15182 /* Add this operand to the list. */
15183 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
15186 /* If the next token is not a `,', there are no more
15188 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15190 /* Consume the `,'. */
15191 cp_lexer_consume_token (parser
->lexer
);
15194 return nreverse (asm_operands
);
15197 /* Parse an asm-clobber-list.
15201 asm-clobber-list , string-literal
15203 Returns a TREE_LIST, indicating the clobbers in the order that they
15204 appeared. The TREE_VALUE of each node is a STRING_CST. */
15207 cp_parser_asm_clobber_list (cp_parser
* parser
)
15209 tree clobbers
= NULL_TREE
;
15213 tree string_literal
;
15215 /* Look for the string literal. */
15216 string_literal
= cp_parser_string_literal (parser
, false, false);
15217 /* Add it to the list. */
15218 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
15219 /* If the next token is not a `,', then the list is
15221 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15223 /* Consume the `,' token. */
15224 cp_lexer_consume_token (parser
->lexer
);
15230 /* Parse an (optional) series of attributes.
15233 attributes attribute
15236 __attribute__ (( attribute-list [opt] ))
15238 The return value is as for cp_parser_attribute_list. */
15241 cp_parser_attributes_opt (cp_parser
* parser
)
15243 tree attributes
= NULL_TREE
;
15248 tree attribute_list
;
15250 /* Peek at the next token. */
15251 token
= cp_lexer_peek_token (parser
->lexer
);
15252 /* If it's not `__attribute__', then we're done. */
15253 if (token
->keyword
!= RID_ATTRIBUTE
)
15256 /* Consume the `__attribute__' keyword. */
15257 cp_lexer_consume_token (parser
->lexer
);
15258 /* Look for the two `(' tokens. */
15259 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15260 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
15262 /* Peek at the next token. */
15263 token
= cp_lexer_peek_token (parser
->lexer
);
15264 if (token
->type
!= CPP_CLOSE_PAREN
)
15265 /* Parse the attribute-list. */
15266 attribute_list
= cp_parser_attribute_list (parser
);
15268 /* If the next token is a `)', then there is no attribute
15270 attribute_list
= NULL
;
15272 /* Look for the two `)' tokens. */
15273 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15274 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
15276 /* Add these new attributes to the list. */
15277 attributes
= chainon (attributes
, attribute_list
);
15283 /* Parse an attribute-list.
15287 attribute-list , attribute
15291 identifier ( identifier )
15292 identifier ( identifier , expression-list )
15293 identifier ( expression-list )
15295 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15296 to an attribute. The TREE_PURPOSE of each node is the identifier
15297 indicating which attribute is in use. The TREE_VALUE represents
15298 the arguments, if any. */
15301 cp_parser_attribute_list (cp_parser
* parser
)
15303 tree attribute_list
= NULL_TREE
;
15304 bool save_translate_strings_p
= parser
->translate_strings_p
;
15306 parser
->translate_strings_p
= false;
15313 /* Look for the identifier. We also allow keywords here; for
15314 example `__attribute__ ((const))' is legal. */
15315 token
= cp_lexer_peek_token (parser
->lexer
);
15316 if (token
->type
== CPP_NAME
15317 || token
->type
== CPP_KEYWORD
)
15319 tree arguments
= NULL_TREE
;
15321 /* Consume the token. */
15322 token
= cp_lexer_consume_token (parser
->lexer
);
15324 /* Save away the identifier that indicates which attribute
15326 identifier
= token
->u
.value
;
15327 attribute
= build_tree_list (identifier
, NULL_TREE
);
15329 /* Peek at the next token. */
15330 token
= cp_lexer_peek_token (parser
->lexer
);
15331 /* If it's an `(', then parse the attribute arguments. */
15332 if (token
->type
== CPP_OPEN_PAREN
)
15334 arguments
= cp_parser_parenthesized_expression_list
15335 (parser
, true, /*cast_p=*/false,
15336 /*allow_expansion_p=*/false,
15337 /*non_constant_p=*/NULL
);
15338 /* Save the arguments away. */
15339 TREE_VALUE (attribute
) = arguments
;
15342 if (arguments
!= error_mark_node
)
15344 /* Add this attribute to the list. */
15345 TREE_CHAIN (attribute
) = attribute_list
;
15346 attribute_list
= attribute
;
15349 token
= cp_lexer_peek_token (parser
->lexer
);
15351 /* Now, look for more attributes. If the next token isn't a
15352 `,', we're done. */
15353 if (token
->type
!= CPP_COMMA
)
15356 /* Consume the comma and keep going. */
15357 cp_lexer_consume_token (parser
->lexer
);
15359 parser
->translate_strings_p
= save_translate_strings_p
;
15361 /* We built up the list in reverse order. */
15362 return nreverse (attribute_list
);
15365 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15366 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15367 current value of the PEDANTIC flag, regardless of whether or not
15368 the `__extension__' keyword is present. The caller is responsible
15369 for restoring the value of the PEDANTIC flag. */
15372 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
15374 /* Save the old value of the PEDANTIC flag. */
15375 *saved_pedantic
= pedantic
;
15377 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
15379 /* Consume the `__extension__' token. */
15380 cp_lexer_consume_token (parser
->lexer
);
15381 /* We're not being pedantic while the `__extension__' keyword is
15391 /* Parse a label declaration.
15394 __label__ label-declarator-seq ;
15396 label-declarator-seq:
15397 identifier , label-declarator-seq
15401 cp_parser_label_declaration (cp_parser
* parser
)
15403 /* Look for the `__label__' keyword. */
15404 cp_parser_require_keyword (parser
, RID_LABEL
, "`__label__'");
15410 /* Look for an identifier. */
15411 identifier
= cp_parser_identifier (parser
);
15412 /* If we failed, stop. */
15413 if (identifier
== error_mark_node
)
15415 /* Declare it as a label. */
15416 finish_label_decl (identifier
);
15417 /* If the next token is a `;', stop. */
15418 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15420 /* Look for the `,' separating the label declarations. */
15421 cp_parser_require (parser
, CPP_COMMA
, "`,'");
15424 /* Look for the final `;'. */
15425 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
15428 /* Support Functions */
15430 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15431 NAME should have one of the representations used for an
15432 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15433 is returned. If PARSER->SCOPE is a dependent type, then a
15434 SCOPE_REF is returned.
15436 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15437 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15438 was formed. Abstractly, such entities should not be passed to this
15439 function, because they do not need to be looked up, but it is
15440 simpler to check for this special case here, rather than at the
15443 In cases not explicitly covered above, this function returns a
15444 DECL, OVERLOAD, or baselink representing the result of the lookup.
15445 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15448 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15449 (e.g., "struct") that was used. In that case bindings that do not
15450 refer to types are ignored.
15452 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15455 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15458 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15461 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15462 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15463 NULL_TREE otherwise. */
15466 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
15467 enum tag_types tag_type
,
15470 bool check_dependency
,
15471 tree
*ambiguous_decls
)
15475 tree object_type
= parser
->context
->object_type
;
15477 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
15478 flags
|= LOOKUP_COMPLAIN
;
15480 /* Assume that the lookup will be unambiguous. */
15481 if (ambiguous_decls
)
15482 *ambiguous_decls
= NULL_TREE
;
15484 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15485 no longer valid. Note that if we are parsing tentatively, and
15486 the parse fails, OBJECT_TYPE will be automatically restored. */
15487 parser
->context
->object_type
= NULL_TREE
;
15489 if (name
== error_mark_node
)
15490 return error_mark_node
;
15492 /* A template-id has already been resolved; there is no lookup to
15494 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
15496 if (BASELINK_P (name
))
15498 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
15499 == TEMPLATE_ID_EXPR
);
15503 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15504 it should already have been checked to make sure that the name
15505 used matches the type being destroyed. */
15506 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15510 /* Figure out to which type this destructor applies. */
15512 type
= parser
->scope
;
15513 else if (object_type
)
15514 type
= object_type
;
15516 type
= current_class_type
;
15517 /* If that's not a class type, there is no destructor. */
15518 if (!type
|| !CLASS_TYPE_P (type
))
15519 return error_mark_node
;
15520 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
15521 lazily_declare_fn (sfk_destructor
, type
);
15522 if (!CLASSTYPE_DESTRUCTORS (type
))
15523 return error_mark_node
;
15524 /* If it was a class type, return the destructor. */
15525 return CLASSTYPE_DESTRUCTORS (type
);
15528 /* By this point, the NAME should be an ordinary identifier. If
15529 the id-expression was a qualified name, the qualifying scope is
15530 stored in PARSER->SCOPE at this point. */
15531 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
15533 /* Perform the lookup. */
15538 if (parser
->scope
== error_mark_node
)
15539 return error_mark_node
;
15541 /* If the SCOPE is dependent, the lookup must be deferred until
15542 the template is instantiated -- unless we are explicitly
15543 looking up names in uninstantiated templates. Even then, we
15544 cannot look up the name if the scope is not a class type; it
15545 might, for example, be a template type parameter. */
15546 dependent_p
= (TYPE_P (parser
->scope
)
15547 && !(parser
->in_declarator_p
15548 && currently_open_class (parser
->scope
))
15549 && dependent_type_p (parser
->scope
));
15550 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
15557 /* The resolution to Core Issue 180 says that `struct
15558 A::B' should be considered a type-name, even if `A'
15560 type
= make_typename_type (parser
->scope
, name
, tag_type
,
15561 /*complain=*/tf_error
);
15562 decl
= TYPE_NAME (type
);
15564 else if (is_template
15565 && (cp_parser_next_token_ends_template_argument_p (parser
)
15566 || cp_lexer_next_token_is (parser
->lexer
,
15568 decl
= make_unbound_class_template (parser
->scope
,
15570 /*complain=*/tf_error
);
15572 decl
= build_qualified_name (/*type=*/NULL_TREE
,
15573 parser
->scope
, name
,
15578 tree pushed_scope
= NULL_TREE
;
15580 /* If PARSER->SCOPE is a dependent type, then it must be a
15581 class type, and we must not be checking dependencies;
15582 otherwise, we would have processed this lookup above. So
15583 that PARSER->SCOPE is not considered a dependent base by
15584 lookup_member, we must enter the scope here. */
15586 pushed_scope
= push_scope (parser
->scope
);
15587 /* If the PARSER->SCOPE is a template specialization, it
15588 may be instantiated during name lookup. In that case,
15589 errors may be issued. Even if we rollback the current
15590 tentative parse, those errors are valid. */
15591 decl
= lookup_qualified_name (parser
->scope
, name
,
15592 tag_type
!= none_type
,
15593 /*complain=*/true);
15595 pop_scope (pushed_scope
);
15597 parser
->qualifying_scope
= parser
->scope
;
15598 parser
->object_scope
= NULL_TREE
;
15600 else if (object_type
)
15602 tree object_decl
= NULL_TREE
;
15603 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15604 OBJECT_TYPE is not a class. */
15605 if (CLASS_TYPE_P (object_type
))
15606 /* If the OBJECT_TYPE is a template specialization, it may
15607 be instantiated during name lookup. In that case, errors
15608 may be issued. Even if we rollback the current tentative
15609 parse, those errors are valid. */
15610 object_decl
= lookup_member (object_type
,
15613 tag_type
!= none_type
);
15614 /* Look it up in the enclosing context, too. */
15615 decl
= lookup_name_real (name
, tag_type
!= none_type
,
15617 /*block_p=*/true, is_namespace
, flags
);
15618 parser
->object_scope
= object_type
;
15619 parser
->qualifying_scope
= NULL_TREE
;
15621 decl
= object_decl
;
15625 decl
= lookup_name_real (name
, tag_type
!= none_type
,
15627 /*block_p=*/true, is_namespace
, flags
);
15628 parser
->qualifying_scope
= NULL_TREE
;
15629 parser
->object_scope
= NULL_TREE
;
15632 /* If the lookup failed, let our caller know. */
15633 if (!decl
|| decl
== error_mark_node
)
15634 return error_mark_node
;
15636 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15637 if (TREE_CODE (decl
) == TREE_LIST
)
15639 if (ambiguous_decls
)
15640 *ambiguous_decls
= decl
;
15641 /* The error message we have to print is too complicated for
15642 cp_parser_error, so we incorporate its actions directly. */
15643 if (!cp_parser_simulate_error (parser
))
15645 error ("reference to %qD is ambiguous", name
);
15646 print_candidates (decl
);
15648 return error_mark_node
;
15651 gcc_assert (DECL_P (decl
)
15652 || TREE_CODE (decl
) == OVERLOAD
15653 || TREE_CODE (decl
) == SCOPE_REF
15654 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
15655 || BASELINK_P (decl
));
15657 /* If we have resolved the name of a member declaration, check to
15658 see if the declaration is accessible. When the name resolves to
15659 set of overloaded functions, accessibility is checked when
15660 overload resolution is done.
15662 During an explicit instantiation, access is not checked at all,
15663 as per [temp.explicit]. */
15665 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
15670 /* Like cp_parser_lookup_name, but for use in the typical case where
15671 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15672 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15675 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
)
15677 return cp_parser_lookup_name (parser
, name
,
15679 /*is_template=*/false,
15680 /*is_namespace=*/false,
15681 /*check_dependency=*/true,
15682 /*ambiguous_decls=*/NULL
);
15685 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15686 the current context, return the TYPE_DECL. If TAG_NAME_P is
15687 true, the DECL indicates the class being defined in a class-head,
15688 or declared in an elaborated-type-specifier.
15690 Otherwise, return DECL. */
15693 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
15695 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15696 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15699 template <typename T> struct B;
15702 template <typename T> struct A::B {};
15704 Similarly, in an elaborated-type-specifier:
15706 namespace N { struct X{}; }
15709 template <typename T> friend struct N::X;
15712 However, if the DECL refers to a class type, and we are in
15713 the scope of the class, then the name lookup automatically
15714 finds the TYPE_DECL created by build_self_reference rather
15715 than a TEMPLATE_DECL. For example, in:
15717 template <class T> struct S {
15721 there is no need to handle such case. */
15723 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
15724 return DECL_TEMPLATE_RESULT (decl
);
15729 /* If too many, or too few, template-parameter lists apply to the
15730 declarator, issue an error message. Returns TRUE if all went well,
15731 and FALSE otherwise. */
15734 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
15735 cp_declarator
*declarator
)
15737 unsigned num_templates
;
15739 /* We haven't seen any classes that involve template parameters yet. */
15742 switch (declarator
->kind
)
15745 if (declarator
->u
.id
.qualifying_scope
)
15750 scope
= declarator
->u
.id
.qualifying_scope
;
15751 member
= declarator
->u
.id
.unqualified_name
;
15753 while (scope
&& CLASS_TYPE_P (scope
))
15755 /* You're supposed to have one `template <...>'
15756 for every template class, but you don't need one
15757 for a full specialization. For example:
15759 template <class T> struct S{};
15760 template <> struct S<int> { void f(); };
15761 void S<int>::f () {}
15763 is correct; there shouldn't be a `template <>' for
15764 the definition of `S<int>::f'. */
15765 if (!CLASSTYPE_TEMPLATE_INFO (scope
))
15766 /* If SCOPE does not have template information of any
15767 kind, then it is not a template, nor is it nested
15768 within a template. */
15770 if (explicit_class_specialization_p (scope
))
15772 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
15775 scope
= TYPE_CONTEXT (scope
);
15778 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
15779 == TEMPLATE_ID_EXPR
)
15780 /* If the DECLARATOR has the form `X<y>' then it uses one
15781 additional level of template parameters. */
15784 return cp_parser_check_template_parameters (parser
,
15790 case cdk_reference
:
15792 return (cp_parser_check_declarator_template_parameters
15793 (parser
, declarator
->declarator
));
15799 gcc_unreachable ();
15804 /* NUM_TEMPLATES were used in the current declaration. If that is
15805 invalid, return FALSE and issue an error messages. Otherwise,
15809 cp_parser_check_template_parameters (cp_parser
* parser
,
15810 unsigned num_templates
)
15812 /* If there are more template classes than parameter lists, we have
15815 template <class T> void S<T>::R<T>::f (); */
15816 if (parser
->num_template_parameter_lists
< num_templates
)
15818 error ("too few template-parameter-lists");
15821 /* If there are the same number of template classes and parameter
15822 lists, that's OK. */
15823 if (parser
->num_template_parameter_lists
== num_templates
)
15825 /* If there are more, but only one more, then we are referring to a
15826 member template. That's OK too. */
15827 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
15829 /* Otherwise, there are too many template parameter lists. We have
15832 template <class T> template <class U> void S::f(); */
15833 error ("too many template-parameter-lists");
15837 /* Parse an optional `::' token indicating that the following name is
15838 from the global namespace. If so, PARSER->SCOPE is set to the
15839 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15840 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15841 Returns the new value of PARSER->SCOPE, if the `::' token is
15842 present, and NULL_TREE otherwise. */
15845 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
15849 /* Peek at the next token. */
15850 token
= cp_lexer_peek_token (parser
->lexer
);
15851 /* If we're looking at a `::' token then we're starting from the
15852 global namespace, not our current location. */
15853 if (token
->type
== CPP_SCOPE
)
15855 /* Consume the `::' token. */
15856 cp_lexer_consume_token (parser
->lexer
);
15857 /* Set the SCOPE so that we know where to start the lookup. */
15858 parser
->scope
= global_namespace
;
15859 parser
->qualifying_scope
= global_namespace
;
15860 parser
->object_scope
= NULL_TREE
;
15862 return parser
->scope
;
15864 else if (!current_scope_valid_p
)
15866 parser
->scope
= NULL_TREE
;
15867 parser
->qualifying_scope
= NULL_TREE
;
15868 parser
->object_scope
= NULL_TREE
;
15874 /* Returns TRUE if the upcoming token sequence is the start of a
15875 constructor declarator. If FRIEND_P is true, the declarator is
15876 preceded by the `friend' specifier. */
15879 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
15881 bool constructor_p
;
15882 tree type_decl
= NULL_TREE
;
15883 bool nested_name_p
;
15884 cp_token
*next_token
;
15886 /* The common case is that this is not a constructor declarator, so
15887 try to avoid doing lots of work if at all possible. It's not
15888 valid declare a constructor at function scope. */
15889 if (parser
->in_function_body
)
15891 /* And only certain tokens can begin a constructor declarator. */
15892 next_token
= cp_lexer_peek_token (parser
->lexer
);
15893 if (next_token
->type
!= CPP_NAME
15894 && next_token
->type
!= CPP_SCOPE
15895 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
15896 && next_token
->type
!= CPP_TEMPLATE_ID
)
15899 /* Parse tentatively; we are going to roll back all of the tokens
15901 cp_parser_parse_tentatively (parser
);
15902 /* Assume that we are looking at a constructor declarator. */
15903 constructor_p
= true;
15905 /* Look for the optional `::' operator. */
15906 cp_parser_global_scope_opt (parser
,
15907 /*current_scope_valid_p=*/false);
15908 /* Look for the nested-name-specifier. */
15910 = (cp_parser_nested_name_specifier_opt (parser
,
15911 /*typename_keyword_p=*/false,
15912 /*check_dependency_p=*/false,
15914 /*is_declaration=*/false)
15916 /* Outside of a class-specifier, there must be a
15917 nested-name-specifier. */
15918 if (!nested_name_p
&&
15919 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
15921 constructor_p
= false;
15922 /* If we still think that this might be a constructor-declarator,
15923 look for a class-name. */
15928 template <typename T> struct S { S(); };
15929 template <typename T> S<T>::S ();
15931 we must recognize that the nested `S' names a class.
15934 template <typename T> S<T>::S<T> ();
15936 we must recognize that the nested `S' names a template. */
15937 type_decl
= cp_parser_class_name (parser
,
15938 /*typename_keyword_p=*/false,
15939 /*template_keyword_p=*/false,
15941 /*check_dependency_p=*/false,
15942 /*class_head_p=*/false,
15943 /*is_declaration=*/false);
15944 /* If there was no class-name, then this is not a constructor. */
15945 constructor_p
= !cp_parser_error_occurred (parser
);
15948 /* If we're still considering a constructor, we have to see a `(',
15949 to begin the parameter-declaration-clause, followed by either a
15950 `)', an `...', or a decl-specifier. We need to check for a
15951 type-specifier to avoid being fooled into thinking that:
15955 is a constructor. (It is actually a function named `f' that
15956 takes one parameter (of type `int') and returns a value of type
15959 && cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
15961 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
15962 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
15963 /* A parameter declaration begins with a decl-specifier,
15964 which is either the "attribute" keyword, a storage class
15965 specifier, or (usually) a type-specifier. */
15966 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
15969 tree pushed_scope
= NULL_TREE
;
15970 unsigned saved_num_template_parameter_lists
;
15972 /* Names appearing in the type-specifier should be looked up
15973 in the scope of the class. */
15974 if (current_class_type
)
15978 type
= TREE_TYPE (type_decl
);
15979 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15981 type
= resolve_typename_type (type
,
15982 /*only_current_p=*/false);
15983 if (type
== error_mark_node
)
15985 cp_parser_abort_tentative_parse (parser
);
15989 pushed_scope
= push_scope (type
);
15992 /* Inside the constructor parameter list, surrounding
15993 template-parameter-lists do not apply. */
15994 saved_num_template_parameter_lists
15995 = parser
->num_template_parameter_lists
;
15996 parser
->num_template_parameter_lists
= 0;
15998 /* Look for the type-specifier. */
15999 cp_parser_type_specifier (parser
,
16000 CP_PARSER_FLAGS_NONE
,
16001 /*decl_specs=*/NULL
,
16002 /*is_declarator=*/true,
16003 /*declares_class_or_enum=*/NULL
,
16004 /*is_cv_qualifier=*/NULL
);
16006 parser
->num_template_parameter_lists
16007 = saved_num_template_parameter_lists
;
16009 /* Leave the scope of the class. */
16011 pop_scope (pushed_scope
);
16013 constructor_p
= !cp_parser_error_occurred (parser
);
16017 constructor_p
= false;
16018 /* We did not really want to consume any tokens. */
16019 cp_parser_abort_tentative_parse (parser
);
16021 return constructor_p
;
16024 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16025 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16026 they must be performed once we are in the scope of the function.
16028 Returns the function defined. */
16031 cp_parser_function_definition_from_specifiers_and_declarator
16032 (cp_parser
* parser
,
16033 cp_decl_specifier_seq
*decl_specifiers
,
16035 const cp_declarator
*declarator
)
16040 /* Begin the function-definition. */
16041 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
16043 /* The things we're about to see are not directly qualified by any
16044 template headers we've seen thus far. */
16045 reset_specialization ();
16047 /* If there were names looked up in the decl-specifier-seq that we
16048 did not check, check them now. We must wait until we are in the
16049 scope of the function to perform the checks, since the function
16050 might be a friend. */
16051 perform_deferred_access_checks ();
16055 /* Skip the entire function. */
16056 cp_parser_skip_to_end_of_block_or_statement (parser
);
16057 fn
= error_mark_node
;
16059 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
16061 /* Seen already, skip it. An error message has already been output. */
16062 cp_parser_skip_to_end_of_block_or_statement (parser
);
16063 fn
= current_function_decl
;
16064 current_function_decl
= NULL_TREE
;
16065 /* If this is a function from a class, pop the nested class. */
16066 if (current_class_name
)
16067 pop_nested_class ();
16070 fn
= cp_parser_function_definition_after_declarator (parser
,
16071 /*inline_p=*/false);
16076 /* Parse the part of a function-definition that follows the
16077 declarator. INLINE_P is TRUE iff this function is an inline
16078 function defined with a class-specifier.
16080 Returns the function defined. */
16083 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
16087 bool ctor_initializer_p
= false;
16088 bool saved_in_unbraced_linkage_specification_p
;
16089 bool saved_in_function_body
;
16090 unsigned saved_num_template_parameter_lists
;
16092 saved_in_function_body
= parser
->in_function_body
;
16093 parser
->in_function_body
= true;
16094 /* If the next token is `return', then the code may be trying to
16095 make use of the "named return value" extension that G++ used to
16097 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
16099 /* Consume the `return' keyword. */
16100 cp_lexer_consume_token (parser
->lexer
);
16101 /* Look for the identifier that indicates what value is to be
16103 cp_parser_identifier (parser
);
16104 /* Issue an error message. */
16105 error ("named return values are no longer supported");
16106 /* Skip tokens until we reach the start of the function body. */
16109 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16110 if (token
->type
== CPP_OPEN_BRACE
16111 || token
->type
== CPP_EOF
16112 || token
->type
== CPP_PRAGMA_EOL
)
16114 cp_lexer_consume_token (parser
->lexer
);
16117 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16118 anything declared inside `f'. */
16119 saved_in_unbraced_linkage_specification_p
16120 = parser
->in_unbraced_linkage_specification_p
;
16121 parser
->in_unbraced_linkage_specification_p
= false;
16122 /* Inside the function, surrounding template-parameter-lists do not
16124 saved_num_template_parameter_lists
16125 = parser
->num_template_parameter_lists
;
16126 parser
->num_template_parameter_lists
= 0;
16127 /* If the next token is `try', then we are looking at a
16128 function-try-block. */
16129 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
16130 ctor_initializer_p
= cp_parser_function_try_block (parser
);
16131 /* A function-try-block includes the function-body, so we only do
16132 this next part if we're not processing a function-try-block. */
16135 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
16137 /* Finish the function. */
16138 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
16139 (inline_p
? 2 : 0));
16140 /* Generate code for it, if necessary. */
16141 expand_or_defer_fn (fn
);
16142 /* Restore the saved values. */
16143 parser
->in_unbraced_linkage_specification_p
16144 = saved_in_unbraced_linkage_specification_p
;
16145 parser
->num_template_parameter_lists
16146 = saved_num_template_parameter_lists
;
16147 parser
->in_function_body
= saved_in_function_body
;
16152 /* Parse a template-declaration, assuming that the `export' (and
16153 `extern') keywords, if present, has already been scanned. MEMBER_P
16154 is as for cp_parser_template_declaration. */
16157 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
16159 tree decl
= NULL_TREE
;
16160 VEC (deferred_access_check
,gc
) *checks
;
16161 tree parameter_list
;
16162 bool friend_p
= false;
16163 bool need_lang_pop
;
16165 /* Look for the `template' keyword. */
16166 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, "`template'"))
16170 if (!cp_parser_require (parser
, CPP_LESS
, "`<'"))
16172 if (at_class_scope_p () && current_function_decl
)
16174 /* 14.5.2.2 [temp.mem]
16176 A local class shall not have member templates. */
16177 error ("invalid declaration of member template in local class");
16178 cp_parser_skip_to_end_of_block_or_statement (parser
);
16183 A template ... shall not have C linkage. */
16184 if (current_lang_name
== lang_name_c
)
16186 error ("template with C linkage");
16187 /* Give it C++ linkage to avoid confusing other parts of the
16189 push_lang_context (lang_name_cplusplus
);
16190 need_lang_pop
= true;
16193 need_lang_pop
= false;
16195 /* We cannot perform access checks on the template parameter
16196 declarations until we know what is being declared, just as we
16197 cannot check the decl-specifier list. */
16198 push_deferring_access_checks (dk_deferred
);
16200 /* If the next token is `>', then we have an invalid
16201 specialization. Rather than complain about an invalid template
16202 parameter, issue an error message here. */
16203 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
16205 cp_parser_error (parser
, "invalid explicit specialization");
16206 begin_specialization ();
16207 parameter_list
= NULL_TREE
;
16210 /* Parse the template parameters. */
16211 parameter_list
= cp_parser_template_parameter_list (parser
);
16213 /* Get the deferred access checks from the parameter list. These
16214 will be checked once we know what is being declared, as for a
16215 member template the checks must be performed in the scope of the
16216 class containing the member. */
16217 checks
= get_deferred_access_checks ();
16219 /* Look for the `>'. */
16220 cp_parser_skip_to_end_of_template_parameter_list (parser
);
16221 /* We just processed one more parameter list. */
16222 ++parser
->num_template_parameter_lists
;
16223 /* If the next token is `template', there are more template
16225 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
16227 cp_parser_template_declaration_after_export (parser
, member_p
);
16230 /* There are no access checks when parsing a template, as we do not
16231 know if a specialization will be a friend. */
16232 push_deferring_access_checks (dk_no_check
);
16233 decl
= cp_parser_single_declaration (parser
,
16237 pop_deferring_access_checks ();
16239 /* If this is a member template declaration, let the front
16241 if (member_p
&& !friend_p
&& decl
)
16243 if (TREE_CODE (decl
) == TYPE_DECL
)
16244 cp_parser_check_access_in_redeclaration (decl
);
16246 decl
= finish_member_template_decl (decl
);
16248 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
16249 make_friend_class (current_class_type
, TREE_TYPE (decl
),
16250 /*complain=*/true);
16252 /* We are done with the current parameter list. */
16253 --parser
->num_template_parameter_lists
;
16255 pop_deferring_access_checks ();
16258 finish_template_decl (parameter_list
);
16260 /* Register member declarations. */
16261 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
16262 finish_member_declaration (decl
);
16263 /* For the erroneous case of a template with C linkage, we pushed an
16264 implicit C++ linkage scope; exit that scope now. */
16266 pop_lang_context ();
16267 /* If DECL is a function template, we must return to parse it later.
16268 (Even though there is no definition, there might be default
16269 arguments that need handling.) */
16270 if (member_p
&& decl
16271 && (TREE_CODE (decl
) == FUNCTION_DECL
16272 || DECL_FUNCTION_TEMPLATE_P (decl
)))
16273 TREE_VALUE (parser
->unparsed_functions_queues
)
16274 = tree_cons (NULL_TREE
, decl
,
16275 TREE_VALUE (parser
->unparsed_functions_queues
));
16278 /* Perform the deferred access checks from a template-parameter-list.
16279 CHECKS is a TREE_LIST of access checks, as returned by
16280 get_deferred_access_checks. */
16283 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check
,gc
)* checks
)
16285 ++processing_template_parmlist
;
16286 perform_access_checks (checks
);
16287 --processing_template_parmlist
;
16290 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16291 `function-definition' sequence. MEMBER_P is true, this declaration
16292 appears in a class scope.
16294 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16295 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16298 cp_parser_single_declaration (cp_parser
* parser
,
16299 VEC (deferred_access_check
,gc
)* checks
,
16303 int declares_class_or_enum
;
16304 tree decl
= NULL_TREE
;
16305 cp_decl_specifier_seq decl_specifiers
;
16306 bool function_definition_p
= false;
16308 /* This function is only used when processing a template
16310 gcc_assert (innermost_scope_kind () == sk_template_parms
16311 || innermost_scope_kind () == sk_template_spec
);
16313 /* Defer access checks until we know what is being declared. */
16314 push_deferring_access_checks (dk_deferred
);
16316 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16318 cp_parser_decl_specifier_seq (parser
,
16319 CP_PARSER_FLAGS_OPTIONAL
,
16321 &declares_class_or_enum
);
16323 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
16325 /* There are no template typedefs. */
16326 if (decl_specifiers
.specs
[(int) ds_typedef
])
16328 error ("template declaration of %qs", "typedef");
16329 decl
= error_mark_node
;
16332 /* Gather up the access checks that occurred the
16333 decl-specifier-seq. */
16334 stop_deferring_access_checks ();
16336 /* Check for the declaration of a template class. */
16337 if (declares_class_or_enum
)
16339 if (cp_parser_declares_only_class_p (parser
))
16341 decl
= shadow_tag (&decl_specifiers
);
16346 friend template <typename T> struct A<T>::B;
16349 A<T>::B will be represented by a TYPENAME_TYPE, and
16350 therefore not recognized by shadow_tag. */
16351 if (friend_p
&& *friend_p
16353 && decl_specifiers
.type
16354 && TYPE_P (decl_specifiers
.type
))
16355 decl
= decl_specifiers
.type
;
16357 if (decl
&& decl
!= error_mark_node
)
16358 decl
= TYPE_NAME (decl
);
16360 decl
= error_mark_node
;
16362 /* Perform access checks for template parameters. */
16363 cp_parser_perform_template_parameter_access_checks (checks
);
16366 /* If it's not a template class, try for a template function. If
16367 the next token is a `;', then this declaration does not declare
16368 anything. But, if there were errors in the decl-specifiers, then
16369 the error might well have come from an attempted class-specifier.
16370 In that case, there's no need to warn about a missing declarator. */
16372 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
16373 || decl_specifiers
.type
!= error_mark_node
))
16374 decl
= cp_parser_init_declarator (parser
,
16377 /*function_definition_allowed_p=*/true,
16379 declares_class_or_enum
,
16380 &function_definition_p
);
16382 pop_deferring_access_checks ();
16384 /* Clear any current qualification; whatever comes next is the start
16385 of something new. */
16386 parser
->scope
= NULL_TREE
;
16387 parser
->qualifying_scope
= NULL_TREE
;
16388 parser
->object_scope
= NULL_TREE
;
16389 /* Look for a trailing `;' after the declaration. */
16390 if (!function_definition_p
16391 && (decl
== error_mark_node
16392 || !cp_parser_require (parser
, CPP_SEMICOLON
, "`;'")))
16393 cp_parser_skip_to_end_of_block_or_statement (parser
);
16398 /* Parse a cast-expression that is not the operand of a unary "&". */
16401 cp_parser_simple_cast_expression (cp_parser
*parser
)
16403 return cp_parser_cast_expression (parser
, /*address_p=*/false,
16407 /* Parse a functional cast to TYPE. Returns an expression
16408 representing the cast. */
16411 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
16413 tree expression_list
;
16417 = cp_parser_parenthesized_expression_list (parser
, false,
16419 /*allow_expansion_p=*/true,
16420 /*non_constant_p=*/NULL
);
16422 cast
= build_functional_cast (type
, expression_list
);
16423 /* [expr.const]/1: In an integral constant expression "only type
16424 conversions to integral or enumeration type can be used". */
16425 if (TREE_CODE (type
) == TYPE_DECL
)
16426 type
= TREE_TYPE (type
);
16427 if (cast
!= error_mark_node
16428 && !cast_valid_in_integral_constant_expression_p (type
)
16429 && (cp_parser_non_integral_constant_expression
16430 (parser
, "a call to a constructor")))
16431 return error_mark_node
;
16435 /* Save the tokens that make up the body of a member function defined
16436 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16437 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16438 specifiers applied to the declaration. Returns the FUNCTION_DECL
16439 for the member function. */
16442 cp_parser_save_member_function_body (cp_parser
* parser
,
16443 cp_decl_specifier_seq
*decl_specifiers
,
16444 cp_declarator
*declarator
,
16451 /* Create the function-declaration. */
16452 fn
= start_method (decl_specifiers
, declarator
, attributes
);
16453 /* If something went badly wrong, bail out now. */
16454 if (fn
== error_mark_node
)
16456 /* If there's a function-body, skip it. */
16457 if (cp_parser_token_starts_function_definition_p
16458 (cp_lexer_peek_token (parser
->lexer
)))
16459 cp_parser_skip_to_end_of_block_or_statement (parser
);
16460 return error_mark_node
;
16463 /* Remember it, if there default args to post process. */
16464 cp_parser_save_default_args (parser
, fn
);
16466 /* Save away the tokens that make up the body of the
16468 first
= parser
->lexer
->next_token
;
16469 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
16470 /* Handle function try blocks. */
16471 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
16472 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
16473 last
= parser
->lexer
->next_token
;
16475 /* Save away the inline definition; we will process it when the
16476 class is complete. */
16477 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
16478 DECL_PENDING_INLINE_P (fn
) = 1;
16480 /* We need to know that this was defined in the class, so that
16481 friend templates are handled correctly. */
16482 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
16484 /* We're done with the inline definition. */
16485 finish_method (fn
);
16487 /* Add FN to the queue of functions to be parsed later. */
16488 TREE_VALUE (parser
->unparsed_functions_queues
)
16489 = tree_cons (NULL_TREE
, fn
,
16490 TREE_VALUE (parser
->unparsed_functions_queues
));
16495 /* Parse a template-argument-list, as well as the trailing ">" (but
16496 not the opening ">"). See cp_parser_template_argument_list for the
16500 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
16504 tree saved_qualifying_scope
;
16505 tree saved_object_scope
;
16506 bool saved_greater_than_is_operator_p
;
16507 bool saved_skip_evaluation
;
16511 When parsing a template-id, the first non-nested `>' is taken as
16512 the end of the template-argument-list rather than a greater-than
16514 saved_greater_than_is_operator_p
16515 = parser
->greater_than_is_operator_p
;
16516 parser
->greater_than_is_operator_p
= false;
16517 /* Parsing the argument list may modify SCOPE, so we save it
16519 saved_scope
= parser
->scope
;
16520 saved_qualifying_scope
= parser
->qualifying_scope
;
16521 saved_object_scope
= parser
->object_scope
;
16522 /* We need to evaluate the template arguments, even though this
16523 template-id may be nested within a "sizeof". */
16524 saved_skip_evaluation
= skip_evaluation
;
16525 skip_evaluation
= false;
16526 /* Parse the template-argument-list itself. */
16527 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
16528 arguments
= NULL_TREE
;
16530 arguments
= cp_parser_template_argument_list (parser
);
16531 /* Look for the `>' that ends the template-argument-list. If we find
16532 a '>>' instead, it's probably just a typo. */
16533 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16535 if (!saved_greater_than_is_operator_p
)
16537 /* If we're in a nested template argument list, the '>>' has
16538 to be a typo for '> >'. We emit the error message, but we
16539 continue parsing and we push a '>' as next token, so that
16540 the argument list will be parsed correctly. Note that the
16541 global source location is still on the token before the
16542 '>>', so we need to say explicitly where we want it. */
16543 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16544 error ("%H%<>>%> should be %<> >%> "
16545 "within a nested template argument list",
16548 /* ??? Proper recovery should terminate two levels of
16549 template argument list here. */
16550 token
->type
= CPP_GREATER
;
16554 /* If this is not a nested template argument list, the '>>'
16555 is a typo for '>'. Emit an error message and continue.
16556 Same deal about the token location, but here we can get it
16557 right by consuming the '>>' before issuing the diagnostic. */
16558 cp_lexer_consume_token (parser
->lexer
);
16559 error ("spurious %<>>%>, use %<>%> to terminate "
16560 "a template argument list");
16564 cp_parser_skip_to_end_of_template_parameter_list (parser
);
16565 /* The `>' token might be a greater-than operator again now. */
16566 parser
->greater_than_is_operator_p
16567 = saved_greater_than_is_operator_p
;
16568 /* Restore the SAVED_SCOPE. */
16569 parser
->scope
= saved_scope
;
16570 parser
->qualifying_scope
= saved_qualifying_scope
;
16571 parser
->object_scope
= saved_object_scope
;
16572 skip_evaluation
= saved_skip_evaluation
;
16577 /* MEMBER_FUNCTION is a member function, or a friend. If default
16578 arguments, or the body of the function have not yet been parsed,
16582 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
16584 /* If this member is a template, get the underlying
16586 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
16587 member_function
= DECL_TEMPLATE_RESULT (member_function
);
16589 /* There should not be any class definitions in progress at this
16590 point; the bodies of members are only parsed outside of all class
16592 gcc_assert (parser
->num_classes_being_defined
== 0);
16593 /* While we're parsing the member functions we might encounter more
16594 classes. We want to handle them right away, but we don't want
16595 them getting mixed up with functions that are currently in the
16597 parser
->unparsed_functions_queues
16598 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
16600 /* Make sure that any template parameters are in scope. */
16601 maybe_begin_member_template_processing (member_function
);
16603 /* If the body of the function has not yet been parsed, parse it
16605 if (DECL_PENDING_INLINE_P (member_function
))
16607 tree function_scope
;
16608 cp_token_cache
*tokens
;
16610 /* The function is no longer pending; we are processing it. */
16611 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
16612 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
16613 DECL_PENDING_INLINE_P (member_function
) = 0;
16615 /* If this is a local class, enter the scope of the containing
16617 function_scope
= current_function_decl
;
16618 if (function_scope
)
16619 push_function_context_to (function_scope
);
16622 /* Push the body of the function onto the lexer stack. */
16623 cp_parser_push_lexer_for_tokens (parser
, tokens
);
16625 /* Let the front end know that we going to be defining this
16627 start_preparsed_function (member_function
, NULL_TREE
,
16628 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
16630 /* Don't do access checking if it is a templated function. */
16631 if (processing_template_decl
)
16632 push_deferring_access_checks (dk_no_check
);
16634 /* Now, parse the body of the function. */
16635 cp_parser_function_definition_after_declarator (parser
,
16636 /*inline_p=*/true);
16638 if (processing_template_decl
)
16639 pop_deferring_access_checks ();
16641 /* Leave the scope of the containing function. */
16642 if (function_scope
)
16643 pop_function_context_from (function_scope
);
16644 cp_parser_pop_lexer (parser
);
16647 /* Remove any template parameters from the symbol table. */
16648 maybe_end_member_template_processing ();
16650 /* Restore the queue. */
16651 parser
->unparsed_functions_queues
16652 = TREE_CHAIN (parser
->unparsed_functions_queues
);
16655 /* If DECL contains any default args, remember it on the unparsed
16656 functions queue. */
16659 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
16663 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
16665 probe
= TREE_CHAIN (probe
))
16666 if (TREE_PURPOSE (probe
))
16668 TREE_PURPOSE (parser
->unparsed_functions_queues
)
16669 = tree_cons (current_class_type
, decl
,
16670 TREE_PURPOSE (parser
->unparsed_functions_queues
));
16675 /* FN is a FUNCTION_DECL which may contains a parameter with an
16676 unparsed DEFAULT_ARG. Parse the default args now. This function
16677 assumes that the current scope is the scope in which the default
16678 argument should be processed. */
16681 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
16683 bool saved_local_variables_forbidden_p
;
16686 /* While we're parsing the default args, we might (due to the
16687 statement expression extension) encounter more classes. We want
16688 to handle them right away, but we don't want them getting mixed
16689 up with default args that are currently in the queue. */
16690 parser
->unparsed_functions_queues
16691 = tree_cons (NULL_TREE
, NULL_TREE
, parser
->unparsed_functions_queues
);
16693 /* Local variable names (and the `this' keyword) may not appear
16694 in a default argument. */
16695 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
16696 parser
->local_variables_forbidden_p
= true;
16698 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
16700 parm
= TREE_CHAIN (parm
))
16702 cp_token_cache
*tokens
;
16703 tree default_arg
= TREE_PURPOSE (parm
);
16705 VEC(tree
,gc
) *insts
;
16712 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
16713 /* This can happen for a friend declaration for a function
16714 already declared with default arguments. */
16717 /* Push the saved tokens for the default argument onto the parser's
16719 tokens
= DEFARG_TOKENS (default_arg
);
16720 cp_parser_push_lexer_for_tokens (parser
, tokens
);
16722 /* Parse the assignment-expression. */
16723 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false);
16725 if (!processing_template_decl
)
16726 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
16728 TREE_PURPOSE (parm
) = parsed_arg
;
16730 /* Update any instantiations we've already created. */
16731 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
16732 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
16733 TREE_PURPOSE (copy
) = parsed_arg
;
16735 /* If the token stream has not been completely used up, then
16736 there was extra junk after the end of the default
16738 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
16739 cp_parser_error (parser
, "expected %<,%>");
16741 /* Revert to the main lexer. */
16742 cp_parser_pop_lexer (parser
);
16745 /* Make sure no default arg is missing. */
16746 check_default_args (fn
);
16748 /* Restore the state of local_variables_forbidden_p. */
16749 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
16751 /* Restore the queue. */
16752 parser
->unparsed_functions_queues
16753 = TREE_CHAIN (parser
->unparsed_functions_queues
);
16756 /* Parse the operand of `sizeof' (or a similar operator). Returns
16757 either a TYPE or an expression, depending on the form of the
16758 input. The KEYWORD indicates which kind of expression we have
16762 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
16764 static const char *format
;
16765 tree expr
= NULL_TREE
;
16766 const char *saved_message
;
16767 bool saved_integral_constant_expression_p
;
16768 bool saved_non_integral_constant_expression_p
;
16769 bool pack_expansion_p
= false;
16771 /* Initialize FORMAT the first time we get here. */
16773 format
= "types may not be defined in '%s' expressions";
16775 /* Types cannot be defined in a `sizeof' expression. Save away the
16777 saved_message
= parser
->type_definition_forbidden_message
;
16778 /* And create the new one. */
16779 parser
->type_definition_forbidden_message
16780 = XNEWVEC (const char, strlen (format
)
16781 + strlen (IDENTIFIER_POINTER (ridpointers
[keyword
]))
16783 sprintf ((char *) parser
->type_definition_forbidden_message
,
16784 format
, IDENTIFIER_POINTER (ridpointers
[keyword
]));
16786 /* The restrictions on constant-expressions do not apply inside
16787 sizeof expressions. */
16788 saved_integral_constant_expression_p
16789 = parser
->integral_constant_expression_p
;
16790 saved_non_integral_constant_expression_p
16791 = parser
->non_integral_constant_expression_p
;
16792 parser
->integral_constant_expression_p
= false;
16794 /* If it's a `...', then we are computing the length of a parameter
16796 if (keyword
== RID_SIZEOF
16797 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16799 /* Consume the `...'. */
16800 cp_lexer_consume_token (parser
->lexer
);
16801 maybe_warn_variadic_templates ();
16803 /* Note that this is an expansion. */
16804 pack_expansion_p
= true;
16807 /* Do not actually evaluate the expression. */
16809 /* If it's a `(', then we might be looking at the type-id
16811 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16814 bool saved_in_type_id_in_expr_p
;
16816 /* We can't be sure yet whether we're looking at a type-id or an
16818 cp_parser_parse_tentatively (parser
);
16819 /* Consume the `('. */
16820 cp_lexer_consume_token (parser
->lexer
);
16821 /* Parse the type-id. */
16822 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
16823 parser
->in_type_id_in_expr_p
= true;
16824 type
= cp_parser_type_id (parser
);
16825 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
16826 /* Now, look for the trailing `)'. */
16827 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "%<)%>");
16828 /* If all went well, then we're done. */
16829 if (cp_parser_parse_definitely (parser
))
16831 cp_decl_specifier_seq decl_specs
;
16833 /* Build a trivial decl-specifier-seq. */
16834 clear_decl_specs (&decl_specs
);
16835 decl_specs
.type
= type
;
16837 /* Call grokdeclarator to figure out what type this is. */
16838 expr
= grokdeclarator (NULL
,
16842 /*attrlist=*/NULL
);
16846 /* If the type-id production did not work out, then we must be
16847 looking at the unary-expression production. */
16849 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
16852 if (pack_expansion_p
)
16853 /* Build a pack expansion. */
16854 expr
= make_pack_expansion (expr
);
16856 /* Go back to evaluating expressions. */
16859 /* Free the message we created. */
16860 free ((char *) parser
->type_definition_forbidden_message
);
16861 /* And restore the old one. */
16862 parser
->type_definition_forbidden_message
= saved_message
;
16863 parser
->integral_constant_expression_p
16864 = saved_integral_constant_expression_p
;
16865 parser
->non_integral_constant_expression_p
16866 = saved_non_integral_constant_expression_p
;
16871 /* If the current declaration has no declarator, return true. */
16874 cp_parser_declares_only_class_p (cp_parser
*parser
)
16876 /* If the next token is a `;' or a `,' then there is no
16878 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
16879 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16882 /* Update the DECL_SPECS to reflect the storage class indicated by
16886 cp_parser_set_storage_class (cp_parser
*parser
,
16887 cp_decl_specifier_seq
*decl_specs
,
16890 cp_storage_class storage_class
;
16892 if (parser
->in_unbraced_linkage_specification_p
)
16894 error ("invalid use of %qD in linkage specification",
16895 ridpointers
[keyword
]);
16898 else if (decl_specs
->storage_class
!= sc_none
)
16900 decl_specs
->conflicting_specifiers_p
= true;
16904 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
16905 && decl_specs
->specs
[(int) ds_thread
])
16907 error ("%<__thread%> before %qD", ridpointers
[keyword
]);
16908 decl_specs
->specs
[(int) ds_thread
] = 0;
16914 storage_class
= sc_auto
;
16917 storage_class
= sc_register
;
16920 storage_class
= sc_static
;
16923 storage_class
= sc_extern
;
16926 storage_class
= sc_mutable
;
16929 gcc_unreachable ();
16931 decl_specs
->storage_class
= storage_class
;
16933 /* A storage class specifier cannot be applied alongside a typedef
16934 specifier. If there is a typedef specifier present then set
16935 conflicting_specifiers_p which will trigger an error later
16936 on in grokdeclarator. */
16937 if (decl_specs
->specs
[(int)ds_typedef
])
16938 decl_specs
->conflicting_specifiers_p
= true;
16941 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16942 is true, the type is a user-defined type; otherwise it is a
16943 built-in type specified by a keyword. */
16946 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
16948 bool user_defined_p
)
16950 decl_specs
->any_specifiers_p
= true;
16952 /* If the user tries to redeclare bool or wchar_t (with, for
16953 example, in "typedef int wchar_t;") we remember that this is what
16954 happened. In system headers, we ignore these declarations so
16955 that G++ can work with system headers that are not C++-safe. */
16956 if (decl_specs
->specs
[(int) ds_typedef
]
16958 && (type_spec
== boolean_type_node
16959 || type_spec
== wchar_type_node
)
16960 && (decl_specs
->type
16961 || decl_specs
->specs
[(int) ds_long
]
16962 || decl_specs
->specs
[(int) ds_short
]
16963 || decl_specs
->specs
[(int) ds_unsigned
]
16964 || decl_specs
->specs
[(int) ds_signed
]))
16966 decl_specs
->redefined_builtin_type
= type_spec
;
16967 if (!decl_specs
->type
)
16969 decl_specs
->type
= type_spec
;
16970 decl_specs
->user_defined_type_p
= false;
16973 else if (decl_specs
->type
)
16974 decl_specs
->multiple_types_p
= true;
16977 decl_specs
->type
= type_spec
;
16978 decl_specs
->user_defined_type_p
= user_defined_p
;
16979 decl_specs
->redefined_builtin_type
= NULL_TREE
;
16983 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16984 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16987 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
16989 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
16992 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16993 issue an error message indicating that TOKEN_DESC was expected.
16995 Returns the token consumed, if the token had the appropriate type.
16996 Otherwise, returns NULL. */
16999 cp_parser_require (cp_parser
* parser
,
17000 enum cpp_ttype type
,
17001 const char* token_desc
)
17003 if (cp_lexer_next_token_is (parser
->lexer
, type
))
17004 return cp_lexer_consume_token (parser
->lexer
);
17007 /* Output the MESSAGE -- unless we're parsing tentatively. */
17008 if (!cp_parser_simulate_error (parser
))
17010 char *message
= concat ("expected ", token_desc
, NULL
);
17011 cp_parser_error (parser
, message
);
17018 /* An error message is produced if the next token is not '>'.
17019 All further tokens are skipped until the desired token is
17020 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17023 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
17025 /* Current level of '< ... >'. */
17026 unsigned level
= 0;
17027 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17028 unsigned nesting_depth
= 0;
17030 /* Are we ready, yet? If not, issue error message. */
17031 if (cp_parser_require (parser
, CPP_GREATER
, "%<>%>"))
17034 /* Skip tokens until the desired token is found. */
17037 /* Peek at the next token. */
17038 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
17041 if (!nesting_depth
)
17046 if (!nesting_depth
&& level
-- == 0)
17048 /* We've reached the token we want, consume it and stop. */
17049 cp_lexer_consume_token (parser
->lexer
);
17054 case CPP_OPEN_PAREN
:
17055 case CPP_OPEN_SQUARE
:
17059 case CPP_CLOSE_PAREN
:
17060 case CPP_CLOSE_SQUARE
:
17061 if (nesting_depth
-- == 0)
17066 case CPP_PRAGMA_EOL
:
17067 case CPP_SEMICOLON
:
17068 case CPP_OPEN_BRACE
:
17069 case CPP_CLOSE_BRACE
:
17070 /* The '>' was probably forgotten, don't look further. */
17077 /* Consume this token. */
17078 cp_lexer_consume_token (parser
->lexer
);
17082 /* If the next token is the indicated keyword, consume it. Otherwise,
17083 issue an error message indicating that TOKEN_DESC was expected.
17085 Returns the token consumed, if the token had the appropriate type.
17086 Otherwise, returns NULL. */
17089 cp_parser_require_keyword (cp_parser
* parser
,
17091 const char* token_desc
)
17093 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
17095 if (token
&& token
->keyword
!= keyword
)
17097 dyn_string_t error_msg
;
17099 /* Format the error message. */
17100 error_msg
= dyn_string_new (0);
17101 dyn_string_append_cstr (error_msg
, "expected ");
17102 dyn_string_append_cstr (error_msg
, token_desc
);
17103 cp_parser_error (parser
, error_msg
->s
);
17104 dyn_string_delete (error_msg
);
17111 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17112 function-definition. */
17115 cp_parser_token_starts_function_definition_p (cp_token
* token
)
17117 return (/* An ordinary function-body begins with an `{'. */
17118 token
->type
== CPP_OPEN_BRACE
17119 /* A ctor-initializer begins with a `:'. */
17120 || token
->type
== CPP_COLON
17121 /* A function-try-block begins with `try'. */
17122 || token
->keyword
== RID_TRY
17123 /* The named return value extension begins with `return'. */
17124 || token
->keyword
== RID_RETURN
);
17127 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17131 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
17135 token
= cp_lexer_peek_token (parser
->lexer
);
17136 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
17139 /* Returns TRUE iff the next token is the "," or ">" ending a
17140 template-argument. */
17143 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
17147 token
= cp_lexer_peek_token (parser
->lexer
);
17148 return (token
->type
== CPP_COMMA
17149 || token
->type
== CPP_GREATER
17150 || token
->type
== CPP_ELLIPSIS
);
17153 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17154 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17157 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
17162 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
17163 if (token
->type
== CPP_LESS
)
17165 /* Check for the sequence `<::' in the original code. It would be lexed as
17166 `[:', where `[' is a digraph, and there is no whitespace before
17168 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
17171 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
17172 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
17178 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17179 or none_type otherwise. */
17181 static enum tag_types
17182 cp_parser_token_is_class_key (cp_token
* token
)
17184 switch (token
->keyword
)
17189 return record_type
;
17198 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17201 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
17203 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
17204 pedwarn ("%qs tag used in naming %q#T",
17205 class_key
== union_type
? "union"
17206 : class_key
== record_type
? "struct" : "class",
17210 /* Issue an error message if DECL is redeclared with different
17211 access than its original declaration [class.access.spec/3].
17212 This applies to nested classes and nested class templates.
17216 cp_parser_check_access_in_redeclaration (tree decl
)
17218 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
17221 if ((TREE_PRIVATE (decl
)
17222 != (current_access_specifier
== access_private_node
))
17223 || (TREE_PROTECTED (decl
)
17224 != (current_access_specifier
== access_protected_node
)))
17225 error ("%qD redeclared with different access", decl
);
17228 /* Look for the `template' keyword, as a syntactic disambiguator.
17229 Return TRUE iff it is present, in which case it will be
17233 cp_parser_optional_template_keyword (cp_parser
*parser
)
17235 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17237 /* The `template' keyword can only be used within templates;
17238 outside templates the parser can always figure out what is a
17239 template and what is not. */
17240 if (!processing_template_decl
)
17242 error ("%<template%> (as a disambiguator) is only allowed "
17243 "within templates");
17244 /* If this part of the token stream is rescanned, the same
17245 error message would be generated. So, we purge the token
17246 from the stream. */
17247 cp_lexer_purge_token (parser
->lexer
);
17252 /* Consume the `template' keyword. */
17253 cp_lexer_consume_token (parser
->lexer
);
17261 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17262 set PARSER->SCOPE, and perform other related actions. */
17265 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
17268 struct tree_check
*check_value
;
17269 deferred_access_check
*chk
;
17270 VEC (deferred_access_check
,gc
) *checks
;
17272 /* Get the stored value. */
17273 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
17274 /* Perform any access checks that were deferred. */
17275 checks
= check_value
->checks
;
17279 VEC_iterate (deferred_access_check
, checks
, i
, chk
) ;
17282 perform_or_defer_access_check (chk
->binfo
,
17287 /* Set the scope from the stored value. */
17288 parser
->scope
= check_value
->value
;
17289 parser
->qualifying_scope
= check_value
->qualifying_scope
;
17290 parser
->object_scope
= NULL_TREE
;
17293 /* Consume tokens up through a non-nested END token. */
17296 cp_parser_cache_group (cp_parser
*parser
,
17297 enum cpp_ttype end
,
17304 /* Abort a parenthesized expression if we encounter a brace. */
17305 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
17306 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17308 /* If we've reached the end of the file, stop. */
17309 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
)
17310 || (end
!= CPP_PRAGMA_EOL
17311 && cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)))
17313 /* Consume the next token. */
17314 token
= cp_lexer_consume_token (parser
->lexer
);
17315 /* See if it starts a new group. */
17316 if (token
->type
== CPP_OPEN_BRACE
)
17318 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
17322 else if (token
->type
== CPP_OPEN_PAREN
)
17323 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
17324 else if (token
->type
== CPP_PRAGMA
)
17325 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
17326 else if (token
->type
== end
)
17331 /* Begin parsing tentatively. We always save tokens while parsing
17332 tentatively so that if the tentative parsing fails we can restore the
17336 cp_parser_parse_tentatively (cp_parser
* parser
)
17338 /* Enter a new parsing context. */
17339 parser
->context
= cp_parser_context_new (parser
->context
);
17340 /* Begin saving tokens. */
17341 cp_lexer_save_tokens (parser
->lexer
);
17342 /* In order to avoid repetitive access control error messages,
17343 access checks are queued up until we are no longer parsing
17345 push_deferring_access_checks (dk_deferred
);
17348 /* Commit to the currently active tentative parse. */
17351 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
17353 cp_parser_context
*context
;
17356 /* Mark all of the levels as committed. */
17357 lexer
= parser
->lexer
;
17358 for (context
= parser
->context
; context
->next
; context
= context
->next
)
17360 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
17362 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
17363 while (!cp_lexer_saving_tokens (lexer
))
17364 lexer
= lexer
->next
;
17365 cp_lexer_commit_tokens (lexer
);
17369 /* Abort the currently active tentative parse. All consumed tokens
17370 will be rolled back, and no diagnostics will be issued. */
17373 cp_parser_abort_tentative_parse (cp_parser
* parser
)
17375 cp_parser_simulate_error (parser
);
17376 /* Now, pretend that we want to see if the construct was
17377 successfully parsed. */
17378 cp_parser_parse_definitely (parser
);
17381 /* Stop parsing tentatively. If a parse error has occurred, restore the
17382 token stream. Otherwise, commit to the tokens we have consumed.
17383 Returns true if no error occurred; false otherwise. */
17386 cp_parser_parse_definitely (cp_parser
* parser
)
17388 bool error_occurred
;
17389 cp_parser_context
*context
;
17391 /* Remember whether or not an error occurred, since we are about to
17392 destroy that information. */
17393 error_occurred
= cp_parser_error_occurred (parser
);
17394 /* Remove the topmost context from the stack. */
17395 context
= parser
->context
;
17396 parser
->context
= context
->next
;
17397 /* If no parse errors occurred, commit to the tentative parse. */
17398 if (!error_occurred
)
17400 /* Commit to the tokens read tentatively, unless that was
17402 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
17403 cp_lexer_commit_tokens (parser
->lexer
);
17405 pop_to_parent_deferring_access_checks ();
17407 /* Otherwise, if errors occurred, roll back our state so that things
17408 are just as they were before we began the tentative parse. */
17411 cp_lexer_rollback_tokens (parser
->lexer
);
17412 pop_deferring_access_checks ();
17414 /* Add the context to the front of the free list. */
17415 context
->next
= cp_parser_context_free_list
;
17416 cp_parser_context_free_list
= context
;
17418 return !error_occurred
;
17421 /* Returns true if we are parsing tentatively and are not committed to
17422 this tentative parse. */
17425 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
17427 return (cp_parser_parsing_tentatively (parser
)
17428 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
17431 /* Returns nonzero iff an error has occurred during the most recent
17432 tentative parse. */
17435 cp_parser_error_occurred (cp_parser
* parser
)
17437 return (cp_parser_parsing_tentatively (parser
)
17438 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
17441 /* Returns nonzero if GNU extensions are allowed. */
17444 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
17446 return parser
->allow_gnu_extensions_p
;
17449 /* Objective-C++ Productions */
17452 /* Parse an Objective-C expression, which feeds into a primary-expression
17456 objc-message-expression
17457 objc-string-literal
17458 objc-encode-expression
17459 objc-protocol-expression
17460 objc-selector-expression
17462 Returns a tree representation of the expression. */
17465 cp_parser_objc_expression (cp_parser
* parser
)
17467 /* Try to figure out what kind of declaration is present. */
17468 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
17472 case CPP_OPEN_SQUARE
:
17473 return cp_parser_objc_message_expression (parser
);
17475 case CPP_OBJC_STRING
:
17476 kwd
= cp_lexer_consume_token (parser
->lexer
);
17477 return objc_build_string_object (kwd
->u
.value
);
17480 switch (kwd
->keyword
)
17482 case RID_AT_ENCODE
:
17483 return cp_parser_objc_encode_expression (parser
);
17485 case RID_AT_PROTOCOL
:
17486 return cp_parser_objc_protocol_expression (parser
);
17488 case RID_AT_SELECTOR
:
17489 return cp_parser_objc_selector_expression (parser
);
17495 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->u
.value
);
17496 cp_parser_skip_to_end_of_block_or_statement (parser
);
17499 return error_mark_node
;
17502 /* Parse an Objective-C message expression.
17504 objc-message-expression:
17505 [ objc-message-receiver objc-message-args ]
17507 Returns a representation of an Objective-C message. */
17510 cp_parser_objc_message_expression (cp_parser
* parser
)
17512 tree receiver
, messageargs
;
17514 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
17515 receiver
= cp_parser_objc_message_receiver (parser
);
17516 messageargs
= cp_parser_objc_message_args (parser
);
17517 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, "`]'");
17519 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
17522 /* Parse an objc-message-receiver.
17524 objc-message-receiver:
17526 simple-type-specifier
17528 Returns a representation of the type or expression. */
17531 cp_parser_objc_message_receiver (cp_parser
* parser
)
17535 /* An Objective-C message receiver may be either (1) a type
17536 or (2) an expression. */
17537 cp_parser_parse_tentatively (parser
);
17538 rcv
= cp_parser_expression (parser
, false);
17540 if (cp_parser_parse_definitely (parser
))
17543 rcv
= cp_parser_simple_type_specifier (parser
,
17544 /*decl_specs=*/NULL
,
17545 CP_PARSER_FLAGS_NONE
);
17547 return objc_get_class_reference (rcv
);
17550 /* Parse the arguments and selectors comprising an Objective-C message.
17555 objc-selector-args , objc-comma-args
17557 objc-selector-args:
17558 objc-selector [opt] : assignment-expression
17559 objc-selector-args objc-selector [opt] : assignment-expression
17562 assignment-expression
17563 objc-comma-args , assignment-expression
17565 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17566 selector arguments and TREE_VALUE containing a list of comma
17570 cp_parser_objc_message_args (cp_parser
* parser
)
17572 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
17573 bool maybe_unary_selector_p
= true;
17574 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17576 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
17578 tree selector
= NULL_TREE
, arg
;
17580 if (token
->type
!= CPP_COLON
)
17581 selector
= cp_parser_objc_selector (parser
);
17583 /* Detect if we have a unary selector. */
17584 if (maybe_unary_selector_p
17585 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
17586 return build_tree_list (selector
, NULL_TREE
);
17588 maybe_unary_selector_p
= false;
17589 cp_parser_require (parser
, CPP_COLON
, "`:'");
17590 arg
= cp_parser_assignment_expression (parser
, false);
17593 = chainon (sel_args
,
17594 build_tree_list (selector
, arg
));
17596 token
= cp_lexer_peek_token (parser
->lexer
);
17599 /* Handle non-selector arguments, if any. */
17600 while (token
->type
== CPP_COMMA
)
17604 cp_lexer_consume_token (parser
->lexer
);
17605 arg
= cp_parser_assignment_expression (parser
, false);
17608 = chainon (addl_args
,
17609 build_tree_list (NULL_TREE
, arg
));
17611 token
= cp_lexer_peek_token (parser
->lexer
);
17614 return build_tree_list (sel_args
, addl_args
);
17617 /* Parse an Objective-C encode expression.
17619 objc-encode-expression:
17620 @encode objc-typename
17622 Returns an encoded representation of the type argument. */
17625 cp_parser_objc_encode_expression (cp_parser
* parser
)
17629 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
17630 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17631 type
= complete_type (cp_parser_type_id (parser
));
17632 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17636 error ("%<@encode%> must specify a type as an argument");
17637 return error_mark_node
;
17640 return objc_build_encode_expr (type
);
17643 /* Parse an Objective-C @defs expression. */
17646 cp_parser_objc_defs_expression (cp_parser
*parser
)
17650 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
17651 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17652 name
= cp_parser_identifier (parser
);
17653 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17655 return objc_get_class_ivars (name
);
17658 /* Parse an Objective-C protocol expression.
17660 objc-protocol-expression:
17661 @protocol ( identifier )
17663 Returns a representation of the protocol expression. */
17666 cp_parser_objc_protocol_expression (cp_parser
* parser
)
17670 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
17671 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17672 proto
= cp_parser_identifier (parser
);
17673 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17675 return objc_build_protocol_expr (proto
);
17678 /* Parse an Objective-C selector expression.
17680 objc-selector-expression:
17681 @selector ( objc-method-signature )
17683 objc-method-signature:
17689 objc-selector-seq objc-selector :
17691 Returns a representation of the method selector. */
17694 cp_parser_objc_selector_expression (cp_parser
* parser
)
17696 tree sel_seq
= NULL_TREE
;
17697 bool maybe_unary_selector_p
= true;
17700 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
17701 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
17702 token
= cp_lexer_peek_token (parser
->lexer
);
17704 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
17705 || token
->type
== CPP_SCOPE
)
17707 tree selector
= NULL_TREE
;
17709 if (token
->type
!= CPP_COLON
17710 || token
->type
== CPP_SCOPE
)
17711 selector
= cp_parser_objc_selector (parser
);
17713 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
17714 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
17716 /* Detect if we have a unary selector. */
17717 if (maybe_unary_selector_p
)
17719 sel_seq
= selector
;
17720 goto finish_selector
;
17724 cp_parser_error (parser
, "expected %<:%>");
17727 maybe_unary_selector_p
= false;
17728 token
= cp_lexer_consume_token (parser
->lexer
);
17730 if (token
->type
== CPP_SCOPE
)
17733 = chainon (sel_seq
,
17734 build_tree_list (selector
, NULL_TREE
));
17736 = chainon (sel_seq
,
17737 build_tree_list (NULL_TREE
, NULL_TREE
));
17741 = chainon (sel_seq
,
17742 build_tree_list (selector
, NULL_TREE
));
17744 token
= cp_lexer_peek_token (parser
->lexer
);
17748 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17750 return objc_build_selector_expr (sel_seq
);
17753 /* Parse a list of identifiers.
17755 objc-identifier-list:
17757 objc-identifier-list , identifier
17759 Returns a TREE_LIST of identifier nodes. */
17762 cp_parser_objc_identifier_list (cp_parser
* parser
)
17764 tree list
= build_tree_list (NULL_TREE
, cp_parser_identifier (parser
));
17765 cp_token
*sep
= cp_lexer_peek_token (parser
->lexer
);
17767 while (sep
->type
== CPP_COMMA
)
17769 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
17770 list
= chainon (list
,
17771 build_tree_list (NULL_TREE
,
17772 cp_parser_identifier (parser
)));
17773 sep
= cp_lexer_peek_token (parser
->lexer
);
17779 /* Parse an Objective-C alias declaration.
17781 objc-alias-declaration:
17782 @compatibility_alias identifier identifier ;
17784 This function registers the alias mapping with the Objective-C front end.
17785 It returns nothing. */
17788 cp_parser_objc_alias_declaration (cp_parser
* parser
)
17792 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
17793 alias
= cp_parser_identifier (parser
);
17794 orig
= cp_parser_identifier (parser
);
17795 objc_declare_alias (alias
, orig
);
17796 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17799 /* Parse an Objective-C class forward-declaration.
17801 objc-class-declaration:
17802 @class objc-identifier-list ;
17804 The function registers the forward declarations with the Objective-C
17805 front end. It returns nothing. */
17808 cp_parser_objc_class_declaration (cp_parser
* parser
)
17810 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
17811 objc_declare_class (cp_parser_objc_identifier_list (parser
));
17812 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17815 /* Parse a list of Objective-C protocol references.
17817 objc-protocol-refs-opt:
17818 objc-protocol-refs [opt]
17820 objc-protocol-refs:
17821 < objc-identifier-list >
17823 Returns a TREE_LIST of identifiers, if any. */
17826 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
17828 tree protorefs
= NULL_TREE
;
17830 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
17832 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
17833 protorefs
= cp_parser_objc_identifier_list (parser
);
17834 cp_parser_require (parser
, CPP_GREATER
, "`>'");
17840 /* Parse a Objective-C visibility specification. */
17843 cp_parser_objc_visibility_spec (cp_parser
* parser
)
17845 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
17847 switch (vis
->keyword
)
17849 case RID_AT_PRIVATE
:
17850 objc_set_visibility (2);
17852 case RID_AT_PROTECTED
:
17853 objc_set_visibility (0);
17855 case RID_AT_PUBLIC
:
17856 objc_set_visibility (1);
17862 /* Eat '@private'/'@protected'/'@public'. */
17863 cp_lexer_consume_token (parser
->lexer
);
17866 /* Parse an Objective-C method type. */
17869 cp_parser_objc_method_type (cp_parser
* parser
)
17871 objc_set_method_type
17872 (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
17877 /* Parse an Objective-C protocol qualifier. */
17880 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
17882 tree quals
= NULL_TREE
, node
;
17883 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17885 node
= token
->u
.value
;
17887 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
17888 && (node
== ridpointers
[(int) RID_IN
]
17889 || node
== ridpointers
[(int) RID_OUT
]
17890 || node
== ridpointers
[(int) RID_INOUT
]
17891 || node
== ridpointers
[(int) RID_BYCOPY
]
17892 || node
== ridpointers
[(int) RID_BYREF
]
17893 || node
== ridpointers
[(int) RID_ONEWAY
]))
17895 quals
= tree_cons (NULL_TREE
, node
, quals
);
17896 cp_lexer_consume_token (parser
->lexer
);
17897 token
= cp_lexer_peek_token (parser
->lexer
);
17898 node
= token
->u
.value
;
17904 /* Parse an Objective-C typename. */
17907 cp_parser_objc_typename (cp_parser
* parser
)
17909 tree typename
= NULL_TREE
;
17911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17913 tree proto_quals
, cp_type
= NULL_TREE
;
17915 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
17916 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
17918 /* An ObjC type name may consist of just protocol qualifiers, in which
17919 case the type shall default to 'id'. */
17920 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
17921 cp_type
= cp_parser_type_id (parser
);
17923 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
17924 typename
= build_tree_list (proto_quals
, cp_type
);
17930 /* Check to see if TYPE refers to an Objective-C selector name. */
17933 cp_parser_objc_selector_p (enum cpp_ttype type
)
17935 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
17936 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
17937 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
17938 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
17939 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
17942 /* Parse an Objective-C selector. */
17945 cp_parser_objc_selector (cp_parser
* parser
)
17947 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
17949 if (!cp_parser_objc_selector_p (token
->type
))
17951 error ("invalid Objective-C++ selector name");
17952 return error_mark_node
;
17955 /* C++ operator names are allowed to appear in ObjC selectors. */
17956 switch (token
->type
)
17958 case CPP_AND_AND
: return get_identifier ("and");
17959 case CPP_AND_EQ
: return get_identifier ("and_eq");
17960 case CPP_AND
: return get_identifier ("bitand");
17961 case CPP_OR
: return get_identifier ("bitor");
17962 case CPP_COMPL
: return get_identifier ("compl");
17963 case CPP_NOT
: return get_identifier ("not");
17964 case CPP_NOT_EQ
: return get_identifier ("not_eq");
17965 case CPP_OR_OR
: return get_identifier ("or");
17966 case CPP_OR_EQ
: return get_identifier ("or_eq");
17967 case CPP_XOR
: return get_identifier ("xor");
17968 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
17969 default: return token
->u
.value
;
17973 /* Parse an Objective-C params list. */
17976 cp_parser_objc_method_keyword_params (cp_parser
* parser
)
17978 tree params
= NULL_TREE
;
17979 bool maybe_unary_selector_p
= true;
17980 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17982 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
17984 tree selector
= NULL_TREE
, typename
, identifier
;
17986 if (token
->type
!= CPP_COLON
)
17987 selector
= cp_parser_objc_selector (parser
);
17989 /* Detect if we have a unary selector. */
17990 if (maybe_unary_selector_p
17991 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
17994 maybe_unary_selector_p
= false;
17995 cp_parser_require (parser
, CPP_COLON
, "`:'");
17996 typename
= cp_parser_objc_typename (parser
);
17997 identifier
= cp_parser_identifier (parser
);
18001 objc_build_keyword_decl (selector
,
18005 token
= cp_lexer_peek_token (parser
->lexer
);
18011 /* Parse the non-keyword Objective-C params. */
18014 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
)
18016 tree params
= make_node (TREE_LIST
);
18017 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18018 *ellipsisp
= false; /* Initially, assume no ellipsis. */
18020 while (token
->type
== CPP_COMMA
)
18022 cp_parameter_declarator
*parmdecl
;
18025 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
18026 token
= cp_lexer_peek_token (parser
->lexer
);
18028 if (token
->type
== CPP_ELLIPSIS
)
18030 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
18035 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
18036 parm
= grokdeclarator (parmdecl
->declarator
,
18037 &parmdecl
->decl_specifiers
,
18038 PARM
, /*initialized=*/0,
18039 /*attrlist=*/NULL
);
18041 chainon (params
, build_tree_list (NULL_TREE
, parm
));
18042 token
= cp_lexer_peek_token (parser
->lexer
);
18048 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18051 cp_parser_objc_interstitial_code (cp_parser
* parser
)
18053 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18055 /* If the next token is `extern' and the following token is a string
18056 literal, then we have a linkage specification. */
18057 if (token
->keyword
== RID_EXTERN
18058 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
18059 cp_parser_linkage_specification (parser
);
18060 /* Handle #pragma, if any. */
18061 else if (token
->type
== CPP_PRAGMA
)
18062 cp_parser_pragma (parser
, pragma_external
);
18063 /* Allow stray semicolons. */
18064 else if (token
->type
== CPP_SEMICOLON
)
18065 cp_lexer_consume_token (parser
->lexer
);
18066 /* Finally, try to parse a block-declaration, or a function-definition. */
18068 cp_parser_block_declaration (parser
, /*statement_p=*/false);
18071 /* Parse a method signature. */
18074 cp_parser_objc_method_signature (cp_parser
* parser
)
18076 tree rettype
, kwdparms
, optparms
;
18077 bool ellipsis
= false;
18079 cp_parser_objc_method_type (parser
);
18080 rettype
= cp_parser_objc_typename (parser
);
18081 kwdparms
= cp_parser_objc_method_keyword_params (parser
);
18082 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
);
18084 return objc_build_method_signature (rettype
, kwdparms
, optparms
, ellipsis
);
18087 /* Pars an Objective-C method prototype list. */
18090 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
18092 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18094 while (token
->keyword
!= RID_AT_END
)
18096 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
18098 objc_add_method_declaration
18099 (cp_parser_objc_method_signature (parser
));
18100 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18103 /* Allow for interspersed non-ObjC++ code. */
18104 cp_parser_objc_interstitial_code (parser
);
18106 token
= cp_lexer_peek_token (parser
->lexer
);
18109 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
18110 objc_finish_interface ();
18113 /* Parse an Objective-C method definition list. */
18116 cp_parser_objc_method_definition_list (cp_parser
* parser
)
18118 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18120 while (token
->keyword
!= RID_AT_END
)
18124 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
18126 push_deferring_access_checks (dk_deferred
);
18127 objc_start_method_definition
18128 (cp_parser_objc_method_signature (parser
));
18130 /* For historical reasons, we accept an optional semicolon. */
18131 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18132 cp_lexer_consume_token (parser
->lexer
);
18134 perform_deferred_access_checks ();
18135 stop_deferring_access_checks ();
18136 meth
= cp_parser_function_definition_after_declarator (parser
,
18138 pop_deferring_access_checks ();
18139 objc_finish_method_definition (meth
);
18142 /* Allow for interspersed non-ObjC++ code. */
18143 cp_parser_objc_interstitial_code (parser
);
18145 token
= cp_lexer_peek_token (parser
->lexer
);
18148 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
18149 objc_finish_implementation ();
18152 /* Parse Objective-C ivars. */
18155 cp_parser_objc_class_ivars (cp_parser
* parser
)
18157 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18159 if (token
->type
!= CPP_OPEN_BRACE
)
18160 return; /* No ivars specified. */
18162 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
18163 token
= cp_lexer_peek_token (parser
->lexer
);
18165 while (token
->type
!= CPP_CLOSE_BRACE
)
18167 cp_decl_specifier_seq declspecs
;
18168 int decl_class_or_enum_p
;
18169 tree prefix_attributes
;
18171 cp_parser_objc_visibility_spec (parser
);
18173 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18176 cp_parser_decl_specifier_seq (parser
,
18177 CP_PARSER_FLAGS_OPTIONAL
,
18179 &decl_class_or_enum_p
);
18180 prefix_attributes
= declspecs
.attributes
;
18181 declspecs
.attributes
= NULL_TREE
;
18183 /* Keep going until we hit the `;' at the end of the
18185 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18187 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
18188 cp_declarator
*declarator
= NULL
;
18189 int ctor_dtor_or_conv_p
;
18191 /* Check for a (possibly unnamed) bitfield declaration. */
18192 token
= cp_lexer_peek_token (parser
->lexer
);
18193 if (token
->type
== CPP_COLON
)
18196 if (token
->type
== CPP_NAME
18197 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
18200 /* Get the name of the bitfield. */
18201 declarator
= make_id_declarator (NULL_TREE
,
18202 cp_parser_identifier (parser
),
18206 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
18207 /* Get the width of the bitfield. */
18209 = cp_parser_constant_expression (parser
,
18210 /*allow_non_constant=*/false,
18215 /* Parse the declarator. */
18217 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
18218 &ctor_dtor_or_conv_p
,
18219 /*parenthesized_p=*/NULL
,
18220 /*member_p=*/false);
18223 /* Look for attributes that apply to the ivar. */
18224 attributes
= cp_parser_attributes_opt (parser
);
18225 /* Remember which attributes are prefix attributes and
18227 first_attribute
= attributes
;
18228 /* Combine the attributes. */
18229 attributes
= chainon (prefix_attributes
, attributes
);
18233 /* Create the bitfield declaration. */
18234 decl
= grokbitfield (declarator
, &declspecs
, width
);
18235 cplus_decl_attributes (&decl
, attributes
, /*flags=*/0);
18238 decl
= grokfield (declarator
, &declspecs
,
18239 NULL_TREE
, /*init_const_expr_p=*/false,
18240 NULL_TREE
, attributes
);
18242 /* Add the instance variable. */
18243 objc_add_instance_variable (decl
);
18245 /* Reset PREFIX_ATTRIBUTES. */
18246 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
18247 attributes
= TREE_CHAIN (attributes
);
18249 TREE_CHAIN (attributes
) = NULL_TREE
;
18251 token
= cp_lexer_peek_token (parser
->lexer
);
18253 if (token
->type
== CPP_COMMA
)
18255 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
18261 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18262 token
= cp_lexer_peek_token (parser
->lexer
);
18265 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
18266 /* For historical reasons, we accept an optional semicolon. */
18267 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18268 cp_lexer_consume_token (parser
->lexer
);
18271 /* Parse an Objective-C protocol declaration. */
18274 cp_parser_objc_protocol_declaration (cp_parser
* parser
)
18276 tree proto
, protorefs
;
18279 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
18280 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
18282 error ("identifier expected after %<@protocol%>");
18286 /* See if we have a forward declaration or a definition. */
18287 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18289 /* Try a forward declaration first. */
18290 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
18292 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
18294 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18297 /* Ok, we got a full-fledged definition (or at least should). */
18300 proto
= cp_parser_identifier (parser
);
18301 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
18302 objc_start_protocol (proto
, protorefs
);
18303 cp_parser_objc_method_prototype_list (parser
);
18307 /* Parse an Objective-C superclass or category. */
18310 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
18313 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
18315 *super
= *categ
= NULL_TREE
;
18316 if (next
->type
== CPP_COLON
)
18318 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
18319 *super
= cp_parser_identifier (parser
);
18321 else if (next
->type
== CPP_OPEN_PAREN
)
18323 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
18324 *categ
= cp_parser_identifier (parser
);
18325 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18329 /* Parse an Objective-C class interface. */
18332 cp_parser_objc_class_interface (cp_parser
* parser
)
18334 tree name
, super
, categ
, protos
;
18336 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
18337 name
= cp_parser_identifier (parser
);
18338 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
18339 protos
= cp_parser_objc_protocol_refs_opt (parser
);
18341 /* We have either a class or a category on our hands. */
18343 objc_start_category_interface (name
, categ
, protos
);
18346 objc_start_class_interface (name
, super
, protos
);
18347 /* Handle instance variable declarations, if any. */
18348 cp_parser_objc_class_ivars (parser
);
18349 objc_continue_interface ();
18352 cp_parser_objc_method_prototype_list (parser
);
18355 /* Parse an Objective-C class implementation. */
18358 cp_parser_objc_class_implementation (cp_parser
* parser
)
18360 tree name
, super
, categ
;
18362 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
18363 name
= cp_parser_identifier (parser
);
18364 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
18366 /* We have either a class or a category on our hands. */
18368 objc_start_category_implementation (name
, categ
);
18371 objc_start_class_implementation (name
, super
);
18372 /* Handle instance variable declarations, if any. */
18373 cp_parser_objc_class_ivars (parser
);
18374 objc_continue_implementation ();
18377 cp_parser_objc_method_definition_list (parser
);
18380 /* Consume the @end token and finish off the implementation. */
18383 cp_parser_objc_end_implementation (cp_parser
* parser
)
18385 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
18386 objc_finish_implementation ();
18389 /* Parse an Objective-C declaration. */
18392 cp_parser_objc_declaration (cp_parser
* parser
)
18394 /* Try to figure out what kind of declaration is present. */
18395 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
18397 switch (kwd
->keyword
)
18400 cp_parser_objc_alias_declaration (parser
);
18403 cp_parser_objc_class_declaration (parser
);
18405 case RID_AT_PROTOCOL
:
18406 cp_parser_objc_protocol_declaration (parser
);
18408 case RID_AT_INTERFACE
:
18409 cp_parser_objc_class_interface (parser
);
18411 case RID_AT_IMPLEMENTATION
:
18412 cp_parser_objc_class_implementation (parser
);
18415 cp_parser_objc_end_implementation (parser
);
18418 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->u
.value
);
18419 cp_parser_skip_to_end_of_block_or_statement (parser
);
18423 /* Parse an Objective-C try-catch-finally statement.
18425 objc-try-catch-finally-stmt:
18426 @try compound-statement objc-catch-clause-seq [opt]
18427 objc-finally-clause [opt]
18429 objc-catch-clause-seq:
18430 objc-catch-clause objc-catch-clause-seq [opt]
18433 @catch ( exception-declaration ) compound-statement
18435 objc-finally-clause
18436 @finally compound-statement
18438 Returns NULL_TREE. */
18441 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
18442 location_t location
;
18445 cp_parser_require_keyword (parser
, RID_AT_TRY
, "`@try'");
18446 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18447 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18448 node, lest it get absorbed into the surrounding block. */
18449 stmt
= push_stmt_list ();
18450 cp_parser_compound_statement (parser
, NULL
, false);
18451 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
18453 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
18455 cp_parameter_declarator
*parmdecl
;
18458 cp_lexer_consume_token (parser
->lexer
);
18459 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
18460 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
18461 parm
= grokdeclarator (parmdecl
->declarator
,
18462 &parmdecl
->decl_specifiers
,
18463 PARM
, /*initialized=*/0,
18464 /*attrlist=*/NULL
);
18465 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18466 objc_begin_catch_clause (parm
);
18467 cp_parser_compound_statement (parser
, NULL
, false);
18468 objc_finish_catch_clause ();
18471 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
18473 cp_lexer_consume_token (parser
->lexer
);
18474 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18475 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18476 node, lest it get absorbed into the surrounding block. */
18477 stmt
= push_stmt_list ();
18478 cp_parser_compound_statement (parser
, NULL
, false);
18479 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
18482 return objc_finish_try_stmt ();
18485 /* Parse an Objective-C synchronized statement.
18487 objc-synchronized-stmt:
18488 @synchronized ( expression ) compound-statement
18490 Returns NULL_TREE. */
18493 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
18494 location_t location
;
18497 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, "`@synchronized'");
18499 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18500 cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('");
18501 lock
= cp_parser_expression (parser
, false);
18502 cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'");
18504 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18505 node, lest it get absorbed into the surrounding block. */
18506 stmt
= push_stmt_list ();
18507 cp_parser_compound_statement (parser
, NULL
, false);
18509 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
18512 /* Parse an Objective-C throw statement.
18515 @throw assignment-expression [opt] ;
18517 Returns a constructed '@throw' statement. */
18520 cp_parser_objc_throw_statement (cp_parser
*parser
) {
18521 tree expr
= NULL_TREE
;
18523 cp_parser_require_keyword (parser
, RID_AT_THROW
, "`@throw'");
18525 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18526 expr
= cp_parser_assignment_expression (parser
, false);
18528 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18530 return objc_build_throw_stmt (expr
);
18533 /* Parse an Objective-C statement. */
18536 cp_parser_objc_statement (cp_parser
* parser
) {
18537 /* Try to figure out what kind of declaration is present. */
18538 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
18540 switch (kwd
->keyword
)
18543 return cp_parser_objc_try_catch_finally_statement (parser
);
18544 case RID_AT_SYNCHRONIZED
:
18545 return cp_parser_objc_synchronized_statement (parser
);
18547 return cp_parser_objc_throw_statement (parser
);
18549 error ("misplaced %<@%D%> Objective-C++ construct", kwd
->u
.value
);
18550 cp_parser_skip_to_end_of_block_or_statement (parser
);
18553 return error_mark_node
;
18556 /* OpenMP 2.5 parsing routines. */
18558 /* Returns name of the next clause.
18559 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18560 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18561 returned and the token is consumed. */
18563 static pragma_omp_clause
18564 cp_parser_omp_clause_name (cp_parser
*parser
)
18566 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
18568 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
18569 result
= PRAGMA_OMP_CLAUSE_IF
;
18570 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
18571 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
18572 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
18573 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
18574 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18576 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
18577 const char *p
= IDENTIFIER_POINTER (id
);
18582 if (!strcmp ("copyin", p
))
18583 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
18584 else if (!strcmp ("copyprivate", p
))
18585 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
18588 if (!strcmp ("firstprivate", p
))
18589 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
18592 if (!strcmp ("lastprivate", p
))
18593 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
18596 if (!strcmp ("nowait", p
))
18597 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
18598 else if (!strcmp ("num_threads", p
))
18599 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
18602 if (!strcmp ("ordered", p
))
18603 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
18606 if (!strcmp ("reduction", p
))
18607 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
18610 if (!strcmp ("schedule", p
))
18611 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
18612 else if (!strcmp ("shared", p
))
18613 result
= PRAGMA_OMP_CLAUSE_SHARED
;
18618 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
18619 cp_lexer_consume_token (parser
->lexer
);
18624 /* Validate that a clause of the given type does not already exist. */
18627 check_no_duplicate_clause (tree clauses
, enum tree_code code
, const char *name
)
18631 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
18632 if (OMP_CLAUSE_CODE (c
) == code
)
18634 error ("too many %qs clauses", name
);
18642 variable-list , identifier
18644 In addition, we match a closing parenthesis. An opening parenthesis
18645 will have been consumed by the caller.
18647 If KIND is nonzero, create the appropriate node and install the decl
18648 in OMP_CLAUSE_DECL and add the node to the head of the list.
18650 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18651 return the list created. */
18654 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
18661 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
18662 /*check_dependency_p=*/true,
18663 /*template_p=*/NULL
,
18664 /*declarator_p=*/false,
18665 /*optional_p=*/false);
18666 if (name
== error_mark_node
)
18669 decl
= cp_parser_lookup_name_simple (parser
, name
);
18670 if (decl
== error_mark_node
)
18671 cp_parser_name_lookup_error (parser
, name
, decl
, NULL
);
18672 else if (kind
!= 0)
18674 tree u
= build_omp_clause (kind
);
18675 OMP_CLAUSE_DECL (u
) = decl
;
18676 OMP_CLAUSE_CHAIN (u
) = list
;
18680 list
= tree_cons (decl
, NULL_TREE
, list
);
18683 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18685 cp_lexer_consume_token (parser
->lexer
);
18688 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18692 /* Try to resync to an unnested comma. Copied from
18693 cp_parser_parenthesized_expression_list. */
18695 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
18696 /*recovering=*/true,
18698 /*consume_paren=*/true);
18706 /* Similarly, but expect leading and trailing parenthesis. This is a very
18707 common case for omp clauses. */
18710 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
18712 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18713 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
18718 default ( shared | none ) */
18721 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
)
18723 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
18726 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18728 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18730 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
18731 const char *p
= IDENTIFIER_POINTER (id
);
18736 if (strcmp ("none", p
) != 0)
18738 kind
= OMP_CLAUSE_DEFAULT_NONE
;
18742 if (strcmp ("shared", p
) != 0)
18744 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
18751 cp_lexer_consume_token (parser
->lexer
);
18756 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
18759 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18760 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18761 /*or_comma=*/false,
18762 /*consume_paren=*/true);
18764 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
18767 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default");
18768 c
= build_omp_clause (OMP_CLAUSE_DEFAULT
);
18769 OMP_CLAUSE_CHAIN (c
) = list
;
18770 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
18776 if ( expression ) */
18779 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
)
18783 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18786 t
= cp_parser_condition (parser
);
18788 if (t
== error_mark_node
18789 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18790 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18791 /*or_comma=*/false,
18792 /*consume_paren=*/true);
18794 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if");
18796 c
= build_omp_clause (OMP_CLAUSE_IF
);
18797 OMP_CLAUSE_IF_EXPR (c
) = t
;
18798 OMP_CLAUSE_CHAIN (c
) = list
;
18807 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18811 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait");
18813 c
= build_omp_clause (OMP_CLAUSE_NOWAIT
);
18814 OMP_CLAUSE_CHAIN (c
) = list
;
18819 num_threads ( expression ) */
18822 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
)
18826 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18829 t
= cp_parser_expression (parser
, false);
18831 if (t
== error_mark_node
18832 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18833 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18834 /*or_comma=*/false,
18835 /*consume_paren=*/true);
18837 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
, "num_threads");
18839 c
= build_omp_clause (OMP_CLAUSE_NUM_THREADS
);
18840 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
18841 OMP_CLAUSE_CHAIN (c
) = list
;
18850 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
18854 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
, "ordered");
18856 c
= build_omp_clause (OMP_CLAUSE_ORDERED
);
18857 OMP_CLAUSE_CHAIN (c
) = list
;
18862 reduction ( reduction-operator : variable-list )
18864 reduction-operator:
18865 One of: + * - & ^ | && || */
18868 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
18870 enum tree_code code
;
18873 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
18876 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
18888 code
= BIT_AND_EXPR
;
18891 code
= BIT_XOR_EXPR
;
18894 code
= BIT_IOR_EXPR
;
18897 code
= TRUTH_ANDIF_EXPR
;
18900 code
= TRUTH_ORIF_EXPR
;
18903 cp_parser_error (parser
, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18905 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
18906 /*or_comma=*/false,
18907 /*consume_paren=*/true);
18910 cp_lexer_consume_token (parser
->lexer
);
18912 if (!cp_parser_require (parser
, CPP_COLON
, "`:'"))
18915 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
18916 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
18917 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
18923 schedule ( schedule-kind )
18924 schedule ( schedule-kind , expression )
18927 static | dynamic | guided | runtime */
18930 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
)
18934 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
18937 c
= build_omp_clause (OMP_CLAUSE_SCHEDULE
);
18939 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18941 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
18942 const char *p
= IDENTIFIER_POINTER (id
);
18947 if (strcmp ("dynamic", p
) != 0)
18949 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
18953 if (strcmp ("guided", p
) != 0)
18955 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
18959 if (strcmp ("runtime", p
) != 0)
18961 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
18968 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
18969 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
18972 cp_lexer_consume_token (parser
->lexer
);
18974 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18976 cp_lexer_consume_token (parser
->lexer
);
18978 t
= cp_parser_assignment_expression (parser
, false);
18980 if (t
== error_mark_node
)
18982 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
18983 error ("schedule %<runtime%> does not take "
18984 "a %<chunk_size%> parameter");
18986 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
18988 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
18991 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`,' or `)'"))
18994 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule");
18995 OMP_CLAUSE_CHAIN (c
) = list
;
18999 cp_parser_error (parser
, "invalid schedule kind");
19001 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19002 /*or_comma=*/false,
19003 /*consume_paren=*/true);
19007 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19008 is a bitmask in MASK. Return the list of clauses found; the result
19009 of clause default goes in *pdefault. */
19012 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
19013 const char *where
, cp_token
*pragma_tok
)
19015 tree clauses
= NULL
;
19017 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
19019 pragma_omp_clause c_kind
= cp_parser_omp_clause_name (parser
);
19020 const char *c_name
;
19021 tree prev
= clauses
;
19025 case PRAGMA_OMP_CLAUSE_COPYIN
:
19026 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
19029 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
19030 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
19032 c_name
= "copyprivate";
19034 case PRAGMA_OMP_CLAUSE_DEFAULT
:
19035 clauses
= cp_parser_omp_clause_default (parser
, clauses
);
19036 c_name
= "default";
19038 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
19039 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
19041 c_name
= "firstprivate";
19043 case PRAGMA_OMP_CLAUSE_IF
:
19044 clauses
= cp_parser_omp_clause_if (parser
, clauses
);
19047 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
19048 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
19050 c_name
= "lastprivate";
19052 case PRAGMA_OMP_CLAUSE_NOWAIT
:
19053 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
);
19056 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
19057 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
);
19058 c_name
= "num_threads";
19060 case PRAGMA_OMP_CLAUSE_ORDERED
:
19061 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
);
19062 c_name
= "ordered";
19064 case PRAGMA_OMP_CLAUSE_PRIVATE
:
19065 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
19067 c_name
= "private";
19069 case PRAGMA_OMP_CLAUSE_REDUCTION
:
19070 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
19071 c_name
= "reduction";
19073 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
19074 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
);
19075 c_name
= "schedule";
19077 case PRAGMA_OMP_CLAUSE_SHARED
:
19078 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
19083 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
19087 if (((mask
>> c_kind
) & 1) == 0)
19089 /* Remove the invalid clause(s) from the list to avoid
19090 confusing the rest of the compiler. */
19092 error ("%qs is not valid for %qs", c_name
, where
);
19096 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
19097 return finish_omp_clauses (clauses
);
19104 In practice, we're also interested in adding the statement to an
19105 outer node. So it is convenient if we work around the fact that
19106 cp_parser_statement calls add_stmt. */
19109 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
19111 unsigned save
= parser
->in_statement
;
19113 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19114 This preserves the "not within loop or switch" style error messages
19115 for nonsense cases like
19121 if (parser
->in_statement
)
19122 parser
->in_statement
= IN_OMP_BLOCK
;
19128 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
19130 parser
->in_statement
= save
;
19134 cp_parser_omp_structured_block (cp_parser
*parser
)
19136 tree stmt
= begin_omp_structured_block ();
19137 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
19139 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
19141 cp_parser_end_omp_structured_block (parser
, save
);
19142 return finish_omp_structured_block (stmt
);
19146 # pragma omp atomic new-line
19150 x binop= expr | x++ | ++x | x-- | --x
19152 +, *, -, /, &, ^, |, <<, >>
19154 where x is an lvalue expression with scalar type. */
19157 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
19160 enum tree_code code
;
19162 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19164 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
19166 switch (TREE_CODE (lhs
))
19171 case PREINCREMENT_EXPR
:
19172 case POSTINCREMENT_EXPR
:
19173 lhs
= TREE_OPERAND (lhs
, 0);
19175 rhs
= integer_one_node
;
19178 case PREDECREMENT_EXPR
:
19179 case POSTDECREMENT_EXPR
:
19180 lhs
= TREE_OPERAND (lhs
, 0);
19182 rhs
= integer_one_node
;
19186 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
19192 code
= TRUNC_DIV_EXPR
;
19200 case CPP_LSHIFT_EQ
:
19201 code
= LSHIFT_EXPR
;
19203 case CPP_RSHIFT_EQ
:
19204 code
= RSHIFT_EXPR
;
19207 code
= BIT_AND_EXPR
;
19210 code
= BIT_IOR_EXPR
;
19213 code
= BIT_XOR_EXPR
;
19216 cp_parser_error (parser
,
19217 "invalid operator for %<#pragma omp atomic%>");
19220 cp_lexer_consume_token (parser
->lexer
);
19222 rhs
= cp_parser_expression (parser
, false);
19223 if (rhs
== error_mark_node
)
19227 finish_omp_atomic (code
, lhs
, rhs
);
19228 cp_parser_consume_semicolon_at_end_of_statement (parser
);
19232 cp_parser_skip_to_end_of_block_or_statement (parser
);
19237 # pragma omp barrier new-line */
19240 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
19242 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19243 finish_omp_barrier ();
19247 # pragma omp critical [(name)] new-line
19248 structured-block */
19251 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
19253 tree stmt
, name
= NULL
;
19255 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
19257 cp_lexer_consume_token (parser
->lexer
);
19259 name
= cp_parser_identifier (parser
);
19261 if (name
== error_mark_node
19262 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
19263 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19264 /*or_comma=*/false,
19265 /*consume_paren=*/true);
19266 if (name
== error_mark_node
)
19269 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19271 stmt
= cp_parser_omp_structured_block (parser
);
19272 return c_finish_omp_critical (stmt
, name
);
19276 # pragma omp flush flush-vars[opt] new-line
19279 ( variable-list ) */
19282 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
19284 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
19285 (void) cp_parser_omp_var_list (parser
, 0, NULL
);
19286 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19288 finish_omp_flush ();
19291 /* Parse the restricted form of the for statment allowed by OpenMP. */
19294 cp_parser_omp_for_loop (cp_parser
*parser
)
19296 tree init
, cond
, incr
, body
, decl
, pre_body
;
19299 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
19301 cp_parser_error (parser
, "for statement expected");
19304 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
19305 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, "`('"))
19308 init
= decl
= NULL
;
19309 pre_body
= push_stmt_list ();
19310 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19312 cp_decl_specifier_seq type_specifiers
;
19314 /* First, try to parse as an initialized declaration. See
19315 cp_parser_condition, from whence the bulk of this is copied. */
19317 cp_parser_parse_tentatively (parser
);
19318 cp_parser_type_specifier_seq (parser
, /*is_condition=*/false,
19320 if (!cp_parser_error_occurred (parser
))
19322 tree asm_specification
, attributes
;
19323 cp_declarator
*declarator
;
19325 declarator
= cp_parser_declarator (parser
,
19326 CP_PARSER_DECLARATOR_NAMED
,
19327 /*ctor_dtor_or_conv_p=*/NULL
,
19328 /*parenthesized_p=*/NULL
,
19329 /*member_p=*/false);
19330 attributes
= cp_parser_attributes_opt (parser
);
19331 asm_specification
= cp_parser_asm_specification_opt (parser
);
19333 cp_parser_require (parser
, CPP_EQ
, "`='");
19334 if (cp_parser_parse_definitely (parser
))
19338 decl
= start_decl (declarator
, &type_specifiers
,
19339 /*initialized_p=*/false, attributes
,
19340 /*prefix_attributes=*/NULL_TREE
,
19343 init
= cp_parser_assignment_expression (parser
, false);
19345 cp_finish_decl (decl
, NULL_TREE
, /*init_const_expr_p=*/false,
19346 asm_specification
, LOOKUP_ONLYCONVERTING
);
19349 pop_scope (pushed_scope
);
19353 cp_parser_abort_tentative_parse (parser
);
19355 /* If parsing as an initialized declaration failed, try again as
19356 a simple expression. */
19358 init
= cp_parser_expression (parser
, false);
19360 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
19361 pre_body
= pop_stmt_list (pre_body
);
19364 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19365 cond
= cp_parser_condition (parser
);
19366 cp_parser_require (parser
, CPP_SEMICOLON
, "`;'");
19369 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
19370 incr
= cp_parser_expression (parser
, false);
19372 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, "`)'"))
19373 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
19374 /*or_comma=*/false,
19375 /*consume_paren=*/true);
19377 /* Note that we saved the original contents of this flag when we entered
19378 the structured block, and so we don't need to re-save it here. */
19379 parser
->in_statement
= IN_OMP_FOR
;
19381 /* Note that the grammar doesn't call for a structured block here,
19382 though the loop as a whole is a structured block. */
19383 body
= push_stmt_list ();
19384 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
19385 body
= pop_stmt_list (body
);
19387 return finish_omp_for (loc
, decl
, init
, cond
, incr
, body
, pre_body
);
19391 #pragma omp for for-clause[optseq] new-line
19394 #define OMP_FOR_CLAUSE_MASK \
19395 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19396 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19397 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19398 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19399 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
19400 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
19401 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19404 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
19406 tree clauses
, sb
, ret
;
19409 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
19410 "#pragma omp for", pragma_tok
);
19412 sb
= begin_omp_structured_block ();
19413 save
= cp_parser_begin_omp_structured_block (parser
);
19415 ret
= cp_parser_omp_for_loop (parser
);
19417 OMP_FOR_CLAUSES (ret
) = clauses
;
19419 cp_parser_end_omp_structured_block (parser
, save
);
19420 add_stmt (finish_omp_structured_block (sb
));
19426 # pragma omp master new-line
19427 structured-block */
19430 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
19432 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19433 return c_finish_omp_master (cp_parser_omp_structured_block (parser
));
19437 # pragma omp ordered new-line
19438 structured-block */
19441 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
19443 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19444 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser
));
19450 { section-sequence }
19453 section-directive[opt] structured-block
19454 section-sequence section-directive structured-block */
19457 cp_parser_omp_sections_scope (cp_parser
*parser
)
19459 tree stmt
, substmt
;
19460 bool error_suppress
= false;
19463 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, "`{'"))
19466 stmt
= push_stmt_list ();
19468 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
19472 substmt
= begin_omp_structured_block ();
19473 save
= cp_parser_begin_omp_structured_block (parser
);
19477 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
19479 tok
= cp_lexer_peek_token (parser
->lexer
);
19480 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
19482 if (tok
->type
== CPP_CLOSE_BRACE
)
19484 if (tok
->type
== CPP_EOF
)
19488 cp_parser_end_omp_structured_block (parser
, save
);
19489 substmt
= finish_omp_structured_block (substmt
);
19490 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
19491 add_stmt (substmt
);
19496 tok
= cp_lexer_peek_token (parser
->lexer
);
19497 if (tok
->type
== CPP_CLOSE_BRACE
)
19499 if (tok
->type
== CPP_EOF
)
19502 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
19504 cp_lexer_consume_token (parser
->lexer
);
19505 cp_parser_require_pragma_eol (parser
, tok
);
19506 error_suppress
= false;
19508 else if (!error_suppress
)
19510 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
19511 error_suppress
= true;
19514 substmt
= cp_parser_omp_structured_block (parser
);
19515 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
19516 add_stmt (substmt
);
19518 cp_parser_require (parser
, CPP_CLOSE_BRACE
, "`}'");
19520 substmt
= pop_stmt_list (stmt
);
19522 stmt
= make_node (OMP_SECTIONS
);
19523 TREE_TYPE (stmt
) = void_type_node
;
19524 OMP_SECTIONS_BODY (stmt
) = substmt
;
19531 # pragma omp sections sections-clause[optseq] newline
19534 #define OMP_SECTIONS_CLAUSE_MASK \
19535 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19536 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19537 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19538 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19539 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19542 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
19546 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
19547 "#pragma omp sections", pragma_tok
);
19549 ret
= cp_parser_omp_sections_scope (parser
);
19551 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
19557 # pragma parallel parallel-clause new-line
19558 # pragma parallel for parallel-for-clause new-line
19559 # pragma parallel sections parallel-sections-clause new-line */
19561 #define OMP_PARALLEL_CLAUSE_MASK \
19562 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19563 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19564 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19565 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19566 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19567 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19568 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19569 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19572 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
19574 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
19575 const char *p_name
= "#pragma omp parallel";
19576 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
19577 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
19580 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
19582 cp_lexer_consume_token (parser
->lexer
);
19583 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
19584 p_name
= "#pragma omp parallel for";
19585 mask
|= OMP_FOR_CLAUSE_MASK
;
19586 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
19588 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19590 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
19591 const char *p
= IDENTIFIER_POINTER (id
);
19592 if (strcmp (p
, "sections") == 0)
19594 cp_lexer_consume_token (parser
->lexer
);
19595 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
19596 p_name
= "#pragma omp parallel sections";
19597 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
19598 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
19602 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
19603 block
= begin_omp_parallel ();
19604 save
= cp_parser_begin_omp_structured_block (parser
);
19608 case PRAGMA_OMP_PARALLEL
:
19609 cp_parser_already_scoped_statement (parser
);
19610 par_clause
= clauses
;
19613 case PRAGMA_OMP_PARALLEL_FOR
:
19614 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
19615 stmt
= cp_parser_omp_for_loop (parser
);
19617 OMP_FOR_CLAUSES (stmt
) = ws_clause
;
19620 case PRAGMA_OMP_PARALLEL_SECTIONS
:
19621 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
19622 stmt
= cp_parser_omp_sections_scope (parser
);
19624 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
19628 gcc_unreachable ();
19631 cp_parser_end_omp_structured_block (parser
, save
);
19632 stmt
= finish_omp_parallel (par_clause
, block
);
19633 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
19634 OMP_PARALLEL_COMBINED (stmt
) = 1;
19639 # pragma omp single single-clause[optseq] new-line
19640 structured-block */
19642 #define OMP_SINGLE_CLAUSE_MASK \
19643 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19644 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19645 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19646 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19649 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
19651 tree stmt
= make_node (OMP_SINGLE
);
19652 TREE_TYPE (stmt
) = void_type_node
;
19654 OMP_SINGLE_CLAUSES (stmt
)
19655 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
19656 "#pragma omp single", pragma_tok
);
19657 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
19659 return add_stmt (stmt
);
19663 # pragma omp threadprivate (variable-list) */
19666 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
19670 vars
= cp_parser_omp_var_list (parser
, 0, NULL
);
19671 cp_parser_require_pragma_eol (parser
, pragma_tok
);
19673 finish_omp_threadprivate (vars
);
19676 /* Main entry point to OpenMP statement pragmas. */
19679 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
19683 switch (pragma_tok
->pragma_kind
)
19685 case PRAGMA_OMP_ATOMIC
:
19686 cp_parser_omp_atomic (parser
, pragma_tok
);
19688 case PRAGMA_OMP_CRITICAL
:
19689 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
19691 case PRAGMA_OMP_FOR
:
19692 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
19694 case PRAGMA_OMP_MASTER
:
19695 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
19697 case PRAGMA_OMP_ORDERED
:
19698 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
19700 case PRAGMA_OMP_PARALLEL
:
19701 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
19703 case PRAGMA_OMP_SECTIONS
:
19704 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
19706 case PRAGMA_OMP_SINGLE
:
19707 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
19710 gcc_unreachable ();
19714 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
19719 static GTY (()) cp_parser
*the_parser
;
19722 /* Special handling for the first token or line in the file. The first
19723 thing in the file might be #pragma GCC pch_preprocess, which loads a
19724 PCH file, which is a GC collection point. So we need to handle this
19725 first pragma without benefit of an existing lexer structure.
19727 Always returns one token to the caller in *FIRST_TOKEN. This is
19728 either the true first token of the file, or the first token after
19729 the initial pragma. */
19732 cp_parser_initial_pragma (cp_token
*first_token
)
19736 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19737 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
19740 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19741 if (first_token
->type
== CPP_STRING
)
19743 name
= first_token
->u
.value
;
19745 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19746 if (first_token
->type
!= CPP_PRAGMA_EOL
)
19747 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19750 error ("expected string literal");
19752 /* Skip to the end of the pragma. */
19753 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
19754 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19756 /* Now actually load the PCH file. */
19758 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
19760 /* Read one more token to return to our caller. We have to do this
19761 after reading the PCH file in, since its pointers have to be
19763 cp_lexer_get_preprocessor_token (NULL
, first_token
);
19766 /* Normal parsing of a pragma token. Here we can (and must) use the
19770 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
19772 cp_token
*pragma_tok
;
19775 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
19776 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
19777 parser
->lexer
->in_pragma
= true;
19779 id
= pragma_tok
->pragma_kind
;
19782 case PRAGMA_GCC_PCH_PREPROCESS
:
19783 error ("%<#pragma GCC pch_preprocess%> must be first");
19786 case PRAGMA_OMP_BARRIER
:
19789 case pragma_compound
:
19790 cp_parser_omp_barrier (parser
, pragma_tok
);
19793 error ("%<#pragma omp barrier%> may only be "
19794 "used in compound statements");
19801 case PRAGMA_OMP_FLUSH
:
19804 case pragma_compound
:
19805 cp_parser_omp_flush (parser
, pragma_tok
);
19808 error ("%<#pragma omp flush%> may only be "
19809 "used in compound statements");
19816 case PRAGMA_OMP_THREADPRIVATE
:
19817 cp_parser_omp_threadprivate (parser
, pragma_tok
);
19820 case PRAGMA_OMP_ATOMIC
:
19821 case PRAGMA_OMP_CRITICAL
:
19822 case PRAGMA_OMP_FOR
:
19823 case PRAGMA_OMP_MASTER
:
19824 case PRAGMA_OMP_ORDERED
:
19825 case PRAGMA_OMP_PARALLEL
:
19826 case PRAGMA_OMP_SECTIONS
:
19827 case PRAGMA_OMP_SINGLE
:
19828 if (context
== pragma_external
)
19830 cp_parser_omp_construct (parser
, pragma_tok
);
19833 case PRAGMA_OMP_SECTION
:
19834 error ("%<#pragma omp section%> may only be used in "
19835 "%<#pragma omp sections%> construct");
19839 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
19840 c_invoke_pragma_handler (id
);
19844 cp_parser_error (parser
, "expected declaration specifiers");
19848 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
19852 /* The interface the pragma parsers have to the lexer. */
19855 pragma_lex (tree
*value
)
19858 enum cpp_ttype ret
;
19860 tok
= cp_lexer_peek_token (the_parser
->lexer
);
19863 *value
= tok
->u
.value
;
19865 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
19867 else if (ret
== CPP_STRING
)
19868 *value
= cp_parser_string_literal (the_parser
, false, false);
19871 cp_lexer_consume_token (the_parser
->lexer
);
19872 if (ret
== CPP_KEYWORD
)
19880 /* External interface. */
19882 /* Parse one entire translation unit. */
19885 c_parse_file (void)
19887 bool error_occurred
;
19888 static bool already_called
= false;
19890 if (already_called
)
19892 sorry ("inter-module optimizations not implemented for C++");
19895 already_called
= true;
19897 the_parser
= cp_parser_new ();
19898 push_deferring_access_checks (flag_access_control
19899 ? dk_no_deferred
: dk_no_check
);
19900 error_occurred
= cp_parser_translation_unit (the_parser
);
19904 #include "gt-cp-parser.h"